Source for file model.php

Documentation is available at model.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('JFormFieldSql'))
  12. {
  13.     require_once JPATH_LIBRARIES '/joomla/form/fields/sql.php';
  14. }
  15.  
  16. /**
  17.  * Form Field class for FOF
  18.  * Generic list from a model's results
  19.  *
  20.  * @package  FrameworkOnFramework
  21.  * @since    2.0
  22.  */
  23. class FOFFormFieldModel extends FOFFormFieldList implements FOFFormField
  24. {
  25.     protected $static;
  26.  
  27.     protected $repeatable;
  28.  
  29.     /**
  30.      * Method to get certain otherwise inaccessible properties from the form field object.
  31.      *
  32.      * @param   string  $name  The property name for which to the the value.
  33.      *
  34.      * @return  mixed  The property value or null.
  35.      *
  36.      * @since   2.0
  37.      */
  38.     public function __get($name)
  39.     {
  40.         switch ($name)
  41.         {
  42.             case 'static':
  43.                 if (empty($this->static))
  44.                 {
  45.                     $this->static = $this->getStatic();
  46.                 }
  47.  
  48.                 return $this->static;
  49.                 break;
  50.  
  51.             case 'repeatable':
  52.                 if (empty($this->repeatable))
  53.                 {
  54.                     $this->repeatable = $this->getRepeatable();
  55.                 }
  56.  
  57.                 return $this->static;
  58.                 break;
  59.  
  60.             default:
  61.                 return parent::__get($name);
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Get the rendering of this field type for static display, e.g. in a single
  67.      * item view (typically a "read" task).
  68.      *
  69.      * @since 2.0
  70.      *
  71.      * @return  string  The field HTML
  72.      */
  73.     public function getStatic()
  74.     {
  75.         $class $this->element['class'' class="' . (string) $this->element['class''"' '';
  76.  
  77.         return '<span id="' $this->id . '" ' $class '>' .
  78.             htmlspecialchars(FOFFormFieldList::getOptionName($this->getOptions()$this->value)ENT_COMPAT'UTF-8'.
  79.             '</span>';
  80.     }
  81.  
  82.     /**
  83.      * Get the rendering of this field type for a repeatable (grid) display,
  84.      * e.g. in a view listing many item (typically a "browse" task)
  85.      *
  86.      * @since 2.0
  87.      *
  88.      * @return  string  The field HTML
  89.      */
  90.     public function getRepeatable()
  91.     {
  92.         $class                $this->id;
  93.         $format_string        '';
  94.         $show_link            false;
  95.         $link_url            '';
  96.         $empty_replacement    '';
  97.  
  98.         // Get field parameters
  99.         if ($this->element['class'])
  100.         {
  101.             $class = (string) $this->element['class'];
  102.         }
  103.  
  104.         if ($this->element['format'])
  105.         {
  106.             $format_string = (string) $this->element['format'];
  107.         }
  108.  
  109.         if ($this->element['show_link'== 'true')
  110.         {
  111.             $show_link true;
  112.         }
  113.  
  114.         if ($this->element['url'])
  115.         {
  116.             $link_url $this->element['url'];
  117.         }
  118.         else
  119.         {
  120.             $show_link false;
  121.         }
  122.  
  123.         if ($show_link && ($this->item instanceof FOFTable))
  124.         {
  125.             // Replace [ITEM:ID] in the URL with the item's key value (usually:
  126.             // the auto-incrementing numeric ID)
  127.             $keyfield $this->item->getKeyName();
  128.             $replace  $this->item->$keyfield;
  129.             $link_url str_replace('[ITEM:ID]'$replace$link_url);
  130.  
  131.             // Replace other field variables in the URL
  132.             $fields $this->item->getFields();
  133.  
  134.             foreach ($fields as $fielddata)
  135.             {
  136.                 $fieldname $fielddata->Field;
  137.  
  138.                 if (empty($fieldname))
  139.                 {
  140.                     $fieldname $fielddata->column_name;
  141.                 }
  142.  
  143.                 $search    '[ITEM:' strtoupper($fieldname']';
  144.                 $replace   $this->item->$fieldname;
  145.                 $link_url  str_replace($search$replace$link_url);
  146.             }
  147.         }
  148.         else
  149.         {
  150.             $show_link false;
  151.         }
  152.  
  153.         if ($this->element['empty_replacement'])
  154.         {
  155.             $empty_replacement = (string) $this->element['empty_replacement'];
  156.         }
  157.  
  158.         $value FOFFormFieldList::getOptionName($this->getOptions()$this->value);
  159.  
  160.         // Get the (optionally formatted) value
  161.         if (!empty($empty_replacement&& empty($value))
  162.         {
  163.             $value JText::_($empty_replacement);
  164.         }
  165.  
  166.         if (empty($format_string))
  167.         {
  168.             $value htmlspecialchars($valueENT_COMPAT'UTF-8');
  169.         }
  170.         else
  171.         {
  172.             $value sprintf($format_string$value);
  173.         }
  174.  
  175.         // Create the HTML
  176.         $html '<span class="' $class '">';
  177.  
  178.         if ($show_link)
  179.         {
  180.             $html .= '<a href="' $link_url '">';
  181.         }
  182.  
  183.         $html .= $value;
  184.  
  185.         if ($show_link)
  186.         {
  187.             $html .= '</a>';
  188.         }
  189.  
  190.         $html .= '</span>';
  191.  
  192.         return $html;
  193.     }
  194.  
  195.     /**
  196.      * Method to get the field options.
  197.      *
  198.      * @return  array  The field option objects.
  199.      */
  200.     protected function getOptions()
  201.     {
  202.         $options array();
  203.  
  204.         // Initialize some field attributes.
  205.         $key $this->element['key_field'? (string) $this->element['key_field''value';
  206.         $value $this->element['value_field'? (string) $this->element['value_field': (string) $this->element['name'];
  207.         $translate $this->element['translate'? (string) $this->element['translate'false;
  208.         $applyAccess $this->element['apply_access'? (string) $this->element['apply_access''false';
  209.         $modelName = (string) $this->element['model'];
  210.         $nonePlaceholder = (string) $this->element['none'];
  211.  
  212.         if (!empty($nonePlaceholder))
  213.         {
  214.             $options[JHtml::_('select.option'JText::_($nonePlaceholder)null);
  215.         }
  216.  
  217.         // Process field atrtibutes
  218.         $applyAccess strtolower($applyAccess);
  219.         $applyAccess in_array($applyAccessarray('yes''on''true''1'));
  220.  
  221.         // Explode model name into model name and prefix
  222.         $parts FOFInflector::explode($modelName);
  223.         $mName ucfirst(array_pop($parts));
  224.         $mPrefix FOFInflector::implode($parts);
  225.  
  226.         // Get the model object
  227.         $config array('savestate' => 0);
  228.         $model FOFModel::getTmpInstance($mName$mPrefix$config);
  229.  
  230.         if ($applyAccess)
  231.         {
  232.             $model->applyAccessFiltering();
  233.         }
  234.  
  235.         // Process state variables
  236.         foreach ($this->element->children(as $stateoption)
  237.         {
  238.             // Only add <option /> elements.
  239.             if ($stateoption->getName(!= 'state')
  240.             {
  241.                 continue;
  242.             }
  243.  
  244.             $stateKey = (string) $stateoption['key'];
  245.             $stateValue = (string) $stateoption;
  246.  
  247.             $model->setState($stateKey$stateValue);
  248.         }
  249.  
  250.         // Set the query and get the result list.
  251.         $items $model->getItemList(true);
  252.  
  253.         // Build the field options.
  254.         if (!empty($items))
  255.         {
  256.             foreach ($items as $item)
  257.             {
  258.                 if ($translate == true)
  259.                 {
  260.                     $options[JHtml::_('select.option'$item->$keyJText::_($item->$value));
  261.                 }
  262.                 else
  263.                 {
  264.                     $options[JHtml::_('select.option'$item->$key$item->$value);
  265.                 }
  266.             }
  267.         }
  268.  
  269.         // Merge any additional options in the XML definition.
  270.         $options array_merge(parent::getOptions()$options);
  271.  
  272.         return $options;
  273.     }
  274. }

Documentation generated on Tue, 19 Nov 2013 15:08:36 +0100 by phpDocumentor 1.4.3