Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 298 lines (260 sloc) 8.109 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
<?php
/**
* @package Joomla.Plugin
* @subpackage Content.joomla
*
* @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

/**
* Example Content Plugin
*
* @package Joomla.Plugin
* @subpackage Content.joomla
* @since 1.6
*/
class PlgContentJoomla extends JPlugin
{
/**
* Example after save content method
* Article is passed by reference, but after the save, so no changes will be saved.
* Method is called right after the content is saved
*
* @param string $context The context of the content passed to the plugin (added in 1.6)
* @param object $article A JTableContent object
* @param boolean $isNew If the content is just about to be created
*
* @return boolean true if function not enabled, is in front-end or is new. Else true or
* false depending on success of save function.
*
* @since 1.6
*/
public function onContentAfterSave($context, $article, $isNew)
{
// Check we are handling the frontend edit form.
if ($context != 'com_content.form')
{
return true;
}

// Check if this function is enabled.
if (!$this->params->def('email_new_fe', 1))
{
return true;
}

// Check this is a new article.
if (!$isNew)
{
return true;
}

$user = JFactory::getUser();

// Messaging for new items
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_messages/models', 'MessagesModel');
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_messages/tables');

$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__users'))
->where($db->quoteName('sendEmail') . ' = 1');
$db->setQuery($query);
$users = (array) $db->loadColumn();

$default_language = JComponentHelper::getParams('com_languages')->get('administrator');
$debug = JFactory::getConfig()->get('debug_lang');

foreach ($users as $user_id)
{
if ($user_id != $user->id)
{
// Load language for messaging
$receiver = JUser::getInstance($user_id);
$lang = JLanguage::getInstance($receiver->getParam('admin_language', $default_language), $debug);
$lang->load('com_content');
$message = array(
'user_id_to' => $user_id,
'subject' => $lang->_('COM_CONTENT_NEW_ARTICLE'),
'message' => sprintf($lang->_('COM_CONTENT_ON_NEW_CONTENT'), $user->get('name'), $article->title)
);
$model_message = JModelLegacy::getInstance('Message', 'MessagesModel');
$result = $model_message->save($message);
}
}

return $result;
}

/**
* Don't allow categories to be deleted if they contain items or subcategories with items
*
* @param string $context The context for the content passed to the plugin.
* @param object $data The data relating to the content that was deleted.
*
* @return boolean
*
* @since 1.6
*/
public function onContentBeforeDelete($context, $data)
{
// Skip plugin if we are deleting something other than categories
if ($context != 'com_categories.category')
{
return true;
}

// Check if this function is enabled.
if (!$this->params->def('check_categories', 1))
{
return true;
}

$extension = JFactory::getApplication()->input->getString('extension');

// Default to true if not a core extension
$result = true;

$tableInfo = array(
'com_banners' => array('table_name' => '#__banners'),
'com_contact' => array('table_name' => '#__contact_details'),
'com_content' => array('table_name' => '#__content'),
'com_newsfeeds' => array('table_name' => '#__newsfeeds'),
'com_weblinks' => array('table_name' => '#__weblinks')
);

// Now check to see if this is a known core extension
if (isset($tableInfo[$extension]))
{
// Get table name for known core extensions
$table = $tableInfo[$extension]['table_name'];

// See if this category has any content items
$count = $this->_countItemsInCategory($table, $data->get('id'));

// Return false if db error
if ($count === false)
{
$result = false;
}
else
{
// Show error if items are found in the category
if ($count > 0)
{
$msg = JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) .
JText::plural('COM_CATEGORIES_N_ITEMS_ASSIGNED', $count);
JError::raiseWarning(403, $msg);
$result = false;
}

// Check for items in any child categories (if it is a leaf, there are no child categories)
if (!$data->isLeaf())
{
$count = $this->_countItemsInChildren($table, $data->get('id'), $data);

if ($count === false)
{
$result = false;
}
elseif ($count > 0)
{
$msg = JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED', $data->get('title')) .
JText::plural('COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS', $count);
JError::raiseWarning(403, $msg);
$result = false;
}
}
}

return $result;
}
}

/**
* Get count of items in a category
*
* @param string $table table name of component table (column is catid)
* @param integer $catid id of the category to check
*
* @return mixed count of items found or false if db error
*
* @since 1.6
*/
private function _countItemsInCategory($table, $catid)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);

// Count the items in this category
$query->select('COUNT(id)')
->from($table)
->where('catid = ' . $catid);
$db->setQuery($query);

try
{
$count = $db->loadResult();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());

return false;
}

return $count;
}

/**
* Get count of items in a category's child categories
*
* @param string $table table name of component table (column is catid)
* @param integer $catid id of the category to check
* @param object $data The data relating to the content that was deleted.
*
* @return mixed count of items found or false if db error
*
* @since 1.6
*/
private function _countItemsInChildren($table, $catid, $data)
{
$db = JFactory::getDbo();

// Create subquery for list of child categories
$childCategoryTree = $data->getTree();

// First element in tree is the current category, so we can skip that one
unset($childCategoryTree[0]);
$childCategoryIds = array();

foreach ($childCategoryTree as $node)
{
$childCategoryIds[] = $node->id;
}

// Make sure we only do the query if we have some categories to look in
if (count($childCategoryIds))
{
// Count the items in this category
$query = $db->getQuery(true)
->select('COUNT(id)')
->from($table)
->where('catid IN (' . implode(',', $childCategoryIds) . ')');
$db->setQuery($query);

try
{
$count = $db->loadResult();
}
catch (RuntimeException $e)
{
JError::raiseWarning(500, $e->getMessage());

return false;
}

return $count;
}
else
// If we didn't have any categories to check, return 0
{
return 0;
}
}

/**
* Change the state in core_content if the state in a table is changed
*
* @param string $context The context for the content passed to the plugin.
* @param array $pks A list of primary key ids of the content that has changed state.
* @param integer $value The value of the state that the content has been changed to.
*
* @return boolean
*
* @since 3.1
*/
public function onContentChangeState($context, $pks, $value)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('core_content_id'))
->from($db->quoteName('#__ucm_content'))
->where($db->quoteName('core_type_alias') . ' = ' . $db->quote($context))
->where($db->quoteName('core_content_item_id') . ' IN (' . $pksImploded = implode(',', $pks) . ')');
$db->setQuery($query);
$ccIds = $db->loadColumn();

$cctable = new JTableCorecontent($db);
$cctable->publish($ccIds, $value);

return true;
}
}
Something went wrong with that request. Please try again.