PageRenderTime 29ms CodeModel.GetById 22ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

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