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\DataSources;
 13 
 14 use Grido\Components\Filters\Condition;
 15 
 16 /**
 17  * Nette Database data source.
 18  *
 19  * @package     Grido
 20  * @subpackage  DataSources
 21  * @author      Petr Bugyík
 22  *
 23  * @property-read \Nette\Database\Table\Selection $selection
 24  * @property-read int $count
 25  * @property-read array $data
 26  */
 27 class NetteDatabase extends \Nette\Object implements IDataSource
 28 {
 29     /** @var \Nette\Database\Table\Selection */
 30     protected $selection;
 31 
 32     /**
 33      * @param \Nette\Database\Table\Selection $selection
 34      */
 35     public function __construct(\Nette\Database\Table\Selection $selection)
 36     {
 37         $this->selection = $selection;
 38     }
 39 
 40     /**
 41      * @return \Nette\Database\Table\Selection
 42      */
 43     public function getSelection()
 44     {
 45         return $this->selection;
 46     }
 47 
 48     /**
 49      * @param Condition $condition
 50      * @param \Nette\Database\Table\Selection $selection
 51      */
 52     protected function makeWhere(Condition $condition, \Nette\Database\Table\Selection $selection = NULL)
 53     {
 54         $selection = $selection === NULL
 55             ? $this->selection
 56             : $selection;
 57 
 58         if ($condition->callback) {
 59             callback($condition->callback)->invokeArgs(array($condition->value, $selection));
 60         } else {
 61             call_user_func_array(array($selection, 'where'), $condition->__toArray());
 62         }
 63     }
 64 
 65     /********************************** inline editation helpers ************************************/
 66 
 67     /**
 68      * Default callback for an inline editation save.
 69      * @param mixed $id
 70      * @param array $values
 71      * @param string $idCol
 72      * @return bool
 73      */
 74     public function update($id, array $values, $idCol)
 75     {
 76         return (bool) $this->getSelection()
 77             ->where(array($idCol => $id)) //TODO: column escaping requires https://github.com/nette/nette/issues/1324
 78             ->update($values);
 79     }
 80 
 81     /**
 82      * Default callback used when an editable column has customRender.
 83      * @param mixed $id
 84      * @param string $idCol
 85      * @return \Nette\Database\Table\ActiveRow
 86      */
 87     public function getRow($id, $idCol)
 88     {
 89         return $this->getSelection()
 90             ->where(array($idCol => $id)) //TODO: column escaping requires https://github.com/nette/nette/issues/1324
 91             ->fetch();
 92     }
 93 
 94     /********************************** interface IDataSource ************************************/
 95 
 96     /**
 97      * @return int
 98      */
 99     public function getCount()
100     {
101         return (int) $this->selection->count('*');
102     }
103 
104     /**
105      * @return array
106      */
107     public function getData()
108     {
109         return $this->selection;
110     }
111 
112     /**
113      * @param array $conditions
114      */
115     public function filter(array $conditions)
116     {
117         foreach ($conditions as $condition) {
118             $this->makeWhere($condition);
119         }
120     }
121 
122     /**
123      * @param int $offset
124      * @param int $limit
125      */
126     public function limit($offset, $limit)
127     {
128         $this->selection->limit($limit, $offset);
129     }
130 
131     /**
132      * @param array $sorting
133      */
134     public function sort(array $sorting)
135     {
136         foreach ($sorting as $column => $sort) {
137             $this->selection->order("$column $sort");
138         }
139     }
140 
141     /**
142      * @param mixed $column
143      * @param array $conditions
144      * @param int $limit
145      * @return array
146      * @throws \InvalidArgumentException
147      */
148     public function suggest($column, array $conditions, $limit)
149     {
150         $selection = clone $this->selection;
151         is_string($column) && $selection->select("DISTINCT $column");
152         $selection->limit($limit);
153 
154         foreach ($conditions as $condition) {
155             $this->makeWhere($condition, $selection);
156         }
157 
158         $items = array();
159         foreach ($selection as $row) {
160             if (is_string($column)) {
161                 $value = (string) $row[$column];
162             } elseif (is_callable($column)) {
163                 $value = (string) $column($row);
164             } else {
165                 $type = gettype($column);
166                 throw new \InvalidArgumentException("Column of suggestion must be string or callback, $type given.");
167             }
168 
169             $items[$value] = \Nette\Templating\Helpers::escapeHtml($value);
170         }
171 
172         return array_values($items);
173     }
174 }
175 
Grido@master API documentation generated by ApiGen