|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ADT\QueryObjectDataSource; |
| 4 | + |
| 5 | +interface IQueryObjectDataSourceFactory { |
| 6 | + |
| 7 | + /** @return QueryObjectDataSource */ |
| 8 | + function create(\Kdyby\Doctrine\QueryObject $queryObject, $primaryKey); |
| 9 | + |
| 10 | +} |
| 11 | + |
| 12 | + |
| 13 | +class QueryObjectDataSource extends \Nette\Object implements \Ublaboo\DataGrid\DataSource\IDataSource { |
| 14 | + |
| 15 | + /** @var \Kdyby\Doctrine\EntityManager */ |
| 16 | + protected $em; |
| 17 | + |
| 18 | + /** @var \Kdyby\Doctrine\ResultSet */ |
| 19 | + protected $resultSet; |
| 20 | + |
| 21 | + /** @var \Kdyby\Doctrine\QueryObject */ |
| 22 | + protected $queryObject; |
| 23 | + |
| 24 | + /** @var string */ |
| 25 | + protected $primaryKey; |
| 26 | + |
| 27 | + /** @var callable */ |
| 28 | + public $filterCallback; |
| 29 | + |
| 30 | + /** @var callable */ |
| 31 | + public $filterOneCallback; |
| 32 | + |
| 33 | + /** @var callable */ |
| 34 | + public $sortCallback; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param \Kdyby\Doctrine\QueryObject $queryObject |
| 38 | + * @param string $primaryKey |
| 39 | + * @param \Kdyby\Doctrine\EntityManager $em |
| 40 | + */ |
| 41 | + public function __construct(\Kdyby\Doctrine\QueryObject $queryObject, $primaryKey, \Kdyby\Doctrine\EntityManager $em) { |
| 42 | + $this->queryObject = $queryObject; |
| 43 | + $this->primaryKey = $primaryKey; |
| 44 | + $this->em = $em; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param callable $callback |
| 49 | + * @return self |
| 50 | + */ |
| 51 | + public function setFilterCallback($callback) { |
| 52 | + $this->filterCallback = $callback; |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param callable $callback |
| 58 | + * @return self |
| 59 | + */ |
| 60 | + public function setFilterOneCallback($callback) { |
| 61 | + $this->filterOneCallback = $callback; |
| 62 | + return $this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param callable $callback |
| 67 | + * @return self |
| 68 | + */ |
| 69 | + public function setSortCallback($callback) { |
| 70 | + $this->sortCallback = $callback; |
| 71 | + return $this; |
| 72 | + } |
| 73 | + |
| 74 | + protected function getResultSet() { |
| 75 | + if (!$this->resultSet) { |
| 76 | + $this->resultSet = $this->em->getRepository($this->queryObject->getEntity()) |
| 77 | + ->fetch($this->queryObject); |
| 78 | + } |
| 79 | + |
| 80 | + return $this->resultSet; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get count of data |
| 85 | + * @return int |
| 86 | + */ |
| 87 | + public function getCount() { |
| 88 | + return $this->em->getRepository($this->queryObject->getEntity()) |
| 89 | + ->fetch($this->queryObject) |
| 90 | + ->getTotalCount(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the data |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function getData() { |
| 98 | + return $this->getResultSet()->toArray(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Filter data |
| 103 | + * @param array $filters |
| 104 | + * @return static |
| 105 | + */ |
| 106 | + public function filter(array $filters) { |
| 107 | + foreach ($filters as $filter) { |
| 108 | + if ($filter->isValueSet()) { |
| 109 | + if ($filter->hasConditionCallback()) { |
| 110 | + \Nette\Utils\Callback::invokeArgs( |
| 111 | + $filter->getConditionCallback(), [ $this->queryObject, $filter->getValue() ] |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if (is_callable($this->filterCallback)) { |
| 118 | + $this->filterCallback($this->queryObject, $filters); |
| 119 | + } |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Filter data - get one row |
| 126 | + * @param array $filter |
| 127 | + * @return static |
| 128 | + */ |
| 129 | + public function filterOne(array $filter) { |
| 130 | + |
| 131 | + if (is_callable($this->filterOneCallback)) { |
| 132 | + $this->filterOneCallback($this->queryObject, $filter); |
| 133 | + } |
| 134 | + |
| 135 | + return $this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Apply limit and offset on data |
| 140 | + * @param int $offset |
| 141 | + * @param int $limit |
| 142 | + * @return static |
| 143 | + */ |
| 144 | + public function limit($offset, $limit) { |
| 145 | + $this->getResultSet()->applyPaging($offset, $limit); |
| 146 | + |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sort data |
| 152 | + * @param \Ublaboo\DataGrid\Utils\Sorting $sorting |
| 153 | + * @return static |
| 154 | + */ |
| 155 | + public function sort(\Ublaboo\DataGrid\Utils\Sorting $sorting) { |
| 156 | + |
| 157 | + if (is_callable($this->sortCallback)) { |
| 158 | + $this->sortCallback($this->queryObject, $sorting); |
| 159 | + } |
| 160 | + |
| 161 | + return $this; |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments