/libraries/src/Form/Field/MediaField.php

https://github.com/fastslack/joomla-cms · PHP · 273 lines · 118 code · 31 blank · 124 comment · 7 complexity · 71e2482e0c2e0a0903587e515ddc8c16 MD5 · raw file

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