Grido@master
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download

Namespaces

  • Grido
    • Components
      • Actions
      • Columns
      • Filters
    • DataSources
    • PropertyAccessors
    • Translations

Classes

  • Grido\Translations\FileTranslator
  1 <?php
  2 
  3 /**
  4  * This file is part of the Grido (https://github.com/o5/grido)
  5  *
  6  * Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
  7  *
  8  * For the full copyright and license information, please view
  9  * the file LICENSE.md that was distributed with this source code.
 10  */
 11 
 12 namespace Grido\Components;
 13 
 14 use Grido\Grid;
 15 use Grido\Helpers;
 16 
 17 /**
 18  * Operation with one or more rows.
 19  *
 20  * @package     Grido
 21  * @subpackage  Components
 22  * @author      Petr Bugyík
 23  *
 24  * @property-read string $primaryKey
 25  */
 26 class Operation extends Component
 27 {
 28     const ID = 'operations';
 29 
 30     /** @var array callback on operation submit */
 31     public $onSubmit;
 32 
 33     /** @var string */
 34     protected $primaryKey;
 35 
 36     /**
 37      * @param \Grido\Grid $grid
 38      * @param array $operations
 39      * @param callback $onSubmit - callback after operation submit
 40      */
 41     public function __construct($grid, array $operations, $onSubmit)
 42     {
 43         $this->grid = $grid;
 44         $grid->addComponent($this, self::ID);
 45 
 46         $grid['form'][$grid::BUTTONS]->addSubmit(self::ID, 'OK')
 47             ->onClick[] = $this->handleOperations;
 48 
 49         $grid['form']->addContainer(self::ID)
 50             ->addSelect(self::ID, 'Selected', $operations)
 51             ->setPrompt('Grido.Selected');
 52 
 53         $that = $this;
 54         $grid->onRender[] = function(Grid $grid) use ($that) {
 55             $that->addCheckers($grid['form'][Operation::ID]);
 56         };
 57 
 58         $this->onSubmit[] = $onSubmit;
 59     }
 60 
 61     /**
 62      * Sets client side confirm for operation.
 63      * @param string $operation
 64      * @param string $message
 65      * @return Operation
 66      */
 67     public function setConfirm($operation, $message)
 68     {
 69         $message = $this->translate($message);
 70         $this->grid->onRender[] = function(Grid $grid) use ($operation, $message) {
 71             $grid['form'][Operation::ID][Operation::ID]->controlPrototype->data["grido-confirm-$operation"] = $message;
 72         };
 73 
 74         return $this;
 75     }
 76 
 77     /**
 78      * Sets primary key.
 79      * @param string $primaryKey
 80      * @return Operation
 81      */
 82     public function setPrimaryKey($primaryKey)
 83     {
 84         $this->primaryKey = $primaryKey;
 85         return $this;
 86     }
 87 
 88     /**********************************************************************************************/
 89 
 90     /**
 91      * @return string
 92      */
 93     public function getPrimaryKey()
 94     {
 95         if ($this->primaryKey === NULL) {
 96             $this->primaryKey = $this->grid->primaryKey;
 97         }
 98 
 99         return $this->primaryKey;
100     }
101 
102     /**********************************************************************************************/
103 
104     /**
105      * @param \Nette\Forms\Controls\SubmitButton $button
106      * @internal
107      */
108     public function handleOperations(\Nette\Forms\Controls\SubmitButton $button)
109     {
110         $this->grid->onRegistered && $this->grid->onRegistered($this->grid);
111         $form = $button->getForm();
112         $this->addCheckers($form[self::ID]);
113 
114         $values = $form[self::ID]->values;
115         if (empty($values[self::ID])) {
116             $httpData = $form->getHttpData();
117             if (!empty($httpData[self::ID][self::ID]) && $operation = $httpData[self::ID][self::ID]) {
118                 trigger_error("Operation with name '$operation' does not exist.", E_USER_NOTICE);
119             }
120 
121             $this->grid->reload();
122         }
123 
124         $ids = array();
125         $operation = $values[self::ID];
126         unset($values[self::ID]);
127 
128         foreach ($values as $key => $val) {
129             if ($val) {
130                 $ids[] = $key;
131             }
132         }
133 
134         $this->onSubmit($operation, $ids);
135     }
136 
137     /**
138      * @param \Nette\Forms\Container $container
139      * @internal
140      */
141     public function addCheckers(\Nette\Forms\Container $container)
142     {
143         $items = $this->grid->getData();
144         $primaryKey = $this->getPrimaryKey();
145         $propertyAccessor = $this->grid->getPropertyAccessor();
146 
147         foreach ($items as $item) {
148             $primaryValue = $propertyAccessor->getProperty($item, $primaryKey);
149             if (!isset($container[$primaryValue])) {
150                 $container->addCheckbox(Helpers::formatColumnName($primaryValue));
151             }
152         }
153     }
154 }
155 
Grido@master API documentation generated by ApiGen