PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/jyaml/form/fields/jyamltemplatestylesheets.php

https://bitbucket.org/hieblmedia/jyaml
PHP | 424 lines | 276 code | 77 blank | 71 comment | 31 complexity | 07a2a0f24462f7995d1dcd465ce62896 MD5 | raw file
  1. <?php
  2. /**
  3. * JYAML - Template Framework includes YAML for Joomla!
  4. *
  5. * All rights reserved. The JYAML project is a template to manage and
  6. * configure Joomla!-Templates with the YAML XHTML/CSS Framework
  7. * - http://www.yaml.de
  8. *
  9. * -----------------------------------------------------------------------------
  10. *
  11. * @package JYAML
  12. * @subpackage JYAML.Admin
  13. *
  14. * @author Reinhard Hiebl <reinhard@hieblmedia.com>
  15. * @copyright Copyright (C) 2006 - 2016, HieblMedia (Reinhard Hiebl)
  16. * @license http://www.jyaml.de/en/license Creative Commons Attribution 3.0
  17. * @link http://www.jyaml.de
  18. */
  19. defined('_JEXEC') or die;
  20. jimport('joomla.form.formfield');
  21. jimport('joomla.filesystem.folder');
  22. jimport('joomla.filesystem.file');
  23. /**
  24. * JYAML Stylesheets Form Field
  25. *
  26. * @package JYAML
  27. * @subpackage JYAML.Admin
  28. * @since 4.0.0
  29. */
  30. class JFormFieldJyamltemplatestylesheets extends JFormField
  31. {
  32. /**
  33. * The form field type
  34. */
  35. protected $type = 'JFormFieldJyamltemplatestylesheets';
  36. /**
  37. * Method to instantiate the form field object.
  38. *
  39. * @param JForm $form The form to attach to the form field object.
  40. *
  41. * @since 11.1
  42. */
  43. public function __construct($form = null)
  44. {
  45. $document = JFactory::getDocument();
  46. JHtml::_('jquery.framework');
  47. JHtml::_('jquery.ui', array('core', 'sortable'));
  48. $document->addScript(JURI::root(true) . '/libraries/jyaml/assets/js/fields/jyaml.js');
  49. $document->addStylesheet(JURI::root(true) . '/libraries/jyaml/assets/css/fields/jyaml.css');
  50. parent::__construct($form);
  51. }
  52. /**
  53. * Get the field input markup
  54. *
  55. * @return string Form field input
  56. */
  57. protected function getInput()
  58. {
  59. $clientId = $this->form->getValue('client_id');
  60. $client = JApplicationHelper::getClientInfo($clientId);
  61. $this->clientPath = $client->path;
  62. $this->template = $this->form->getValue('template');
  63. $this->templatePath = $this->clientPath . '/templates/' . $this->template;
  64. $formParams = $this->form->getValue('params');
  65. $params = new \Joomla\Registry\Registry;
  66. $params->loadObject($formParams);
  67. $stylesheets = (array) $this->value;
  68. // Dummy for new script
  69. $dummy = array('file' => '', 'media' => 'all', 'IEconditionalComment' => '', 'dummy' => '1');
  70. $stylesheets = array_merge($stylesheets, array($dummy));
  71. $html = '';
  72. $html .= '<div clas="jyamltemplatestylesheets_form">';
  73. $regexName = preg_quote($this->name, '/');
  74. $groupRegex = '(' . $regexName . '\[)([0-9]*|dummy)(\].*)';
  75. $html .= '<div class="jyaml-move-group" data-group-regex="' . $groupRegex . '">';
  76. if (!empty($stylesheets))
  77. {
  78. $i = 0;
  79. foreach ($stylesheets as $stylesheet)
  80. {
  81. $stylesheet = new \Joomla\Registry\Registry($stylesheet);
  82. $file = $stylesheet->get('file', '');
  83. $media = $stylesheet->get('media', 'all');
  84. $IEcc = $stylesheet->get('IEconditionalComment', '');
  85. $isDummy = ($stylesheet->get('dummy', false) ? true : false);
  86. if (!$isDummy && empty($file))
  87. {
  88. continue;
  89. }
  90. $k = $isDummy ? 'dummy' : $i;
  91. $html .= '<div class="jyaml-group-entry' . ($isDummy ? '-dummy' : '') . ' jyaml-group-entry-bordered"'
  92. . ($isDummy ? ' style="display:none"' : '') . '>';
  93. $html .= '<span class="btn btn-mini jyaml-group-move-btn" title="' . JText::_('JYAML_STYLESHEET_MOVE')
  94. . '"><i class="icon-move"></i></span>';
  95. $html .= $this->getStylesheetSelect($file, $isDummy, $k);
  96. $html .= $this->getMediaSelect($media, $k);
  97. $html .= '<button class="btn btn-danger btn-mini jyaml-group-entry-remove" data-confirm="'
  98. . JText::_('JYAML_STYLESHEET_CONFIRM_REMOVE') . '" type="button" title="'
  99. . JText::_('JYAML_STYLESHEET_REMOVE') . '"><i class="icon-remove"></i></button>';
  100. $html .= '<hr /><div class="jyamltemplate-advanced-container">';
  101. $html .= $this->getIEConditionalCommentHtml($IEcc, $isDummy, $k);
  102. $html .= '</div>';
  103. $html .= '</div>';
  104. ++$i;
  105. }
  106. }
  107. $html .= '</div>';
  108. $html .= '<button class="btn btn-success jyaml-group-entry-add" type="button"><i class="icon-plus"></i> '
  109. . JText::_('JYAML_STYLESHEET_ADD') . '</button>';
  110. $html .= '</div>';
  111. return $html;
  112. }
  113. /**
  114. * Get Stylesheet File select
  115. *
  116. * @param string $selected Selected
  117. * @param boolean $disabled Disabled
  118. * @param int $k Key
  119. *
  120. * @return string Html input
  121. */
  122. private function getStylesheetSelect($selected = '', $disabled = false, $k = 0)
  123. {
  124. static $optionsCache;
  125. $name = $this->name . '[' . $k . '][file]';
  126. $id = $this->name . '_file_' . $k;
  127. $id = str_replace(array('[', ']'), '', $id);
  128. $options = array();
  129. if ($optionsCache == null)
  130. {
  131. $forceAutoload = array(
  132. '{$source.theme}editor.css',
  133. '{$source.theme}template.css',
  134. '{$source.theme}system.error.css'
  135. );
  136. $optionsCache = array();
  137. $themePath = $this->clientPath . '/templates/' . $this->template . '/css';
  138. $yamlPath = JYAML_LIB_PATH . '/yaml';
  139. $themeFiles = array();
  140. if (JFolder::exists($themePath))
  141. {
  142. $themeFiles = JFolder::files($themePath, '.css$', true, true);
  143. sort($themeFiles);
  144. }
  145. $yamlFiles = array();
  146. if (JFolder::exists($yamlPath))
  147. {
  148. $yamlFiles = JFolder::files($yamlPath, '.css$', true, true);
  149. sort($yamlFiles);
  150. }
  151. $autoloadFilesProcessed = array();
  152. $autoloadFiles = JYAML::getAutoLoadFiles(
  153. $this->clientPath . '/templates/' . $this->template, 'stylesheets', false, true
  154. );
  155. $options[] = JHtml::_('select.option', '', '--' . JText::_('JYAML_SELECT') . '--', 'value', 'text');
  156. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_STYLESHEET_THEME_FILES', '/templates/' . $this->template . '/css/'));
  157. foreach ($themeFiles as $file)
  158. {
  159. if (preg_match('#skin\.(.*)\.css#Uis', basename($file)))
  160. {
  161. // Exclude skin files (skin.*.css)
  162. continue;
  163. }
  164. $file = JPath::clean($file, DIRECTORY_SEPARATOR);
  165. $themePath = JPath::clean($themePath, DIRECTORY_SEPARATOR);
  166. $_disabled = false;
  167. $value = str_replace($themePath . DIRECTORY_SEPARATOR, '{$source.theme}', $file);
  168. $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
  169. $text = str_replace($themePath . DIRECTORY_SEPARATOR, '', $file);
  170. $text = str_replace(DIRECTORY_SEPARATOR, '/', $text);
  171. $valueAlsoCheck = $value;
  172. if (strpos($value, '.min.css'))
  173. {
  174. $valueAlsoCheck = preg_replace('/.min.css$/', '.css', $valueAlsoCheck);
  175. }
  176. else
  177. {
  178. $valueAlsoCheck = preg_replace('/.css$/', '.min.css', $valueAlsoCheck);
  179. }
  180. // Rtl stylesheets always autoloaded
  181. $isRTL = (preg_match('#[_-]rtl.css$#Uis', $value) || preg_match('#[_-]rtl.css$#Uis', $valueAlsoCheck));
  182. if (in_array($value, $autoloadFiles) || in_array($valueAlsoCheck, $autoloadFiles) || $isRTL || in_array($value, $forceAutoload))
  183. {
  184. $_disabled = true;
  185. $text = $text . ' - (autoload)';
  186. }
  187. $autoloadFilesProcessed[] = $value;
  188. $options[] = JHtml::_('select.option', $value, $text, 'value', 'text', $_disabled);
  189. }
  190. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_STYLESHEET_THEME_FILES', '/templates/' . $this->template . '/css/'));
  191. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_STYLESHEET_YAML_FILES', '/templates/' . $this->template . '/yaml/'));
  192. foreach ($yamlFiles as $file)
  193. {
  194. $file = JPath::clean($file, DIRECTORY_SEPARATOR);
  195. $yamlPath = JPath::clean($yamlPath, DIRECTORY_SEPARATOR);
  196. $_disabled = false;
  197. $value = str_replace($yamlPath . DIRECTORY_SEPARATOR, '{$source.yaml}', $file);
  198. $value = str_replace(DIRECTORY_SEPARATOR, '/', $value);
  199. $text = str_replace($yamlPath . DIRECTORY_SEPARATOR, '', $file);
  200. $text = str_replace(DIRECTORY_SEPARATOR, '/', $text);
  201. $valueAlsoCheck = $value;
  202. if (strpos($value, '.min.css'))
  203. {
  204. $valueAlsoCheck = preg_replace('/.min.css$/', '.css', $valueAlsoCheck);
  205. }
  206. else
  207. {
  208. $valueAlsoCheck = preg_replace('/.css$/', '.min.css', $valueAlsoCheck);
  209. }
  210. // Rtl stylesheets always autoloaded
  211. $isRTL = (preg_match('#[_-]rtl.css$#Uis', $value) || preg_match('#[_-]rtl.css$#Uis', $valueAlsoCheck));
  212. if (in_array($value, $autoloadFiles) || in_array($valueAlsoCheck, $autoloadFiles)|| $isRTL || in_array($value, $forceAutoload))
  213. {
  214. $_disabled = true;
  215. $text = $text . ' - (autoload)';
  216. }
  217. $autoloadFilesProcessed[] = $value;
  218. $options[] = JHtml::_('select.option', $value, $text, 'value', 'text', $_disabled);
  219. }
  220. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_STYLESHEET_YAML_FILES', '/templates/' . $this->template . '/yaml/'));
  221. $otherFiles = array();
  222. foreach ($autoloadFiles as $_file)
  223. {
  224. if (!in_array($_file, $autoloadFilesProcessed))
  225. {
  226. $otherFiles[] = $_file;
  227. }
  228. }
  229. if (!empty($otherFiles))
  230. {
  231. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_OTHER_AUTOLOAD_FILES'));
  232. foreach ($otherFiles as $file)
  233. {
  234. $options[] = JHtml::_('select.option', $file, $file . ' - (autoload)', 'value', 'text', true);
  235. }
  236. $options[] = JHTML::_('select.optgroup', JText::sprintf('JYAML_OTHER_AUTOLOAD_FILES'));
  237. }
  238. $optionsCache = $options;
  239. }
  240. $options = $optionsCache;
  241. if (preg_match('#^(http|https|\/\/)#i', $selected))
  242. {
  243. $options[] = JHTML::_('select.optgroup', JText::_('JYAML_EXTERNAL'));
  244. $options[] = JHtml::_('select.option', $selected, $selected, 'value', 'text');
  245. $options[] = JHTML::_('select.optgroup', JText::_('JYAML_EXTERNAL'));
  246. }
  247. $select = '<label for="' . $id . '" class="jyaml-hide">' . JText::_('JYAML_STYLESHEET_FILE') . ':</label> ';
  248. $urlRegex = '^(http:\/\/|https:\/\/|\/\/).*';
  249. $attributes = 'class="jyamltemplatestylesheet-name jyaml-custom-select-input" data-remove-group="is_empty" data-type-custom-text="'
  250. . JText::_('JYAML_TYPE_EXTERNAL_URL') . '" data-type-custom-notrim="1" data-type-custom-regex="' . $urlRegex . '"';
  251. if ($disabled)
  252. {
  253. $attributes .= ' data-dummy-init="1" disabled="disabled" style="display:none"';
  254. }
  255. $select .= JHtml::_('select.genericlist', $options, $name, $attributes, 'value', 'text', $selected, $id);
  256. return $select;
  257. }
  258. /**
  259. * Get Stylesheet Media select
  260. *
  261. * @param string $selected Selected
  262. * @param boolean $disabled Disabled
  263. * @param int $k Key
  264. *
  265. * @return string Html input
  266. */
  267. private function getMediaSelect($selected = 'all', $disabled = false, $k = 0)
  268. {
  269. static $options;
  270. $name = $this->name . '[' . $k . '][media]';
  271. $id = $this->name . '_media_' . $k;
  272. $id = str_replace(array('[', ']'), '', $id);
  273. if ($options == null)
  274. {
  275. $options = array();
  276. $mediaOptions = array(
  277. 'all' => '',
  278. 'screen' => '',
  279. 'projection' => '',
  280. 'screen, procection' => '',
  281. 'print' => '',
  282. 'only screen and (min-device-width : 320px) and (max-device-width : 480px)' => 'Smartphones [portrait and landscape]',
  283. 'only screen and (min-width : 321px)' => 'Smartphones [landscape]',
  284. 'only screen and (max-width : 320px)' => 'Smartphones [portrait]',
  285. 'only screen and (min-device-width : 768px) and (max-device-width : 1024px)' => 'iPads [portrait and landscape]',
  286. 'only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)' => 'iPads [landscape]',
  287. 'only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)' => 'iPads [portrait]',
  288. 'only screen and (min-width : 1224px)' => 'Desktops and Laptops',
  289. 'only screen and (min-width : 1824px)' => 'Large screens',
  290. 'only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5)' => 'iPhone 4',
  291. 'only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min--moz-device-pixel-ratio: 2), '
  292. . 'only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min-device-pixel-ratio: 2), '
  293. . 'only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx)' => 'High pixel ratio [Retina]'
  294. );
  295. if ($selected != "" && !array_key_exists($selected, $mediaOptions))
  296. {
  297. $mediaOptions[$selected] = '';
  298. }
  299. $options[] = JHTML::_('select.optgroup', JText::_('JYAML_STYLESHEET_MEDIA'));
  300. foreach ($mediaOptions as $media => $desc)
  301. {
  302. $mediaLabel = $media;
  303. if (strlen($media) > 100)
  304. {
  305. // Truncate
  306. $mediaLabel = substr($mediaLabel, 0, 97) . '...';
  307. }
  308. $options[] = JHtml::_('select.option', $media, (!empty($desc) ? '(' . $desc . ') - ' : '') . $mediaLabel, 'value', 'text');
  309. }
  310. $options[] = JHTML::_('select.optgroup', JText::_('JYAML_STYLESHEET_MEDIA'));
  311. }
  312. $attributes = 'class="jyamltemplatestylesheet-media jyaml-custom-select-input"';
  313. if ($disabled)
  314. {
  315. $attributes .= ' data-dummy-init="1" disabled="disabled" style="display:none"';
  316. }
  317. $select = '<label for="' . $id . '" class="jyaml-hide">' . JText::_('JYAML_STYLESHEET_MEDIA') . ': </label>';
  318. $select .= JHtml::_('select.genericlist', $options, $name, $attributes, 'value', 'text', $selected, $id);
  319. return $select;
  320. }
  321. /**
  322. * Get IECC HTML
  323. *
  324. * @param string $value Current value
  325. * @param boolean $disabled Disabled
  326. * @param int $k Key
  327. *
  328. * @return string Html input
  329. */
  330. private function getIEConditionalCommentHtml($value = '', $disabled = false, $k = 0)
  331. {
  332. $name = $this->name . '[' . $k . '][IEconditionalComment]';
  333. $id = $this->name . '_IEconditionalComment_' . $k;
  334. $id = str_replace(array('[', ']'), '', $id);
  335. $html = '';
  336. $attributes = 'class="input-small"';
  337. if ($disabled)
  338. {
  339. $attributes .= ' data-dummy-init="1" disabled="disabled" style="display:none"';
  340. }
  341. $html .= '<label for="' . $id . '">' . JText::_('JYAML_CONDITIONAL_COMMENT_IE') . '</label> ';
  342. $html .= '&lt;!--[<input type="text" value="' . $value . '" name="' . $name . '" id="'
  343. . $id . '" ' . $attributes . ' />]&gt;&nbsp;<small>['
  344. . JText::_('JYAML_STYLESHEET') . ']</small> &lt;![endif]--&gt;';
  345. return $html;
  346. }
  347. }