1 <?php
  2 /**
  3  * @package    FrameworkOnFramework
  4  * @subpackage form
  5  * @copyright   Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / 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('FOF_INCLUDED') or die;
 10 
 11 JFormHelper::loadFieldClass('list');
 12 
 13 /**
 14  * Form Field class for FOF
 15  * Supports a generic list of options.
 16  *
 17  * @package  FrameworkOnFramework
 18  * @since    2.0
 19  */
 20 class FOFFormFieldGroupedbutton extends JFormFieldText implements FOFFormField
 21 {
 22     protected $static;
 23 
 24     protected $repeatable;
 25 
 26     /** @var   FOFTable  The item being rendered in a repeatable form field */
 27     public $item;
 28 
 29     /** @var int A monotonically increasing number, denoting the row number in a repeatable view */
 30     public $rowid;
 31 
 32     /**
 33      * Method to get certain otherwise inaccessible properties from the form field object.
 34      *
 35      * @param   string  $name  The property name for which to the the value.
 36      *
 37      * @return  mixed  The property value or null.
 38      *
 39      * @since   2.0
 40      */
 41     public function __get($name)
 42     {
 43         switch ($name)
 44         {
 45             case 'static':
 46                 if (empty($this->static))
 47                 {
 48                     $this->static = $this->getStatic();
 49                 }
 50 
 51                 return $this->static;
 52                 break;
 53 
 54             case 'repeatable':
 55                 if (empty($this->repeatable))
 56                 {
 57                     $this->repeatable = $this->getRepeatable();
 58                 }
 59 
 60                 return $this->repeatable;
 61                 break;
 62 
 63             default:
 64                 return parent::__get($name);
 65         }
 66     }
 67 
 68     /**
 69      * Get the rendering of this field type for static display, e.g. in a single
 70      * item view (typically a "read" task).
 71      *
 72      * @since 2.0
 73      *
 74      * @return  string  The field HTML
 75      */
 76     public function getStatic()
 77     {
 78         return $this->getInput();
 79     }
 80 
 81     /**
 82      * Get the rendering of this field type for a repeatable (grid) display,
 83      * e.g. in a view listing many item (typically a "browse" task)
 84      *
 85      * @since 2.0
 86      *
 87      * @return  string  The field HTML
 88      */
 89     public function getRepeatable()
 90     {
 91         return $this->getInput();
 92     }
 93 
 94     /**
 95      * Get the rendering of this field type for static display, e.g. in a single
 96      * item view (typically a "read" task).
 97      *
 98      * @since 2.0
 99      *
100      * @return  string  The field HTML
101      */
102     public function getInput()
103     {
104         $class = $this->element['class'] ? (string) $this->element['class'] : '';
105 
106         $html = '<div id="' . $this->id . '" class="btn-group ' . $class . '">';
107 
108         foreach ($this->element->children() as $option)
109         {
110             $renderedAttributes = array();
111 
112             foreach ($option->attributes() as $name => $value)
113             {
114                 if (!is_null($value))
115                 {
116                     $renderedAttributes[] = $name . '="' . htmlentities($value) . '"';
117                 }
118             }
119 
120             $buttonXML   = new SimpleXMLElement('<field ' . implode(' ', $renderedAttributes) . ' />');
121             $buttonField = new FOFFormFieldButton($this->form);
122 
123             // Pass required objects to the field
124             $buttonField->item = $this->item;
125             $buttonField->rowid = $this->rowid;
126             $buttonField->setup($buttonXML, null);
127 
128             $html .= $buttonField->getRepeatable();
129         }
130         $html .= '</div>';
131 
132         return $html;
133     }
134 }
135