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