Source for file list.php

Documentation is available at list.php

  1. <?php
  2. /**
  3.  * @package    FrameworkOnFramework
  4.  * @subpackage form
  5.  * @copyright  Copyright (C) 2010 - 2012 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.  * Supports a generic list of options.
  19.  *
  20.  * @package  FrameworkOnFramework
  21.  * @since    2.0
  22.  */
  23. class FOFFormFieldList extends JFormFieldList implements FOFFormField
  24. {
  25.     protected $static;
  26.  
  27.     protected $repeatable;
  28.     
  29.     /** @var   FOFTable  The item being rendered in a repeatable form field */
  30.     public $item;
  31.     
  32.     /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
  33.     public $rowid;
  34.  
  35.     /**
  36.      * Method to get certain otherwise inaccessible properties from the form field object.
  37.      *
  38.      * @param   string  $name  The property name for which to the the value.
  39.      *
  40.      * @return  mixed  The property value or null.
  41.      *
  42.      * @since   2.0
  43.      */
  44.     public function __get($name)
  45.     {
  46.         switch ($name)
  47.         {
  48.             case 'static':
  49.                 if (empty($this->static))
  50.                 {
  51.                     $this->static = $this->getStatic();
  52.                 }
  53.  
  54.                 return $this->static;
  55.                 break;
  56.  
  57.             case 'repeatable':
  58.                 if (empty($this->repeatable))
  59.                 {
  60.                     $this->repeatable = $this->getRepeatable();
  61.                 }
  62.  
  63.                 return $this->static;
  64.                 break;
  65.  
  66.             default:
  67.                 return parent::__get($name);
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Get the rendering of this field type for static display, e.g. in a single
  73.      * item view (typically a "read" task).
  74.      *
  75.      * @since 2.0
  76.      *
  77.      * @return  string  The field HTML
  78.      */
  79.     public function getStatic()
  80.     {
  81.         $class $this->element['class'' class="' . (string) $this->element['class''"' '';
  82.  
  83.         return '<span id="' $this->id . '" ' $class '>' .
  84.             htmlspecialchars(self::getOptionName($this->getOptions()$this->value)ENT_COMPAT'UTF-8'.
  85.             '</span>';
  86.     }
  87.  
  88.     /**
  89.      * Get the rendering of this field type for a repeatable (grid) display,
  90.      * e.g. in a view listing many item (typically a "browse" task)
  91.      *
  92.      * @since 2.0
  93.      *
  94.      * @return  string  The field HTML
  95.      */
  96.     public function getRepeatable()
  97.     {
  98.         $show_link         false;
  99.         $link_url          '';
  100.  
  101.         $class $this->element['class'? (string) $this->element['class''';
  102.  
  103.         if ($this->element['show_link'== 'true')
  104.         {
  105.             $show_link true;
  106.         }
  107.  
  108.         if ($this->element['url'])
  109.         {
  110.             $link_url $this->element['url'];
  111.         }
  112.         else
  113.         {
  114.             $show_link false;
  115.         }
  116.  
  117.         if ($show_link && ($this->item instanceof FOFTable))
  118.         {
  119.             // Replace [ITEM:ID] in the URL with the item's key value (usually:
  120.             // the auto-incrementing numeric ID)
  121.             $keyfield $this->item->getKeyName();
  122.             $replace  $this->item->$keyfield;
  123.             $link_url str_replace('[ITEM:ID]'$replace$link_url);
  124.  
  125.             // Replace other field variables in the URL
  126.             $fields $this->item->getFields();
  127.  
  128.             foreach ($fields as $fielddata)
  129.             {
  130.                 $fieldname $fielddata->Field;
  131.  
  132.                 if (empty($fieldname))
  133.                 {
  134.                     $fieldname $fielddata->column_name;
  135.                 }
  136.  
  137.                 $search    '[ITEM:' strtoupper($fieldname']';
  138.                 $replace   $this->item->$fieldname;
  139.                 $link_url  str_replace($search$replace$link_url);
  140.             }
  141.         }
  142.         else
  143.         {
  144.             $show_link false;
  145.         }
  146.  
  147.         $html '<span class="' $this->id . ' ' $class '">';
  148.  
  149.         if ($show_link)
  150.         {
  151.             $html .= '<a href="' $link_url '">';
  152.         }
  153.  
  154.         $html .= htmlspecialchars(self::getOptionName($this->getOptions()$this->value)ENT_COMPAT'UTF-8');
  155.  
  156.         if ($show_link)
  157.         {
  158.             $html .= '</a>';
  159.         }
  160.  
  161.         $html .= '</span>';
  162.  
  163.         return $html;
  164.     }
  165.  
  166.     /**
  167.      * Gets the active option's label given an array of JHtml options
  168.      *
  169.      * @param   array   $data      The JHtml options to parse
  170.      * @param   mixed   $selected  The currently selected value
  171.      * @param   string  $optKey    Key name
  172.      * @param   string  $optText   Value name
  173.      *
  174.      * @return  mixed   The label of the currently selected option
  175.      */
  176.     public static function getOptionName($data$selected null$optKey 'value'$optText 'text')
  177.     {
  178.         $ret null;
  179.  
  180.         foreach ($data as $elementKey => &$element)
  181.         {
  182.             if (is_array($element))
  183.             {
  184.                 $key $optKey === null $elementKey $element[$optKey];
  185.                 $text $element[$optText];
  186.             }
  187.             elseif (is_object($element))
  188.             {
  189.                 $key $optKey === null $elementKey $element->$optKey;
  190.                 $text $element->$optText;
  191.             }
  192.             else
  193.             {
  194.                 // This is a simple associative array
  195.                 $key $elementKey;
  196.                 $text $element;
  197.             }
  198.  
  199.             if (is_null($ret))
  200.             {
  201.                 $ret $text;
  202.             }
  203.             elseif ($selected == $key)
  204.             {
  205.                 $ret $text;
  206.             }
  207.         }
  208.  
  209.         return $ret;
  210.     }
  211.  
  212.     /**
  213.      * Method to get the field options.
  214.      *
  215.      * Ordering is disabled by default. You can enable ordering by setting the
  216.      * 'order' element in your form field. The other order values are optional.
  217.      *
  218.      * - order                    What to order.            Possible values: 'name' or 'value' (default = false)
  219.      * - order_dir                Order direction.        Possible values: 'asc' = Ascending or 'desc' = Descending (default = 'asc')
  220.      * - order_case_sensitive    Order case sensitive.    Possible values: 'true' or 'false' (default = false)
  221.      *
  222.      * @return  array  The field option objects.
  223.      *
  224.      * @since    Ordering is available since FOF 2.1.b2.
  225.      */
  226.     protected function getOptions()
  227.     {
  228.         // Ordering is disabled by default for backward compatibility
  229.         $order false;
  230.  
  231.         // Set default order direction
  232.         $order_dir 'asc';
  233.  
  234.         // Set default value for case sensitive sorting
  235.         $order_case_sensitive false;
  236.  
  237.         if ($this->element['order'&& $this->element['order'!== 'false')
  238.         {
  239.             $order $this->element['order'];
  240.         }
  241.  
  242.         if ($this->element['order_dir'])
  243.         {
  244.             $order_dir $this->element['order_dir'];
  245.         }
  246.  
  247.         if ($this->element['order_case_sensitive'])
  248.         {
  249.             // Override default setting when the form element value is 'true'
  250.             if ($this->element['order_case_sensitive'== 'true')
  251.             {
  252.                 $order_case_sensitive true;
  253.             }
  254.         }
  255.  
  256.         // Create a $sortOptions array in order to apply sorting
  257.         $i 0;
  258.         $sortOptions array();
  259.  
  260.         foreach ($this->element->children(as $option)
  261.         {
  262.             $name JText::alt(trim((string) $option)preg_replace('/[^a-zA-Z0-9_\-]/''_'$this->fieldname));
  263.  
  264.             $sortOptions[$inew stdClass;
  265.             $sortOptions[$i]->option $option;
  266.             $sortOptions[$i]->value $option['value'];
  267.             $sortOptions[$i]->name $name;
  268.             $i++;
  269.         }
  270.  
  271.         // Only order if it's set
  272.         if ($order)
  273.         {
  274.             jimport('joomla.utilities.arrayhelper');
  275.             JArrayHelper::sortObjects($sortOptions$order$order_dir == 'asc' : -1$order_case_sensitivefalse);
  276.         }
  277.  
  278.         // Initialise the options
  279.         $options array();
  280.  
  281.         // Do we have a class and method source for our options?
  282.         $source_file      empty($this->element['source_file']'' : (string) $this->element['source_file'];
  283.         $source_class     empty($this->element['source_class']'' : (string) $this->element['source_class'];
  284.         $source_method    empty($this->element['source_method']'' : (string) $this->element['source_method'];
  285.         $source_key       empty($this->element['source_key']'*' : (string) $this->element['source_key'];
  286.         $source_value     empty($this->element['source_value']'*' : (string) $this->element['source_value'];
  287.         $source_translate empty($this->element['source_translate']'true' : (string) $this->element['source_translate'];
  288.         $source_translate in_array(strtolower($source_translate)array('true','yes','1','on')) true false;
  289.  
  290.         if ($source_class && $source_method)
  291.         {
  292.             // Maybe we have to load a file?
  293.             if (!empty($source_file))
  294.             {
  295.                 $source_file FOFTemplateUtils::parsePath($source_filetrue);
  296.  
  297.                 JLoader::import('joomla.filesystem.file');
  298.  
  299.                 if (JFile::exists($source_file))
  300.                 {
  301.                     include_once $source_file;
  302.                 }
  303.             }
  304.  
  305.             // Make sure the class exists
  306.             if (class_exists($source_classtrue))
  307.             {
  308.                 // ...and so does the option
  309.                 if (in_array($source_methodget_class_methods($source_class)))
  310.                 {
  311.                     // Get the data from the class
  312.                     $source_data $source_class::$source_method();
  313.  
  314.                     // Loop through the data and prime the $options array
  315.                     foreach ($source_data as $k => $v)
  316.                     {
  317.                         $key (empty($source_key|| ($source_key == '*')) $k $v[$source_key];
  318.                         $value (empty($source_value|| ($source_value == '*')) $v $v[$source_value];
  319.  
  320.                         if ($source_translate)
  321.                         {
  322.                             $value JText::_($value);
  323.                         }
  324.  
  325.                         $options[JHtml::_('select.option'$key$value'value''text');
  326.                     }
  327.                 }
  328.             }
  329.         }
  330.  
  331.         // Get the field $options
  332.         foreach ($sortOptions as $sortOption)
  333.         {
  334.             $option $sortOption->option;
  335.             $name $sortOption->name;
  336.  
  337.             // Only add <option /> elements.
  338.             if ($option->getName(!= 'option')
  339.             {
  340.                 continue;
  341.             }
  342.  
  343.             $tmp JHtml::_('select.option'(string) $option['value']$name'value''text'((string) $option['disabled'== 'true'));
  344.  
  345.             // Set some option attributes.
  346.             $tmp->class = (string) $option['class'];
  347.  
  348.             // Set some JavaScript option attributes.
  349.             $tmp->onclick = (string) $option['onclick'];
  350.  
  351.             // Add the option object to the result set.
  352.             $options[$tmp;
  353.         }
  354.  
  355.         reset($options);
  356.  
  357.         return $options;
  358.     }
  359. }

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