3declare(strict_types=1);
23 private string $table =
'';
24 private string $order_field =
'';
25 private string $id_field =
'';
26 private string $common_field =
'';
39 public static function getInstance(
string $table,
string $order_field =
'position',
string $id_field =
'id',
string $common_field =
''): object
41 if (
null === static::$instance)
43 static::$instance =
new static($table, $order_field, $id_field, $common_field);
45 return static::$instance;
54 public function __construct(
string $table,
string $order_field =
'position',
string $id_field =
'id',
string $common_field =
'')
56 $this->table = $table;
57 $this->order_field = $order_field;
58 $this->id_field = $id_field;
59 $this->common_field = $common_field;
73 $this->errorMessage =
"";
75 $sql =
'SELECT `'.$this->order_field.
'`';
76 if ($this->common_field !=
'')
78 $sql .=
',`'.$this->common_field.
'`';
80 $sql .=
' FROM `'.$this->table.
'` WHERE `'.$this->id_field.
'`='.$id;
90 if (!empty($rec_current))
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 !=
'')
98 $sql .=
'AND (`'.$this->common_field.
'`=\''.$rec_current[$this->common_field].
'\')
';
100 $sql .= 'ORDER BY `
'.$this->order_field.'` DESC
';
103 $database->execute_query(
109 if (!empty($rec_prev))
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))
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))
126 $this->errorMessage = $database->get_error()." [1]";
130 $this->errorMessage = $database->get_error()." [2]";
135 $this->errorMessage = $database->get_error()." [3]";
140 $this->errorMessage = $database->get_error()." [4]";
150 public function move_down(int $id): bool
154 $this->errorMessage = "";
156 // get current record
157 $sql = 'SELECT `
'.$this->order_field.'`
';
158 if ($this->common_field != '')
160 $sql .= ',`
'.$this->common_field.'`
';
162 $sql .= ' FROM `
'.$this->table.'` WHERE `
'.$this->id_field.'`=
'.(int)$id;
165 $database->execute_query(
172 if (!empty($rec_current))
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 != '')
181 $sql .= 'AND (`
'.$this->common_field.'`=\
''.$rec_current[$this->common_field].
'\')
';
183 $sql .= 'ORDER BY `
'.$this->order_field.'` ASC
';
185 $database->execute_query(
192 if (!empty($rec_next))
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;
199 if ($database->simple_query($sql))
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))
209 $this->errorMessage = $database->get_error()." [5]";
213 $this->errorMessage = $database->get_error()." [6]";
218 $this->errorMessage = $database->get_error()." [7]";
223 $this->errorMessage = $database->get_error()." [8]";
233 public function get_new(string $cf_value = '
'): int
235 $database = LEPTON_database::getInstance();
237 $sql = 'SELECT MAX(`
' . $this->order_field . '`) FROM `' . $this->table . '` ';
240 $sql .=
'WHERE `' . $this->common_field .
'`=\'' . $cf_value .
'\'';
242 return 1+ intval(
$database->get_one($sql));
251 public function clean(
string|
int $cf_value =
''): bool
255 $sql =
'SELECT `'.$this->id_field.
'` FROM `'.$this->table.
'` ';
258 $sql .=
'WHERE `'.$this->common_field.
'`=\''.$cf_value.
'\'';
260 $sql .=
'ORDER BY `'.$this->order_field.
'` ASC';
269 if (!empty($res_ids))
272 foreach ($res_ids as $rec_id)
274 $sql =
'UPDATE `'.$this->table.
'` SET `'.$this->order_field.
'`='.($count++).
' ';
275 $sql .=
'WHERE `'.$this->id_field.
'`='.$rec_id[$this->id_field];
278 $this->errorMessage =
$database->get_error().
" [9]";
284 $this->errorMessage =
$database->get_error();
static getInstance(array &$settings=[])
__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='')