/plugins/content/joomla/joomla.php

https://bitbucket.org/eternaware/joomus · PHP · 235 lines · 143 code · 24 blank · 68 comment · 15 complexity · 03f7a0da7799cf077aebdce9c19f2920 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Plugin
  4. * @subpackage Content.joomla
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Example Content Plugin
  12. *
  13. * @package Joomla.Plugin
  14. * @subpackage Content.joomla
  15. * @since 1.6
  16. */
  17. class plgContentJoomla extends JPlugin
  18. {
  19. /**
  20. * Example after save content method
  21. * Article is passed by reference, but after the save, so no changes will be saved.
  22. * Method is called right after the content is saved
  23. *
  24. * @param string The context of the content passed to the plugin (added in 1.6)
  25. * @param object A JTableContent object
  26. * @param bool If the content is just about to be created
  27. * @since 1.6
  28. */
  29. public function onContentAfterSave($context, &$article, $isNew)
  30. {
  31. // Check we are handling the frontend edit form.
  32. if ($context != 'com_content.form') {
  33. return true;
  34. }
  35. // Check if this function is enabled.
  36. if (!$this->params->def('email_new_fe', 1)) {
  37. return true;
  38. }
  39. // Check this is a new article.
  40. if (!$isNew) {
  41. return true;
  42. }
  43. $user = JFactory::getUser();
  44. // Messaging for new items
  45. JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_messages/models', 'MessagesModel');
  46. JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_messages/tables');
  47. $db = JFactory::getDbo();
  48. $db->setQuery('SELECT id FROM #__users WHERE sendEmail = 1');
  49. $users = (array) $db->loadColumn();
  50. $default_language = JComponentHelper::getParams('com_languages')->get('administrator');
  51. $debug = JFactory::getConfig()->get('debug_lang');
  52. foreach ($users as $user_id)
  53. {
  54. if ($user_id != $user->id) {
  55. // Load language for messaging
  56. $receiver = JUser::getInstance($user_id);
  57. $lang = JLanguage::getInstance($receiver->getParam('admin_language', $default_language), $debug);
  58. $lang->load('com_content');
  59. $message = array(
  60. 'user_id_to' => $user_id,
  61. 'subject' => $lang->_('COM_CONTENT_NEW_ARTICLE'),
  62. 'message' => sprintf($lang->_('COM_CONTENT_ON_NEW_CONTENT'), $user->get('name'), $article->title)
  63. );
  64. $model_message = JModelLegacy::getInstance('Message', 'MessagesModel');
  65. $model_message->save($message);
  66. }
  67. }
  68. return true;
  69. }
  70. /**
  71. * Don't allow categories to be deleted if they contain items or subcategories with items
  72. *
  73. * @param string The context for the content passed to the plugin.
  74. * @param object The data relating to the content that was deleted.
  75. * @return boolean
  76. * @since 1.6
  77. */
  78. public function onContentBeforeDelete($context, $data)
  79. {
  80. // Skip plugin if we are deleting something other than categories
  81. if ($context != 'com_categories.category') {
  82. return true;
  83. }
  84. // Check if this function is enabled.
  85. if (!$this->params->def('check_categories', 1)) {
  86. return true;
  87. }
  88. $extension = JFactory::getApplication()->input->getString('extension');
  89. // Default to true if not a core extension
  90. $result = true;
  91. $tableInfo = array (
  92. 'com_banners' => array('table_name' => '#__banners'),
  93. 'com_contact' => array('table_name' => '#__contact_details'),
  94. 'com_content' => array('table_name' => '#__content'),
  95. 'com_newsfeeds' => array('table_name' => '#__newsfeeds'),
  96. 'com_weblinks' => array('table_name' => '#__weblinks')
  97. );
  98. // Now check to see if this is a known core extension
  99. if (isset($tableInfo[$extension]))
  100. {
  101. // Get table name for known core extensions
  102. $table = $tableInfo[$extension]['table_name'];
  103. // See if this category has any content items
  104. $count = $this->_countItemsInCategory($table, $data->get('id'));
  105. // Return false if db error
  106. if ($count === false)
  107. {
  108. $result = false;
  109. }
  110. else
  111. {
  112. // Show error if items are found in the category
  113. if ($count > 0 ) {
  114. $msg = JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) .
  115. JText::plural('COM_CATEGORIES_N_ITEMS_ASSIGNED', $count);
  116. JError::raiseWarning(403, $msg);
  117. $result = false;
  118. }
  119. // Check for items in any child categories (if it is a leaf, there are no child categories)
  120. if (!$data->isLeaf()) {
  121. $count = $this->_countItemsInChildren($table, $data->get('id'), $data);
  122. if ($count === false)
  123. {
  124. $result = false;
  125. }
  126. elseif ($count > 0)
  127. {
  128. $msg = JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) .
  129. JText::plural('COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS', $count);
  130. JError::raiseWarning(403, $msg);
  131. $result = false;
  132. }
  133. }
  134. }
  135. return $result;
  136. }
  137. }
  138. /**
  139. * Get count of items in a category
  140. *
  141. * @param string table name of component table (column is catid)
  142. * @param int id of the category to check
  143. * @return mixed count of items found or false if db error
  144. * @since 1.6
  145. */
  146. private function _countItemsInCategory($table, $catid)
  147. {
  148. $db = JFactory::getDbo();
  149. $query = $db->getQuery(true);
  150. // Count the items in this category
  151. $query->select('COUNT(id)');
  152. $query->from($table);
  153. $query->where('catid = ' . $catid);
  154. $db->setQuery($query);
  155. try
  156. {
  157. $count = $db->loadResult();
  158. }
  159. catch (RuntimeException $e)
  160. {
  161. JError::raiseWarning(500, $e->getMessage());
  162. return false;
  163. }
  164. return $count;
  165. }
  166. /**
  167. * Get count of items in a category's child categories
  168. *
  169. * @param string table name of component table (column is catid)
  170. * @param int id of the category to check
  171. * @return mixed count of items found or false if db error
  172. * @since 1.6
  173. */
  174. private function _countItemsInChildren($table, $catid, $data)
  175. {
  176. $db = JFactory::getDbo();
  177. // Create subquery for list of child categories
  178. $childCategoryTree = $data->getTree();
  179. // First element in tree is the current category, so we can skip that one
  180. unset($childCategoryTree[0]);
  181. $childCategoryIds = array();
  182. foreach ($childCategoryTree as $node) {
  183. $childCategoryIds[] = $node->id;
  184. }
  185. // Make sure we only do the query if we have some categories to look in
  186. if (count($childCategoryIds))
  187. {
  188. // Count the items in this category
  189. $query = $db->getQuery(true);
  190. $query->select('COUNT(id)');
  191. $query->from($table);
  192. $query->where('catid IN (' . implode(',', $childCategoryIds) . ')');
  193. $db->setQuery($query);
  194. try
  195. {
  196. $count = $db->loadResult();
  197. }
  198. catch (RuntimeException $e)
  199. {
  200. JError::raiseWarning(500, $e->getMessage());
  201. return false;
  202. }
  203. return $count;
  204. }
  205. else
  206. // If we didn't have any categories to check, return 0
  207. {
  208. return 0;
  209. }
  210. }
  211. }