[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/plugins/content/joomla/ -> joomla.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1