/** * Saves an asset group * * @apiMethod POST * @apiUri /courses/assetgroup/save * @apiParameter { * "name": "id", * "description": "Asset group ID to edit", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "title", * "description": "Asset group title", * "type": "string", * "required": false, * "default": "New asset group" * } * @apiParameter { * "name": "state", * "description": "State of asset group", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "description", * "description": "Short description", * "type": "string", * "required": false, * "default": null * } * @apiParameter { * "name": "unit_id", * "description": "ID of parent unit", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "parent", * "description": "ID of parent asset group", * "type": "integer", * "required": false, * "default": null * } * @apiParameter { * "name": "params", * "description": "Parameters related to the asset group", * "type": "array", * "required": false, * "default": null * } * @return void */ public function saveTask() { // Require authentication and authorization $this->authorizeOrFail(); // Check for an incoming 'id' $id = Request::getInt('id', null); // Create an asset group instance $assetGroup = new Assetgroup($id); // Check to make sure we have an asset group object if (!is_object($assetGroup)) { App::abort(500, 'Failed to create an asset group object'); } // We'll always save the title again, even if it's just to the same thing $title = $assetGroup->get('title'); $title = !empty($title) ? $title : 'New asset group'; // Set our variables $assetGroup->set('title', Request::getString('title', $title)); $assetGroup->set('alias', strtolower(str_replace(' ', '', $assetGroup->get('title')))); // Save the asset group if (!$assetGroup->get('title')) { App::abort(400, 'No title provided'); } $state = Request::getInt('state', null); if (!is_null($state)) { $assetGroup->set('state', $state); } $assetGroup->set('description', Request::getVar('description', $assetGroup->get('description'))); // When creating a new asset group if (!$id) { $assetGroup->set('unit_id', Request::getInt('unit_id', 0)); $assetGroup->set('parent', Request::getInt('parent', 0)); $assetGroup->set('created', Date::toSql()); $assetGroup->set('created_by', App::get('authn')['user_id']); } if (($params = Request::getVar('params', false, 'post')) || !$id) { $p = new Registry(''); $db = App::get('db'); $query = $db->getQuery(true); $query->select('folder AS type, element AS name, params')->from('#__extensions')->where('enabled >= 1')->where('type =' . $db->quote('plugin'))->where('state >= 0')->where('folder =' . $db->quote('courses'))->order('ordering'); if ($plugins = $db->setQuery($query)->loadObjectList()) { foreach ($plugins as $plugin) { $default = new Registry($plugin->params); foreach ($default->toArray() as $k => $v) { if (substr($k, 0, strlen('default_')) == 'default_') { $p->set(substr($k, strlen('default_')), $default->get($k, $v)); } } } } if ($params) { $p->parse($params); } $assetGroup->set('params', $p->toString()); } // Save the asset group if (!$assetGroup->store()) { App::abort(500, 'Asset group save failed'); } // Return message $this->send(['assetgroup_id' => $assetGroup->get('id'), 'assetgroup_title' => $assetGroup->get('title'), 'assetgroup_state' => (int) $assetGroup->get('state'), 'assetgroup_style' => 'display:none', 'course_id' => $this->course_id, 'offering_alias' => $this->offering_alias], $id ? 200 : 201); }
/** * Load the editor * * @param array $config Associative array of editor config paramaters * @return void */ private function load($config = array()) { // Check if editor is already loaded if (!is_null($this->editor)) { return; } // Build the path to the needed editor plugin $name = (string) preg_replace('/[^A-Z0-9_\\.-]/i', '', $this->name); $name = ltrim($name, '.'); $path = PATH_CORE . DS . 'plugins' . DS . 'wiki' . DS . $name . DS . $name . '.php'; if (!is_file($path)) { throw new Exception(Lang::txt('Cannot load the editor'), 500); return false; } // Require plugin file require_once $path; // Get the plugin $plugin = Plugin::byType('wiki', $this->name); $params = new Registry($plugin->params); $params->toArray($config); $plugin->params = $params; // Build editor plugin classname $name = 'plgWiki' . $this->name; if ($this->editor = new $name($this, (array) $plugin)) { // Load plugin parameters $this->initialise(); } }
/** * Method to filter the form data. * * @param array $data An array of field values to filter. * @param string $group The dot-separated form group path on which to filter the fields. * @return mixed Array or false. */ public function filter($data, $group = null) { // Make sure there is a valid Form XML document. if (!$this->xml instanceof SimpleXMLElement) { return false; } // Initialise variables. $input = new Registry($data); $output = new Registry(); // Get the fields for which to filter the data. $fields = $this->findFieldsByGroup($group); if (!$fields) { // PANIC! return false; } // Filter the fields. foreach ($fields as $field) { // Initialise variables. $name = (string) $field['name']; // Get the field groups for the element. $attrs = $field->xpath('ancestor::fields[@name]/@name'); $groups = array_map('strval', $attrs ? $attrs : array()); $group = implode('.', $groups); // Get the field value from the data input. if ($group) { // Filter the value if it exists. if ($input->exists($group . '.' . $name)) { $output->set($group . '.' . $name, $this->filterField($field, $input->get($group . '.' . $name, (string) $field['default']))); } } else { // Filter the value if it exists. if ($input->exists($name)) { $output->set($name, $this->filterField($field, $input->get($name, (string) $field['default']))); } } } return $output->toArray(); }
/** * Method to get a single record. * * @param integer $pk The id of the primary key. * @return mixed Object on success, false on failure. */ public function getItem($pk = null) { // Initialise variables. $pk = !empty($pk) ? $pk : (int) $this->getState('plugin.id'); if (!isset($this->_cache[$pk])) { $false = false; // Get a row instance. $table = $this->getTable(); // Attempt to load the row. $return = $table->load($pk); // Check for a table object error. if ($return === false && $table->getError()) { $this->setError($table->getError()); return $false; } // Convert to the Object before adding other data. $properties = $table->getProperties(1); $this->_cache[$pk] = Arr::toObject($properties, '\\Hubzero\\Base\\Object'); // Convert the params field to an array. $registry = new Registry($table->params); $this->_cache[$pk]->params = $registry->toArray(); // Get the plugin XML. $path = array('app' => Filesystem::cleanPath(PATH_APP . DS . 'plugins' . DS . $table->folder . DS . $table->element . DS . $table->element . '.xml'), 'core' => Filesystem::cleanPath(PATH_CORE . DS . 'plugins' . DS . $table->folder . DS . $table->element . DS . $table->element . '.xml')); if (file_exists($path['app'])) { $this->_cache[$pk]->xml = \JFactory::getXML($path['app']); } else { if (file_exists($path['core'])) { $this->_cache[$pk]->xml = \JFactory::getXML($path['core']); } else { $this->_cache[$pk]->xml = null; } } } return $this->_cache[$pk]; }