LEPTON CMS 7.2.0
feel free to keep it strictly simple...
Loading...
Searching...
No Matches
lepton_order.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22{
23 private string $table = '';
24 private string $order_field = '';
25 private string $id_field = '';
26 private string $common_field = '';
27
28 public string $errorMessage = '';
29
30 public static $instance;
31
39 public static function getInstance(string $table, string $order_field = 'position', string $id_field = 'id', string $common_field = ''): object
40 {
41 if (null === static::$instance)
42 {
43 static::$instance = new static($table, $order_field, $id_field, $common_field);
44 }
45 return static::$instance;
46 }
47
54 public function __construct(string $table, string $order_field = 'position', string $id_field = 'id', string $common_field = '')
55 {
56 $this->table = $table;
57 $this->order_field = $order_field;
58 $this->id_field = $id_field;
59 $this->common_field = $common_field;
60 }
61
68 public function move_up(int $id): bool
69 {
71 // get current record
72
73 $this->errorMessage = "";
74
75 $sql = 'SELECT `'.$this->order_field.'`';
76 if ($this->common_field != '')
77 {
78 $sql .= ',`'.$this->common_field.'`';
79 }
80 $sql .= ' FROM `'.$this->table.'` WHERE `'.$this->id_field.'`='.$id;
81
82 $rec_current = [];
83 $database->execute_query(
84 $sql,
85 true,
86 $rec_current,
87 false
88 );
89
90 if (!empty($rec_current))
91 {
92 // get previous record
93 $sql = 'SELECT `'.$this->id_field.'`,`'.$this->order_field.'` ';
94 $sql .= 'FROM `'.$this->table.'` ';
95 $sql .= 'WHERE (`'.$this->order_field.'`<'.$rec_current[$this->order_field].') ';
96 if ($this->common_field != '')
97 {
98 $sql .= 'AND (`'.$this->common_field.'`=\''.$rec_current[$this->common_field].'\') ';
99 }
100 $sql .= 'ORDER BY `'.$this->order_field.'` DESC';
101
102 $rec_prev = [];
103 $database->execute_query(
104 $sql,
105 true,
106 $rec_prev,
107 false
108 );
109 if (!empty($rec_prev))
110 {
111 // update current record
112 $sql = 'UPDATE `'.$this->table.'` ';
113 $sql .= 'SET `'.$this->order_field.'`='.$rec_prev[$this->order_field].' ';
114 $sql .= 'WHERE `'.$this->id_field.'`='.$id;
115 if ($database->simple_query($sql))
116 {
117 // update previous record
118 $sql = 'UPDATE `'.$this->table.'` ';
119 $sql .= 'SET `'.$this->order_field.'`='.$rec_current[$this->order_field].' ';
120 $sql .= 'WHERE `'.$this->id_field.'`='.$rec_prev[$this->id_field];
121 if ($database->simple_query($sql))
122 {
123 return true;
124
125 } else {
126 $this->errorMessage = $database->get_error()." [1]";
127 return false;
128 }
129 } else {
130 $this->errorMessage = $database->get_error()." [2]";
131 return false;
132 }
133
134 } else {
135 $this->errorMessage = $database->get_error()." [3]";
136 return false;
137 }
138
139 }
140 $this->errorMessage = $database->get_error()." [4]";
141 return false;
142 }
143
150 public function move_down(int $id): bool
151 {
152 global $database;
153
154 $this->errorMessage = "";
155
156 // get current record
157 $sql = 'SELECT `'.$this->order_field.'`';
158 if ($this->common_field != '')
159 {
160 $sql .= ',`'.$this->common_field.'`';
161 }
162 $sql .= ' FROM `'.$this->table.'` WHERE `'.$this->id_field.'`='.(int)$id;
163
164 $rec_current = [];
165 $database->execute_query(
166 $sql,
167 true,
168 $rec_current,
169 false
170 );
171
172 if (!empty($rec_current))
173 {
174
175 // get next record
176 $sql = 'SELECT `'.$this->id_field.'`,`'.$this->order_field.'` ';
177 $sql .= 'FROM `'.$this->table.'` ';
178 $sql .= 'WHERE (`'.$this->order_field.'`>'.$rec_current[$this->order_field].') ';
179 if ($this->common_field != '')
180 {
181 $sql .= 'AND (`'.$this->common_field.'`=\''.$rec_current[$this->common_field].'\') ';
182 }
183 $sql .= 'ORDER BY `'.$this->order_field.'` ASC';
184 $rec_next = [];
185 $database->execute_query(
186 $sql,
187 true,
188 $rec_next,
189 false
190 );
191
192 if (!empty($rec_next))
193 {
194 // update current record
195 $sql = 'UPDATE `'.$this->table.'` ';
196 $sql .= 'SET `'.$this->order_field.'`='.$rec_next[$this->order_field].' ';
197 $sql .= 'WHERE `'.$this->id_field.'`='.$id;
198
199 if ($database->simple_query($sql))
200 {
201 // update next record
202 $sql = 'UPDATE `'.$this->table.'` ';
203 $sql .= 'SET `'.$this->order_field.'`='.$rec_current[$this->order_field].' ';
204 $sql .= 'WHERE `'.$this->id_field.'`='.$rec_next[$this->id_field];
205 if ($database->simple_query($sql))
206 {
207 return true;
208 } else {
209 $this->errorMessage = $database->get_error()." [5]";
210 return false;
211 }
212 } else {
213 $this->errorMessage = $database->get_error()." [6]";
214 return false;
215 }
216
217 } else {
218 $this->errorMessage = $database->get_error()." [7]";
219 return false;
220 }
221
222 }
223 $this->errorMessage = $database->get_error()." [8]";
224 return false;
225 }
226
233 public function get_new(string $cf_value = ''): int
234 {
235 $database = LEPTON_database::getInstance();
236 // Get last order
237 $sql = 'SELECT MAX(`' . $this->order_field . '`) FROM `' . $this->table . '` ';
238 if ($cf_value != '')
239 {
240 $sql .= 'WHERE `' . $this->common_field . '`=\'' . $cf_value . '\'';
241 }
242 return 1+ intval($database->get_one($sql));
243 }
244
251 public function clean(string|int $cf_value = ''): bool
252 {
254 // Loop through all records and give new order
255 $sql = 'SELECT `'.$this->id_field.'` FROM `'.$this->table.'` ';
256 if ($cf_value > -1)
257 {
258 $sql .= 'WHERE `'.$this->common_field.'`=\''.$cf_value.'\'';
259 }
260 $sql .= 'ORDER BY `'.$this->order_field.'` ASC';
261
262 $res_ids = [];
263 $database->execute_query(
264 $sql,
265 true,
266 $res_ids,
267 true
268 );
269 if (!empty($res_ids))
270 {
271 $count = 1;
272 foreach ($res_ids as $rec_id)
273 {
274 $sql = 'UPDATE `'.$this->table.'` SET `'.$this->order_field.'`='.($count++).' ';
275 $sql .= 'WHERE `'.$this->id_field.'`='.$rec_id[$this->id_field];
276 if (!$database->simple_query($sql))
277 {
278 $this->errorMessage = $database->get_error()." [9]";
279 return false;
280 }
281 }
282 return true;
283 }
284 $this->errorMessage = $database->get_error();
285 return false;
286 }
287}
static getInstance(array &$settings=[])
move_up(int $id)
__construct(string $table, string $order_field='position', string $id_field='id', string $common_field='')
clean(string|int $cf_value='')
static getInstance(string $table, string $order_field='position', string $id_field='id', string $common_field='')
string $errorMessage
$database
Definition constants.php:52