Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file TFieldList.php

Documentation is available at TFieldList.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Core\AdiantiCoreTranslator;
  5. use Adianti\Control\TAction;
  6. use Adianti\Widget\Form\AdiantiWidgetInterface;
  7. use Adianti\Widget\Container\TTable;
  8. use Adianti\Widget\Form\TLabel;
  9. use Adianti\Widget\Form\THidden;
  10. use Adianti\Widget\Util\TImage;
  11. use Adianti\Widget\Base\TElement;
  12. use Adianti\Widget\Base\TScript;
  13. use Exception;
  14. use stdClass;
  15.  
  16. /**
  17.  * Create a field list
  18.  *
  19.  * @version    7.4
  20.  * @package    widget
  21.  * @subpackage form
  22.  * @author     Pablo Dall'Oglio
  23.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  24.  * @license    http://www.adianti.com.br/framework-license
  25.  */
  26. class TFieldList extends TTable
  27. {
  28.     private $fields;
  29.     private $labels;
  30.     private $body_created;
  31.     private $detail_row;
  32.     private $remove_function;
  33.     private $remove_action;
  34.     private $clone_function;
  35.     private $sort_action;
  36.     private $sorting;
  37.     private $fields_properties;
  38.     private $row_functions;
  39.     private $row_actions;
  40.     private $automatic_aria;
  41.     private $summarize;
  42.     private $totals;
  43.     private $total_functions;
  44.     private $remove_enabled;
  45.     private $remove_icon;
  46.     private $remove_title;
  47.     private $field_prefix;
  48.     private $thead;
  49.     private $tfoot;
  50.     private $tbody;
  51.     private $allow_post_empty;
  52.     protected $totalUpdateAction;
  53.     
  54.     /**
  55.      * Class Constructor
  56.      */
  57.     public function __construct()
  58.     {
  59.         parent::__construct();
  60.         $this->{'id'}     'tfieldlist_' mt_rand(10000000001999999999);
  61.         $this->{'name'}   $this->{'id'};
  62.         $this->{'class'}  'tfieldlist';
  63.         
  64.         $this->fields [];
  65.         $this->fields_properties [];
  66.         $this->row_functions [];
  67.         $this->row_actions [];
  68.         $this->body_created false;
  69.         $this->detail_row 0;
  70.         $this->sorting false;
  71.         $this->automatic_aria false;
  72.         $this->remove_function 'ttable_remove_row(this)';
  73.         $this->clone_function  'ttable_clone_previous_row(this)';
  74.         $this->summarize false;
  75.         $this->total_functions null;
  76.         $this->remove_enabled true;
  77.         $this->allow_post_empty true;
  78.     }
  79.     
  80.     /**
  81.      *
  82.      */
  83.     public function disablePostEmptyRow()
  84.     {
  85.         $this->allow_post_empty false;
  86.     }
  87.     
  88.     /**
  89.      * Get post data as object list
  90.      */
  91.     public function getPostData()
  92.     {
  93.         $data [];
  94.         
  95.         foreach($this->fields as $field)
  96.         {
  97.             $field_name $field->getName();
  98.             $name  str_replace['['']']['''']$field->getName());
  99.             
  100.             $data[$name$field->getPostData();
  101.         }
  102.         
  103.         $results [];
  104.         
  105.         foreach ($data as $name => $values)
  106.         {
  107.             $field_name $name;
  108.             
  109.             if (!empty($this->field_prefix))
  110.             {
  111.                 $field_name str_replace($this->field_prefix '_'''$field_name);
  112.             }
  113.             
  114.             foreach ($values as $row => $value)
  115.             {
  116.                 $results[$row$results[$row?? new stdClass;
  117.                 $results[$row]->$field_name $value;
  118.             }
  119.         }
  120.         
  121.         if (!$this->allow_post_empty)
  122.         {
  123.             if ($results)
  124.             {
  125.                 foreach ($results as $row => $object)
  126.                 {
  127.                     $array_object = (array) $object;
  128.                     unset($array_object['uniq']);
  129.                     if (count(array_filter($array_object)) == 0)
  130.                     {
  131.                         unset($results[$row]);
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.         
  137.         return $results;
  138.     }
  139.     
  140.     /**
  141.      * Get post row count
  142.      */
  143.     public function getRowCount($field_name null)
  144.     {
  145.         if (count($this->fields0)
  146.         {
  147.             if (isset($this->fields[$field_name]))
  148.             {
  149.                 $field $this->fields[$field_name];
  150.             }
  151.             else if (isset($this->fields[$field_name.'[]']))
  152.             {
  153.                 $field $this->fields[$field_name.'[]'];
  154.             }
  155.             else
  156.             {
  157.                 $field array_values($this->fields)[0];
  158.             }
  159.             
  160.             return count(array_filter($field->getPostData()function($value){
  161.                 return $value !== '';
  162.             }));
  163.         }
  164.         
  165.         return 0;
  166.     }
  167.     
  168.     /**
  169.      * Disable remove button
  170.      */
  171.     public function disableRemoveButton()
  172.     {
  173.         $this->remove_enabled false;
  174.     }
  175.     
  176.     /**
  177.      * Enable sorting
  178.      */
  179.     public function enableSorting()
  180.     {
  181.         $this->sorting true;
  182.     }
  183.     
  184.     /**
  185.      * Generate automatic aria-labels
  186.      */
  187.     public function generateAria()
  188.     {
  189.         $this->automatic_aria true;
  190.     }
  191.     
  192.     /**
  193.      * Define the action to be executed when the user sort rows
  194.      * @param $action TAction object
  195.      */
  196.     public function setSortAction(TAction $action)
  197.     {
  198.         if ($action->isStatic())
  199.         {
  200.             $this->sort_action $action;
  201.         }
  202.         else
  203.         {
  204.             $string_action $action->toString();
  205.             throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  206.         }
  207.     }
  208.     
  209.     /**
  210.      * Set the remove javascript action
  211.      */
  212.     public function setRemoveFunction($action$icon null$title null)
  213.     {
  214.         $this->remove_function $action;
  215.         $this->remove_icon     $icon;
  216.         $this->remove_title    $title;
  217.     }
  218.     
  219.     /**
  220.      * Set the remove action
  221.      */
  222.     public function setRemoveAction(TAction $action null$icon null$title null)
  223.     {
  224.         if ($action)
  225.         {
  226.             if ($action->isStatic())
  227.             {
  228.                 $this->remove_action $action;
  229.             }
  230.             else
  231.             {
  232.                 $string_action $action->toString();
  233.                 throw new Exception(AdiantiCoreTranslator::translate('Action (^1) must be static to be used in ^2'$string_action__METHOD__));
  234.             }
  235.         }
  236.         
  237.         $this->remove_icon  $icon;
  238.         $this->remove_title $title;
  239.     }
  240.     
  241.     /**
  242.      * Set the clone javascript action
  243.      */
  244.     public function setCloneFunction($action)
  245.     {
  246.         $this->clone_function $action;
  247.     }
  248.     
  249.     /**
  250.      * Add function
  251.      */
  252.     public function addButtonFunction($function$icon$title)
  253.     {
  254.         $this->row_functions[[$function$icon$title];
  255.     }
  256.     
  257.     /**
  258.      * Add action
  259.      */
  260.     public function addButtonAction(TAction $action$icon$title)
  261.     {
  262.         $this->row_actions[[$action$icon$title];
  263.     }
  264.     
  265.     /**
  266.      * Define total update action
  267.      */
  268.     public function setTotalUpdateAction(TAction $action)
  269.     {
  270.         $this->totalUpdateAction $action;
  271.         parent::setProperty('total-update-action'$action->serialize(false));
  272.     }
  273.     
  274.     /**
  275.      * Set field prefix
  276.      */
  277.     public function setFieldPrefix($prefix)
  278.     {
  279.         $this->field_prefix $prefix;
  280.     }
  281.     
  282.     /**
  283.      * Get field prefix
  284.      */
  285.     public function getFieldPrefix()
  286.     {
  287.         return $this->field_prefix;
  288.     }
  289.     
  290.     /**
  291.      * Add a field
  292.      * @param $label  Field Label
  293.      * @param $object Field Object
  294.      */
  295.     public function addField($labelAdiantiWidgetInterface $field$properties null)
  296.     {
  297.         if ($field instanceof TField)
  298.         {
  299.             $name $field->getName();
  300.             
  301.             if (!empty($this->field_prefix&& strpos($name$this->field_prefix=== false)
  302.             {
  303.                 $name $this->field_prefix '_' $name;
  304.                 $field->setName($name);
  305.             }
  306.             
  307.             if (isset($this->fields[$name]AND substr($name,-2!== '[]')
  308.             {
  309.                 throw new Exception(AdiantiCoreTranslator::translate('You have already added a field called "^1" inside the form'$name));
  310.             }
  311.             
  312.             if ($name)
  313.             {
  314.                 $this->fields[$name$field;
  315.                 $this->fields_properties[$name$properties;
  316.             }
  317.             
  318.             if (isset($properties['sum']&& $properties['sum'== true)
  319.             {
  320.                 $this->summarize true;
  321.             }
  322.             
  323.             if (isset($properties['uniqid']&& $properties['uniqid'== true)
  324.             {
  325.                 $field->{'uniqid''true';
  326.             }
  327.             
  328.             if ($label instanceof TLabel)
  329.             {
  330.                 $label_field $label;
  331.                 $label_value $label->getValue();
  332.             }
  333.             else
  334.             {
  335.                 $label_field new TLabel($label);
  336.                 $label_value $label;
  337.             }
  338.             
  339.             $field->setLabel($label_value);
  340.             $this->labels[$name$label_field;
  341.         }
  342.     }
  343.     
  344.     /**
  345.      * Add table header
  346.      */
  347.     public function addHeader()
  348.     {
  349.         $this->thead $section parent::addSection('thead');
  350.         
  351.         if ($this->fields)
  352.         {
  353.             $row parent::addRow();
  354.             
  355.             if ($this->sorting)
  356.             {
  357.                 $row->addCell'' );
  358.             }
  359.             
  360.             foreach ($this->fields as $name => $field)
  361.             {
  362.                 if ($field instanceof THidden)
  363.                 {
  364.                     $cell $row->addCell'' );
  365.                     $cell->{'style''display:none';
  366.                 }
  367.                 else
  368.                 {
  369.                     $cell $row->addCell$this->labels$field->getName());
  370.                     
  371.                     if (!empty($this->fields_properties[$name]))
  372.                     {
  373.                         foreach ($this->fields_properties[$nameas $property => $value)
  374.                         {
  375.                             $cell->setProperty($property$value);
  376.                         }
  377.                     }
  378.                 }
  379.             }
  380.             
  381.             $all_actions array_merge(array) $this->row_functions(array) $this->row_actions );
  382.             
  383.             if ($all_actions)
  384.             {
  385.                 foreach ($all_actions as $row_action)
  386.                 {
  387.                     $cell $row->addCell'' );
  388.                     $cell->{'style''display:none';
  389.                 }
  390.             }
  391.             
  392.             if ($this->remove_enabled)
  393.             {
  394.                 // aligned with remove button
  395.                 $cell $row->addCell'' );
  396.                 $cell->{'style''display:none';
  397.             }
  398.         }
  399.         
  400.         return $section;
  401.     }
  402.     
  403.     /**
  404.      * Add detail row
  405.      * @param $item Data object
  406.      */
  407.     public function addDetail$item )
  408.     {
  409.         $uniqid mt_rand(10000009999999);
  410.         $field_list_name $this->{'name'};
  411.         
  412.         if (!$this->body_created)
  413.         {
  414.             $this->tbody parent::addSection('tbody');
  415.             $this->body_created true;
  416.         }
  417.         
  418.         if ($this->fields)
  419.         {
  420.             $row parent::addRow();
  421.             $row->{'id'$uniqid;
  422.             
  423.             if ($this->sorting)
  424.             {
  425.                 $move new TImage('fas:arrows-alt gray');
  426.                 $move->{'class'.= ' handle';
  427.                 $move->{'style'.= ';font-size:100%;cursor:move';
  428.                 $row->addCell$move );
  429.             }
  430.             
  431.             foreach ($this->fields as $field)
  432.             {
  433.                 $field_name $field->getName();
  434.                 $name  str_replace['['']']['''']$field->getName());
  435.                 
  436.                 if ($this->detail_row == 0)
  437.                 {
  438.                     $clone $field;
  439.                 }
  440.                 else
  441.                 {
  442.                     $clone clone $field;
  443.                 }
  444.                 
  445.                 if (isset($this->fields_properties[$field_name]['sum']&& $this->fields_properties[$field_name]['sum'== true)
  446.                 {
  447.                     $field->{'exitaction'"tfieldlist_update_sum('{$field_list_name}', '{$name}', 'callback')";
  448.                     $field->{'onBlur'}     "tfieldlist_update_sum('{$field_list_name}', '{$name}', 'callback')";
  449.                     
  450.                     $this->total_functions .= $field->{'exitaction'';';
  451.                     
  452.                     $value = isset($item->$name$item->$name 0;
  453.                     
  454.                     if (isset($field->{'data-nmask'}))
  455.                     {
  456.                         $dec_sep substr($field->{'data-nmask'},1,1);
  457.                         $tho_sep substr($field->{'data-nmask'},2,1);
  458.                         
  459.                         if ( (strpos($value$tho_sep!== false&& (strpos($value$dec_sep!== false) )
  460.                         {
  461.                             $value   str_replace($tho_sep''$value);
  462.                             $value   str_replace($dec_sep'.'$value);
  463.                         }
  464.                     }
  465.                     
  466.                     if (isset($this->totals[$name]))
  467.                     {
  468.                         $this->totals[$name+= $value;
  469.                     }
  470.                     else
  471.                     {
  472.                         $this->totals[$name$value;
  473.                     }
  474.                 }
  475.                 
  476.                 if ($this->automatic_aria)
  477.                 {
  478.                     $label $this->labels$field->getName(];
  479.                     $aria_label $label->getValue();
  480.                     $field->{'aria-label'$aria_label;
  481.                 }
  482.                 
  483.                 $clone->setId($name.'_'.$uniqid);
  484.                 $clone->{'data-row'$this->detail_row;
  485.                 
  486.                 $cell $row->addCell$clone );
  487.                 $cell->{'class''field';
  488.                 
  489.                 if (!empty($this->fields_properties[$field_name]))
  490.                 {
  491.                     foreach ($this->fields_properties[$field_nameas $property => $value)
  492.                     {
  493.                         $cell->setProperty($property$value);
  494.                     }
  495.                 }
  496.                 
  497.                 if ($clone instanceof THidden)
  498.                 {
  499.                     $cell->{'style''display:none';
  500.                 }
  501.                 
  502.                 if (!empty($item->$nameOR (isset($item->$nameAND $item->$name == '0'))
  503.                 {
  504.                     $clone->setValue$item->$name );
  505.                 }
  506.                 else
  507.                 {
  508.                     if ($field->{'uniqid'== true)
  509.                     {
  510.                         $clone->setValuemt_rand(10000000001999999999) );
  511.                     }
  512.                     else
  513.                     {
  514.                         $clone->setValuenull );
  515.                     }
  516.                 }
  517.             }
  518.             
  519.             if ($this->row_actions)
  520.             {
  521.                 foreach ($this->row_actions as $row_action)
  522.                 {
  523.                     $string_action $row_action[0]->serialize(FALSE);
  524.                     
  525.                     $btn new TElement('div');
  526.                     $btn->{'class''btn btn-default btn-sm';
  527.                     $btn->{'onclick'"__adianti_post_exec('{$string_action}', tfieldlist_get_row_data(this), null, undefined, '1')";
  528.                     $btn->{'title'$row_action[2];
  529.                     $btn->add(new TImage($row_action[1]));
  530.                     $row->addCell$btn );
  531.                 }
  532.             }
  533.             
  534.             if ($this->row_functions)
  535.             {
  536.                 foreach ($this->row_functions as $row_function)
  537.                 {
  538.                     $btn new TElement('div');
  539.                     $btn->{'class''btn btn-default btn-sm';
  540.                     $btn->{'onclick'$row_function[0];
  541.                     $btn->{'title'$row_function[2];
  542.                     $btn->add(new TImage($row_function[1]));
  543.                     $row->addCell$btn );
  544.                 }
  545.             }
  546.             
  547.             if ($this->remove_enabled)
  548.             {
  549.                 $del new TElement('div');
  550.                 $del->{'class''btn btn-default btn-sm';
  551.                 $del->{'onclick'$this->total_functions $this->remove_function;
  552.                 
  553.                 if (isset($this->remove_action))
  554.                 {
  555.                     $string_action $this->remove_action->serialize(FALSE);
  556.                     $del->{'onclick'.= ";__adianti_post_exec('{$string_action}', tfieldlist_get_row_data(this), null, undefined, '1')";
  557.                 }
  558.                 
  559.                 $del->{'title'$this->remove_title $this->remove_title AdiantiCoreTranslator::translate('Delete');
  560.                 $del->add($this->remove_icon new TImage($this->remove_icon'<i class="fa fa-times red"></i>');
  561.                 $row->addCell$del );
  562.             }
  563.         }
  564.         
  565.         $this->detail_row ++;
  566.         
  567.         return $row;
  568.     }
  569.     
  570.     /**
  571.      * Add clone action
  572.      */
  573.     public function addCloneAction(TAction $clone_action null$icon null$title null)
  574.     {
  575.         if (!$this->body_created)
  576.         {
  577.             throw new Exception(AdiantiCoreTranslator::translate('You must call ^1 before ^2''addDetail''addCloneAction'));
  578.         }
  579.         
  580.         $this->tfoot parent::addSection('tfoot');
  581.         
  582.         $row parent::addRow();
  583.         
  584.         if ($this->sorting)
  585.         {
  586.             $row->addCell'' );
  587.         }
  588.         
  589.         if ($this->fields)
  590.         {
  591.             foreach ($this->fields as $field)
  592.             {
  593.                 $field_name $field->getName();
  594.                 
  595.                 $cell $row->addCell('');
  596.                 if ($field instanceof THidden)
  597.                 {
  598.                     $cell->{'style''display:none';
  599.                 }
  600.                 else if (isset($this->fields_properties[$field_name]['sum']&& $this->fields_properties[$field_name]['sum'== true)
  601.                 {
  602.                     $field_name str_replace('[]'''$field_name);
  603.                     $grand_total clone $field;
  604.                     $grand_total->setId($field_name.'_'.mt_rand(10000009999999));
  605.                     $grand_total->setName('grandtotal_'.$field_name);
  606.                     $grand_total->{'field_name'$field_name;
  607.                     $grand_total->setEditable(FALSE);
  608.                     $grand_total->{'style'}  .= ';font-weight:bold;border:0 !important;background:none';
  609.                     
  610.                     if (!empty($this->totals[$field_name]))
  611.                     {
  612.                         $grand_total->setValue($this->totals[$field_name]);
  613.                     }
  614.                     
  615.                     $cell->add($grand_total);
  616.                 }
  617.             }
  618.         }
  619.         
  620.         $all_actions array_merge(array) $this->row_functions(array) $this->row_actions );
  621.         
  622.         if ($all_actions)
  623.         {
  624.             foreach ($all_actions as $row_action)
  625.             {
  626.                 $cell $row->addCell('');
  627.             }
  628.         }
  629.         
  630.         $add new TElement('div');
  631.         $add->{'class''btn btn-default btn-sm';
  632.         $add->{'onclick'$this->clone_function;
  633.         $add->{'title'$title $title AdiantiCoreTranslator::translate('Add');
  634.         
  635.         if ($clone_action)
  636.         {
  637.             $string_action $clone_action->serialize(FALSE);
  638.             $add->{'onclick'"__adianti_post_exec('{$string_action}', tfieldlist_get_last_row_data(this), null, undefined, '1');".$add->{'onclick'};
  639.         }
  640.         
  641.         $add->add($icon new TImage($icon'<i class="fa fa-plus green"></i>');
  642.         
  643.         // add buttons in table
  644.         $row->addCell($add);
  645.     }
  646.     
  647.     /**
  648.      * Clear field list
  649.      * @param $name field list name
  650.      */
  651.     public static function clear($name)
  652.     {
  653.         TScript::create"tfieldlist_clear('{$name}'););
  654.     }
  655.     
  656.     /**
  657.      * Clear some field list rows
  658.      * @param $name     field list name
  659.      * @param $index    field list name
  660.      * @param $quantity field list name
  661.      */
  662.     public static function clearRows($name$start 0$length 0)
  663.     {
  664.         TScript::create"tfieldlist_clear_rows('{$name}', {$start}, {$length}););
  665.     }
  666.     
  667.     /**
  668.      * Add rows on field list
  669.      * @param $name     field list name
  670.      * @param $rows     quantity rows
  671.      * @param $timeout  timeout
  672.      */
  673.     public static function addRows($name$rows$timeout 50)
  674.     {
  675.         TScript::create"tfieldlist_add_rows('{$name}', {$rows}, {$timeout}););
  676.     }
  677.     
  678.     /**
  679.      * Enable scrolling
  680.      */
  681.     public function makeScrollable($height)
  682.     {
  683.         if (empty($this->tfoot))
  684.         {
  685.             throw new Exception(AdiantiCoreTranslator::translate('You must call ^1 before ^2''addCloneAction()''makeScrollable()'));
  686.         }
  687.         else
  688.         {
  689.             $this->thead->{'style'.= ';display:block';
  690.             $this->tbody->{'style'.= ';display:block;overflow-y: scroll;height:'.$height.'px';
  691.             $this->tbody->{'class'.= ' thin-scroll';
  692.             $this->tfoot->{'style'.= ';display:block;float:right;margin-right:10px';
  693.         }
  694.     }
  695.     
  696.     /**
  697.      * Show component
  698.      */
  699.     public function show()
  700.     {
  701.         parent::show();
  702.         $id $this->{'id'};
  703.         
  704.         if ($this->sorting)
  705.         {
  706.             if (empty($this->sort_action))
  707.             {
  708.                 TScript::create("ttable_sortable_rows('{$id}', '.handle')");
  709.             }
  710.             else
  711.             {
  712.                 if (!empty($this->fields))
  713.                 {
  714.                     $first_field array_values($this->fields)[0];
  715.                     $this->sort_action->setParameter('static''1');
  716.                     $form_name   $first_field->getFormName();
  717.                     $string_action $this->sort_action->serialize(FALSE);
  718.                     $sort_action "function() { __adianti_post_data('{$form_name}', '{$string_action}'); }";
  719.                     TScript::create("ttable_sortable_rows('{$id}', '.handle', $sort_action)");
  720.                 }
  721.             }
  722.         }
  723.     }
  724. }