Advertisement
Guenni007

table

Aug 6th, 2020 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.59 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Table
  4.  *
  5.  * Creates a data or pricing table
  6.  */
  7.  
  8.  
  9. if ( ! defined( 'ABSPATH' ) ) {  exit;  }    // Exit if accessed directly
  10.  
  11.  
  12. if ( ! class_exists( 'avia_sc_table' ) )
  13. {
  14.     class avia_sc_table extends aviaShortcodeTemplate
  15.     {
  16.         /**
  17.          *
  18.          * @var int
  19.          */
  20.         static $table_count = 0;
  21.  
  22.         /**
  23.          *
  24.          * @since 4.5.6
  25.          * @var array
  26.          */
  27.         protected $screen_options;
  28.  
  29.         /**
  30.          *
  31.          * @since 4.5.6
  32.          * @param AviaBuilder $builder
  33.          */
  34.         public function __construct( $builder )
  35.         {
  36.             $this->screen_options = array();
  37.  
  38.             parent::__construct( $builder );
  39.         }
  40.  
  41.  
  42.         /**
  43.          * @since 4.5.6
  44.          */
  45.         public function __destruct()
  46.         {  
  47.             parent::__destruct();
  48.  
  49.             unset( $this->screen_options );
  50.         }
  51.  
  52.            
  53.         /**
  54.          * Create the config array for the shortcode button
  55.          */
  56.         function shortcode_insert_button()
  57.         {
  58.             $this->config['version']        = '1.0';
  59.             $this->config['self_closing']   = 'no';
  60.             $this->config['auto_repair']    = 'no';
  61.  
  62.             $this->config['name']       = __( 'Table', 'avia_framework' );
  63.             $this->config['tab']        = __( 'Content Elements', 'avia_framework' );
  64.             $this->config['icon']       = AviaBuilder::$path['imagesURL'] . 'sc-table.png';
  65.             $this->config['order']      = 35;
  66.             $this->config['target']     = 'avia-target-insert';
  67.             $this->config['shortcode']  = 'av_table';
  68.             $this->config['modal_data'] = array( 'modal_class' => 'bigscreen', 'before_save' => 'before_table_save' );
  69.             $this->config['shortcode_nested'] = array( 'av_row', 'av_cell','av_button' );
  70.             $this->config['tooltip']    = __( 'Creates a data or pricing table', 'avia_framework' );
  71.             $this->config['preview']    = false;
  72.             $this->config['disabling_allowed'] = true;
  73.            
  74.             $this->config['id_name']    = 'id';
  75.             $this->config['id_show']    = 'yes';
  76.             $this->config['alb_desc_id']    = 'alb_description';
  77.         }
  78.  
  79.  
  80.         function admin_assets()
  81.         {
  82.             $ver = AviaBuilder::VERSION;
  83.            
  84.             wp_register_script('avia_table_js', AviaBuilder::$path['assetsURL'] . 'js/avia-table.js', array( 'avia_modal_js' ), $ver, true );
  85.             Avia_Builder()->add_registered_admin_script( 'avia_table_js' );
  86.         }
  87.  
  88.  
  89.         function extra_assets()
  90.         {
  91.             //load css
  92.             wp_enqueue_style( 'avia-module-table', AviaBuilder::$path['pluginUrlRoot'] . 'avia-shortcodes/table/table.css', array( 'avia-layout' ), false );
  93.         }
  94.  
  95.  
  96.  
  97.         /**
  98.          * Popup Elements
  99.          *
  100.          * If this function is defined in a child class the element automatically gets an edit button, that, when pressed
  101.          * opens a modal window that allows to edit the element properties
  102.          *
  103.          * @return void
  104.          */
  105.         function popup_elements()
  106.         {
  107.            
  108.             $this->elements = array(
  109.                
  110.                 array(
  111.                         'type'  => 'tab_container',
  112.                         'nodescription' => true
  113.                     ),
  114.                        
  115.                 array(
  116.                         'type'  => 'tab',
  117.                         'name'  => __( 'Content', 'avia_framework' ),
  118.                         'nodescription' => true
  119.                     ),
  120.                
  121.                     array(
  122.                             'type'          => 'template',
  123.                             'template_id'   => $this->popup_key( 'content_table' )
  124.                         ),
  125.                
  126.                 array(
  127.                         'type'  => 'tab_close',
  128.                         'nodescription' => true
  129.                     ),
  130.                
  131.                 array(
  132.                         'type'  => 'tab',
  133.                         'name'  => __( 'Styling', 'avia_framework' ),
  134.                         'nodescription' => true
  135.                     ),
  136.                
  137.                     array(
  138.                             'type'          => 'template',
  139.                             'template_id'   => 'toggle_container',
  140.                             'templates_include' => array(
  141.                                                     $this->popup_key( 'styling_table' ),
  142.                                                     $this->popup_key( 'styling_caption' )
  143.                                                 ),
  144.                             'nodescription' => true
  145.                         ),
  146.                
  147.                 array(
  148.                         'type'  => 'tab_close',
  149.                         'nodescription' => true
  150.                     ),
  151.                
  152.                 array(
  153.                         'type'  => 'tab',
  154.                         'name'  => __( 'Advanced', 'avia_framework' ),
  155.                         'nodescription' => true
  156.                     ),
  157.                
  158.                     array(
  159.                             'type'  => 'toggle_container',
  160.                             'nodescription' => true
  161.                         ),
  162.                
  163.                         array( 
  164.                                 'type'              => 'template',
  165.                                 'template_id'       => 'screen_options_toggle',
  166.                                 'templates_include' => array(
  167.                                                             $this->popup_key( 'advanced_table_responsive' ),
  168.                                                             'screen_options_visibility'
  169.                                                         )
  170.                             ),
  171.  
  172.                         array( 
  173.                                 'type'          => 'template',
  174.                                 'template_id'   => 'developer_options_toggle',
  175.                                 'args'          => array( 'sc' => $this )
  176.                             ),
  177.                
  178.                     array(
  179.                             'type'  => 'toggle_container_close',
  180.                             'nodescription' => true
  181.                         ),
  182.                
  183.                 array(
  184.                         'type'  => 'tab_close',
  185.                         'nodescription' => true
  186.                     ),
  187.  
  188.                 array(
  189.                         'type'  => 'tab_container_close',
  190.                         'nodescription' => true
  191.                     )
  192.                                
  193.                 );
  194.         }
  195.        
  196.         /**
  197.          * Create and register templates for easier maintainance
  198.          *
  199.          * @since 4.6.4
  200.          */
  201.         protected function register_dynamic_templates()
  202.         {
  203.            
  204.             /**
  205.              * Content Tab
  206.              * ===========
  207.              */
  208.            
  209.             $c = array(
  210.                         array( 
  211.                             'name'  => __( 'Table Builder', 'avia_framework' ),
  212.                             'desc'  => __( 'Start by adding columns and rows, then add content and styling to each.', 'avia_framework' ),
  213.                             'id'    => 'table',
  214.                             'container_class' => 'avia-element-fullwidth',
  215.                             'type'  => 'table',
  216.                             'row_style'     => array(  
  217.                                                     __( 'Default Row', 'avia_framework' )   => '',
  218.                                                     __( 'Heading Row', 'avia_framework' )   => 'avia-heading-row',
  219.                                                     __( 'Pricing Row', 'avia_framework' )   => 'avia-pricing-row',
  220.                                                     __( 'Button Row', 'avia_framework' )    => 'avia-button-row'
  221.                                                 ),
  222.                             'column_style'  => array(
  223.                                                     __('Default Column', 'avia_framework' )     => '',
  224.                                                     __('Highlight Column', 'avia_framework' )   => 'avia-highlight-col',
  225.                                                     __('Description Column', 'avia_framework' ) => 'avia-desc-col',
  226.                                                     __('Center Text Column', 'avia_framework' ) => 'avia-center-col'
  227.                                                 )
  228.                         ),
  229.                
  230.                 );
  231.            
  232.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'content_table' ), $c );
  233.            
  234.             /**
  235.              * Styling Tab
  236.              * ===========
  237.              */
  238.            
  239.             $c = array(
  240.                         array( 
  241.                             'name'  => __( 'Table Purpose', 'avia_framework' ),
  242.                             'desc'  => __( 'Choose if the table should be used to display tabular data or to display pricing options. (Difference: Pricing tables are flashier and try to stand out)', 'avia_framework' ),
  243.                             'id'    => 'purpose',
  244.                             'type'  => 'select',
  245.                             'std'   => 'pricing',
  246.                             'subtype'   => array(
  247.                                                 __( 'Use the table as a Pricing Table', 'avia_framework' )      => 'pricing',
  248.                                                 __( 'Use the table to display tabular data', 'avia_framework' ) => 'tabular'
  249.                                             )
  250.                             ), 
  251.                
  252.                         array(
  253.                         'name'  => __( 'Table Design', 'avia_framework' ),
  254.                         'desc'  => __( 'Use either the default or minimal design', 'avia_framework' ),
  255.                         'id'    => 'pricing_table_design',
  256.                         'type'  => 'select',
  257.                         'std'   => 'avia_pricing_default',
  258.                         'subtype'   => array(
  259.                                             __( 'Default', 'avia_framework')    => 'avia_pricing_default',
  260.                                             __( 'Minimal', 'avia_framework')    => 'avia_pricing_minimal'
  261.                                         )
  262.                         ),
  263.                            
  264.                         array(
  265.                             'name'  => __( 'Empty Cells', 'avia_framework' ),
  266.                             'desc'  => __( 'Empty Cells are by default hidden. If you want to force equal height across all columns set them to display', 'avia_framework' ),
  267.                             'id'    => 'pricing_hidden_cells',
  268.                             'type'  => 'select',
  269.                             'std'   => '',
  270.                             'required' => array( 'purpose', 'equals', 'pricing' ),
  271.                             'subtype' => array(
  272.                                             __( 'Hide empty Cells', 'avia_framework' )  => '',
  273.                                             __( 'Show empty Cells', 'avia_framework' )  => 'avia_show_empty_cells'
  274.                                         )
  275.                         ),
  276.                                
  277.                
  278.                 );
  279.            
  280.             $template = array(
  281.                             array( 
  282.                                 'type'          => 'template',
  283.                                 'template_id'   => 'toggle',
  284.                                 'title'         => __( 'Table Styling', 'avia_framework' ),
  285.                                 'content'       => $c
  286.                             ),
  287.                     );
  288.            
  289.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_table' ), $template );
  290.            
  291.             $c = array(
  292.                         array( 
  293.                             'name'  => __( 'Table Caption', 'avia_framework' ),
  294.                             'desc'  => __( 'Add a short caption to the table so visitors know what the data is about', 'avia_framework' ),
  295.                             'id'    => 'caption',
  296.                             'type'  => 'input',
  297.                             'std'   => '',
  298.                             'required' => array( 'purpose', 'equals', 'tabular' )
  299.                         ),
  300.                
  301.                 );
  302.            
  303.             $template = array(
  304.                             array( 
  305.                                 'type'          => 'template',
  306.                                 'template_id'   => 'toggle',
  307.                                 'title'         => __( 'Caption', 'avia_framework' ),
  308.                                 'content'       => $c
  309.                             ),
  310.                     );
  311.            
  312.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'styling_caption' ), $template );
  313.            
  314.             /**
  315.              * Advanced Tab
  316.              * ===========
  317.              */
  318.            
  319.             $c = array(
  320.                         array(
  321.                             'name'  => __( 'Responsive Styling', 'avia_framework' ),
  322.                             'desc'  => __( 'Select which table styling should be used if the screen is too small for the table.', 'avia_framework' ),
  323.                             'id'    => 'responsive_styling',
  324.                             'type'  => 'select',
  325.                             'std'   => 'avia_responsive_table',
  326.                             'required'  => array( 'purpose', 'equals', 'tabular' ),
  327.                             'subtype'   => array(
  328.                                                 __( 'Adjust table to screen size', 'avia_framework' )   => 'avia_responsive_table',
  329.                                                 __( 'Make entire table scrollable', 'avia_framework' )  => 'avia_scrollable_table'
  330.                                             )
  331.                         ),
  332.  
  333.                 );
  334.            
  335.             AviaPopupTemplates()->register_dynamic_template( $this->popup_key( 'advanced_table_responsive' ), $c );
  336.            
  337.            
  338.         }
  339.            
  340.            
  341.            
  342.         /**
  343.          * Editor Element - this function defines the visual appearance of an element on the AviaBuilder Canvas
  344.          * Most common usage is to define some markup in the $params['innerHtml'] which is then inserted into the drag and drop container
  345.          * Less often used: $params['data'] to add data attributes, $params['class'] to modify the className
  346.          *
  347.          *
  348.          * @param array $params this array holds the default values for $content and $args.
  349.          * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  350.          */
  351.         function editor_element( $params )
  352.         {  
  353.             $params = parent::editor_element( $params );
  354.             return $params;
  355.         }
  356.  
  357.         /**
  358.          * Editor Sub Element - this function defines the visual appearance of an element that is displayed within a modal window and on click opens its own modal window
  359.          * Works in the same way as Editor Element
  360.          * @param array $params this array holds the default values for $content and $args.
  361.          * @return $params the return array usually holds an innerHtml key that holds item specific markup.
  362.          */
  363.         function editor_sub_element( $params )
  364.         {  
  365.             $template = $this->update_template( 'label', __( 'Element', 'avia_framework' ) . ': {{label}}' );
  366.  
  367.             $params['content'] = null;
  368.             $params['innerHtml']  = '';
  369.             $params['innerHtml'] .= "<div class='avia_image_container' {$template}>" . __( 'Element', 'avia_framework' ) . ": {$params['args']['label']}</div>";
  370.             $params['data'] = array( 'modal_class' => 'mediumscreen' );
  371.  
  372.             return $params;
  373.         }
  374.  
  375.  
  376.  
  377.         /**
  378.          * Frontend Shortcode Handler
  379.          *
  380.          * @param array $atts array of attributes
  381.          * @param string $content text within enclosing form of shortcode element
  382.          * @param string $shortcodename the shortcode found, when == callback name
  383.          * @return string $output returns the modified html string
  384.          */
  385.         function shortcode_handler( $atts, $content = '', $shortcodename = '', $meta = '' )
  386.         {  
  387.             $this->screen_options = AviaHelper::av_mobile_sizes( $atts );
  388.  
  389.             $atts = shortcode_atts( array(
  390.                             'purpose'               => 'pricing',
  391.                             'caption'               => '',
  392.                             'responsive_styling'    => 'avia_responsive_table',
  393.                             'pricing_hidden_cells'  => '',
  394.                             'pricing_table_design'  => 'avia_pricing_default'
  395.                     ), $atts, $this->config['shortcode'] );
  396.            
  397.            
  398.             $depth      = 2;
  399.             $table_rows = ShortcodeHelper::shortcode2array( $content, $depth );
  400.  
  401.             $output     = '';
  402.  
  403.             if( empty( $table_rows ) )
  404.             {
  405.                 return $output;
  406.             }
  407.  
  408.             self::$table_count ++;
  409.  
  410.             switch( $atts['purpose'] )
  411.             {
  412.                 case 'pricing':  
  413.                     $output .= $this->pricing_table( $table_rows, $atts, $meta );
  414.                     break;
  415.                 default:         
  416.                     $output .= $this->data_table( $table_rows, $atts, $meta );
  417.                     break;
  418.             }
  419.  
  420.             return $output;
  421.         }
  422.  
  423.  
  424.         /**
  425.          * resort the array so that its easier to do a liststlye output when using pricing tables
  426.          *
  427.          * @param array $table_rows
  428.          * @return array
  429.          */
  430.         protected function list_sort_array( $table_rows )
  431.         {
  432.             $new = array();
  433.  
  434.             foreach( $table_rows as $rk => $row )
  435.             {
  436.                 foreach( $row['content'] as $ck => $cell )
  437.                 {
  438.                     $new[$ck]['ul_style']   = $cell['attr']['col_style'];
  439.                     $new[$ck]['attr'][]     = $row['attr'];
  440.                     $new[$ck]['content'][]  = $cell;
  441.                 }
  442.             }
  443.  
  444.             return $new;
  445.         }
  446.            
  447.         /**
  448.          * Pricing table uses unordered lists to display the table structure
  449.          *
  450.          * @param array $table_rows
  451.          * @param array $atts
  452.          * @param array $meta
  453.          * @return string
  454.          */
  455.         protected function pricing_table( $table_rows, $atts, $meta )
  456.         {  
  457.             extract( $this->screen_options );
  458.  
  459.  
  460.             $class = "{$atts['pricing_hidden_cells']} {$atts['pricing_table_design']} {$av_display_classes} {$meta['el_class']}";
  461.             $sorted_rows = $this->list_sort_array( $table_rows );
  462.             $markup = avia_markup_helper( array( 'context' => 'table', 'echo' => false, 'custom_markup' => $meta['custom_markup'] ) );
  463.  
  464.             $output  =  '';    
  465.             $output .= "<div {$meta['custom_el_id']} class='avia-table main_color avia-pricing-table-container {$class} avia-table-" . self::$table_count . "' $markup>";
  466.  
  467.             $fallback_values = array();
  468.             $empty_cells = false;
  469.  
  470.             foreach( $sorted_rows as $ul_k => $ul )
  471.             {
  472.                
  473.                 $output .= "<div class='pricing-table-wrap'>";
  474.                 $output .= "<ul class='pricing-table {$ul['ul_style']}'>";
  475.  
  476.                 foreach( $ul['content'] as $key => $li )
  477.                 {
  478.                     $content = trim( do_shortcode( $li['content'] ) );
  479.  
  480.                     if( empty( $content ) && $content !== '0' )
  481.                     {
  482.                         $ul['attr'][ $key ]['row_style'] .= ' empty-table-cell';
  483.                         $content = "{{content-{$key}}}";
  484.                         $empty_cells = true;
  485.                     }
  486.                     else if( empty( $fallback_values[ $key ] ) )
  487.                     {
  488.                         $fallback_values[ $key ] = $content;
  489.                     }
  490.  
  491.                     if( strpos( $ul['attr'][ $key ]['row_style'], 'avia-pricing-row' ) !== false )
  492.                     {
  493.                         $content = preg_replace( '!(\$|€|¥|£|¢|¤|%|‰|&cent;|&curren;|&pound;|&yen;|&euro;)!' , '<span class="currency-symbol">$1</span>', $content );
  494.                     }
  495.  
  496.  
  497.                     $output .= "<li class='{$ul['attr'][$key]['row_style']}'>";
  498.                     $output .= $key == 0 ? "<div class='first-table-item'>{$content}</div>" : $content;
  499.                     $output .= $key == 0 ? "<span class='pricing-extra'></span>" :'';
  500.  
  501.                     $output .= '</li>';
  502.                 }
  503.                        
  504.                 $output .= '</ul>';
  505.                 $output .= '</div>';
  506.             }
  507.  
  508.             if( $empty_cells )
  509.             {
  510.                 foreach( $fallback_values as $key => $value )
  511.                 {
  512.                     $output = str_replace( "{{content-{$key}}}", "<span class='fallback-table-val'>{$value}</span>", $output );
  513.                 }
  514.             }
  515.  
  516.             $output .= '</div>';
  517.             return $output;
  518.         }
  519.            
  520.         /**
  521.          * Data table uses the real table html tag to display its structure
  522.          *
  523.          * @param array $table_rows
  524.          * @param array $atts
  525.          * @param array $meta
  526.          * @return type
  527.         */
  528.         protected function data_table( $table_rows, $atts, $meta )
  529.         {  
  530.             extract( $this->screen_options );
  531.  
  532.             $responsive_style = '';
  533.             $class = $meta['el_class'] . ' ' . $atts['pricing_table_design'] . ' ' . $av_display_classes;
  534.  
  535.             $markup = avia_markup_helper( array( 'context' => 'table', 'echo' => false, 'custom_markup' => $meta['custom_markup'] ) );
  536.  
  537.             $output  = "<div class='avia-data-table-wrap {$atts['responsive_styling']}'>";
  538.             $output .=      "<table {$meta['custom_el_id']} class='avia-table avia-data-table avia-table-" . self::$table_count . " {$class}' {$markup}>";
  539.             $output .=          $atts['caption'] ? '<caption>' . $atts['caption'] . '</caption>' : '';
  540.             $output .=          '<tbody>'; 
  541.             $counter = 0;
  542.  
  543.             foreach( $table_rows as $rk => $row )
  544.             {  
  545.                 $responsive_style_nth_modifier = 1;
  546.  
  547.                 if( empty( $row['attr'] ) )
  548.                 {
  549.                     $row['attr'] = array();
  550.                 }
  551.  
  552.                 $row_attributes = array_merge( array( 'row_style' => '' ), $row['attr'] );
  553.  
  554.                 $output .= "<tr class='{$row_attributes['row_style']}'>";
  555.  
  556.                 foreach( $row['content'] as $key => $cell )
  557.                 {
  558.                     if( empty( $cell['attr'] ) )
  559.                     {
  560.                         $cell['attr'] = array();
  561.                     }
  562.                    
  563.                     $cell_attributes = array_merge( array( 'col_style' => '' ), $cell['attr'] );
  564.  
  565.                     $tag = $row_attributes['row_style'] == 'avia-heading-row' ? 'th' : 'td';
  566.                     $tag = $cell_attributes['col_style'] == 'avia-desc-col' ? 'th' : $tag;
  567.  
  568.                     if( $row_attributes['row_style'] == 'avia-heading-row' && $cell_attributes['col_style'] == 'avia-desc-col' )
  569.                     {
  570.                         //fixes issues like
  571.                         //https://kriesi.at/support/topic/display-of-a-table-displays-wron-headlines-on-mobile/.
  572.  
  573.                         $responsive_style_nth_modifier = 0;
  574.                     }
  575.  
  576.                     if( $rk == 0 && $tag == 'th' )
  577.                     {
  578.                         $responsive_style .= ".avia-table-" . self::$table_count . " td:nth-of-type(" . ( $counter + $responsive_style_nth_modifier ) . "):before { content: '" . strip_tags( html_entity_decode( $row['content'][ $counter ]['content'] ) ) . "'; } ";
  579.                         $counter ++;
  580.                     }
  581.  
  582.                     $output .= "<{$tag} class='{$cell_attributes['col_style']}'>";
  583.                     $output .=      do_shortcode( $cell['content'] );
  584.                     $output .= "</{$tag}>";
  585.                 }
  586.                 $output .= '</tr>';
  587.             }
  588.  
  589.             $output .=          '</tbody>';
  590.             $output .=      '</table>';
  591.             $output .= '</div>';
  592.            
  593.             if( $atts['responsive_styling'] == 'avia_responsive_table' )
  594.             {
  595.                 $output .= "<style type='text/css'>{$responsive_style}</style>";
  596.             }
  597.  
  598.             return $output;
  599.         }
  600.  
  601.     }
  602. }
  603.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement