/** * Save custom description schema * * @return void */ public function saveDescriptionTask() { // Check for request forgeries Request::checkToken(); if (!User::authorise('core.manage', $this->_option) && !User::authorise('core.admin', $this->_option)) { return $this->cancelTask(); } // Incoming data $description = json_decode(Request::getVar('descriptionFields', '{}', 'post', 'none', 2)); // Get the old schema $fields = Field::all()->including(['options', function ($option) { $option->select('*')->ordered(); }])->ordered()->rows(); // Collect old fields $oldFields = array(); foreach ($fields as $oldField) { $oldFields[$oldField->get('id')] = $oldField; } foreach ($description->fields as $i => $element) { $field = null; $fid = isset($element->field_id) ? $element->field_id : 0; if ($fid && isset($oldFields[$fid])) { $field = $oldFields[$fid]; // Remove found fields from the list // Anything remaining will be deleted unset($oldFields[$fid]); } $field = $field ?: Field::oneOrNew($fid); $field->set(array('type' => (string) $element->field_type, 'label' => (string) $element->label, 'name' => (string) $element->name, 'description' => isset($element->field_options->description) ? (string) $element->field_options->description : '', 'ordering' => $i + 1, 'access' => isset($element->access) ? (int) $element->access : 0, 'option_other' => isset($element->field_options->include_other_option) ? (int) $element->field_options->include_other_option : '', 'option_blank' => isset($element->field_options->include_blank_option) ? (int) $element->field_options->include_blank_option : '', 'action_create' => isset($element->create) ? (int) $element->create : 1, 'action_update' => isset($element->update) ? (int) $element->update : 1, 'action_edit' => isset($element->edit) ? (int) $element->edit : 1)); if ($field->get('type') == 'dropdown') { $field->set('type', 'select'); } if ($field->get('type') == 'paragraph') { $field->set('type', 'textarea'); } if (!$field->save()) { Notify::error($field->getError()); continue; } // Collect old options $oldOptions = array(); foreach ($field->options as $oldOption) { $oldOptions[$oldOption->get('id')] = $oldOption; } // Does this field have any set options? if (isset($element->field_options->options)) { foreach ($element->field_options->options as $k => $opt) { $option = null; $oid = isset($opt->field_id) ? $opt->field_id : 0; if ($oid && isset($oldOptions[$oid])) { $option = $oldOptions[$oid]; // Remove found options from the list // Anything remaining will be deleted unset($oldOptions[$oid]); } $dependents = array(); if (isset($opt->dependents)) { $dependents = explode(',', trim($opt->dependents)); $dependents = array_map('trim', $dependents); foreach ($dependents as $j => $dependent) { if (!$dependent) { unset($dependents[$j]); } } } $option = $option ?: Option::oneOrNew($oid); $option->set(array('field_id' => $field->get('id'), 'label' => (string) $opt->label, 'value' => isset($opt->value) ? (string) $opt->value : '', 'checked' => isset($opt->checked) ? (int) $opt->checked : 0, 'ordering' => $k + 1, 'dependents' => json_encode($dependents))); if (!$option->save()) { Notify::error($option->getError()); continue; } } } // Remove any options not in the incoming list foreach ($oldOptions as $option) { if (!$option->destroy()) { Notify::error($option->getError()); continue; } } } // Remove any fields not in the incoming list foreach ($oldFields as $field) { if (!$field->destroy()) { Notify::error($field->getError()); continue; } } // Set success message Notify::success(Lang::txt('COM_PROJECTS_DESCRIPTION_SCHEMA_SAVED')); // Drop through to edit form? if ($this->getTask() == 'applydescription') { // Redirect, instead of falling through, to avoid caching issues App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&task=customizeDescription', false)); } // Redirect $this->cancelTask(); }
/** * Edit project * * @return void */ public function editTask() { // Check that project exists if (!$this->model->exists() || $this->model->isDeleted()) { throw new Exception(Lang::txt('COM_PROJECTS_PROJECT_CANNOT_LOAD'), 404); } // Check if project is in setup if ($this->model->inSetup()) { App::redirect(Route::url($this->model->link('setup'))); } // Only managers can edit project if (!$this->model->access('manager') && !($this->model->access('content') && $this->config->get('edit_description'))) { throw new Exception(Lang::txt('ALERTNOTAUTH'), 403); } // Which section are we editing? $sections = array('info', 'team', 'settings'); if ($this->config->get('edit_settings', 0) == 0) { array_pop($sections); } $this->section = in_array($this->section, $sections) ? $this->section : 'info'; if ($this->config->get('custom_profile') == 'custom' && ($this->section == 'info' || $this->section == 'info_custom')) { //$sections = array('info_custom', 'team', 'settings'); $this->view->fields = Field::all()->order('ordering', 'ASC'); $projectID = $this->model->get('id'); // Load project values $projectData = Description::all()->where('project_id', '=', $projectID); $data = Description::collect($projectData); $this->view->data = $data; $this->section = 'info_custom'; } // Set the pathway $this->_buildPathway(); // Set the page title $this->_buildTitle(); $this->view->setLayout('edit'); if ($this->section == 'team') { $this->view->content = $this->_loadTeamEditor(); } // Output HTML $this->view->model = $this->model; $this->view->uid = User::get('id'); $this->view->section = $this->section; $this->view->sections = $sections; $this->view->title = $this->title; $this->view->option = $this->_option; $this->view->config = $this->config; $this->view->task = $this->_task; $this->view->publishing = $this->_publishing; $this->view->active = 'edit'; // Get messages and errors $error = $this->getError() ? $this->getError() : $this->_getNotifications('error'); if ($error) { $this->view->setError($error); } $this->view->msg = $this->_getNotifications('success'); $this->view->display(); }