/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $record = $this->getTable(); $key = $record->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; if ($pk > 0) { if ($record->load($pk)) { $is_new = false; } } if (!$is_new) { $data['project_id'] = $record->project_id; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $data['milestone_id'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Store the record if (parent::save($data)) { $id = $this->getState($this->getName() . '.id'); // Load the just updated row $updated = $this->getTable(); if ($updated->load($id) === false) { return false; } // Set the active project PFApplicationHelper::setActiveProject($updated->project_id); return true; } return false; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $old = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); $dispatcher = JDispatcher::getInstance(); // Allow an exception to be thrown. try { // Load the row if saving an existing record. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; $old = clone $table; } } if (!$is_new) { $data['project_id'] = $table->project_id; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $prev_access = $is_new ? 0 : $table->access; $access = PFAccessHelper::getViewLevelFromRules($data['rules'], $prev_access); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } $pk_name = $table->getKeyName(); if (isset($table->{$pk_name})) { $this->setState($this->getName() . '.id', $table->{$pk_name}); } $this->setState($this->getName() . '.new', $is_new); $id = $this->getState($this->getName() . '.id'); // Clean the cache. $this->cleanCache(); // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $record = $this->getTable(); $key = $record->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; if ($pk > 0) { if ($record->load($pk)) { $is_new = false; } } if (!$is_new) { $data['project_id'] = $record->project_id; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Store the record if (parent::save($data)) { $id = $this->getState($this->getName() . '.id'); // Load the just updated row $updated = $this->getTable(); if ($updated->load($id) === false) { return false; } // Set the active project PFApplicationHelper::setActiveProject($updated->project_id); // Add to watch list if ($is_new) { $cid = array($id); if (!$this->watch($cid, 1)) { return false; } } // Store the attachments if (isset($data['attachment']) && PFApplicationHelper::exists('com_pfrepo')) { $attachments = $this->getInstance('Attachments', 'PFrepoModel'); if (!$attachments->getState('item.type')) { $attachments->setState('item.type', 'com_pfforum.topic'); } if ($attachments->getState('item.id') == 0) { $attachments->setState('item.id', $id); } if ((int) $attachments->getState('item.project') == 0) { $attachments->setState('item.project', $updated->project_id); } if (!$attachments->save($data['attachment'])) { $this->setError($attachments->getError()); return false; } } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $updated->project_id); } $labels->setState('item.type', 'com_pfforum.topic'); $labels->setState('item.id', $id); if (!$labels->saveRefs($data['labels'])) { return false; } } return true; } return false; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); $dispatcher = JDispatcher::getInstance(); try { if ($pk > 0) { if ($table->load($pk)) { $is_new = false; } } if (!$is_new) { $data['project_id'] = $table->project_id; } if (!PFApplicationHelper::enabled('com_pfmilestones')) { $data['milestone_id'] = $is_new ? 0 : $table->milestone_id; } // Handle task completition meta info if (isset($data['complete'])) { $date = new JDate(); if ($is_new && $data['complete'] == '1') { $data['completed'] = $date->toSql(); $data['completed_by'] = JFactory::getUser()->id; } if (!$is_new) { if ($data['complete'] == '0') { $data['completed'] = JFactory::getDbo()->getNullDate(); $data['completed_by'] = '0'; } else { if (JFactory::getUser()->id != $table->completed_by) { $data['completed'] = $date->toSql(); $data['completed_by'] = JFactory::getUser()->id; } } } } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $data['milestone_id'], $data['list_id'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Try to convert estimate string to time if (isset($data['estimate'])) { if (!is_numeric($data['estimate'])) { $estimate_time = strtotime($data['estimate']); if ($estimate_time === false || $estimate_time <= 0) { $data['estimate'] = 1; } else { $data['estimate'] = $estimate_time - time(); } } else { // not a literal time, so convert minutes to secs $data['estimate'] = $data['estimate'] * 60; } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Make item priority 1 by default if not set if (!isset($data['priority']) && $is_new) { $data['priority'] = 1; } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } $pk_name = $table->getKeyName(); if (isset($table->{$pk_name})) { $this->setState($this->getName() . '.id', $table->{$pk_name}); } $this->setState($this->getName() . '.new', $is_new); $id = $this->getState($this->getName() . '.id'); // Load the just updated row $updated = $this->getTable(); if ($updated->load($id) === false) { return false; } // Set the active project PFApplicationHelper::setActiveProject($updated->project_id); // Store entered rate in session if (isset($data['rate']) && !empty($data['rate']) && $data['rate'] != '0.00') { JFactory::getApplication()->setUserState('com_projectfork.jform_rate', $data['rate']); } // Add to watch list if ($is_new) { $cid = array($id); if (!$this->watch($cid, 1)) { return false; } } // Store the attachments if (isset($data['attachment']) && PFApplicationHelper::exists('com_pfrepo')) { $attachments = $this->getInstance('Attachments', 'PFrepoModel'); if (!$attachments->getState('item.type')) { $attachments->setState('item.type', 'com_pftasks.task'); } if ($attachments->getState('item.id') == 0) { $attachments->setState('item.id', $this->getState($this->getName() . '.id')); } if ((int) $attachments->getState('item.project') == 0) { $attachments->setState('item.project', $updated->project_id); } if (!$attachments->save($data['attachment'])) { $this->setError($attachments->getError()); return false; } } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $updated->project_id); } $labels->setState('item.type', 'com_pftasks.task'); $labels->setState('item.id', $id); if (!$labels->saveRefs($data['labels'])) { return false; } } // Store the dependencies if (isset($data['dependency'])) { $taskrefs = $this->getInstance('TaskRefs', 'PFtasksModel'); if ((int) $taskrefs->getState('item.project') == 0) { $taskrefs->setState('item.project', $updated->project_id); } $taskrefs->setState('item.id', $id); if (!$taskrefs->save($data['dependency'])) { return false; } } // Store users if (isset($data['users'])) { $this->saveUsers($id, $data['users']); } // Clean the cache. $this->cleanCache(); // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to save an item * * @param array $data The item data * * @return boolean True on success, False on error */ public function save($data) { $dispatcher = JDispatcher::getInstance(); $date = JFactory::getDate(); $table = $this->getTable(); $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $old_path = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); // Load the row if saving an existing item. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; if (!empty($table->path)) { $old_path = $table->path; } } } // Prevent project id override for existing items if (!$is_new) { $data['project_id'] = $table->project_id; } // Make sure the title and alias are always unique list($title, $alias) = $this->generateNewTitle($data['parent_id'], $data['title'], '', $pk); $data['title'] = $title; $data['alias'] = $alias; // If we're not creating a new project repo... if (!$this->getState('create_repo')) { // Don't allow the creation of new folders in root if ($data['parent_id'] <= 1 && $is_new) { $this->setError(JText::_('COM_PROJECTFORK_ERROR_REPO_SAVE_ROOT_DIR')); return false; } // Don't allow new folders to be protected if (isset($data['protected'])) { $data['protected'] = 0; } } // Set the new parent id if parent id not matched OR while New/Save as Copy. if ($table->parent_id != $data['parent_id'] || $is_new) { // Fix: Folder cannot be parent of self if ($data['parent_id'] != $table->id) { $table->setLocation($data['parent_id'], 'last-child'); } else { $data['parent_id'] = $table->parent_id; } } // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } elseif (isset($data['access'])) { // Keep the existing access in the table unset($data['access']); } } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Add to watch list - if not opt-out if ($is_new) { $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); if (!$opt_out) { $cid = array($table->id); if (!$this->watch($cid, 1)) { return false; } } } // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel', $config = array()); $labels->getState('item.project'); $labels->setState('item.project', $table->project_id); $labels->setState('item.id', $table->id); if (!$labels->saveRefs($data['labels'], 'com_pfrepo.directory')) { return false; } } // Rebuild the path for the directory if (!$table->rebuildPath($table->id)) { $this->setError($table->getError()); return false; } // Update physical path if (!$is_new) { $this->savePhysical($table->project_id, $old_path, $table->path); } // Rebuild the paths of the directory children if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path)) { $this->setError($table->getError()); return false; } // Create physical path if ($is_new) { $this->savePhysical($table->project_id, $table->path); } // Set id state $this->setState($this->getName() . '.id', $table->id); // Clear the cache $this->cleanCache(); return true; }
/** * Method to save an item * * @param array $data The item data * * @return boolean True on success, False on error */ public function save($data) { $dispatcher = JDispatcher::getInstance(); $table = $this->getTable(); $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState($this->getName() . '.id'); $date = JFactory::getDate(); $is_new = true; $old_path = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); // Load the row if saving an existing item. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; if (!empty($table->path)) { $old_path = $table->path; } } else { $pk = 0; } } // Save revision if not new if (!$is_new) { $head_data = $table->getProperties(true); $config = array('ignore_request' => true); $rev_model = $this->getInstance('NoteRevision', 'PFrepoModel', $config); $head_data['parent_id'] = $head_data['id']; $head_data['id'] = null; if (!$rev_model->save($head_data)) { $this->setError($rev_model->getError()); return false; } } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['dir_id'], $data['title'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); $this->setState($this->getName() . '.id', $table->id); $updated = $this->getTable(); if ($updated->load($table->id) === false) { return false; } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $updated->project_id); } $labels->setState('item.type', 'com_pfrepo.note'); $labels->setState('item.id', $updated->id); if (!$labels->saveRefs($data['labels'])) { return false; } } // Clear the cache $this->cleanCache(); return true; }
/** * Method to save the form data. * * @param array $data The form data. * * @return boolean True on success, False on error. */ public function save($data) { $record = $this->getTable(); $key = $record->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $db = JFactory::getDbo(); $query = $db->getQuery(true); if ($pk > 0) { if ($record->load($pk)) { $is_new = false; } } // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Try to find the task title if (isset($data['task_id']) && $is_new) { $query->select('title')->from('#__pf_tasks')->where('id = ' . (int) $data['task_id']); $db->setQuery($query); $task_title = $db->loadResult(); if ($task_title) { $data['task_title'] = $task_title; } } // Try to convert estimate string to time if (isset($data['log_time'])) { if (!is_numeric($data['log_time'])) { $log_time = strtotime($data['log_time']); if ($log_time === false || $log_time <= 0) { $data['log_time'] = 1; } else { $data['log_time'] = $log_time - time(); } } else { // not a literal time, so convert minutes to secs $data['log_time'] = $data['log_time'] * 60; } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } if (parent::save($data)) { $id = $this->getState($this->getName() . '.id'); // Load the just updated row $updated = $this->getTable(); if ($updated->load($id) === false) { return false; } // Set the active project PFApplicationHelper::setActiveProject($updated->project_id); // Store entered rate in session if (isset($data['rate']) && !empty($data['rate']) && $data['rate'] != '0.00' && $is_new) { JFactory::getApplication()->setUserState('com_projectfork.jform_rate', $data['rate']); } return true; } return false; }
/** * Method to save the form data. * * @param array The form data * @return boolean True on success */ public function save($data) { $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $old = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); $dispatcher = JDispatcher::getInstance(); // Allow an exception to be thrown. try { if ($pk > 0) { if ($table->load($pk)) { $is_new = false; $old = clone $table; } } if (!$is_new) { $data['project_id'] = $table->project_id; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } $pk_name = $table->getKeyName(); if (isset($table->{$pk_name})) { $this->setState($this->getName() . '.id', $table->{$pk_name}); } $this->setState($this->getName() . '.new', $is_new); $id = $this->getState($this->getName() . '.id'); // Set the active project PFApplicationHelper::setActiveProject($table->project_id); // Add to watch list if ($is_new) { $cid = array($id); if (!$this->watch($cid, 1)) { return false; } } // Store the attachments if (isset($data['attachment']) && PFApplicationHelper::exists('com_pfrepo')) { $attachments = $this->getInstance('Attachments', 'PFrepoModel'); if (!$attachments->getState('item.type')) { $attachments->setState('item.type', 'com_pfmilestones.milestone'); } if ($attachments->getState('item.id') == 0) { $attachments->setState('item.id', $id); } if ((int) $attachments->getState('item.project') == 0) { $attachments->setState('item.project', $table->project_id); } if (!$attachments->save($data['attachment'])) { $this->setError($attachments->getError()); return false; } } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $table->project_id); } $labels->setState('item.type', 'com_pfmilestones.milestone'); $labels->setState('item.id', $id); if (!$labels->saveRefs($data['labels'])) { return false; } } // Clean the cache. $this->cleanCache(); // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $old = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); $dispatcher = JDispatcher::getInstance(); // Allow an exception to be thrown. try { // Load the row if saving an existing record. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; $old = clone $table; } } if (!$is_new) { $data['project_id'] = $table->project_id; $data['parent_id'] = $table->parent_id; } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Delete the old file if a new one is uploaded if (!$is_new && isset($data['file']['name'])) { $this->deleteFile($table->file_name, $table->project_id, $table->id); } // Use the file name as title if empty if (!isset($data['title'])) { $data['title'] = ''; } if ($data['title'] == '' && isset($data['file']['name'])) { $data['title'] = JFile::stripExt($data['file']['name']); } // Get the other file properties if (isset($data['file']['name'])) { $data['file_name'] = $data['file']['name']; } if (isset($data['file']['extension'])) { $data['file_extension'] = $data['file']['extension']; } if (isset($data['file']['size'])) { $data['file_size'] = $data['file']['size'] > 0 ? round($data['file']['size'] / 1024) : 0; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['project_id'], $data['parent_id'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $prev_access = $is_new ? 0 : $table->access; $access = PFAccessHelper::getViewLevelFromRules($data['rules'], $prev_access); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } $pk_name = $table->getKeyName(); if (isset($table->{$pk_name})) { $this->setState($this->getName() . '.id', $table->{$pk_name}); } $this->setState($this->getName() . '.new', $is_new); $id = $this->getState($this->getName() . '.id'); // Add to watch list if ($is_new) { $cid = array($id); if (!$this->watch($cid, 1)) { return false; } } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $table->project_id); } $labels->setState('item.type', 'com_pfdesigns.revision'); $labels->setState('item.id', $id); if (!$labels->saveRefs($data['labels'])) { return false; } } // Clean the cache. $this->cleanCache(); // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $record = $this->getTable(); $key = $record->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; if ($pk > 0) { if ($record->load($pk)) { $is_new = false; } } // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } if (!$is_new) { $data['project_id'] = $record->project_id; $data['topic_id'] = $record->topic_id; } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // On quick-save, access and publishing state are missing if ($this->getState('task') == 'quicksave' && isset($data['topic_id'])) { $db = JFactory::getDbo(); $query = $db->getQuery(true); $topic = (int) $data['topic_id']; if ($topic) { $query->select('state, access')->from('#__pf_topics')->where('id = ' . $db->quote($topic)); $db->setQuery((string) $query); $parent = $db->loadObject(); if ($parent) { $data['state'] = $parent->state; $data['access'] = $parent->access; } } } // Store the record if (parent::save($data)) { $id = $this->getState($this->getName() . '.id'); // Load the just updated row $updated = $this->getTable(); if ($updated->load($id) === false) { return false; } // Set the active project PFApplicationHelper::setActiveProject($updated->project_id); // Store the attachments if (isset($data['attachment']) && PFApplicationHelper::exists('com_pfrepo')) { $attachments = $this->getInstance('Attachments', 'PFrepoModel'); if (!$attachments->getState('item.type')) { $attachments->setState('item.type', 'com_pfforum.reply'); } if ($attachments->getState('item.id') == 0) { $attachments->setState('item.id', $id); } if ((int) $attachments->getState('item.project') == 0) { $attachments->setState('item.project', $updated->project_id); } if (!$attachments->save($data['attachment'])) { $this->setError($attachments->getError()); return false; } } return true; } return false; }
/** * Method to save the form data. * * @param array The form data * * @return boolean True on success */ public function save($data) { $user = JFactory::getUser(); $table = $this->getTable(); $key = $table->getKeyName(); $pk = !empty($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $is_new = true; $old = null; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); $dispatcher = JDispatcher::getInstance(); $cfg = JComponentHelper::getParams('com_pfprojects'); $create_group = (int) $cfg->get('create_group'); $group_location = (int) $cfg->get('group_location'); $group_id = 0; if (!$group_location) { $group_location = 1; } // Allow an exception to be thrown. try { // Load the row if saving an existing record. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; $old = clone $table; } } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['title'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Create new user group? if ($is_new && $create_group) { $group_users = array(JFactory::getUser()->get('id')); $group_id = $this->createUserGroup($data['title'], $group_location, $group_users); if ($group_id) { if (!isset($data['attribs'])) { $data['attribs'] = array(); } $data['attribs']['usergroup'] = $group_id; // Inject non-existant group if no rules are set if (!isset($data['rules'])) { $data['rules'] = array(0 => 0); } } } // Inject group into component rules $is_admin = $user->authorise('core.admin'); if (isset($data['component_rules']) && $is_new && $create_group) { foreach ($data['component_rules'] as $component => $rules) { foreach ($rules as $action => $groups) { if (!is_numeric($action) && is_array($groups)) { foreach ($groups as $gid => $v) { if ($gid == 0) { if (!$is_admin && $action == 'core.admin') { // Dont allow non-admins to inject core admin permission unset($data['component_rules'][$component][$action]); } else { if ($group_id) { unset($data['component_rules'][$component][$action][$gid]); $data['component_rules'][$component][$action][$group_id] = $v; } else { unset($data['component_rules'][$component][$action][$gid]); } } } } } } } } // Inject group into "add_groupuser" field if (isset($data['add_groupuser']) && $is_new && $create_group) { if (isset($data['add_groupuser'][0])) { $data['add_groupuser'][$group_id] = $data['add_groupuser'][0]; unset($data['add_groupuser'][0]); } } // Add users to groups if (isset($data['add_groupuser'])) { $this->addGroupUsers($data['add_groupuser']); } // Remove users from groups if (isset($data['rm_groupuser'])) { $this->removeGroupUsers($data['rm_groupuser']); } // Rename project group if (!$is_new && $create_group) { $reg = new JRegistry(); $reg->loadString($table->attribs); $group_id = (int) $reg->get('usergroup'); if ($group_id) { // Re-inject the group and other attribs if (!isset($data['attribs'])) { $data['attribs'] = $reg->toArray(); } else { $data['attribs']['usergroup'] = $group_id; } // Rename $this->renameUserGroup($group_id, $data['title']); } } // Handle permissions and access level if (isset($data['rules'])) { // Inject newly created group if ($is_new && $create_group) { foreach ($data['rules'] as $action => $groups) { if (is_numeric($action) && is_numeric($groups) && $groups == 0) { unset($data['rules'][$action]); if ($group_id) { $data['rules'][$action] = $group_id; } } if (!is_numeric($action) && is_array($groups)) { foreach ($groups as $gid => $v) { if ($gid == 0) { if ($group_id) { unset($data['rules'][$action][$gid]); $data['rules'][$action][$group_id] = $v; } else { unset($data['rules'][$action][$gid]); } } } } } } $prev_access = $is_new ? 0 : $table->access; $access = PFAccessHelper::getViewLevelFromRules($data['rules'], $prev_access); if ($access) { $data['access'] = $access; // If we created a new group, we must inject the new access level if ($create_group) { $user = JFactory::getUser(); $levels = $user->getAuthorisedViewLevels(); if (!in_array($access, $user->getAuthorisedViewLevels())) { $levels[] = (int) $access; $user->set('_authLevels', $levels); } } } } else { if ($is_new) { $data['access'] = (int) JFactory::getConfig()->get('access'); } else { if (isset($data['access'])) { unset($data['access']); } } } // Delete logo? if (isset($data['attribs']['logo']['delete']) && $pk && !$is_new) { $this->deleteLogo($pk); } // Make item published by default if new if (!isset($data['state']) && $is_new) { $data['state'] = 1; } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } $pk_name = $table->getKeyName(); if (isset($table->{$pk_name})) { $this->setState($this->getName() . '.id', $table->{$pk_name}); } $this->setState($this->getName() . '.new', $is_new); $id = $this->getState($this->getName() . '.id'); $this->setActive(array('id' => $id)); // Add to watch list - if not opt-out if ($is_new) { $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); if (!$opt_out) { $cid = array($id); if (!$this->watch($cid, 1)) { return false; } } } // Create repo base and attachments folder if (PFApplicationHelper::exists('com_pfrepo')) { if (!$this->createRepository($table)) { return false; } // Store the attachments if (isset($data['attachment']) && !$is_new) { $attachments = $this->getInstance('Attachments', 'PFrepoModel', array('ignore_request' => true)); $attachments->setState('item.type', 'com_pfprojects.project'); $attachments->setState('item.id', $id); $attachments->setState('item.project', $id); if (!$attachments->save($data['attachment'])) { $this->setError($attachments->getError()); return false; } } } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel', array('ignore_request' => true)); $lbl_project = (int) $labels->getState('item.project'); $labels->setState('item.project', $id); $labels->setState('item.id', $id); if (!$labels->save($data['labels'])) { /*$this->setError($labels->getError()); return false;*/ } } // Handle project logo if (!$this->saveLogo()) { return false; } // Clean the cache. $this->cleanCache(); // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); } catch (Exception $e) { $this->setError($e->getMessage()); return false; } return true; }
/** * Method to save an item * * @param array $data The item data * * @return boolean True on success, False on error */ public function save($data) { $dispatcher = JDispatcher::getInstance(); $table = $this->getTable(); $pk = !empty($data['id']) ? $data['id'] : (int) $this->getState($this->getName() . '.id'); $date = JFactory::getDate(); $is_new = true; // Include the content plugins for the on save events. JPluginHelper::importPlugin('content'); // Load the row if saving an existing item. if ($pk > 0) { if ($table->load($pk)) { $is_new = false; } else { $pk = 0; } } // Save revision if not new if (!$is_new && isset($data['file']['name'])) { // $this->deleteFile($table->file_name, $table->dir_id); $head_data = $table->getProperties(true); $config = array('ignore_request' => true); $rev_model = $this->getInstance('FileRevision', 'PFrepoModel', $config); $head_data['parent_id'] = $head_data['id']; $head_data['id'] = null; $head_data['created_by'] = JFactory::getUser()->id; if (!$rev_model->save($head_data)) { $this->setError($rev_model->getError()); return false; } // Change the title to the file name if (strrpos($data['title'], $data['file']['extension']) !== false) { $data['title'] = ''; } } // Use the file name as title if empty if ($data['title'] == '' && isset($data['file']['name'])) { $data['title'] = $data['file']['name']; } // Get the other file properties if (isset($data['file']['name'])) { $data['file_name'] = $data['file']['name']; } if (isset($data['file']['extension'])) { $data['file_extension'] = $data['file']['extension']; } if (isset($data['file']['size'])) { $data['file_size'] = $data['file']['size'] > 0 ? round($data['file']['size'] / 1024) : 0; } // Make sure the title and alias are always unique $data['alias'] = ''; list($title, $alias) = $this->generateNewTitle($data['dir_id'], $data['title'], $data['alias'], $pk); $data['title'] = $title; $data['alias'] = $alias; // Handle permissions and access level if (isset($data['rules'])) { $access = PFAccessHelper::getViewLevelFromRules($data['rules'], intval($data['access'])); if ($access) { $data['access'] = $access; } } else { if ($is_new) { // Let the table class find the correct access level $data['access'] = 0; } else { // Keep the existing access in the table if (isset($data['access'])) { unset($data['access']); } } } $new_dir = isset($data['dir_id']) ? (int) $data['dir_id'] : 0; // Move file to new location? if ($new_dir > 0 && !$is_new && $new_dir != $table->dir_id) { $pks = array($table->id); $contexts = array(); if (!$this->batchMove($new_dir, $pks, $contexts)) { return false; } } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the onContentBeforeSave event. $result = $dispatcher->trigger($this->event_before_save, array($this->option . '.' . $this->name, &$table, $is_new)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Trigger the onContentAfterSave event. $dispatcher->trigger($this->event_after_save, array($this->option . '.' . $this->name, &$table, $is_new)); $this->setState($this->getName() . '.id', $table->id); $updated = $this->getTable(); if ($updated->load($table->id) === false) { return false; } // Store the labels if (isset($data['labels'])) { $labels = $this->getInstance('Labels', 'PFModel'); if ((int) $labels->getState('item.project') == 0) { $labels->setState('item.project', $updated->project_id); } $labels->setState('item.type', 'com_pfrepo.file'); $labels->setState('item.id', $table->id); if (!$labels->saveRefs($data['labels'])) { return false; } } // Clear the cache $this->cleanCache(); return true; }