PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.1
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.1
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 3.10.4 All 148 releases
visualizer / classes / Visualizer / Render / Layout.php

Layout.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.4.1, at classes/Visualizer/Render/Layout.php

462 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | Copyright 2013 Madpixels (email : visualizer@madpixels.net) |
5 // +----------------------------------------------------------------------+
6 // | This program is free software; you can redistribute it and/or modify |
7 // | it under the terms of the GNU General Public License, version 2, as |
8 // | published by the Free Software Foundation. |
9 // | |
10 // | This program is distributed in the hope that it will be useful, |
11 // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 // | GNU General Public License for more details. |
14 // | |
15 // | You should have received a copy of the GNU General Public License |
16 // | along with this program; if not, write to the Free Software |
17 // | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
18 // | MA 02110-1301 USA |
19 // +----------------------------------------------------------------------+
20 // | Author: Eugene Manuilov <eugene@manuilov.org> |
21 // +----------------------------------------------------------------------+
22 /**
23 * Layout rendering class.
24 *
25 * @category Visualizer
26 * @package Render
27 */
28 class Visualizer_Render_Layout extends Visualizer_Render {
29
30 /**
31 * Renders template.
32 *
33 * @since 1.0.0
34 *
35 * @abstract
36 * @access protected
37 */
38 protected function _toHTML() {
39 // empty.
40 }
41
42 /**
43 * Show the layout by delegating the call to the layout-specific method with the params.
44 *
45 * @access public
46 */
47 public static function show( $layout ) {
48 return call_user_func( array( __CLASS__, '_render' . str_replace( ' ', '', ucwords( str_replace( '-', ' ', $layout ) ) ) ), func_get_args() );
49 }
50
51 /**
52 * Show the DB query box.
53 *
54 * @access public
55 */
56 public static function _renderDbQuery( $args ) {
57 $query = $args[1];
58 ?>
59 <div id='visualizer-db-query' style="display: none">
60 <div class="visualizer-db-query-form">
61 <div>
62 <form id='db-query-form'>
63 <input type="hidden" name="chart_id" value="<?php echo $args[2]; ?>">
64 <?php do_action( 'visualizer_db_query_add_layout', $args ); ?>
65 <textarea name='query' class='visualizer-db-query' placeholder="<?php _e( 'Your query goes here', 'visualizer' ); ?>"><?php echo $query; ?></textarea>
66 </form>
67 <div class='db-wizard-error'></div>
68 </div>
69 <div>
70 <input type="button" class="button button-primary" id='visualizer-query-fetch' value='<?php _e( 'Show Results', 'visualizer' ); ?>'>
71 </div>
72 </div>
73 <div class='db-wizard-hints'>
74 <ul>
75 <li><?php echo sprintf( __( 'For examples of queries and links to resources that you can use with this feature, please click %1$shere%2$s', 'visualizer' ), '<a href="' . VISUALIZER_DB_QUERY_DOC_URL . '" target="_blank">', '</a>' ); ?></li>
76 <li><?php echo sprintf( __( 'Use %1$sControl+Space%2$s for autocompleting keywords or table names.', 'visualizer' ), '<span class="visualizer-emboss">', '</span>' ); ?></li>
77 <?php do_action( 'visualizer_db_query_add_hints', $args ); ?>
78 </ul>
79 </div>
80 <div class='db-wizard-results'></div>
81
82 </div>
83 <?php
84 }
85
86 /**
87 * Show the DB wizard's results table.
88 *
89 * @access public
90 */
91 public static function _renderDbWizardResults( $args ) {
92 $headers = $args[1];
93 $results = $args[2];
94 ob_start();
95 ?>
96 <table cellspacing="0" width="100%" id="results">
97 <thead>
98 <tr>
99 <?php
100 foreach ( $headers as $header ) {
101 echo '<th>' . $header['label'] . '</th>';
102 }
103 ?>
104 </tr>
105 </thead>
106 <tfoot>
107 </tfoot>
108 <tbody>
109 <?php
110 foreach ( $results as $result ) {
111 echo '<tr>';
112 foreach ( $result as $r ) {
113 echo '<td>' . $r . '</td>';
114 }
115 echo '</tr>';
116 }
117 ?>
118 </tbody>
119 </table>
120 <?php
121 return ob_get_clean();
122 }
123
124 /**
125 * Show the JSON/REST parameters boxes.
126 *
127 * @access public
128 */
129 public static function _renderJsonScreen( $args ) {
130 $id = $args[1];
131 $action = add_query_arg(
132 array(
133 'action' => Visualizer_Plugin::ACTION_JSON_SET_DATA,
134 'security' => wp_create_nonce( Visualizer_Plugin::ACTION_JSON_SET_DATA . Visualizer_Plugin::VERSION ),
135 'chart' => $id,
136 ),
137 admin_url( 'admin-ajax.php' )
138 );
139
140 $url = get_post_meta( $id, Visualizer_Plugin::CF_JSON_URL, true );
141 $root = get_post_meta( $id, Visualizer_Plugin::CF_JSON_ROOT, true );
142 $paging = get_post_meta( $id, Visualizer_Plugin::CF_JSON_PAGING, true );
143 $headers = get_post_meta( $id, Visualizer_Plugin::CF_JSON_HEADERS, true );
144 if ( empty( $headers['method'] ) ) {
145 $headers['method'] = 'get';
146 }
147 $methods = apply_filters( 'visualizer_json_request_methods', array( 'GET', 'POST' ) );
148 ?>
149 <div id="visualizer-json-screen" style="display: none">
150 <div class="visualizer-json-form">
151 <h3 class="viz-step step1"><?php _e( 'STEP 1: Specify the JSON endpoint/URL', 'visualizer' ); ?></h3>
152 <div>
153 <form id="json-endpoint-form">
154 <div class="json-wizard-hints">
155 <ul class="info">
156 <li><?php echo sprintf( __( 'If you want to add authentication or headers to the endpoint or change the request in any way, please refer to our document %1$shere%2$s.', 'visualizer' ), '<a href="https://docs.themeisle.com/article/1043-visualizer-how-to-extend-rest-endpoints-with-json-response" target="_blank">', '</a>' ); ?></li>
157 </ul>
158 </div>
159
160 <input
161 type="url"
162 id="vz-import-json-url"
163 name="url"
164 value="<?php echo esc_url( $url ); ?>"
165 placeholder="<?php esc_html_e( 'Please enter the URL', 'visualizer' ); ?>"
166 class="visualizer-input json-form-element">
167 <button class="button button-secondary button-small" id="visualizer-json-fetch"><?php esc_html_e( 'Fetch Endpoint', 'visualizer' ); ?></button>
168
169 <div class="visualizer-json-subform">
170 <h3 class="viz-substep"><?php _e( 'Headers', 'visualizer' ); ?></h3>
171 <div class="json-wizard-headers">
172 <div class="json-wizard-header">
173 <div><?php _e( 'Request Type', 'visualizer' ); ?></div>
174 <div>
175 <select name="method" class="json-form-element">
176 <?php foreach ( $methods as $method ) { ?>
177 <option value="<?php echo $method; ?>" <?php selected( $headers['method'], $method ); ?>><?php echo $method; ?></option>
178 <?php } ?>
179 </select>
180 </div>
181 </div>
182 <div class="json-wizard-header">
183 <div><?php _e( 'Credentials', 'visualizer' ); ?></div>
184 <div>
185 <input
186 type="text"
187 id="vz-import-json-username"
188 name="username"
189 value="<?php echo ( array_key_exists( 'auth', $headers ) && array_key_exists( 'username', $headers['auth'] ) ? $headers['auth']['username'] : '' ); ?>"
190 placeholder="<?php esc_html_e( 'Username/Access Key', 'visualizer' ); ?>"
191 class="json-form-element">
192 &
193 <input
194 type="password"
195 id="vz-import-json-password"
196 name="password"
197 value="<?php echo ( array_key_exists( 'auth', $headers ) && array_key_exists( 'password', $headers['auth'] ) ? $headers['auth']['password'] : '' ); ?>"
198 placeholder="<?php esc_html_e( 'Password/Secret Key', 'visualizer' ); ?>"
199 class="json-form-element">
200 </div>
201 </div>
202 <div class="json-wizard-header">
203 <div></div>
204 <div><?php esc_html_e( 'OR', 'visualizer' ); ?></div>
205 </div>
206 <div class="json-wizard-header">
207 <div><?php esc_html_e( 'Authorization', 'visualizer' ); ?></div>
208 <div>
209 <input
210 type="text"
211 id="vz-import-json-auth"
212 name="auth"
213 value="<?php echo ( ! empty( $headers ) && array_key_exists( 'auth', $headers ) ? $headers['auth']['auth'] : '' ); ?>"
214 placeholder="<?php esc_html_e( 'e.g. SharedKey <AccountName>:<Signature>', 'visualizer' ); ?>"
215 class="visualizer-input json-form-element">
216 </div>
217 </div>
218 </div>
219 </div>
220 </form>
221 </div>
222 <h3 class="viz-step step2 <?php echo empty( $url ) ? 'ui-state-disabled' : ''; ?> json-root-form"><?php _e( 'STEP 2: Choose the JSON root', 'visualizer' ); ?></h3>
223 <div>
224 <form id="json-root-form">
225 <input type="hidden" name="chart" value="<?php echo $id; ?>">
226 <select name="root" id="vz-import-json-root" class="json-form-element">
227 <?php
228 if ( ! empty( $root ) ) {
229 ?>
230 <option value="<?php echo esc_attr( $root ); ?>"><?php echo str_replace( Visualizer_Source_Json::TAG_SEPARATOR, Visualizer_Source_Json::TAG_SEPARATOR_VIEW, $root ); ?></option>
231 <?php
232 }
233 ?>
234 </select>
235 <button class="button button-secondary button-small" id="visualizer-json-parse"><?php esc_html_e( 'Parse Endpoint', 'visualizer' ); ?></button>
236 </form>
237 </div>
238 <h3 class="viz-step step3 <?php echo empty( $root ) ? 'ui-state-disabled' : ''; ?>"><?php _e( 'STEP 3: Specify miscellaneous parameters', 'visualizer' ); ?></h3>
239 <div>
240 <form id="json-conclude-form-helper">
241 <div class="<?php echo apply_filters( 'visualizer_pro_upsell_class', 'only-pro-feature' ); ?>">
242 <div class="json-pagination">
243 <select name="paging" id="vz-import-json-paging" class="json-form-element" data-template='<?php echo sprintf( 'Get results from the first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), '?' ); ?>'>
244 <option value="0" class="static"><?php _e( 'Get results from the first page only', 'visualizer' ); ?></option>
245 <?php
246 if ( ! empty( $paging ) ) {
247 ?>
248 <option value="<?php echo esc_attr( $paging ); ?>"><?php echo sprintf( 'Get results from the first %d pages using %s', apply_filters( 'visualizer_json_fetch_pages', 5, $url ), str_replace( Visualizer_Source_Json::TAG_SEPARATOR, Visualizer_Source_Json::TAG_SEPARATOR_VIEW, $paging ) ); ?></option>
249 <?php
250 }
251 ?>
252 </select>
253 <?php
254 if ( ! Visualizer_Module::is_pro() ) {
255 ?>
256 <br/>
257 <br/>
258 <br/>
259 <br/>
260 <?php
261 }
262 ?>
263 <?php echo apply_filters( 'visualizer_pro_upsell', '' ); ?>
264 </div>
265 </div>
266 </form>
267 </div>
268 <h3 class="viz-step step4 ui-state-disabled"><?php _e( 'STEP 4: Select the data to display in the chart', 'visualizer' ); ?></h3>
269 <div>
270 <form id="json-conclude-form" action="<?php echo $action; ?>" method="post" target="thehole">
271 <div class="json-wizard-hints html-table-editor-hints">
272 <ul class="info">
273 <li><?php _e( 'If you see Invalid Data in the table, you may have selected the wrong root to fetch data from. Please select an alternative from the JSON root dropdown.', 'visualizer' ); ?></li>
274 <li><?php _e( 'Select whether to include the data in the chart. Each column selected will form one series.', 'visualizer' ); ?></li>
275 <li><?php _e( 'If a column is selected to be included, specify its data type.', 'visualizer' ); ?></li>
276 <li><?php _e( 'You can use drag/drop to reorder the columns but this column position is not saved. So when you reload the table, you may have to reorder again.', 'visualizer' ); ?></li>
277 <li><?php _e( 'You can select any number of columns but the chart type selected will determine how many will display in the chart.', 'visualizer' ); ?></li>
278 <li><?php _e( 'Once you have made your selection, click \'Show Chart\' on the right to view the chart.', 'visualizer' ); ?></li>
279 </ul>
280 </div>
281 <div class="json-table"></div>
282 <button class="button button-primary" style="display: none" id="visualizer-json-conclude"></button>
283 </form>
284 </div>
285 </div>
286 </div>
287 <?php
288 }
289
290 /**
291 * Show the simple editor(s).
292 *
293 * @access public
294 */
295 public static function _renderSimpleEditorScreen( $args ) {
296 $chart_id = $args[1];
297 $action = add_query_arg(
298 array(
299 'action' => Visualizer_Plugin::ACTION_UPLOAD_DATA,
300 'nonce' => wp_create_nonce(),
301 'chart' => $chart_id,
302 ),
303 admin_url( 'admin-ajax.php' )
304 );
305 ?>
306 <div class="viz-simple-editor">
307 <div class="viz-simple-editor-type viz-table-editor">
308 <form id="table-editor-form" action="<?php echo $action; ?>" method="post" target="thehole">
309 <div class="html-table-editor-hints">
310 <ul class="info">
311 <li><?php _e( 'Select whether to include the data in the chart. Each column selected will form one series.', 'visualizer' ); ?></li>
312 <li><?php _e( 'If a column is selected to be included, specify its data type.', 'visualizer' ); ?></li>
313 <li><?php _e( 'You can use drag/drop to reorder the columns but this column position is not saved. So when you reload the table, you may have to reorder again.', 'visualizer' ); ?></li>
314 <li><?php _e( 'You can select any number of columns but the chart type selected will determine how many will display in the chart.', 'visualizer' ); ?></li>
315 </ul>
316 </div>
317 <div class="viz-html-table">
318 <?php Visualizer_Render_Layout::show( 'editor-table', null, $chart_id, 'viz-html-table', true, true ); ?>
319 </div>
320 <input type="hidden" name="table_data" value="yes">
321 <input type="hidden" name="editor-type" value="table">
322 </form>
323 </div>
324
325 <?php Visualizer_Render_Layout::show( 'text-editor', $chart_id ); ?>
326
327 </div>
328 <?php
329
330 }
331
332 /**
333 * Show the text area editor.
334 *
335 * @access public
336 */
337 public static function _renderTextEditor( $args ) {
338 $chart_id = $args[1];
339 $csv = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA_AS, array(), $chart_id, 'csv-display' );
340 $data = '';
341 if ( ! empty( $csv ) && isset( $csv['string'] ) ) {
342 $data = str_replace( PHP_EOL, "\n", $csv['string'] );
343 }
344 ?>
345 <div class="viz-simple-editor-type viz-text-editor">
346 <textarea id="edited_text"><?php echo $data; ?></textarea>
347 </div>
348 <?php
349 }
350
351 /**
352 * Show the JSON endpoint's parsed table.
353 *
354 * @access public
355 */
356 public static function _renderEditorTable( $args ) {
357 $data = $args[1];
358 $chart_id = $args[2];
359 $class = $args[3];
360 $echo = $args[4];
361 $editable_data = $args[5];
362 $series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
363 $headers = array();
364
365 if ( is_null( $data ) ) {
366 foreach ( $series as $column ) {
367 $headers[] = $column['label'];
368 }
369 $chart = get_post( $chart_id );
370 $type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
371 $data = apply_filters( Visualizer_Plugin::FILTER_GET_CHART_DATA, unserialize( str_replace( "'", "\'", html_entity_decode( $chart->post_content ) ) ), $type );
372 } else {
373 $headers = array_keys( $data[0] );
374 }
375
376 $classes = implode( ' ', array( 'viz-editor-table', $class ) );
377
378 if ( $series ) {
379 $temp = $series;
380 $series = array();
381 foreach ( $temp as $array ) {
382 $series[ $array['label'] ] = $array['type'];
383 }
384 }
385 if ( ! $echo ) {
386 ob_start();
387 }
388 ?>
389 <table cellspacing="0" width="100%" class="results cell-border stripe <?php echo $classes; ?>">
390 <thead>
391 <tr>
392 <th><?php _e( 'Label', 'visualizer' ); ?></th>
393 <?php
394 foreach ( $headers as $header ) {
395 if ( $editable_data ) {
396 echo '<th><input type="text" name="header[]" value="' . esc_attr( $header ) . '"></th>';
397 } else {
398 echo '<th>' . $header . '</th>';
399 }
400 }
401 ?>
402 </tr>
403 </thead>
404 <tbody>
405 <tr>
406 <th><?php _e( 'Data Type', 'visualizer' ); ?></th>
407 <?php
408 $index = 0;
409 foreach ( $headers as $header ) {
410 echo '<td>';
411 if ( $editable_data ) {
412 echo '<select name="type[]">';
413 } else {
414 echo '<input name="header[]" type="hidden" value="' . $header . '">';
415 echo '<select name="type[' . $header . ']" class="viz-select-data-type">';
416 }
417 echo '<option value="" title="' . __( 'Exclude from chart', 'visualizer' ) . '">' . __( 'Exclude', 'visualizer' ) . '</option>';
418 echo '<option value="0" disabled title="' . __( 'Include in chart and select data type', 'visualizer' ) . '">--' . __( 'OR', 'visualizer' ) . '--</option>';
419
420 foreach ( Visualizer_Source::getAllowedTypes() as $type ) {
421 $selected = array_key_exists( $header, $series ) && $type === $series[ $header ] ? 'selected' : '';
422 echo '<option value="' . $type . '" ' . $selected . '>' . $type . '</option>';
423 }
424
425 echo '</select></td>';
426 }
427 ?>
428 </tr>
429 <?php
430 // for remote sources, the data exists inside 'data'.
431 if ( array_key_exists( 'data', $data ) ) {
432 $data = $data['data'];
433 }
434
435 foreach ( $data as $row ) {
436 echo '<tr>';
437 echo '<th>' . __( 'Value', 'visualizer' ) . '</th>';
438 $index = 0;
439 if ( empty( $row ) ) {
440 echo '<td></td>';
441 continue;
442 }
443 foreach ( array_values( $row ) as $value ) {
444 if ( $editable_data ) {
445 echo '<td><input type="text" name="data' . $index++ . '[]" value="' . esc_attr( stripslashes( $value ) ) . '"></td>';
446 } else {
447 echo '<td>' . ( is_array( $value ) ? __( 'Invalid Data', 'visualizer' ) : $value ) . '</td>';
448 }
449 }
450
451 echo '</tr>';
452 }
453 ?>
454 </tbody>
455 </table>
456 <?php
457 if ( ! $echo ) {
458 return ob_get_clean();
459 }
460 }
461 }
462