Пример #1
0
 /**
  * Returns the schemes that are linked to this scheme.
  *
  * @return \TableRecords\SchemePart[]
  */
 public function getSchemeParts() : array
 {
     if (!$this->scheme_parts) {
         $table = new \Tables\SchemeSchemeParts();
         $linking_records = $table->by('scheme_id', $this->id)->get();
         if (!$linking_records) {
             return [];
         }
         $scheme_part_ids = [];
         foreach ($linking_records as $record) {
             $scheme_part_ids[] = (int) $record->scheme_part_id;
         }
         $scheme_part_ids = array_filter($scheme_part_ids);
         if (!$scheme_part_ids) {
             return [];
         }
         $table = new \Tables\SchemeParts();
         foreach ($scheme_part_ids as $scheme_part_id) {
             $this->scheme_parts[] = $table->get($scheme_part_id);
         }
     }
     return array_reverse($this->scheme_parts);
     // Reverse as newest is displayed first.
 }
Пример #2
0
$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]);
}
$Ase->happen('cache_update', ['schemes']);
$Javascript->setOutput('id', $scheme_id);
$Javascript->setSuccessMessage(__('Scheme saved.'))->output();