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 /**
 13  * Provides a modal media selector including upload mechanism
 14  *
 15  * @since  1.6
 16  */
 17 class JFormFieldMedia extends JFormField
 18 {
 19     /**
 20      * The form field type.
 21      *
 22      * @var    string
 23      * @since  1.6
 24      */
 25     protected $type = 'Media';
 26 
 27     /**
 28      * The authorField.
 29      *
 30      * @var    string
 31      * @since  3.2
 32      */
 33     protected $authorField;
 34 
 35     /**
 36      * The asset.
 37      *
 38      * @var    string
 39      * @since  3.2
 40      */
 41     protected $asset;
 42 
 43     /**
 44      * The link.
 45      *
 46      * @var    string
 47      * @since  3.2
 48      */
 49     protected $link;
 50 
 51     /**
 52      * Modal width.
 53      *
 54      * @var    integer
 55      * @since  3.4.5
 56      */
 57     protected $width;
 58 
 59     /**
 60      * Modal height.
 61      *
 62      * @var    integer
 63      * @since  3.4.5
 64      */
 65     protected $height;
 66 
 67     /**
 68      * The authorField.
 69      *
 70      * @var    string
 71      * @since  3.2
 72      */
 73     protected $preview;
 74 
 75     /**
 76      * The preview.
 77      *
 78      * @var    string
 79      * @since  3.2
 80      */
 81     protected $directory;
 82 
 83     /**
 84      * The previewWidth.
 85      *
 86      * @var    int
 87      * @since  3.2
 88      */
 89     protected $previewWidth;
 90 
 91     /**
 92      * The previewHeight.
 93      *
 94      * @var    int
 95      * @since  3.2
 96      */
 97     protected $previewHeight;
 98 
 99     /**
100      * Layout to render
101      *
102      * @var    string
103      * @since  3.5
104      */
105     protected $layout = 'joomla.form.field.media';
106 
107     /**
108      * Method to get certain otherwise inaccessible properties from the form field object.
109      *
110      * @param   string  $name  The property name for which to the the value.
111      *
112      * @return  mixed  The property value or null.
113      *
114      * @since   3.2
115      */
116     public function __get($name)
117     {
118         switch ($name)
119         {
120             case 'authorField':
121             case 'asset':
122             case 'link':
123             case 'width':
124             case 'height':
125             case 'preview':
126             case 'directory':
127             case 'previewWidth':
128             case 'previewHeight':
129                 return $this->$name;
130         }
131 
132         return parent::__get($name);
133     }
134 
135     /**
136      * Method to set certain otherwise inaccessible properties of the form field object.
137      *
138      * @param   string  $name   The property name for which to the the value.
139      * @param   mixed   $value  The value of the property.
140      *
141      * @return  void
142      *
143      * @since   3.2
144      */
145     public function __set($name, $value)
146     {
147         switch ($name)
148         {
149             case 'authorField':
150             case 'asset':
151             case 'link':
152             case 'width':
153             case 'height':
154             case 'preview':
155             case 'directory':
156                 $this->$name = (string) $value;
157                 break;
158 
159             case 'previewWidth':
160             case 'previewHeight':
161                 $this->$name = (int) $value;
162                 break;
163 
164             default:
165                 parent::__set($name, $value);
166         }
167     }
168 
169     /**
170      * Method to attach a JForm object to the field.
171      *
172      * @param   SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
173      * @param   mixed             $value    The form field value to validate.
174      * @param   string            $group    The field name group control value. This acts as an array container for the field.
175      *                                      For example if the field has name="foo" and the group value is set to "bar" then the
176      *                                      full field name would end up being "bar[foo]".
177      *
178      * @return  boolean  True on success.
179      *
180      * @see     JFormField::setup()
181      * @since   3.2
182      */
183     public function setup(SimpleXMLElement $element, $value, $group = null)
184     {
185         $result = parent::setup($element, $value, $group);
186 
187         if ($result === true)
188         {
189             $assetField = $this->element['asset_field'] ? (string) $this->element['asset_field'] : 'asset_id';
190 
191             $this->authorField   = $this->element['created_by_field'] ? (string) $this->element['created_by_field'] : 'created_by';
192             $this->asset         = $this->form->getValue($assetField) ?: (string) $this->element['asset_id'];
193             $this->link          = (string) $this->element['link'];
194             $this->width         = isset($this->element['width']) ? (int) $this->element['width'] : 800;
195             $this->height        = isset($this->element['height']) ? (int) $this->element['height'] : 500;
196             $this->preview       = (string) $this->element['preview'];
197             $this->directory     = (string) $this->element['directory'];
198             $this->previewWidth  = isset($this->element['preview_width']) ? (int) $this->element['preview_width'] : 200;
199             $this->previewHeight = isset($this->element['preview_height']) ? (int) $this->element['preview_height'] : 200;
200         }
201 
202         return $result;
203     }
204 
205     /**
206      * Method to get the field input markup for a media selector.
207      * Use attributes to identify specific created_by and asset_id fields
208      *
209      * @return  string  The field input markup.
210      *
211      * @since   1.6
212      */
213     protected function getInput()
214     {
215         if (empty($this->layout))
216         {
217             throw new UnexpectedValueException(sprintf('%s has no layout assigned.', $this->name));
218         }
219 
220         return $this->getRenderer($this->layout)->render($this->getLayoutData());
221     }
222 
223     /**
224      * Get the data that is going to be passed to the layout
225      *
226      * @return  array
227      */
228     public function getLayoutData()
229     {
230         // Get the basic field data
231         $data = parent::getLayoutData();
232 
233         $asset = $this->asset;
234 
235         if ($asset === '')
236         {
237             $asset = JFactory::getApplication()->input->get('option');
238         }
239 
240         if ($this->value && file_exists(JPATH_ROOT . '/' . $this->value))
241         {
242             $this->folder = explode('/', $this->value);
243             $this->folder = array_diff_assoc($this->folder, explode('/', JComponentHelper::getParams('com_media')->get('image_path', 'images')));
244             array_pop($this->folder);
245             $this->folder = implode('/', $this->folder);
246         }
247         elseif (file_exists(JPATH_ROOT . '/' . JComponentHelper::getParams('com_media')->get('image_path', 'images') . '/' . $this->directory))
248         {
249             $this->folder = $this->directory;
250         }
251         else
252         {
253             $this->folder = '';
254         }
255 
256         $extraData = array(
257             'asset'         => $asset,
258             'authorField'   => $this->authorField,
259             'authorId'      => $this->form->getValue($this->authorField),
260             'folder'        => $this->folder,
261             'link'          => $this->link,
262             'preview'       => $this->preview,
263             'previewHeight' => $this->previewHeight,
264             'previewWidth'  => $this->previewWidth,
265         );
266 
267         return array_merge($data, $extraData);
268     }
269 }
270