Source for file plugin.php

Documentation is available at plugin.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_plugins
  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.  * Plugin model.
  14.  *
  15.  * @package     Joomla.Administrator
  16.  * @subpackage  com_plugins
  17.  * @since       1.6
  18.  */
  19. {
  20.     /**
  21.      * @var        string    The help screen key for the module.
  22.      * @since   1.6
  23.      */
  24.     protected $helpKey = 'JHELP_EXTENSIONS_PLUGIN_MANAGER_EDIT';
  25.  
  26.     /**
  27.      * @var        string    The help screen base URL for the module.
  28.      * @since   1.6
  29.      */
  30.     protected $helpURL;
  31.  
  32.     protected $_cache;
  33.  
  34.     /**
  35.      * @var        string    The event to trigger after saving the data.
  36.      * @since   1.6
  37.      */
  38.     protected $event_after_save = 'onExtensionAfterSave';
  39.  
  40.     /**
  41.      * @var        string    The event to trigger after before the data.
  42.      * @since   1.6
  43.      */
  44.     protected $event_before_save = 'onExtensionBeforeSave';
  45.  
  46.     /**
  47.      * Method to get the record form.
  48.      *
  49.      * @param   array  $data        Data for the form.
  50.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  51.      * @return  JForm    A JForm object on success, false on failure
  52.      * @since   1.6
  53.      */
  54.     public function getForm($data array()$loadData true)
  55.     {
  56.         // The folder and element vars are passed when saving the form.
  57.         if (empty($data))
  58.         {
  59.             $item        $this->getItem();
  60.             $folder        $item->folder;
  61.             $element    $item->element;
  62.         }
  63.         else
  64.         {
  65.             $folder        JArrayHelper::getValue($data'folder''''cmd');
  66.             $element    JArrayHelper::getValue($data'element''''cmd');
  67.         }
  68.  
  69.         // These variables are used to add data from the plugin XML files.
  70.         $this->setState('item.folder',    $folder);
  71.         $this->setState('item.element',    $element);
  72.  
  73.         // Get the form.
  74.         $form $this->loadForm('com_plugins.plugin''plugin'array('control' => 'jform''load_data' => $loadData));
  75.         if (empty($form))
  76.         {
  77.             return false;
  78.         }
  79.  
  80.         // Modify the form based on access controls.
  81.         if (!$this->canEditState((object) $data))
  82.         {
  83.             // Disable fields for display.
  84.             $form->setFieldAttribute('ordering''disabled''true');
  85.             $form->setFieldAttribute('enabled''disabled''true');
  86.  
  87.             // Disable fields while saving.
  88.             // The controller has already verified this is a record you can edit.
  89.             $form->setFieldAttribute('ordering''filter''unset');
  90.             $form->setFieldAttribute('enabled''filter''unset');
  91.         }
  92.  
  93.         return $form;
  94.     }
  95.  
  96.     /**
  97.      * Method to get the data that should be injected in the form.
  98.      *
  99.      * @return  mixed  The data for the form.
  100.      * @since   1.6
  101.      */
  102.     protected function loadFormData()
  103.     {
  104.         // Check the session for previously entered form data.
  105.         $data JFactory::getApplication()->getUserState('com_plugins.edit.plugin.data'array());
  106.  
  107.         if (empty($data))
  108.         {
  109.             $data $this->getItem();
  110.         }
  111.  
  112.         $this->preprocessData('com_plugins.plugin'$data);
  113.  
  114.         return $data;
  115.     }
  116.  
  117.     /**
  118.      * Method to get a single record.
  119.      *
  120.      * @param   integer    The id of the primary key.
  121.      *
  122.      * @return  mixed  Object on success, false on failure.
  123.      */
  124.     public function getItem($pk null)
  125.     {
  126.         $pk (!empty($pk)) $pk : (int) $this->getState('plugin.id');
  127.  
  128.         if (!isset($this->_cache[$pk]))
  129.         {
  130.             $false    false;
  131.  
  132.             // Get a row instance.
  133.             $table $this->getTable();
  134.  
  135.             // Attempt to load the row.
  136.             $return $table->load($pk);
  137.  
  138.             // Check for a table object error.
  139.             if ($return === false && $table->getError())
  140.             {
  141.                 $this->setError($table->getError());
  142.                 return $false;
  143.             }
  144.  
  145.             // Convert to the JObject before adding other data.
  146.             $properties $table->getProperties(1);
  147.             $this->_cache[$pkJArrayHelper::toObject($properties'JObject');
  148.  
  149.             // Convert the params field to an array.
  150.             $registry new JRegistry;
  151.             $registry->loadString($table->params);
  152.             $this->_cache[$pk]->params $registry->toArray();
  153.  
  154.             // Get the plugin XML.
  155.             $path JPath::clean(JPATH_PLUGINS.'/'.$table->folder.'/'.$table->element.'/'.$table->element.'.xml');
  156.  
  157.             if (file_exists($path))
  158.             {
  159.                 $this->_cache[$pk]->xml simplexml_load_file($path);
  160.             else {
  161.                 $this->_cache[$pk]->xml null;
  162.             }
  163.         }
  164.  
  165.         return $this->_cache[$pk];
  166.     }
  167.  
  168.     /**
  169.      * Returns a reference to the a Table object, always creating it.
  170.      *
  171.      * @param   type    The table type to instantiate
  172.      * @param   string    A prefix for the table class name. Optional.
  173.      * @param   array  Configuration array for model. Optional.
  174.      * @return  JTable    A database object
  175.     */
  176.     public function getTable($type 'Extension'$prefix 'JTable'$config array())
  177.     {
  178.         return JTable::getInstance($type$prefix$config);
  179.     }
  180.  
  181.     /**
  182.      * Auto-populate the model state.
  183.      *
  184.      * Note. Calling getState in this method will result in recursion.
  185.      *
  186.      * @return  void 
  187.      * @since   1.6
  188.      */
  189.     protected function populateState()
  190.     {
  191.         // Execute the parent method.
  192.         parent::populateState();
  193.  
  194.         $app JFactory::getApplication('administrator');
  195.  
  196.         // Load the User state.
  197.         $pk $app->input->getInt('extension_id');
  198.         $this->setState('plugin.id'$pk);
  199.     }
  200.  
  201.     /**
  202.      * @param   object    form object.
  203.      * @param   mixed    The data expected for the form.
  204.      * @return  mixed  True if successful.
  205.      * @throws    Exception if there is an error in the form event.
  206.      * @since   1.6
  207.      */
  208.     protected function preprocessForm(JForm $form$data$group 'content')
  209.     {
  210.         jimport('joomla.filesystem.path');
  211.  
  212.         $folder        $this->getState('item.folder');
  213.         $element    $this->getState('item.element');
  214.         $lang        JFactory::getLanguage();
  215.  
  216.         // Load the core and/or local language sys file(s) for the ordering field.
  217.         $db JFactory::getDbo();
  218.         $query 'SELECT element' .
  219.                 ' FROM #__extensions' .
  220.                 ' WHERE (type =' .$db->quote('plugin')'AND folder='$db->quote($folder')';
  221.         $db->setQuery($query);
  222.         $elements $db->loadColumn();
  223.  
  224.         foreach ($elements as $elementa)
  225.         {
  226.                 $lang->load('plg_'.$folder.'_'.$elementa.'.sys'JPATH_ADMINISTRATORnullfalsetrue)
  227.             ||    $lang->load('plg_'.$folder.'_'.$elementa.'.sys'JPATH_PLUGINS.'/'.$folder.'/'.$elementanullfalsetrue);
  228.         }
  229.  
  230.         if (empty($folder|| empty($element))
  231.         {
  232.             $app JFactory::getApplication();
  233.             $app->redirect(JRoute::_('index.php?option=com_plugins&view=plugins'false));
  234.         }
  235.  
  236.         $formFile JPath::clean(JPATH_PLUGINS '/' $folder '/' $element '/' $element '.xml');
  237.         if (!file_exists($formFile))
  238.         {
  239.             throw new Exception(JText::sprintf('COM_PLUGINS_ERROR_FILE_NOT_FOUND'$element '.xml'));
  240.         }
  241.  
  242.         // Load the core and/or local language file(s).
  243.             $lang->load('plg_'.$folder.'_'.$elementJPATH_ADMINISTRATORnullfalsetrue)
  244.         ||    $lang->load('plg_'.$folder.'_'.$elementJPATH_PLUGINS.'/'.$folder.'/'.$elementnullfalsetrue);
  245.  
  246.         if (file_exists($formFile))
  247.         {
  248.             // Get the plugin form.
  249.             if (!$form->loadFile($formFilefalse'//config'))
  250.             {
  251.                 throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
  252.             }
  253.         }
  254.  
  255.         // Attempt to load the xml file.
  256.         if (!$xml simplexml_load_file($formFile))
  257.         {
  258.             throw new Exception(JText::_('JERROR_LOADFILE_FAILED'));
  259.         }
  260.  
  261.         // Get the help data from the XML file if present.
  262.         $help $xml->xpath('/extension/help');
  263.         if (!empty($help))
  264.         {
  265.             $helpKey trim((string) $help[0]['key']);
  266.             $helpURL trim((string) $help[0]['url']);
  267.  
  268.             $this->helpKey = $helpKey $helpKey $this->helpKey;
  269.             $this->helpURL = $helpURL $helpURL $this->helpURL;
  270.         }
  271.  
  272.         // Trigger the default form events.
  273.         parent::preprocessForm($form$data$group);
  274.     }
  275.  
  276.     /**
  277.      * A protected method to get a set of ordering conditions.
  278.      *
  279.      * @param   object    record object.
  280.      * @return  array  An array of conditions to add to add to ordering queries.
  281.      * @since   1.6
  282.      */
  283.     protected function getReorderConditions($table)
  284.     {
  285.         $condition array();
  286.         $condition['type = '$this->_db->quote($table->type);
  287.         $condition['folder = '$this->_db->quote($table->folder);
  288.         return $condition;
  289.     }
  290.  
  291.     /**
  292.      * Override method to save the form data.
  293.      *
  294.      * @param   array  The form data.
  295.      * @return  boolean  True on success.
  296.      * @since   1.6
  297.      */
  298.     public function save($data)
  299.     {
  300.         // Load the extension plugin group.
  301.         JPluginHelper::importPlugin('extension');
  302.  
  303.         // Setup type
  304.         $data['type''plugin';
  305.  
  306.         return parent::save($data);
  307.     }
  308.  
  309.     /**
  310.      * Get the necessary data to load an item help screen.
  311.      *
  312.      * @return  object  An object with key, url, and local properties for loading the item help screen.
  313.      * @since   1.6
  314.      */
  315.     public function getHelp()
  316.     {
  317.         return (object) array('key' => $this->helpKey'url' => $this->helpURL);
  318.     }
  319.  
  320.     /**
  321.      * Custom clean cache method, plugins are cached in 2 places for different clients
  322.      *
  323.      * @since   1.6
  324.      */
  325.     protected function cleanCache($group null$client_id 0)
  326.     {
  327.         parent::cleanCache('com_plugins');
  328.     }
  329. }

Documentation generated on Tue, 19 Nov 2013 15:10:44 +0100 by phpDocumentor 1.4.3