Source for file components.php

Documentation is available at components.php

  1. <?php
  2. /**
  3.  * @package    FrameworkOnFramework
  4.  * @subpackage form
  5.  * @copyright  Copyright (C) 2010 - 2013 Akeeba Ltd. All rights reserved.
  6.  * @license    GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. if (!class_exists('JFormFieldList'))
  12. {
  13.     require_once JPATH_LIBRARIES '/joomla/form/fields/list.php';
  14. }
  15.  
  16. /**
  17.  * Form Field class for FOF
  18.  * Components installed on the site
  19.  *
  20.  * @package  FrameworkOnFramework
  21.  * @since    2.1
  22.  */
  23. class FOFFormFieldComponents extends JFormFieldList implements FOFFormField
  24. {
  25.     protected $static;
  26.  
  27.     protected $repeatable;
  28.  
  29.     public $client_ids = null;
  30.     
  31.     /** @var   FOFTable  The item being rendered in a repeatable form field */
  32.     public $item;
  33.     
  34.     /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
  35.     public $rowid;
  36.  
  37.     /**
  38.      * Method to get certain otherwise inaccessible properties from the form field object.
  39.      *
  40.      * @param   string  $name  The property name for which to the the value.
  41.      *
  42.      * @return  mixed  The property value or null.
  43.      *
  44.      * @since   2.1
  45.      */
  46.     public function __get($name)
  47.     {
  48.         switch ($name)
  49.         {
  50.             case 'static':
  51.                 if (empty($this->static))
  52.                 {
  53.                     $this->static = $this->getStatic();
  54.                 }
  55.  
  56.                 return $this->static;
  57.                 break;
  58.  
  59.             case 'repeatable':
  60.                 if (empty($this->repeatable))
  61.                 {
  62.                     $this->repeatable = $this->getRepeatable();
  63.                 }
  64.  
  65.                 return $this->static;
  66.                 break;
  67.  
  68.             default:
  69.                 return parent::__get($name);
  70.         }
  71.     }
  72.  
  73.     /**
  74.      * Get the rendering of this field type for static display, e.g. in a single
  75.      * item view (typically a "read" task).
  76.      *
  77.      * @since 2.1
  78.      *
  79.      * @return  string  The field HTML
  80.      */
  81.     public function getStatic()
  82.     {
  83.         $class $this->element['class'' class="' . (string) $this->element['class''"' '';
  84.  
  85.         return '<span id="' $this->id . '" ' $class '>' .
  86.             htmlspecialchars(FOFFormFieldList::getOptionName($this->getOptions()$this->value)ENT_COMPAT'UTF-8'.
  87.             '</span>';
  88.     }
  89.  
  90.     /**
  91.      * Get the rendering of this field type for a repeatable (grid) display,
  92.      * e.g. in a view listing many item (typically a "browse" task)
  93.      *
  94.      * @since 2.1
  95.      *
  96.      * @return  string  The field HTML
  97.      */
  98.     public function getRepeatable()
  99.     {
  100.         $class $this->element['class'? (string) $this->element['class''';
  101.  
  102.         return '<span class="' $this->id . ' ' $class '">' .
  103.             htmlspecialchars(FOFFormFieldList::getOptionName($this->getOptions()$this->value)ENT_COMPAT'UTF-8'.
  104.             '</span>';
  105.     }
  106.  
  107.     /**
  108.      * Get a list of all installed components and also translates them.
  109.      *
  110.      * The manifest_cache is used to get the extension names, since JInstaller is also
  111.      * translating those names in stead of the name column. Else some of the translations
  112.      * fails.
  113.      *
  114.      * @since    2.1
  115.      *
  116.      * @return     array    An array of JHtml options.
  117.      */
  118.     protected function getOptions()
  119.     {
  120.         $db JFactory::getDbo();
  121.  
  122.         // Check for client_ids override
  123.         if ($this->client_ids !== null)
  124.         {
  125.             $client_ids $this->client_ids;
  126.         }
  127.         else
  128.         {
  129.             $client_ids $this->element['client_ids'];
  130.         }
  131.  
  132.         $client_ids explode(','$client_ids);
  133.  
  134.         // Calculate client_ids where clause
  135.         foreach ($client_ids as &$client_id)
  136.         {
  137.             $client_id = (int) trim($client_id);
  138.             $client_id $db->q($client_id);
  139.         }
  140.  
  141.         $query $db->getQuery(true)
  142.             ->select(
  143.                 array(
  144.                     $db->qn('name'),
  145.                     $db->qn('element'),
  146.                     $db->qn('client_id'),
  147.                     $db->qn('manifest_cache'),
  148.                 )
  149.             )
  150.             ->from($db->qn('#__extensions'))
  151.             ->where($db->qn('type'' = ' $db->q('component'))
  152.             ->where($db->qn('client_id'' IN (' implode(','$client_ids')');
  153.         $db->setQuery($query);
  154.         $components $db->loadObjectList('element');
  155.  
  156.         // Convert to array of objects, so we can use sortObjects()
  157.         // Also translate component names with JText::_()
  158.         $aComponents array();
  159.         $user JFactory::getUser();
  160.  
  161.         foreach ($components as $component)
  162.         {
  163.             // Don't show components in the list where the user doesn't have access for
  164.             // TODO: perhaps add an option for this
  165.             if (!$user->authorise('core.manage'$component->element))
  166.             {
  167.                 continue;
  168.             }
  169.  
  170.             $oData = (object) array(
  171.                 'value'    => $component->element,
  172.                 'text'     => $this->translate($component'component')
  173.             );
  174.             $aComponents[$component->element$oData;
  175.         }
  176.  
  177.         // Reorder the components array, because the alphabetical
  178.         // ordering changed due to the JText::_() translation
  179.         uasort(
  180.             $aComponents,
  181.             function ($a$b{
  182.                 return strcasecmp($a->text$b->text);
  183.             }
  184.         );
  185.  
  186.         return $aComponents;
  187.     }
  188.  
  189.     /**
  190.      * Translate a list of objects with JText::_().
  191.      *
  192.      * @param   array   $item  The array of objects
  193.      * @param   string  $type  The extension type (e.g. component)
  194.      *
  195.      * @since   2.1
  196.      *
  197.      * @return  string  $text  The translated name of the extension
  198.      *
  199.      * @see administrator/com_installer/models/extension.php
  200.      */
  201.     public function translate($item$type)
  202.     {
  203.         // Map the manifest cache to $item. This is needed to get the name from the
  204.         // manifest_cache and NOT from the name column, else some JText::_() translations fails.
  205.         $mData json_decode($item->manifest_cache);
  206.  
  207.         if ($mData)
  208.         {
  209.             foreach ($mData as $key => $value)
  210.             {
  211.                 if ($key == 'type')
  212.                 {
  213.                     // Ignore the type field
  214.                     continue;
  215.                 }
  216.  
  217.                 $item->$key $value;
  218.             }
  219.         }
  220.  
  221.         $lang JFactory::getLanguage();
  222.  
  223.         switch ($type)
  224.         {
  225.             case 'component':
  226.                 $source JPATH_ADMINISTRATOR '/components/' $item->element;
  227.                 $lang->load("$item->element.sys"JPATH_ADMINISTRATORnullfalsetrue)
  228.                     ||    $lang->load("$item->element.sys"$sourcenullfalsetrue)
  229.                     ||    $lang->load("$item->element.sys"JPATH_ADMINISTRATOR$lang->getDefault()falsefalse)
  230.                     ||    $lang->load("$item->element.sys"$source$lang->getDefault()falsefalse);
  231.                 break;
  232.         }
  233.  
  234.         $text JText::_($item->name);
  235.  
  236.         return $text;
  237.     }
  238. }

Documentation generated on Tue, 19 Nov 2013 14:56:26 +0100 by phpDocumentor 1.4.3