Пример #1
0
 /**
  * Method to delete a node and, optionally, its child nodes from the table.
  *
  * @param   integer  $pk        The primary key of the node to delete.
  * @param   boolean  $children  True to delete child nodes, false to move them up a level.
  *
  * @return  boolean  True on success.
  *
  * @link    http://docs.joomla.org/JTableNested/delete
  */
 public function delete($pk = null, $children = true)
 {
     // Initialise variables.
     $k = $this->_tbl_key;
     $pk = is_null($pk) ? $this->{$k} : $pk;
     if ($this->hasChildren($pk)) {
         // category has children so we can not delete it
         JError::raiseNotice(100, JText::_('COM_JDOWNLOADS_BE_NO_DEL_SUBCATS_EXISTS'));
         return false;
     }
     if ($this->hasDownloads($pk)) {
         // category has downloads so we can not delete it
         JError::raiseNotice(100, JText::_('COM_JDOWNLOADS_BE_NO_DEL_FILES_EXISTS'));
         return false;
     }
     // Lock the table for writing.
     if (!$this->_lock()) {
         // Error message set in lock method.
         return false;
     }
     // If tracking assets, remove the asset first.
     if ($this->_trackAssets) {
         $name = $this->_getAssetName();
         $asset = JTable::getInstance('Asset');
         // Lock the table for writing.
         if (!$asset->_lock()) {
             // Error message set in lock method.
             return false;
         }
         if ($asset->loadByName($name)) {
             // Delete the node in assets table.
             if (!$asset->delete(null, $children)) {
                 $this->setError($asset->getError());
                 $asset->_unlock();
                 return false;
             }
             $asset->_unlock();
         } else {
             $this->setError($asset->getError());
             $asset->_unlock();
             return false;
         }
     }
     // Get the node by id.
     if (!($node = $this->_getNode($pk))) {
         // Error message set in getNode method.
         $this->_unlock();
         return false;
     }
     // get first the folder name, so we can later delete this folder
     $query = $this->_db->getQuery(true);
     $query->select('cat_dir, cat_dir_parent');
     $query->from($this->_tbl);
     $query->where('id = ' . (int) $pk);
     $this->_db->setQuery($query);
     $cat_dirs = $this->_db->loadObject();
     // Should we delete all children along with the node?
     if ($children) {
         // Delete the node and all of its children.
         $query = $this->_db->getQuery(true);
         $query->delete();
         $query->from($this->_tbl);
         $query->where('lft BETWEEN ' . (int) $node->lft . ' AND ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Compress the left values.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('lft = lft - ' . (int) $node->width);
         $query->where('lft > ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Compress the right values.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('rgt = rgt - ' . (int) $node->width);
         $query->where('rgt > ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
     } else {
         // Delete the node.
         $query = $this->_db->getQuery(true);
         $query->delete();
         $query->from($this->_tbl);
         $query->where('lft = ' . (int) $node->lft);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Shift all node's children up a level.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('lft = lft - 1');
         $query->set('rgt = rgt - 1');
         $query->set('level = level - 1');
         $query->where('lft BETWEEN ' . (int) $node->lft . ' AND ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Adjust all the parent values for direct children of the deleted node.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('parent_id = ' . (int) $node->parent_id);
         $query->where('parent_id = ' . (int) $node->{$k});
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Shift all of the left values that are right of the node.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('lft = lft - 2');
         $query->where('lft > ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
         // Shift all of the right values that are right of the node.
         $query = $this->_db->getQuery(true);
         $query->update($this->_tbl);
         $query->set('rgt = rgt - 2');
         $query->where('rgt > ' . (int) $node->rgt);
         $this->_runQuery($query, 'JLIB_DATABASE_ERROR_DELETE_FAILED');
     }
     // delete now the folder
     if ($cat_dirs) {
         if ($cat_dirs->cat_dir_parent != '') {
             $cat_dir = $cat_dirs->cat_dir_parent . '/' . $cat_dirs->cat_dir;
         } else {
             $cat_dir = $cat_dirs->cat_dir;
         }
         JDownloadsHelper::deleteCategoryFolder($cat_dir);
     }
     // Unlock the table for writing.
     $this->_unlock();
     return true;
 }