@@ -13,99 +13,99 @@ discard block |
||
13 | 13 | |
14 | 14 | class Material extends \Model { |
15 | 15 | |
16 | - public static $objectName = 'Материал'; |
|
17 | - public static $categoryModel = 'Materials\Category'; |
|
18 | - public static $labels = [ |
|
19 | - 'name' => 'Заголовок', |
|
20 | - 'category_id' => 'Раздел', |
|
21 | - 'preview' => 'Краткое превью', |
|
22 | - 'text' => 'Текст страницы', |
|
23 | - 'alias' => 'Алиас страницы', |
|
24 | - 'template' => 'Шаблон сайта', |
|
25 | - 'viewer' => 'Тип страницы', |
|
26 | - 'image_file_id' => 'Фото материала', |
|
27 | - 'description' => 'Описание для поисковиков', |
|
28 | - 'keywords' => 'Ключевые слова', |
|
29 | - 'user_id' => 'Создатель', |
|
30 | - 'date_create' => 'Дата создания', |
|
31 | - 'tag_list' => 'Теги' |
|
32 | - ]; |
|
33 | - public static $dataManagers = [ |
|
34 | - 'manager' => [ |
|
35 | - 'cols' => [ |
|
36 | - 'name', |
|
37 | - 'alias', |
|
38 | - 'category_id', |
|
39 | - 'date_create', |
|
40 | - 'tag_list' |
|
41 | - ], |
|
42 | - 'sortable' => [ |
|
43 | - 'name', |
|
44 | - 'alias', |
|
45 | - 'category_id', |
|
46 | - 'date_create' |
|
47 | - ], |
|
48 | - 'filters' => [ |
|
49 | - 'name', |
|
50 | - 'preview', |
|
51 | - 'text', |
|
52 | - 'alias', |
|
53 | - 'template', |
|
54 | - 'viewer', |
|
55 | - 'description', |
|
56 | - 'keywords', |
|
57 | - 'user_id', |
|
58 | - 'date_create' |
|
59 | - ], |
|
60 | - 'categorys' => [ |
|
61 | - 'model' => 'Materials\Category', |
|
62 | - ], |
|
63 | - 'sortMode' => true |
|
64 | - ] |
|
65 | - ]; |
|
66 | - public static $cols = [ |
|
67 | - 'name' => ['type' => 'text'], |
|
68 | - 'alias' => ['type' => 'text'], |
|
69 | - 'preview' => ['type' => 'html'], |
|
70 | - 'text' => ['type' => 'html'], |
|
71 | - 'keywords' => ['type' => 'text'], |
|
72 | - 'description' => ['type' => 'text'], |
|
73 | - 'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'], |
|
74 | - 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
|
75 | - 'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesList', 'module' => 'Materials'], |
|
76 | - 'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsList', 'module' => 'Materials'], |
|
77 | - 'default' => ['type' => 'bool'], |
|
78 | - 'hidden' => ['type' => 'bool'], |
|
79 | - 'image_file_id' => ['type' => 'image'], |
|
80 | - 'link' => ['type' => 'dataManager', 'relation' => 'links'], |
|
81 | - 'tree_path' => ['type' => 'text'], |
|
82 | - 'weight' => ['type' => 'number'], |
|
83 | - 'date_create' => ['type' => 'dateTime'], |
|
84 | - 'tag_list' => ['type' => 'text'], |
|
85 | - ]; |
|
86 | - public static $forms = [ |
|
87 | - 'manager' => [ |
|
88 | - 'options' => [ |
|
89 | - 'access' => [ |
|
90 | - 'groups' => [ |
|
91 | - 3 |
|
92 | - ] |
|
93 | - ] |
|
94 | - ], |
|
95 | - 'map' => [ |
|
96 | - ['name', 'category_id'], |
|
97 | - ['alias', 'image_file_id'], |
|
98 | - ['template', 'viewer'], |
|
99 | - ['keywords', 'description'], |
|
100 | - ['tag_list'], |
|
101 | - ['preview'], |
|
102 | - ['text'], |
|
103 | - ['link'], |
|
104 | - ] |
|
105 | - ] |
|
106 | - ]; |
|
16 | + public static $objectName = 'Материал'; |
|
17 | + public static $categoryModel = 'Materials\Category'; |
|
18 | + public static $labels = [ |
|
19 | + 'name' => 'Заголовок', |
|
20 | + 'category_id' => 'Раздел', |
|
21 | + 'preview' => 'Краткое превью', |
|
22 | + 'text' => 'Текст страницы', |
|
23 | + 'alias' => 'Алиас страницы', |
|
24 | + 'template' => 'Шаблон сайта', |
|
25 | + 'viewer' => 'Тип страницы', |
|
26 | + 'image_file_id' => 'Фото материала', |
|
27 | + 'description' => 'Описание для поисковиков', |
|
28 | + 'keywords' => 'Ключевые слова', |
|
29 | + 'user_id' => 'Создатель', |
|
30 | + 'date_create' => 'Дата создания', |
|
31 | + 'tag_list' => 'Теги' |
|
32 | + ]; |
|
33 | + public static $dataManagers = [ |
|
34 | + 'manager' => [ |
|
35 | + 'cols' => [ |
|
36 | + 'name', |
|
37 | + 'alias', |
|
38 | + 'category_id', |
|
39 | + 'date_create', |
|
40 | + 'tag_list' |
|
41 | + ], |
|
42 | + 'sortable' => [ |
|
43 | + 'name', |
|
44 | + 'alias', |
|
45 | + 'category_id', |
|
46 | + 'date_create' |
|
47 | + ], |
|
48 | + 'filters' => [ |
|
49 | + 'name', |
|
50 | + 'preview', |
|
51 | + 'text', |
|
52 | + 'alias', |
|
53 | + 'template', |
|
54 | + 'viewer', |
|
55 | + 'description', |
|
56 | + 'keywords', |
|
57 | + 'user_id', |
|
58 | + 'date_create' |
|
59 | + ], |
|
60 | + 'categorys' => [ |
|
61 | + 'model' => 'Materials\Category', |
|
62 | + ], |
|
63 | + 'sortMode' => true |
|
64 | + ] |
|
65 | + ]; |
|
66 | + public static $cols = [ |
|
67 | + 'name' => ['type' => 'text'], |
|
68 | + 'alias' => ['type' => 'text'], |
|
69 | + 'preview' => ['type' => 'html'], |
|
70 | + 'text' => ['type' => 'html'], |
|
71 | + 'keywords' => ['type' => 'text'], |
|
72 | + 'description' => ['type' => 'text'], |
|
73 | + 'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'], |
|
74 | + 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
|
75 | + 'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesList', 'module' => 'Materials'], |
|
76 | + 'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsList', 'module' => 'Materials'], |
|
77 | + 'default' => ['type' => 'bool'], |
|
78 | + 'hidden' => ['type' => 'bool'], |
|
79 | + 'image_file_id' => ['type' => 'image'], |
|
80 | + 'link' => ['type' => 'dataManager', 'relation' => 'links'], |
|
81 | + 'tree_path' => ['type' => 'text'], |
|
82 | + 'weight' => ['type' => 'number'], |
|
83 | + 'date_create' => ['type' => 'dateTime'], |
|
84 | + 'tag_list' => ['type' => 'text'], |
|
85 | + ]; |
|
86 | + public static $forms = [ |
|
87 | + 'manager' => [ |
|
88 | + 'options' => [ |
|
89 | + 'access' => [ |
|
90 | + 'groups' => [ |
|
91 | + 3 |
|
92 | + ] |
|
93 | + ] |
|
94 | + ], |
|
95 | + 'map' => [ |
|
96 | + ['name', 'category_id'], |
|
97 | + ['alias', 'image_file_id'], |
|
98 | + ['template', 'viewer'], |
|
99 | + ['keywords', 'description'], |
|
100 | + ['tag_list'], |
|
101 | + ['preview'], |
|
102 | + ['text'], |
|
103 | + ['link'], |
|
104 | + ] |
|
105 | + ] |
|
106 | + ]; |
|
107 | 107 | |
108 | - public static function relations() { |
|
108 | + public static function relations() { |
|
109 | 109 | return [ |
110 | 110 | 'category' => [ |
111 | 111 | 'model' => '\Materials\Category', |
@@ -125,43 +125,43 @@ discard block |
||
125 | 125 | 'col' => 'material_id' |
126 | 126 | ] |
127 | 127 | ]; |
128 | - } |
|
128 | + } |
|
129 | 129 | |
130 | - public function getHref() { |
|
130 | + public function getHref() { |
|
131 | 131 | $href = !empty(\App::$primary->config['defaultModule']) && \App::$primary->config['defaultModule'] == 'Materials' ? '' : '/materials'; |
132 | 132 | $treePath = array_filter(explode('/', $this->tree_path)); |
133 | 133 | if ($treePath) { |
134 | - $categorys = Category::getList(['where' => ['id', implode(',', $treePath), 'IN']]); |
|
135 | - foreach ($categorys as $category) { |
|
134 | + $categorys = Category::getList(['where' => ['id', implode(',', $treePath), 'IN']]); |
|
135 | + foreach ($categorys as $category) { |
|
136 | 136 | if ($category->alias) { |
137 | - $href .="/{$category->alias}"; |
|
137 | + $href .="/{$category->alias}"; |
|
138 | + } |
|
138 | 139 | } |
139 | - } |
|
140 | 140 | } |
141 | 141 | if ((!$href || $href == '/materials') && !$this->alias) { |
142 | - return ($href ? $href : '/materials') . '/view/' . $this->pk(); |
|
142 | + return ($href ? $href : '/materials') . '/view/' . $this->pk(); |
|
143 | 143 | } |
144 | 144 | return $href . "/" . ($this->alias ? $this->alias : $this->pk()); |
145 | - } |
|
145 | + } |
|
146 | 146 | |
147 | - public function resolveTemplate() { |
|
147 | + public function resolveTemplate() { |
|
148 | 148 | if ($this->template !== 'inherit') { |
149 | - return $this->template; |
|
149 | + return $this->template; |
|
150 | 150 | } elseif ($this->template == 'inherit' && $this->category) { |
151 | - return $this->category->resolveTemplate(true); |
|
151 | + return $this->category->resolveTemplate(true); |
|
152 | 152 | } else { |
153 | - return 'current'; |
|
153 | + return 'current'; |
|
154 | + } |
|
154 | 155 | } |
155 | - } |
|
156 | 156 | |
157 | - public function resolveViewer() { |
|
157 | + public function resolveViewer() { |
|
158 | 158 | if ($this->viewer !== 'inherit') { |
159 | - return $this->viewer; |
|
159 | + return $this->viewer; |
|
160 | 160 | } elseif ($this->viewer == 'inherit' && $this->category) { |
161 | - return $this->category->resolveViewer(true); |
|
161 | + return $this->category->resolveViewer(true); |
|
162 | 162 | } else { |
163 | - return 'default'; |
|
163 | + return 'default'; |
|
164 | + } |
|
164 | 165 | } |
165 | - } |
|
166 | 166 | |
167 | 167 | } |
@@ -134,14 +134,14 @@ |
||
134 | 134 | $categorys = Category::getList(['where' => ['id', implode(',', $treePath), 'IN']]); |
135 | 135 | foreach ($categorys as $category) { |
136 | 136 | if ($category->alias) { |
137 | - $href .="/{$category->alias}"; |
|
137 | + $href .= "/{$category->alias}"; |
|
138 | 138 | } |
139 | 139 | } |
140 | 140 | } |
141 | 141 | if ((!$href || $href == '/materials') && !$this->alias) { |
142 | - return ($href ? $href : '/materials') . '/view/' . $this->pk(); |
|
142 | + return ($href ? $href : '/materials').'/view/'.$this->pk(); |
|
143 | 143 | } |
144 | - return $href . "/" . ($this->alias ? $this->alias : $this->pk()); |
|
144 | + return $href."/".($this->alias ? $this->alias : $this->pk()); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | public function resolveTemplate() { |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | /** |
383 | 383 | * Information extractor for col relations path |
384 | 384 | * |
385 | - * @param string|array $info |
|
385 | + * @param string $info |
|
386 | 386 | * @return array |
387 | 387 | */ |
388 | 388 | public static function parseColRecursion($info) { |
@@ -465,7 +465,7 @@ discard block |
||
465 | 465 | * Generate params string for col by name |
466 | 466 | * |
467 | 467 | * @param string $colName |
468 | - * @return boolean|string |
|
468 | + * @return false|string |
|
469 | 469 | */ |
470 | 470 | public static function genColParams($colName) { |
471 | 471 | if (empty(static::$cols[$colName]) || static::$storage['type'] == 'moduleConfig') { |
@@ -619,7 +619,7 @@ discard block |
||
619 | 619 | /** |
620 | 620 | * return relations list |
621 | 621 | * |
622 | - * @return array |
|
622 | + * @return string |
|
623 | 623 | */ |
624 | 624 | public static function relations() { |
625 | 625 | return []; |
@@ -1127,7 +1127,7 @@ discard block |
||
1127 | 1127 | * |
1128 | 1128 | * @param array $params |
1129 | 1129 | * @param array $where |
1130 | - * @return boolean |
|
1130 | + * @return false|null |
|
1131 | 1131 | */ |
1132 | 1132 | public static function update($params, $where = []) { |
1133 | 1133 | static::fixPrefix($params); |
@@ -10,354 +10,354 @@ discard block |
||
10 | 10 | */ |
11 | 11 | class Model { |
12 | 12 | |
13 | - /** |
|
14 | - * Object storage type |
|
15 | - * |
|
16 | - * @var array |
|
17 | - */ |
|
18 | - public static $storage = ['type' => 'db']; |
|
19 | - |
|
20 | - /** |
|
21 | - * Object name |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - public static $objectName = ''; |
|
26 | - |
|
27 | - /** |
|
28 | - * App type for separate data storage |
|
29 | - * |
|
30 | - * @var string |
|
31 | - */ |
|
32 | - public $appType = 'app'; |
|
33 | - |
|
34 | - /** |
|
35 | - * Object current params |
|
36 | - * |
|
37 | - * @var array |
|
38 | - */ |
|
39 | - public $_params = []; |
|
40 | - |
|
41 | - /** |
|
42 | - * List of changed params in current instance |
|
43 | - * |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - public $_changedParams = []; |
|
47 | - |
|
48 | - /** |
|
49 | - * Loaded relations |
|
50 | - * |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - public $loadedRelations = []; |
|
54 | - |
|
55 | - /** |
|
56 | - * Model name where this model uses as category |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - public static $treeCategory = ''; |
|
61 | - |
|
62 | - /** |
|
63 | - * Model name who uses as category in this model |
|
64 | - * |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - public static $categoryModel = ''; |
|
68 | - |
|
69 | - /** |
|
70 | - * Col labels |
|
71 | - * |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - public static $labels = []; |
|
75 | - |
|
76 | - /** |
|
77 | - * Model forms |
|
78 | - * |
|
79 | - * @var array |
|
80 | - */ |
|
81 | - public static $forms = []; |
|
82 | - |
|
83 | - /** |
|
84 | - * Model cols |
|
85 | - * |
|
86 | - * @var array |
|
87 | - */ |
|
88 | - public static $cols = []; |
|
89 | - |
|
90 | - /** |
|
91 | - * Options group for display inforamtion from model |
|
92 | - * |
|
93 | - * @var array |
|
94 | - */ |
|
95 | - public static $view = []; |
|
96 | - |
|
97 | - /** |
|
98 | - * List of relations need loaded with item |
|
99 | - * |
|
100 | - * @var array |
|
101 | - */ |
|
102 | - public static $needJoin = []; |
|
103 | - |
|
104 | - /** |
|
105 | - * List of joins who need to laod |
|
106 | - * |
|
107 | - * @var array |
|
108 | - */ |
|
109 | - public static $relJoins = []; |
|
110 | - |
|
111 | - /** |
|
112 | - * Set params when model create |
|
113 | - * |
|
114 | - * @param array $params |
|
115 | - */ |
|
116 | - public function __construct($params = []) { |
|
13 | + /** |
|
14 | + * Object storage type |
|
15 | + * |
|
16 | + * @var array |
|
17 | + */ |
|
18 | + public static $storage = ['type' => 'db']; |
|
19 | + |
|
20 | + /** |
|
21 | + * Object name |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + public static $objectName = ''; |
|
26 | + |
|
27 | + /** |
|
28 | + * App type for separate data storage |
|
29 | + * |
|
30 | + * @var string |
|
31 | + */ |
|
32 | + public $appType = 'app'; |
|
33 | + |
|
34 | + /** |
|
35 | + * Object current params |
|
36 | + * |
|
37 | + * @var array |
|
38 | + */ |
|
39 | + public $_params = []; |
|
40 | + |
|
41 | + /** |
|
42 | + * List of changed params in current instance |
|
43 | + * |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + public $_changedParams = []; |
|
47 | + |
|
48 | + /** |
|
49 | + * Loaded relations |
|
50 | + * |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + public $loadedRelations = []; |
|
54 | + |
|
55 | + /** |
|
56 | + * Model name where this model uses as category |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + public static $treeCategory = ''; |
|
61 | + |
|
62 | + /** |
|
63 | + * Model name who uses as category in this model |
|
64 | + * |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + public static $categoryModel = ''; |
|
68 | + |
|
69 | + /** |
|
70 | + * Col labels |
|
71 | + * |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + public static $labels = []; |
|
75 | + |
|
76 | + /** |
|
77 | + * Model forms |
|
78 | + * |
|
79 | + * @var array |
|
80 | + */ |
|
81 | + public static $forms = []; |
|
82 | + |
|
83 | + /** |
|
84 | + * Model cols |
|
85 | + * |
|
86 | + * @var array |
|
87 | + */ |
|
88 | + public static $cols = []; |
|
89 | + |
|
90 | + /** |
|
91 | + * Options group for display inforamtion from model |
|
92 | + * |
|
93 | + * @var array |
|
94 | + */ |
|
95 | + public static $view = []; |
|
96 | + |
|
97 | + /** |
|
98 | + * List of relations need loaded with item |
|
99 | + * |
|
100 | + * @var array |
|
101 | + */ |
|
102 | + public static $needJoin = []; |
|
103 | + |
|
104 | + /** |
|
105 | + * List of joins who need to laod |
|
106 | + * |
|
107 | + * @var array |
|
108 | + */ |
|
109 | + public static $relJoins = []; |
|
110 | + |
|
111 | + /** |
|
112 | + * Set params when model create |
|
113 | + * |
|
114 | + * @param array $params |
|
115 | + */ |
|
116 | + public function __construct($params = []) { |
|
117 | 117 | $this->setParams($params); |
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * return object name |
|
122 | - * |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - public static function objectName() { |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * return object name |
|
122 | + * |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + public static function objectName() { |
|
126 | 126 | return static::$objectName; |
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Retrn col value with col params and relations path |
|
131 | - * |
|
132 | - * @param Model $object |
|
133 | - * @param string $valuePath |
|
134 | - * @param boolean $convert |
|
135 | - * @param boolean $manageHref |
|
136 | - * @return string |
|
137 | - */ |
|
138 | - public static function getColValue($object, $valuePath, $convert = false, $manageHref = false) { |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Retrn col value with col params and relations path |
|
131 | + * |
|
132 | + * @param Model $object |
|
133 | + * @param string $valuePath |
|
134 | + * @param boolean $convert |
|
135 | + * @param boolean $manageHref |
|
136 | + * @return string |
|
137 | + */ |
|
138 | + public static function getColValue($object, $valuePath, $convert = false, $manageHref = false) { |
|
139 | 139 | if (strpos($valuePath, ':')) { |
140 | - $rel = substr($valuePath, 0, strpos($valuePath, ':')); |
|
141 | - $param = substr($valuePath, strpos($valuePath, ':') + 1); |
|
142 | - if (!$object->$rel) { |
|
140 | + $rel = substr($valuePath, 0, strpos($valuePath, ':')); |
|
141 | + $param = substr($valuePath, strpos($valuePath, ':') + 1); |
|
142 | + if (!$object->$rel) { |
|
143 | 143 | $modelName = get_class($object); |
144 | 144 | $relations = $modelName::relations(); |
145 | 145 | if (empty($relations[$rel]['type']) || $relations[$rel]['type'] == 'one') { |
146 | - return $object->{$relations[$rel]['col']}; |
|
146 | + return $object->{$relations[$rel]['col']}; |
|
147 | 147 | } |
148 | 148 | return 0; |
149 | - } |
|
150 | - if (strpos($valuePath, ':')) { |
|
149 | + } |
|
150 | + if (strpos($valuePath, ':')) { |
|
151 | 151 | return self::getColValue($object->$rel, $param, $convert, $manageHref); |
152 | - } else { |
|
152 | + } else { |
|
153 | 153 | return $convert ? Model::resloveTypeValue($object->$rel, $param, $manageHref) : $object->$rel->$param; |
154 | - } |
|
154 | + } |
|
155 | 155 | } else { |
156 | - return $convert ? Model::resloveTypeValue($object, $valuePath, $manageHref) : $object->$valuePath; |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Retrun value for view |
|
162 | - * |
|
163 | - * @param Model $item |
|
164 | - * @param string $colName |
|
165 | - * @param boolean $manageHref |
|
166 | - * @return string |
|
167 | - */ |
|
168 | - public static function resloveTypeValue($item, $colName, $manageHref = false) { |
|
156 | + return $convert ? Model::resloveTypeValue($object, $valuePath, $manageHref) : $object->$valuePath; |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Retrun value for view |
|
162 | + * |
|
163 | + * @param Model $item |
|
164 | + * @param string $colName |
|
165 | + * @param boolean $manageHref |
|
166 | + * @return string |
|
167 | + */ |
|
168 | + public static function resloveTypeValue($item, $colName, $manageHref = false) { |
|
169 | 169 | $modelName = get_class($item); |
170 | 170 | $colInfo = $modelName::getColInfo($colName); |
171 | 171 | $type = !empty($colInfo['colParams']['type']) ? $colInfo['colParams']['type'] : 'string'; |
172 | 172 | $value = ''; |
173 | 173 | switch ($type) { |
174 | - case 'select': |
|
174 | + case 'select': |
|
175 | 175 | switch ($colInfo['colParams']['source']) { |
176 | - case 'model': |
|
176 | + case 'model': |
|
177 | 177 | $sourceValue = ''; |
178 | 178 | if ($item->$colName) { |
179 | - $sourceValue = $colInfo['colParams']['model']::get($item->$colName); |
|
179 | + $sourceValue = $colInfo['colParams']['model']::get($item->$colName); |
|
180 | 180 | } |
181 | 181 | $value = $sourceValue ? $sourceValue->name() : 'Не задано'; |
182 | 182 | break; |
183 | - case 'array': |
|
183 | + case 'array': |
|
184 | 184 | $value = !empty($colInfo['colParams']['sourceArray'][$item->$colName]) ? $colInfo['colParams']['sourceArray'][$item->$colName] : 'Не задано'; |
185 | 185 | if (is_array($value) && $value['text']) { |
186 | - $value = $value['text']; |
|
186 | + $value = $value['text']; |
|
187 | 187 | } |
188 | 188 | break; |
189 | - case 'bool': |
|
189 | + case 'bool': |
|
190 | 190 | return $item->$colName ? 'Да' : 'Нет'; |
191 | - case 'method': |
|
191 | + case 'method': |
|
192 | 192 | if (!empty($colInfo['colParams']['params'])) { |
193 | - $values = call_user_func_array([App::$cur->$colInfo['colParams']['module'], $colInfo['colParams']['method']], $colInfo['colParams']['params']); |
|
193 | + $values = call_user_func_array([App::$cur->$colInfo['colParams']['module'], $colInfo['colParams']['method']], $colInfo['colParams']['params']); |
|
194 | 194 | } else { |
195 | - $values = $colInfo['colParams']['module']->$colInfo['colParams']['method'](); |
|
195 | + $values = $colInfo['colParams']['module']->$colInfo['colParams']['method'](); |
|
196 | 196 | } |
197 | 197 | $value = !empty($values[$item->$colName]) ? $values[$item->$colName] : 'Не задано'; |
198 | 198 | break; |
199 | - case 'void': |
|
199 | + case 'void': |
|
200 | 200 | if (!empty($modelName::$cols[$colName]['value']['type']) && $modelName::$cols[$colName]['value']['type'] == 'moduleMethod') { |
201 | - return \App::$cur->{$modelName::$cols[$colName]['value']['module']}->{$modelName::$cols[$colName]['value']['method']}($item, $colName, $modelName::$cols[$colName]); |
|
201 | + return \App::$cur->{$modelName::$cols[$colName]['value']['module']}->{$modelName::$cols[$colName]['value']['method']}($item, $colName, $modelName::$cols[$colName]); |
|
202 | 202 | } |
203 | 203 | break; |
204 | - case 'relation': |
|
204 | + case 'relation': |
|
205 | 205 | $relations = $colInfo['modelName']::relations(); |
206 | 206 | $relValue = $relations[$colInfo['colParams']['relation']]['model']::get($item->$colName); |
207 | 207 | $relModel = $relations[$colInfo['colParams']['relation']]['model']; |
208 | 208 | $relModel = strpos($relModel, '\\') === 0 ? substr($relModel, 1) : $relModel; |
209 | 209 | if ($manageHref) { |
210 | - $value = $relValue ? "<a href='/admin/" . str_replace('\\', '/view/', $relModel) . "/" . $relValue->pk() . "'>" . $relValue->name() . "</a>" : 'Не задано'; |
|
210 | + $value = $relValue ? "<a href='/admin/" . str_replace('\\', '/view/', $relModel) . "/" . $relValue->pk() . "'>" . $relValue->name() . "</a>" : 'Не задано'; |
|
211 | 211 | } else { |
212 | - $value = $relValue ? $relValue->name() : 'Не задано'; |
|
212 | + $value = $relValue ? $relValue->name() : 'Не задано'; |
|
213 | 213 | } |
214 | 214 | break; |
215 | 215 | } |
216 | 216 | break; |
217 | - case 'image': |
|
217 | + case 'image': |
|
218 | 218 | $file = Files\File::get($item->$colName); |
219 | 219 | if ($file) { |
220 | - $value = '<img src="' . $file->path . '?resize=60x120" />'; |
|
220 | + $value = '<img src="' . $file->path . '?resize=60x120" />'; |
|
221 | 221 | } else { |
222 | - $value = '<img src="/static/system/images/no-image.png?resize=60x120" />'; |
|
222 | + $value = '<img src="/static/system/images/no-image.png?resize=60x120" />'; |
|
223 | 223 | } |
224 | 224 | break; |
225 | - case 'bool': |
|
225 | + case 'bool': |
|
226 | 226 | $value = $item->$colName ? 'Да' : 'Нет'; |
227 | 227 | break; |
228 | - case 'void': |
|
228 | + case 'void': |
|
229 | 229 | if (!empty($colInfo['colParams']['value']['type']) && $colInfo['colParams']['value']['type'] == 'moduleMethod') { |
230 | - return \App::$cur->{$colInfo['colParams']['value']['module']}->{$colInfo['colParams']['value']['method']}($item, $colName, $colInfo['colParams']); |
|
230 | + return \App::$cur->{$colInfo['colParams']['value']['module']}->{$colInfo['colParams']['value']['method']}($item, $colName, $colInfo['colParams']); |
|
231 | 231 | } |
232 | 232 | break; |
233 | - case 'dynamicType': |
|
233 | + case 'dynamicType': |
|
234 | 234 | switch ($colInfo['colParams']['typeSource']) { |
235 | - case'selfMethod': |
|
235 | + case'selfMethod': |
|
236 | 236 | $type = $item->{$colInfo['colParams']['selfMethod']}(); |
237 | 237 | if (is_array($type)) { |
238 | - if (strpos($type['relation'], ':')) { |
|
238 | + if (strpos($type['relation'], ':')) { |
|
239 | 239 | $relationPath = explode(':', $type['relation']); |
240 | 240 | $relationName = array_pop($relationPath); |
241 | 241 | $curItem = $item; |
242 | 242 | foreach ($relationPath as $path) { |
243 | - $curItem = $curItem->$path; |
|
243 | + $curItem = $curItem->$path; |
|
244 | 244 | } |
245 | 245 | $itemModel = get_class($curItem); |
246 | 246 | $relation = $itemModel::getRelation($relationName); |
247 | 247 | $sourceModel = $relation['model']; |
248 | - } else { |
|
248 | + } else { |
|
249 | 249 | $relation = static::getRelation($type['relation']); |
250 | 250 | $sourceModel = $relation['model']; |
251 | - } |
|
252 | - $inputType = 'select'; |
|
253 | - $value = $sourceModel::get($item->$colName); |
|
254 | - if ($value) { |
|
251 | + } |
|
252 | + $inputType = 'select'; |
|
253 | + $value = $sourceModel::get($item->$colName); |
|
254 | + if ($value) { |
|
255 | 255 | $value = $value->name(); |
256 | - } else { |
|
256 | + } else { |
|
257 | 257 | $value = $item->$colName; |
258 | - } |
|
258 | + } |
|
259 | 259 | } else { |
260 | - $value = $item->$colName; |
|
260 | + $value = $item->$colName; |
|
261 | 261 | } |
262 | 262 | break; |
263 | 263 | } |
264 | 264 | break; |
265 | - default: |
|
265 | + default: |
|
266 | 266 | $value = $item->$colName; |
267 | 267 | break; |
268 | 268 | } |
269 | 269 | return $value; |
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Fix col prefix |
|
274 | - * |
|
275 | - * @param mixed $array |
|
276 | - * @param string $searchtype |
|
277 | - * @param string $rootModel |
|
278 | - * @return null |
|
279 | - */ |
|
280 | - public static function fixPrefix(&$array, $searchtype = 'key', $rootModel = '') { |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Fix col prefix |
|
274 | + * |
|
275 | + * @param mixed $array |
|
276 | + * @param string $searchtype |
|
277 | + * @param string $rootModel |
|
278 | + * @return null |
|
279 | + */ |
|
280 | + public static function fixPrefix(&$array, $searchtype = 'key', $rootModel = '') { |
|
281 | 281 | if (!$rootModel) { |
282 | - $rootModel = get_called_class(); |
|
282 | + $rootModel = get_called_class(); |
|
283 | 283 | } |
284 | 284 | $cols = static::cols(); |
285 | 285 | if (!$array) { |
286 | - return; |
|
286 | + return; |
|
287 | 287 | } |
288 | 288 | if (!is_array($array)) { |
289 | - if (!isset($cols[static::colPrefix() . $array]) && isset(static::$cols[$array])) { |
|
289 | + if (!isset($cols[static::colPrefix() . $array]) && isset(static::$cols[$array])) { |
|
290 | 290 | static::createCol($array); |
291 | 291 | $cols = static::cols(true); |
292 | - } |
|
293 | - if (!isset($cols[$array]) && isset($cols[static::colPrefix() . $array])) { |
|
292 | + } |
|
293 | + if (!isset($cols[$array]) && isset($cols[static::colPrefix() . $array])) { |
|
294 | 294 | $array = static::colPrefix() . $array; |
295 | - } else { |
|
295 | + } else { |
|
296 | 296 | static::checkForJoin($array, $rootModel); |
297 | - } |
|
298 | - return; |
|
297 | + } |
|
298 | + return; |
|
299 | 299 | } |
300 | 300 | switch ($searchtype) { |
301 | - case 'key': |
|
301 | + case 'key': |
|
302 | 302 | foreach ($array as $key => $item) { |
303 | - if (!isset($cols[static::colPrefix() . $key]) && isset(static::$cols[$key])) { |
|
303 | + if (!isset($cols[static::colPrefix() . $key]) && isset(static::$cols[$key])) { |
|
304 | 304 | static::createCol($key); |
305 | 305 | $cols = static::cols(true); |
306 | - } |
|
307 | - if (!isset($cols[$key]) && isset($cols[static::colPrefix() . $key])) { |
|
306 | + } |
|
307 | + if (!isset($cols[$key]) && isset($cols[static::colPrefix() . $key])) { |
|
308 | 308 | $array[static::colPrefix() . $key] = $item; |
309 | 309 | unset($array[$key]); |
310 | 310 | $key = static::colPrefix() . $key; |
311 | - } |
|
312 | - if (is_array($array[$key])) { |
|
311 | + } |
|
312 | + if (is_array($array[$key])) { |
|
313 | 313 | static::fixPrefix($array[$key], 'key', $rootModel); |
314 | - } else { |
|
314 | + } else { |
|
315 | 315 | static::checkForJoin($key, $rootModel); |
316 | - } |
|
316 | + } |
|
317 | 317 | } |
318 | 318 | break; |
319 | - case 'first': |
|
319 | + case 'first': |
|
320 | 320 | if (isset($array[0]) && is_string($array[0])) { |
321 | - if (!isset($cols[static::colPrefix() . $array[0]]) && isset(static::$cols[$array[0]])) { |
|
321 | + if (!isset($cols[static::colPrefix() . $array[0]]) && isset(static::$cols[$array[0]])) { |
|
322 | 322 | static::createCol($array[0]); |
323 | 323 | $cols = static::cols(true); |
324 | - } |
|
325 | - if (!isset($cols[$array[0]]) && isset($cols[static::colPrefix() . $array[0]])) { |
|
324 | + } |
|
325 | + if (!isset($cols[$array[0]]) && isset($cols[static::colPrefix() . $array[0]])) { |
|
326 | 326 | $array[0] = static::colPrefix() . $array[0]; |
327 | - } else { |
|
327 | + } else { |
|
328 | 328 | static::checkForJoin($array[0], $rootModel); |
329 | - } |
|
329 | + } |
|
330 | 330 | } elseif (isset($array[0]) && is_array($array[0])) { |
331 | - foreach ($array as &$item) { |
|
331 | + foreach ($array as &$item) { |
|
332 | 332 | static::fixPrefix($item, 'first', $rootModel); |
333 | - } |
|
333 | + } |
|
334 | 334 | } |
335 | 335 | break; |
336 | 336 | } |
337 | - } |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Check model relations path and load need relations |
|
341 | - * |
|
342 | - * @param string $col |
|
343 | - * @param string $rootModel |
|
344 | - */ |
|
345 | - public static function checkForJoin(&$col, $rootModel) { |
|
339 | + /** |
|
340 | + * Check model relations path and load need relations |
|
341 | + * |
|
342 | + * @param string $col |
|
343 | + * @param string $rootModel |
|
344 | + */ |
|
345 | + public static function checkForJoin(&$col, $rootModel) { |
|
346 | 346 | |
347 | 347 | if (strpos($col, ':') !== false) { |
348 | - $relations = static::relations(); |
|
349 | - if (isset($relations[substr($col, 0, strpos($col, ':'))])) { |
|
348 | + $relations = static::relations(); |
|
349 | + if (isset($relations[substr($col, 0, strpos($col, ':'))])) { |
|
350 | 350 | $rel = substr($col, 0, strpos($col, ':')); |
351 | 351 | $col = substr($col, strpos($col, ':') + 1); |
352 | 352 | |
353 | 353 | $type = empty($relations[$rel]['type']) ? 'to' : $relations[$rel]['type']; |
354 | 354 | switch ($type) { |
355 | - case 'to': |
|
355 | + case 'to': |
|
356 | 356 | $relCol = $relations[$rel]['col']; |
357 | 357 | static::fixPrefix($relCol); |
358 | 358 | $rootModel::$relJoins[$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol]; |
359 | 359 | break; |
360 | - case 'one': |
|
360 | + case 'one': |
|
361 | 361 | case 'many': |
362 | 362 | $relCol = $relations[$rel]['col']; |
363 | 363 | $relations[$rel]['model']::fixPrefix($relCol); |
@@ -365,44 +365,44 @@ discard block |
||
365 | 365 | break; |
366 | 366 | } |
367 | 367 | $relations[$rel]['model']::fixPrefix($col, 'key', $rootModel); |
368 | - } |
|
368 | + } |
|
369 | + } |
|
369 | 370 | } |
370 | - } |
|
371 | 371 | |
372 | - /** |
|
373 | - * Return full col information |
|
374 | - * |
|
375 | - * @param string $col |
|
376 | - * @return array |
|
377 | - */ |
|
378 | - public static function getColInfo($col) { |
|
372 | + /** |
|
373 | + * Return full col information |
|
374 | + * |
|
375 | + * @param string $col |
|
376 | + * @return array |
|
377 | + */ |
|
378 | + public static function getColInfo($col) { |
|
379 | 379 | return static::parseColRecursion($col); |
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Information extractor for col relations path |
|
384 | - * |
|
385 | - * @param string|array $info |
|
386 | - * @return array |
|
387 | - */ |
|
388 | - public static function parseColRecursion($info) { |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Information extractor for col relations path |
|
384 | + * |
|
385 | + * @param string|array $info |
|
386 | + * @return array |
|
387 | + */ |
|
388 | + public static function parseColRecursion($info) { |
|
389 | 389 | if (is_string($info)) { |
390 | - $info = ['col' => $info, 'rawCol' => $info, 'modelName' => '', 'label' => [], 'joins' => []]; |
|
390 | + $info = ['col' => $info, 'rawCol' => $info, 'modelName' => '', 'label' => [], 'joins' => []]; |
|
391 | 391 | } |
392 | 392 | if (strpos($info['col'], ':') !== false) { |
393 | - $relations = static::relations(); |
|
394 | - if (isset($relations[substr($info['col'], 0, strpos($info['col'], ':'))])) { |
|
393 | + $relations = static::relations(); |
|
394 | + if (isset($relations[substr($info['col'], 0, strpos($info['col'], ':'))])) { |
|
395 | 395 | $rel = substr($info['col'], 0, strpos($info['col'], ':')); |
396 | 396 | $info['col'] = substr($info['col'], strpos($info['col'], ':') + 1); |
397 | 397 | $type = empty($relations[$rel]['type']) ? 'to' : $relations[$rel]['type']; |
398 | 398 | switch ($type) { |
399 | - case 'to': |
|
399 | + case 'to': |
|
400 | 400 | $relCol = $relations[$rel]['col']; |
401 | 401 | static::fixPrefix($relCol); |
402 | 402 | //$info['modelName'] = $relations[$rel]['model']; |
403 | 403 | $info['joins'][$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol]; |
404 | 404 | break; |
405 | - case 'one': |
|
405 | + case 'one': |
|
406 | 406 | $relCol = $relations[$rel]['col']; |
407 | 407 | $relations[$rel]['model']::fixPrefix($relCol); |
408 | 408 | //$info['modelName'] = $relations[$rel]['model']; |
@@ -410,770 +410,770 @@ discard block |
||
410 | 410 | break; |
411 | 411 | } |
412 | 412 | $info = $relations[$rel]['model']::parseColRecursion($info); |
413 | - } |
|
413 | + } |
|
414 | 414 | } else { |
415 | - $cols = static::cols(); |
|
416 | - if (!empty(static::$labels[$info['col']])) { |
|
415 | + $cols = static::cols(); |
|
416 | + if (!empty(static::$labels[$info['col']])) { |
|
417 | 417 | $info['label'] = static::$labels[$info['col']]; |
418 | - } |
|
418 | + } |
|
419 | 419 | |
420 | - if (isset(static::$cols[$info['col']])) { |
|
420 | + if (isset(static::$cols[$info['col']])) { |
|
421 | 421 | $info['colParams'] = static::$cols[$info['col']]; |
422 | - } elseif (isset(static::$cols[str_replace(static::colPrefix(), '', $info['col'])])) { |
|
422 | + } elseif (isset(static::$cols[str_replace(static::colPrefix(), '', $info['col'])])) { |
|
423 | 423 | $info['colParams'] = static::$cols[str_replace(static::colPrefix(), '', $info['col'])]; |
424 | - } else { |
|
424 | + } else { |
|
425 | 425 | $info['colParams'] = []; |
426 | - } |
|
427 | - if (!isset($cols[$info['col']]) && isset($cols[static::colPrefix() . $info['col']])) { |
|
426 | + } |
|
427 | + if (!isset($cols[$info['col']]) && isset($cols[static::colPrefix() . $info['col']])) { |
|
428 | 428 | $info['col'] = static::colPrefix() . $info['col']; |
429 | - } |
|
430 | - $info['modelName'] = get_called_class(); |
|
429 | + } |
|
430 | + $info['modelName'] = get_called_class(); |
|
431 | 431 | } |
432 | 432 | return $info; |
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Return actual cols from data base |
|
437 | - * |
|
438 | - * @param boolean $refresh |
|
439 | - * @return array |
|
440 | - */ |
|
441 | - public static function cols($refresh = false) { |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Return actual cols from data base |
|
437 | + * |
|
438 | + * @param boolean $refresh |
|
439 | + * @return array |
|
440 | + */ |
|
441 | + public static function cols($refresh = false) { |
|
442 | 442 | if (static::$storage['type'] == 'moduleConfig') { |
443 | - return []; |
|
443 | + return []; |
|
444 | 444 | } |
445 | 445 | if (empty(Model::$cols[static::table()]) || $refresh) { |
446 | - Model::$cols[static::table()] = App::$cur->db->getTableCols(static::table()); |
|
446 | + Model::$cols[static::table()] = App::$cur->db->getTableCols(static::table()); |
|
447 | 447 | } |
448 | 448 | if (!Model::$cols[static::table()]) { |
449 | - static::createTable(); |
|
450 | - Model::$cols[static::table()] = App::$cur->db->getTableCols(static::table()); |
|
449 | + static::createTable(); |
|
450 | + Model::$cols[static::table()] = App::$cur->db->getTableCols(static::table()); |
|
451 | 451 | } |
452 | 452 | return Model::$cols[static::table()]; |
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Return cols indexes for create tables |
|
457 | - * |
|
458 | - * @return array |
|
459 | - */ |
|
460 | - public static function indexes() { |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Return cols indexes for create tables |
|
457 | + * |
|
458 | + * @return array |
|
459 | + */ |
|
460 | + public static function indexes() { |
|
461 | 461 | return []; |
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Generate params string for col by name |
|
466 | - * |
|
467 | - * @param string $colName |
|
468 | - * @return boolean|string |
|
469 | - */ |
|
470 | - public static function genColParams($colName) { |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Generate params string for col by name |
|
466 | + * |
|
467 | + * @param string $colName |
|
468 | + * @return boolean|string |
|
469 | + */ |
|
470 | + public static function genColParams($colName) { |
|
471 | 471 | if (empty(static::$cols[$colName]) || static::$storage['type'] == 'moduleConfig') { |
472 | - return false; |
|
472 | + return false; |
|
473 | 473 | } |
474 | 474 | |
475 | 475 | $params = false; |
476 | 476 | switch (static::$cols[$colName]['type']) { |
477 | - case 'select': |
|
477 | + case 'select': |
|
478 | 478 | switch (static::$cols[$colName]['source']) { |
479 | - case 'relation': |
|
479 | + case 'relation': |
|
480 | 480 | $params = 'int(11) UNSIGNED NOT NULL'; |
481 | 481 | break; |
482 | - default: |
|
482 | + default: |
|
483 | 483 | $params = 'varchar(255) NOT NULL'; |
484 | 484 | } |
485 | 485 | break; |
486 | - case 'image': |
|
486 | + case 'image': |
|
487 | 487 | case 'file': |
488 | 488 | $params = 'int(11) UNSIGNED NOT NULL'; |
489 | 489 | break; |
490 | - case 'number': |
|
490 | + case 'number': |
|
491 | 491 | $params = 'int(11) NOT NULL'; |
492 | 492 | break; |
493 | - case 'text': |
|
493 | + case 'text': |
|
494 | 494 | case 'email': |
495 | 495 | $params = 'varchar(255) NOT NULL'; |
496 | 496 | break; |
497 | - case 'html': |
|
497 | + case 'html': |
|
498 | 498 | case 'textarea': |
499 | 499 | case 'json': |
500 | 500 | case 'password': |
501 | 501 | case 'dynamicType': |
502 | 502 | $params = 'text NOT NULL'; |
503 | 503 | break; |
504 | - case 'bool': |
|
504 | + case 'bool': |
|
505 | 505 | $params = 'tinyint(1) UNSIGNED NOT NULL'; |
506 | 506 | break; |
507 | - case 'decimal': |
|
507 | + case 'decimal': |
|
508 | 508 | $params = 'decimal(8, 2) NOT NULL'; |
509 | 509 | break; |
510 | - case 'date': |
|
510 | + case 'date': |
|
511 | 511 | $params = 'date NOT NULL DEFAULT 0'; |
512 | 512 | break; |
513 | - case 'dateTime': |
|
513 | + case 'dateTime': |
|
514 | 514 | $params = 'timestamp NOT NULL DEFAULT 0'; |
515 | 515 | break; |
516 | 516 | } |
517 | 517 | return $params; |
518 | - } |
|
519 | - |
|
520 | - /** |
|
521 | - * Create new col in data base |
|
522 | - * |
|
523 | - * @param string $colName |
|
524 | - * @return boolean|integer |
|
525 | - */ |
|
526 | - public static function createCol($colName) { |
|
518 | + } |
|
519 | + |
|
520 | + /** |
|
521 | + * Create new col in data base |
|
522 | + * |
|
523 | + * @param string $colName |
|
524 | + * @return boolean|integer |
|
525 | + */ |
|
526 | + public static function createCol($colName) { |
|
527 | 527 | $params = static::genColParams($colName); |
528 | 528 | if ($params === false) { |
529 | - return false; |
|
529 | + return false; |
|
530 | 530 | } |
531 | 531 | return App::$cur->db->addCol(static::table(), static::colPrefix() . $colName, $params); |
532 | - } |
|
532 | + } |
|
533 | 533 | |
534 | - public static function createTable() { |
|
534 | + public static function createTable() { |
|
535 | 535 | if (static::$storage['type'] == 'moduleConfig') { |
536 | - return true; |
|
536 | + return true; |
|
537 | 537 | } |
538 | 538 | if (!App::$cur->db) { |
539 | - return false; |
|
539 | + return false; |
|
540 | 540 | } |
541 | 541 | |
542 | 542 | $query = App::$cur->db->newQuery(); |
543 | 543 | if (!$query) { |
544 | - return false; |
|
544 | + return false; |
|
545 | 545 | } |
546 | 546 | |
547 | 547 | if (!isset($this)) { |
548 | - $tableName = static::table(); |
|
549 | - $colPrefix = static::colPrefix(); |
|
550 | - $indexes = static::indexes(); |
|
548 | + $tableName = static::table(); |
|
549 | + $colPrefix = static::colPrefix(); |
|
550 | + $indexes = static::indexes(); |
|
551 | 551 | } else { |
552 | - $tableName = $this->table(); |
|
553 | - $colPrefix = $this->colPrefix(); |
|
554 | - $indexes = $this->indexes(); |
|
552 | + $tableName = $this->table(); |
|
553 | + $colPrefix = $this->colPrefix(); |
|
554 | + $indexes = $this->indexes(); |
|
555 | 555 | } |
556 | 556 | if (App::$cur->db->tableExist($tableName)) { |
557 | - return true; |
|
557 | + return true; |
|
558 | 558 | } |
559 | 559 | $cols = [ |
560 | 560 | $colPrefix . 'id' => 'pk' |
561 | 561 | ]; |
562 | 562 | $className = get_called_class(); |
563 | 563 | if (!empty($className::$cols)) { |
564 | - foreach ($className::$cols as $colName => $colParams) { |
|
564 | + foreach ($className::$cols as $colName => $colParams) { |
|
565 | 565 | if ($colName == 'date_create') { |
566 | - $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
567 | - continue; |
|
566 | + $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
567 | + continue; |
|
568 | 568 | } |
569 | 569 | $params = $className::genColParams($colName); |
570 | 570 | if ($params) { |
571 | - $cols[$colPrefix . $colName] = $params; |
|
571 | + $cols[$colPrefix . $colName] = $params; |
|
572 | + } |
|
572 | 573 | } |
573 | - } |
|
574 | 574 | } |
575 | 575 | if (empty($cols[$colPrefix . 'date_create'])) { |
576 | - $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
576 | + $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
577 | 577 | } |
578 | 578 | $tableIndexes = []; |
579 | 579 | if ($indexes) { |
580 | - foreach ($indexes as $indexName => $index) { |
|
580 | + foreach ($indexes as $indexName => $index) { |
|
581 | 581 | $tableIndexes[] = $index['type'] . ' ' . App::$cur->db->table_prefix . $indexName . ' (' . implode(',', $index['cols']) . ')'; |
582 | - } |
|
582 | + } |
|
583 | 583 | } |
584 | 584 | |
585 | 585 | $query->createTable($tableName, $cols, $tableIndexes); |
586 | 586 | return true; |
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * Return table name |
|
591 | - * |
|
592 | - * @return string |
|
593 | - */ |
|
594 | - public static function table() { |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * Return table name |
|
591 | + * |
|
592 | + * @return string |
|
593 | + */ |
|
594 | + public static function table() { |
|
595 | 595 | return strtolower(str_replace('\\', '_', get_called_class())); |
596 | - } |
|
596 | + } |
|
597 | 597 | |
598 | - /** |
|
599 | - * Return table index col name |
|
600 | - * |
|
601 | - * @return string |
|
602 | - */ |
|
603 | - public static function index() { |
|
598 | + /** |
|
599 | + * Return table index col name |
|
600 | + * |
|
601 | + * @return string |
|
602 | + */ |
|
603 | + public static function index() { |
|
604 | 604 | |
605 | 605 | return static::colPrefix() . 'id'; |
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Return col prefix |
|
610 | - * |
|
611 | - * @return string |
|
612 | - */ |
|
613 | - public static function colPrefix() { |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Return col prefix |
|
610 | + * |
|
611 | + * @return string |
|
612 | + */ |
|
613 | + public static function colPrefix() { |
|
614 | 614 | $classPath = explode('\\', get_called_class()); |
615 | 615 | $classPath = array_slice($classPath, 1); |
616 | 616 | return strtolower(implode('_', $classPath)) . '_'; |
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * return relations list |
|
621 | - * |
|
622 | - * @return array |
|
623 | - */ |
|
624 | - public static function relations() { |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * return relations list |
|
621 | + * |
|
622 | + * @return array |
|
623 | + */ |
|
624 | + public static function relations() { |
|
625 | 625 | return []; |
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Return name of col with object name |
|
630 | - * |
|
631 | - * @return string |
|
632 | - */ |
|
633 | - public static function nameCol() { |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Return name of col with object name |
|
630 | + * |
|
631 | + * @return string |
|
632 | + */ |
|
633 | + public static function nameCol() { |
|
634 | 634 | return 'name'; |
635 | - } |
|
636 | - |
|
637 | - /** |
|
638 | - * Return object name |
|
639 | - * |
|
640 | - * @return string |
|
641 | - */ |
|
642 | - public function name() { |
|
635 | + } |
|
636 | + |
|
637 | + /** |
|
638 | + * Return object name |
|
639 | + * |
|
640 | + * @return string |
|
641 | + */ |
|
642 | + public function name() { |
|
643 | 643 | return $this->{$this->nameCol()} ? $this->{$this->nameCol()} : $this->pk(); |
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * Get single object from data base |
|
648 | - * |
|
649 | - * @param mixed $param |
|
650 | - * @param string $col |
|
651 | - * @param array $options |
|
652 | - * @return boolean|\Model |
|
653 | - */ |
|
654 | - public static function get($param, $col = null, $options = []) { |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * Get single object from data base |
|
648 | + * |
|
649 | + * @param mixed $param |
|
650 | + * @param string $col |
|
651 | + * @param array $options |
|
652 | + * @return boolean|\Model |
|
653 | + */ |
|
654 | + public static function get($param, $col = null, $options = []) { |
|
655 | 655 | if (static::$storage['type'] == 'moduleConfig') { |
656 | - return static::getFromModuleStorage($param, $col, $options); |
|
656 | + return static::getFromModuleStorage($param, $col, $options); |
|
657 | 657 | } |
658 | 658 | if (!empty($col)) { |
659 | - static::fixPrefix($col); |
|
659 | + static::fixPrefix($col); |
|
660 | 660 | } |
661 | 661 | |
662 | 662 | if (is_array($param)) { |
663 | - static::fixPrefix($param, 'first'); |
|
663 | + static::fixPrefix($param, 'first'); |
|
664 | 664 | } |
665 | 665 | foreach (static::$relJoins as $join) { |
666 | - App::$cur->db->join($join[0], $join[1]); |
|
666 | + App::$cur->db->join($join[0], $join[1]); |
|
667 | 667 | } |
668 | 668 | static::$relJoins = []; |
669 | 669 | foreach (static::$needJoin as $rel) { |
670 | - $relations = static::relations(); |
|
671 | - if (isset($relations[$rel])) { |
|
670 | + $relations = static::relations(); |
|
671 | + if (isset($relations[$rel])) { |
|
672 | 672 | $type = empty($relations[$rel]['type']) ? 'to' : $relations[$rel]['type']; |
673 | 673 | switch ($type) { |
674 | - case 'to': |
|
674 | + case 'to': |
|
675 | 675 | $relCol = $relations[$rel]['col']; |
676 | 676 | static::fixPrefix($relCol); |
677 | 677 | App::$cur->db->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
678 | 678 | break; |
679 | - case 'one': |
|
679 | + case 'one': |
|
680 | 680 | $col = $relations[$rel]['col']; |
681 | 681 | $relations[$rel]['model']::fixPrefix($col); |
682 | 682 | App::$cur->db->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
683 | 683 | break; |
684 | 684 | } |
685 | - } |
|
685 | + } |
|
686 | 686 | } |
687 | 687 | static::$needJoin = []; |
688 | 688 | if (is_array($param)) { |
689 | - App::$cur->db->where($param); |
|
689 | + App::$cur->db->where($param); |
|
690 | 690 | } else { |
691 | - if ($col === null) { |
|
691 | + if ($col === null) { |
|
692 | 692 | |
693 | 693 | $col = static::index(); |
694 | - } |
|
695 | - if ($param !== null) { |
|
694 | + } |
|
695 | + if ($param !== null) { |
|
696 | 696 | $cols = static::cols(); |
697 | 697 | if (!isset($cols[$col]) && isset($cols[static::colPrefix() . $col])) { |
698 | - $col = static::colPrefix() . $col; |
|
698 | + $col = static::colPrefix() . $col; |
|
699 | 699 | } |
700 | 700 | App::$cur->db->where($col, $param); |
701 | - } else { |
|
701 | + } else { |
|
702 | 702 | return false; |
703 | - } |
|
703 | + } |
|
704 | 704 | } |
705 | 705 | if (!App::$cur->db->where) { |
706 | - return false; |
|
706 | + return false; |
|
707 | 707 | } |
708 | 708 | try { |
709 | - $result = App::$cur->db->select(static::table()); |
|
709 | + $result = App::$cur->db->select(static::table()); |
|
710 | 710 | } catch (PDOException $exc) { |
711 | - if ($exc->getCode() == '42S02') { |
|
711 | + if ($exc->getCode() == '42S02') { |
|
712 | 712 | static::createTable(); |
713 | - } |
|
714 | - $result = App::$cur->db->select(static::table()); |
|
713 | + } |
|
714 | + $result = App::$cur->db->select(static::table()); |
|
715 | 715 | } |
716 | 716 | if (!$result) { |
717 | - return false; |
|
717 | + return false; |
|
718 | 718 | } |
719 | 719 | return $result->fetch(get_called_class()); |
720 | - } |
|
721 | - |
|
722 | - /** |
|
723 | - * Old method |
|
724 | - * |
|
725 | - * @param type $options |
|
726 | - * @return Array |
|
727 | - */ |
|
728 | - public static function get_list($options = []) { |
|
720 | + } |
|
721 | + |
|
722 | + /** |
|
723 | + * Old method |
|
724 | + * |
|
725 | + * @param type $options |
|
726 | + * @return Array |
|
727 | + */ |
|
728 | + public static function get_list($options = []) { |
|
729 | 729 | $query = App::$cur->db->newQuery(); |
730 | 730 | if (!$query) { |
731 | - return []; |
|
731 | + return []; |
|
732 | 732 | } |
733 | 733 | if (!empty($options['where'])) |
734 | - $query->where($options['where']); |
|
734 | + $query->where($options['where']); |
|
735 | 735 | if (!empty($options['cols'])) |
736 | - $query->cols = $options['cols']; |
|
736 | + $query->cols = $options['cols']; |
|
737 | 737 | if (!empty($options['group'])) { |
738 | - $query->group($options['group']); |
|
738 | + $query->group($options['group']); |
|
739 | 739 | } |
740 | 740 | if (!empty($options['having'])) { |
741 | - $query->having($options['having']); |
|
741 | + $query->having($options['having']); |
|
742 | 742 | } |
743 | 743 | if (!empty($options['order'])) |
744 | - $query->order($options['order']); |
|
744 | + $query->order($options['order']); |
|
745 | 745 | if (!empty($options['join'])) |
746 | - $query->join($options['join']); |
|
746 | + $query->join($options['join']); |
|
747 | 747 | if (!empty($options['distinct'])) |
748 | - $query->distinct = $options['distinct']; |
|
748 | + $query->distinct = $options['distinct']; |
|
749 | 749 | |
750 | 750 | foreach (static::$relJoins as $join) { |
751 | - $query->join($join[0], $join[1]); |
|
751 | + $query->join($join[0], $join[1]); |
|
752 | 752 | } |
753 | 753 | static::$relJoins = []; |
754 | 754 | foreach (static::$needJoin as $rel) { |
755 | - $relations = static::relations(); |
|
756 | - if (isset($relations[$rel])) { |
|
755 | + $relations = static::relations(); |
|
756 | + if (isset($relations[$rel])) { |
|
757 | 757 | $type = empty($relations[$rel]['type']) ? 'to' : $relations[$rel]['type']; |
758 | 758 | switch ($type) { |
759 | - case 'to': |
|
759 | + case 'to': |
|
760 | 760 | $relCol = $relations[$rel]['col']; |
761 | 761 | static::fixPrefix($relCol); |
762 | 762 | $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
763 | 763 | break; |
764 | - case 'one': |
|
764 | + case 'one': |
|
765 | 765 | $col = $relations[$rel]['col']; |
766 | 766 | $relations[$rel]['model']::fixPrefix($col); |
767 | 767 | $query->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
768 | 768 | break; |
769 | 769 | } |
770 | - } |
|
770 | + } |
|
771 | 771 | } |
772 | 772 | static::$needJoin = []; |
773 | 773 | |
774 | 774 | if (!empty($options['limit'])) |
775 | - $limit = (int) $options['limit']; |
|
775 | + $limit = (int) $options['limit']; |
|
776 | 776 | else { |
777 | - $limit = 0; |
|
777 | + $limit = 0; |
|
778 | 778 | } |
779 | 779 | if (!empty($options['start'])) |
780 | - $start = (int) $options['start']; |
|
780 | + $start = (int) $options['start']; |
|
781 | 781 | else { |
782 | - $start = 0; |
|
782 | + $start = 0; |
|
783 | 783 | } |
784 | 784 | if ($limit || $start) { |
785 | - $query->limit($start, $limit); |
|
785 | + $query->limit($start, $limit); |
|
786 | 786 | } |
787 | 787 | if (isset($options['key'])) { |
788 | - $key = $options['key']; |
|
788 | + $key = $options['key']; |
|
789 | 789 | } else { |
790 | - $key = static::index(); |
|
790 | + $key = static::index(); |
|
791 | 791 | } |
792 | 792 | try { |
793 | - $query->operation = 'SELECT'; |
|
794 | - $query->table = static::table(); |
|
795 | - $queryArr = $query->buildQuery(); |
|
796 | - $result = $query->query($queryArr); |
|
793 | + $query->operation = 'SELECT'; |
|
794 | + $query->table = static::table(); |
|
795 | + $queryArr = $query->buildQuery(); |
|
796 | + $result = $query->query($queryArr); |
|
797 | 797 | } catch (PDOException $exc) { |
798 | - if ($exc->getCode() == '42S02') { |
|
798 | + if ($exc->getCode() == '42S02') { |
|
799 | 799 | static::createTable(); |
800 | 800 | $result = $query->query($queryArr); |
801 | - } else { |
|
801 | + } else { |
|
802 | 802 | throw $exc; |
803 | - } |
|
803 | + } |
|
804 | 804 | } |
805 | 805 | |
806 | 806 | if (!empty($options['array'])) { |
807 | - return $result->getArray($key); |
|
807 | + return $result->getArray($key); |
|
808 | 808 | } |
809 | 809 | $list = $result->getObjects(get_called_class(), $key); |
810 | 810 | if (!empty($options['forSelect'])) { |
811 | - $return = []; |
|
812 | - foreach ($list as $key => $item) { |
|
811 | + $return = []; |
|
812 | + foreach ($list as $key => $item) { |
|
813 | 813 | $return[$key] = $item->name(); |
814 | - } |
|
815 | - return $return; |
|
814 | + } |
|
815 | + return $return; |
|
816 | 816 | } |
817 | 817 | return $list; |
818 | - } |
|
819 | - |
|
820 | - /** |
|
821 | - * Return list of objects from data base |
|
822 | - * |
|
823 | - * @param type $options |
|
824 | - * @return type |
|
825 | - */ |
|
826 | - public static function getList($options = []) { |
|
818 | + } |
|
819 | + |
|
820 | + /** |
|
821 | + * Return list of objects from data base |
|
822 | + * |
|
823 | + * @param type $options |
|
824 | + * @return type |
|
825 | + */ |
|
826 | + public static function getList($options = []) { |
|
827 | 827 | if (static::$storage['type'] != 'db') { |
828 | - return static::getListFromModuleStorage($options); |
|
828 | + return static::getListFromModuleStorage($options); |
|
829 | 829 | } |
830 | 830 | if (!empty($options['where'])) { |
831 | - static::fixPrefix($options['where'], 'first'); |
|
831 | + static::fixPrefix($options['where'], 'first'); |
|
832 | 832 | } |
833 | 833 | if (!empty($options['group'])) { |
834 | - static::fixPrefix($options['group'], 'first'); |
|
834 | + static::fixPrefix($options['group'], 'first'); |
|
835 | 835 | } |
836 | 836 | if (!empty($options['order'])) { |
837 | - static::fixPrefix($options['order'], 'first'); |
|
837 | + static::fixPrefix($options['order'], 'first'); |
|
838 | 838 | } |
839 | 839 | if (!empty($options['having'])) { |
840 | - static::fixPrefix($options['having'], 'first'); |
|
840 | + static::fixPrefix($options['having'], 'first'); |
|
841 | 841 | } |
842 | 842 | return static::get_list($options); |
843 | - } |
|
844 | - |
|
845 | - /** |
|
846 | - * Get single item from module storage |
|
847 | - * |
|
848 | - * @param array $param |
|
849 | - * @param string $col |
|
850 | - * @param array $options |
|
851 | - * @return boolean|\Model |
|
852 | - */ |
|
853 | - public static function getFromModuleStorage($param = null, $col = null, $options = []) { |
|
843 | + } |
|
844 | + |
|
845 | + /** |
|
846 | + * Get single item from module storage |
|
847 | + * |
|
848 | + * @param array $param |
|
849 | + * @param string $col |
|
850 | + * @param array $options |
|
851 | + * @return boolean|\Model |
|
852 | + */ |
|
853 | + public static function getFromModuleStorage($param = null, $col = null, $options = []) { |
|
854 | 854 | if ($col === null) { |
855 | 855 | |
856 | - $col = static::index(); |
|
856 | + $col = static::index(); |
|
857 | 857 | } |
858 | 858 | if ($param == null) { |
859 | - return false; |
|
859 | + return false; |
|
860 | 860 | } |
861 | 861 | $classPath = explode('\\', get_called_class()); |
862 | 862 | if (!empty(static::$storage['options']['share'])) { |
863 | - $moduleConfig = Config::share($classPath[0]); |
|
863 | + $moduleConfig = Config::share($classPath[0]); |
|
864 | 864 | } else { |
865 | - $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
865 | + $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
866 | 866 | } |
867 | 867 | $appType = App::$cur->type; |
868 | 868 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
869 | - if (!empty($options['appType'])) { |
|
869 | + if (!empty($options['appType'])) { |
|
870 | 870 | $appType = $options['appType']; |
871 | - } |
|
872 | - $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
871 | + } |
|
872 | + $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
873 | 873 | } else { |
874 | - $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
874 | + $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
875 | 875 | } |
876 | 876 | if (!empty($storage[$classPath[1]])) { |
877 | - $items = $storage[$classPath[1]]; |
|
878 | - $class = get_called_class(); |
|
879 | - foreach ($items as $key => $item) { |
|
877 | + $items = $storage[$classPath[1]]; |
|
878 | + $class = get_called_class(); |
|
879 | + foreach ($items as $key => $item) { |
|
880 | 880 | if ($item[$col] == $param) { |
881 | - if (!empty($options['array'])) { |
|
881 | + if (!empty($options['array'])) { |
|
882 | 882 | return $item; |
883 | - } |
|
884 | - $item = new $class($item); |
|
885 | - $item->appType = $appType; |
|
886 | - return $item; |
|
883 | + } |
|
884 | + $item = new $class($item); |
|
885 | + $item->appType = $appType; |
|
886 | + return $item; |
|
887 | + } |
|
887 | 888 | } |
888 | - } |
|
889 | 889 | } |
890 | 890 | return false; |
891 | - } |
|
892 | - |
|
893 | - /** |
|
894 | - * Return list items from module storage |
|
895 | - * |
|
896 | - * @param array $options |
|
897 | - * @return array |
|
898 | - */ |
|
899 | - public static function getListFromModuleStorage($options = []) { |
|
891 | + } |
|
892 | + |
|
893 | + /** |
|
894 | + * Return list items from module storage |
|
895 | + * |
|
896 | + * @param array $options |
|
897 | + * @return array |
|
898 | + */ |
|
899 | + public static function getListFromModuleStorage($options = []) { |
|
900 | 900 | $classPath = explode('\\', get_called_class()); |
901 | 901 | if (!empty(static::$storage['options']['share'])) { |
902 | - $moduleConfig = Config::share($classPath[0]); |
|
902 | + $moduleConfig = Config::share($classPath[0]); |
|
903 | 903 | } else { |
904 | - $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
904 | + $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
905 | 905 | } |
906 | 906 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
907 | - if (empty($options['appType'])) { |
|
907 | + if (empty($options['appType'])) { |
|
908 | 908 | $appType = App::$cur->type; |
909 | - } else { |
|
909 | + } else { |
|
910 | 910 | $appType = $options['appType']; |
911 | - } |
|
912 | - $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
911 | + } |
|
912 | + $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
913 | 913 | } else { |
914 | - $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
914 | + $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
915 | 915 | } |
916 | 916 | if (!empty($storage[$classPath[1]])) { |
917 | - $items = []; |
|
918 | - $class = get_called_class(); |
|
919 | - if (isset($options['key'])) { |
|
917 | + $items = []; |
|
918 | + $class = get_called_class(); |
|
919 | + if (isset($options['key'])) { |
|
920 | 920 | $arrayKey = $options['key']; |
921 | - } else { |
|
921 | + } else { |
|
922 | 922 | $arrayKey = static::index(); |
923 | - } |
|
924 | - foreach ($storage[$classPath[1]] as $key => $item) { |
|
923 | + } |
|
924 | + foreach ($storage[$classPath[1]] as $key => $item) { |
|
925 | 925 | if (!empty($options['where']) && !Model::checkWhere($item, $options['where'])) { |
926 | - continue; |
|
926 | + continue; |
|
927 | 927 | } |
928 | 928 | $items[$item[$arrayKey]] = new $class($item); |
929 | - } |
|
930 | - if (!empty($options['order'])) { |
|
929 | + } |
|
930 | + if (!empty($options['order'])) { |
|
931 | 931 | usort($items, function($a, $b) use($options) { |
932 | - if ($a->{$options['order'][0]} > $b->{$options['order'][0]} && $options['order'][1] = 'asc') { |
|
932 | + if ($a->{$options['order'][0]} > $b->{$options['order'][0]} && $options['order'][1] = 'asc') { |
|
933 | 933 | return 1; |
934 | - } elseif ($a->{$options['order'][0]} < $b->{$options['order'][0]} && $options['order'][1] = 'asc') { |
|
934 | + } elseif ($a->{$options['order'][0]} < $b->{$options['order'][0]} && $options['order'][1] = 'asc') { |
|
935 | 935 | return -1; |
936 | - } |
|
937 | - return 0; |
|
936 | + } |
|
937 | + return 0; |
|
938 | 938 | }); |
939 | - } |
|
940 | - if (!empty($options['forSelect'])) { |
|
939 | + } |
|
940 | + if (!empty($options['forSelect'])) { |
|
941 | 941 | $return = []; |
942 | 942 | foreach ($items as $key => $item) { |
943 | - $return[$key] = $item->name(); |
|
943 | + $return[$key] = $item->name(); |
|
944 | 944 | } |
945 | 945 | return $return; |
946 | - } |
|
947 | - return $items; |
|
946 | + } |
|
947 | + return $items; |
|
948 | 948 | } |
949 | 949 | return []; |
950 | - } |
|
950 | + } |
|
951 | 951 | |
952 | - /** |
|
953 | - * Return count of records from module storage |
|
954 | - * |
|
955 | - * @param array $options |
|
956 | - * @return int |
|
957 | - */ |
|
958 | - public static function getCountFromModuleStorage($options = []) { |
|
952 | + /** |
|
953 | + * Return count of records from module storage |
|
954 | + * |
|
955 | + * @param array $options |
|
956 | + * @return int |
|
957 | + */ |
|
958 | + public static function getCountFromModuleStorage($options = []) { |
|
959 | 959 | |
960 | 960 | $classPath = explode('\\', get_called_class()); |
961 | 961 | $count = 0; |
962 | 962 | if (empty($options['appType'])) { |
963 | - $appType = App::$cur->type; |
|
963 | + $appType = App::$cur->type; |
|
964 | 964 | } else { |
965 | - $appType = $options['appType']; |
|
965 | + $appType = $options['appType']; |
|
966 | 966 | } |
967 | 967 | if (!empty(static::$storage['options']['share'])) { |
968 | - $moduleConfig = Config::share($classPath[0]); |
|
968 | + $moduleConfig = Config::share($classPath[0]); |
|
969 | 969 | } else { |
970 | - $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
970 | + $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
971 | 971 | } |
972 | 972 | if (!empty($moduleConfig['storage'][$appType][$classPath[1]])) { |
973 | - $items = $moduleConfig['storage'][$appType][$classPath[1]]; |
|
974 | - if (empty($options['where'])) { |
|
973 | + $items = $moduleConfig['storage'][$appType][$classPath[1]]; |
|
974 | + if (empty($options['where'])) { |
|
975 | 975 | return count($items); |
976 | - } |
|
977 | - foreach ($items as $key => $item) { |
|
976 | + } |
|
977 | + foreach ($items as $key => $item) { |
|
978 | 978 | if (!empty($options['where'])) { |
979 | - if (Model::checkWhere($item, $options['where'])) { |
|
979 | + if (Model::checkWhere($item, $options['where'])) { |
|
980 | 980 | $count++; |
981 | - } |
|
981 | + } |
|
982 | 982 | } else { |
983 | - $count++; |
|
983 | + $count++; |
|
984 | + } |
|
984 | 985 | } |
985 | - } |
|
986 | 986 | } |
987 | 987 | return $count; |
988 | - } |
|
989 | - |
|
990 | - /** |
|
991 | - * Check where for module storage query |
|
992 | - * |
|
993 | - * @param array $item |
|
994 | - * @param array|string $where |
|
995 | - * @param string $value |
|
996 | - * @param string $operation |
|
997 | - * @param string $concatenation |
|
998 | - * @return boolean |
|
999 | - */ |
|
1000 | - public static function checkWhere($item = [], $where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
988 | + } |
|
989 | + |
|
990 | + /** |
|
991 | + * Check where for module storage query |
|
992 | + * |
|
993 | + * @param array $item |
|
994 | + * @param array|string $where |
|
995 | + * @param string $value |
|
996 | + * @param string $operation |
|
997 | + * @param string $concatenation |
|
998 | + * @return boolean |
|
999 | + */ |
|
1000 | + public static function checkWhere($item = [], $where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
1001 | 1001 | |
1002 | 1002 | if (is_array($where)) { |
1003 | - if (is_array($where[0])) { |
|
1003 | + if (is_array($where[0])) { |
|
1004 | 1004 | foreach ($where as $whereItem) { |
1005 | - $result = forward_static_call_array(['Model', 'checkWhere'], array_merge([$item], $whereItem)); |
|
1006 | - if (!$result) { |
|
1005 | + $result = forward_static_call_array(['Model', 'checkWhere'], array_merge([$item], $whereItem)); |
|
1006 | + if (!$result) { |
|
1007 | 1007 | return false; |
1008 | - } |
|
1008 | + } |
|
1009 | 1009 | } |
1010 | 1010 | return true; |
1011 | - } else { |
|
1011 | + } else { |
|
1012 | 1012 | return forward_static_call_array(['Model', 'checkWhere'], array_merge([$item], $where)); |
1013 | - } |
|
1013 | + } |
|
1014 | 1014 | } |
1015 | 1015 | |
1016 | 1016 | if (!isset($item[$where]) && !$value) { |
1017 | - return true; |
|
1017 | + return true; |
|
1018 | 1018 | } |
1019 | 1019 | if (!isset($item[$where]) && $value) { |
1020 | - return false; |
|
1020 | + return false; |
|
1021 | 1021 | } |
1022 | 1022 | if ($item[$where] == $value) { |
1023 | - return true; |
|
1023 | + return true; |
|
1024 | 1024 | } |
1025 | 1025 | return false; |
1026 | - } |
|
1027 | - |
|
1028 | - /** |
|
1029 | - * Return count of records from data base |
|
1030 | - * |
|
1031 | - * @param array $options |
|
1032 | - * @return array|int |
|
1033 | - */ |
|
1034 | - public static function getCount($options = []) { |
|
1026 | + } |
|
1027 | + |
|
1028 | + /** |
|
1029 | + * Return count of records from data base |
|
1030 | + * |
|
1031 | + * @param array $options |
|
1032 | + * @return array|int |
|
1033 | + */ |
|
1034 | + public static function getCount($options = []) { |
|
1035 | 1035 | if (static::$storage['type'] == 'moduleConfig') { |
1036 | - return static::getCountFromModuleStorage($options); |
|
1036 | + return static::getCountFromModuleStorage($options); |
|
1037 | 1037 | } |
1038 | 1038 | $query = App::$cur->db->newQuery(); |
1039 | 1039 | if (!$query) { |
1040 | - return 0; |
|
1040 | + return 0; |
|
1041 | 1041 | } |
1042 | 1042 | if (!empty($options['where'])) { |
1043 | - static::fixPrefix($options['where'], 'first'); |
|
1043 | + static::fixPrefix($options['where'], 'first'); |
|
1044 | 1044 | } |
1045 | 1045 | if (!empty($options['group'])) { |
1046 | - static::fixPrefix($options['group'], 'first'); |
|
1046 | + static::fixPrefix($options['group'], 'first'); |
|
1047 | 1047 | } |
1048 | 1048 | if (!empty($options['where'])) |
1049 | - $query->where($options['where']); |
|
1049 | + $query->where($options['where']); |
|
1050 | 1050 | if (!empty($options['join'])) |
1051 | - $query->join($options['join']); |
|
1051 | + $query->join($options['join']); |
|
1052 | 1052 | if (!empty($options['order'])) { |
1053 | - $query->order($options['order']); |
|
1053 | + $query->order($options['order']); |
|
1054 | 1054 | } |
1055 | 1055 | if (!empty($options['limit'])) |
1056 | - $limit = (int) $options['limit']; |
|
1056 | + $limit = (int) $options['limit']; |
|
1057 | 1057 | else { |
1058 | - $limit = 0; |
|
1058 | + $limit = 0; |
|
1059 | 1059 | } |
1060 | 1060 | if (!empty($options['start'])) |
1061 | - $start = (int) $options['start']; |
|
1061 | + $start = (int) $options['start']; |
|
1062 | 1062 | else { |
1063 | - $start = 0; |
|
1063 | + $start = 0; |
|
1064 | 1064 | } |
1065 | 1065 | if ($limit || $start) { |
1066 | - $query->limit($start, $limit); |
|
1066 | + $query->limit($start, $limit); |
|
1067 | 1067 | } |
1068 | 1068 | |
1069 | 1069 | foreach (static::$relJoins as $join) { |
1070 | - $query->join($join[0], $join[1]); |
|
1070 | + $query->join($join[0], $join[1]); |
|
1071 | 1071 | } |
1072 | 1072 | static::$relJoins = []; |
1073 | 1073 | foreach (static::$needJoin as $rel) { |
1074 | - $relations = static::relations(); |
|
1075 | - if (isset($relations[$rel])) { |
|
1074 | + $relations = static::relations(); |
|
1075 | + if (isset($relations[$rel])) { |
|
1076 | 1076 | $type = empty($relations[$rel]['type']) ? 'to' : $relations[$rel]['type']; |
1077 | 1077 | switch ($type) { |
1078 | - case 'to': |
|
1078 | + case 'to': |
|
1079 | 1079 | $relCol = $relations[$rel]['col']; |
1080 | 1080 | static::fixPrefix($relCol); |
1081 | 1081 | $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
1082 | 1082 | break; |
1083 | - case 'one': |
|
1083 | + case 'one': |
|
1084 | 1084 | $col = $relations[$rel]['col']; |
1085 | 1085 | $relations[$rel]['model']::fixPrefix($col); |
1086 | 1086 | $query->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
1087 | 1087 | break; |
1088 | 1088 | } |
1089 | - } |
|
1089 | + } |
|
1090 | 1090 | } |
1091 | 1091 | static::$needJoin = []; |
1092 | 1092 | $cols = 'COUNT('; |
1093 | 1093 | |
1094 | 1094 | if (!empty($options['distinct'])) { |
1095 | - if (is_bool($options['distinct'])) { |
|
1095 | + if (is_bool($options['distinct'])) { |
|
1096 | 1096 | $cols .= 'DISTINCT *'; |
1097 | - } else { |
|
1097 | + } else { |
|
1098 | 1098 | $cols .= "DISTINCT {$options['distinct']}"; |
1099 | - } |
|
1099 | + } |
|
1100 | 1100 | } else { |
1101 | - $cols .= '*'; |
|
1101 | + $cols .= '*'; |
|
1102 | 1102 | } |
1103 | 1103 | $cols .=') as `count`' . (!empty($options['cols']) ? ',' . $options['cols'] : ''); |
1104 | 1104 | $query->cols = $cols; |
1105 | 1105 | if (!empty($options['group'])) { |
1106 | - $query->group($options['group']); |
|
1106 | + $query->group($options['group']); |
|
1107 | 1107 | } |
1108 | 1108 | try { |
1109 | - $result = $query->select(static::table()); |
|
1109 | + $result = $query->select(static::table()); |
|
1110 | 1110 | } catch (PDOException $exc) { |
1111 | - if ($exc->getCode() == '42S02') { |
|
1111 | + if ($exc->getCode() == '42S02') { |
|
1112 | 1112 | static::createTable(); |
1113 | - } |
|
1114 | - $result = $query->select(static::table()); |
|
1113 | + } |
|
1114 | + $result = $query->select(static::table()); |
|
1115 | 1115 | } |
1116 | 1116 | if (!empty($options['group'])) { |
1117 | - $count = $result->getArray(); |
|
1118 | - return $count; |
|
1117 | + $count = $result->getArray(); |
|
1118 | + return $count; |
|
1119 | 1119 | } else { |
1120 | - $count = $result->fetch(); |
|
1121 | - return $count['count']; |
|
1122 | - } |
|
1123 | - } |
|
1124 | - |
|
1125 | - /** |
|
1126 | - * Update records in data base |
|
1127 | - * |
|
1128 | - * @param array $params |
|
1129 | - * @param array $where |
|
1130 | - * @return boolean |
|
1131 | - */ |
|
1132 | - public static function update($params, $where = []) { |
|
1120 | + $count = $result->fetch(); |
|
1121 | + return $count['count']; |
|
1122 | + } |
|
1123 | + } |
|
1124 | + |
|
1125 | + /** |
|
1126 | + * Update records in data base |
|
1127 | + * |
|
1128 | + * @param array $params |
|
1129 | + * @param array $where |
|
1130 | + * @return boolean |
|
1131 | + */ |
|
1132 | + public static function update($params, $where = []) { |
|
1133 | 1133 | static::fixPrefix($params); |
1134 | 1134 | |
1135 | 1135 | $cols = self::cols(); |
1136 | 1136 | |
1137 | 1137 | $values = []; |
1138 | 1138 | foreach ($cols as $col => $param) { |
1139 | - if (isset($params[$col])) |
|
1139 | + if (isset($params[$col])) |
|
1140 | 1140 | $values[$col] = $params[$col]; |
1141 | 1141 | } |
1142 | 1142 | if (empty($values)) { |
1143 | - return false; |
|
1143 | + return false; |
|
1144 | 1144 | } |
1145 | 1145 | |
1146 | 1146 | if (!empty($where)) { |
1147 | - static::fixPrefix($where, 'first'); |
|
1147 | + static::fixPrefix($where, 'first'); |
|
1148 | 1148 | |
1149 | - App::$cur->db->where($where); |
|
1149 | + App::$cur->db->where($where); |
|
1150 | 1150 | } |
1151 | 1151 | App::$cur->db->update(static::table(), $values); |
1152 | - } |
|
1153 | - |
|
1154 | - /** |
|
1155 | - * Return primary key of object |
|
1156 | - * |
|
1157 | - * @return mixed |
|
1158 | - */ |
|
1159 | - public function pk() { |
|
1152 | + } |
|
1153 | + |
|
1154 | + /** |
|
1155 | + * Return primary key of object |
|
1156 | + * |
|
1157 | + * @return mixed |
|
1158 | + */ |
|
1159 | + public function pk() { |
|
1160 | 1160 | return $this->{$this->index()}; |
1161 | - } |
|
1161 | + } |
|
1162 | 1162 | |
1163 | - /** |
|
1164 | - * Before save trigger |
|
1165 | - */ |
|
1166 | - public function beforeSave() { |
|
1163 | + /** |
|
1164 | + * Before save trigger |
|
1165 | + */ |
|
1166 | + public function beforeSave() { |
|
1167 | 1167 | |
1168 | - } |
|
1168 | + } |
|
1169 | 1169 | |
1170 | - /** |
|
1171 | - * Save object to module storage |
|
1172 | - * |
|
1173 | - * @param array $options |
|
1174 | - * @return boolean |
|
1175 | - */ |
|
1176 | - public function saveModuleStorage($options) { |
|
1170 | + /** |
|
1171 | + * Save object to module storage |
|
1172 | + * |
|
1173 | + * @param array $options |
|
1174 | + * @return boolean |
|
1175 | + */ |
|
1176 | + public function saveModuleStorage($options) { |
|
1177 | 1177 | |
1178 | 1178 | $col = static::index(); |
1179 | 1179 | $id = $this->pk(); |
@@ -1181,54 +1181,54 @@ discard block |
||
1181 | 1181 | $classPath = explode('\\', get_called_class()); |
1182 | 1182 | |
1183 | 1183 | if (!empty(static::$storage['options']['share'])) { |
1184 | - $moduleConfig = Config::share($classPath[0]); |
|
1184 | + $moduleConfig = Config::share($classPath[0]); |
|
1185 | 1185 | } else { |
1186 | - $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
1186 | + $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
1187 | 1187 | } |
1188 | 1188 | |
1189 | 1189 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
1190 | - if (empty($options['appType'])) { |
|
1190 | + if (empty($options['appType'])) { |
|
1191 | 1191 | $appType = App::$cur->type; |
1192 | - } else { |
|
1192 | + } else { |
|
1193 | 1193 | $appType = $options['appType']; |
1194 | - } |
|
1195 | - $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
1194 | + } |
|
1195 | + $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
1196 | 1196 | } else { |
1197 | - $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
1197 | + $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
1198 | 1198 | } |
1199 | 1199 | if (empty($storage[$classPath[1]])) { |
1200 | - $storage[$classPath[1]] = []; |
|
1200 | + $storage[$classPath[1]] = []; |
|
1201 | 1201 | } |
1202 | 1202 | if ($id) { |
1203 | - foreach ($storage[$classPath[1]] as $key => $item) { |
|
1203 | + foreach ($storage[$classPath[1]] as $key => $item) { |
|
1204 | 1204 | if ($item[$col] == $id) { |
1205 | - $storage[$classPath[1]][$key] = $this->_params; |
|
1206 | - break; |
|
1205 | + $storage[$classPath[1]][$key] = $this->_params; |
|
1206 | + break; |
|
1207 | + } |
|
1207 | 1208 | } |
1208 | - } |
|
1209 | 1209 | } else { |
1210 | - $id = !empty($storage['scheme'][$classPath[1]]['ai']) ? $storage['scheme'][$classPath[1]]['ai'] : 1; |
|
1211 | - $this->$col = $id; |
|
1212 | - $storage['scheme'][$classPath[1]]['ai'] = $id + 1; |
|
1213 | - $storage[$classPath[1]][] = $this->_params; |
|
1210 | + $id = !empty($storage['scheme'][$classPath[1]]['ai']) ? $storage['scheme'][$classPath[1]]['ai'] : 1; |
|
1211 | + $this->$col = $id; |
|
1212 | + $storage['scheme'][$classPath[1]]['ai'] = $id + 1; |
|
1213 | + $storage[$classPath[1]][] = $this->_params; |
|
1214 | 1214 | } |
1215 | 1215 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
1216 | - $moduleConfig['storage'][$appType] = $storage; |
|
1216 | + $moduleConfig['storage'][$appType] = $storage; |
|
1217 | 1217 | } else { |
1218 | - $moduleConfig['storage'] = $storage; |
|
1218 | + $moduleConfig['storage'] = $storage; |
|
1219 | 1219 | } |
1220 | 1220 | if (empty(static::$storage['options']['share'])) { |
1221 | - Config::save('module', $moduleConfig, $classPath[0]); |
|
1221 | + Config::save('module', $moduleConfig, $classPath[0]); |
|
1222 | 1222 | } else { |
1223 | - Config::save('share', $moduleConfig, $classPath[0]); |
|
1223 | + Config::save('share', $moduleConfig, $classPath[0]); |
|
1224 | 1224 | } |
1225 | 1225 | return true; |
1226 | - } |
|
1226 | + } |
|
1227 | 1227 | |
1228 | - /** |
|
1229 | - * Update tree path category |
|
1230 | - */ |
|
1231 | - public function changeCategoryTree() { |
|
1228 | + /** |
|
1229 | + * Update tree path category |
|
1230 | + */ |
|
1231 | + public function changeCategoryTree() { |
|
1232 | 1232 | $class = get_class($this); |
1233 | 1233 | $itemModel = $class::$treeCategory; |
1234 | 1234 | $oldPath = $this->tree_path; |
@@ -1239,563 +1239,563 @@ discard block |
||
1239 | 1239 | $categoryTreeCol = $this->colPrefix() . 'tree_path'; |
1240 | 1240 | $categoryTable = \App::$cur->db->table_prefix . $this->table(); |
1241 | 1241 | if ($oldPath) { |
1242 | - \App::$cur->db->query('UPDATE |
|
1242 | + \App::$cur->db->query('UPDATE |
|
1243 | 1243 | ' . $categoryTable . ' |
1244 | 1244 | SET |
1245 | 1245 | ' . $categoryTreeCol . ' = REPLACE(' . $categoryTreeCol . ', "' . $oldPath . $this->id . '/' . '", "' . $this->tree_path . $this->id . '/' . '") |
1246 | 1246 | WHERE ' . $categoryTreeCol . ' LIKE "' . $oldPath . $this->id . '/' . '%"'); |
1247 | 1247 | |
1248 | - \App::$cur->db->query('UPDATE |
|
1248 | + \App::$cur->db->query('UPDATE |
|
1249 | 1249 | ' . $itemsTable . ' |
1250 | 1250 | SET |
1251 | 1251 | ' . $itemTreeCol . ' = REPLACE(' . $itemTreeCol . ', "' . $oldPath . $this->id . '/' . '", "' . $this->tree_path . $this->id . '/' . '") |
1252 | 1252 | WHERE ' . $itemTreeCol . ' LIKE "' . $oldPath . $this->id . '/' . '%"'); |
1253 | 1253 | } |
1254 | 1254 | $itemModel::update([$itemTreeCol => $this->tree_path . $this->id . '/'], [$itemModel::colPrefix() . $this->index(), $this->id]); |
1255 | - } |
|
1256 | - |
|
1257 | - /** |
|
1258 | - * Return tree path |
|
1259 | - * |
|
1260 | - * @param \Model $catalog |
|
1261 | - * @return string |
|
1262 | - */ |
|
1263 | - public function getCatalogTree($catalog) { |
|
1255 | + } |
|
1256 | + |
|
1257 | + /** |
|
1258 | + * Return tree path |
|
1259 | + * |
|
1260 | + * @param \Model $catalog |
|
1261 | + * @return string |
|
1262 | + */ |
|
1263 | + public function getCatalogTree($catalog) { |
|
1264 | 1264 | $catalogClass = get_class($catalog); |
1265 | 1265 | $catalogParent = $catalogClass::get($catalog->parent_id); |
1266 | 1266 | if ($catalog && $catalogParent) { |
1267 | - if ($catalogParent->tree_path) { |
|
1267 | + if ($catalogParent->tree_path) { |
|
1268 | 1268 | return $catalogParent->tree_path . $catalogParent->id . '/'; |
1269 | - } else { |
|
1269 | + } else { |
|
1270 | 1270 | return $this->getCatalogTree($catalogParent) . $catalogParent->id . '/'; |
1271 | - } |
|
1271 | + } |
|
1272 | 1272 | } |
1273 | 1273 | return '/'; |
1274 | - } |
|
1274 | + } |
|
1275 | 1275 | |
1276 | - /** |
|
1277 | - * Update tree path item |
|
1278 | - */ |
|
1279 | - public function changeItemTree() { |
|
1276 | + /** |
|
1277 | + * Update tree path item |
|
1278 | + */ |
|
1279 | + public function changeItemTree() { |
|
1280 | 1280 | $class = get_class($this); |
1281 | 1281 | $categoryModel = $class::$categoryModel; |
1282 | 1282 | $category = $categoryModel::get($this->{$categoryModel::index()}); |
1283 | 1283 | if ($category) { |
1284 | - $this->tree_path = $category->tree_path . $category->pk() . '/'; |
|
1284 | + $this->tree_path = $category->tree_path . $category->pk() . '/'; |
|
1285 | 1285 | } else { |
1286 | - $this->tree_path = '/'; |
|
1286 | + $this->tree_path = '/'; |
|
1287 | + } |
|
1287 | 1288 | } |
1288 | - } |
|
1289 | 1289 | |
1290 | - /** |
|
1291 | - * Save object to data base |
|
1292 | - * |
|
1293 | - * @param array $options |
|
1294 | - * @return boolean|int |
|
1295 | - */ |
|
1296 | - public function save($options = []) { |
|
1290 | + /** |
|
1291 | + * Save object to data base |
|
1292 | + * |
|
1293 | + * @param array $options |
|
1294 | + * @return boolean|int |
|
1295 | + */ |
|
1296 | + public function save($options = []) { |
|
1297 | 1297 | |
1298 | 1298 | if (static::$storage['type'] == 'moduleConfig') { |
1299 | - return static::saveModuleStorage($options); |
|
1299 | + return static::saveModuleStorage($options); |
|
1300 | 1300 | } |
1301 | 1301 | $class = get_class($this); |
1302 | 1302 | if ($class::$categoryModel) { |
1303 | - $this->changeItemTree(); |
|
1303 | + $this->changeItemTree(); |
|
1304 | 1304 | } |
1305 | 1305 | if ($class::$treeCategory) { |
1306 | - $this->changeCategoryTree(); |
|
1306 | + $this->changeCategoryTree(); |
|
1307 | 1307 | } |
1308 | 1308 | if (!empty($this->_changedParams) && $this->pk()) { |
1309 | - Inji::$inst->event('modelItemParamsChanged-' . get_called_class(), $this); |
|
1309 | + Inji::$inst->event('modelItemParamsChanged-' . get_called_class(), $this); |
|
1310 | 1310 | } |
1311 | 1311 | $this->beforeSave(); |
1312 | 1312 | |
1313 | 1313 | $values = []; |
1314 | 1314 | |
1315 | 1315 | foreach ($this->cols() as $col => $param) { |
1316 | - if (isset($this->_params[$col])) { |
|
1316 | + if (isset($this->_params[$col])) { |
|
1317 | 1317 | $values[$col] = $this->_params[$col]; |
1318 | - } |
|
1318 | + } |
|
1319 | 1319 | } |
1320 | 1320 | foreach ($class::$cols as $colName => $params) { |
1321 | - $class::fixPrefix($colName); |
|
1322 | - if (isset($params['default']) && !isset($values[$colName])) { |
|
1321 | + $class::fixPrefix($colName); |
|
1322 | + if (isset($params['default']) && !isset($values[$colName])) { |
|
1323 | 1323 | $this->_params[$colName] = $values[$colName] = $params['default']; |
1324 | - } |
|
1324 | + } |
|
1325 | 1325 | } |
1326 | 1326 | if (empty($values) && empty($options['empty'])) { |
1327 | - return false; |
|
1327 | + return false; |
|
1328 | 1328 | } |
1329 | 1329 | |
1330 | 1330 | if ($this->pk()) { |
1331 | - $new = false; |
|
1332 | - if ($this->get($this->_params[$this->index()])) { |
|
1331 | + $new = false; |
|
1332 | + if ($this->get($this->_params[$this->index()])) { |
|
1333 | 1333 | App::$cur->db->where($this->index(), $this->_params[$this->index()]); |
1334 | 1334 | App::$cur->db->update($this->table(), $values); |
1335 | - } else { |
|
1335 | + } else { |
|
1336 | 1336 | |
1337 | 1337 | $this->_params[$this->index()] = App::$cur->db->insert($this->table(), $values); |
1338 | - } |
|
1338 | + } |
|
1339 | 1339 | } else { |
1340 | - $new = true; |
|
1341 | - $this->_params[$this->index()] = App::$cur->db->insert($this->table(), $values); |
|
1340 | + $new = true; |
|
1341 | + $this->_params[$this->index()] = App::$cur->db->insert($this->table(), $values); |
|
1342 | 1342 | } |
1343 | 1343 | App::$cur->db->where($this->index(), $this->_params[$this->index()]); |
1344 | 1344 | try { |
1345 | - $result = App::$cur->db->select($this->table()); |
|
1345 | + $result = App::$cur->db->select($this->table()); |
|
1346 | 1346 | } catch (PDOException $exc) { |
1347 | - if ($exc->getCode() == '42S02') { |
|
1347 | + if ($exc->getCode() == '42S02') { |
|
1348 | 1348 | $this->createTable(); |
1349 | - } |
|
1350 | - $result = App::$cur->db->select($this->table()); |
|
1349 | + } |
|
1350 | + $result = App::$cur->db->select($this->table()); |
|
1351 | 1351 | } |
1352 | 1352 | $this->_params = $result->fetch(); |
1353 | 1353 | if ($new) { |
1354 | - Inji::$inst->event('modelCreatedItem-' . get_called_class(), $this); |
|
1354 | + Inji::$inst->event('modelCreatedItem-' . get_called_class(), $this); |
|
1355 | 1355 | } |
1356 | 1356 | $this->afterSave(); |
1357 | 1357 | return $this->{$this->index()}; |
1358 | - } |
|
1358 | + } |
|
1359 | 1359 | |
1360 | - /** |
|
1361 | - * After save trigger |
|
1362 | - */ |
|
1363 | - public function afterSave() { |
|
1360 | + /** |
|
1361 | + * After save trigger |
|
1362 | + */ |
|
1363 | + public function afterSave() { |
|
1364 | 1364 | |
1365 | - } |
|
1365 | + } |
|
1366 | 1366 | |
1367 | - /** |
|
1368 | - * Before delete trigger |
|
1369 | - */ |
|
1370 | - public function beforeDelete() { |
|
1367 | + /** |
|
1368 | + * Before delete trigger |
|
1369 | + */ |
|
1370 | + public function beforeDelete() { |
|
1371 | 1371 | |
1372 | - } |
|
1372 | + } |
|
1373 | 1373 | |
1374 | - /** |
|
1375 | - * Delete item from module storage |
|
1376 | - * |
|
1377 | - * @param array $options |
|
1378 | - * @return boolean |
|
1379 | - */ |
|
1380 | - public function deleteFromModuleStorage($options) { |
|
1374 | + /** |
|
1375 | + * Delete item from module storage |
|
1376 | + * |
|
1377 | + * @param array $options |
|
1378 | + * @return boolean |
|
1379 | + */ |
|
1380 | + public function deleteFromModuleStorage($options) { |
|
1381 | 1381 | |
1382 | 1382 | $col = static::index(); |
1383 | 1383 | $id = $this->pk(); |
1384 | 1384 | $appType = ''; |
1385 | 1385 | $classPath = explode('\\', get_called_class()); |
1386 | 1386 | if (!empty(static::$storage['options']['share'])) { |
1387 | - $moduleConfig = Config::share($classPath[0]); |
|
1387 | + $moduleConfig = Config::share($classPath[0]); |
|
1388 | 1388 | } else { |
1389 | - $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
1389 | + $moduleConfig = Config::module($classPath[0], strpos(static::$storage['type'], 'system') !== false); |
|
1390 | 1390 | } |
1391 | 1391 | |
1392 | 1392 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
1393 | - if (empty($options['appType'])) { |
|
1393 | + if (empty($options['appType'])) { |
|
1394 | 1394 | $appType = App::$cur->type; |
1395 | - } else { |
|
1395 | + } else { |
|
1396 | 1396 | $appType = $options['appType']; |
1397 | - } |
|
1398 | - $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
1397 | + } |
|
1398 | + $storage = !empty($moduleConfig['storage'][$appType]) ? $moduleConfig['storage'][$appType] : []; |
|
1399 | 1399 | } else { |
1400 | - $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
1400 | + $storage = !empty($moduleConfig['storage']) ? $moduleConfig['storage'] : []; |
|
1401 | 1401 | } |
1402 | 1402 | if (empty($storage[$classPath[1]])) { |
1403 | - $storage[$classPath[1]] = []; |
|
1403 | + $storage[$classPath[1]] = []; |
|
1404 | 1404 | } |
1405 | 1405 | foreach ($storage[$classPath[1]] as $key => $item) { |
1406 | 1406 | |
1407 | - if ($item[$col] == $id) { |
|
1407 | + if ($item[$col] == $id) { |
|
1408 | 1408 | unset($storage[$classPath[1]][$key]); |
1409 | 1409 | break; |
1410 | - } |
|
1410 | + } |
|
1411 | 1411 | } |
1412 | 1412 | if (!empty($moduleConfig['storage']['appTypeSplit'])) { |
1413 | - $moduleConfig['storage'][$appType] = $storage; |
|
1413 | + $moduleConfig['storage'][$appType] = $storage; |
|
1414 | 1414 | } else { |
1415 | - $moduleConfig['storage'] = $storage; |
|
1415 | + $moduleConfig['storage'] = $storage; |
|
1416 | 1416 | } |
1417 | 1417 | if (empty(static::$storage['options']['share'])) { |
1418 | - Config::save('module', $moduleConfig, $classPath[0]); |
|
1418 | + Config::save('module', $moduleConfig, $classPath[0]); |
|
1419 | 1419 | } else { |
1420 | - Config::save('share', $moduleConfig, $classPath[0]); |
|
1420 | + Config::save('share', $moduleConfig, $classPath[0]); |
|
1421 | 1421 | } |
1422 | 1422 | return true; |
1423 | - } |
|
1424 | - |
|
1425 | - /** |
|
1426 | - * Delete item from data base |
|
1427 | - * |
|
1428 | - * @param array $options |
|
1429 | - * @return boolean |
|
1430 | - */ |
|
1431 | - public function delete($options = []) { |
|
1423 | + } |
|
1424 | + |
|
1425 | + /** |
|
1426 | + * Delete item from data base |
|
1427 | + * |
|
1428 | + * @param array $options |
|
1429 | + * @return boolean |
|
1430 | + */ |
|
1431 | + public function delete($options = []) { |
|
1432 | 1432 | $this->beforeDelete(); |
1433 | 1433 | |
1434 | 1434 | if (static::$storage['type'] == 'moduleConfig') { |
1435 | - return static::deleteFromModuleStorage($options); |
|
1435 | + return static::deleteFromModuleStorage($options); |
|
1436 | 1436 | } |
1437 | 1437 | if (!empty($this->_params[$this->index()])) { |
1438 | - App::$cur->db->where($this->index(), $this->_params[$this->index()]); |
|
1439 | - $result = App::$cur->db->delete($this->table()); |
|
1440 | - if ($result) { |
|
1438 | + App::$cur->db->where($this->index(), $this->_params[$this->index()]); |
|
1439 | + $result = App::$cur->db->delete($this->table()); |
|
1440 | + if ($result) { |
|
1441 | 1441 | $this->afterDelete(); |
1442 | 1442 | return $result; |
1443 | - } |
|
1443 | + } |
|
1444 | 1444 | } |
1445 | 1445 | return false; |
1446 | - } |
|
1447 | - |
|
1448 | - /** |
|
1449 | - * Delete items from data base |
|
1450 | - * |
|
1451 | - * @param array $where |
|
1452 | - */ |
|
1453 | - public static function deleteList($where) { |
|
1446 | + } |
|
1447 | + |
|
1448 | + /** |
|
1449 | + * Delete items from data base |
|
1450 | + * |
|
1451 | + * @param array $where |
|
1452 | + */ |
|
1453 | + public static function deleteList($where) { |
|
1454 | 1454 | if (!empty($where)) { |
1455 | - static::fixPrefix($where, 'first'); |
|
1456 | - App::$cur->db->where($where); |
|
1455 | + static::fixPrefix($where, 'first'); |
|
1456 | + App::$cur->db->where($where); |
|
1457 | 1457 | } |
1458 | 1458 | App::$cur->db->delete(static::table()); |
1459 | - } |
|
1459 | + } |
|
1460 | 1460 | |
1461 | - /** |
|
1462 | - * After delete trigger |
|
1463 | - */ |
|
1464 | - public function afterDelete() { |
|
1461 | + /** |
|
1462 | + * After delete trigger |
|
1463 | + */ |
|
1464 | + public function afterDelete() { |
|
1465 | 1465 | |
1466 | - } |
|
1466 | + } |
|
1467 | 1467 | |
1468 | - /** |
|
1469 | - * find relation for col name |
|
1470 | - * |
|
1471 | - * @param string $col |
|
1472 | - * @return array|null |
|
1473 | - */ |
|
1474 | - public static function findRelation($col) { |
|
1468 | + /** |
|
1469 | + * find relation for col name |
|
1470 | + * |
|
1471 | + * @param string $col |
|
1472 | + * @return array|null |
|
1473 | + */ |
|
1474 | + public static function findRelation($col) { |
|
1475 | 1475 | |
1476 | 1476 | foreach (static::relations() as $relName => $rel) { |
1477 | - if ($rel['col'] == $col) |
|
1477 | + if ($rel['col'] == $col) |
|
1478 | 1478 | return $relName; |
1479 | 1479 | } |
1480 | 1480 | return NULL; |
1481 | - } |
|
1482 | - |
|
1483 | - /** |
|
1484 | - * Set params for model |
|
1485 | - * |
|
1486 | - * @param array $params |
|
1487 | - */ |
|
1488 | - public function setParams($params) { |
|
1481 | + } |
|
1482 | + |
|
1483 | + /** |
|
1484 | + * Set params for model |
|
1485 | + * |
|
1486 | + * @param array $params |
|
1487 | + */ |
|
1488 | + public function setParams($params) { |
|
1489 | 1489 | static::fixPrefix($params); |
1490 | 1490 | $className = get_called_class(); |
1491 | 1491 | foreach ($params as $paramName => $value) { |
1492 | - $shortName = preg_replace('!' . $this->colPrefix() . '!', '', $paramName); |
|
1493 | - if (!empty($className::$cols[$shortName])) { |
|
1492 | + $shortName = preg_replace('!' . $this->colPrefix() . '!', '', $paramName); |
|
1493 | + if (!empty($className::$cols[$shortName])) { |
|
1494 | 1494 | switch ($className::$cols[$shortName]['type']) { |
1495 | - case 'decimal': |
|
1495 | + case 'decimal': |
|
1496 | 1496 | $params[$paramName] = (float) $value; |
1497 | 1497 | break; |
1498 | - case 'number': |
|
1498 | + case 'number': |
|
1499 | 1499 | $params[$paramName] = (int) $value; |
1500 | 1500 | break; |
1501 | - case 'bool': |
|
1501 | + case 'bool': |
|
1502 | 1502 | $params[$paramName] = (bool) $value; |
1503 | 1503 | break; |
1504 | 1504 | } |
1505 | - } |
|
1505 | + } |
|
1506 | 1506 | } |
1507 | 1507 | $this->_params = array_merge($this->_params, $params); |
1508 | - } |
|
1509 | - |
|
1510 | - /** |
|
1511 | - * Return relation |
|
1512 | - * |
|
1513 | - * @param string $relName |
|
1514 | - * @return array|boolean |
|
1515 | - */ |
|
1516 | - public static function getRelation($relName) { |
|
1508 | + } |
|
1509 | + |
|
1510 | + /** |
|
1511 | + * Return relation |
|
1512 | + * |
|
1513 | + * @param string $relName |
|
1514 | + * @return array|boolean |
|
1515 | + */ |
|
1516 | + public static function getRelation($relName) { |
|
1517 | 1517 | $relations = static::relations(); |
1518 | 1518 | return !empty($relations[$relName]) ? $relations[$relName] : false; |
1519 | - } |
|
1520 | - |
|
1521 | - /** |
|
1522 | - * Load relation |
|
1523 | - * |
|
1524 | - * @param string $name |
|
1525 | - * @param array $params |
|
1526 | - * @return null|array|integer|\Model |
|
1527 | - */ |
|
1528 | - public function loadRelation($name, $params = []) { |
|
1519 | + } |
|
1520 | + |
|
1521 | + /** |
|
1522 | + * Load relation |
|
1523 | + * |
|
1524 | + * @param string $name |
|
1525 | + * @param array $params |
|
1526 | + * @return null|array|integer|\Model |
|
1527 | + */ |
|
1528 | + public function loadRelation($name, $params = []) { |
|
1529 | 1529 | $relation = static::getRelation($name); |
1530 | 1530 | if ($relation) { |
1531 | - if (!isset($relation['type'])) { |
|
1531 | + if (!isset($relation['type'])) { |
|
1532 | 1532 | $type = 'to'; |
1533 | - } else { |
|
1533 | + } else { |
|
1534 | 1534 | $type = $relation['type']; |
1535 | - } |
|
1536 | - $getCol = null; |
|
1537 | - $getParams = []; |
|
1538 | - switch ($type) { |
|
1535 | + } |
|
1536 | + $getCol = null; |
|
1537 | + $getParams = []; |
|
1538 | + switch ($type) { |
|
1539 | 1539 | case 'relModel': |
1540 | 1540 | if (!$this->pk()) { |
1541 | 1541 | return []; |
1542 | - } |
|
1543 | - $fixedCol = $relation['model']::index(); |
|
1544 | - $relation['relModel']::fixPrefix($fixedCol); |
|
1545 | - $ids = array_keys($relation['relModel']::getList(['where' => [$this->index(), $this->pk()], 'array' => true, 'key' => $fixedCol])); |
|
1546 | - if (empty($ids)) { |
|
1542 | + } |
|
1543 | + $fixedCol = $relation['model']::index(); |
|
1544 | + $relation['relModel']::fixPrefix($fixedCol); |
|
1545 | + $ids = array_keys($relation['relModel']::getList(['where' => [$this->index(), $this->pk()], 'array' => true, 'key' => $fixedCol])); |
|
1546 | + if (empty($ids)) { |
|
1547 | 1547 | if (empty($params['count'])) { |
1548 | - return []; |
|
1548 | + return []; |
|
1549 | 1549 | } else { |
1550 | - return 0; |
|
1550 | + return 0; |
|
1551 | + } |
|
1551 | 1552 | } |
1552 | - } |
|
1553 | - $getType = 'getList'; |
|
1554 | - $options = [ |
|
1555 | - 'where' => [$relation['model']::index(), implode(',', $ids), 'IN'], |
|
1556 | - 'array' => (!empty($params['array'])) ? true : false, |
|
1557 | - 'key' => (isset($params['key'])) ? $params['key'] : ((isset($relation['resultKey'])) ? $relation['resultKey'] : null), |
|
1558 | - 'start' => (isset($params['start'])) ? $params['start'] : ((isset($relation['start'])) ? $relation['start'] : null), |
|
1559 | - 'order' => (isset($params['order'])) ? $params['order'] : ((isset($relation['order'])) ? $relation['order'] : null), |
|
1560 | - 'limit' => (isset($params['limit'])) ? $params['limit'] : ((isset($relation['limit'])) ? $relation['limit'] : null), |
|
1561 | - ]; |
|
1562 | - break; |
|
1553 | + $getType = 'getList'; |
|
1554 | + $options = [ |
|
1555 | + 'where' => [$relation['model']::index(), implode(',', $ids), 'IN'], |
|
1556 | + 'array' => (!empty($params['array'])) ? true : false, |
|
1557 | + 'key' => (isset($params['key'])) ? $params['key'] : ((isset($relation['resultKey'])) ? $relation['resultKey'] : null), |
|
1558 | + 'start' => (isset($params['start'])) ? $params['start'] : ((isset($relation['start'])) ? $relation['start'] : null), |
|
1559 | + 'order' => (isset($params['order'])) ? $params['order'] : ((isset($relation['order'])) ? $relation['order'] : null), |
|
1560 | + 'limit' => (isset($params['limit'])) ? $params['limit'] : ((isset($relation['limit'])) ? $relation['limit'] : null), |
|
1561 | + ]; |
|
1562 | + break; |
|
1563 | 1563 | case 'many': |
1564 | 1564 | if (!$this->{$this->index()}) { |
1565 | 1565 | return []; |
1566 | - } |
|
1567 | - $getType = 'getList'; |
|
1568 | - $options = [ |
|
1569 | - 'join' => (isset($relation['join'])) ? $relation['join'] : null, |
|
1570 | - 'key' => (isset($params['key'])) ? $params['key'] : ((isset($relation['resultKey'])) ? $relation['resultKey'] : null), |
|
1571 | - 'array' => (!empty($params['array'])) ? true : false, |
|
1572 | - 'forSelect' => (!empty($params['forSelect'])) ? true : false, |
|
1573 | - 'order' => (isset($params['order'])) ? $params['order'] : ((isset($relation['order'])) ? $relation['order'] : null), |
|
1574 | - 'start' => (isset($params['start'])) ? $params['start'] : ((isset($relation['start'])) ? $relation['start'] : null), |
|
1575 | - 'limit' => (isset($params['limit'])) ? $params['limit'] : ((isset($relation['limit'])) ? $relation['limit'] : null), |
|
1576 | - 'appType' => (isset($params['appType'])) ? $params['appType'] : ((isset($relation['appType'])) ? $relation['appType'] : null), |
|
1577 | - 'where' => [] |
|
1578 | - ]; |
|
1579 | - $options['where'][] = [$relation['col'], $this->{$this->index()}]; |
|
1580 | - if (!empty($relation['where'])) { |
|
1566 | + } |
|
1567 | + $getType = 'getList'; |
|
1568 | + $options = [ |
|
1569 | + 'join' => (isset($relation['join'])) ? $relation['join'] : null, |
|
1570 | + 'key' => (isset($params['key'])) ? $params['key'] : ((isset($relation['resultKey'])) ? $relation['resultKey'] : null), |
|
1571 | + 'array' => (!empty($params['array'])) ? true : false, |
|
1572 | + 'forSelect' => (!empty($params['forSelect'])) ? true : false, |
|
1573 | + 'order' => (isset($params['order'])) ? $params['order'] : ((isset($relation['order'])) ? $relation['order'] : null), |
|
1574 | + 'start' => (isset($params['start'])) ? $params['start'] : ((isset($relation['start'])) ? $relation['start'] : null), |
|
1575 | + 'limit' => (isset($params['limit'])) ? $params['limit'] : ((isset($relation['limit'])) ? $relation['limit'] : null), |
|
1576 | + 'appType' => (isset($params['appType'])) ? $params['appType'] : ((isset($relation['appType'])) ? $relation['appType'] : null), |
|
1577 | + 'where' => [] |
|
1578 | + ]; |
|
1579 | + $options['where'][] = [$relation['col'], $this->{$this->index()}]; |
|
1580 | + if (!empty($relation['where'])) { |
|
1581 | 1581 | $options['where'] = array_merge($options['where'], [$relation['where']]); |
1582 | - } |
|
1583 | - if (!empty($params['where'])) { |
|
1582 | + } |
|
1583 | + if (!empty($params['where'])) { |
|
1584 | 1584 | $options['where'] = array_merge($options['where'], [$params['where']]); |
1585 | - } |
|
1586 | - break; |
|
1585 | + } |
|
1586 | + break; |
|
1587 | 1587 | case 'one': |
1588 | 1588 | $getType = 'get'; |
1589 | - $options = [$relation['col'], $this->pk()]; |
|
1590 | - break; |
|
1589 | + $options = [$relation['col'], $this->pk()]; |
|
1590 | + break; |
|
1591 | 1591 | default: |
1592 | 1592 | if ($this->$relation['col'] === NULL) { |
1593 | 1593 | return null; |
1594 | - } |
|
1595 | - $getType = 'get'; |
|
1596 | - $options = $this->$relation['col']; |
|
1597 | - $getParams['appType'] = $this->appType; |
|
1598 | - } |
|
1599 | - if (!empty($params['count'])) { |
|
1594 | + } |
|
1595 | + $getType = 'get'; |
|
1596 | + $options = $this->$relation['col']; |
|
1597 | + $getParams['appType'] = $this->appType; |
|
1598 | + } |
|
1599 | + if (!empty($params['count'])) { |
|
1600 | 1600 | if (class_exists($relation['model'])) { |
1601 | - return $relation['model']::getCount($options); |
|
1601 | + return $relation['model']::getCount($options); |
|
1602 | 1602 | } |
1603 | 1603 | return 0; |
1604 | - } else { |
|
1604 | + } else { |
|
1605 | 1605 | if (class_exists($relation['model'])) { |
1606 | - $this->loadedRelations[$name][json_encode($params)] = $relation['model']::$getType($options, $getCol, $getParams); |
|
1606 | + $this->loadedRelations[$name][json_encode($params)] = $relation['model']::$getType($options, $getCol, $getParams); |
|
1607 | 1607 | } else { |
1608 | - $this->loadedRelations[$name][json_encode($params)] = []; |
|
1608 | + $this->loadedRelations[$name][json_encode($params)] = []; |
|
1609 | 1609 | } |
1610 | - } |
|
1611 | - return $this->loadedRelations[$name][json_encode($params)]; |
|
1610 | + } |
|
1611 | + return $this->loadedRelations[$name][json_encode($params)]; |
|
1612 | 1612 | } |
1613 | 1613 | return NULL; |
1614 | - } |
|
1615 | - |
|
1616 | - /** |
|
1617 | - * Add relation item |
|
1618 | - * |
|
1619 | - * @param string $relName |
|
1620 | - * @param \Model $objectId |
|
1621 | - * @return \Model|boolean |
|
1622 | - */ |
|
1623 | - public function addRelation($relName, $objectId) { |
|
1614 | + } |
|
1615 | + |
|
1616 | + /** |
|
1617 | + * Add relation item |
|
1618 | + * |
|
1619 | + * @param string $relName |
|
1620 | + * @param \Model $objectId |
|
1621 | + * @return \Model|boolean |
|
1622 | + */ |
|
1623 | + public function addRelation($relName, $objectId) { |
|
1624 | 1624 | $relation = $this->getRelation($relName); |
1625 | 1625 | if ($relation) { |
1626 | - $rel = $relation['relModel']::get([[$relation['model']::index(), $objectId], [$this->index(), $this->pk()]]); |
|
1627 | - if (!$rel) { |
|
1626 | + $rel = $relation['relModel']::get([[$relation['model']::index(), $objectId], [$this->index(), $this->pk()]]); |
|
1627 | + if (!$rel) { |
|
1628 | 1628 | $rel = new $relation['relModel']([ |
1629 | 1629 | $relation['model']::index() => $objectId, |
1630 | 1630 | $this->index() => $this->pk() |
1631 | 1631 | ]); |
1632 | 1632 | $rel->save(); |
1633 | - } |
|
1634 | - return $rel; |
|
1633 | + } |
|
1634 | + return $rel; |
|
1635 | 1635 | } |
1636 | 1636 | return false; |
1637 | - } |
|
1638 | - |
|
1639 | - /** |
|
1640 | - * Check user access for form |
|
1641 | - * |
|
1642 | - * @param string $formName |
|
1643 | - * @return boolean |
|
1644 | - */ |
|
1645 | - public function checkFormAccess($formName) { |
|
1637 | + } |
|
1638 | + |
|
1639 | + /** |
|
1640 | + * Check user access for form |
|
1641 | + * |
|
1642 | + * @param string $formName |
|
1643 | + * @return boolean |
|
1644 | + */ |
|
1645 | + public function checkFormAccess($formName) { |
|
1646 | 1646 | if ($formName == 'manage' && !Users\User::$cur->isAdmin()) { |
1647 | - return false; |
|
1647 | + return false; |
|
1648 | 1648 | } |
1649 | 1649 | return true; |
1650 | - } |
|
1651 | - |
|
1652 | - /** |
|
1653 | - * Check access for model |
|
1654 | - * |
|
1655 | - * @param string $mode |
|
1656 | - * @param \Users\User $user |
|
1657 | - * @return boolean |
|
1658 | - */ |
|
1659 | - public function checkAccess($mode = 'write', $user = null) { |
|
1650 | + } |
|
1651 | + |
|
1652 | + /** |
|
1653 | + * Check access for model |
|
1654 | + * |
|
1655 | + * @param string $mode |
|
1656 | + * @param \Users\User $user |
|
1657 | + * @return boolean |
|
1658 | + */ |
|
1659 | + public function checkAccess($mode = 'write', $user = null) { |
|
1660 | 1660 | if (!$user) { |
1661 | - $user = \Users\User::$cur; |
|
1661 | + $user = \Users\User::$cur; |
|
1662 | 1662 | } |
1663 | 1663 | return $user->isAdmin(); |
1664 | - } |
|
1665 | - |
|
1666 | - /** |
|
1667 | - * Param and relation with params getter |
|
1668 | - * |
|
1669 | - * @param string $name |
|
1670 | - * @param array $params |
|
1671 | - * @return \Value|mixed |
|
1672 | - */ |
|
1673 | - public function __call($name, $params) { |
|
1664 | + } |
|
1665 | + |
|
1666 | + /** |
|
1667 | + * Param and relation with params getter |
|
1668 | + * |
|
1669 | + * @param string $name |
|
1670 | + * @param array $params |
|
1671 | + * @return \Value|mixed |
|
1672 | + */ |
|
1673 | + public function __call($name, $params) { |
|
1674 | 1674 | $fixedName = $name; |
1675 | 1675 | static::fixPrefix($fixedName); |
1676 | 1676 | if (isset($this->_params[$fixedName])) { |
1677 | - return new Value($this, $fixedName); |
|
1677 | + return new Value($this, $fixedName); |
|
1678 | 1678 | } elseif (isset($this->_params[$name])) { |
1679 | - return new Value($this, $name); |
|
1679 | + return new Value($this, $name); |
|
1680 | 1680 | } |
1681 | 1681 | return call_user_func_array([$this, 'loadRelation'], array_merge([$name], $params)); |
1682 | - } |
|
1683 | - |
|
1684 | - /** |
|
1685 | - * Param and relation getter |
|
1686 | - * |
|
1687 | - * @param string $name |
|
1688 | - * @return mixed |
|
1689 | - */ |
|
1690 | - public function __get($name) { |
|
1682 | + } |
|
1683 | + |
|
1684 | + /** |
|
1685 | + * Param and relation getter |
|
1686 | + * |
|
1687 | + * @param string $name |
|
1688 | + * @return mixed |
|
1689 | + */ |
|
1690 | + public function __get($name) { |
|
1691 | 1691 | $fixedName = $name; |
1692 | 1692 | static::fixPrefix($fixedName); |
1693 | 1693 | if (isset($this->_params[$fixedName])) { |
1694 | - return $this->_params[$fixedName]; |
|
1694 | + return $this->_params[$fixedName]; |
|
1695 | 1695 | } |
1696 | 1696 | if (isset($this->loadedRelations[$name][json_encode([])])) { |
1697 | - return $this->loadedRelations[$name][json_encode([])]; |
|
1697 | + return $this->loadedRelations[$name][json_encode([])]; |
|
1698 | 1698 | } |
1699 | 1699 | return $this->loadRelation($name); |
1700 | - } |
|
1701 | - |
|
1702 | - /** |
|
1703 | - * Return model value in object |
|
1704 | - * |
|
1705 | - * @param string $name |
|
1706 | - * @return \Value|null |
|
1707 | - */ |
|
1708 | - public function value($name) { |
|
1700 | + } |
|
1701 | + |
|
1702 | + /** |
|
1703 | + * Return model value in object |
|
1704 | + * |
|
1705 | + * @param string $name |
|
1706 | + * @return \Value|null |
|
1707 | + */ |
|
1708 | + public function value($name) { |
|
1709 | 1709 | $fixedName = $name; |
1710 | 1710 | static::fixPrefix($fixedName); |
1711 | 1711 | if (isset($this->_params[$fixedName])) { |
1712 | - return new Value($this, $fixedName); |
|
1712 | + return new Value($this, $fixedName); |
|
1713 | 1713 | } elseif ($this->_params[$name]) { |
1714 | - return new Value($this, $name); |
|
1714 | + return new Value($this, $name); |
|
1715 | 1715 | } |
1716 | 1716 | return null; |
1717 | - } |
|
1718 | - |
|
1719 | - /** |
|
1720 | - * Return manager filters |
|
1721 | - * |
|
1722 | - * @return array |
|
1723 | - */ |
|
1724 | - public static function managerFilters() { |
|
1717 | + } |
|
1718 | + |
|
1719 | + /** |
|
1720 | + * Return manager filters |
|
1721 | + * |
|
1722 | + * @return array |
|
1723 | + */ |
|
1724 | + public static function managerFilters() { |
|
1725 | 1725 | return []; |
1726 | - } |
|
1727 | - |
|
1728 | - /** |
|
1729 | - * Return validators for cols |
|
1730 | - * |
|
1731 | - * @return array |
|
1732 | - */ |
|
1733 | - public static function validators() { |
|
1726 | + } |
|
1727 | + |
|
1728 | + /** |
|
1729 | + * Return validators for cols |
|
1730 | + * |
|
1731 | + * @return array |
|
1732 | + */ |
|
1733 | + public static function validators() { |
|
1734 | 1734 | return []; |
1735 | - } |
|
1736 | - |
|
1737 | - /** |
|
1738 | - * Return validator by name |
|
1739 | - * |
|
1740 | - * @param string $name |
|
1741 | - * @return array |
|
1742 | - */ |
|
1743 | - public static function validator($name) { |
|
1735 | + } |
|
1736 | + |
|
1737 | + /** |
|
1738 | + * Return validator by name |
|
1739 | + * |
|
1740 | + * @param string $name |
|
1741 | + * @return array |
|
1742 | + */ |
|
1743 | + public static function validator($name) { |
|
1744 | 1744 | $validators = static::validators(); |
1745 | 1745 | if (!empty($validators[$name])) { |
1746 | - return $validators[$name]; |
|
1746 | + return $validators[$name]; |
|
1747 | 1747 | } |
1748 | 1748 | return []; |
1749 | - } |
|
1750 | - |
|
1751 | - /** |
|
1752 | - * Set handler for model params |
|
1753 | - * |
|
1754 | - * @param string $name |
|
1755 | - * @param mixed $value |
|
1756 | - */ |
|
1757 | - public function __set($name, $value) { |
|
1749 | + } |
|
1750 | + |
|
1751 | + /** |
|
1752 | + * Set handler for model params |
|
1753 | + * |
|
1754 | + * @param string $name |
|
1755 | + * @param mixed $value |
|
1756 | + */ |
|
1757 | + public function __set($name, $value) { |
|
1758 | 1758 | static::fixPrefix($name); |
1759 | 1759 | $className = get_called_class(); |
1760 | 1760 | $shortName = preg_replace('!' . $this->colPrefix() . '!', '', $name); |
1761 | 1761 | if (!empty($className::$cols[$shortName])) { |
1762 | - switch ($className::$cols[$shortName]['type']) { |
|
1762 | + switch ($className::$cols[$shortName]['type']) { |
|
1763 | 1763 | case 'decimal': |
1764 | 1764 | $value = (float) $value; |
1765 | - break; |
|
1765 | + break; |
|
1766 | 1766 | case 'number': |
1767 | 1767 | $value = (int) $value; |
1768 | - break; |
|
1768 | + break; |
|
1769 | 1769 | case 'bool': |
1770 | 1770 | $value = (bool) $value; |
1771 | - break; |
|
1772 | - } |
|
1771 | + break; |
|
1772 | + } |
|
1773 | 1773 | } |
1774 | 1774 | if ((isset($this->_params[$name]) && $this->_params[$name] != $value) && !isset($this->_changedParams[$name])) { |
1775 | - $this->_changedParams[$name] = $this->_params[$name]; |
|
1775 | + $this->_changedParams[$name] = $this->_params[$name]; |
|
1776 | 1776 | } |
1777 | 1777 | |
1778 | 1778 | $this->_params[$name] = $value; |
1779 | - } |
|
1780 | - |
|
1781 | - /** |
|
1782 | - * Isset handler for model params |
|
1783 | - * |
|
1784 | - * @param string $name |
|
1785 | - * @return boolean |
|
1786 | - */ |
|
1787 | - public function __isset($name) { |
|
1779 | + } |
|
1780 | + |
|
1781 | + /** |
|
1782 | + * Isset handler for model params |
|
1783 | + * |
|
1784 | + * @param string $name |
|
1785 | + * @return boolean |
|
1786 | + */ |
|
1787 | + public function __isset($name) { |
|
1788 | 1788 | static::fixPrefix($name); |
1789 | 1789 | return isset($this->_params[$name]); |
1790 | - } |
|
1791 | - |
|
1792 | - /** |
|
1793 | - * Convert object to string |
|
1794 | - * |
|
1795 | - * @return string |
|
1796 | - */ |
|
1797 | - public function __toString() { |
|
1790 | + } |
|
1791 | + |
|
1792 | + /** |
|
1793 | + * Convert object to string |
|
1794 | + * |
|
1795 | + * @return string |
|
1796 | + */ |
|
1797 | + public function __toString() { |
|
1798 | 1798 | return $this->name(); |
1799 | - } |
|
1799 | + } |
|
1800 | 1800 | |
1801 | 1801 | } |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | $relModel = $relations[$colInfo['colParams']['relation']]['model']; |
208 | 208 | $relModel = strpos($relModel, '\\') === 0 ? substr($relModel, 1) : $relModel; |
209 | 209 | if ($manageHref) { |
210 | - $value = $relValue ? "<a href='/admin/" . str_replace('\\', '/view/', $relModel) . "/" . $relValue->pk() . "'>" . $relValue->name() . "</a>" : 'Не задано'; |
|
210 | + $value = $relValue ? "<a href='/admin/".str_replace('\\', '/view/', $relModel)."/".$relValue->pk()."'>".$relValue->name()."</a>" : 'Не задано'; |
|
211 | 211 | } else { |
212 | 212 | $value = $relValue ? $relValue->name() : 'Не задано'; |
213 | 213 | } |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | case 'image': |
218 | 218 | $file = Files\File::get($item->$colName); |
219 | 219 | if ($file) { |
220 | - $value = '<img src="' . $file->path . '?resize=60x120" />'; |
|
220 | + $value = '<img src="'.$file->path.'?resize=60x120" />'; |
|
221 | 221 | } else { |
222 | 222 | $value = '<img src="/static/system/images/no-image.png?resize=60x120" />'; |
223 | 223 | } |
@@ -286,12 +286,12 @@ discard block |
||
286 | 286 | return; |
287 | 287 | } |
288 | 288 | if (!is_array($array)) { |
289 | - if (!isset($cols[static::colPrefix() . $array]) && isset(static::$cols[$array])) { |
|
289 | + if (!isset($cols[static::colPrefix().$array]) && isset(static::$cols[$array])) { |
|
290 | 290 | static::createCol($array); |
291 | 291 | $cols = static::cols(true); |
292 | 292 | } |
293 | - if (!isset($cols[$array]) && isset($cols[static::colPrefix() . $array])) { |
|
294 | - $array = static::colPrefix() . $array; |
|
293 | + if (!isset($cols[$array]) && isset($cols[static::colPrefix().$array])) { |
|
294 | + $array = static::colPrefix().$array; |
|
295 | 295 | } else { |
296 | 296 | static::checkForJoin($array, $rootModel); |
297 | 297 | } |
@@ -300,14 +300,14 @@ discard block |
||
300 | 300 | switch ($searchtype) { |
301 | 301 | case 'key': |
302 | 302 | foreach ($array as $key => $item) { |
303 | - if (!isset($cols[static::colPrefix() . $key]) && isset(static::$cols[$key])) { |
|
303 | + if (!isset($cols[static::colPrefix().$key]) && isset(static::$cols[$key])) { |
|
304 | 304 | static::createCol($key); |
305 | 305 | $cols = static::cols(true); |
306 | 306 | } |
307 | - if (!isset($cols[$key]) && isset($cols[static::colPrefix() . $key])) { |
|
308 | - $array[static::colPrefix() . $key] = $item; |
|
307 | + if (!isset($cols[$key]) && isset($cols[static::colPrefix().$key])) { |
|
308 | + $array[static::colPrefix().$key] = $item; |
|
309 | 309 | unset($array[$key]); |
310 | - $key = static::colPrefix() . $key; |
|
310 | + $key = static::colPrefix().$key; |
|
311 | 311 | } |
312 | 312 | if (is_array($array[$key])) { |
313 | 313 | static::fixPrefix($array[$key], 'key', $rootModel); |
@@ -318,12 +318,12 @@ discard block |
||
318 | 318 | break; |
319 | 319 | case 'first': |
320 | 320 | if (isset($array[0]) && is_string($array[0])) { |
321 | - if (!isset($cols[static::colPrefix() . $array[0]]) && isset(static::$cols[$array[0]])) { |
|
321 | + if (!isset($cols[static::colPrefix().$array[0]]) && isset(static::$cols[$array[0]])) { |
|
322 | 322 | static::createCol($array[0]); |
323 | 323 | $cols = static::cols(true); |
324 | 324 | } |
325 | - if (!isset($cols[$array[0]]) && isset($cols[static::colPrefix() . $array[0]])) { |
|
326 | - $array[0] = static::colPrefix() . $array[0]; |
|
325 | + if (!isset($cols[$array[0]]) && isset($cols[static::colPrefix().$array[0]])) { |
|
326 | + $array[0] = static::colPrefix().$array[0]; |
|
327 | 327 | } else { |
328 | 328 | static::checkForJoin($array[0], $rootModel); |
329 | 329 | } |
@@ -355,13 +355,13 @@ discard block |
||
355 | 355 | case 'to': |
356 | 356 | $relCol = $relations[$rel]['col']; |
357 | 357 | static::fixPrefix($relCol); |
358 | - $rootModel::$relJoins[$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol]; |
|
358 | + $rootModel::$relJoins[$relations[$rel]['model'].'_'.$rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index().' = '.$relCol]; |
|
359 | 359 | break; |
360 | 360 | case 'one': |
361 | 361 | case 'many': |
362 | 362 | $relCol = $relations[$rel]['col']; |
363 | 363 | $relations[$rel]['model']::fixPrefix($relCol); |
364 | - $rootModel::$relJoins[$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), static::index() . ' = ' . $relCol]; |
|
364 | + $rootModel::$relJoins[$relations[$rel]['model'].'_'.$rel] = [$relations[$rel]['model']::table(), static::index().' = '.$relCol]; |
|
365 | 365 | break; |
366 | 366 | } |
367 | 367 | $relations[$rel]['model']::fixPrefix($col, 'key', $rootModel); |
@@ -400,13 +400,13 @@ discard block |
||
400 | 400 | $relCol = $relations[$rel]['col']; |
401 | 401 | static::fixPrefix($relCol); |
402 | 402 | //$info['modelName'] = $relations[$rel]['model']; |
403 | - $info['joins'][$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol]; |
|
403 | + $info['joins'][$relations[$rel]['model'].'_'.$rel] = [$relations[$rel]['model']::table(), $relations[$rel]['model']::index().' = '.$relCol]; |
|
404 | 404 | break; |
405 | 405 | case 'one': |
406 | 406 | $relCol = $relations[$rel]['col']; |
407 | 407 | $relations[$rel]['model']::fixPrefix($relCol); |
408 | 408 | //$info['modelName'] = $relations[$rel]['model']; |
409 | - $info['joins'][$relations[$rel]['model'] . '_' . $rel] = [$relations[$rel]['model']::table(), static::index() . ' = ' . $relCol]; |
|
409 | + $info['joins'][$relations[$rel]['model'].'_'.$rel] = [$relations[$rel]['model']::table(), static::index().' = '.$relCol]; |
|
410 | 410 | break; |
411 | 411 | } |
412 | 412 | $info = $relations[$rel]['model']::parseColRecursion($info); |
@@ -424,8 +424,8 @@ discard block |
||
424 | 424 | } else { |
425 | 425 | $info['colParams'] = []; |
426 | 426 | } |
427 | - if (!isset($cols[$info['col']]) && isset($cols[static::colPrefix() . $info['col']])) { |
|
428 | - $info['col'] = static::colPrefix() . $info['col']; |
|
427 | + if (!isset($cols[$info['col']]) && isset($cols[static::colPrefix().$info['col']])) { |
|
428 | + $info['col'] = static::colPrefix().$info['col']; |
|
429 | 429 | } |
430 | 430 | $info['modelName'] = get_called_class(); |
431 | 431 | } |
@@ -528,7 +528,7 @@ discard block |
||
528 | 528 | if ($params === false) { |
529 | 529 | return false; |
530 | 530 | } |
531 | - return App::$cur->db->addCol(static::table(), static::colPrefix() . $colName, $params); |
|
531 | + return App::$cur->db->addCol(static::table(), static::colPrefix().$colName, $params); |
|
532 | 532 | } |
533 | 533 | |
534 | 534 | public static function createTable() { |
@@ -557,28 +557,28 @@ discard block |
||
557 | 557 | return true; |
558 | 558 | } |
559 | 559 | $cols = [ |
560 | - $colPrefix . 'id' => 'pk' |
|
560 | + $colPrefix.'id' => 'pk' |
|
561 | 561 | ]; |
562 | 562 | $className = get_called_class(); |
563 | 563 | if (!empty($className::$cols)) { |
564 | 564 | foreach ($className::$cols as $colName => $colParams) { |
565 | 565 | if ($colName == 'date_create') { |
566 | - $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
566 | + $cols[$colPrefix.'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
567 | 567 | continue; |
568 | 568 | } |
569 | 569 | $params = $className::genColParams($colName); |
570 | 570 | if ($params) { |
571 | - $cols[$colPrefix . $colName] = $params; |
|
571 | + $cols[$colPrefix.$colName] = $params; |
|
572 | 572 | } |
573 | 573 | } |
574 | 574 | } |
575 | - if (empty($cols[$colPrefix . 'date_create'])) { |
|
576 | - $cols[$colPrefix . 'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
575 | + if (empty($cols[$colPrefix.'date_create'])) { |
|
576 | + $cols[$colPrefix.'date_create'] = 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP'; |
|
577 | 577 | } |
578 | 578 | $tableIndexes = []; |
579 | 579 | if ($indexes) { |
580 | 580 | foreach ($indexes as $indexName => $index) { |
581 | - $tableIndexes[] = $index['type'] . ' ' . App::$cur->db->table_prefix . $indexName . ' (' . implode(',', $index['cols']) . ')'; |
|
581 | + $tableIndexes[] = $index['type'].' '.App::$cur->db->table_prefix.$indexName.' ('.implode(',', $index['cols']).')'; |
|
582 | 582 | } |
583 | 583 | } |
584 | 584 | |
@@ -602,7 +602,7 @@ discard block |
||
602 | 602 | */ |
603 | 603 | public static function index() { |
604 | 604 | |
605 | - return static::colPrefix() . 'id'; |
|
605 | + return static::colPrefix().'id'; |
|
606 | 606 | } |
607 | 607 | |
608 | 608 | /** |
@@ -613,7 +613,7 @@ discard block |
||
613 | 613 | public static function colPrefix() { |
614 | 614 | $classPath = explode('\\', get_called_class()); |
615 | 615 | $classPath = array_slice($classPath, 1); |
616 | - return strtolower(implode('_', $classPath)) . '_'; |
|
616 | + return strtolower(implode('_', $classPath)).'_'; |
|
617 | 617 | } |
618 | 618 | |
619 | 619 | /** |
@@ -674,12 +674,12 @@ discard block |
||
674 | 674 | case 'to': |
675 | 675 | $relCol = $relations[$rel]['col']; |
676 | 676 | static::fixPrefix($relCol); |
677 | - App::$cur->db->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
|
677 | + App::$cur->db->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index().' = '.$relCol); |
|
678 | 678 | break; |
679 | 679 | case 'one': |
680 | 680 | $col = $relations[$rel]['col']; |
681 | 681 | $relations[$rel]['model']::fixPrefix($col); |
682 | - App::$cur->db->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
|
682 | + App::$cur->db->join($relations[$rel]['model']::table(), static::index().' = '.$col); |
|
683 | 683 | break; |
684 | 684 | } |
685 | 685 | } |
@@ -694,8 +694,8 @@ discard block |
||
694 | 694 | } |
695 | 695 | if ($param !== null) { |
696 | 696 | $cols = static::cols(); |
697 | - if (!isset($cols[$col]) && isset($cols[static::colPrefix() . $col])) { |
|
698 | - $col = static::colPrefix() . $col; |
|
697 | + if (!isset($cols[$col]) && isset($cols[static::colPrefix().$col])) { |
|
698 | + $col = static::colPrefix().$col; |
|
699 | 699 | } |
700 | 700 | App::$cur->db->where($col, $param); |
701 | 701 | } else { |
@@ -759,12 +759,12 @@ discard block |
||
759 | 759 | case 'to': |
760 | 760 | $relCol = $relations[$rel]['col']; |
761 | 761 | static::fixPrefix($relCol); |
762 | - $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
|
762 | + $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index().' = '.$relCol); |
|
763 | 763 | break; |
764 | 764 | case 'one': |
765 | 765 | $col = $relations[$rel]['col']; |
766 | 766 | $relations[$rel]['model']::fixPrefix($col); |
767 | - $query->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
|
767 | + $query->join($relations[$rel]['model']::table(), static::index().' = '.$col); |
|
768 | 768 | break; |
769 | 769 | } |
770 | 770 | } |
@@ -1078,12 +1078,12 @@ discard block |
||
1078 | 1078 | case 'to': |
1079 | 1079 | $relCol = $relations[$rel]['col']; |
1080 | 1080 | static::fixPrefix($relCol); |
1081 | - $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index() . ' = ' . $relCol); |
|
1081 | + $query->join($relations[$rel]['model']::table(), $relations[$rel]['model']::index().' = '.$relCol); |
|
1082 | 1082 | break; |
1083 | 1083 | case 'one': |
1084 | 1084 | $col = $relations[$rel]['col']; |
1085 | 1085 | $relations[$rel]['model']::fixPrefix($col); |
1086 | - $query->join($relations[$rel]['model']::table(), static::index() . ' = ' . $col); |
|
1086 | + $query->join($relations[$rel]['model']::table(), static::index().' = '.$col); |
|
1087 | 1087 | break; |
1088 | 1088 | } |
1089 | 1089 | } |
@@ -1100,7 +1100,7 @@ discard block |
||
1100 | 1100 | } else { |
1101 | 1101 | $cols .= '*'; |
1102 | 1102 | } |
1103 | - $cols .=') as `count`' . (!empty($options['cols']) ? ',' . $options['cols'] : ''); |
|
1103 | + $cols .= ') as `count`'.(!empty($options['cols']) ? ','.$options['cols'] : ''); |
|
1104 | 1104 | $query->cols = $cols; |
1105 | 1105 | if (!empty($options['group'])) { |
1106 | 1106 | $query->group($options['group']); |
@@ -1233,25 +1233,25 @@ discard block |
||
1233 | 1233 | $itemModel = $class::$treeCategory; |
1234 | 1234 | $oldPath = $this->tree_path; |
1235 | 1235 | $this->tree_path = $this->getCatalogTree($this); |
1236 | - $itemsTable = \App::$cur->db->table_prefix . $itemModel::table(); |
|
1237 | - $itemTreeCol = $itemModel::colPrefix() . 'tree_path'; |
|
1236 | + $itemsTable = \App::$cur->db->table_prefix.$itemModel::table(); |
|
1237 | + $itemTreeCol = $itemModel::colPrefix().'tree_path'; |
|
1238 | 1238 | |
1239 | - $categoryTreeCol = $this->colPrefix() . 'tree_path'; |
|
1240 | - $categoryTable = \App::$cur->db->table_prefix . $this->table(); |
|
1239 | + $categoryTreeCol = $this->colPrefix().'tree_path'; |
|
1240 | + $categoryTable = \App::$cur->db->table_prefix.$this->table(); |
|
1241 | 1241 | if ($oldPath) { |
1242 | 1242 | \App::$cur->db->query('UPDATE |
1243 | - ' . $categoryTable . ' |
|
1243 | + ' . $categoryTable.' |
|
1244 | 1244 | SET |
1245 | - ' . $categoryTreeCol . ' = REPLACE(' . $categoryTreeCol . ', "' . $oldPath . $this->id . '/' . '", "' . $this->tree_path . $this->id . '/' . '") |
|
1246 | - WHERE ' . $categoryTreeCol . ' LIKE "' . $oldPath . $this->id . '/' . '%"'); |
|
1245 | + ' . $categoryTreeCol.' = REPLACE('.$categoryTreeCol.', "'.$oldPath.$this->id.'/'.'", "'.$this->tree_path.$this->id.'/'.'") |
|
1246 | + WHERE ' . $categoryTreeCol.' LIKE "'.$oldPath.$this->id.'/'.'%"'); |
|
1247 | 1247 | |
1248 | 1248 | \App::$cur->db->query('UPDATE |
1249 | - ' . $itemsTable . ' |
|
1249 | + ' . $itemsTable.' |
|
1250 | 1250 | SET |
1251 | - ' . $itemTreeCol . ' = REPLACE(' . $itemTreeCol . ', "' . $oldPath . $this->id . '/' . '", "' . $this->tree_path . $this->id . '/' . '") |
|
1252 | - WHERE ' . $itemTreeCol . ' LIKE "' . $oldPath . $this->id . '/' . '%"'); |
|
1251 | + ' . $itemTreeCol.' = REPLACE('.$itemTreeCol.', "'.$oldPath.$this->id.'/'.'", "'.$this->tree_path.$this->id.'/'.'") |
|
1252 | + WHERE ' . $itemTreeCol.' LIKE "'.$oldPath.$this->id.'/'.'%"'); |
|
1253 | 1253 | } |
1254 | - $itemModel::update([$itemTreeCol => $this->tree_path . $this->id . '/'], [$itemModel::colPrefix() . $this->index(), $this->id]); |
|
1254 | + $itemModel::update([$itemTreeCol => $this->tree_path.$this->id.'/'], [$itemModel::colPrefix().$this->index(), $this->id]); |
|
1255 | 1255 | } |
1256 | 1256 | |
1257 | 1257 | /** |
@@ -1265,9 +1265,9 @@ discard block |
||
1265 | 1265 | $catalogParent = $catalogClass::get($catalog->parent_id); |
1266 | 1266 | if ($catalog && $catalogParent) { |
1267 | 1267 | if ($catalogParent->tree_path) { |
1268 | - return $catalogParent->tree_path . $catalogParent->id . '/'; |
|
1268 | + return $catalogParent->tree_path.$catalogParent->id.'/'; |
|
1269 | 1269 | } else { |
1270 | - return $this->getCatalogTree($catalogParent) . $catalogParent->id . '/'; |
|
1270 | + return $this->getCatalogTree($catalogParent).$catalogParent->id.'/'; |
|
1271 | 1271 | } |
1272 | 1272 | } |
1273 | 1273 | return '/'; |
@@ -1281,7 +1281,7 @@ discard block |
||
1281 | 1281 | $categoryModel = $class::$categoryModel; |
1282 | 1282 | $category = $categoryModel::get($this->{$categoryModel::index()}); |
1283 | 1283 | if ($category) { |
1284 | - $this->tree_path = $category->tree_path . $category->pk() . '/'; |
|
1284 | + $this->tree_path = $category->tree_path.$category->pk().'/'; |
|
1285 | 1285 | } else { |
1286 | 1286 | $this->tree_path = '/'; |
1287 | 1287 | } |
@@ -1306,7 +1306,7 @@ discard block |
||
1306 | 1306 | $this->changeCategoryTree(); |
1307 | 1307 | } |
1308 | 1308 | if (!empty($this->_changedParams) && $this->pk()) { |
1309 | - Inji::$inst->event('modelItemParamsChanged-' . get_called_class(), $this); |
|
1309 | + Inji::$inst->event('modelItemParamsChanged-'.get_called_class(), $this); |
|
1310 | 1310 | } |
1311 | 1311 | $this->beforeSave(); |
1312 | 1312 | |
@@ -1351,7 +1351,7 @@ discard block |
||
1351 | 1351 | } |
1352 | 1352 | $this->_params = $result->fetch(); |
1353 | 1353 | if ($new) { |
1354 | - Inji::$inst->event('modelCreatedItem-' . get_called_class(), $this); |
|
1354 | + Inji::$inst->event('modelCreatedItem-'.get_called_class(), $this); |
|
1355 | 1355 | } |
1356 | 1356 | $this->afterSave(); |
1357 | 1357 | return $this->{$this->index()}; |
@@ -1489,7 +1489,7 @@ discard block |
||
1489 | 1489 | static::fixPrefix($params); |
1490 | 1490 | $className = get_called_class(); |
1491 | 1491 | foreach ($params as $paramName => $value) { |
1492 | - $shortName = preg_replace('!' . $this->colPrefix() . '!', '', $paramName); |
|
1492 | + $shortName = preg_replace('!'.$this->colPrefix().'!', '', $paramName); |
|
1493 | 1493 | if (!empty($className::$cols[$shortName])) { |
1494 | 1494 | switch ($className::$cols[$shortName]['type']) { |
1495 | 1495 | case 'decimal': |
@@ -1757,7 +1757,7 @@ discard block |
||
1757 | 1757 | public function __set($name, $value) { |
1758 | 1758 | static::fixPrefix($name); |
1759 | 1759 | $className = get_called_class(); |
1760 | - $shortName = preg_replace('!' . $this->colPrefix() . '!', '', $name); |
|
1760 | + $shortName = preg_replace('!'.$this->colPrefix().'!', '', $name); |
|
1761 | 1761 | if (!empty($className::$cols[$shortName])) { |
1762 | 1762 | switch ($className::$cols[$shortName]['type']) { |
1763 | 1763 | case 'decimal': |
@@ -730,22 +730,27 @@ discard block |
||
730 | 730 | if (!$query) { |
731 | 731 | return []; |
732 | 732 | } |
733 | - if (!empty($options['where'])) |
|
734 | - $query->where($options['where']); |
|
735 | - if (!empty($options['cols'])) |
|
736 | - $query->cols = $options['cols']; |
|
733 | + if (!empty($options['where'])) { |
|
734 | + $query->where($options['where']); |
|
735 | + } |
|
736 | + if (!empty($options['cols'])) { |
|
737 | + $query->cols = $options['cols']; |
|
738 | + } |
|
737 | 739 | if (!empty($options['group'])) { |
738 | 740 | $query->group($options['group']); |
739 | 741 | } |
740 | 742 | if (!empty($options['having'])) { |
741 | 743 | $query->having($options['having']); |
742 | 744 | } |
743 | - if (!empty($options['order'])) |
|
744 | - $query->order($options['order']); |
|
745 | - if (!empty($options['join'])) |
|
746 | - $query->join($options['join']); |
|
747 | - if (!empty($options['distinct'])) |
|
748 | - $query->distinct = $options['distinct']; |
|
745 | + if (!empty($options['order'])) { |
|
746 | + $query->order($options['order']); |
|
747 | + } |
|
748 | + if (!empty($options['join'])) { |
|
749 | + $query->join($options['join']); |
|
750 | + } |
|
751 | + if (!empty($options['distinct'])) { |
|
752 | + $query->distinct = $options['distinct']; |
|
753 | + } |
|
749 | 754 | |
750 | 755 | foreach (static::$relJoins as $join) { |
751 | 756 | $query->join($join[0], $join[1]); |
@@ -771,14 +776,14 @@ discard block |
||
771 | 776 | } |
772 | 777 | static::$needJoin = []; |
773 | 778 | |
774 | - if (!empty($options['limit'])) |
|
775 | - $limit = (int) $options['limit']; |
|
776 | - else { |
|
779 | + if (!empty($options['limit'])) { |
|
780 | + $limit = (int) $options['limit']; |
|
781 | + } else { |
|
777 | 782 | $limit = 0; |
778 | 783 | } |
779 | - if (!empty($options['start'])) |
|
780 | - $start = (int) $options['start']; |
|
781 | - else { |
|
784 | + if (!empty($options['start'])) { |
|
785 | + $start = (int) $options['start']; |
|
786 | + } else { |
|
782 | 787 | $start = 0; |
783 | 788 | } |
784 | 789 | if ($limit || $start) { |
@@ -1045,21 +1050,23 @@ discard block |
||
1045 | 1050 | if (!empty($options['group'])) { |
1046 | 1051 | static::fixPrefix($options['group'], 'first'); |
1047 | 1052 | } |
1048 | - if (!empty($options['where'])) |
|
1049 | - $query->where($options['where']); |
|
1050 | - if (!empty($options['join'])) |
|
1051 | - $query->join($options['join']); |
|
1053 | + if (!empty($options['where'])) { |
|
1054 | + $query->where($options['where']); |
|
1055 | + } |
|
1056 | + if (!empty($options['join'])) { |
|
1057 | + $query->join($options['join']); |
|
1058 | + } |
|
1052 | 1059 | if (!empty($options['order'])) { |
1053 | 1060 | $query->order($options['order']); |
1054 | 1061 | } |
1055 | - if (!empty($options['limit'])) |
|
1056 | - $limit = (int) $options['limit']; |
|
1057 | - else { |
|
1062 | + if (!empty($options['limit'])) { |
|
1063 | + $limit = (int) $options['limit']; |
|
1064 | + } else { |
|
1058 | 1065 | $limit = 0; |
1059 | 1066 | } |
1060 | - if (!empty($options['start'])) |
|
1061 | - $start = (int) $options['start']; |
|
1062 | - else { |
|
1067 | + if (!empty($options['start'])) { |
|
1068 | + $start = (int) $options['start']; |
|
1069 | + } else { |
|
1063 | 1070 | $start = 0; |
1064 | 1071 | } |
1065 | 1072 | if ($limit || $start) { |
@@ -1136,8 +1143,9 @@ discard block |
||
1136 | 1143 | |
1137 | 1144 | $values = []; |
1138 | 1145 | foreach ($cols as $col => $param) { |
1139 | - if (isset($params[$col])) |
|
1140 | - $values[$col] = $params[$col]; |
|
1146 | + if (isset($params[$col])) { |
|
1147 | + $values[$col] = $params[$col]; |
|
1148 | + } |
|
1141 | 1149 | } |
1142 | 1150 | if (empty($values)) { |
1143 | 1151 | return false; |
@@ -1474,8 +1482,9 @@ discard block |
||
1474 | 1482 | public static function findRelation($col) { |
1475 | 1483 | |
1476 | 1484 | foreach (static::relations() as $relName => $rel) { |
1477 | - if ($rel['col'] == $col) |
|
1478 | - return $relName; |
|
1485 | + if ($rel['col'] == $col) { |
|
1486 | + return $relName; |
|
1487 | + } |
|
1479 | 1488 | } |
1480 | 1489 | return NULL; |
1481 | 1490 | } |
@@ -31,6 +31,9 @@ discard block |
||
31 | 31 | public $params = []; |
32 | 32 | public $distinct = false; |
33 | 33 | |
34 | + /** |
|
35 | + * @param $instance |
|
36 | + */ |
|
34 | 37 | public function __construct($instance = null) { |
35 | 38 | if (!$instance) { |
36 | 39 | $this->curInstance = \App::$cur->db->connection; |
@@ -47,6 +50,9 @@ discard block |
||
47 | 50 | return $this->curInstance->pdo->lastInsertId(); |
48 | 51 | } |
49 | 52 | |
53 | + /** |
|
54 | + * @param string $table |
|
55 | + */ |
|
50 | 56 | public function select($table) { |
51 | 57 | $this->operation = 'SELECT'; |
52 | 58 | $this->table = $table; |
@@ -13,413 +13,413 @@ discard block |
||
13 | 13 | |
14 | 14 | class Query extends \Object { |
15 | 15 | |
16 | - public $curInstance = null; |
|
17 | - public $where = []; |
|
18 | - public $whereString = ''; |
|
19 | - public $having = []; |
|
20 | - public $havingString = ''; |
|
21 | - public $cols = []; |
|
22 | - public $order = NULL; |
|
23 | - public $join = []; |
|
24 | - public $group = []; |
|
25 | - public $limit = ''; |
|
26 | - public $error = ''; |
|
27 | - public $query = ''; |
|
28 | - public $table = ''; |
|
29 | - public $operation = ''; |
|
30 | - public $indexes = []; |
|
31 | - public $params = []; |
|
32 | - public $distinct = false; |
|
33 | - |
|
34 | - public function __construct($instance = null) { |
|
16 | + public $curInstance = null; |
|
17 | + public $where = []; |
|
18 | + public $whereString = ''; |
|
19 | + public $having = []; |
|
20 | + public $havingString = ''; |
|
21 | + public $cols = []; |
|
22 | + public $order = NULL; |
|
23 | + public $join = []; |
|
24 | + public $group = []; |
|
25 | + public $limit = ''; |
|
26 | + public $error = ''; |
|
27 | + public $query = ''; |
|
28 | + public $table = ''; |
|
29 | + public $operation = ''; |
|
30 | + public $indexes = []; |
|
31 | + public $params = []; |
|
32 | + public $distinct = false; |
|
33 | + |
|
34 | + public function __construct($instance = null) { |
|
35 | 35 | if (!$instance) { |
36 | - $this->curInstance = \App::$cur->db->connection; |
|
36 | + $this->curInstance = \App::$cur->db->connection; |
|
37 | 37 | } else { |
38 | - $this->curInstance = $instance; |
|
38 | + $this->curInstance = $instance; |
|
39 | + } |
|
39 | 40 | } |
40 | - } |
|
41 | 41 | |
42 | - public function insert($table, $data) { |
|
42 | + public function insert($table, $data) { |
|
43 | 43 | $this->operation = 'INSERT'; |
44 | 44 | $this->table = $table; |
45 | 45 | $this->cols = $data; |
46 | 46 | $this->query(); |
47 | 47 | return $this->curInstance->pdo->lastInsertId(); |
48 | - } |
|
48 | + } |
|
49 | 49 | |
50 | - public function select($table) { |
|
50 | + public function select($table) { |
|
51 | 51 | $this->operation = 'SELECT'; |
52 | 52 | $this->table = $table; |
53 | 53 | return $this->query(); |
54 | - } |
|
54 | + } |
|
55 | 55 | |
56 | - public function update($table, $data) { |
|
56 | + public function update($table, $data) { |
|
57 | 57 | $this->operation = 'UPDATE'; |
58 | 58 | $this->table = $table; |
59 | 59 | $this->cols = $data; |
60 | 60 | $result = $this->query(); |
61 | 61 | return $result->pdoResult->rowCount(); |
62 | - } |
|
62 | + } |
|
63 | 63 | |
64 | - public function delete($table) { |
|
64 | + public function delete($table) { |
|
65 | 65 | $this->operation = 'DELETE'; |
66 | 66 | $this->table = $table; |
67 | 67 | $result = $this->query(); |
68 | 68 | return $result->pdoResult->rowCount(); |
69 | - } |
|
69 | + } |
|
70 | 70 | |
71 | - public function createTable($table_name, $cols, $indexes = []) { |
|
71 | + public function createTable($table_name, $cols, $indexes = []) { |
|
72 | 72 | $this->operation = 'CREATE TABLE'; |
73 | 73 | $this->table = $table_name; |
74 | 74 | $this->cols = $cols; |
75 | 75 | $this->indexes = $indexes; |
76 | 76 | return $this->query(); |
77 | - } |
|
77 | + } |
|
78 | 78 | |
79 | - public function cols($cols) { |
|
79 | + public function cols($cols) { |
|
80 | 80 | if (is_array($cols)) { |
81 | - $this->cols = array_merge($this->cols, array_values($cols)); |
|
81 | + $this->cols = array_merge($this->cols, array_values($cols)); |
|
82 | 82 | } else { |
83 | - $this->cols[] = $cols; |
|
83 | + $this->cols[] = $cols; |
|
84 | + } |
|
84 | 85 | } |
85 | - } |
|
86 | 86 | |
87 | - public function join($table, $where = false, $type = 'LEFT', $alias = '') { |
|
87 | + public function join($table, $where = false, $type = 'LEFT', $alias = '') { |
|
88 | 88 | if (is_array($table)) { |
89 | - foreach ($table as $item) { |
|
89 | + foreach ($table as $item) { |
|
90 | 90 | if (!is_array($item)) { |
91 | - call_user_func_array(array($this, 'join'), $table); |
|
92 | - break; |
|
91 | + call_user_func_array(array($this, 'join'), $table); |
|
92 | + break; |
|
93 | 93 | } else { |
94 | - $this->join($item); |
|
94 | + $this->join($item); |
|
95 | + } |
|
95 | 96 | } |
96 | - } |
|
97 | 97 | } else { |
98 | - $this->join[] = [$table, $where, $type, $alias]; |
|
98 | + $this->join[] = [$table, $where, $type, $alias]; |
|
99 | + } |
|
99 | 100 | } |
100 | - } |
|
101 | 101 | |
102 | - public function where($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
|
102 | + public function where($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
|
103 | 103 | if (!is_array($where)) { |
104 | - $this->where[] = [$where, $value, $operation, $concatenation]; |
|
104 | + $this->where[] = [$where, $value, $operation, $concatenation]; |
|
105 | 105 | } else { |
106 | - $this->where[] = $where; |
|
106 | + $this->where[] = $where; |
|
107 | + } |
|
107 | 108 | } |
108 | - } |
|
109 | 109 | |
110 | - public function having($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
|
110 | + public function having($where = '', $value = '', $operation = false, $concatenation = 'AND') { |
|
111 | 111 | if (!is_array($where)) { |
112 | - $this->having[] = [$where, $value, $operation, $concatenation]; |
|
112 | + $this->having[] = [$where, $value, $operation, $concatenation]; |
|
113 | 113 | } else { |
114 | - $this->having[] = $where; |
|
114 | + $this->having[] = $where; |
|
115 | + } |
|
115 | 116 | } |
116 | - } |
|
117 | 117 | |
118 | - public function group($colname) { |
|
118 | + public function group($colname) { |
|
119 | 119 | $this->group[] = $colname; |
120 | - } |
|
120 | + } |
|
121 | 121 | |
122 | - public function order($order, $type = 'ASC') { |
|
122 | + public function order($order, $type = 'ASC') { |
|
123 | 123 | |
124 | 124 | |
125 | 125 | if (!is_array($order)) { |
126 | - $this->order[] = "{$order} {$type}"; |
|
126 | + $this->order[] = "{$order} {$type}"; |
|
127 | 127 | } else { |
128 | - foreach ($order as $item) |
|
128 | + foreach ($order as $item) |
|
129 | 129 | if (!is_array($item)) { |
130 | - call_user_func_array(array($this, 'order'), $order); |
|
131 | - break; |
|
130 | + call_user_func_array(array($this, 'order'), $order); |
|
131 | + break; |
|
132 | 132 | } else |
133 | - $this->order($item); |
|
133 | + $this->order($item); |
|
134 | + } |
|
134 | 135 | } |
135 | - } |
|
136 | 136 | |
137 | - public function limit($start = 0, $len = 0) { |
|
137 | + public function limit($start = 0, $len = 0) { |
|
138 | 138 | $start = intval($start); |
139 | 139 | $len = intval($len); |
140 | 140 | $this->limit = "LIMIT {$start}"; |
141 | 141 | if ($len !== 0) |
142 | - $this->limit .= ",{$len}"; |
|
143 | - } |
|
142 | + $this->limit .= ",{$len}"; |
|
143 | + } |
|
144 | 144 | |
145 | - public function buildJoin($table, $where = false, $type = 'LEFT', $alias = '') { |
|
145 | + public function buildJoin($table, $where = false, $type = 'LEFT', $alias = '') { |
|
146 | 146 | $join = ''; |
147 | 147 | if (is_array($table)) { |
148 | - $joins = func_get_args(); |
|
149 | - foreach ($joins as $joinAr) { |
|
148 | + $joins = func_get_args(); |
|
149 | + foreach ($joins as $joinAr) { |
|
150 | 150 | $join .= call_user_func_array([$this, 'buildJoin'], $joinAr); |
151 | - } |
|
151 | + } |
|
152 | 152 | } else { |
153 | - $join .= " {$type} JOIN {$this->curInstance->table_prefix}{$table}"; |
|
154 | - if ($alias) |
|
153 | + $join .= " {$type} JOIN {$this->curInstance->table_prefix}{$table}"; |
|
154 | + if ($alias) |
|
155 | 155 | $join .= " AS `{$alias}`"; |
156 | - if ($where) |
|
156 | + if ($where) |
|
157 | 157 | $join .= " ON {$where}"; |
158 | 158 | } |
159 | 159 | return $join; |
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Build where string |
|
164 | - * |
|
165 | - * @param string|array $where |
|
166 | - * @param mixed $value |
|
167 | - * @param string $operation |
|
168 | - * @param string $concatenation |
|
169 | - */ |
|
170 | - public function buildWhere($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Build where string |
|
164 | + * |
|
165 | + * @param string|array $where |
|
166 | + * @param mixed $value |
|
167 | + * @param string $operation |
|
168 | + * @param string $concatenation |
|
169 | + */ |
|
170 | + public function buildWhere($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
171 | 171 | if (!is_array($where)) { |
172 | - if (empty($operation)) { |
|
172 | + if (empty($operation)) { |
|
173 | 173 | $operation = '='; |
174 | - } |
|
174 | + } |
|
175 | 175 | |
176 | - if ($concatenation === false) |
|
176 | + if ($concatenation === false) |
|
177 | 177 | $concatenation = 'AND'; |
178 | - elseif ($concatenation === true) |
|
178 | + elseif ($concatenation === true) |
|
179 | 179 | $concatenation = ''; |
180 | 180 | |
181 | - if ($this->whereString == NULL) |
|
181 | + if ($this->whereString == NULL) |
|
182 | 182 | $this->whereString = ' WHERE '; |
183 | 183 | |
184 | - if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
184 | + if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
185 | 185 | if (is_array($value)) { |
186 | - $newValue = ''; |
|
187 | - foreach ($value as $item) { |
|
186 | + $newValue = ''; |
|
187 | + foreach ($value as $item) { |
|
188 | 188 | if ($newValue) { |
189 | - $newValue.=','; |
|
189 | + $newValue.=','; |
|
190 | 190 | } |
191 | 191 | if (is_string($item)) { |
192 | - $newValue .='"' . $item . '"'; |
|
192 | + $newValue .='"' . $item . '"'; |
|
193 | 193 | } else { |
194 | - $newValue .=$item; |
|
194 | + $newValue .=$item; |
|
195 | + } |
|
195 | 196 | } |
196 | - } |
|
197 | - $value = '(' . $newValue . ')'; |
|
197 | + $value = '(' . $newValue . ')'; |
|
198 | 198 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
199 | - $value = "({$value})"; |
|
199 | + $value = "({$value})"; |
|
200 | 200 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
201 | - $value = "\"{$value}\""; |
|
201 | + $value = "\"{$value}\""; |
|
202 | 202 | } |
203 | - } elseif (!in_array($value, array('CURRENT_TIMESTAMP'))) { |
|
203 | + } elseif (!in_array($value, array('CURRENT_TIMESTAMP'))) { |
|
204 | 204 | $this->params[] = $value; |
205 | 205 | $value = "?"; |
206 | - } |
|
206 | + } |
|
207 | 207 | |
208 | - if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') |
|
208 | + if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') |
|
209 | 209 | $this->whereString .= " {$where} {$operation} {$value} "; |
210 | - else |
|
210 | + else |
|
211 | 211 | $this->whereString .= "{$concatenation} {$where} {$operation} {$value} "; |
212 | 212 | } |
213 | 213 | else { |
214 | - $i = -1; |
|
215 | - while (isset($where[++$i])) { |
|
214 | + $i = -1; |
|
215 | + while (isset($where[++$i])) { |
|
216 | 216 | $item = $where[$i]; |
217 | 217 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
218 | - if ($this->whereString != NULL && substr($this->whereString, -1, 1) != '(' && $this->whereString != 'WHERE ') { |
|
218 | + if ($this->whereString != NULL && substr($this->whereString, -1, 1) != '(' && $this->whereString != 'WHERE ') { |
|
219 | 219 | if (!isset($item[3])) { |
220 | - $concatenation = 'AND'; |
|
220 | + $concatenation = 'AND'; |
|
221 | 221 | } else { |
222 | - $concatenation = $item[3]; |
|
222 | + $concatenation = $item[3]; |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | $this->whereString .= "{$concatenation} "; |
226 | - } |
|
226 | + } |
|
227 | 227 | |
228 | - if ($this->whereString != NULL) |
|
228 | + if ($this->whereString != NULL) |
|
229 | 229 | $this->whereString .= '('; |
230 | - else |
|
230 | + else |
|
231 | 231 | $this->whereString = 'WHERE ('; |
232 | 232 | } |
233 | 233 | |
234 | 234 | if (!is_array($item)) { |
235 | - call_user_func_array(array($this, 'buildWhere'), $where); |
|
236 | - break; |
|
235 | + call_user_func_array(array($this, 'buildWhere'), $where); |
|
236 | + break; |
|
237 | 237 | } else { |
238 | - $this->buildWhere($item); |
|
238 | + $this->buildWhere($item); |
|
239 | 239 | } |
240 | 240 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
241 | - $this->whereString .= ') '; |
|
241 | + $this->whereString .= ') '; |
|
242 | + } |
|
242 | 243 | } |
243 | - } |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Build having string |
|
249 | - * |
|
250 | - * @param string|array $where |
|
251 | - * @param mixed $value |
|
252 | - * @param string $operation |
|
253 | - * @param string $concatenation |
|
254 | - */ |
|
255 | - public function buildHaving($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Build having string |
|
249 | + * |
|
250 | + * @param string|array $where |
|
251 | + * @param mixed $value |
|
252 | + * @param string $operation |
|
253 | + * @param string $concatenation |
|
254 | + */ |
|
255 | + public function buildHaving($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
256 | 256 | if (!is_array($where)) { |
257 | - if (empty($operation)) { |
|
257 | + if (empty($operation)) { |
|
258 | 258 | $operation = '='; |
259 | - } |
|
260 | - if ($concatenation === false) |
|
259 | + } |
|
260 | + if ($concatenation === false) |
|
261 | 261 | $concatenation = 'AND'; |
262 | - elseif ($concatenation === true) |
|
262 | + elseif ($concatenation === true) |
|
263 | 263 | $concatenation = ''; |
264 | 264 | |
265 | - if ($this->havingString == NULL) |
|
265 | + if ($this->havingString == NULL) |
|
266 | 266 | $this->havingString = ' HAVING '; |
267 | 267 | |
268 | - if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
268 | + if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
269 | 269 | if (is_array($value)) { |
270 | - $newValue = ''; |
|
271 | - foreach ($value as $item) { |
|
270 | + $newValue = ''; |
|
271 | + foreach ($value as $item) { |
|
272 | 272 | if ($newValue) { |
273 | - $newValue.=','; |
|
273 | + $newValue.=','; |
|
274 | 274 | } |
275 | 275 | if (is_string($item)) { |
276 | - $newValue .='"' . $item . '"'; |
|
276 | + $newValue .='"' . $item . '"'; |
|
277 | 277 | } else { |
278 | - $newValue .=$item; |
|
278 | + $newValue .=$item; |
|
279 | + } |
|
279 | 280 | } |
280 | - } |
|
281 | - $value = '(' . $newValue . ')'; |
|
281 | + $value = '(' . $newValue . ')'; |
|
282 | 282 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
283 | - $value = "({$value})"; |
|
283 | + $value = "({$value})"; |
|
284 | 284 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
285 | - $value = "\"{$value}\""; |
|
285 | + $value = "\"{$value}\""; |
|
286 | 286 | } |
287 | - } elseif (!in_array($value, array('CURRENT_TIMESTAMP'))) { |
|
287 | + } elseif (!in_array($value, array('CURRENT_TIMESTAMP'))) { |
|
288 | 288 | $this->params[] = $value; |
289 | 289 | $value = "?"; |
290 | - } |
|
290 | + } |
|
291 | 291 | |
292 | - if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'E ') |
|
292 | + if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'E ') |
|
293 | 293 | $this->havingString .= " {$where} {$operation} {$value} "; |
294 | - else |
|
294 | + else |
|
295 | 295 | $this->havingString .= "{$concatenation} {$where} {$operation} {$value} "; |
296 | 296 | } |
297 | 297 | else { |
298 | - $i = -1; |
|
299 | - while (isset($where[++$i])) { |
|
298 | + $i = -1; |
|
299 | + while (isset($where[++$i])) { |
|
300 | 300 | $item = $where[$i]; |
301 | 301 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
302 | - if ($this->havingString != NULL && substr($this->havingString, -1, 1) != '(' && $this->havingString != 'HAVING ') { |
|
302 | + if ($this->havingString != NULL && substr($this->havingString, -1, 1) != '(' && $this->havingString != 'HAVING ') { |
|
303 | 303 | if (!isset($item[3])) { |
304 | - $concatenation = 'AND'; |
|
304 | + $concatenation = 'AND'; |
|
305 | 305 | } else { |
306 | - $concatenation = $item[3]; |
|
306 | + $concatenation = $item[3]; |
|
307 | 307 | } |
308 | 308 | |
309 | 309 | $this->havingString .= "{$concatenation} "; |
310 | - } |
|
310 | + } |
|
311 | 311 | |
312 | - if ($this->havingString != NULL) |
|
312 | + if ($this->havingString != NULL) |
|
313 | 313 | $this->havingString .= '('; |
314 | - else |
|
314 | + else |
|
315 | 315 | $this->havingString = 'HAVING ('; |
316 | 316 | } |
317 | 317 | |
318 | 318 | if (!is_array($item)) { |
319 | - call_user_func_array(array($this, 'buildHaving'), $where); |
|
320 | - break; |
|
319 | + call_user_func_array(array($this, 'buildHaving'), $where); |
|
320 | + break; |
|
321 | 321 | } else { |
322 | - $this->buildHaving($item); |
|
322 | + $this->buildHaving($item); |
|
323 | 323 | } |
324 | 324 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
325 | - $this->havingString .= ') '; |
|
325 | + $this->havingString .= ') '; |
|
326 | 326 | } |
327 | - } |
|
327 | + } |
|
328 | + } |
|
328 | 329 | } |
329 | - } |
|
330 | 330 | |
331 | - public function buildQuery() { |
|
331 | + public function buildQuery() { |
|
332 | 332 | $query = $this->operation; |
333 | 333 | $this->operation = strtoupper($this->operation); |
334 | 334 | |
335 | 335 | switch ($this->operation) { |
336 | - case 'SELECT': |
|
336 | + case 'SELECT': |
|
337 | 337 | $query .= ' ' . ($this->distinct ? 'DISTINCT' : ''); |
338 | 338 | $query .= ' ' . (!$this->cols ? '*' : ((is_array($this->cols) ? implode(',', $this->cols) : $this->cols))); |
339 | - case 'DELETE': |
|
339 | + case 'DELETE': |
|
340 | 340 | $query .= ' FROM'; |
341 | 341 | break; |
342 | - case 'INSERT': |
|
342 | + case 'INSERT': |
|
343 | 343 | $query .= ' INTO'; |
344 | 344 | break; |
345 | 345 | } |
346 | 346 | $query .= " `{$this->curInstance->db_name}`.`{$this->curInstance->table_prefix}{$this->table}`"; |
347 | 347 | if ($this->join) { |
348 | - $query .= $this->buildJoin($this->join); |
|
348 | + $query .= $this->buildJoin($this->join); |
|
349 | 349 | } |
350 | 350 | switch ($this->operation) { |
351 | - case 'INSERT': |
|
351 | + case 'INSERT': |
|
352 | 352 | $this->params = array_merge($this->params, array_values($this->cols)); |
353 | 353 | $colsStr = ''; |
354 | 354 | if ($this->cols) { |
355 | - $colsStr = '`' . implode('`,`', array_keys($this->cols)) . '`'; |
|
355 | + $colsStr = '`' . implode('`,`', array_keys($this->cols)) . '`'; |
|
356 | 356 | } |
357 | 357 | $query .= ' (' . $colsStr . ') VALUES (' . rtrim(str_repeat('?,', count($this->cols)), ',') . ')'; |
358 | 358 | break; |
359 | - case 'CREATE TABLE': |
|
359 | + case 'CREATE TABLE': |
|
360 | 360 | $query .= " ("; |
361 | 361 | foreach ($this->cols as $col_name => $param) { |
362 | - if ($param == 'pk') { |
|
362 | + if ($param == 'pk') { |
|
363 | 363 | $param = "int(11) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (`{$col_name}`)"; |
364 | - } |
|
365 | - $query .= " `{$col_name}` {$param},"; |
|
364 | + } |
|
365 | + $query .= " `{$col_name}` {$param},"; |
|
366 | 366 | } |
367 | 367 | $query = rtrim($query, ','); |
368 | 368 | if ($this->indexes) { |
369 | - $query .= ', ' . implode(',', $this->indexes); |
|
369 | + $query .= ', ' . implode(',', $this->indexes); |
|
370 | 370 | } |
371 | 371 | $query .= ") ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci"; |
372 | 372 | break; |
373 | - case 'UPDATE': |
|
373 | + case 'UPDATE': |
|
374 | 374 | $updates = []; |
375 | 375 | foreach ($this->cols as $key => $item) { |
376 | - if (!in_array($item, array('CURRENT_TIMESTAMP'))) { |
|
376 | + if (!in_array($item, array('CURRENT_TIMESTAMP'))) { |
|
377 | 377 | $this->params[] = $item; |
378 | 378 | $updates[] = "`{$key}` = ?"; |
379 | - } else { |
|
379 | + } else { |
|
380 | 380 | $updates[] = "`{$key}` = {$item}"; |
381 | - } |
|
381 | + } |
|
382 | 382 | } |
383 | 383 | $update = implode(',', $updates); |
384 | 384 | $query .=" SET {$update}"; |
385 | - case 'SELECT': |
|
385 | + case 'SELECT': |
|
386 | 386 | case 'DELETE': |
387 | 387 | $this->buildWhere($this->where); |
388 | 388 | if ($this->whereString) { |
389 | - $query .= ' ' . $this->whereString; |
|
389 | + $query .= ' ' . $this->whereString; |
|
390 | 390 | } |
391 | 391 | break; |
392 | 392 | } |
393 | 393 | if ($this->group) { |
394 | - $query .= ' GROUP BY ' . implode(',', $this->group); |
|
394 | + $query .= ' GROUP BY ' . implode(',', $this->group); |
|
395 | 395 | } |
396 | 396 | $this->buildHaving($this->having); |
397 | 397 | if ($this->havingString) { |
398 | - $query .= ' ' . $this->havingString; |
|
398 | + $query .= ' ' . $this->havingString; |
|
399 | 399 | } |
400 | 400 | if ($this->order) { |
401 | - $query .= ' ORDER BY ' . implode(',', $this->order); |
|
401 | + $query .= ' ORDER BY ' . implode(',', $this->order); |
|
402 | 402 | } |
403 | 403 | if ($this->limit) { |
404 | - $query .= ' ' . $this->limit; |
|
404 | + $query .= ' ' . $this->limit; |
|
405 | 405 | } |
406 | 406 | return ['query' => $query, 'params' => $this->params]; |
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Execute query |
|
411 | - * |
|
412 | - * @param string|array $query |
|
413 | - * @return \Db\Mysql\Result |
|
414 | - */ |
|
415 | - public function query($query = []) { |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Execute query |
|
411 | + * |
|
412 | + * @param string|array $query |
|
413 | + * @return \Db\Mysql\Result |
|
414 | + */ |
|
415 | + public function query($query = []) { |
|
416 | 416 | if (!$query) { |
417 | - $this->params = []; |
|
418 | - $query = $this->buildQuery(); |
|
417 | + $this->params = []; |
|
418 | + $query = $this->buildQuery(); |
|
419 | 419 | } |
420 | 420 | |
421 | 421 | if (is_string($query)) { |
422 | - $query = ['query' => $query, 'params' => $this->params]; |
|
422 | + $query = ['query' => $query, 'params' => $this->params]; |
|
423 | 423 | } |
424 | 424 | |
425 | 425 | $prepare = $this->curInstance->pdo->prepare($query['query']); |
@@ -429,10 +429,10 @@ discard block |
||
429 | 429 | $result = new Result(); |
430 | 430 | $result->pdoResult = $prepare; |
431 | 431 | if ($this->curInstance->dbInstance && $this->curInstance->dbInstance->curQuery && $this->curInstance->dbInstance->curQuery === $this) { |
432 | - $this->curInstance->dbInstance->curQuery = null; |
|
432 | + $this->curInstance->dbInstance->curQuery = null; |
|
433 | 433 | } |
434 | 434 | |
435 | 435 | return $result; |
436 | - } |
|
436 | + } |
|
437 | 437 | |
438 | 438 | } |
@@ -186,15 +186,15 @@ discard block |
||
186 | 186 | $newValue = ''; |
187 | 187 | foreach ($value as $item) { |
188 | 188 | if ($newValue) { |
189 | - $newValue.=','; |
|
189 | + $newValue .= ','; |
|
190 | 190 | } |
191 | 191 | if (is_string($item)) { |
192 | - $newValue .='"' . $item . '"'; |
|
192 | + $newValue .= '"'.$item.'"'; |
|
193 | 193 | } else { |
194 | - $newValue .=$item; |
|
194 | + $newValue .= $item; |
|
195 | 195 | } |
196 | 196 | } |
197 | - $value = '(' . $newValue . ')'; |
|
197 | + $value = '('.$newValue.')'; |
|
198 | 198 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
199 | 199 | $value = "({$value})"; |
200 | 200 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
@@ -270,15 +270,15 @@ discard block |
||
270 | 270 | $newValue = ''; |
271 | 271 | foreach ($value as $item) { |
272 | 272 | if ($newValue) { |
273 | - $newValue.=','; |
|
273 | + $newValue .= ','; |
|
274 | 274 | } |
275 | 275 | if (is_string($item)) { |
276 | - $newValue .='"' . $item . '"'; |
|
276 | + $newValue .= '"'.$item.'"'; |
|
277 | 277 | } else { |
278 | - $newValue .=$item; |
|
278 | + $newValue .= $item; |
|
279 | 279 | } |
280 | 280 | } |
281 | - $value = '(' . $newValue . ')'; |
|
281 | + $value = '('.$newValue.')'; |
|
282 | 282 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
283 | 283 | $value = "({$value})"; |
284 | 284 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
@@ -334,8 +334,8 @@ discard block |
||
334 | 334 | |
335 | 335 | switch ($this->operation) { |
336 | 336 | case 'SELECT': |
337 | - $query .= ' ' . ($this->distinct ? 'DISTINCT' : ''); |
|
338 | - $query .= ' ' . (!$this->cols ? '*' : ((is_array($this->cols) ? implode(',', $this->cols) : $this->cols))); |
|
337 | + $query .= ' '.($this->distinct ? 'DISTINCT' : ''); |
|
338 | + $query .= ' '.(!$this->cols ? '*' : ((is_array($this->cols) ? implode(',', $this->cols) : $this->cols))); |
|
339 | 339 | case 'DELETE': |
340 | 340 | $query .= ' FROM'; |
341 | 341 | break; |
@@ -352,9 +352,9 @@ discard block |
||
352 | 352 | $this->params = array_merge($this->params, array_values($this->cols)); |
353 | 353 | $colsStr = ''; |
354 | 354 | if ($this->cols) { |
355 | - $colsStr = '`' . implode('`,`', array_keys($this->cols)) . '`'; |
|
355 | + $colsStr = '`'.implode('`,`', array_keys($this->cols)).'`'; |
|
356 | 356 | } |
357 | - $query .= ' (' . $colsStr . ') VALUES (' . rtrim(str_repeat('?,', count($this->cols)), ',') . ')'; |
|
357 | + $query .= ' ('.$colsStr.') VALUES ('.rtrim(str_repeat('?,', count($this->cols)), ',').')'; |
|
358 | 358 | break; |
359 | 359 | case 'CREATE TABLE': |
360 | 360 | $query .= " ("; |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | } |
367 | 367 | $query = rtrim($query, ','); |
368 | 368 | if ($this->indexes) { |
369 | - $query .= ', ' . implode(',', $this->indexes); |
|
369 | + $query .= ', '.implode(',', $this->indexes); |
|
370 | 370 | } |
371 | 371 | $query .= ") ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci"; |
372 | 372 | break; |
@@ -381,27 +381,27 @@ discard block |
||
381 | 381 | } |
382 | 382 | } |
383 | 383 | $update = implode(',', $updates); |
384 | - $query .=" SET {$update}"; |
|
384 | + $query .= " SET {$update}"; |
|
385 | 385 | case 'SELECT': |
386 | 386 | case 'DELETE': |
387 | 387 | $this->buildWhere($this->where); |
388 | 388 | if ($this->whereString) { |
389 | - $query .= ' ' . $this->whereString; |
|
389 | + $query .= ' '.$this->whereString; |
|
390 | 390 | } |
391 | 391 | break; |
392 | 392 | } |
393 | 393 | if ($this->group) { |
394 | - $query .= ' GROUP BY ' . implode(',', $this->group); |
|
394 | + $query .= ' GROUP BY '.implode(',', $this->group); |
|
395 | 395 | } |
396 | 396 | $this->buildHaving($this->having); |
397 | 397 | if ($this->havingString) { |
398 | - $query .= ' ' . $this->havingString; |
|
398 | + $query .= ' '.$this->havingString; |
|
399 | 399 | } |
400 | 400 | if ($this->order) { |
401 | - $query .= ' ORDER BY ' . implode(',', $this->order); |
|
401 | + $query .= ' ORDER BY '.implode(',', $this->order); |
|
402 | 402 | } |
403 | 403 | if ($this->limit) { |
404 | - $query .= ' ' . $this->limit; |
|
404 | + $query .= ' '.$this->limit; |
|
405 | 405 | } |
406 | 406 | return ['query' => $query, 'params' => $this->params]; |
407 | 407 | } |
@@ -125,12 +125,14 @@ discard block |
||
125 | 125 | if (!is_array($order)) { |
126 | 126 | $this->order[] = "{$order} {$type}"; |
127 | 127 | } else { |
128 | - foreach ($order as $item) |
|
129 | - if (!is_array($item)) { |
|
128 | + foreach ($order as $item) { |
|
129 | + if (!is_array($item)) { |
|
130 | 130 | call_user_func_array(array($this, 'order'), $order); |
131 | + } |
|
131 | 132 | break; |
132 | - } else |
|
133 | - $this->order($item); |
|
133 | + } else { |
|
134 | + $this->order($item); |
|
135 | + } |
|
134 | 136 | } |
135 | 137 | } |
136 | 138 | |
@@ -138,8 +140,9 @@ discard block |
||
138 | 140 | $start = intval($start); |
139 | 141 | $len = intval($len); |
140 | 142 | $this->limit = "LIMIT {$start}"; |
141 | - if ($len !== 0) |
|
142 | - $this->limit .= ",{$len}"; |
|
143 | + if ($len !== 0) { |
|
144 | + $this->limit .= ",{$len}"; |
|
145 | + } |
|
143 | 146 | } |
144 | 147 | |
145 | 148 | public function buildJoin($table, $where = false, $type = 'LEFT', $alias = '') { |
@@ -151,10 +154,12 @@ discard block |
||
151 | 154 | } |
152 | 155 | } else { |
153 | 156 | $join .= " {$type} JOIN {$this->curInstance->table_prefix}{$table}"; |
154 | - if ($alias) |
|
155 | - $join .= " AS `{$alias}`"; |
|
156 | - if ($where) |
|
157 | - $join .= " ON {$where}"; |
|
157 | + if ($alias) { |
|
158 | + $join .= " AS `{$alias}`"; |
|
159 | + } |
|
160 | + if ($where) { |
|
161 | + $join .= " ON {$where}"; |
|
162 | + } |
|
158 | 163 | } |
159 | 164 | return $join; |
160 | 165 | } |
@@ -173,13 +178,15 @@ discard block |
||
173 | 178 | $operation = '='; |
174 | 179 | } |
175 | 180 | |
176 | - if ($concatenation === false) |
|
177 | - $concatenation = 'AND'; |
|
178 | - elseif ($concatenation === true) |
|
179 | - $concatenation = ''; |
|
181 | + if ($concatenation === false) { |
|
182 | + $concatenation = 'AND'; |
|
183 | + } elseif ($concatenation === true) { |
|
184 | + $concatenation = ''; |
|
185 | + } |
|
180 | 186 | |
181 | - if ($this->whereString == NULL) |
|
182 | - $this->whereString = ' WHERE '; |
|
187 | + if ($this->whereString == NULL) { |
|
188 | + $this->whereString = ' WHERE '; |
|
189 | + } |
|
183 | 190 | |
184 | 191 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
185 | 192 | if (is_array($value)) { |
@@ -205,12 +212,12 @@ discard block |
||
205 | 212 | $value = "?"; |
206 | 213 | } |
207 | 214 | |
208 | - if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') |
|
209 | - $this->whereString .= " {$where} {$operation} {$value} "; |
|
210 | - else |
|
211 | - $this->whereString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
212 | - } |
|
213 | - else { |
|
215 | + if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') { |
|
216 | + $this->whereString .= " {$where} {$operation} {$value} "; |
|
217 | + } else { |
|
218 | + $this->whereString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
219 | + } |
|
220 | + } else { |
|
214 | 221 | $i = -1; |
215 | 222 | while (isset($where[++$i])) { |
216 | 223 | $item = $where[$i]; |
@@ -225,10 +232,11 @@ discard block |
||
225 | 232 | $this->whereString .= "{$concatenation} "; |
226 | 233 | } |
227 | 234 | |
228 | - if ($this->whereString != NULL) |
|
229 | - $this->whereString .= '('; |
|
230 | - else |
|
231 | - $this->whereString = 'WHERE ('; |
|
235 | + if ($this->whereString != NULL) { |
|
236 | + $this->whereString .= '('; |
|
237 | + } else { |
|
238 | + $this->whereString = 'WHERE ('; |
|
239 | + } |
|
232 | 240 | } |
233 | 241 | |
234 | 242 | if (!is_array($item)) { |
@@ -257,13 +265,15 @@ discard block |
||
257 | 265 | if (empty($operation)) { |
258 | 266 | $operation = '='; |
259 | 267 | } |
260 | - if ($concatenation === false) |
|
261 | - $concatenation = 'AND'; |
|
262 | - elseif ($concatenation === true) |
|
263 | - $concatenation = ''; |
|
268 | + if ($concatenation === false) { |
|
269 | + $concatenation = 'AND'; |
|
270 | + } elseif ($concatenation === true) { |
|
271 | + $concatenation = ''; |
|
272 | + } |
|
264 | 273 | |
265 | - if ($this->havingString == NULL) |
|
266 | - $this->havingString = ' HAVING '; |
|
274 | + if ($this->havingString == NULL) { |
|
275 | + $this->havingString = ' HAVING '; |
|
276 | + } |
|
267 | 277 | |
268 | 278 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
269 | 279 | if (is_array($value)) { |
@@ -289,12 +299,12 @@ discard block |
||
289 | 299 | $value = "?"; |
290 | 300 | } |
291 | 301 | |
292 | - if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'E ') |
|
293 | - $this->havingString .= " {$where} {$operation} {$value} "; |
|
294 | - else |
|
295 | - $this->havingString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
296 | - } |
|
297 | - else { |
|
302 | + if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'E ') { |
|
303 | + $this->havingString .= " {$where} {$operation} {$value} "; |
|
304 | + } else { |
|
305 | + $this->havingString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
306 | + } |
|
307 | + } else { |
|
298 | 308 | $i = -1; |
299 | 309 | while (isset($where[++$i])) { |
300 | 310 | $item = $where[$i]; |
@@ -309,10 +319,11 @@ discard block |
||
309 | 319 | $this->havingString .= "{$concatenation} "; |
310 | 320 | } |
311 | 321 | |
312 | - if ($this->havingString != NULL) |
|
313 | - $this->havingString .= '('; |
|
314 | - else |
|
315 | - $this->havingString = 'HAVING ('; |
|
322 | + if ($this->havingString != NULL) { |
|
323 | + $this->havingString .= '('; |
|
324 | + } else { |
|
325 | + $this->havingString = 'HAVING ('; |
|
326 | + } |
|
316 | 327 | } |
317 | 328 | |
318 | 329 | if (!is_array($item)) { |
@@ -116,6 +116,9 @@ discard block |
||
116 | 116 | } |
117 | 117 | } |
118 | 118 | |
119 | + /** |
|
120 | + * @param string $user_mail |
|
121 | + */ |
|
119 | 122 | public function passre($user_mail) { |
120 | 123 | $user = $this->get($user_mail, 'mail'); |
121 | 124 | if (!$user) { |
@@ -400,6 +403,9 @@ discard block |
||
400 | 403 | return $return; |
401 | 404 | } |
402 | 405 | |
406 | + /** |
|
407 | + * @param integer $cat_id |
|
408 | + */ |
|
403 | 409 | public function addUserActivity($user_id, $cat_id, $text = '') { |
404 | 410 | $ua = new Users\Activity([ |
405 | 411 | 'user_id' => $user_id, |
@@ -37,8 +37,8 @@ discard block |
||
37 | 37 | } |
38 | 38 | * |
39 | 39 | */ |
40 | - if (filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_session_hash') && filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_id')) { |
|
41 | - return $this->cuntinueSession(filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_session_hash'), filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_id')); |
|
40 | + if (filter_input(INPUT_COOKIE, $this->cookiePrefix.'_user_session_hash') && filter_input(INPUT_COOKIE, $this->cookiePrefix.'_user_id')) { |
|
41 | + return $this->cuntinueSession(filter_input(INPUT_COOKIE, $this->cookiePrefix.'_user_session_hash'), filter_input(INPUT_COOKIE, $this->cookiePrefix.'_user_id')); |
|
42 | 42 | } |
43 | 43 | if (isset($_POST['autorization']) && trim(filter_input(INPUT_POST, 'user_login')) && trim(filter_input(INPUT_POST, 'user_pass'))) { |
44 | 44 | unset($_POST['autorization']); |
@@ -53,18 +53,18 @@ discard block |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | public function logOut($redirect = true) { |
56 | - if (!empty($_COOKIE[$this->cookiePrefix . "_user_session_hash"]) && !empty($_COOKIE[$this->cookiePrefix . "_user_id"])) { |
|
56 | + if (!empty($_COOKIE[$this->cookiePrefix."_user_session_hash"]) && !empty($_COOKIE[$this->cookiePrefix."_user_id"])) { |
|
57 | 57 | $session = Users\Session::get([ |
58 | - ['user_id', $_COOKIE[$this->cookiePrefix . "_user_id"]], |
|
59 | - ['hash', $_COOKIE[$this->cookiePrefix . "_user_session_hash"]] |
|
58 | + ['user_id', $_COOKIE[$this->cookiePrefix."_user_id"]], |
|
59 | + ['hash', $_COOKIE[$this->cookiePrefix."_user_session_hash"]] |
|
60 | 60 | ]); |
61 | 61 | if ($session) { |
62 | 62 | $session->delete(); |
63 | 63 | } |
64 | 64 | } |
65 | 65 | if (!headers_sent()) { |
66 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
67 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
66 | + setcookie($this->cookiePrefix."_user_session_hash", '', 0, "/"); |
|
67 | + setcookie($this->cookiePrefix."_user_id", '', 0, "/"); |
|
68 | 68 | } |
69 | 69 | if ($redirect) { |
70 | 70 | if (!empty($this->config['logoutUrl'][$this->app->type])) { |
@@ -81,25 +81,25 @@ discard block |
||
81 | 81 | ]); |
82 | 82 | if ($session && $session->user && $session->user->blocked) { |
83 | 83 | if (!headers_sent()) { |
84 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
85 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
84 | + setcookie($this->cookiePrefix."_user_session_hash", '', 0, "/"); |
|
85 | + setcookie($this->cookiePrefix."_user_id", '', 0, "/"); |
|
86 | 86 | } |
87 | 87 | Msg::add('Ваш аккаунт заблокирован', 'info'); |
88 | 88 | return; |
89 | 89 | } |
90 | 90 | if ($session && $session->user && !$session->user->blocked) { |
91 | 91 | if (!headers_sent()) { |
92 | - setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/"); |
|
93 | - setcookie($this->cookiePrefix . "_user_id", $session->user_id, time() + 360000, "/"); |
|
92 | + setcookie($this->cookiePrefix."_user_session_hash", $session->hash, time() + 360000, "/"); |
|
93 | + setcookie($this->cookiePrefix."_user_id", $session->user_id, time() + 360000, "/"); |
|
94 | 94 | } |
95 | 95 | if (!empty($this->config['needActivation']) && $session->user->activation) { |
96 | 96 | if (!headers_sent()) { |
97 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
98 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
97 | + setcookie($this->cookiePrefix."_user_session_hash", '', 0, "/"); |
|
98 | + setcookie($this->cookiePrefix."_user_id", '', 0, "/"); |
|
99 | 99 | } |
100 | - Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
|
100 | + Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/'.$session->user->id.'"><b>повторно выслать ссылку активации</b></a>'); |
|
101 | 101 | } elseif ($session->user->activation) { |
102 | - Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
|
102 | + Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/'.$session->user->id.'"><b>повторно выслать ссылку активации</b></a>'); |
|
103 | 103 | } |
104 | 104 | if (!$session->user->mail && !empty($this->config['noMailNotify'])) { |
105 | 105 | Msg::add($this->config['noMailNotify']); |
@@ -109,8 +109,8 @@ discard block |
||
109 | 109 | Users\User::$cur->save(); |
110 | 110 | } else { |
111 | 111 | if (!headers_sent()) { |
112 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
113 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
112 | + setcookie($this->cookiePrefix."_user_session_hash", '', 0, "/"); |
|
113 | + setcookie($this->cookiePrefix."_user_id", '', 0, "/"); |
|
114 | 114 | } |
115 | 115 | Msg::add('Ваша сессия устарела или более недействительна, вам необходимо пройти <a href = "/users/login">авторизацию</a> заново', 'info'); |
116 | 116 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | public function passre($user_mail) { |
120 | 120 | $user = $this->get($user_mail, 'mail'); |
121 | 121 | if (!$user) { |
122 | - Msg::add('Пользователь ' . $user_mail . ' не найден, проверьте првильность ввода e-mail или зарегистрируйтесь', 'danger'); |
|
122 | + Msg::add('Пользователь '.$user_mail.' не найден, проверьте првильность ввода e-mail или зарегистрируйтесь', 'danger'); |
|
123 | 123 | return false; |
124 | 124 | } |
125 | 125 | $passre = Users\Passre::get([['user_id', $user->id], ['status', 1]]); |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | $passre->status = 2; |
128 | 128 | $passre->save(); |
129 | 129 | } |
130 | - $hash = $user->id . '_' . Tools::randomString(50); |
|
130 | + $hash = $user->id.'_'.Tools::randomString(50); |
|
131 | 131 | $passre = new Users\Passre(['user_id' => $user->id, 'status' => 1, 'hash' => $hash]); |
132 | 132 | $passre->save(); |
133 | - Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, $user_mail, 'Восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для продолжения восстановления пароля перейдите по ссылке: <a href = "http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/?passrecont=1&hash=' . $hash . '">' . idn_to_utf8(INJI_DOMAIN_NAME) . '/?passrecont=1&hash=' . $hash . '</a>'); |
|
133 | + Tools::sendMail('noreply@'.INJI_DOMAIN_NAME, $user_mail, 'Восстановление пароля на сайте '.idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте '.idn_to_utf8(INJI_DOMAIN_NAME).'<br />для продолжения восстановления пароля перейдите по ссылке: <a href = "http://'.idn_to_utf8(INJI_DOMAIN_NAME).'/?passrecont=1&hash='.$hash.'">'.idn_to_utf8(INJI_DOMAIN_NAME).'/?passrecont=1&hash='.$hash.'</a>'); |
|
134 | 134 | Tools::redirect('/', 'На указанный почтовый ящик была выслана инструкция по восстановлению пароля', 'success'); |
135 | 135 | } |
136 | 136 | |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | $user->pass = $this->hashpass($pass); |
148 | 148 | $user->save(); |
149 | 149 | $this->autorization($user->mail, $user->pass, 'mail'); |
150 | - Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, $user->mail, 'Новый пароль на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />Ваш новый пароль: ' . $pass); |
|
150 | + Tools::sendMail('noreply@'.INJI_DOMAIN_NAME, $user->mail, 'Новый пароль на сайте '.idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте '.idn_to_utf8(INJI_DOMAIN_NAME).'<br />Ваш новый пароль: '.$pass); |
|
151 | 151 | Tools::redirect('/', 'На указанный почтовый ящик был выслан новый пароль', 'success'); |
152 | 152 | } |
153 | 153 | } |
@@ -159,9 +159,9 @@ discard block |
||
159 | 159 | $user = $this->get($login, $ltype); |
160 | 160 | if ($user && $this->verifypass($pass, $user->pass) && !$user->blocked) { |
161 | 161 | if (!empty($this->config['needActivation']) && $user->activation) { |
162 | - Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
|
162 | + Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/'.$user->id.'"><b>повторно выслать ссылку активации</b></a>'); |
|
163 | 163 | } elseif ($user->activation) { |
164 | - Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
|
164 | + Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/'.$user->id.'"><b>повторно выслать ссылку активации</b></a>'); |
|
165 | 165 | } |
166 | 166 | if (!$user->mail && !empty($this->config['noMailNotify'])) { |
167 | 167 | Msg::add($this->config['noMailNotify']); |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | if ($user && $user->blocked) { |
181 | 181 | Msg::add('Вы заблокированы', 'danger'); |
182 | 182 | } elseif ($user) { |
183 | - Msg::add('Вы ошиблись при наборе пароля или логина, попробуйте ещё раз или воспользуйтесь <a href = "?passre=1&user_mail=' . $user->mail . '">Восстановлением пароля</a>', 'danger'); |
|
183 | + Msg::add('Вы ошиблись при наборе пароля или логина, попробуйте ещё раз или воспользуйтесь <a href = "?passre=1&user_mail='.$user->mail.'">Восстановлением пароля</a>', 'danger'); |
|
184 | 184 | } else { |
185 | 185 | Msg::add('Данный почтовый ящик не зарегистрирован в системе', 'danger'); |
186 | 186 | } |
@@ -202,8 +202,8 @@ discard block |
||
202 | 202 | $session->save(); |
203 | 203 | |
204 | 204 | if (!headers_sent()) { |
205 | - setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/"); |
|
206 | - setcookie($this->cookiePrefix . "_user_id", $session->user_id, time() + 360000, "/"); |
|
205 | + setcookie($this->cookiePrefix."_user_session_hash", $session->hash, time() + 360000, "/"); |
|
206 | + setcookie($this->cookiePrefix."_user_id", $session->user_id, time() + 360000, "/"); |
|
207 | 207 | } else { |
208 | 208 | Msg::add('Не удалось провести авторизацию. Попробуйте позже', 'info'); |
209 | 209 | } |
@@ -346,20 +346,20 @@ discard block |
||
346 | 346 | $this->autorization($data['user_mail'], $pass, 'mail'); |
347 | 347 | } |
348 | 348 | if (!empty($this->config['needActivation'])) { |
349 | - $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
349 | + $from = 'noreply@'.INJI_DOMAIN_NAME; |
|
350 | 350 | $to = $data['user_mail']; |
351 | - $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
352 | - $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
353 | - $text .='<br />'; |
|
351 | + $subject = 'Регистрация на сайте '.idn_to_utf8(INJI_DOMAIN_NAME); |
|
352 | + $text = 'Вы были зарегистрированы на сайте '.idn_to_utf8(INJI_DOMAIN_NAME).'<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: '.$pass; |
|
354 | 353 | $text .= '<br />'; |
355 | - $text .= 'Для активации вашего аккаунта перейдите по ссылке <a href = "http://' . INJI_DOMAIN_NAME . '/users/activation/' . $user->id . '/' . $user->activation . '">http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/users/activation/' . $user->id . '/' . $user->activation . '</a>'; |
|
354 | + $text .= '<br />'; |
|
355 | + $text .= 'Для активации вашего аккаунта перейдите по ссылке <a href = "http://'.INJI_DOMAIN_NAME.'/users/activation/'.$user->id.'/'.$user->activation.'">http://'.idn_to_utf8(INJI_DOMAIN_NAME).'/users/activation/'.$user->id.'/'.$user->activation.'</a>'; |
|
356 | 356 | Tools::sendMail($from, $to, $subject, $text); |
357 | 357 | Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль и ссылка для активации', 'success'); |
358 | 358 | } else { |
359 | - $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
359 | + $from = 'noreply@'.INJI_DOMAIN_NAME; |
|
360 | 360 | $to = $data['user_mail']; |
361 | - $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
362 | - $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
361 | + $subject = 'Регистрация на сайте '.idn_to_utf8(INJI_DOMAIN_NAME); |
|
362 | + $text = 'Вы были зарегистрированы на сайте '.idn_to_utf8(INJI_DOMAIN_NAME).'<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: '.$pass; |
|
363 | 363 | Tools::sendMail($from, $to, $subject, $text); |
364 | 364 | Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль', 'success'); |
365 | 365 | } |
@@ -217,17 +217,20 @@ |
||
217 | 217 | * @return boolean|\User\User |
218 | 218 | */ |
219 | 219 | public function get($idn, $ltype = 'id') { |
220 | - if (!$idn) |
|
221 | - return false; |
|
220 | + if (!$idn) { |
|
221 | + return false; |
|
222 | + } |
|
222 | 223 | |
223 | - if (is_numeric($idn) && $ltype != 'login') |
|
224 | - $user = Users\User::get($idn, 'id'); |
|
225 | - elseif ($ltype == 'login') |
|
226 | - $user = Users\User::get($idn, 'login'); |
|
227 | - else |
|
228 | - $user = Users\User::get($idn, 'mail'); |
|
229 | - if (!$user) |
|
230 | - return []; |
|
224 | + if (is_numeric($idn) && $ltype != 'login') { |
|
225 | + $user = Users\User::get($idn, 'id'); |
|
226 | + } elseif ($ltype == 'login') { |
|
227 | + $user = Users\User::get($idn, 'login'); |
|
228 | + } else { |
|
229 | + $user = Users\User::get($idn, 'mail'); |
|
230 | + } |
|
231 | + if (!$user) { |
|
232 | + return []; |
|
233 | + } |
|
231 | 234 | |
232 | 235 | return $user; |
233 | 236 | } |
@@ -10,18 +10,18 @@ discard block |
||
10 | 10 | */ |
11 | 11 | class Users extends Module { |
12 | 12 | |
13 | - public $cookiePrefix = ''; |
|
13 | + public $cookiePrefix = ''; |
|
14 | 14 | |
15 | - public function init() { |
|
15 | + public function init() { |
|
16 | 16 | if (!empty($this->config['cookieSplit'])) { |
17 | - $this->cookiePrefix = \App::$cur->type; |
|
17 | + $this->cookiePrefix = \App::$cur->type; |
|
18 | 18 | } |
19 | 19 | \Users\User::$cur = new Users\User(array('group_id' => 1, 'role_id' => 1)); |
20 | 20 | if (!App::$cur->db->connect) { |
21 | - return; |
|
21 | + return; |
|
22 | 22 | } |
23 | 23 | if (isset($_GET['logout'])) { |
24 | - return $this->logOut(); |
|
24 | + return $this->logOut(); |
|
25 | 25 | } |
26 | 26 | /* |
27 | 27 | $activeSocials = Users\Social::getList(['where' => ['active', 1]]); |
@@ -38,158 +38,158 @@ discard block |
||
38 | 38 | * |
39 | 39 | */ |
40 | 40 | if (isset($_GET['passre']) && filter_input(INPUT_GET, 'user_mail')) { |
41 | - $this->passre(trim(filter_input(INPUT_GET, 'user_mail'))); |
|
41 | + $this->passre(trim(filter_input(INPUT_GET, 'user_mail'))); |
|
42 | 42 | } |
43 | 43 | if (!empty($_GET['passrecont']) && filter_input(INPUT_GET, 'hash')) { |
44 | - $this->passrecont(filter_input(INPUT_GET, 'hash')); |
|
44 | + $this->passrecont(filter_input(INPUT_GET, 'hash')); |
|
45 | 45 | } |
46 | 46 | if (filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_session_hash') && filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_id')) { |
47 | - return $this->cuntinueSession(filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_session_hash'), filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_id')); |
|
47 | + return $this->cuntinueSession(filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_session_hash'), filter_input(INPUT_COOKIE, $this->cookiePrefix . '_user_id')); |
|
48 | 48 | } |
49 | 49 | if (isset($_POST['autorization']) && trim(filter_input(INPUT_POST, 'user_login')) && trim(filter_input(INPUT_POST, 'user_pass'))) { |
50 | - unset($_POST['autorization']); |
|
51 | - return $this->autorization(trim(filter_input(INPUT_POST, 'user_login')), trim(filter_input(INPUT_POST, 'user_pass')), strpos(filter_input(INPUT_POST, 'user_login'), '@') ? 'mail' : 'login', false); |
|
50 | + unset($_POST['autorization']); |
|
51 | + return $this->autorization(trim(filter_input(INPUT_POST, 'user_login')), trim(filter_input(INPUT_POST, 'user_pass')), strpos(filter_input(INPUT_POST, 'user_login'), '@') ? 'mail' : 'login', false); |
|
52 | + } |
|
52 | 53 | } |
53 | - } |
|
54 | 54 | |
55 | - public function logOut($redirect = true) { |
|
55 | + public function logOut($redirect = true) { |
|
56 | 56 | if (!empty($_COOKIE[$this->cookiePrefix . "_user_session_hash"]) && !empty($_COOKIE[$this->cookiePrefix . "_user_id"])) { |
57 | - $session = Users\Session::get([ |
|
58 | - ['user_id', $_COOKIE[$this->cookiePrefix . "_user_id"]], |
|
59 | - ['hash', $_COOKIE[$this->cookiePrefix . "_user_session_hash"]] |
|
60 | - ]); |
|
61 | - if ($session) { |
|
57 | + $session = Users\Session::get([ |
|
58 | + ['user_id', $_COOKIE[$this->cookiePrefix . "_user_id"]], |
|
59 | + ['hash', $_COOKIE[$this->cookiePrefix . "_user_session_hash"]] |
|
60 | + ]); |
|
61 | + if ($session) { |
|
62 | 62 | $session->delete(); |
63 | - } |
|
63 | + } |
|
64 | 64 | } |
65 | 65 | if (!headers_sent()) { |
66 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
67 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
66 | + setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
67 | + setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
68 | 68 | } |
69 | 69 | if ($redirect) { |
70 | - if (!empty($this->config['logoutUrl'][$this->app->type])) { |
|
70 | + if (!empty($this->config['logoutUrl'][$this->app->type])) { |
|
71 | 71 | Tools::redirect($this->config['logoutUrl'][$this->app->type]); |
72 | - } |
|
73 | - Tools::redirect('/', 'Вы вышли из своего профиля', 'success'); |
|
72 | + } |
|
73 | + Tools::redirect('/', 'Вы вышли из своего профиля', 'success'); |
|
74 | + } |
|
74 | 75 | } |
75 | - } |
|
76 | 76 | |
77 | - public function cuntinueSession($hash, $userId) { |
|
77 | + public function cuntinueSession($hash, $userId) { |
|
78 | 78 | $session = Users\Session::get([ |
79 | 79 | ['user_id', $userId], |
80 | 80 | ['hash', $hash] |
81 | 81 | ]); |
82 | 82 | if ($session && $session->user && $session->user->blocked) { |
83 | - if (!headers_sent()) { |
|
83 | + if (!headers_sent()) { |
|
84 | 84 | setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
85 | 85 | setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
86 | - } |
|
87 | - Msg::add('Ваш аккаунт заблокирован', 'info'); |
|
88 | - return; |
|
86 | + } |
|
87 | + Msg::add('Ваш аккаунт заблокирован', 'info'); |
|
88 | + return; |
|
89 | 89 | } |
90 | 90 | if ($session && $session->user && !$session->user->blocked) { |
91 | - if (!headers_sent()) { |
|
91 | + if (!headers_sent()) { |
|
92 | 92 | setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/"); |
93 | 93 | setcookie($this->cookiePrefix . "_user_id", $session->user_id, time() + 360000, "/"); |
94 | - } |
|
95 | - if (!empty($this->config['needActivation']) && $session->user->activation) { |
|
94 | + } |
|
95 | + if (!empty($this->config['needActivation']) && $session->user->activation) { |
|
96 | 96 | if (!headers_sent()) { |
97 | - setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
98 | - setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
97 | + setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
|
98 | + setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
|
99 | 99 | } |
100 | 100 | Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
101 | - } elseif ($session->user->activation) { |
|
101 | + } elseif ($session->user->activation) { |
|
102 | 102 | Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
103 | - } |
|
104 | - if (!$session->user->mail && !empty($this->config['noMailNotify'])) { |
|
103 | + } |
|
104 | + if (!$session->user->mail && !empty($this->config['noMailNotify'])) { |
|
105 | 105 | Msg::add($this->config['noMailNotify']); |
106 | - } |
|
107 | - Users\User::$cur = $session->user; |
|
108 | - Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP'; |
|
109 | - Users\User::$cur->save(); |
|
106 | + } |
|
107 | + Users\User::$cur = $session->user; |
|
108 | + Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP'; |
|
109 | + Users\User::$cur->save(); |
|
110 | 110 | } else { |
111 | - if (!headers_sent()) { |
|
111 | + if (!headers_sent()) { |
|
112 | 112 | setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/"); |
113 | 113 | setcookie($this->cookiePrefix . "_user_id", '', 0, "/"); |
114 | - } |
|
115 | - Msg::add('Ваша сессия устарела или более недействительна, вам необходимо пройти <a href = "/users/login">авторизацию</a> заново', 'info'); |
|
114 | + } |
|
115 | + Msg::add('Ваша сессия устарела или более недействительна, вам необходимо пройти <a href = "/users/login">авторизацию</a> заново', 'info'); |
|
116 | + } |
|
116 | 117 | } |
117 | - } |
|
118 | 118 | |
119 | - public function passre($user_mail) { |
|
119 | + public function passre($user_mail) { |
|
120 | 120 | $user = $this->get($user_mail, 'mail'); |
121 | 121 | if (!$user) { |
122 | - Msg::add('Пользователь ' . $user_mail . ' не найден, проверьте првильность ввода e-mail или зарегистрируйтесь', 'danger'); |
|
123 | - return false; |
|
122 | + Msg::add('Пользователь ' . $user_mail . ' не найден, проверьте првильность ввода e-mail или зарегистрируйтесь', 'danger'); |
|
123 | + return false; |
|
124 | 124 | } |
125 | 125 | $passre = Users\Passre::get([['user_id', $user->id], ['status', 1]]); |
126 | 126 | if ($passre) { |
127 | - $passre->status = 2; |
|
128 | - $passre->save(); |
|
127 | + $passre->status = 2; |
|
128 | + $passre->save(); |
|
129 | 129 | } |
130 | 130 | $hash = $user->id . '_' . Tools::randomString(50); |
131 | 131 | $passre = new Users\Passre(['user_id' => $user->id, 'status' => 1, 'hash' => $hash]); |
132 | 132 | $passre->save(); |
133 | 133 | Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, $user_mail, 'Восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для продолжения восстановления пароля перейдите по ссылке: <a href = "http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/?passrecont=1&hash=' . $hash . '">' . idn_to_utf8(INJI_DOMAIN_NAME) . '/?passrecont=1&hash=' . $hash . '</a>'); |
134 | 134 | Tools::redirect('/', 'На указанный почтовый ящик была выслана инструкция по восстановлению пароля', 'success'); |
135 | - } |
|
135 | + } |
|
136 | 136 | |
137 | - public function passrecont($hash) { |
|
137 | + public function passrecont($hash) { |
|
138 | 138 | $passre = Users\Passre::get([['hash', $hash]]); |
139 | 139 | if ($passre) { |
140 | - if ($passre->status != 1) { |
|
140 | + if ($passre->status != 1) { |
|
141 | 141 | Tools::redirect('/', 'Этот код восстановление более недействителен', 'danger'); |
142 | - } |
|
143 | - $passre->status = 3; |
|
144 | - $passre->save(); |
|
145 | - $pass = Tools::randomString(10); |
|
146 | - $user = Users\User::get($passre->user_id); |
|
147 | - $user->pass = $this->hashpass($pass); |
|
148 | - $user->save(); |
|
149 | - $this->autorization($user->mail, $user->pass, 'mail'); |
|
150 | - Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, $user->mail, 'Новый пароль на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />Ваш новый пароль: ' . $pass); |
|
151 | - Tools::redirect('/', 'На указанный почтовый ящик был выслан новый пароль', 'success'); |
|
152 | - } |
|
153 | - } |
|
142 | + } |
|
143 | + $passre->status = 3; |
|
144 | + $passre->save(); |
|
145 | + $pass = Tools::randomString(10); |
|
146 | + $user = Users\User::get($passre->user_id); |
|
147 | + $user->pass = $this->hashpass($pass); |
|
148 | + $user->save(); |
|
149 | + $this->autorization($user->mail, $user->pass, 'mail'); |
|
150 | + Tools::sendMail('noreply@' . INJI_DOMAIN_NAME, $user->mail, 'Новый пароль на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME), 'Было запрошено восстановление пароля на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />Ваш новый пароль: ' . $pass); |
|
151 | + Tools::redirect('/', 'На указанный почтовый ящик был выслан новый пароль', 'success'); |
|
152 | + } |
|
153 | + } |
|
154 | 154 | |
155 | - public function autorization($login, $pass, $ltype = 'login', $noMsg = true) { |
|
155 | + public function autorization($login, $pass, $ltype = 'login', $noMsg = true) { |
|
156 | 156 | |
157 | 157 | sleep(3); //simple anti brute |
158 | 158 | |
159 | 159 | $user = $this->get($login, $ltype); |
160 | 160 | if ($user && $this->verifypass($pass, $user->pass) && !$user->blocked) { |
161 | - if (!empty($this->config['needActivation']) && $user->activation) { |
|
161 | + if (!empty($this->config['needActivation']) && $user->activation) { |
|
162 | 162 | Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
163 | - } elseif ($user->activation) { |
|
163 | + } elseif ($user->activation) { |
|
164 | 164 | Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>'); |
165 | - } |
|
166 | - if (!$user->mail && !empty($this->config['noMailNotify'])) { |
|
165 | + } |
|
166 | + if (!$user->mail && !empty($this->config['noMailNotify'])) { |
|
167 | 167 | Msg::add($this->config['noMailNotify']); |
168 | - } |
|
169 | - $this->newSession($user); |
|
168 | + } |
|
169 | + $this->newSession($user); |
|
170 | 170 | |
171 | - Users\User::$cur = $user; |
|
172 | - Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP'; |
|
173 | - Users\User::$cur->save(); |
|
174 | - if (!$noMsg && !empty($this->config['loginUrl'][$this->app->type])) { |
|
171 | + Users\User::$cur = $user; |
|
172 | + Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP'; |
|
173 | + Users\User::$cur->save(); |
|
174 | + if (!$noMsg && !empty($this->config['loginUrl'][$this->app->type])) { |
|
175 | 175 | Tools::redirect($this->config['loginUrl'][$this->app->type]); |
176 | - } |
|
177 | - return true; |
|
176 | + } |
|
177 | + return true; |
|
178 | 178 | } |
179 | 179 | if (!$noMsg) { |
180 | - if ($user && $user->blocked) { |
|
180 | + if ($user && $user->blocked) { |
|
181 | 181 | Msg::add('Вы заблокированы', 'danger'); |
182 | - } elseif ($user) { |
|
182 | + } elseif ($user) { |
|
183 | 183 | Msg::add('Вы ошиблись при наборе пароля или логина, попробуйте ещё раз или воспользуйтесь <a href = "?passre=1&user_mail=' . $user->mail . '">Восстановлением пароля</a>', 'danger'); |
184 | - } else { |
|
184 | + } else { |
|
185 | 185 | Msg::add('Данный почтовый ящик не зарегистрирован в системе', 'danger'); |
186 | - } |
|
186 | + } |
|
187 | 187 | } |
188 | 188 | |
189 | 189 | return false; |
190 | - } |
|
190 | + } |
|
191 | 191 | |
192 | - public function newSession($user) { |
|
192 | + public function newSession($user) { |
|
193 | 193 | $hash = Tools::randomString(255); |
194 | 194 | |
195 | 195 | $session = new Users\Session([ |
@@ -202,115 +202,115 @@ discard block |
||
202 | 202 | $session->save(); |
203 | 203 | |
204 | 204 | if (!headers_sent()) { |
205 | - setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/"); |
|
206 | - setcookie($this->cookiePrefix . "_user_id", $session->user_id, time() + 360000, "/"); |
|
205 | + setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/"); |
|
206 | + setcookie($this->cookiePrefix . "_user_id", $session->user_id, time() + 360000, "/"); |
|
207 | 207 | } else { |
208 | - Msg::add('Не удалось провести авторизацию. Попробуйте позже', 'info'); |
|
208 | + Msg::add('Не удалось провести авторизацию. Попробуйте позже', 'info'); |
|
209 | + } |
|
209 | 210 | } |
210 | - } |
|
211 | 211 | |
212 | - /** |
|
213 | - * Return user |
|
214 | - * |
|
215 | - * @param integer|string $idn |
|
216 | - * @param type $ltype |
|
217 | - * @return boolean|\User\User |
|
218 | - */ |
|
219 | - public function get($idn, $ltype = 'id') { |
|
212 | + /** |
|
213 | + * Return user |
|
214 | + * |
|
215 | + * @param integer|string $idn |
|
216 | + * @param type $ltype |
|
217 | + * @return boolean|\User\User |
|
218 | + */ |
|
219 | + public function get($idn, $ltype = 'id') { |
|
220 | 220 | if (!$idn) |
221 | - return false; |
|
221 | + return false; |
|
222 | 222 | |
223 | 223 | if (is_numeric($idn) && $ltype != 'login') |
224 | - $user = Users\User::get($idn, 'id'); |
|
224 | + $user = Users\User::get($idn, 'id'); |
|
225 | 225 | elseif ($ltype == 'login') |
226 | - $user = Users\User::get($idn, 'login'); |
|
226 | + $user = Users\User::get($idn, 'login'); |
|
227 | 227 | else |
228 | - $user = Users\User::get($idn, 'mail'); |
|
228 | + $user = Users\User::get($idn, 'mail'); |
|
229 | 229 | if (!$user) |
230 | - return []; |
|
230 | + return []; |
|
231 | 231 | |
232 | 232 | return $user; |
233 | - } |
|
233 | + } |
|
234 | 234 | |
235 | - public function registration($data, $autorization = false) { |
|
235 | + public function registration($data, $autorization = false) { |
|
236 | 236 | |
237 | 237 | if (empty($data['user_mail'])) { |
238 | - Msg::add('Вы не ввели E-mail', 'danger'); |
|
239 | - return false; |
|
238 | + Msg::add('Вы не ввели E-mail', 'danger'); |
|
239 | + return false; |
|
240 | 240 | } |
241 | 241 | $data['user_mail'] = trim($data['user_mail']); |
242 | 242 | if (!filter_var($data['user_mail'], FILTER_VALIDATE_EMAIL)) { |
243 | - Msg::add('Вы ввели не корректный E-mail', 'danger'); |
|
244 | - return false; |
|
243 | + Msg::add('Вы ввели не корректный E-mail', 'danger'); |
|
244 | + return false; |
|
245 | 245 | } |
246 | 246 | |
247 | 247 | $user = $this->get($data['user_mail'], 'mail'); |
248 | 248 | if ($user) { |
249 | - Msg::add('Введенный вами E-mail зарегистрирован в нашей системе, войдите или введите другой E-mail', 'danger'); |
|
250 | - return false; |
|
249 | + Msg::add('Введенный вами E-mail зарегистрирован в нашей системе, войдите или введите другой E-mail', 'danger'); |
|
250 | + return false; |
|
251 | 251 | } |
252 | 252 | if (empty($data['user_login'])) { |
253 | - $data['user_login'] = $data['user_mail']; |
|
253 | + $data['user_login'] = $data['user_mail']; |
|
254 | 254 | } |
255 | 255 | $data['user_login'] = trim($data['user_login']); |
256 | 256 | $user = $this->get($data['user_login'], 'login'); |
257 | 257 | if ($user) { |
258 | - Msg::add('Введенный вами логин зарегистрирован в нашей системе, войдите или введите другой логин', 'danger'); |
|
259 | - return false; |
|
258 | + Msg::add('Введенный вами логин зарегистрирован в нашей системе, войдите или введите другой логин', 'danger'); |
|
259 | + return false; |
|
260 | 260 | } |
261 | 261 | if (empty($data['first_name'])) { |
262 | - $data['first_name'] = ''; |
|
262 | + $data['first_name'] = ''; |
|
263 | 263 | } |
264 | 264 | if (empty($data['last_name'])) { |
265 | - $data['last_name'] = ''; |
|
265 | + $data['last_name'] = ''; |
|
266 | 266 | } |
267 | 267 | if (!empty($user_name)) { |
268 | - $data['first_name'] = ''; |
|
268 | + $data['first_name'] = ''; |
|
269 | 269 | } |
270 | 270 | if (empty($data['user_city'])) { |
271 | - $data['user_city'] = ''; |
|
271 | + $data['user_city'] = ''; |
|
272 | 272 | } |
273 | 273 | if (empty($data['user_birthday'])) { |
274 | - $data['user_birthday'] = ''; |
|
274 | + $data['user_birthday'] = ''; |
|
275 | 275 | } |
276 | 276 | if (empty($data['user_phone'])) { |
277 | - $data['user_phone'] = ''; |
|
277 | + $data['user_phone'] = ''; |
|
278 | 278 | } |
279 | 279 | $invite_code = (!empty($data['invite_code']) ? $data['invite_code'] : (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : '')))))); |
280 | 280 | if (!empty($invite_code)) { |
281 | - $invite = Users\User\Invite::get($invite_code, 'code'); |
|
282 | - if (!$invite) { |
|
281 | + $invite = Users\User\Invite::get($invite_code, 'code'); |
|
282 | + if (!$invite) { |
|
283 | 283 | Msg::add('Такой код приглашения не найден', 'danger'); |
284 | 284 | return false; |
285 | - } |
|
286 | - if ($invite->limit && !($invite->limit - $invite->count)) { |
|
285 | + } |
|
286 | + if ($invite->limit && !($invite->limit - $invite->count)) { |
|
287 | 287 | Msg::add('Лимит приглашений для данного кода исчерпан', 'danger'); |
288 | 288 | return false; |
289 | - } |
|
290 | - $data['parent_id'] = $invite->user_id; |
|
291 | - $inviter = $data['parent_id']; |
|
292 | - $invite->count++; |
|
293 | - $invite->save(); |
|
289 | + } |
|
290 | + $data['parent_id'] = $invite->user_id; |
|
291 | + $inviter = $data['parent_id']; |
|
292 | + $invite->count++; |
|
293 | + $invite->save(); |
|
294 | 294 | } |
295 | 295 | if (empty($data['parent_id']) && !empty($this->config['defaultPartner'])) { |
296 | - $data['parent_id'] = $this->config['defaultPartner']; |
|
296 | + $data['parent_id'] = $this->config['defaultPartner']; |
|
297 | 297 | } |
298 | 298 | if (!empty($data['user_pass'])) { |
299 | - if (empty($data['user_pass'][0])) { |
|
299 | + if (empty($data['user_pass'][0])) { |
|
300 | 300 | Msg::add('Введите пароль', 'danger'); |
301 | 301 | return false; |
302 | - } |
|
303 | - if (empty($data['user_pass'][1])) { |
|
302 | + } |
|
303 | + if (empty($data['user_pass'][1])) { |
|
304 | 304 | Msg::add('Повторите ввод пароля', 'danger'); |
305 | 305 | return false; |
306 | - } |
|
307 | - if ($data['user_pass'][0] != $data['user_pass'][1]) { |
|
306 | + } |
|
307 | + if ($data['user_pass'][0] != $data['user_pass'][1]) { |
|
308 | 308 | Msg::add('Введенные пароли несовпадают', 'danger'); |
309 | 309 | return false; |
310 | - } |
|
311 | - $pass = $data['user_pass'][0]; |
|
310 | + } |
|
311 | + $pass = $data['user_pass'][0]; |
|
312 | 312 | } else { |
313 | - $pass = Tools::randomString(10); |
|
313 | + $pass = Tools::randomString(10); |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | $user = new Users\User([ |
@@ -322,12 +322,12 @@ discard block |
||
322 | 322 | 'parent_id' => !empty($data['parent_id']) ? $data['parent_id'] : 0 |
323 | 323 | ]); |
324 | 324 | if (!empty($this->config['needActivation'])) { |
325 | - $user->activation = Tools::randomString(); |
|
325 | + $user->activation = Tools::randomString(); |
|
326 | 326 | } |
327 | 327 | $user->save(); |
328 | 328 | if (!$user->id) { |
329 | - Msg::add('Не удалось зарегистрировать', 'danger'); |
|
330 | - return false; |
|
329 | + Msg::add('Не удалось зарегистрировать', 'danger'); |
|
330 | + return false; |
|
331 | 331 | } |
332 | 332 | $info = new \Users\User\Info([ |
333 | 333 | 'user_id' => $user->id, |
@@ -340,41 +340,41 @@ discard block |
||
340 | 340 | ]); |
341 | 341 | $info->save(); |
342 | 342 | if (isset($inviter)) { |
343 | - $this->AddUserActivity($inviter, 2, "У вас зарегистрировался новый партнер, {$info->first_name} {$info->last_name} (id: {$user->id}, email: {$user->mail})"); |
|
343 | + $this->AddUserActivity($inviter, 2, "У вас зарегистрировался новый партнер, {$info->first_name} {$info->last_name} (id: {$user->id}, email: {$user->mail})"); |
|
344 | 344 | } |
345 | 345 | if ($autorization) { |
346 | - $this->autorization($data['user_mail'], $pass, 'mail'); |
|
346 | + $this->autorization($data['user_mail'], $pass, 'mail'); |
|
347 | 347 | } |
348 | 348 | if (!empty($this->config['needActivation'])) { |
349 | - $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
350 | - $to = $data['user_mail']; |
|
351 | - $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
352 | - $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
353 | - $text .='<br />'; |
|
354 | - $text .= '<br />'; |
|
355 | - $text .= 'Для активации вашего аккаунта перейдите по ссылке <a href = "http://' . INJI_DOMAIN_NAME . '/users/activation/' . $user->id . '/' . $user->activation . '">http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/users/activation/' . $user->id . '/' . $user->activation . '</a>'; |
|
356 | - Tools::sendMail($from, $to, $subject, $text); |
|
357 | - Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль и ссылка для активации', 'success'); |
|
349 | + $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
350 | + $to = $data['user_mail']; |
|
351 | + $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
352 | + $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
353 | + $text .='<br />'; |
|
354 | + $text .= '<br />'; |
|
355 | + $text .= 'Для активации вашего аккаунта перейдите по ссылке <a href = "http://' . INJI_DOMAIN_NAME . '/users/activation/' . $user->id . '/' . $user->activation . '">http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/users/activation/' . $user->id . '/' . $user->activation . '</a>'; |
|
356 | + Tools::sendMail($from, $to, $subject, $text); |
|
357 | + Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль и ссылка для активации', 'success'); |
|
358 | 358 | } else { |
359 | - $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
360 | - $to = $data['user_mail']; |
|
361 | - $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
362 | - $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
363 | - Tools::sendMail($from, $to, $subject, $text); |
|
364 | - Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль', 'success'); |
|
359 | + $from = 'noreply@' . INJI_DOMAIN_NAME; |
|
360 | + $to = $data['user_mail']; |
|
361 | + $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME); |
|
362 | + $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass; |
|
363 | + Tools::sendMail($from, $to, $subject, $text); |
|
364 | + Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль', 'success'); |
|
365 | 365 | } |
366 | 366 | return $user->id; |
367 | - } |
|
367 | + } |
|
368 | 368 | |
369 | - public function hashpass($pass) { |
|
369 | + public function hashpass($pass) { |
|
370 | 370 | return password_hash($pass, PASSWORD_DEFAULT); |
371 | - } |
|
371 | + } |
|
372 | 372 | |
373 | - public function verifypass($pass, $hash) { |
|
373 | + public function verifypass($pass, $hash) { |
|
374 | 374 | return password_verify($pass, $hash); |
375 | - } |
|
375 | + } |
|
376 | 376 | |
377 | - public function getUserPartners($user, $levels = 0) { |
|
377 | + public function getUserPartners($user, $levels = 0) { |
|
378 | 378 | $return = [ |
379 | 379 | 'users' => [], |
380 | 380 | 'levels' => [], |
@@ -384,29 +384,29 @@ discard block |
||
384 | 384 | $levels = []; |
385 | 385 | $userIds = $user->user_id; |
386 | 386 | for ($i = 1; $i <= $levels || !$levels; $i++) { |
387 | - if (!$userIds && $levels) { |
|
387 | + if (!$userIds && $levels) { |
|
388 | 388 | $levels[$i] = []; |
389 | 389 | continue; |
390 | - } elseif (!$userIds && !$levels) { |
|
390 | + } elseif (!$userIds && !$levels) { |
|
391 | 391 | break; |
392 | - } |
|
393 | - $usersLevel = \Users\User::getList(['where' => [['parent_id', $userIds, 'IN']]]); |
|
394 | - $return['users'] += $usersLevel; |
|
395 | - $return['levels'][$i] = array_keys($usersLevel); |
|
396 | - $userIds = implode(',', $return['levels'][$i]); |
|
397 | - $return['lastLevel'] = $i; |
|
392 | + } |
|
393 | + $usersLevel = \Users\User::getList(['where' => [['parent_id', $userIds, 'IN']]]); |
|
394 | + $return['users'] += $usersLevel; |
|
395 | + $return['levels'][$i] = array_keys($usersLevel); |
|
396 | + $userIds = implode(',', $return['levels'][$i]); |
|
397 | + $return['lastLevel'] = $i; |
|
398 | 398 | } |
399 | 399 | $return['count'] = count($return['users']); |
400 | 400 | return $return; |
401 | - } |
|
401 | + } |
|
402 | 402 | |
403 | - public function addUserActivity($user_id, $cat_id, $text = '') { |
|
403 | + public function addUserActivity($user_id, $cat_id, $text = '') { |
|
404 | 404 | $ua = new Users\Activity([ |
405 | 405 | 'user_id' => $user_id, |
406 | 406 | 'category_id' => $cat_id, |
407 | 407 | 'text' => $text, |
408 | 408 | ]); |
409 | 409 | $ua->save(); |
410 | - } |
|
410 | + } |
|
411 | 411 | |
412 | 412 | } |
@@ -1,17 +1,17 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - echo empty($options['noContainer']) ? '<div class="form-group">' : ''; |
|
4 | - echo $label !== false ? "<label>{$label}</label>" : ''; |
|
5 | - $attributes = [ |
|
6 | - 'name' => $name, |
|
7 | - 'class' => "form-control " . (!empty($options['class']) ? $options['class'] : '') |
|
8 | - ]; |
|
9 | - if (!empty($options['required'])) { |
|
3 | + echo empty($options['noContainer']) ? '<div class="form-group">' : ''; |
|
4 | + echo $label !== false ? "<label>{$label}</label>" : ''; |
|
5 | + $attributes = [ |
|
6 | + 'name' => $name, |
|
7 | + 'class' => "form-control " . (!empty($options['class']) ? $options['class'] : '') |
|
8 | + ]; |
|
9 | + if (!empty($options['required'])) { |
|
10 | 10 | $attributes['required'] = 'required'; |
11 | - } |
|
12 | - if (!empty($options['attributes'])) { |
|
11 | + } |
|
12 | + if (!empty($options['attributes'])) { |
|
13 | 13 | $attributes = array_merge($attributes, $options['attributes']); |
14 | - } |
|
15 | - echo Html::el('textarea', $attributes, !empty($options['value']) ? $options['value'] : (!empty($form->userDataTree[$name]) ? addcslashes($form->userDataTree[$name], "'") : '')); |
|
16 | - echo empty($options['noContainer']) ? '</div>' : ''; |
|
17 | - |
|
18 | 14 | \ No newline at end of file |
15 | + } |
|
16 | + echo Html::el('textarea', $attributes, !empty($options['value']) ? $options['value'] : (!empty($form->userDataTree[$name]) ? addcslashes($form->userDataTree[$name], "'") : '')); |
|
17 | + echo empty($options['noContainer']) ? '</div>' : ''; |
|
18 | + |
|
19 | 19 | \ No newline at end of file |
@@ -4,7 +4,7 @@ |
||
4 | 4 | echo $label !== false ? "<label>{$label}</label>" : ''; |
5 | 5 | $attributes = [ |
6 | 6 | 'name' => $name, |
7 | - 'class' => "form-control " . (!empty($options['class']) ? $options['class'] : '') |
|
7 | + 'class' => "form-control ".(!empty($options['class']) ? $options['class'] : '') |
|
8 | 8 | ]; |
9 | 9 | if (!empty($options['required'])) { |
10 | 10 | $attributes['required'] = 'required'; |
@@ -9,16 +9,16 @@ |
||
9 | 9 | 'value' => !empty($options['value']) ? addcslashes($options['value'], "'") : (!empty($form->userDataTree[$name]) ? addcslashes($form->userDataTree[$name], "'") : '') |
10 | 10 | ]; |
11 | 11 | if (!empty($options['disabled'])) { |
12 | - $attributes['disabled'] = 'disabled'; |
|
12 | + $attributes['disabled'] = 'disabled'; |
|
13 | 13 | } |
14 | 14 | if (!empty($options['placeholder'])) { |
15 | - $attributes['placeholder'] = $options['placeholder']; |
|
15 | + $attributes['placeholder'] = $options['placeholder']; |
|
16 | 16 | } |
17 | 17 | if (!empty($options['checked'])) { |
18 | - $attributes['checked'] = 'checked'; |
|
18 | + $attributes['checked'] = 'checked'; |
|
19 | 19 | } |
20 | 20 | if (!empty($options['attributes'])) { |
21 | - $attributes = array_merge($attributes, $options['attributes']); |
|
21 | + $attributes = array_merge($attributes, $options['attributes']); |
|
22 | 22 | } |
23 | 23 | echo Html::el('input', $attributes, '', null); |
24 | 24 | echo!empty($options['helpText']) ? "<div class='help-block'>{$options['helpText']}</div>" : ''; |
@@ -6,10 +6,10 @@ discard block |
||
6 | 6 | <?= $label; ?> |
7 | 7 | </h3> |
8 | 8 | <?php |
9 | - if ($options['source'] == 'relation' && empty($options['modelPk'])) { |
|
9 | + if ($options['source'] == 'relation' && empty($options['modelPk'])) { |
|
10 | 10 | echo '<h4 class=" text-muted">Чтобы добавить связи, сначала создайте объект</h4>'; |
11 | 11 | echo '<p class=" text-muted">Просто заполните доступные поля и нажмите кнопку внизу формы. После этого дополнительные поля разблокируются</p>'; |
12 | - } else { |
|
12 | + } else { |
|
13 | 13 | ?> |
14 | 14 | <div class="table-responsive"> |
15 | 15 | <table class ='table table-striped'> |
@@ -17,18 +17,18 @@ discard block |
||
17 | 17 | <tr> |
18 | 18 | <?php |
19 | 19 | foreach ($options['cols'] as $colName => $col) { |
20 | - if (!empty($col['hidden'])) { |
|
20 | + if (!empty($col['hidden'])) { |
|
21 | 21 | continue; |
22 | - } |
|
23 | - echo "<th>"; |
|
24 | - echo $col['col']['label']; |
|
25 | - if (!empty($col['col']['model'])) { |
|
22 | + } |
|
23 | + echo "<th>"; |
|
24 | + echo $col['col']['label']; |
|
25 | + if (!empty($col['col']['model'])) { |
|
26 | 26 | $modelName = $col['col']['model']; |
27 | 27 | $onclick = 'inji.Ui.forms.popUp(\'' . addslashes($modelName) . '\',{},function(elem){' |
28 | 28 | . 'return function(data,modal){inji.Ui.forms.submitAjax($(elem).closest(\'form\')[0], {notSave: true});}}(this));return false;'; |
29 | 29 | echo ' (<a href="" onclick="' . $onclick . ';this.disabled=true;return false;">Создать</a>)'; |
30 | - } |
|
31 | - echo "</th>"; |
|
30 | + } |
|
31 | + echo "</th>"; |
|
32 | 32 | } |
33 | 33 | ?> |
34 | 34 | <td> </td> |
@@ -36,14 +36,14 @@ discard block |
||
36 | 36 | </thead> |
37 | 37 | <tbody class="listBody"> |
38 | 38 | <?php |
39 | - $i = 0; |
|
40 | - if (!empty($options['values'])) { |
|
39 | + $i = 0; |
|
40 | + if (!empty($options['values'])) { |
|
41 | 41 | foreach ($options['values'] as $row) { |
42 | - echo '<tr>'; |
|
43 | - foreach ($options['cols'] as $colName => $col) { |
|
42 | + echo '<tr>'; |
|
43 | + foreach ($options['cols'] as $colName => $col) { |
|
44 | 44 | $input = clone $col['input']; |
45 | 45 | if (empty($col['hidden'])) { |
46 | - echo '<td>'; |
|
46 | + echo '<td>'; |
|
47 | 47 | } |
48 | 48 | $input->options['noContainer'] = true; |
49 | 49 | $input->colParams['label'] = false; |
@@ -51,24 +51,24 @@ discard block |
||
51 | 51 | $input->colName .= '[' . $colName . '][' . ($i) . ']'; |
52 | 52 | $input->draw(); |
53 | 53 | if (empty($col['hidden'])) { |
54 | - echo '</td>'; |
|
54 | + echo '</td>'; |
|
55 | + } |
|
55 | 56 | } |
56 | - } |
|
57 | - $i++; |
|
58 | - echo '<td class="actionTd"><a class="btn btn-danger btn-xs" onclick="inji.Ui.forms.delRowFromList(this);"><i class="glyphicon glyphicon-remove"></i></a></td>'; |
|
59 | - echo '</tr>'; |
|
57 | + $i++; |
|
58 | + echo '<td class="actionTd"><a class="btn btn-danger btn-xs" onclick="inji.Ui.forms.delRowFromList(this);"><i class="glyphicon glyphicon-remove"></i></a></td>'; |
|
59 | + echo '</tr>'; |
|
60 | 60 | } |
61 | - } |
|
62 | - ?> |
|
61 | + } |
|
62 | + ?> |
|
63 | 63 | </tbody> |
64 | 64 | <tfoot> |
65 | 65 | <tr> |
66 | 66 | <?php |
67 | 67 | foreach ($options['cols'] as $colName => $col) { |
68 | - if (!empty($col['hidden'])) { |
|
68 | + if (!empty($col['hidden'])) { |
|
69 | 69 | continue; |
70 | - } |
|
71 | - echo "<th>{$col['col']['label']}</th>"; |
|
70 | + } |
|
71 | + echo "<th>{$col['col']['label']}</th>"; |
|
72 | 72 | } |
73 | 73 | ?> |
74 | 74 | <td> </td> |
@@ -81,26 +81,26 @@ discard block |
||
81 | 81 | <script>/* |
82 | 82 | <tr> |
83 | 83 | <?php |
84 | - foreach ($options['cols'] as $colName => $col) { |
|
84 | + foreach ($options['cols'] as $colName => $col) { |
|
85 | 85 | if (empty($col['hidden'])) { |
86 | - echo '<td>'; |
|
86 | + echo '<td>'; |
|
87 | 87 | } |
88 | 88 | $col['input']->options['noContainer'] = true; |
89 | 89 | $col['input']->colParams['label'] = false; |
90 | 90 | $col['input']->colName.="[{$colName}][]"; |
91 | 91 | $col['input']->draw(); |
92 | 92 | if (empty($col['hidden'])) { |
93 | - echo '</td>'; |
|
93 | + echo '</td>'; |
|
94 | + } |
|
94 | 95 | } |
95 | - } |
|
96 | - ?> |
|
96 | + ?> |
|
97 | 97 | <td class="actionTd"><a class="btn btn-danger btn-xs" onclick="inji.Ui.forms.delRowFromList(this);"><i class="glyphicon glyphicon-remove"></i></a></td> |
98 | 98 | </tr> |
99 | 99 | */</script> |
100 | 100 | </div> |
101 | 101 | <?php |
102 | - } |
|
103 | - ?> |
|
102 | + } |
|
103 | + ?> |
|
104 | 104 | </div> |
105 | 105 | <?php |
106 | 106 | //exit(); |
@@ -24,9 +24,9 @@ discard block |
||
24 | 24 | echo $col['col']['label']; |
25 | 25 | if (!empty($col['col']['model'])) { |
26 | 26 | $modelName = $col['col']['model']; |
27 | - $onclick = 'inji.Ui.forms.popUp(\'' . addslashes($modelName) . '\',{},function(elem){' |
|
27 | + $onclick = 'inji.Ui.forms.popUp(\''.addslashes($modelName).'\',{},function(elem){' |
|
28 | 28 | . 'return function(data,modal){inji.Ui.forms.submitAjax($(elem).closest(\'form\')[0], {notSave: true});}}(this));return false;'; |
29 | - echo ' (<a href="" onclick="' . $onclick . ';this.disabled=true;return false;">Создать</a>)'; |
|
29 | + echo ' (<a href="" onclick="'.$onclick.';this.disabled=true;return false;">Создать</a>)'; |
|
30 | 30 | } |
31 | 31 | echo "</th>"; |
32 | 32 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $input->options['noContainer'] = true; |
49 | 49 | $input->colParams['label'] = false; |
50 | 50 | $input->colParams['value'] = $row[$colName]; |
51 | - $input->colName .= '[' . $colName . '][' . ($i) . ']'; |
|
51 | + $input->colName .= '['.$colName.']['.($i).']'; |
|
52 | 52 | $input->draw(); |
53 | 53 | if (empty($col['hidden'])) { |
54 | 54 | echo '</td>'; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | } |
88 | 88 | $col['input']->options['noContainer'] = true; |
89 | 89 | $col['input']->colParams['label'] = false; |
90 | - $col['input']->colName.="[{$colName}][]"; |
|
90 | + $col['input']->colName .= "[{$colName}][]"; |
|
91 | 91 | $col['input']->draw(); |
92 | 92 | if (empty($col['hidden'])) { |
93 | 93 | echo '</td>'; |
@@ -22,7 +22,7 @@ |
||
22 | 22 | |
23 | 23 | <?php |
24 | 24 | if (!empty($options['value']['lat']) && !empty($options['value']['lng'])) { |
25 | - /* |
|
25 | + /* |
|
26 | 26 | ?> |
27 | 27 | myPlacemark = new ymaps.Placemark([<?= $options['value']['lat']; ?>,<?= $options['value']['lng']; ?>], { |
28 | 28 | // Чтобы балун и хинт открывались на метке, необходимо задать ей определенные свойства. |
@@ -13,22 +13,22 @@ |
||
13 | 13 | |
14 | 14 | class FastEdit extends \Object { |
15 | 15 | |
16 | - public static function block($object, $col, $value = null, $parse = false) { |
|
16 | + public static function block($object, $col, $value = null, $parse = false) { |
|
17 | 17 | |
18 | 18 | $str = "<div class = 'fastEdit' "; |
19 | 19 | if ($object) { |
20 | - $str .= "data-model='" . get_class($object) . "' data-col='{$col}' data-key='" . $object->pk() . "'"; |
|
20 | + $str .= "data-model='" . get_class($object) . "' data-col='{$col}' data-key='" . $object->pk() . "'"; |
|
21 | 21 | } |
22 | 22 | $str .= ">"; |
23 | 23 | $value = $value !== null ? $value : ($object ? $object->$col : ''); |
24 | 24 | if ($parse) { |
25 | - \App::$cur->view->parseSource($value); |
|
25 | + \App::$cur->view->parseSource($value); |
|
26 | 26 | } else { |
27 | - $str .= $value; |
|
27 | + $str .= $value; |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | $str .= "</div>"; |
31 | 31 | return $str; |
32 | - } |
|
32 | + } |
|
33 | 33 | |
34 | 34 | } |
@@ -17,7 +17,7 @@ |
||
17 | 17 | |
18 | 18 | $str = "<div class = 'fastEdit' "; |
19 | 19 | if ($object) { |
20 | - $str .= "data-model='" . get_class($object) . "' data-col='{$col}' data-key='" . $object->pk() . "'"; |
|
20 | + $str .= "data-model='".get_class($object)."' data-col='{$col}' data-key='".$object->pk()."'"; |
|
21 | 21 | } |
22 | 22 | $str .= ">"; |
23 | 23 | $value = $value !== null ? $value : ($object ? $object->$col : ''); |