PluginProbe
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress / 10.10.2
AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress v10.10.2
11.1.1 11.1.0 11.0.5 11.0.4 11.0.3 11.0.2 11.0.1 11.0.0 10.11.1 10.11.0 10.10.2 10.10.1 10.10.0 10.9.1 trunk 10.0.0 10.0.1 10.1.0 10.1.1 10.1.2 10.1.3 10.1.4 10.2.0 10.2.1 10.2.2 All 62 releases
acymailing / back / Core / AcymClass.php

AcymClass.php in AcyMailing – An Ultimate Newsletter Plugin and Marketing Automation Solution for WordPress 10.10.2, at back/Core/AcymClass.php

185 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace AcyMailing\Core;
4
5 abstract class AcymClass extends AcymObject
6 {
7 public array $errors = [];
8 public array $messages = [];
9
10 protected string $table;
11 protected string $pkey = '';
12 protected bool $forceInsert = false;
13 protected array $intColumns = [];
14 protected array $jsonColumns = [];
15
16 public function getMatchingElements(array $settings = []): array
17 {
18 if (!empty($this->table) && !empty($this->pkey)) {
19 $query = 'SELECT * FROM #__acym_'.acym_secureDBColumn($this->table);
20 $queryCount = 'SELECT COUNT(*) AS total FROM #__acym_'.acym_secureDBColumn($this->table);
21
22 if (empty($settings['ordering'])) $settings['ordering'] = $this->pkey;
23 $query .= ' ORDER BY `'.acym_secureDBColumn($settings['ordering']).'`';
24 if (!empty($settings['ordering_sort_order'])) $query .= ' '.acym_secureDBColumn(strtoupper($settings['ordering_sort_order']));
25
26 $elements = acym_loadObjectList($query);
27 $total = acym_loadObject($queryCount);
28 } else {
29 $elements = [];
30 $total = new \stdClass();
31 $total->total = 0;
32 }
33
34 array_map([$this, 'fixTypes'], $elements);
35
36 return [
37 'elements' => $elements,
38 'total' => $total,
39 ];
40 }
41
42 public function getOneById(int $id): ?object
43 {
44 $element = acym_loadObject('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE `'.acym_secureDBColumn($this->pkey).'` = '.intval($id));
45
46 if (empty($element)) {
47 return null;
48 }
49
50 $this->fixTypes($element);
51
52 return $element;
53 }
54
55 public function getByIds(array $ids): array
56 {
57 if (empty($ids)) {
58 return [];
59 }
60
61 acym_arrayToInteger($ids);
62
63 $elements = acym_loadObjectList('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE `'.acym_secureDBColumn($this->pkey).'` IN ("'.implode('","', $ids).'")');
64 array_map([$this, 'fixTypes'], $elements);
65
66 return $elements;
67 }
68
69 public function getAll(?string $key = null): array
70 {
71 if (empty($key)) {
72 $key = $this->pkey;
73 }
74
75 $elements = acym_loadObjectList('SELECT * FROM #__acym_'.acym_secureDBColumn($this->table), $key);
76 array_map([$this, 'fixTypes'], $elements);
77
78 return $elements;
79 }
80
81 public function save(object $element): ?int
82 {
83 $tableColumns = acym_getColumns($this->table);
84 $cloneElement = clone $element;
85 foreach ($cloneElement as $column => $value) {
86 if (!in_array($column, $tableColumns)) {
87 unset($cloneElement->$column);
88 continue;
89 }
90 acym_secureDBColumn($column);
91 }
92
93 $pkey = $this->pkey;
94
95 try {
96 if (empty($cloneElement->$pkey) || $this->forceInsert) {
97 $status = acym_insertObject('#__acym_'.$this->table, $cloneElement);
98 } else {
99 $status = acym_updateObject('#__acym_'.$this->table, $cloneElement, [$pkey]);
100 }
101 } catch (\Exception $e) {
102 $status = false;
103 }
104
105 if (empty($status)) {
106 $dbError = strip_tags(isset($e) ? $e->getMessage() : acym_getDBError());
107 if (!empty($dbError)) {
108 if (strlen($dbError) > 203) $dbError = substr($dbError, 0, 200).'...';
109 $this->errors[] = $dbError;
110 }
111
112 return false;
113 }
114
115 return (int)(empty($cloneElement->$pkey) ? $status : $cloneElement->$pkey);
116 }
117
118 public function delete(array $elements): int
119 {
120 if (empty($elements)) {
121 return 0;
122 }
123
124 $escapedElements = array_map('acym_escapeDB', $elements);
125
126 if (empty($this->pkey) || empty($this->table) || empty($escapedElements)) {
127 return 0;
128 }
129
130 acym_trigger('onAcymBefore'.ucfirst($this->table).'Delete', [&$elements]);
131
132 $query = 'DELETE FROM #__acym_'.acym_secureDBColumn($this->table).' WHERE '.acym_secureDBColumn($this->pkey).' IN ('.implode(',', $escapedElements).')';
133 $result = acym_query($query);
134
135 if (!$result) {
136 return 0;
137 }
138
139 acym_trigger('onAcymAfter'.ucfirst($this->table).'Delete', [&$elements]);
140
141 return (int)$result;
142 }
143
144 public function setActive(array $elements): void
145 {
146 if (empty($elements)) {
147 return;
148 }
149
150 acym_arrayToInteger($elements);
151 acym_query('UPDATE '.acym_secureDBColumn('#__acym_'.$this->table).' SET active = 1 WHERE `'.acym_secureDBColumn($this->pkey).'` IN ('.implode(',', $elements).')');
152 }
153
154 public function setInactive(array $elements): void
155 {
156 if (empty($elements)) {
157 return;
158 }
159
160 acym_arrayToInteger($elements);
161 acym_query('UPDATE '.acym_secureDBColumn('#__acym_'.$this->table).' SET active = 0 WHERE `'.acym_secureDBColumn($this->pkey).'` IN ('.implode(',', $elements).')');
162 }
163
164 protected function fixTypes(object $element): void
165 {
166 foreach ($this->intColumns as $intColumn) {
167 if (isset($element->$intColumn)) {
168 $element->$intColumn = (int)$element->$intColumn;
169 }
170 }
171
172 foreach ($this->jsonColumns as $oneColumn) {
173 if (!isset($element->$oneColumn)) {
174 continue;
175 }
176
177 if (empty($element->$oneColumn)) {
178 $element->$oneColumn = [];
179 } elseif (is_string($element->$oneColumn)) {
180 $element->$oneColumn = json_decode($element->$oneColumn, true);
181 }
182 }
183 }
184 }
185