1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22:
23: class GroupedTableSelection extends TableSelection
24: {
25:
26: protected $refTable;
27:
28:
29: protected $column;
30:
31:
32: protected $active;
33:
34:
35:
36: 37: 38: 39: 40: 41:
42: public function __construct(TableSelection $refTable, $table, $column)
43: {
44: parent::__construct($table, $refTable->connection);
45: $this->refTable = $refTable;
46: $this->column = $column;
47: }
48:
49:
50:
51: 52: 53: 54: 55: 56:
57: public function setActive($active)
58: {
59: $this->active = $active;
60: return $this;
61: }
62:
63:
64:
65:
66: public function through($column)
67: {
68: trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::related("' . $this->name . '", "' . $column . '") instead.', E_USER_WARNING);
69: $this->column = $column;
70: $this->delimitedColumn = $this->refTable->connection->getSupplementalDriver()->delimite($this->column);
71: return $this;
72: }
73:
74:
75:
76: public function select($columns)
77: {
78: if (!$this->sqlBuilder->getSelect()) {
79: $this->sqlBuilder->addSelect("$this->name.$this->column");
80: }
81:
82: return parent::select($columns);
83: }
84:
85:
86:
87: public function order($columns)
88: {
89: if (!$this->sqlBuilder->getOrder()) {
90:
91: $this->sqlBuilder->addOrder("$this->name.$this->column" . (preg_match('~\bDESC\z~i', $columns) ? ' DESC' : ''));
92: }
93:
94: return parent::order($columns);
95: }
96:
97:
98:
99:
100:
101:
102:
103: public function aggregation($function)
104: {
105: $aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->sqlBuilder->buildSelectQuery() . json_encode($this->sqlBuilder->getParameters())];
106:
107: if ($aggregation === NULL) {
108: $aggregation = array();
109:
110: $selection = $this->createSelectionInstance();
111: $selection->getSqlBuilder()->importConditions($this->getSqlBuilder());
112: $selection->select($function);
113: $selection->select("$this->name.$this->column");
114: $selection->group("$this->name.$this->column");
115:
116: foreach ($selection as $row) {
117: $aggregation[$row[$this->column]] = $row;
118: }
119: }
120:
121: if (isset($aggregation[$this->active])) {
122: foreach ($aggregation[$this->active] as $val) {
123: return $val;
124: }
125: }
126: }
127:
128:
129:
130: public function count($column = NULL)
131: {
132: $return = parent::count($column);
133: return isset($return) ? $return : 0;
134: }
135:
136:
137:
138:
139:
140:
141:
142: protected function execute()
143: {
144: if ($this->rows !== NULL) {
145: return;
146: }
147:
148: $hash = md5($this->sqlBuilder->buildSelectQuery() . json_encode($this->sqlBuilder->getParameters()));
149:
150: $referencing = & $this->getRefTable($refPath)->referencing[$refPath . $hash];
151: $this->rows = & $referencing['rows'];
152: $this->referenced = & $referencing['refs'];
153: $this->accessed = & $referencing['accessed'];
154: $refData = & $referencing['data'];
155:
156: if ($refData === NULL) {
157: $limit = $this->sqlBuilder->getLimit();
158: $rows = count($this->refTable->rows);
159: if ($limit && $rows > 1) {
160: $this->sqlBuilder->setLimit(NULL, NULL);
161: }
162: parent::execute();
163: $this->sqlBuilder->setLimit($limit, NULL);
164: $refData = array();
165: $offset = array();
166: foreach ($this->rows as $key => $row) {
167: $ref = & $refData[$row[$this->column]];
168: $skip = & $offset[$row[$this->column]];
169: if ($limit === NULL || $rows <= 1 || (count($ref) < $limit && $skip >= $this->sqlBuilder->getOffset())) {
170: $ref[$key] = $row;
171: } else {
172: unset($this->rows[$key]);
173: }
174: $skip++;
175: unset($ref, $skip);
176: }
177: }
178:
179: $this->data = & $refData[$this->active];
180: if ($this->data === NULL) {
181: $this->data = array();
182: } else {
183: foreach ($this->data as $row) {
184: $row->setTable($this);
185: }
186: reset($this->data);
187: }
188: }
189:
190:
191:
192: protected function getRefTable(& $refPath)
193: {
194: $refObj = $this->refTable;
195: $refPath = $this->name . '.';
196: while ($refObj instanceof GroupedTableSelection) {
197: $refPath .= $refObj->name . '.';
198: $refObj = $refObj->refTable;
199: }
200:
201: return $refObj;
202: }
203:
204:
205:
206:
207:
208:
209:
210: public function insert($data)
211: {
212: if ($data instanceof Traversable && !$data instanceof TableSelection) {
213: $data = iterator_to_array($data);
214: }
215:
216: if (Validators::isList($data)) {
217: foreach (array_keys($data) as $key) {
218: $data[$key][$this->column] = $this->active;
219: }
220: } else {
221: $data[$this->column] = $this->active;
222: }
223:
224: return parent::insert($data);
225: }
226:
227:
228:
229: public function update($data)
230: {
231: $builder = $this->sqlBuilder;
232:
233: $this->sqlBuilder = new SqlBuilder($this);
234: $this->where($this->column, $this->active);
235: $return = parent::update($data);
236:
237: $this->sqlBuilder = $builder;
238: return $return;
239: }
240:
241:
242:
243: public function delete()
244: {
245: $builder = $this->sqlBuilder;
246:
247: $this->sqlBuilder = new SqlBuilder($this);
248: $this->where($this->column, $this->active);
249: $return = parent::delete();
250:
251: $this->sqlBuilder = $builder;
252: return $return;
253: }
254:
255: }
256: