1 <?php
  2 /**
  3  * @package     Joomla.Libraries
  4  * @subpackage  Form
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
  7  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8  */
  9 
 10 defined('JPATH_PLATFORM') or die;
 11 
 12 JFormHelper::loadFieldClass('text');
 13 
 14 /**
 15  * Module Position field.
 16  *
 17  * @since  1.6
 18  */
 19 class JFormFieldModulePosition extends JFormFieldText
 20 {
 21     /**
 22      * The form field type.
 23      *
 24      * @var    string
 25      * @since  1.6
 26      */
 27     protected $type = 'ModulePosition';
 28 
 29     /**
 30      * The client ID.
 31      *
 32      * @var    integer
 33      * @since  3.2
 34      */
 35     protected $clientId;
 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   3.2
 45      */
 46     public function __get($name)
 47     {
 48         switch ($name)
 49         {
 50             case 'clientId':
 51                 return $this->clientId;
 52         }
 53 
 54         return parent::__get($name);
 55     }
 56 
 57     /**
 58      * Method to set certain otherwise inaccessible properties of the form field object.
 59      *
 60      * @param   string  $name   The property name for which to the the value.
 61      * @param   mixed   $value  The value of the property.
 62      *
 63      * @return  void
 64      *
 65      * @since   3.2
 66      */
 67     public function __set($name, $value)
 68     {
 69         switch ($name)
 70         {
 71             case 'clientId':
 72                 $this->clientId = (string) $value;
 73                 break;
 74 
 75             default:
 76                 parent::__set($name, $value);
 77         }
 78     }
 79 
 80     /**
 81      * Method to attach a JForm object to the field.
 82      *
 83      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 84      * @param   mixed             $value    The form field value to validate.
 85      * @param   string            $group    The field name group control value. This acts as an array container for the field.
 86      *                                      For example if the field has name="foo" and the group value is set to "bar" then the
 87      *                                      full field name would end up being "bar[foo]".
 88      *
 89      * @return  boolean  True on success.
 90      *
 91      * @see     JFormField::setup()
 92      * @since   3.2
 93      */
 94     public function setup(SimpleXMLElement $element, $value, $group = null)
 95     {
 96         $result = parent::setup($element, $value, $group);
 97 
 98         if ($result === true)
 99         {
100             // Get the client id.
101             $clientId = $this->element['client_id'];
102 
103             if (!isset($clientId))
104             {
105                 $clientName = $this->element['client'];
106 
107                 if (isset($clientName))
108                 {
109                     $client = JApplicationHelper::getClientInfo($clientName, true);
110                     $clientId = $client->id;
111                 }
112             }
113 
114             if (!isset($clientId) && $this->form instanceof JForm)
115             {
116                 $clientId = $this->form->getValue('client_id');
117             }
118 
119             $this->clientId = (int) $clientId;
120         }
121 
122         return $result;
123     }
124 
125     /**
126      * Method to get the field input markup.
127      *
128      * @return  string  The field input markup.
129      *
130      * @since   1.6
131      */
132     protected function getInput()
133     {
134         // Load the modal behavior script.
135         JHtml::_('behavior.modal', 'a.modal');
136 
137         // Build the script.
138         $script = array();
139         $script[] = '   function jSelectPosition_' . $this->id . '(name) {';
140         $script[] = '       document.getElementById("' . $this->id . '").value = name;';
141         $script[] = '       jModalClose();';
142         $script[] = '   }';
143 
144         // Add the script to the document head.
145         JFactory::getDocument()->addScriptDeclaration(implode("\n", $script));
146 
147         // Setup variables for display.
148         $html = array();
149         $link = 'index.php?option=com_modules&view=positions&layout=modal&tmpl=component&function=jSelectPosition_' . $this->id
150             . '&client_id=' . $this->clientId;
151 
152         // The current user display field.
153         $html[] = '<div class="input-append">';
154         $html[] = parent::getInput()
155             . '<a class="btn modal" title="' . JText::_('COM_MODULES_CHANGE_POSITION_TITLE') . '"  href="' . $link
156             . '" rel="{handler: \'iframe\', size: {x: 800, y: 450}}">'
157             . JText::_('COM_MODULES_CHANGE_POSITION_BUTTON') . '</a>';
158         $html[] = '</div>';
159 
160         return implode("\n", $html);
161     }
162 }
163