| @@ -3,9 +3,11 @@ | ||
| 3 | 3 | namespace AcyMailing\Core; |
| 4 | 4 | |
| 5 | 5 | abstract class AcymClass extends AcymObject |
| 6 | 6 | { |
| 7 | + // Handle errors | |
| 7 | 8 | public array $errors = []; |
| 9 | + // Information messages, mainly for the cron report | |
| 8 | 10 | public array $messages = []; |
| 9 | 11 | |
| 10 | 12 | protected string $table; |
| 11 | 13 | protected string $pkey = ''; |
| @@ -12,8 +14,12 @@ | ||
| 12 | 14 | protected bool $forceInsert = false; |
| 13 | 15 | protected array $intColumns = []; |
| 14 | 16 | protected array $jsonColumns = []; |
| 15 | 17 | |
| 18 | + /** | |
| 19 | + * Returns an array containing "elements[]" which are all the requested elements and "total" which is the number of elements. | |
| 20 | + * Can also contain "status[]", containing the status of elements. Optional. | |
| 21 | + */ | |
| 16 | 22 | public function getMatchingElements(array $settings = []): array |
| 17 | 23 | { |
| 18 | 24 | if (!empty($this->table) && !empty($this->pkey)) { |
| 19 | 25 | $query = 'SELECT * FROM #__acym_'.acym_secureDBColumn($this->table); |
| @@ -80,11 +86,13 @@ | ||
| 80 | 86 | |
| 81 | 87 | public function save(object $element): ?int |
| 82 | 88 | { |
| 83 | 89 | $tableColumns = acym_getColumns($this->table); |
| 90 | + // We clone the element because we don't want to modify it for later in the code | |
| 84 | 91 | $cloneElement = clone $element; |
| 85 | 92 | foreach ($cloneElement as $column => $value) { |
| 86 | 93 | if (!in_array($column, $tableColumns)) { |
| 94 | + // Unset variables that don't exist in the table | |
| 87 | 95 | unset($cloneElement->$column); |
| 88 | 96 | continue; |
| 89 | 97 | } |
| 90 | 98 | acym_secureDBColumn($column); |
| @@ -102,9 +110,9 @@ | ||
| 102 | 110 | $status = false; |
| 103 | 111 | } |
| 104 | 112 | |
| 105 | 113 | if (empty($status)) { |
| 106 | - $dbError = strip_tags(isset($e) ? $e->getMessage() : acym_getDBError()); | |
| 114 | + $dbError = acym_stripTags(isset($e) ? $e->getMessage() : acym_getDBError()); | |
| 107 | 115 | if (!empty($dbError)) { |
| 108 | 116 | if (strlen($dbError) > 203) $dbError = substr($dbError, 0, 200).'...'; |
| 109 | 117 | $this->errors[] = $dbError; |
| 110 | 118 | } |
| @@ -160,8 +168,11 @@ | ||
| 160 | 168 | acym_arrayToInteger($elements); |
| 161 | 169 | acym_query('UPDATE '.acym_secureDBColumn('#__acym_'.$this->table).' SET active = 0 WHERE `'.acym_secureDBColumn($this->pkey).'` IN ('.implode(',', $elements).')'); |
| 162 | 170 | } |
| 163 | 171 | |
| 172 | + /** | |
| 173 | + * Joomla/WordPress can return ints as strings or ints depending on their versions | |
| 174 | + */ | |
| 164 | 175 | protected function fixTypes(object $element): void |
| 165 | 176 | { |
| 166 | 177 | foreach ($this->intColumns as $intColumn) { |
| 167 | 178 | if (isset($element->$intColumn)) { |
| @@ -169,9 +180,9 @@ | ||
| 169 | 180 | } |
| 170 | 181 | } |
| 171 | 182 | |
| 172 | 183 | foreach ($this->jsonColumns as $oneColumn) { |
| 173 | - if (!isset($element->$oneColumn)) { | |
| 184 | + if (!property_exists($element, $oneColumn)) { | |
| 174 | 185 | continue; |
| 175 | 186 | } |
| 176 | 187 | |
| 177 | 188 | if (empty($element->$oneColumn)) { |
| @@ -176,9 +187,10 @@ | ||
| 176 | 187 | |
| 177 | 188 | if (empty($element->$oneColumn)) { |
| 178 | 189 | $element->$oneColumn = []; |
| 179 | 190 | } elseif (is_string($element->$oneColumn)) { |
| 180 | - $element->$oneColumn = json_decode($element->$oneColumn, true); | |
| 191 | + $decodedColumn = json_decode($element->$oneColumn, true); | |
| 192 | + $element->$oneColumn = is_array($decodedColumn) ? $decodedColumn : []; | |
| 181 | 193 | } |
| 182 | 194 | } |
| 183 | 195 | } |
| 184 | 196 | } |