/** * Method to get a list of user id's which are observing the item * * @param string $context The item context * @param object $table Instance of the item table * @param boolean $is_new True if the item is new * * @return array */ public static function getObservers($context, $table, $is_new = false) { $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->select('a.user_id')->from('#__pf_ref_observer AS a')->where('a.item_type = ' . $db->quote('com_pfprojects.project'))->where('a.item_id = ' . $db->quote((int) $table->id)); $db->setQuery($query); $users = (array) $db->loadColumn(); if ($opt_out) { $blacklist = $users; $users = array(); $groups = PFAccessHelper::getGroupsByAccessLevel($table->access); if (!count($groups)) { return array(); } $query->clear()->select('a.user_id')->from('#__user_usergroup_map AS a')->innerJoin('#__users AS u ON u.id = a.user_id'); if (count($blacklist)) { $query->where('a.user_id NOT IN(' . implode(', ', $blacklist) . ')'); } $query->where('a.group_id IN(' . implode(', ', $groups) . ')')->group('a.user_id')->order('a.user_id ASC'); $db->setQuery($query); $users = (array) $db->loadColumn(); } return $users; }
/** * Generates a list of JSON items. * * @return void */ public function display($tpl = null) { $user = JFactory::getUser(); $access = JRequest::getUInt('filter_access'); // No access if not logged in if ($user->id == 0) { JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR')); return false; } // Check Access for non-admins if (!$user->authorise('core.admin')) { $allowed = PFAccessHelper::getGroupsByAccessLevel($access, true); $groups = $user->getAuthorisedGroups(); $can_access = false; foreach ($groups as $group) { if (in_array($group, $allowed)) { $can_access = true; break; } } if (!$can_access) { JError::raiseError(403, JText::_('JERROR_ALERTNOAUTHOR')); return false; } } $this->items = $this->get('Items'); // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseWarning(500, implode("\n", $errors)); return false; } parent::display($tpl); }
/** * Build an SQL query to load the list data. * * @return jdatabasequery */ protected function getListQuery() { $query = $this->_db->getQuery(true); // Get possible filters $filter_search = $this->getState('filter.search'); $filter_access = (int) $this->getState('filter.access'); $filter_groups = PFAccessHelper::getGroupsByAccessLevel($filter_access, true); if (!count($filter_groups)) { return $query; } // Select the required fields from the table. $query->select($this->getState('list.select', 'a.id, a.username, a.name')); $query->from('#__users AS a'); // Join on user groups $query->join('INNER', '#__user_usergroup_map AS m ON m.user_id = a.id'); $query->where('m.group_id IN(' . implode(',', $filter_groups) . ')'); // Filter by search if (!empty($filter_search)) { if (stripos($filter_search, 'id:') === 0) { $query->where('a.id = ' . (int) substr($filter_search, 3)); } else { $search = $this->_db->quote('%' . $this->_db->escape($filter_search, true) . '%'); $query->where('(a.name LIKE ' . $search . ' OR a.username LIKE ' . $search . ')'); } } $order_col = $this->state->get('list.ordering', 'a.username'); $order_dir = $this->state->get('list.direction', 'asc'); $query->group('a.id'); $query->order($this->_db->escape($order_col . ' ' . $order_dir)); return $query; }
/** * 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 update the access level of all milestones * associated with the given project * * @param integer $project The project * @param integer $access The access level * * @return void */ protected function updateAccess($project, $access) { jimport('projectfork.library'); $db = JFactory::getDbo(); $query = $db->getQuery(true); $allowed = PFAccessHelper::getAccessTree($access); $query->update('#__pf_milestones')->set('access = ' . (int) $access)->where('project_id = ' . (int) $project); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); }
/** * 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 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 update the access level of all albums, designs and revs * associated with the given context * * @param string $context The context name * @param integer $id The context id * @param integer $access The access level * * @return void */ protected function updateAccess($context, $id, $access) { jimport('projectfork.library'); $db = JFactory::getDbo(); $query = $db->getQuery(true); $allowed = PFAccessHelper::getAccessTree($access); $fields = array('com_pfprojects.project' => 'project_id', 'com_pfdesigns.album' => 'album_id', 'com_pfdesigns.design' => 'parent_id'); // Update albums if ($context == 'com_pfprojects.project') { // Update tasks $query->update('#__pf_design_albums')->set('access = ' . (int) $access)->where($fields[$context] . ' = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } // Update designs if (in_array($context, array('com_pfprojects.project', 'com_pfdesigns.album'))) { $query->clear(); $query->update('#__pf_designs')->set('access = ' . (int) $access)->where($fields[$context] . ' = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } // Update revisions if (in_array($context, array('com_pfprojects.project', 'com_pfdesigns.design'))) { $query->clear(); $query->update('#__pf_design_revisions')->set('access = ' . (int) $access)->where($fields[$context] . ' = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } elseif ($context == 'com_pfdesigns.album') { $query->clear(); $query->select('id')->from('#__pf_designs')->where($fields[$context] . ' = ' . (int) $id); $db->setQuery($query); $designs = (array) $db->loadColumn(); if (!count($designs)) { return; } $query->clear(); $query->update('#__pf_design_revisions')->set('access = ' . (int) $access)->where('parent_id IN(' . implode(', ', $designs) . ')'); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } }
/** * 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; }
/** * Method to update the access level of topics and replies * associated with the given context * * @param string $context The context name * @param integer $id The context id * @param integer $access The access level * * @return void */ protected function updateAccess($context, $id, $access) { jimport('projectfork.library'); $db = JFactory::getDbo(); $query = $db->getQuery(true); $allowed = PFAccessHelper::getAccessTree($access); if ($context == 'com_pfrepo.directory') { $query->select('lft, rgt')->from('#__pf_repo_dirs')->where('id = ' . (int) $id); $db->setQuery($query); $dir = $db->loadObject(); if (empty($dir)) { return; } // Get sub dirs $query->clear()->select('id')->from('#__pf_repo_dirs')->where('lft > ' . (int) $dir->lft)->where('rgt < ' . (int) $dir->rgt); $db->setQuery($query); $dirs = (array) $db->loadColumn(); $dirs[] = (int) $id; // Update sub-dirs $query->clear(); $query->update('#__pf_repo_dirs')->set('access = ' . (int) $access)->where('lft > ' . (int) $dir->lft)->where('rgt < ' . (int) $dir->rgt); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); // Update notes and files foreach ($dirs as $pk) { // Update notes $query->clear(); $query->update('#__pf_repo_notes')->set('access = ' . (int) $access)->where('dir_id = ' . (int) $pk); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); // Update files $query->clear(); $query->update('#__pf_repo_files')->set('access = ' . (int) $access)->where('dir_id = ' . (int) $pk); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } } else { // Update dirs $query->clear(); $query->update('#__pf_repo_dirs')->set('access = ' . (int) $access)->where('project_id = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); // Update notes $query->clear(); $query->update('#__pf_repo_notes')->set('access = ' . (int) $access)->where('project_id = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); // Update files $query->clear(); $query->update('#__pf_repo_files')->set('access = ' . (int) $access)->where('project_id = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN(' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); } }
/** * 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 get a list of user id's which are observing the item * * @param string $context The item context * @param object $table Instance of the item table * @param boolean $is_new True if the item is new * * @return array */ public static function getObservers($context, $table, $is_new = false) { if (!$is_new) { return array(); } $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); $db = JFactory::getDbo(); $query = $db->getQuery(true); $task_ms = 0; if ($table->context == 'com_pftasks.task') { $query->select('milestone_id')->from('#__pf_tasks')->where('id = ' . (int) $table->item_id); $db->setQuery($query); $task_ms = (int) $db->loadResult(); } $query->clear(); $query->select('a.user_id')->from('#__pf_ref_observer AS a'); if ($table->context != 'com_pfprojects.form' && $table->context != 'com_pfprojects.project' && isset($table->project_id)) { if ($task_ms) { $query->where('(a.item_type = ' . $db->quote($db->escape($table->context)) . ' AND a.item_id = ' . $db->quote((int) $table->item_id) . ')' . ' OR (a.item_type = ' . $db->quote('com_pfmilestones.milestone') . ' AND a.item_id = ' . $task_ms . ')' . ' OR (a.item_type = ' . $db->quote('com_pfprojects.project') . ' AND a.item_id = ' . $table->project_id . ')'); } else { $query->where('(a.item_type = ' . $db->quote($db->escape($table->context)) . ' AND a.item_id = ' . $db->quote((int) $table->item_id) . ')' . ' OR (a.item_type = ' . $db->quote('com_pfprojects.project') . ' AND a.item_id = ' . $table->project_id . ')'); } } else { $query->where('a.item_type = ' . $db->quote($db->escape($table->context)))->where('a.item_id = ' . $db->quote((int) $table->item_id)); } $db->setQuery($query); $users = (array) $db->loadColumn(); if ($opt_out) { $blacklist = $users; $users = array(); $tables = array('com_pfprojects.project' => '#__pf_projects', 'com_pfmilestones.milestone' => '#__pf_milestones', 'com_pftasks.task' => '#__pf_tasks', 'com_pfdesigns.design' => '#__pf_designs', 'com_pfdesigns.revision' => '#__pf_designs', 'com_pfrepo.file' => '#__pf_repo_files', 'com_pfrepo.note' => '#__pf_repo_notes'); if (!isset($tables[$table->context])) { return array(); } $q_table = $tables[$table->context]; $q_id = (int) $table->item_id; if ($table->context == 'com_pfdesigns.revision') { $query->clear()->select('parent_id')->from('#__pf_design_revisions')->where('id = ' . $q_id); $db->setQuery($query); $q_id = (int) $db->loadResult(); } if (!$q_id) { return array(); } $query->clear()->select('access')->from($q_table)->where('id = ' . $q_id); $db->setQuery($query); $item_access = (int) $db->loadResult(); $item_groups = PFAccessHelper::getGroupsByAccessLevel($item_access); $query->clear()->select('access')->from('#__pf_projects')->where('id = ' . (int) $table->project_id); $db->setQuery($query); $project_access = $db->loadResult(); $p_groups = PFAccessHelper::getGroupsByAccessLevel($project_access); $groups = array_unique(array_merge($p_groups, $item_groups)); if (!count($groups)) { return array(); } $query->clear()->select('a.user_id')->from('#__user_usergroup_map AS a')->innerJoin('#__users AS u ON u.id = a.user_id'); if (count($blacklist)) { $query->where('a.user_id NOT IN(' . implode(', ', $blacklist) . ')'); } $query->where('a.group_id IN(' . implode(', ', $groups) . ')')->group('a.user_id')->order('a.user_id ASC'); $db->setQuery($query); $users = (array) $db->loadColumn(); } return $users; }
/** * 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 get a list of user id's which are observing the item * * @param string $context The item context * @param object $table Instance of the item table * @param boolean $is_new True if the item is new * * @return array */ public static function getObservers($context, $table, $is_new = false) { $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); $db = JFactory::getDbo(); $query = $db->getQuery(true); // Get observers $query->select('a.user_id')->from('#__pf_ref_observer AS a')->where('(' . 'a.item_type = ' . $db->quote('com_pftasks.task') . ' AND a.item_id = ' . (int) $table->id . ')' . ($table->milestone_id > 0 ? ' OR (' . 'a.item_type = ' . $db->quote('com_pfmilestones.milestone') . ' AND a.item_id = ' . (int) $table->milestone_id . ')' : '') . ' OR (' . 'a.item_type = ' . $db->quote('com_pfprojects.project') . ' AND a.item_id = ' . (int) $table->project_id . ')'); $db->setQuery($query); $users = (array) $db->loadColumn(); // Get assigned users $query->clear()->select('user_id')->from('#__pf_ref_users')->where('item_type = ' . $db->quote('com_pftasks.task'))->where('item_id = ' . (int) $table->id); $db->setQuery($query); $assigned = (array) $db->loadColumn(); $return = array_merge($users, $assigned); if ($opt_out) { $blacklist = $users; $t_groups = PFAccessHelper::getGroupsByAccessLevel($table->access); if ($table->milestone_id > 0) { $query->clear()->select('access')->from('#__pf_milestones')->where('id = ' . (int) $table->milestone_id); $db->setQuery($query); $ms_access = $db->loadResult(); $ms_groups = PFAccessHelper::getGroupsByAccessLevel($ms_access); } else { $ms_groups = array(); } $query->clear()->select('access')->from('#__pf_projects')->where('id = ' . (int) $table->project_id); $db->setQuery($query); $project_access = $db->loadResult(); $p_groups = PFAccessHelper::getGroupsByAccessLevel($project_access); $groups = array_unique(array_merge($t_groups, $p_groups, $ms_groups)); if (!count($groups)) { return array(); } $query->clear()->select('a.user_id')->from('#__user_usergroup_map AS a')->innerJoin('#__users AS u ON u.id = a.user_id'); if (count($blacklist)) { $query->where('a.user_id NOT IN(' . implode(', ', $blacklist) . ')'); } $query->where('a.group_id IN(' . implode(', ', $groups) . ')')->group('a.user_id')->order('a.user_id ASC'); $db->setQuery($query); $return = (array) $db->loadColumn(); } return $return; }
/** * 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['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) { $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 set the "access" field value of a record * * @param mixed $old The current value * @param mixed $new The new value * * @return mixed */ public function setAccessValue($old = null, $new = null) { $allowed = PFAccessHelper::getAccessTree($new); return in_array($old, $allowed) ? $old : $new; }
/** * Method to get the filtering groups (null means no filtering) * * @return mixed $groups Array of filtering groups */ protected function getGroups() { static $groups = ''; if ($groups !== '') { return $groups; } $access = (int) $this->form->getValue('access'); if (!$access) { return ''; } $groups = array(); $group_list = (array) PFAccessHelper::getGroupsByAccessLevel($access); foreach ($group_list as $group) { $groups[] = (int) $group; } if (!count($groups)) { $groups = ''; } return $groups; }
/** * 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 update the access level of topics and replies * associated with the given context * * @param string $context The context name * @param integer $id The context id * @param integer $access The access level * * @return void */ protected function updateAccess($context, $id, $access) { jimport('projectfork.library'); $db = JFactory::getDbo(); $query = $db->getQuery(true); $allowed = PFAccessHelper::getAccessTree($access); $fields = array('com_pfprojects.project' => 'project_id', 'com_pfforum.topic' => 'topic_id'); // Update replies $query->update('#__pf_replies')->set('access = ' . (int) $access)->where($fields[$context] . ' = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); if ($context == 'com_pfforum.topic') { return; } // Update topics $query->clear(); $query->update('#__pf_topics')->set('access = ' . (int) $access)->where($fields[$context] . ' = ' . (int) $id); if (count($allowed) == 1) { $query->where('access <> ' . (int) $allowed[0]); } elseif (count($allowed) > 1) { $query->where('access NOT IN( ' . implode(', ', $allowed) . ')'); } $db->setQuery($query); $db->execute(); }
/** * Method to get a list of user id's which are observing the item * * @param string $context The item context * @param object $table Instance of the item table * @param boolean $is_new True if the item is new * * @return array */ public static function getObservers($context, $table, $is_new = false) { $plugin = JPluginHelper::getPlugin('content', 'pfnotifications'); $params = new JRegistry($plugin->params); $opt_out = (int) $params->get('sub_method', 0); $db = JFactory::getDbo(); $query = $db->getQuery(true); if (in_array($context, array('com_pfrepo.file', 'com_pfrepo.fileform', 'com_pfrepo.note', 'com_pfrepo.noteform'))) { $parents = self::getParentDirectories($table->dir_id); $query = $db->getQuery(true); $query->select('a.user_id')->from('#__pf_ref_observer AS a'); $query->where('(' . 'a.item_type = ' . $db->quote('com_pfrepo.directory') . ' AND a.item_id IN(' . implode(', ', $parents) . ') ' . ')' . ' OR (' . 'a.item_type = ' . $db->quote('com_pfprojects.project') . ' AND a.item_id = ' . (int) $table->project_id . ')'); } else { if (isset($table->item_type) && isset($table->item_id) && !$is_new) { $query = $db->getQuery(true); $query->select('a.user_id')->from('#__pf_ref_observer AS a'); $query->where('a.item_type = ' . $db->quote($db->escape($table->item_type)))->where('a.item_id = ' . $db->quote((int) $table->item_id)); } else { return array(); } } $db->setQuery($query); $users = (array) $db->loadColumn(); if ($opt_out) { $blacklist = $users; $users = array(); $repo_groups = array(); if (isset($table->access)) { $repo_groups = PFAccessHelper::getGroupsByAccessLevel($table->access); } $query->clear()->select('access')->from('#__pf_projects')->where('id = ' . (int) $table->project_id); $db->setQuery($query); $project_access = $db->loadResult(); $p_groups = PFAccessHelper::getGroupsByAccessLevel($project_access); $groups = array_unique(array_merge($p_groups, $repo_groups)); if (!count($groups)) { return array(); } $query->clear()->select('a.user_id')->from('#__user_usergroup_map AS a')->innerJoin('#__users AS u ON u.id = a.user_id'); if (count($blacklist)) { $query->where('a.user_id NOT IN(' . implode(', ', $blacklist) . ')'); } $query->where('a.group_id IN(' . implode(', ', $groups) . ')')->group('a.user_id')->order('a.user_id ASC'); $db->setQuery($query); $users = (array) $db->loadColumn(); } return $users; }