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