WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-dynamic-table.php
Go to the documentation of this file.
1 <?php
2 
21 
27  const COLUMN_ROW_MANAGE = '_wpdk_dt_column_row_manage';
28 
36  public $sortable;
37 
46  private $_id;
47 
55  private $_class;
56 
64  private $_columns;
65 
73  private $_items;
74 
75 
87  public function __construct( $id, $columns, $items ) {
88 
89  $this->_columns = $columns;
90  $this->_items = $items;
91  $this->_class = '';
92 
93  $this->sortable = false;
94 
95  /* Added dynamic + */
96  $this->_columns[self::COLUMN_ROW_MANAGE] = '';
97 
98  // Backward compatibility
99  wp_enqueue_script( 'wpdk-dynamic-table', WPDK_URI_JAVASCRIPT . 'wpdk-dynamic-table.js', array(), WPDK_VERSION );
100  wp_enqueue_style( 'wpdk-dynamic-table', WPDK_URI_CSS . 'wpdk-dynamic-table.css', array(), WPDK_VERSION );
101  }
102 
103  // -----------------------------------------------------------------------------------------------------------------
104  // Items
105  // -----------------------------------------------------------------------------------------------------------------
106 
114  public function items() {
115  if ( func_num_args() > 0 ) {
116  $this->_items = func_get_arg( 0 );
117  }
118  else {
119  return $this->_items;
120  }
121  return false;
122  }
123 
124  // -----------------------------------------------------------------------------------------------------------------
125  // Display
126  // -----------------------------------------------------------------------------------------------------------------
127 
133  public function display() {
134  echo $this->html();
135  }
136 
144  public function html() {
145 
146  $html_thead = $this->thead();
147  $html_tbody = $this->tbody();
148  $html_tfoot = $this->tfoot();
149  $id = $this->_id;
150  $class = $this->classes();
151  $data = $this->data();
152 
153  $html = <<< HTML
154  <table id="{$id}" {$data} class="wpdk-dynamic-table {$class}" border="0" cellpadding="0" cellspacing="0">
155  {$html_thead}
156  {$html_tbody}
157  {$html_tfoot}
158  </table>
159 HTML;
160  return $html;
161  }
162 
163  // -----------------------------------------------------------------------------------------------------------------
164  // HTML assets
165  // -----------------------------------------------------------------------------------------------------------------
166 
175  private function classes() {
176  $stack = array( $this->_class );
177  if ( true == $this->sortable ) {
178  $stack[] = 'wpdk-dynamic-table-sortable';
179  }
180  return join( ' ', $stack );
181  }
182 
190  private function data() {
191  $stack = array();
192  if ( true == $this->sortable ) {
193  $stack[] = sprintf( 'data-sortable="true"' );
194  }
195  return join( ' ', $stack );
196  }
197 
205  private function buttonAdd() {
206  $label = __( 'Add', WPDK_TEXTDOMAIN );
207  $title = __( 'Add a new empty row', WPDK_TEXTDOMAIN );
208 
209  $html = <<< HTML
210  <input data-placement="left" title="{$title}" title-backup="{$title}" type="button" value="{$label}" class="wpdk-has-tooltip wpdk-dt-add-row">
211 HTML;
212  return $html;
213  }
214 
222  private function buttonDelete() {
223  $label = __( 'Delete', WPDK_TEXTDOMAIN );
224  $title = __( 'Delete entire row', WPDK_TEXTDOMAIN );
225 
226  $html = <<< HTML
227  <input data-placement="left" title="{$title}" content="{$title}" type="button" value="{$label}" class="wpdk-has-tooltip wpdk-dt-delete-row">
228 HTML;
229  return $html;
230  }
231 
232  // -----------------------------------------------------------------------------------------------------------------
233  // HTML assets table
234  // -----------------------------------------------------------------------------------------------------------------
235 
243  private function thead() {
244 
245  $ths = '';
246  foreach ( $this->_columns as $key => $column ) {
247  if ( $key != self::COLUMN_ROW_MANAGE ) {
248  $ths .= sprintf( '<th class="wpdk-dynamic-table-column-%s">%s</th>', $key, $column['table_title'] );
249  }
250  else {
251  //$ths .= sprintf( '<th class="%s"></th>', $key );
252  }
253  }
254 
255  $html = <<< HTML
256  <thead>
257  <tr>
258  {$ths}
259  </tr>
260  </thead>
261 HTML;
262  return $html;
263  }
264 
272  private function tbody() {
273  $trs = '';
274 
275  /* Il primo รจ sempre display none e usato per la clonazione */
276  $trs .= sprintf( '<tr class="wpdk-dt-clone">%s</tr>', $this->tbodyRow() );
277 
278  if ( !empty( $this->_items ) ) {
279  foreach ( $this->_items as $item ) {
280  $trs .= sprintf( '<tr>%s</tr>', $this->tbodyRow( $item ) );
281  }
282  }
283  $trs .= sprintf( '<tr>%s</tr>', $this->tbodyRow() );
284 
285  $html = <<< HTML
286  <tbody>
287  {$trs}
288  </tbody>
289 HTML;
290  return $html;
291  }
292 
302  private function tbodyRow( $item = null ) {
303  $tds = '';
304  foreach ( $this->_columns as $key => $column ) {
305  if ( self::COLUMN_ROW_MANAGE != $key ) {
306 
307  if ( !is_null( $item ) && is_array( $item ) ) {
308  $column['value'] = isset( $item[$key] ) ? $item[$key] : '';
309  }
310 
311  /* Get a single field. */
312  $field = WPDKUIControlsLayout::item( $column );
313 
314  $tds .= sprintf( '<td class="wpdk-dynamic-table-cel-%s">%s</td>', $key, $field );
315  }
316  else {
317  if ( is_null( $item ) ) {
318  $tds .= sprintf( '<td class="%s">%s<span class="wpdk-dt-clone delete">%s</span></td>', $key, $this->buttonAdd(), $this->buttonDelete() );
319  }
320  else {
321  $tds .= sprintf( '<td class="%s">%s</td>', $key, $this->buttonDelete() );
322  }
323 
324  }
325  }
326  return $tds;
327  }
328 
336  private function tfoot() {
337 
338  $tds = '';
339  foreach ( $this->_columns as $key => $column ) {
340  if ( $key != self::COLUMN_ROW_MANAGE ) {
341  $tds .= sprintf( '<td class="wpdk-dynamic-table-cel-%s"></td>', $key );
342  }
343  else {
344  }
345  }
346  $html = <<< HTML
347  <tfoot>
348  <tr>
349  {$tds}
350  </tr>
351  </tfoot>
352 HTML;
353  return $html;
354  }
355 
356 }
357 
358 
371 class WPDKDynamicTableView extends WPDKView {
372 
378  const COLUMN_ROW_MANAGE = '_wpdk_dt_column_row_manage';
379 
387  public $sortable = false;
388 
396  private $columns;
397 
407  public function __construct( $id ) {
408 
409  parent::__construct( $id );
410 
411  // Added dynamic
412  $this->columns[self::COLUMN_ROW_MANAGE] = '';
413 
414  // Enqueue components
416 
417  }
418 
419  // -------------------------------------------------------------------------------------------------------------------
420  // Columns
421  // -------------------------------------------------------------------------------------------------------------------
422 
445  public function columns()
446  {
447  die( __METHOD__ . ' must be override in your subclass' );
448  }
449 
455  private function _columns()
456  {
457  $columns = $this->columns();
458  $columns[self::COLUMN_ROW_MANAGE] = '';
459  return $columns;
460  }
461 
462  // -------------------------------------------------------------------------------------------------------------------
463  // Items
464  // -------------------------------------------------------------------------------------------------------------------
465 
473  public function items()
474  {
475  die( __METHOD__ . ' must be override in your subclass' );
476  }
477 
478  // -------------------------------------------------------------------------------------------------------------------
479  // Display
480  // -------------------------------------------------------------------------------------------------------------------
481 
489  public function draw()
490  {
492  ?>
493  <table id="<?php printf( 'wpdk-dynamic-table-%s', $this->id ) ?>"
494  class="wpdk-dynamic-table <?php echo $this->sortable ? 'wpdk-dynamic-table-sortable' : '' ?>"
495  <?php echo $this->sortable ? 'data-sortable="true"' : '' ?>
496  cellspacing="0"
497  cellpadding="0"
498  border="0">
499 
500  <!-- Columns -->
501  <thead>
502  <?php $index = 0; foreach ( $this->_columns() as $column_key => $column ) : ?>
503  <?php if ( self::COLUMN_ROW_MANAGE != $column_key ) : ?>
504  <th <?php echo ( true == $this->sortable && empty( $index ) ) ? 'colspan="2"' : '' ?>
505  class="wpdk-dynamic-table-column-<?php echo $column_key ?>">
506  <?php echo $column['_label']; $index++; ?>
507  </th>
508  <?php endif; ?>
509  <?php endforeach; ?>
510  </thead>
511 
512  <tbody>
513 
514  <!-- This row is used for clone -->
515  <tr class="wpdk-dt-clone">
516  <?php $index = 0; foreach ( $this->_columns() as $column_key => $column ) : ?>
517 
518  <?php if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
519  <td class="<?php echo $column_key ?>">
520  <?php echo $this->buttonAdd() ?>
521  <span class="wpdk-dt-clone delete"><?php echo $this->buttonDelete() ?></span>
522  </td>
523  <?php else : ?>
524  <?php if( $this->sortable && empty( $index ) ) : ?>
526  <?php endif; ?>
527  <td class="wpdk-dynamic-table-cel-<?php echo $column_key ?>">
528  <?php echo WPDKUIControlsLayout::item( $column ); $index++ ?>
529  </td>
530  <?php endif; ?>
531 
532  <?php endforeach; ?>
533  </tr>
534 
535  <!-- Main Body -->
536  <?php foreach ( $this->items() as $item ) : ?>
537  <tr>
538  <?php $index = 0; foreach ( $this->_columns() as $column_key => $column ) : $column['value'] = isset( $item[$column_key] ) ? $item[$column_key] : '' ?>
539 
540  <?php if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
541  <td class="<?php echo $column_key ?>">
542  <?php echo $this->buttonDelete() ?>
543  </td>
544  <?php else : ?>
545  <?php if( $this->sortable && empty( $index ) ) : ?>
547  <?php endif; ?>
548  <td class="wpdk-dynamic-table-cel-<?php echo $column_key ?>">
549  <?php echo WPDKUIControlsLayout::item( $column ); $index++ ?>
550  </td>
551  <?php endif; ?>
552 
553  <?php endforeach; ?>
554  </tr>
555  <?php endforeach; ?>
556 
557  <!-- Extra last child row -->
558  <tr>
559  <?php $index = 0; foreach ( $this->_columns() as $column_key => $column ) : ?>
560 
561  <?php if ( self::COLUMN_ROW_MANAGE == $column_key ) : ?>
562  <td class="<?php echo $column_key ?>">
563  <?php echo $this->buttonAdd() ?>
564  <span class="wpdk-dt-clone delete"><?php echo $this->buttonDelete() ?></span>
565  </td>
566  <?php else : ?>
567  <?php if( $this->sortable && empty( $index ) ) : ?>
569  <?php endif; ?>
570  <td class="wpdk-dynamic-table-cel-<?php echo $column_key ?>">
571  <?php echo WPDKUIControlsLayout::item( $column ); $index++ ?>
572  </td>
573  <?php endif; ?>
574 
575  <?php endforeach; ?>
576  </tr>
577 
578  </tbody>
579 
580  <?php if( 1 == 0 ) : ?>
581  <!-- Footer -->
582  <tfoot>
583  <tr>
584  <?php $index = 0; foreach ( $this->_columns() as $column_key => $column ) : ?>
585 
586  <?php if ( self::COLUMN_ROW_MANAGE != $column_key ) : ?>
587  <td <?php echo ( true == $this->sortable && empty( $index ) ) ? 'colspan="2"' : '' ?>
588  class="wpdk-dynamic-table-cel-<?php echo $column_key ?>"></td>
589  <?php endif; $index++ ?>
590 
591  <?php endforeach; ?>
592  </tr>
593  </tfoot>
594  <?php endif; ?>
595 
596  </table>
597  <?php
599  }
600 
601  // -------------------------------------------------------------------------------------------------------------------
602  // HTML assets
603  // -------------------------------------------------------------------------------------------------------------------
604 
612  private function buttonAdd()
613  {
615  <button
616  class="wpdk-has-tooltip wpdk-dt-add-row"
617  title="<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
618  title-backup="<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
619  data-placement="left"
620  >
622  </button>
623  <?php
624  return WPDKHTML::endHTMLCompress();
625 
627  <input type="button"
628  class="wpdk-has-tooltip wpdk-dt-add-row"
629  title="<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
630  title-backup="<?php _e( 'Add a new empty row', WPDK_TEXTDOMAIN ) ?>"
631  data-placement="left"
632  value="<?php _e( 'Add', WPDK_TEXTDOMAIN ) ?>"
633  />
634  <?php
635  return WPDKHTML::endHTMLCompress();
636  }
637 
645  private function buttonDelete()
646  {
648  <button
649  class="wpdk-has-tooltip wpdk-dt-delete-row"
650  title="<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
651  title-backup="<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
652  data-placement="left"
653  >
655  </button>
656  <?php
657  return WPDKHTML::endHTMLCompress();
658 
660  <input type="button"
661  class="wpdk-has-tooltip wpdk-dt-delete-row"
662  title="<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
663  title-backup="<?php _e( 'Delete entire row', WPDK_TEXTDOMAIN ) ?>"
664  data-placement="left"
665  value="<?php _e( 'Delete', WPDK_TEXTDOMAIN ) ?>"
666  />
667  <?php
668  return WPDKHTML::endHTMLCompress();
669  }
670 
671 }