method order documentation in bbn\User\Preferences
Changes the position of the given option among its sortable list.
function(string $id_option, int $index, bool $upd = false)
{
// What matters are the info from the parent
$id_parent = $this->opt->getIdParent($id_option);
// Only usable on sortable options
if (($id_parent !== false) && $this->opt->isSortable($id_parent)) {
// List of options IDs with personalized order
$items = $this->items($id_parent);
// Creating a new array of arrays with ID and NUM indexes
$res = [];
$o_index = false;
foreach ($items as $i => $it) {
$res[] = [
$this->fields['id'] => $it,
$this->fields['num'] => $i + 1
];
// If user has preference on that option, NUM will be set to the user's value
if ($cfg = $this->get($it)) {
$res[$i] = X::mergeArrays($res[$i], $cfg);
}
// This is the index of the given option
if ($it === $id_option) {
$o_index = $i;
}
}
if ($o_index === false) {
throw new \Exception(X::_('Impossible to find this option'));
}
// Changing the position of all the affected options
if ($o_index > $index) {
for ($i = $index; $i < $o_index; $i++) {
$res[$i][$this->fields['num']]++;
}
}
elseif ($o_index < $index) {
for ($i = $o_index + 1; $i <= $index; $i++) {
$res[$i][$this->fields['num']]--;
}
}
// Changing the given option's NUM
$res[$o_index][$this->fields['num']] = $index + 1;
foreach ($res as $i => $r) {
if ($upd) {
$this->updateByOption($r[$this->fields['id']], $r);
}
else {
$this->add($r[$this->fields['id']], $r);
}
}
X::sortBy($res, $this->fields['num']);
return $res;
}
}
Changes the position of the given option among its sortable list. BBN is a suite of PHP and JS libraries and VueJS components - all open-source! bbn.io, build applications, the quick way
This website uses cookies to ensure you get the best experience on our website.
© 2011-2025
BBN Solutions