Source for file joomla.php

Documentation is available at joomla.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Content.joomla
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Example Content Plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Content.joomla
  17.  * @since       1.6
  18.  */
  19. class 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   $context  The context of the content passed to the plugin (added in 1.6)
  27.      * @param   object   $article  A JTableContent object
  28.      * @param   boolean  $isNew    If the content is just about to be created
  29.      *
  30.      * @return  boolean   true if function not enabled, is in front-end or is new. Else true or
  31.      *                     false depending on success of save function.
  32.      *
  33.      * @since   1.6
  34.      */
  35.     public function onContentAfterSave($context$article$isNew)
  36.     {
  37.         // Check we are handling the frontend edit form.
  38.         if ($context != 'com_content.form')
  39.         {
  40.             return true;
  41.         }
  42.  
  43.         // Check if this function is enabled.
  44.         if (!$this->params->def('email_new_fe'1))
  45.         {
  46.             return true;
  47.         }
  48.  
  49.         // Check this is a new article.
  50.         if (!$isNew)
  51.         {
  52.             return true;
  53.         }
  54.  
  55.         $user JFactory::getUser();
  56.  
  57.         // Messaging for new items
  58.         JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR '/components/com_messages/models''MessagesModel');
  59.         JTable::addIncludePath(JPATH_ADMINISTRATOR '/components/com_messages/tables');
  60.  
  61.         $db JFactory::getDbo();
  62.         $query $db->getQuery(true)
  63.         ->select($db->quoteName('id'))
  64.         ->from($db->quoteName('#__users'))
  65.         ->where($db->quoteName('sendEmail'' = 1');
  66.         $db->setQuery($query);
  67.         $users = (array) $db->loadColumn();
  68.  
  69.         $default_language JComponentHelper::getParams('com_languages')->get('administrator');
  70.         $debug JFactory::getConfig()->get('debug_lang');
  71.  
  72.         foreach ($users as $user_id)
  73.         {
  74.             if ($user_id != $user->id)
  75.             {
  76.                 // Load language for messaging
  77.                 $receiver JUser::getInstance($user_id);
  78.                 $lang JLanguage::getInstance($receiver->getParam('admin_language'$default_language)$debug);
  79.                 $lang->load('com_content');
  80.                 $message array(
  81.                     'user_id_to' => $user_id,
  82.                     'subject' => $lang->_('COM_CONTENT_NEW_ARTICLE'),
  83.                     'message' => sprintf($lang->_('COM_CONTENT_ON_NEW_CONTENT')$user->get('name')$article->title)
  84.                 );
  85.                 $model_message JModelLegacy::getInstance('Message''MessagesModel');
  86.                 $result $model_message->save($message);
  87.             }
  88.         }
  89.  
  90.         return $result;
  91.     }
  92.  
  93.     /**
  94.      * Don't allow categories to be deleted if they contain items or subcategories with items
  95.      *
  96.      * @param   string  $context  The context for the content passed to the plugin.
  97.      * @param   object  $data     The data relating to the content that was deleted.
  98.      *
  99.      * @return  boolean 
  100.      *
  101.      * @since   1.6
  102.      */
  103.     public function onContentBeforeDelete($context$data)
  104.     {
  105.         // Skip plugin if we are deleting something other than categories
  106.         if ($context != 'com_categories.category')
  107.         {
  108.             return true;
  109.         }
  110.  
  111.         // Check if this function is enabled.
  112.         if (!$this->params->def('check_categories'1))
  113.         {
  114.             return true;
  115.         }
  116.  
  117.         $extension JFactory::getApplication()->input->getString('extension');
  118.  
  119.         // Default to true if not a core extension
  120.         $result true;
  121.  
  122.         $tableInfo array(
  123.             'com_banners' => array('table_name' => '#__banners'),
  124.             'com_contact' => array('table_name' => '#__contact_details'),
  125.             'com_content' => array('table_name' => '#__content'),
  126.             'com_newsfeeds' => array('table_name' => '#__newsfeeds'),
  127.             'com_weblinks' => array('table_name' => '#__weblinks')
  128.         );
  129.  
  130.         // Now check to see if this is a known core extension
  131.         if (isset($tableInfo[$extension]))
  132.         {
  133.             // Get table name for known core extensions
  134.             $table $tableInfo[$extension]['table_name'];
  135.  
  136.             // See if this category has any content items
  137.             $count $this->_countItemsInCategory($table$data->get('id'));
  138.  
  139.             // Return false if db error
  140.             if ($count === false)
  141.             {
  142.                 $result false;
  143.             }
  144.             else
  145.             {
  146.                 // Show error if items are found in the category
  147.                 if ($count 0)
  148.                 {
  149.                     $msg JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED'$data->get('title')) .
  150.                         JText::plural('COM_CATEGORIES_N_ITEMS_ASSIGNED'$count);
  151.                     JError::raiseWarning(403$msg);
  152.                     $result false;
  153.                 }
  154.  
  155.                 // Check for items in any child categories (if it is a leaf, there are no child categories)
  156.                 if (!$data->isLeaf())
  157.                 {
  158.                     $count $this->_countItemsInChildren($table$data->get('id')$data);
  159.  
  160.                     if ($count === false)
  161.                     {
  162.                         $result false;
  163.                     }
  164.                     elseif ($count 0)
  165.                     {
  166.                         $msg JText::sprintf('COM_CATEGORIES_DELETE_NOT_ALLOWED'$data->get('title')) .
  167.                             JText::plural('COM_CATEGORIES_HAS_SUBCATEGORY_ITEMS'$count);
  168.                         JError::raiseWarning(403$msg);
  169.                         $result false;
  170.                     }
  171.                 }
  172.             }
  173.  
  174.             return $result;
  175.         }
  176.     }
  177.  
  178.     /**
  179.      * Get count of items in a category
  180.      *
  181.      * @param   string   $table  table name of component table (column is catid)
  182.      * @param   integer  $catid  id of the category to check
  183.      *
  184.      * @return  mixed  count of items found or false if db error
  185.      *
  186.      * @since   1.6
  187.      */
  188.     private function _countItemsInCategory($table$catid)
  189.     {
  190.         $db JFactory::getDbo();
  191.         $query $db->getQuery(true);
  192.  
  193.         // Count the items in this category
  194.         $query->select('COUNT(id)')
  195.             ->from($table)
  196.             ->where('catid = ' $catid);
  197.         $db->setQuery($query);
  198.  
  199.         try
  200.         {
  201.             $count $db->loadResult();
  202.         }
  203.         catch (RuntimeException $e)
  204.         {
  205.             JError::raiseWarning(500$e->getMessage());
  206.  
  207.             return false;
  208.         }
  209.  
  210.         return $count;
  211.     }
  212.  
  213.     /**
  214.      * Get count of items in a category's child categories
  215.      *
  216.      * @param   string   $table  table name of component table (column is catid)
  217.      * @param   integer  $catid  id of the category to check
  218.      * @param   object   $data   The data relating to the content that was deleted.
  219.      *
  220.      * @return  mixed  count of items found or false if db error
  221.      *
  222.      * @since   1.6
  223.      */
  224.     private function _countItemsInChildren($table$catid$data)
  225.     {
  226.         $db JFactory::getDbo();
  227.  
  228.         // Create subquery for list of child categories
  229.         $childCategoryTree $data->getTree();
  230.  
  231.         // First element in tree is the current category, so we can skip that one
  232.         unset($childCategoryTree[0]);
  233.         $childCategoryIds array();
  234.  
  235.         foreach ($childCategoryTree as $node)
  236.         {
  237.             $childCategoryIds[$node->id;
  238.         }
  239.  
  240.         // Make sure we only do the query if we have some categories to look in
  241.         if (count($childCategoryIds))
  242.         {
  243.             // Count the items in this category
  244.             $query $db->getQuery(true)
  245.                 ->select('COUNT(id)')
  246.                 ->from($table)
  247.                 ->where('catid IN (' implode(','$childCategoryIds')');
  248.             $db->setQuery($query);
  249.  
  250.             try
  251.             {
  252.                 $count $db->loadResult();
  253.             }
  254.             catch (RuntimeException $e)
  255.             {
  256.                 JError::raiseWarning(500$e->getMessage());
  257.  
  258.                 return false;
  259.             }
  260.  
  261.             return $count;
  262.         }
  263.         else
  264.             // If we didn't have any categories to check, return 0
  265.         {
  266.             return 0;
  267.         }
  268.     }
  269.  
  270.     /**
  271.      * Change the state in core_content if the state in a table is changed
  272.      *
  273.      * @param   string   $context  The context for the content passed to the plugin.
  274.      * @param   array    $pks      A list of primary key ids of the content that has changed state.
  275.      * @param   integer  $value    The value of the state that the content has been changed to.
  276.      *
  277.      * @return  boolean 
  278.      *
  279.      * @since   3.1
  280.      */
  281.     public function onContentChangeState($context$pks$value)
  282.     {
  283.         $db JFactory::getDbo();
  284.         $query $db->getQuery(true)
  285.             ->select($db->quoteName('core_content_id'))
  286.             ->from($db->quoteName('#__ucm_content'))
  287.             ->where($db->quoteName('core_type_alias'' = ' $db->quote($context))
  288.             ->where($db->quoteName('core_content_item_id'' IN (' $pksImploded implode(','$pks')');
  289.         $db->setQuery($query);
  290.         $ccIds $db->loadColumn();
  291.  
  292.         $cctable new JTableCorecontent($db);
  293.         $cctable->publish($ccIds$value);
  294.  
  295.         return true;
  296.     }
  297. }

Documentation generated on Tue, 19 Nov 2013 15:06:07 +0100 by phpDocumentor 1.4.3