PluginProbe ʕ •ᴥ•ʔ
AI Copilot – Content Generator / 1.4.21
AI Copilot – Content Generator v1.4.21
1.5.4 1.4.21 1.4.18 1.4.19 1.4.20 trunk 1.0.4 1.1.0 1.2.0 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.17 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.9
ai-copilot-content-generator / classes / model.php
ai-copilot-content-generator / classes Last commit date
helpers 1 month ago tables 1 month ago aIProviderInterface.php 1 month ago assets.php 1 month ago baseObject.php 1 month ago builderBlock.php 1 month ago controller.php 1 month ago date.php 1 month ago db.php 1 month ago dispatcher.php 1 month ago errors.php 1 month ago field.php 1 month ago fieldAdapter.php 1 month ago frame.php 1 month ago helper.php 1 month ago html.php 1 month ago installer.php 1 month ago installerDbUpdater.php 1 month ago integration.php 1 month ago modInstaller.php 1 month ago model.php 1 month ago module.php 1 month ago req.php 1 month ago response.php 1 month ago table.php 1 month ago uri.php 1 month ago user.php 1 month ago utils.php 1 month ago validator.php 1 month ago view.php 1 month ago
model.php
324 lines
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5 abstract class WaicModel extends WaicBaseObject {
6 protected $_data = array();
7 protected $_code = '';
8
9 protected $_orderBy = '';
10 protected $_sortOrder = '';
11 protected $_groupBy = '';
12 protected $_limit = '';
13 protected $_where = array();
14 protected $_stringWhere = '';
15 protected $_selectFields = '*';
16 protected $_tbl = '';
17 protected $_lastGetCount = 0;
18 protected $_idField = 'id';
19 protected $_indexes = array();
20 // lists for fields values
21 public $fieldLists = array();
22
23 public function setCode( $code ) {
24 $this->_code = $code;
25 }
26 public function getCode() {
27 return $this->_code;
28 }
29 public function getModule() {
30 return WaicFrame::_()->getModule( $this->_code );
31 }
32
33 protected function _setTbl( $tbl ) {
34 $this->_tbl = $tbl;
35 }
36 public function setOrderBy( $orderBy ) {
37 $this->_orderBy = $orderBy;
38 return $this;
39 }
40 public function setIndexes( $indexes ) {
41 $this->_indexes = $indexes;
42 }
43 /**
44 * ASC, DESC
45 */
46 public function setSortOrder( $sortOrder ) {
47 $this->_sortOrder = $sortOrder;
48 return $this;
49 }
50 public function setLimit( $limit ) {
51 $this->_limit = $limit;
52 return $this;
53 }
54 public function setWhere( $where ) {
55 $this->_where = $where;
56 return $this;
57 }
58 public function addWhere( $where ) {
59 if (empty($this->_where) && !is_string($where)) {
60 $this->setWhere( $where );
61 } elseif (is_array($this->_where) && is_array($where)) {
62 $this->_where = array_merge($this->_where, $where);
63 } elseif (is_string($where)) {
64 if (!isset($this->_where['additionalCondition'])) {
65 $this->_where['additionalCondition'] = '';
66 }
67 if (!empty($this->_where['additionalCondition'])) {
68 $this->_where['additionalCondition'] .= ' AND ';
69 }
70 $this->_where['additionalCondition'] .= $where;
71 }
72 return $this;
73 }
74 public function setSelectFields( $selectFields ) {
75 if (is_array($selectFields)) {
76 $selectFields = implode(',', $selectFields);
77 }
78 $this->_selectFields = $selectFields;
79 return $this;
80 }
81 public function groupBy( $groupBy ) {
82 $this->_groupBy = $groupBy;
83 return $this;
84 }
85 public function getLastGetCount() {
86 return $this->_lastGetCount;
87 }
88 public function getTable() {
89 return WaicFrame::_()->getTable( $this->_tbl );
90 }
91 public function getFromTbl( $params = array() ) {
92 $this->_lastGetCount = 0;
93 $tbl = isset($params['tbl']) ? $params['tbl'] : $this->_tbl;
94 $table = WaicFrame::_()->getTable( $tbl );
95 $this->_buildQuery( $table );
96 $return = isset($params['return']) ? $params['return'] : 'all';
97 $data = $table->get($this->_selectFields, $this->_where, '', $return);
98 if (!empty($data)) {
99 switch ($return) {
100 case 'one':
101 $this->_lastGetCount = 1;
102 break;
103 case 'row':
104 $data = $this->_afterGetFromTbl( $data );
105 $this->_lastGetCount = 1;
106 break;
107 default:
108 foreach ($data as $i => $row) {
109 $data[ $i ] = $this->_afterGetFromTbl( $row );
110 }
111 $this->_lastGetCount = count( $data );
112 break;
113 }
114 }
115 $this->_clearQuery( $params );
116 return $data;
117 }
118 protected function _clearQuery( $params = array() ) {
119 $clear = isset($params['clear']) ? $params['clear'] : array();
120 if (!is_array($clear)) {
121 $clear = array($clear);
122 }
123 if (empty($clear) || in_array('limit', $clear)) {
124 $this->_limit = '';
125 }
126 if (empty($clear) || in_array('orderBy', $clear)) {
127 $this->_orderBy = '';
128 }
129 if (empty($clear) || in_array('sortOrder', $clear)) {
130 $this->_sortOrder = '';
131 }
132 if (empty($clear) || in_array('where', $clear)) {
133 $this->_where = array();
134 }
135 if (empty($clear) || in_array('selectFields', $clear)) {
136 $this->_selectFields = '*';
137 }
138 if (empty($clear) || in_array('groupBy', $clear)) {
139 $this->_groupBy = '';
140 }
141 }
142 public function getCount( $params = array() ) {
143 $tbl = isset($params['tbl']) ? $params['tbl'] : $this->_tbl;
144 $table = WaicFrame::_()->getTable( $tbl );
145 $this->setSelectFields('COUNT(*) AS total');
146 $this->_buildQuery( $table );
147 $data = (int) $table->get($this->_selectFields, $this->_where, '', 'one');
148 $this->_clearQuery($params);
149 return $data;
150 }
151 protected function _afterGetFromTbl( $row ) {
152 // You can re-define this method in your own model
153 return $row;
154 }
155 protected function _buildQuery( $table = null ) {
156 if (!$table) {
157 $table = WaicFrame::_()->getTable( $this->_tbl );
158 }
159 if (!empty($this->_orderBy)) {
160 $order = $this->_orderBy;
161 if (!empty($this->_sortOrder)) {
162 $order .= ' ' . strtoupper($this->_sortOrder);
163 }
164 $table->orderBy( $order );
165 }
166 if (!empty($this->_groupBy)) {
167 $table->groupBy($this->_groupBy);
168 }
169 if (!empty($this->_limit)) {
170 $table->setLimit($this->_limit);
171 }
172 }
173 public function removeGroup( $ids ) {
174 if (!is_array($ids)) {
175 $ids = array($ids);
176 }
177 // Remove all empty values
178 $ids = array_filter(array_map('intval', $ids));
179 if (!empty($ids)) {
180 if (WaicFrame::_()->getTable($this->_tbl)->delete(array('additionalCondition' => 'id IN (' . implode(',', $ids) . ')'))) {
181 $this->_dataRemove($ids);
182 return true;
183 } else {
184 $this->pushError(esc_html__('Database error detected', 'ai-copilot-content-generator'));
185 }
186 } else {
187 $this->pushError(esc_html__('Invalid ID', 'ai-copilot-content-generator'));
188 }
189 return false;
190 }
191 public function clear() {
192 return $this->delete(); // Just delete all
193 }
194 public function delete( $params = array() ) {
195 if (WaicFrame::_()->getTable($this->_tbl)->delete($params)) {
196 if (is_numeric($params)) {
197 $this->_dataRemove(array($params));
198 }
199 return true;
200 } else {
201 $this->pushError(esc_html__('Database error detected', 'ai-copilot-content-generator'));
202 }
203 return false;
204 }
205 public function getById( $id ) {
206 $data = $this->setWhere(array($this->_idField => $id))->getFromTbl();
207 return empty($data) ? false : array_shift($data);
208 }
209 public function insert( $data ) {
210 $data = $this->_dataSave($data, false);
211 $id = WaicFrame::_()->getTable($this->_tbl)->insert($data);
212 if ($id) {
213 return $id;
214 }
215 $this->pushError(WaicFrame::_()->getTable($this->_tbl)->getErrors());
216 return false;
217 }
218 public function updateById( $data, $id = 0 ) {
219 if (!$id) {
220 $id = isset($data[ $this->_idField ]) ? (int) $data[ $this->_idField ] : 0;
221 }
222 if ($id) {
223 return $this->update($data, array($this->_idField => $id));
224 } else {
225 $this->pushError(esc_html__('Empty or invalid ID', 'ai-copilot-content-generator'));
226 }
227 return false;
228 }
229 public function update( $data, $where ) {
230 $data = $this->_dataSave($data, true);
231 if (WaicFrame::_()->getTable($this->_tbl)->update($data, $where)) {
232 return true;
233 }
234 $this->pushError(WaicFrame::_()->getTable($this->_tbl)->getErrors());
235 return false;
236 }
237 protected function _dataSave( $data, $update = false ) {
238 return $data;
239 }
240 public function getTbl() {
241 return $this->_tbl;
242 }
243 /**
244 * We can re-define this method to not retrive all data - for simple tables
245 */
246 public function setSimpleGetFields() {
247 return $this;
248 }
249 protected function setFieldLists( $list ) {
250 $this->fieldLists = $list;
251 $values = array();
252 foreach ($list as $field => $data) {
253 $values[$field] = array_keys($data);
254 }
255 $this->getTable()->setLists($values);
256 }
257 public function getFieldLists( $field, $key = false ) {
258 $list = WaicUtils::getArrayValue($this->fieldLists, $field, array(), 2);
259 return false === $key ? $list : WaicUtils::getArrayValue($list, $key);
260 }
261 public function getFieldKeys( $field ) {
262 $list = WaicUtils::getArrayValue($this->fieldLists, $field, array(), 2);
263 return is_array($list) ? array_keys($list) : $list;
264 }
265 protected function _dataRemove( $ids ) {
266 return false;
267 }
268 public function dropIndexes( $withPrimary = false ) {
269 $table = $this->_tbl;
270 $indexes = WaicDb::get('SHOW INDEX FROM `@__' . $table . '`');
271 if (!$indexes) {
272 $this->pushError(WaicDb::getError());
273 return false;
274 }
275
276 $drop = array();
277 foreach ($indexes as $index) {
278 $name = $index['Key_name'];
279 if ($withPrimary || 'PRIMARY' != $name) {
280 $drop[] = ' DROP INDEX ' . $name;
281 }
282 }
283 if (!empty($drop)) {
284 if (!WaicDb::query('ALTER TABLE `@__' . $table . '`' . implode(',', array_unique($drop)))) {
285 $this->pushError(WaicDb::getError());
286 return false;
287 }
288 }
289 return true;
290 }
291 public function addIndexes( $delete = true ) {
292 if (empty($this->_indexes)) {
293 return true;
294 }
295 $table = $this->_tbl;
296 $indexes = WaicDb::get('SHOW INDEX FROM `@__' . $table . '`');
297 if (!$indexes) {
298 $this->pushError(WaicDb::getError());
299 return false;
300 }
301 $exists = array();
302 foreach ($indexes as $index) {
303 $exists[] = $index['Key_name'];
304 }
305
306 $alter = '';
307 foreach ($this->_indexes as $name => $index) {
308 if (!in_array($name, $exists)) {
309 $alter .= ' ADD ' . $index . ',';
310 }
311 }
312 if (!empty($alter)) {
313 if (!WaicDb::query('ALTER TABLE `@__' . $table . '`' . substr($alter, 0, -1))) {
314 $this->pushError(WaicDb::getError());
315 if ($delete) {
316 $this->delete();
317 }
318 return false;
319 }
320 }
321 return true;
322 }
323 }
324