english/administrator/components/com_categories/models/category.php
/**
	 * Batch copy categories to a new category.
	 *
	 * @param	int		$value	The new category or sub-item.
	 * @param	array	$pks	An array of row IDs.
	 *
	 * @return	booelan	True if successful, false otherwise and internal error is set.
	 * @since	1.6
	 */
Batch copy categories to a new category.
@param       int             $value  The new category or sub-item.
@param       array   $pks    An array of row IDs.

@return      booelan True if successful, false otherwise and internal error is set.
Batch copy categories to a new category . @param int $value The new category or sub - item . @param array $pks An array of row IDs . @return booelan True if successful false otherwise and internal error is set .
protected function batchCopy($value, $pks)
	{
		$parts		= explode('.', $value);
		$parentId	= (int) JArrayHelper::getValue($parts, 0, 1);
		$table	= $this->getTable();
		$db		= $this->getDbo();
		$user	= JFactory::getUser();
		$extension = JRequest::getWord('extension');
		if ($parentId) {
			if (!$table->load($parentId)) {
				if ($error = $table->getError()) {
					$this->setError($error);
					return false;
				}
				else {
					$this->setError(JText::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
					$parentId = 0;
				}
			}
			$canCreate = ($parentId == $table->getRootId()) ? $user->authorise('core.create', $extension) :
				$user->authorise('core.create', $extension.'.category.'.$parentId);
			if (!$canCreate) {
				$this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
				return false;
			}
		}
		if (empty($parentId)) {
			if (!$parentId = $table->getRootId()) {
				$this->setError($db->getErrorMsg());
				return false;
			}
			elseif (!$user->authorise('core.create', $extension)) {
				$this->setError(JText::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
				return false;
			}
		}
		$parents = array();
		$db->setQuery(
			'SELECT COUNT(id)' .
			' FROM #__categories'
		);
		$count = $db->loadResult();
		if ($error = $db->getErrorMsg()) {
			$this->setError($error);
			return false;
		}
		while (!empty($pks) && $count > 0)
		{
			$pk = array_shift($pks);
			$table->reset();
			if (!$table->load($pk)) {
				if ($error = $table->getError()) {
					$this->setError($error);
					return false;
				}
				else {
					$this->setError(JText::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
					continue;
				}
			}
			$db->setQuery(
				'SELECT id' .
				' FROM #__categories' .
				' WHERE lft > '.(int) $table->lft.' AND rgt < '.(int) $table->rgt
			);
			$childIds = $db->loadResultArray();
			foreach ($childIds as $childId)
			{
				if (!in_array($childId, $pks)) {
					array_push($pks, $childId);
				}
			}
			$oldId				= $table->id;
			$oldParentId		= $table->parent_id;
			$table->id			= 0;
			$table->parent_id	= isset($parents[$oldParentId]) ? $parents[$oldParentId] : $parentId;
			$table->setLocation($table->parent_id, 'last-child');
			$table->level		= null;
			$table->asset_id	= null;
			$table->lft			= null;
			$table->rgt			= null;
			list($title,$alias) = $this->generateNewTitle($table->parent_id, $table->alias, $table->title);
			$table->title   = $title;
			$table->alias   = $alias;
			if (!$table->store()) {
				$this->setError($table->getError());
				return false;
			}
			$parents[$oldId] = $table->id;
			$count--;
		}
		if (!$table->rebuild()) {
			$this->setError($table->getError());
			return false;
		}
		if (!$table->rebuildPath($table->id)) {
			$this->setError($table->getError());
			return false;
		}
		return true;
	}
protected function batchCopy ( $ value , $ pks ) { $ parts = explode ( '.' , $ value ) ; $ parentId = ( int ) JArrayHelper :: getValue ( $ parts , 0 , 1 ) ; $ table = $ this -> getTable ( ) ; $ db = $ this -> getDbo ( ) ; $ user = JFactory :: getUser ( ) ; $ extension = JRequest :: getWord ( 'extension' ) ; if ( $ parentId ) { if ( ! $ table -> load ( $ parentId ) ) { if ( $ error = $ table -> getError ( ) ) { $ this -> setError ( $ error ) ; return false ; } else { $ this -> setError ( JText :: _ ( 'JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND' ) ) ; $ parentId = 0 ; } } $ canCreate = ( $ parentId == $ table -> getRootId ( ) ) ? $ user -> authorise ( 'core.create' , $ extension ) : $ user -> authorise ( 'core.create' , $ extension . '.category.' . $ parentId ) ; if ( ! $ canCreate ) { $ this -> setError ( JText :: _ ( 'COM_CATEGORIES_BATCH_CANNOT_CREATE' ) ) ; return false ; } } if ( empty ( $ parentId ) ) { if ( ! $ parentId = $ table -> getRootId ( ) ) { $ this -> setError ( $ db -> getErrorMsg ( ) ) ; return false ; } elseif ( ! $ user -> authorise ( 'core.create' , $ extension ) ) { $ this -> setError ( JText :: _ ( 'COM_CATEGORIES_BATCH_CANNOT_CREATE' ) ) ; return false ; } } $ parents = array ( ) ; $ db -> setQuery ( 'SELECT COUNT(id)' . ' FROM #__categories' ) ; $ count = $ db -> loadResult ( ) ; if ( $ error = $ db -> getErrorMsg ( ) ) { $ this -> setError ( $ error ) ; return false ; } while ( ! empty ( $ pks ) && $ count > 0 ) { $ pk = array_shift ( $ pks ) ; $ table -> reset ( ) ; if ( ! $ table -> load ( $ pk ) ) { if ( $ error = $ table -> getError ( ) ) { $ this -> setError ( $ error ) ; return false ; } else { $ this -> setError ( JText :: sprintf ( 'JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND' , $ pk ) ) ; continue ; } } $ db -> setQuery ( 'SELECT id' . ' FROM #__categories' . ' WHERE lft > ' . ( int ) $ table -> lft . ' AND rgt < ' . ( int ) $ table -> rgt ) ; $ childIds = $ db -> loadResultArray ( ) ; foreach ( $ childIds as $ childId ) { if ( ! in_array ( $ childId , $ pks ) ) { array_push ( $ pks , $ childId ) ; } } $ oldId = $ table -> id ; $ oldParentId = $ table -> parent_id ; $ table -> id = 0 ; $ table -> parent_id = isset ( $ parents [ $ oldParentId ] ) ? $ parents [ $ oldParentId ] : $ parentId ; $ table -> setLocation ( $ table -> parent_id , 'last-child' ) ; $ table -> level = null ; $ table -> asset_id = null ; $ table -> lft = null ; $ table -> rgt = null ; list ( $ title , $ alias ) = $ this -> generateNewTitle ( $ table -> parent_id , $ table -> alias , $ table -> title ) ; $ table -> title = $ title ; $ table -> alias = $ alias ; if ( ! $ table -> store ( ) ) { $ this -> setError ( $ table -> getError ( ) ) ; return false ; } $ parents [ $ oldId ] = $ table -> id ; $ count -- ; } if ( ! $ table -> rebuild ( ) ) { $ this -> setError ( $ table -> getError ( ) ) ; return false ; } if ( ! $ table -> rebuildPath ( $ table -> id ) ) { $ this -> setError ( $ table -> getError ( ) ) ; return false ; } return true ; }
Batch copy categories to a new category.
Batch copy categories to a new category .
// $value comes as {parent_id}.{extension} // Check that the parent exists // Fatal error // Non-fatal error // Check that user has create permission for parent category // Error since user cannot create in parent category // If the parent is 0, set it to the ID of the root item in the tree // Make sure we can create in root // We need to log the parent ID // Calculate the emergency stop count as a precaution against a runaway loop bug // Parent exists so we let's proceed // Pop the first id off the stack // Check that the row actually exists // Fatal error // Not fatal error // Copy is a bit tricky, because we also need to copy the children // Add child ID's to the array only if they aren't already there. // Make a copy of the old ID and Parent ID // Reset the id because we are making a copy. // If we a copying children, the Old ID will turn up in the parents list // otherwise it's a new top level item // Set the new location in the tree for the node. // TODO: Deal with ordering?
 //$table->ordering	= 1; // Alter the title & alias // Store the row. // Now we log the old 'parent' to the new 'parent' // Rebuild the hierarchy. // Rebuild the tree path.