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 TCheckList.php

Documentation is available at TCheckList.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Base\TScript;
  5. use Adianti\Widget\Datagrid\TDataGrid;
  6. use Adianti\Widget\Datagrid\TDataGridColumn;
  7. use Adianti\Database\TTransaction;
  8. use Adianti\Database\TCriteria;
  9. use Adianti\Wrapper\BootstrapDatagridWrapper;
  10. use Adianti\Widget\Wrapper\AdiantiDatabaseWidgetTrait;
  11. use Adianti\Validator\TFieldValidator;
  12.  
  13. /**
  14.  * Checklist
  15.  *
  16.  * @version    7.4
  17.  * @package    widget
  18.  * @subpackage form
  19.  * @author     Pablo Dall'Oglio
  20.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  21.  * @license    http://www.adianti.com.br/framework-license
  22.  */
  23. class TCheckList implements AdiantiWidgetInterface
  24. {
  25.     protected $datagrid;
  26.     protected $idColumn;
  27.     protected $fields;
  28.     protected $formName;
  29.     protected $name;
  30.     protected $value;
  31.     protected $validations;
  32.     protected $checkColumn;
  33.     protected $checkAllButton;
  34.     protected $width;
  35.     
  36.     use AdiantiDatabaseWidgetTrait;
  37.     
  38.     /**
  39.      * Construct method
  40.      */
  41.     public function __construct($name)
  42.     {
  43.         $this->datagrid = new BootstrapDatagridWrapper(new TDataGrid);
  44.         $this->datagrid->{'style''width: 100%';
  45.         $this->datagrid->{'widget''tchecklist';
  46.         $this->datagrid->disableDefaultClick()// important!
  47.         
  48.         $id $this->datagrid->{'id'};
  49.         
  50.         $check new TCheckButton('check_all_'.$id);
  51.         $check->setIndexValue('on');
  52.         $check->{'onclick'"tchecklist_select_all(this, '{$id}')";
  53.         $check->{'style''cursor:pointer';
  54.         $check->setProperty('class''filled-in');
  55.         $this->checkAllButton $check;
  56.         
  57.         $label new TLabel('');
  58.         $label->{'style''margin:0';
  59.         $label->{'class''checklist-label';
  60.         $check->after($label);
  61.         $label->{'for'$check->getId();
  62.         
  63.         $this->checkColumn $this->datagrid->addColumnnew TDataGridColumn('check',   $check->getContents(),   'center',  '1%') );
  64.         
  65.         $this->setName($name);
  66.         $this->value [];
  67.         $this->fields [];
  68.         $this->width '100%';
  69.     }
  70.     
  71.     /**
  72.      * Disable htmlspecialchars on output
  73.      */
  74.     public function disableHtmlConversion()
  75.     {
  76.         $this->datagrid->disableHtmlConversion();
  77.     }
  78.  
  79.     /**
  80.      * Set checklist size
  81.      */
  82.     public function setSize($size)
  83.     {
  84.         $this->width $size;
  85.         
  86.         if (strstr($size'%'!== FALSE)
  87.         {
  88.             $this->datagrid->{'style'.= ";width: {$size}";
  89.         }
  90.         else
  91.         {
  92.             $this->datagrid->{'style'.= ";width: {$size}px";
  93.         }
  94.     }
  95.     
  96.     /**
  97.      * Returns checklist size
  98.      */
  99.     function getSize()
  100.     {
  101.         return [$this->width$this->datagrid->getHeight()];
  102.     }
  103.     
  104.     /**
  105.      * Change Id
  106.      */
  107.     public function setId($id)
  108.     {
  109.         $this->checkAllButton->{'onclick'"tchecklist_select_all(this, '{$id}')";
  110.         $this->checkColumn->setLabel$this->checkAllButton->getContents() );
  111.         $this->datagrid->setId($id);
  112.     }
  113.     
  114.     /**
  115.      * Disable check all button
  116.      */
  117.     public function disableCheckAll()
  118.     {
  119.         $this->checkColumn->setLabel('');
  120.     }
  121.     
  122.     /**
  123.      * Define the field's name
  124.      * @param $name   A string containing the field's name
  125.      */
  126.     public function setName($name)
  127.     {
  128.         $this->name $name;
  129.     }
  130.  
  131.     /**
  132.      * Returns the field's name
  133.      */
  134.     public function getName()
  135.     {
  136.         return $this->name;
  137.     }
  138.     
  139.     /**
  140.      * Define the checklist selected ids
  141.      * @param $value 
  142.      */
  143.     public function setValue($value)
  144.     {
  145.         $this->value $value;
  146.         $id_column $this->idColumn;
  147.         $items $this->datagrid->getItems();
  148.         
  149.         if ($items)
  150.         {
  151.             foreach ($items as $item)
  152.             {
  153.                 $item->{'check'}->setValue(null);
  154.                 $position $this->datagrid->getRowIndex$id_column$item->$id_column );
  155.                 if (is_int($position))
  156.                 {
  157.                     $row $this->datagrid->getRow($position);
  158.                     $row->{'className''';
  159.                 }
  160.                 
  161.                 if ($this->value)
  162.                 {
  163.                     if (in_array($item->$id_column$this->value))
  164.                     {
  165.                         $item->{'check'}->setValue('on');
  166.                         
  167.                         if (is_int($position))
  168.                         {
  169.                             $row $this->datagrid->getRow($position);
  170.                             $row->{'className''selected';
  171.                         }
  172.                     }
  173.                 }
  174.             }
  175.         }
  176.     }
  177.     
  178.     /**
  179.      * Returns the selected ids
  180.      */
  181.     public function getValue()
  182.     {
  183.         return $this->value;
  184.     }
  185.     
  186.     /**
  187.      * Define the Identification column
  188.      */
  189.     public function setIdColumn($name)
  190.     {
  191.         $this->idColumn $name;
  192.     }
  193.     
  194.     /**
  195.      * Add list column
  196.      * @param  $name  = Name of the column in the database
  197.      * @param  $label = Text label that will be shown in the header
  198.      * @param  $align = Column align (left, center, right)
  199.      * @param  $width = Column Width (pixels)
  200.      */
  201.     public function addColumn($name$label$align$width)
  202.     {
  203.         if (empty($this->idColumn))
  204.         {
  205.             $this->idColumn $name;
  206.         }
  207.         
  208.         return $this->datagrid->addColumnnew TDataGridColumn($name$label$align$width) );
  209.     }
  210.     
  211.     /**
  212.      * Add item
  213.      */
  214.     public function addItem($object)
  215.     {
  216.         $id_column $this->idColumn;
  217.         $object->{'check'new TCheckButton('check_' $this->name '_' base64_encode($object->$id_column));
  218.         $object->{'check'}->setIndexValue('on');
  219.         $object->{'check'}->setProperty('class''filled-in');
  220.         $object->{'check'}->{'style''cursor:pointer';
  221.         
  222.         $label new TLabel('');
  223.         $label->{'style''margin:0';
  224.         $label->{'class''checklist-label';
  225.         $object->{'check'}->after($label);
  226.         $label->{'for'$object->{'check'}->getId();
  227.         
  228.         if (count($this->datagrid->getItems()) == 0)
  229.         {
  230.             $this->datagrid->createModel();
  231.         }
  232.         
  233.         $row $this->datagrid->addItem($object);
  234.         
  235.         if (in_array($object->$id_column$this->value))
  236.         {
  237.             $object->{'check'}->setValue('on');
  238.             $row->{'className''selected';
  239.         }
  240.         
  241.         $this->fields[$object->{'check'};
  242.         
  243.         $form TForm::getFormByName($this->formName);
  244.         if ($form)
  245.         {
  246.             $form->addField($object->{'check'});
  247.         }
  248.     }
  249.     
  250.     /**
  251.      * add Items
  252.      */
  253.     public function addItems($objects)
  254.     {
  255.         if ($objects)
  256.         {
  257.             foreach ($objects as $object)
  258.             {
  259.                 $this->addItem($object);
  260.             }
  261.         }
  262.     }
  263.     
  264.     /**
  265.      * Fill with model objects
  266.      */
  267.     public function fillWith($database$model$key$ordercolumn NULLTCriteria $criteria NULL)
  268.     {
  269.         TTransaction::open($database);
  270.         $this->addItems$this->getObjectsFromModel($database$model$key$ordercolumn$criteria) );
  271.         TTransaction::close();
  272.     }
  273.     
  274.     /**
  275.      * Clear datagrid
  276.      */
  277.     public function clear()
  278.     {
  279.         $this->datagrid->clear();
  280.     }
  281.     
  282.     /**
  283.      * Get fields
  284.      */
  285.     public function getFields()
  286.     {
  287.         return $this->fields;
  288.     }
  289.     
  290.     /**
  291.      * Define the name of the form to wich the field is attached
  292.      * @param $name    A string containing the name of the form
  293.      * @ignore-autocomplete on
  294.      */
  295.     public function setFormName($name)
  296.     {
  297.         $this->formName $name;
  298.     }
  299.     
  300.     /**
  301.      * Return the name of the form to wich the field is attached
  302.      */
  303.     public function getFormName()
  304.     {
  305.         return $this->formName;
  306.     }
  307.     
  308.     /**
  309.      * Redirect calls to decorated object
  310.      */
  311.     public function __call($method$parameters)
  312.     {
  313.         return call_user_func_array(array($this->datagrid$method),$parameters);
  314.     }
  315.     
  316.     /**
  317.      * Get post data
  318.      */
  319.     public function getPostData()
  320.     {
  321.         $value [];
  322.         $items $this->datagrid->getItems();
  323.         
  324.         $id_column $this->idColumn;
  325.         if ($items)
  326.         {
  327.             foreach ($items as $item)
  328.             {
  329.                 $field_name 'check_'.$this->name '_' base64_encode($item->$id_column);
  330.                 
  331.                 if (!empty($_POST[$field_name]&& $_POST[$field_name== 'on')
  332.                 {
  333.                     $value[$item->$id_column;
  334.                 }
  335.             }
  336.         }
  337.         
  338.         return $value;
  339.     }
  340.     
  341.     /**
  342.      * Add a field validator
  343.      * @param $label Field name
  344.      * @param $validator TFieldValidator object
  345.      * @param $parameters Aditional parameters
  346.      */
  347.     public function addValidation($labelTFieldValidator $validator$parameters NULL)
  348.     {
  349.         $this->validations[array($label$validator$parameters);
  350.     }
  351.     
  352.     /**
  353.      * Returns field validations
  354.      */
  355.     public function getValidations()
  356.     {
  357.         return $this->validations;
  358.     }
  359.     
  360.     /**
  361.      * Validate a field
  362.      */
  363.     public function validate()
  364.     {
  365.         if ($this->validations)
  366.         {
  367.             foreach ($this->validations as $validation)
  368.             {
  369.                 $label      $validation[0];
  370.                 $validator  $validation[1];
  371.                 $parameters $validation[2];
  372.                 
  373.                 $validator->validate($label$this->getValue()$parameters);
  374.             }
  375.         }
  376.     }
  377.     
  378.     /**
  379.      * Show checklist
  380.      */
  381.     public function show()
  382.     {
  383.         if (count($this->datagrid->getItems()) == 0)
  384.         {
  385.             $this->datagrid->createModel();
  386.         }
  387.         
  388.         $this->datagrid->show();
  389.         
  390.         $id $this->datagrid->{'id'};
  391.         TScript::create("tchecklist_row_enable_check('{$id}')");
  392.     }
  393. }