This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AModel.php
536 lines (426 loc) · 13.4 KB
/
AModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
/***************************************************************************
* *
* (c) 2004 Vladimir V. Kalynyak, Alexey V. Vinokurov, Ilya M. Shalnev *
* *
* This is commercial software, only users who have purchased a valid *
* license and accept to the terms of the License Agreement can install *
* and use this program. *
* *
****************************************************************************
* PLEASE READ THE FULL TEXT OF THE SOFTWARE LICENSE AGREEMENT IN THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE. *
****************************************************************************/
namespace Tygh\Models;
use Tygh\Languages\Languages;
use Tygh\Models\Components\Fields;
use Tygh\Models\Components\Sorting;
use Tygh\Models\Components\Joins;
use Tygh\Models\Components\Condition;
use Tygh\Models\Components\Limit;
use Tygh\Navigation\LastView;
abstract class AModel implements IModel, \IteratorAggregate, \ArrayAccess
{
protected static $models = array();
protected $id;
protected $attributes = array();
protected $params = array();
protected static $_enum_elements = array();
public function __construct($params = array(), $attributes = array())
{
$this->params = array_merge(array(
'lang_code' => CART_LANGUAGE,
), $params);
if (!empty($attributes)) {
$this->load($attributes);
}
}
public function __set($name, $value)
{
$this->attributes[$name] = $value;
}
public function __get($name)
{
if (isset($this->attributes[$name])) {
return $this->attributes[$name];
}
return null;
}
public function __isset($name)
{
return isset($this->attributes[$name]);
}
public function __unset($name)
{
unset($this->attributes[$name]);
}
/**
* Returns whether there is an element at the specified offset.
* This method is required by the interface ArrayAccess.
*
* @param mixed $offset the offset to check on
* @return boolean
*/
public function offsetExists($offset)
{
return property_exists($this, $offset);
}
/**
* Returns the element at the specified offset.
* This method is required by the interface ArrayAccess.
*
* @param integer $offset the offset to retrieve element.
* @return mixed the element at the offset, null if no element is found at the offset
*/
public function offsetGet($offset)
{
return $this->$offset;
}
/**
* Sets the element at the specified offset.
* This method is required by the interface ArrayAccess.
*
* @param integer $offset the offset to set element
* @param mixed $item the element value
*/
public function offsetSet($offset, $item)
{
$this->$offset = $item;
}
/**
* Unsets the element at the specified offset.
* This method is required by the interface ArrayAccess.
* @param mixed $offset the offset to unset element
*/
public function offsetUnset($offset)
{
unset($this->$offset);
}
/**
* Returns an iterator for traversing the attributes in the model.
* This method is required by the interface IteratorAggregate.
*
* @return CMapIterator an iterator for traversing the items in the list.
*/
public function getIterator()
{
return new Iterator($this->attributes);
}
public static function model($params = array())
{
$class = get_called_class();
if (!isset(static::$models[$class])) {
static::$models[$class] = new static($params);
}
return static::$models[$class];
}
public function findMany($params = array())
{
$this->beforeFind($params);
$params = array_merge($this->params, $params);
// Init filter
if ($last_view_object_name = $this->getLastViewObjectName()) {
$params = LastView::instance()->update($last_view_object_name, $params);
}
$fields = new Fields($this, $params);
$sorting = new Sorting($this, $params);
$joins = new Joins($this, $params);
$condition = new Condition($this, $params, $joins);
$this->prepareQuery($params, $fields->result, $sorting->result, $joins->result, $condition->result);
$limit = new Limit($this, $params, $joins, $condition);
if (!empty($params['get_count']) && isset($params['total_items'])) {
return $params['total_items'];
}
$query_foundation =
" FROM " . $this->getTableName()
. $joins->get()
. $condition->get()
. $sorting->get()
. $limit->get()
;
if (!empty($params['get_ids'])) {
return db_get_fields("SELECT " . $this->getTableName() . "." . $this->getPrimaryField() . $query_foundation);
}
$items = db_get_array("SELECT " . $fields->get() . $query_foundation);
$this->gatherAdditionalItemsData($items, $params);
if (!empty($params['to_array'])) {
$models = $items;
} else {
$models = $this->loadMany($items, true);
}
if (!empty($params['return_params'])) {
return array($models, $params);
}
return $models;
}
public function find($id, $params = array())
{
if (is_array($id) && empty($params)) {
$params = $id;
$id = 0;
}
if (!empty($id)) {
$params['ids'] = $id;
}
$params['limit'] = 1;
$items = $this->findMany($params);
return reset($items);
}
public function findAll($params = array())
{
return $this->findMany($params);
}
public function attributes($attributes = array())
{
if (!empty($attributes)) {
if (!is_array($attributes) && is_a($attributes, 'Tygh\Models\IModel')) {
$attributes = $attributes->attributes();
}
if (is_array($attributes)) {
$this->attributes = array_merge($this->attributes, $attributes);
}
}
return $this->attributes;
}
public function save()
{
if ($this->beforeSave()) {
$result = $this->isNewRecord() ? $this->insert() : $this->update();
$this->load($this->find($this->id));
$this->afterSave();
return $result;
}
return false;
}
public function delete()
{
$is_deleted = false;
if (!$this->isNewRecord() && $this->beforeDelete()) {
$table_name = $this->getTableName();
$description_table_name = $this->getDescriptionTableName();
$primary_field = $this->getPrimaryField();
$is_deleted = db_query(
sprintf('DELETE FROM %s WHERE %s = ?s', $table_name, $primary_field),
$this->id
);
if ($is_deleted && !empty($description_table_name)) {
$is_deleted = db_query(
sprintf("DELETE FROM %s WHERE %s = ?s", $description_table_name, $primary_field),
$this->id
);
}
if ($is_deleted) {
$this->id = null;
$this->afterDelete();
}
}
return $is_deleted;
}
public function deleteMany($params)
{
$models = $this->findMany($params);
foreach ($models as $model) {
$model->delete();
}
return true;
}
protected function insert()
{
$table_name = $this->getTableName();
$description_table_name = $this->getDescriptionTableName();
$primary_field = $this->getPrimaryField();
$_data = $this->prepareAttributes();
$result = db_query("INSERT INTO $table_name ?e", $_data);
if ($this->primaryAutoIncrement()) {
$this->id = $result;
} else {
$this->id = $this->{$primary_field};
}
if (!empty($description_table_name)) {
$_data[$primary_field] = $this->id;
foreach (Languages::getAll() as $_data['lang_code'] => $v) {
db_query("INSERT INTO $description_table_name ?e", $_data);
}
}
$this->{$primary_field} = $this->id;
return true;
}
protected function update()
{
$table_name = $this->getTableName();
$primary_field = $this->getPrimaryField();
$description_table_name = $this->getDescriptionTableName();
$_data = $this->prepareAttributes();
db_query("UPDATE $table_name SET ?u WHERE $primary_field = ?s", $_data, $this->id);
if (!empty($description_table_name)) {
db_query(
"UPDATE $description_table_name SET ?u WHERE $primary_field = ?s AND lang_code = ?s",
$_data, $this->id, $this->params['lang_code']
);
}
return true;
}
public function isNewRecord()
{
return empty($this->id);
}
public function getFields($params)
{
return array(
$this->getTableName() . '.*',
);
}
protected function prepareAttributes()
{
$attributes = $this->attributes;
if ($this->primaryAutoIncrement()) {
if (isset($attributes[$this->getPrimaryField()])) {
unset($attributes[$this->getPrimaryField()]);
}
}
return $attributes;
}
protected function load($attributes, $find = false)
{
$primary_field = $this->getPrimaryField();
if (isset($attributes[$primary_field])) {
$this->id = $attributes[$primary_field];
}
$this->attributes($attributes);
if ($find) {
$this->afterFind();
}
}
protected function loadMany($items, $find = false)
{
$models = array();
foreach ($items as $item) {
$model = new static(array(
'lang_code' => $this->params['lang_code'],
));
$model->load($item, $find);
$models[] = $model;
}
return $models;
}
/**
* Setting DB table name
* @return str
*/
abstract public function getTableName();
/**
* Setting DB table name of related multi-lamguage data.
* @return str
*/
public function getDescriptionTableName()
{
return '';
}
/**
* Autoincrement enabled?
* @return bool
*/
protected function primaryAutoIncrement()
{
return true;
}
/**
* Getting default sort directino.
* @return str asc or desc
*/
public function getSortDefaultDirection()
{
return 'asc';
}
/**
* Ability to rewrite query parts.
*
* @param array $params Params
* @param array $fields Fields
* @param array $sorting Sorting
* @param array $joins Joins
* @param array $condition Condition
* @param string $limit Limit
*/
public function prepareQuery(&$params, &$fields, &$sorting, &$joins, &$condition)
{
}
/**
* Setting extra conditions
*
* @param array $params Params
* @return array Conditions
*/
public function getExtraCondition($params)
{
return array();
}
/**
* Gather additional items data. Items modified by link.
*
* @param array &$items Selected items
* @param array $params Params
*/
protected function gatherAdditionalItemsData(&$items, $params)
{
}
/**
* Getting query joins
* @param array $params params
* @return array
*/
public function getJoins($params)
{
$joins = array();
$description_table_name = $this->getDescriptionTableName();
if (!empty($description_table_name)) {
$table_name = $this->getTableName();
$primary_field = $this->getPrimaryField();
$joins[] = db_quote(
" LEFT JOIN $description_table_name"
. " ON $description_table_name.$primary_field = $table_name.$primary_field"
. " AND $description_table_name.lang_code = ?s", $this->params['lang_code']
);
}
return $joins;
}
/**
* Getting search fields schema. Available keys: number, range, in, not_in, string, text, time. For more info see: Condition::prepare()
* @return array
*/
public function getSearchFields()
{
return array();
}
public function getSortFields()
{
return array(
'id' => $this->getPrimaryField()
);
}
public function getLastViewObjectName()
{
return false; // disabled by default
}
// Events
public function beforeFind(&$params)
{
}
public function afterFind()
{
}
public function beforeSave()
{
return true;
}
public function afterSave()
{
}
public function beforeDelete()
{
return true;
}
public function afterDelete()
{
}
}