/** * onBeforeDelete method. Hook for chidlren model. * * @param JTable $table The table object. * * @return boolean */ protected function onBeforeDelete($table) { $user = JFactory::getUser(); if (!$user->authorise('k2.category.delete', 'com_k2.category.' . $table->id)) { $this->setError(JText::_('K2_YOU_ARE_NOT_AUTHORIZED_TO_PERFORM_THIS_OPERATION')); return false; } // Check that the category is trashed if ($table->state != -1) { $this->setError(JText::sprintf('K2_CATEGORY_IS_NOT_TRASHED', $table->title)); return false; } // Check for categories that are not trashed in the tree $tree = $table->getTree($table->id); foreach ($tree as $category) { if ($category->state != -1) { $this->setError(JText::sprintf('K2_COULD_NOT_DELETE_CATEGORY_BECAUSE_IT_CONTAINS_NON_TRASHED_CATEGORIES', $table->title)); return false; } } // Ensure the category and it's children do not contain any items $model = K2Model::getInstance('Items'); $model->setState('category', $table->id); $count = $model->countRows(); if ($count) { $this->setError(JText::sprintf('K2_COULD_NOT_DELETE_CATEGORY_BECAUSE_IT_CONTAINS_ITEMS', $table->title)); return false; } return true; }