/** * 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. }
<?php $validation_rules = ['id' => ['int', 'ID'], 'name' => ['presence', __('Name')]]; $Validator->validateArray($_POST, $validation_rules); if ($errors = $Validator->getErrors()) { return $Javascript->setErrors($errors)->output(); } $scheme_part_id = !empty($_POST['id']) ? (int) $_POST['id'] : null; $update = !!$scheme_part_id; $table = new \Tables\SchemeParts(); if ($update) { // May the current user edit this scheme part? $scheme_part = $table->by('user_id', getUser()->id)->get($scheme_part_id); if (!$scheme_part) { return $Javascript->setError(__('You are not authorized to edit this scheme part.'))->output(); } } if ($update) { $table->update($_POST, $scheme_part_id); } else { $data = $_POST; $data['user_id'] = getUser()->id; $scheme_part_id = $table->create($data); } # Update scheme part food table. $table = new \Tables\SchemePartFoods(); if ($update) { // Delete existing scheme part foods. $table->by('scheme_part_id', $scheme_part_id)->delete(); } // Add new scheme part foods.
<?php $page_title = __('Scheme parts'); $Crumbs->setRoot($page_title); $Template->output('header', ['title' => $page_title, 'nav_active' => 'scheme_parts']); $Template->output('page_controls', [['text' => __('New'), 'url' => 'scheme_parts/scheme_part']]); $Template->output('validator/status'); $Cache->cacheOutput(function ($data) { extract($data); $scheme_parts_table = new Tables\SchemeParts(); $scheme_parts = $scheme_parts_table->by('user_id', getUser()->id)->setOrderby('name ASC')->get(); ?> <table class="overview amounts_overview"> <?php $Template->output('tables/food_table_thead'); ?> <tbody> <?php foreach ($scheme_parts as $scheme_part) { $carbs = $scheme_part->getCarbs(); $proteins = $scheme_part->getProteins(); $fats = $scheme_part->getFats(); ?> <tr class="odf_href" data-href="scheme_parts/scheme_part?id=<?php echo $scheme_part->id; ?> "> <td><?php echo $scheme_part->name; ?> </td>
<?php $Validator->useSession(); setRedirect('referer'); $scheme_part_id = !empty($_GET['id']) ? (int) $_GET['id'] : null; $Validator->validate($scheme_part_id, 'int', 'ID*2'); if ($Validator->hasErrors()) { return; } $table = new Tables\SchemeParts(); $scheme_part = $table->by('user_id', getUser()->id)->get($scheme_part_id); if (!$scheme_part) { return $Validator->setError(__('Scheme part with given ID not found.')); } $table->delete($scheme_part_id); $Ase->happen('cache_update', ['scheme_parts']); $Validator->setSuccessMessage(__('Scheme part deleted.')); setRedirect('scheme_parts');
<?php $scheme_part_id = !empty($_GET['id']) ? (int) $_GET['id'] : null; $page_title = __(sprintf('%s scheme part', $scheme_part_id ? 'Edit' : 'New')); $scheme_part_foods = []; $Crumbs->addCrumb($page_title); $page_controls = [['text' => __('Back'), 'url' => $Crumbs->getPreviousPageUrl()]]; // Handle edit instead of new: if ($scheme_part_id) { $scheme_parts_table = new Tables\SchemeParts(); $scheme_part = $scheme_parts_table->get($scheme_part_id); if (!$scheme_part) { return $Template->output('error', 404); } elseif ($scheme_part->user_id != getUser()->id) { return $Template->output('error', 403); } $page_controls[] = ['text' => __('Delete'), 'url' => 'action/scheme_parts/delete?id=' . $scheme_part_id, 'confirm_message' => true]; $scheme_part_foods = $scheme_part->getFoods(); } $Template->output('header', ['title' => $page_title, 'nav_active' => 'scheme_parts']); $Template->output('page_controls', $page_controls); $Template->output('validator/status'); ?> <script src="js/page_specific/scheme_parts/scheme_part.js"></script> <form id="form-save_scheme_part" action="ajaxa/scheme_parts/save" method="post" class="odf_ajax"> <input type="hidden" name="id" value="<?php echo $scheme_part_id; ?> ">
<div id="scheme_scheme_parts_management" class="overviews_side_by_side"> <div id="scheme_parts"> <?php $Cache->cacheOutput(function ($args) { extract($args); $Template->output('tables/scheme_parts_overview', ['scheme_parts' => $scheme_parts, 'parent' => 'scheme', 'count_totals' => true, 'show_percentages_per_source' => true, 'include_scheme_part_id_inputs' => true]); }, compact('Template', 'scheme_parts'), 'schemes', getDefaultUniqueCacheData(['scheme_id' => $scheme_id])); ?> </div> <div id="available_scheme_parts"> <?php $Cache->cacheOutput(function ($args) { extract($args); $table = new Tables\SchemeParts(); $available_scheme_parts = $table->by('user_id', getUser()->id)->setOrderBy('name ASC')->get(); $Template->output('tables/scheme_parts_overview', ['scheme_parts' => $available_scheme_parts, 'parent' => 'scheme', 'show_percentages_per_source' => true]); }, compact('Template'), 'scheme_parts', getDefaultUniqueCacheData()); ?> </div> </div> <button type="submit"><?php echo __('Save'); ?> </button> </form> <?php $Template->output('footer');