1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\Sql\DataObjects\Result; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database; |
19
|
|
|
use O2System\Framework\Models\Sql\Model; |
20
|
|
|
use O2System\Spl\DataStructures\SplArrayObject; |
21
|
|
|
use O2System\Spl\Info\SplClassInfo; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Row |
25
|
|
|
* |
26
|
|
|
* @package O2System\Framework\Models\Sql\DataObjects |
27
|
|
|
*/ |
28
|
|
|
class Row extends SplArrayObject |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Row::$model |
32
|
|
|
* |
33
|
|
|
* @var \O2System\Spl\Info\SplClassInfo |
34
|
|
|
*/ |
35
|
|
|
private $model; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Row::$record |
39
|
|
|
* |
40
|
|
|
* @var \O2System\Database\DataObjects\Result\Row\Record |
41
|
|
|
*/ |
42
|
|
|
private $record; |
43
|
|
|
|
44
|
|
|
// ------------------------------------------------------------------------ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Row::__construct |
48
|
|
|
* |
49
|
|
|
* @param mixed $row |
50
|
|
|
* @param Model $model |
51
|
|
|
*/ |
52
|
|
|
public function __construct($row, Model &$model) |
53
|
|
|
{ |
54
|
|
|
$this->model = new SplClassInfo($model); |
55
|
|
|
$this->record = $row->getRecord(); |
56
|
|
|
|
57
|
|
|
$hideColumns = []; |
58
|
|
|
|
59
|
|
|
// Visible Columns |
60
|
|
|
if (count($model->visibleColumns)) { |
61
|
|
|
$hideColumns = array_diff($row->getColumns(), $model->visibleColumns); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Final rebuild row columns |
65
|
|
|
$model->rebuildRow($row); |
66
|
|
|
|
67
|
|
|
// Hide Columns |
68
|
|
|
if (count($model->hideColumns)) { |
69
|
|
|
$hideColumns = array_merge($model->hideColumns); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($row instanceof Database\DataObjects\Result\Row) { |
73
|
|
|
parent::__construct($row->getArrayCopy()); |
74
|
|
|
} elseif (is_array($row)) { |
75
|
|
|
parent::__construct($row); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Append Columns |
79
|
|
|
if (count($model->appendColumns)) { |
80
|
|
|
foreach ($model->appendColumns as $appendColumn) { |
81
|
|
|
$this->offsetSet($appendColumn, $this->offsetGet($appendColumn)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Unset Columns |
86
|
|
|
foreach ($hideColumns as $column) { |
87
|
|
|
$this->offsetUnset($column); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
models($this->model->getClass())->row = $this; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// ------------------------------------------------------------------------ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Row::offsetGet |
97
|
|
|
* |
98
|
|
|
* @param string $offset |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function offsetGet($offset) |
103
|
|
|
{ |
104
|
|
|
if ($this->offsetExists($offset)) { |
105
|
|
|
return parent::offsetGet($offset); |
106
|
|
|
} elseif(strpos($offset, 'record') !== false) { |
107
|
|
|
switch ($offset) { |
108
|
|
|
case 'record': |
109
|
|
|
return $this->record; |
110
|
|
|
break; |
|
|
|
|
111
|
|
|
case 'record_status': |
112
|
|
|
return $this->record->status; |
113
|
|
|
break; |
114
|
|
|
case 'record_left': |
115
|
|
|
return $this->record->left; |
116
|
|
|
break; |
117
|
|
|
case 'record_right': |
118
|
|
|
return $this->record->right; |
119
|
|
|
break; |
120
|
|
|
case 'record_depth': |
121
|
|
|
return $this->record->depth; |
122
|
|
|
break; |
123
|
|
|
case 'record_ordering': |
124
|
|
|
return $this->record->ordering; |
125
|
|
|
break; |
126
|
|
|
case 'record_create_user': |
127
|
|
|
return $this->record->create->user; |
128
|
|
|
break; |
129
|
|
|
case 'record_create_timestamp': |
130
|
|
|
return $this->record->create->timestamp; |
131
|
|
|
break; |
132
|
|
|
case 'record_update_user': |
133
|
|
|
return $this->record->update->user; |
134
|
|
|
break; |
135
|
|
|
case 'record_update_timestamp': |
136
|
|
|
return $this->record->update->timestamp; |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
} elseif (null !== ($result = $this->__call($offset))) { |
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// ------------------------------------------------------------------------ |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Row::__call |
150
|
|
|
* |
151
|
|
|
* @param string $method |
152
|
|
|
* @param array $args |
153
|
|
|
* |
154
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null |
155
|
|
|
*/ |
156
|
|
|
public function __call($method, $args = []) |
157
|
|
|
{ |
158
|
|
|
$model = models($this->model->getClass()); |
159
|
|
|
|
160
|
|
|
if (method_exists($model, $method)) { |
161
|
|
|
$model->row = $this; |
|
|
|
|
162
|
|
|
|
163
|
|
|
if (false !== ($result = call_user_func_array([&$model, $method], $args))) { |
164
|
|
|
$this->offsetSet($method, $result); |
165
|
|
|
|
166
|
|
|
return $result; |
167
|
|
|
} |
168
|
|
|
} elseif (strpos($method, 'Url')) { |
169
|
|
|
$key = str_replace('Url', '', $method); |
170
|
|
|
|
171
|
|
|
if ($key === $model->uploadedImageKey) { |
|
|
|
|
172
|
|
|
if (isset($model->uploadedImageFilePath)) { |
|
|
|
|
173
|
|
|
if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) { |
174
|
|
|
return images_url($filePath); |
175
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) { |
176
|
|
|
return images_url($filePath); |
177
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) { |
178
|
|
|
return images_url($filePath); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} elseif (in_array($key, $model->uploadedImageKeys)) { |
|
|
|
|
182
|
|
|
if (isset($model->uploadedImageFilePath)) { |
183
|
|
|
if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) { |
184
|
|
|
return images_url($filePath); |
185
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) { |
186
|
|
|
return images_url($filePath); |
187
|
|
|
} elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) { |
188
|
|
|
return images_url($filePath); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} elseif ($key === $model->uploadedFileKey) { |
|
|
|
|
192
|
|
|
if (isset($model->uploadedFileFilepath)) { |
|
|
|
|
193
|
|
|
return storage_url($model->uploadedFileFilepath . $this->offsetGet($key)); |
194
|
|
|
} |
195
|
|
|
} elseif (in_array($key, $model->uploadedFileKeys)) { |
|
|
|
|
196
|
|
|
if (isset($model->uploadedFileFilepath)) { |
197
|
|
|
return storage_url($model->uploadedFileFilepath . $this->offsetGet($key)); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return null; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// ------------------------------------------------------------------------ |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Row::delete |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
* @throws \O2System\Spl\Exceptions\RuntimeException |
212
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
213
|
|
|
*/ |
214
|
|
|
public function delete() |
215
|
|
|
{ |
216
|
|
|
$model = models($this->model->getClass()); |
217
|
|
|
|
218
|
|
|
$primaryKey = isset($model->primaryKey) ? $model->primaryKey : 'id'; |
|
|
|
|
219
|
|
|
$id = $this->offsetGet($primaryKey); |
220
|
|
|
|
221
|
|
|
if (method_exists($model, 'beforeDelete')) { |
222
|
|
|
$model->beforeDelete(); |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ($model->qb->table($model->table)->limit(1)->delete([$primaryKey => $id])) { |
|
|
|
|
226
|
|
|
if (method_exists($model, 'afterDelete')) { |
227
|
|
|
return $model->afterDelete(); |
|
|
|
|
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return true; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
} |