meraproject/themes/merakomis/dictionary/section/controller.php
keboss-m 5c21d25d45 Initial commit: Merakomis portal, Docker stack and user-reader API.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-24 11:04:05 +03:00

73 lines
2.3 KiB
PHP

<?
namespace controller\themes\merakomis\dictionary\section;
use ms\ms\structure\msControllerTable;
use themes\merakomis\dictionary\Section;
use themes\merakomis\dictionary\Step;
use themes\merakomis\Emp;
use themes\merakomis\structure\merakomisControllerTable;
class Api extends merakomisControllerTable {
static $class = 'themes\merakomis\dictionary\Section';
static function create(){
$res = [];
if(Emp::$IS_AUTH){
$step_id = intval($_POST['id']);
$sections = $_POST['sections'];
$structure = $_POST['structure'];
$IDS = [];
$data = [
Step::$NAME => $_POST['name'],
Step::$TEXT => $_POST['text'],
Step::$VIS => boolval($_POST['vis']),
];
if($step_id){
Step::updateById($step_id,$data );
} else {
$r = Step::insert($data);
$step_id = $r->id;
}
foreach($sections as $id=>$v){
if(!$id) continue;
if($id<0){
$r = Section::insert([
Section::$NAME => $v['name'],
Section::$STEP => $step_id,
]);
$sections[$id]['id'] = $r->id;
} else {
Section::updateById($id,[
Section::$NAME=>$v['name']
]);
}
}
foreach ($structure as $pid=>$ids){
foreach ($ids as $id){
$parent = $sections[$pid];
if(!$pid or ($parent and intval($pid))) {
$real_id = $sections[$id]['id'];
Section::updateById($real_id, [
Section::$PARENT => $parent['id'],
]);
$IDS[] = $real_id;
}
}
}
Section::delete($w = new \Where(
\Where::_and([
\Where::_not([
\Where::_in(Section::$ID, $IDS)
]),
\Where::_operator(Section::$STEP,'=',$step_id)
])
));
}
echo je($res);
}
}