Beispiel #1
0
 /**
  * Method to perform batch operations on an item or a set of items.
  *
  * @param   array  $commands  An array of commands to perform.
  * @param   array  $pks       An array of item ids.
  * @param   array  $contexts  An array of item contexts.
  *
  * @return  boolean  Returns true on success, false on failure.
  *
  */
 public function batch($commands, $pks, $contexts)
 {
     // Sanitize user ids.
     $pks = array_unique($pks);
     JArrayHelper::toInteger($pks);
     // Remove any values of zero.
     if (array_search(0, $pks, true)) {
         unset($pks[array_search(0, $pks, true)]);
     }
     if (empty($pks)) {
         $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
         return false;
     }
     // Set some needed variables.
     $this->user = JFactory::getUser();
     $this->table = $this->getTable();
     $this->tableClassName = get_class($this->table);
     $this->contentType = new JUcmType();
     $this->type = $this->contentType->getTypeByTable($this->tableClassName);
     $this->batchSet = true;
     if ($this->type === false) {
         $type = new JUcmType();
         $this->type = $type->getTypeByAlias($this->typeAlias);
         $type_alias = $this->type->type_alias;
     } else {
         $type_alias = $this->type->type_alias;
     }
     $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags');
     $done = false;
     //Check box selected to copy items and then apply changes
     if (!empty($commands['copy_items']) and $commands['copy_items'] == '1') {
         $result = $this->batchCopy(0, $pks, $contexts);
         if (is_array($result)) {
             $pks = $result;
             // Build a new array of item contexts for the copied items
             $contexts = array();
             foreach ($pks as $pk) {
                 $contexts[$pk] = $this->context . '.' . $pk;
             }
         } else {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['category_id'])) {
         if (!$this->batchCategory($commands['category_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['assetgroup_id'])) {
         if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['tag'])) {
         if (!$this->batchTag($commands['tag'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['language_id'])) {
         if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!$done) {
         $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
         return false;
     }
     // Clear the cache
     $this->cleanCache();
     return true;
 }
 /**
  * Method to perform batch operations on an item or a set of items.
  *
  * @param   array  $commands  An array of commands to perform.
  * @param   array  $pks       An array of item ids.
  * @param   array  $contexts  An array of item contexts.
  *
  * @return  boolean  Returns true on success, false on failure.
  *
  * @since   12.2
  */
 public function batch($commands, $pks, $contexts)
 {
     // Sanitize ids.
     $pks = array_unique($pks);
     JArrayHelper::toInteger($pks);
     // Remove any values of zero.
     if (array_search(0, $pks, true)) {
         unset($pks[array_search(0, $pks, true)]);
     }
     if (empty($pks)) {
         $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
         return false;
     }
     $done = false;
     // Set some needed variables.
     $this->user = JFactory::getUser();
     $this->table = $this->getTable();
     $this->tableClassName = get_class($this->table);
     $this->contentType = new JUcmType();
     $this->type = $this->contentType->getTypeByTable($this->tableClassName);
     $this->canDo = DemoHelper::getActions('look');
     $this->batchSet = true;
     if (!$this->canDo->get('core.batch')) {
         $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
         return false;
     }
     if ($this->type == false) {
         $type = new JUcmType();
         $this->type = $type->getTypeByAlias($this->typeAlias);
     }
     $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags');
     if (!empty($commands['move_copy'])) {
         $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
         if ($cmd == 'c') {
             $result = $this->batchCopy($commands, $pks, $contexts);
             if (is_array($result)) {
                 foreach ($result as $old => $new) {
                     $contexts[$new] = $contexts[$old];
                 }
                 $pks = array_values($result);
             } else {
                 return false;
             }
         } elseif ($cmd == 'm' && !$this->batchMove($commands, $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!$done) {
         $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
         return false;
     }
     // Clear the cache
     $this->cleanCache();
     return true;
 }
Beispiel #3
0
 /**
  * Batch move categories to a new category.
  *
  * @param   integer  $value     The new category ID.
  * @param   array    $pks       An array of row IDs.
  * @param   array    $contexts  An array of item contexts.
  *
  * @return  boolean  True on success.
  *
  * @since   1.6
  */
 protected function batchMove($value, $pks, $contexts)
 {
     $parentId = (int) $value;
     $type = new JUcmType();
     $this->type = $type->getTypeByAlias($this->typeAlias);
     $db = $this->getDbo();
     $query = $db->getQuery(true);
     $extension = JFactory::getApplication()->input->get('extension', '', 'word');
     // Check that the parent exists.
     if ($parentId) {
         if (!$this->table->load($parentId)) {
             if ($error = $this->table->getError()) {
                 // Fatal error.
                 $this->setError($error);
                 return false;
             } else {
                 // Non-fatal error.
                 $this->setError(JText::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
                 $parentId = 0;
             }
         }
         // Check that user has create permission for parent category.
         if ($parentId == $this->table->getRootId()) {
             $canCreate = $this->user->authorise('core.create', $extension);
         } else {
             $canCreate = $this->user->authorise('core.create', $extension . '.category.' . $parentId);
         }
         if (!$canCreate) {
             // Error since user cannot create in parent category
             $this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
             return false;
         }
         // Check that user has edit permission for every category being moved
         // Note that the entire batch operation fails if any category lacks edit permission
         foreach ($pks as $pk) {
             if (!$this->user->authorise('core.edit', $extension . '.category.' . $pk)) {
                 // Error since user cannot edit this category
                 $this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_EDIT'));
                 return false;
             }
         }
     }
     // We are going to store all the children and just move the category
     $children = array();
     // Parent exists so let's proceed
     foreach ($pks as $pk) {
         // Check that the row actually exists
         if (!$this->table->load($pk)) {
             if ($error = $this->table->getError()) {
                 // Fatal error
                 $this->setError($error);
                 return false;
             } else {
                 // Not fatal error
                 $this->setError(JText::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
                 continue;
             }
         }
         // Set the new location in the tree for the node.
         $this->table->setLocation($parentId, 'last-child');
         // Check if we are moving to a different parent
         if ($parentId != $this->table->parent_id) {
             // Add the child node ids to the children array.
             $query->clear()->select('id')->from($db->quoteName('#__categories'))->where($db->quoteName('lft') . ' BETWEEN ' . (int) $this->table->lft . ' AND ' . (int) $this->table->rgt);
             $db->setQuery($query);
             try {
                 $children = array_merge($children, (array) $db->loadColumn());
             } catch (RuntimeException $e) {
                 $this->setError($e->getMessage());
                 return false;
             }
         }
         parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
         // Store the row.
         if (!$this->table->store()) {
             $this->setError($this->table->getError());
             return false;
         }
         // Rebuild the tree path.
         if (!$this->table->rebuildPath()) {
             $this->setError($this->table->getError());
             return false;
         }
     }
     // Process the child rows
     if (!empty($children)) {
         // Remove any duplicates and sanitize ids.
         $children = array_unique($children);
         JArrayHelper::toInteger($children);
     }
     return true;
 }
 /**
  * Method to perform batch operations on an item or a set of items.
  *
  * @param   array  $commands  An array of commands to perform.
  * @param   array  $pks       An array of item ids.
  * @param   array  $contexts  An array of item contexts.
  *
  * @return  boolean  Returns true on success, false on failure.
  *
  * @since   2.5
  */
 public function batch($commands, $pks, $contexts)
 {
     // Sanitize user ids.
     $pks = array_unique($pks);
     JArrayHelper::toInteger($pks);
     // Remove any values of zero.
     if (array_search(0, $pks, true)) {
         unset($pks[array_search(0, $pks, true)]);
     }
     if (empty($pks)) {
         $this->setError(JText::_('JGLOBAL_NO_ITEM_SELECTED'));
         return false;
     }
     $done = false;
     // Set some needed variables.
     $this->user = JFactory::getUser();
     $this->table = $this->getTable();
     $this->tableClassName = get_class($this->table);
     $this->contentType = new JUcmType();
     $this->type = $this->contentType->getTypeByTable($this->tableClassName);
     $this->batchSet = true;
     if ($this->type === false) {
         $type = new JUcmType();
         $this->type = $type->getTypeByAlias($this->typeAlias);
         $typeAlias = $this->type->type_alias;
     } else {
         $typeAlias = $this->type->type_alias;
     }
     $this->tagsObserver = $this->table->getObserverOfClass('JTableObserverTags');
     if (!empty($commands['category_id'])) {
         $cmd = JArrayHelper::getValue($commands, 'move_copy', 'c');
         if ($cmd == 'c') {
             $result = $this->batchCopy($commands['category_id'], $pks, $contexts);
             if (is_array($result)) {
                 $pks = $result;
             } else {
                 return false;
             }
         } elseif ($cmd == 'm' && !$this->batchMove($commands['category_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['assetgroup_id'])) {
         if (!$this->batchAccess($commands['assetgroup_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['language_id'])) {
         if (!$this->batchLanguage($commands['language_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!empty($commands['tag'])) {
         if (!$this->batchTag($commands['tag'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (strlen($commands['user_id']) > 0) {
         if (!$this->batchUser($commands['user_id'], $pks, $contexts)) {
             return false;
         }
         $done = true;
     }
     if (!$done) {
         $this->setError(JText::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION'));
         return false;
     }
     // Clear the cache
     $this->cleanCache();
     return true;
 }
Beispiel #5
0
 /**
  * Tests the getTypeByAlias() method
  *
  * @return  void
  *
  * @since   3.2
  */
 public function testGetTypeByAlias()
 {
     $this->assertEquals(1, $this->object->getTypeByAlias('com_content.article')->type_id, 'Articles are type_id = 1');
 }
Beispiel #6
0
 /**
  * Execute and display a template script.
  *
  * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
  *
  * @return  mixed  A string if successful, otherwise a Error object.
  *
  * @since   3.2
  */
 public function display($tpl = null)
 {
     $app = JFactory::getApplication();
     $document = JFactory::getDocument();
     $extension = $app->input->getString('option');
     $contentType = $extension . '.' . $this->viewName;
     $ucmType = new JUcmType();
     $ucmRow = $ucmType->getTypeByAlias($contentType);
     $ucmMapCommon = json_decode($ucmRow->field_mappings)->common;
     $createdField = null;
     $titleField = null;
     if (is_object($ucmMapCommon)) {
         $createdField = $ucmMapCommon->core_created_time;
         $titleField = $ucmMapCommon->core_title;
     } elseif (is_array($ucmMapCommon)) {
         $createdField = $ucmMapCommon[0]->core_created_time;
         $titleField = $ucmMapCommon[0]->core_title;
     }
     $document->link = JRoute::_(JHelperRoute::getCategoryRoute($app->input->getInt('id'), $language = 0, $extension));
     $app->input->set('limit', $app->get('feed_limit'));
     $siteEmail = $app->get('mailfrom');
     $fromName = $app->get('fromname');
     $feedEmail = $app->get('feed_email', 'author');
     $document->editor = $fromName;
     if ($feedEmail != 'none') {
         $document->editorEmail = $siteEmail;
     }
     // Get some data from the model
     $items = $this->get('Items');
     $category = $this->get('Category');
     foreach ($items as $item) {
         $this->reconcileNames($item);
         // Strip html from feed item title
         if ($titleField) {
             $title = $this->escape($item->{$titleField});
             $title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
         } else {
             $title = '';
         }
         // URL link to article
         $router = new JHelperRoute();
         $link = JRoute::_($router->getRoute($item->id, $contentType, null, null, $item->catid));
         // Strip HTML from feed item description text.
         $description = $item->description;
         $author = $item->created_by_alias ? $item->created_by_alias : $item->author;
         if ($createdField) {
             $date = isset($item->{$createdField}) ? date('r', strtotime($item->{$createdField})) : '';
         } else {
             $date = '';
         }
         // Load individual item creator class.
         $feeditem = new JFeedItem();
         $feeditem->title = $title;
         $feeditem->link = $link;
         $feeditem->description = $description;
         $feeditem->date = $date;
         $feeditem->category = $category->title;
         $feeditem->author = $author;
         // We don't have the author email so we have to use site in both cases.
         if ($feedEmail == 'site') {
             $feeditem->authorEmail = $siteEmail;
         } elseif ($feedEmail === 'author') {
             $feeditem->authorEmail = $item->author_email;
         }
         // Loads item information into RSS array
         $document->addItem($feeditem);
     }
 }
 /**
  * Batch copy categories to a new category.
  *
  * @param   integer  $value     The new category.
  * @param   array    $pks       An array of row IDs.
  * @param   array    $contexts  An array of item contexts.
  *
  * @return  mixed  An array of new IDs on success, boolean false on failure.
  *
  * @since   1.6
  */
 protected function batchCopy($value, $pks, $contexts)
 {
     $type = new JUcmType();
     $this->type = $type->getTypeByAlias($this->typeAlias);
     // $value comes as {parent_id}.{extension}
     $parts = explode('.', $value);
     $parentId = (int) JArrayHelper::getValue($parts, 0, 1);
     $db = $this->getDbo();
     $extension = JFactory::getApplication()->input->get('extension', '', 'word');
     $newIds = array();
     // Check that the parent exists
     if ($parentId) {
         if (!$this->table->load($parentId)) {
             if ($error = $this->table->getError()) {
                 // Fatal error
                 $this->setError($error);
                 return false;
             } else {
                 // Non-fatal error
                 $this->setError(JText::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
                 $parentId = 0;
             }
         }
         // Check that user has create permission for parent category
         if ($parentId == $this->table->getRootId()) {
             $canCreate = $this->user->authorise('core.create', 'com_tz_portfolio_plus');
         } else {
             $canCreate = $this->user->authorise('core.create', 'com_tz_portfolio_plus.category.' . $parentId);
         }
         if (!$canCreate) {
             // Error since user cannot create in parent category
             $this->setError(JText::_('COM_TZ_PORTFOLIO_PLUS_CATEGORIES_BATCH_CANNOT_CREATE'));
             return false;
         }
     }
     // If the parent is 0, set it to the ID of the root item in the tree
     if (empty($parentId)) {
         if (!($parentId = $this->table->getRootId())) {
             $this->setError($db->getErrorMsg());
             return false;
         } elseif (!$this->user->authorise('core.create', $extension)) {
             $this->setError(JText::_('COM_TZ_PORTFOLIO_PLUS_CATEGORIES_BATCH_CANNOT_CREATE'));
             return false;
         }
     }
     // We need to log the parent ID
     $parents = array();
     // Calculate the emergency stop count as a precaution against a runaway loop bug
     $query = $db->getQuery(true)->select('COUNT(id)')->from($db->quoteName('#__tz_portfolio_plus_categories'));
     $db->setQuery($query);
     try {
         $count = $db->loadResult();
     } catch (RuntimeException $e) {
         $this->setError($e->getMessage());
         return false;
     }
     // Parent exists so let's proceed
     while (!empty($pks) && $count > 0) {
         // Pop the first id off the stack
         $pk = array_shift($pks);
         $this->table->reset();
         // Check that the row actually exists
         if (!$this->table->load($pk)) {
             if ($error = $this->table->getError()) {
                 // Fatal error
                 $this->setError($error);
                 return false;
             } else {
                 // Not fatal error
                 $this->setError(JText::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
                 continue;
             }
         }
         // Copy is a bit tricky, because we also need to copy the children
         $query->clear()->select('id')->from($db->quoteName('#__tz_portfolio_plus_categories'))->where('lft > ' . (int) $this->table->lft)->where('rgt < ' . (int) $this->table->rgt);
         $db->setQuery($query);
         $childIds = $db->loadColumn();
         // Add child ID's to the array only if they aren't already there.
         foreach ($childIds as $childId) {
             if (!in_array($childId, $pks)) {
                 array_push($pks, $childId);
             }
         }
         // Make a copy of the old ID and Parent ID
         $oldId = $this->table->id;
         $oldParentId = $this->table->parent_id;
         // Reset the id because we are making a copy.
         $this->table->id = 0;
         // If we a copying children, the Old ID will turn up in the parents list
         // otherwise it's a new top level item
         $this->table->parent_id = isset($parents[$oldParentId]) ? $parents[$oldParentId] : $parentId;
         // Set the new location in the tree for the node.
         $this->table->setLocation($this->table->parent_id, 'last-child');
         // TODO: Deal with ordering?
         // $this->table->ordering	= 1;
         $this->table->level = null;
         $this->table->asset_id = null;
         $this->table->lft = null;
         $this->table->rgt = null;
         // Alter the title & alias
         list($title, $alias) = $this->generateNewTitle($this->table->parent_id, $this->table->alias, $this->table->title);
         $this->table->title = $title;
         $this->table->alias = $alias;
         // Unpublish because we are making a copy
         $this->table->published = 0;
         parent::createTagsHelper($this->tagsObserver, $this->type, $pk, $this->typeAlias, $this->table);
         // Store the row.
         if (!$this->table->store()) {
             $this->setError($this->table->getError());
             return false;
         }
         // Get the new item ID
         $newId = $this->table->get('id');
         // Add the new ID to the array
         $newIds[$pk] = $newId;
         // Now we log the old 'parent' to the new 'parent'
         $parents[$oldId] = $this->table->id;
         $count--;
     }
     // Rebuild the hierarchy.
     if (!$this->table->rebuild()) {
         $this->setError($this->table->getError());
         return false;
     }
     // Rebuild the tree path.
     if (!$this->table->rebuildPath($this->table->id)) {
         $this->setError($this->table->getError());
         return false;
     }
     return $newIds;
 }
Beispiel #8
0
 /**
  * Tests the getTypeByAlias() method
  *
  * @return  void
  *
  * @since   3.2
  */
 public function testGetTypeByAlias()
 {
     $this->markTestSkipped('The alias column is being casted to integer at present...');
     $this->assertEquals(1, $this->object->getTypeByAlias('com_content.article')->type_id, 'Articles are type_id = 1');
 }