/** * Batch copy items to a new category or current. * * @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 11.1 */ protected function batchCopy($value, $pks, $contexts) { $categoryId = (int) $value; $newIds = array(); if (!parent::checkCategoryId($categoryId)) { return false; } // Parent exists so we let's proceed while (!empty($pks)) { // 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('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Alter the title & alias $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->title); $this->table->title = $data['0']; $this->table->alias = $data['1']; // Reset the ID because we are making a copy $this->table->id = 0; // Reset hits because we are making a copy $this->table->hits = 0; // Unpublish because we are making a copy $this->table->state = 0; // New category ID $this->table->catid = $categoryId; // TODO: Deal with ordering? // $table->ordering = 1; // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); 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; } // Get the new item ID $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$pk] = $newId; } // Clean the cache $this->cleanCache(); return $newIds; }
/** * Batch copy items . * * @param integer $value Dummy to match the category in the JModelAdmin calls. * @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. * */ protected function batchCopy($value, $pks, $contexts) { $i = 0; $new_ids = array(); // Parent exists so we let's proceed while (!empty($pks)) { // 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('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Reset the ID because we are making a copy $this->table->id = 0; // Reset hits because we are making a copy $this->table->hits = 0; $this->table->alias = $this->generateUniqueAlias((array) $this->table); $this->table->name = $this->generateUniqueName((array) $this->table); // Set ordering to 0 so it is forced to be set later to last position $this->table->ordering = 0; $this->prepareTable($this->table); // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); 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; } // Get the new item ID $new_id = $this->table->get('id'); // Add the new ID to the array $new_ids[$pk] = $new_id; } // Clean the cache $this->cleanCache(); return $new_ids; }
/** * 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; }
/** * Batch copy items to a new category or current. * * @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 11.1 */ protected function batchCopy($value, $pks, $contexts) { $categoryId = (int) $value; $i = 0; if (!parent::checkCategoryId($categoryId)) { return false; } // Parent exists so we let's proceed while (!empty($pks)) { // 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('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Alter the title & alias $data = $this->generateNewTitle($categoryId, $this->table->alias, $this->table->title); $this->table->title = $data['0']; $this->table->alias = $data['1']; // Reset the ID because we are making a copy $this->table->id = 0; // New category ID $this->table->catid = $categoryId; // TODO: Deal with ordering? //$table->ordering = 1; // Get the featured state $featured = $this->table->featured; // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); 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; } // Get the new item ID $newId = $this->table->get('id'); // Add the new ID to the array $newIds[$i] = $newId; $i++; // Check if the article was featured and update the #__content_frontpage table if ($featured == 1) { $db = $this->getDbo(); $query = $db->getQuery(true)->insert($db->quoteName('#__content_frontpage'))->values($newId . ', 0'); $db->setQuery($query); $db->execute(); } } // Clean the cache $this->cleanCache(); return $newIds; }
/** * 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; }