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

Documentation is available at TSortList.php

  1. <?php
  2. namespace Adianti\Widget\Form;
  3.  
  4. use Adianti\Widget\Form\AdiantiWidgetInterface;
  5. use Adianti\Widget\Base\TElement;
  6. use Adianti\Widget\Base\TScript;
  7. use Adianti\Widget\Form\TField;
  8. use Adianti\Widget\Util\TImage;
  9. use Adianti\Core\AdiantiCoreTranslator;
  10. use Adianti\Control\TAction;
  11.  
  12. use Exception;
  13.  
  14. /**
  15.  * A Sortable list
  16.  *
  17.  * @version    7.4
  18.  * @package    widget
  19.  * @subpackage form
  20.  * @author     Pablo Dall'Oglio
  21.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  22.  * @license    http://www.adianti.com.br/framework-license
  23.  */
  24. class TSortList extends TField implements AdiantiWidgetInterface
  25. {
  26.     private $initialItems;
  27.     private $items;
  28.     private $valueSet;
  29.     private $connectedTo;
  30.     private $itemIcon;
  31.     private $changeAction;
  32.     private $orientation;
  33.     private $limit;
  34.     protected $id;
  35.     protected $changeFunction;
  36.     protected $width;
  37.     protected $height;
  38.     protected $separator;
  39.     
  40.     /**
  41.      * Class Constructor
  42.      * @param  $name widget's name
  43.      */
  44.     public function __construct($name)
  45.     {
  46.         // executes the parent class constructor
  47.         parent::__construct($name);
  48.         $this->id   = 'tsortlist_'.mt_rand(10000000001999999999);
  49.         
  50.         $this->initialItems array();
  51.         $this->items array();
  52.         $this->limit = -1;
  53.         
  54.         // creates a <ul> tag
  55.         $this->tag = new TElement('ul');
  56.         $this->tag->{'class''tsortlist';
  57.         $this->tag->{'itemname'$name;
  58.     }
  59.     
  60.     /**
  61.      * Define orientation
  62.      * @param $orienatation (horizontal, vertical)
  63.      */
  64.     public function setOrientation($orientation)
  65.     {
  66.         $this->orientation $orientation;
  67.     }
  68.     
  69.     /**
  70.      * Define limit
  71.      */
  72.     public function setLimit($limit)
  73.     {
  74.         $this->limit $limit;
  75.     }
  76.     
  77.     /**
  78.      * Define the item icon
  79.      * @param $image Item icon
  80.      */
  81.     public function setItemIcon(TImage $icon)
  82.     {
  83.         $this->itemIcon $icon;
  84.     }
  85.     
  86.     /**
  87.      * Define the list size
  88.      */
  89.     public function setSize($width$height NULL)
  90.     {
  91.         $this->width $width;
  92.         $this->height $height;
  93.     }
  94.     
  95.     /**
  96.      * Define the field's separator
  97.      * @param $sep A string containing the field's separator
  98.      */
  99.     public function setValueSeparator($sep)
  100.     {
  101.         $this->separator $sep;
  102.     }
  103.     
  104.     /**
  105.      * Define the field's value
  106.      * @param $value An array the field's values
  107.      */
  108.     public function setValue($value)
  109.     {
  110.         if (!empty($this->separator))
  111.         {
  112.             $value explode($this->separator$value);
  113.         }
  114.         
  115.         $items $this->initialItems;
  116.         if (is_array($value))
  117.         {
  118.             $this->items array();
  119.             foreach ($value as $index)
  120.             {
  121.                 if (isset($items[$index]))
  122.                 {
  123.                     $this->items[$index$items[$index];
  124.                 }
  125.                 else if (isset($this->connectedToAND is_array($this->connectedTo))
  126.                 {
  127.                     foreach ($this->connectedTo as $connectedList)
  128.                     {
  129.                         if (isset($connectedList->initialItems[$index) )
  130.                         {
  131.                             $this->items[$index$connectedList->initialItems[$index];
  132.                         }
  133.                     }
  134.                 }
  135.             }
  136.             $this->valueSet TRUE;
  137.         }
  138.     }
  139.     
  140.     /**
  141.      * Connect to another list
  142.      * @param $list Another TSortList
  143.      */
  144.     public function connectTo(TSortList $list)
  145.     {
  146.         $this->connectedTo[$list;
  147.     }
  148.     
  149.     /**
  150.      * Add items to the sort list
  151.      * @param $items An indexed array containing the options
  152.      */
  153.     public function addItems($items)
  154.     {
  155.         if (is_array($items))
  156.         {
  157.             $this->initialItems += $items;
  158.             $this->items += $items;
  159.         }
  160.     }
  161.     
  162.     /**
  163.      * Return the sort items
  164.      */
  165.     public function getItems()
  166.     {
  167.         return $this->initialItems;
  168.     }
  169.     
  170.     /**
  171.      * Return the post data
  172.      */
  173.     public function getPostData()
  174.     {
  175.         if (isset($_POST[$this->name]))
  176.         {
  177.             if (empty($this->separator))
  178.             {
  179.                 return $_POST[$this->name];
  180.             }
  181.             else
  182.             {
  183.                 return implode($this->separator$_POST[$this->name]);
  184.             }
  185.         }
  186.         else
  187.         {
  188.             return array();
  189.         }
  190.     }
  191.     
  192.     /**
  193.      * Define the action to be executed when the user changes the combo
  194.      * @param $action TAction object
  195.      */
  196.     public function setChangeAction(TAction $action)
  197.     {
  198.         if ($action->isStatic())
  199.         {
  200.             $this->changeAction $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 change function
  211.      */
  212.     public function setChangeFunction($function)
  213.     {
  214.         $this->changeFunction $function;
  215.     }
  216.     
  217.     /**
  218.      * Enable the field
  219.      */
  220.     public static function enableField($form_name$field)
  221.     {
  222.         TScript::create" tsortlist_enable_field('{$form_name}', '{$field}'); );
  223.     }
  224.     
  225.     /**
  226.      * Disable the field
  227.      */
  228.     public static function disableField($form_name$field)
  229.     {
  230.         TScript::create" tsortlist_disable_field('{$form_name}', '{$field}'); );
  231.     }
  232.     
  233.     /**
  234.      * Clear the field
  235.      */
  236.     public static function clearField($form_name$field)
  237.     {
  238.         TScript::create" tsortlist_clear_field('{$form_name}', '{$field}'); );
  239.     }
  240.     
  241.     /**
  242.      * Shows the widget at the screen
  243.      */
  244.     public function show()
  245.     {
  246.         $this->tag->{'id'$this->id;
  247.         
  248.         $this->setProperty('style'(strstr($this->width'%'!== FALSE)  "width:{$this->width};"   "width:{$this->width}px;",   false)//aggregate style info
  249.         $this->setProperty('style'(strstr($this->height'%'!== FALSE"height:{$this->height};"height:{$this->height}px;"false)//aggregate style info
  250.         
  251.         if ($this->orientation == 'horizontal')
  252.         {
  253.             $this->tag->{'itemdisplay''inline-block';
  254.         }
  255.         else
  256.         {
  257.             $this->tag->{'itemdisplay''block';
  258.         }
  259.         
  260.         if ($this->items)
  261.         {
  262.             $i 1;
  263.             // iterate the checkgroup options
  264.             foreach ($this->items as $index => $label)
  265.             {
  266.                 // control to reduce available options when they are present
  267.                 // in another connected list as a post value
  268.                 if ($this->connectedTo AND is_array($this->connectedTo))
  269.                 {
  270.                     foreach ($this->connectedTo as $connectedList)
  271.                     {
  272.                         if (isset($connectedList->items[$index]AND $connectedList->valueSet )
  273.                         {
  274.                             continue 2;
  275.                         }
  276.                     }
  277.                 }
  278.  
  279.                 // instantiates a new Item
  280.                 $item new TElement('li');
  281.                 
  282.                 if ($this->itemIcon)
  283.                 {
  284.                     $item->add($this->itemIcon);
  285.                 }
  286.  
  287.                 $label new TLabel($label);
  288.                 $label->{'style''width: 100%;';
  289.  
  290.                 $item->add($label);
  291.                 $item->{'class''tsortlist_item btn btn-default';
  292.                 $item->{'style''display:block;';
  293.                 $item->{'id'"tsortlist_{$this->name}_item_{$i}_li";
  294.                 $item->{'title'$this->tag->title;
  295.                 
  296.                 if ($this->orientation == 'horizontal')
  297.                 {
  298.                     $item->{'style''display:inline-block';
  299.                 }
  300.                 
  301.                 $input new TElement('input');
  302.                 $input->{'id'}   "tsortlist_{$this->name}_item_{$i}_li_input";
  303.                 $input->{'type''hidden';
  304.                 $input->{'name'$this->name '[]';
  305.                 $input->{'value'$index;
  306.                 $item->add($input);
  307.                 
  308.                 $this->tag->add($item);
  309.                 $i ++;
  310.             }
  311.         }
  312.         
  313.         if (parent::getEditable())
  314.         {
  315.             $change_action 'function() {}';
  316.             if (isset($this->changeAction))
  317.             {
  318.                 if (!TForm::getFormByName($this->formNameinstanceof TForm)
  319.                 {
  320.                     throw new Exception(AdiantiCoreTranslator::translate('You must pass the ^1 (^2) as a parameter to ^3'__CLASS__$this->name'TForm::setFields()') );
  321.                 }            
  322.                 $string_action $this->changeAction->serialize(FALSE);
  323.                 $change_action "function() { __adianti_post_lookup('{$this->formName}', '{$string_action}', '{$this->id}', 'callback'); }";
  324.             }
  325.             
  326.             if (isset($this->changeFunction))
  327.             {
  328.                 $change_action "function() { $this->changeFunction }";
  329.             }
  330.             
  331.             $connect 'false';
  332.             if ($this->connectedTo AND is_array($this->connectedTo))
  333.             {
  334.                 foreach ($this->connectedTo as $connectedList)
  335.                 {
  336.                     $connectIds[=  '#'.$connectedList->getId();
  337.                 }
  338.                 $connect implode(', '$connectIds);
  339.             }
  340.             TScript::create(" tsortlist_start( '#{$this->id}', '{$connect}', $change_action$this->limit ) ");
  341.         }
  342.         $this->tag->show();
  343.     }
  344. }