Ejemplo n.º 1
0
$Validator->validateArray($_POST, $validation_rules);
if ($errors = $Validator->getErrors()) {
    return $Javascript->setErrors($errors)->output();
}
$scheme_id = !empty($_POST['id']) ? (int) $_POST['id'] : null;
$update = !!$scheme_id;
$table = new \Tables\Schemes();
if ($update) {
    // May the current user edit this scheme part?
    $scheme_part = $table->by('user_id', getUser()->id)->get($scheme_id);
    if (!$scheme_part) {
        return $Javascript->setError(__('You are not authorized to edit this scheme.'))->output();
    }
}
if ($update) {
    $table->update($_POST, $scheme_id);
} else {
    $data = $_POST;
    $data['user_id'] = getUser()->id;
    $scheme_id = $table->create($data);
}
# Update scheme scheme parts table.
$table = new \Tables\SchemeSchemeParts();
if ($update) {
    // Delete existing scheme scheme parts.
    $table->by('scheme_id', $scheme_id)->delete();
}
// Add new scheme scheme parts.
$scheme_part_ids = $_POST['scheme_part_ids'] ?? array();
foreach ($scheme_part_ids as $scheme_part_id) {
    $table->create(['scheme_id' => $scheme_id, 'scheme_part_id' => $scheme_part_id]);