PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_roksprocket/lib/RokSprocket/Form/Fields/Wordpress/imagepicker.php

https://gitlab.com/che234/adn
PHP | 267 lines | 174 code | 45 blank | 48 comment | 36 complexity | 8b85f727968fbe4101ef8c79ccb0eb95 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: imagepicker.php 11812 2013-06-27 17:49:30Z djamil $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. class RokCommon_Form_Field_ImagePicker extends RokCommon_Form_AbstractField
  9. {
  10. protected $type = 'ImagePicker';
  11. protected static $assets_loaded = false;
  12. protected $options = array();
  13. protected $data;
  14. protected $isCustom = true;
  15. function getInput(){
  16. //JHTML::_('behavior.modal');
  17. //$this->_loadAssets();
  18. $this->_setOptions();
  19. $container = RokCommon_Service::getContainer();
  20. /** @var RokSprocket_IProvider $provider */
  21. $provider = $container->getParameter('roksprocket.current_provider');
  22. $provider->filterPerItemTypes($this->type, $this->fieldname, $this->options);
  23. $this->value = str_replace("'", '"', str_replace('\\', '', $this->value));
  24. $link = $this->options['mediamanager']['attributes']['value'];
  25. if (!$this->value){
  26. $link = $this->options['mediamanager']['attributes']['value'];
  27. $this->value = '{"type":"mediamanager","path":"","preview":"","link":"'.$link.'"}';
  28. }
  29. if (preg_match("/^-([a-z]{1,})-$/", $this->value)){
  30. if ($this->value == '-primary-') $this->value = '-article-';
  31. $this->isCustom = false;
  32. } else if (!preg_match("/[{]/", $this->value)){
  33. $this->value = '{"type":"mediamanager","path":"'.$this->value.'","preview":"","link":"'.$link.'"}';
  34. }
  35. $this->data = json_decode($this->value);
  36. if (empty($this->data->path) && !preg_match("/^-([a-z]{1,})-$/", $this->value)) $this->value = "";
  37. if (isset($this->data->link)) $link = $this->data->link;
  38. $class = $this->element['class'] ? ' class="' . (string) $this->element['class'] . ' imagepicker'.((count($this->options) == 1) ? ' single' : '').'"' : ' class="imagepicker"';
  39. $placeholder = $this->element['placeholder'] ? ' placeholder="' .(string)$this->element['placeholder'] .'"' : '';
  40. if (isset($this->data->path)){
  41. $preview = !empty($this->data->preview) ? $this->data->preview : $this->data->path;
  42. if (!preg_match("/^https?:\/\//", $preview) && substr($preview, 0, 1) != '/'){
  43. //$preview = JURI::root(true) . '/' . $preview;
  44. }
  45. $tipTitle = '<div class=\'imagepicker-tip-preview\'><img src=\''.$preview.'\' /></div>';
  46. if (isset($this->data->width)) $tipTitle .= '<div class=\'imagepicker-tip-size\'>'.$this->data->width.' &times; '.$this->data->height.'</div>';
  47. $tipTitle .= '<div class=\'imagepicker-tip-path\'>'.$this->data->path.'</div>';
  48. $path = $this->data->path;
  49. if (!strlen($path)) $tipTitle = '';
  50. } else {
  51. $path = '';
  52. $tipTitle = "";
  53. }
  54. $html = array();
  55. $html[] = '<div class="imagepicker-wrapper'.(!$this->isCustom ? ' peritempicker-noncustom' : '').'" data-imagepicker="true" data-imagepicker-id="'.$this->id.'" data-imagepicker-name="'.$this->name.'">';
  56. $html[] = ' <input data-imagepicker-display="true" data-original-title="'.rc__($tipTitle).'" type="text" value="'.$path.'" '.$class.$placeholder.' />';
  57. $html[] = ' <input type="hidden" id="'.$this->id.'" name="'.$this->name.'" value="'.htmlspecialchars($this->value).'" />';
  58. $html[] = $this->_getDropdown();
  59. $html[] = ' <a href="'.$link.'" class="thickbox imagepicker" title="Select Image" rel="{handler: \'iframe\', size: {x: 695, y: 450}}"><i class="icon tool picker"></i></a>';
  60. $html[] = '</div>';
  61. return implode("\n", $html);
  62. }
  63. public function _setOptions(){
  64. $options = array();
  65. $platforminfo = $this->container->getService('platforminfo');
  66. if (!isset($this->element['article-defaults'])){
  67. // Defaults List
  68. $options['-none-'] = array(
  69. 'name' => rc__('JNONE'),
  70. 'attributes' => array(
  71. 'value' => '-none-',
  72. 'icon' => false,
  73. 'data-value' => '-none-'
  74. )
  75. );
  76. $options['-default-'] = array(
  77. 'name' => rc__('JDEFAULT'),
  78. 'attributes' => array(
  79. 'value' => '-default-',
  80. 'icon' => false,
  81. 'data-value' => '-default-'
  82. )
  83. );
  84. $options['-article-'] = array(
  85. 'name' => rc__('ROKSPROCKET_ARTICLE_IMAGE'),
  86. 'attributes' => array(
  87. 'value' => '-article-',
  88. 'icon' => false,
  89. 'data-value' => '-article-'
  90. )
  91. );
  92. // Divider
  93. $options['divider'] = array(
  94. 'name' => '',
  95. 'attributes' => array(
  96. 'class' => 'divider',
  97. 'data-divider' => 'true'
  98. )
  99. );
  100. }
  101. // Default Wordpress Media Manager
  102. $options['mediamanager'] = array(
  103. 'name' => 'MediaManager',
  104. 'attributes' => array(
  105. 'value' => $platforminfo->getRootUrl().'/wp-admin/media-upload.php?post_id=0&width=640&height=687&e_name=' . $this->id . '&TB_iframe=1',
  106. 'icon' => 'mediamanager',
  107. 'data-value' => 'mediamanager'
  108. )
  109. );
  110. // List of Supported components picker such as RokGallery here
  111. if ($this->_isInstalled('wp_rokgallery')){
  112. $options['rokgallery'] = array(
  113. 'name' => 'RokGallery',
  114. 'attributes' => array(
  115. 'value' => $platforminfo->getRootUrl().'/wp-admin/admin-ajax.php?action=rokgallery_gallerypicker&height=600&width=675&show_menuitems=0&inputfield=' . $this->id . '&modal=false&TB_iframe=true',
  116. 'icon' => 'rokgallery',
  117. 'data-value' => 'rokgallery'
  118. )
  119. );
  120. }
  121. $this->options = $options;
  122. return $options;
  123. }
  124. public function _isInstalled($component = false){
  125. $platforminfo = $this->container->getService('platforminfo');
  126. $installed = false;
  127. // Right now this is only checking for the folder is in the plugins/
  128. // really this should also check if the component is activated or not
  129. // like we do in joomla
  130. $installed = file_exists($platforminfo->getRootPath().'/wp-content/plugins/'.$component);
  131. //if (file_exists($platforminfo->getRootPath().'/wp-content/plugins/'.$component)) {
  132. // $component = JComponentHelper::getComponent($component, true);
  133. // $installed = $component->enabled;
  134. //}
  135. return $installed;
  136. }
  137. public function _getDropdown(){
  138. $render = $output = $list = array();
  139. $list = "";
  140. $displayValue = "mediamanager";
  141. $options = $this->options;
  142. if (isset($this->value) && !array_key_exists($this->value, $options)) $this->value = '-title-';
  143. if (isset($this->value) && !array_key_exists($this->value, $options)) $this->value = '-none-';
  144. foreach($options as $option){
  145. $attributes = $option['attributes'];
  146. $class = (isset($attributes['class']) ? $attributes['class'] : "") . (isset($attributes['disabled']) ? " disabled" : "");
  147. $class = (strlen($class) ? 'class="'. $class . '"' : "");
  148. $icon = (isset($attributes['icon']) ? $attributes['icon'] : "");
  149. $divider = (isset($attributes['data-divider']))? $attributes['data-divider'] : "";
  150. $dataValue = (isset($attributes['data-value']))? $attributes['data-value'] : '';
  151. $value = (isset($attributes['value']))? $attributes['value'] : '';
  152. if (isset($this->data->type) && $this->data->type == $dataValue){
  153. $displayValue = $dataValue;
  154. }
  155. if (strlen($icon)) $icon_html = '<i data-dynamic="false" class="icon media '.$attributes['icon'].'"></i>';
  156. else $icon_html = "";
  157. $list[] = ' <li '.$class.' data-dynamic="false" data-icon="media '.$icon.'" data-text="" data-value="'.$value.'">';
  158. if (!$divider) $list[] = ' <a href="#">'.$icon_html.'<span>'.$option['name'].'</span></a>';
  159. $list[] = ' </li>';
  160. }
  161. // rendering output
  162. $class = "media " . $displayValue;
  163. if (count($options) > 1){
  164. $output[] = '<div class="sprocket-dropdown imagepicker">';
  165. $output[] = ' <a href="#" class="btn dropdown-toggle" data-toggle="dropdown">';
  166. if (strlen($icon))
  167. $output[] = ' <i data-dynamic="false" class="icon '.$class.'"></i> ';
  168. $output[] = ' <span class="name">'.(!$this->isCustom ? $this->options[$this->value]['name'] : '').'</span>';
  169. $output[] = ' <span class="caret"></span>';
  170. $output[] = ' </a>';
  171. $output[] = ' <ul class="dropdown-menu">';
  172. $output[] = implode("\n", $list);
  173. $output[] = ' </ul>';
  174. $output[] = ' <div class="dropdown-original">';
  175. } else {
  176. $output[] = '<div class="sprocket-dropdown imagepicker">';
  177. $output[] = ' <div class="single-layout btn dropdown-toggle"><i class="icon '.$class.'"></i></div>';
  178. $output[] = '</div>';
  179. }
  180. // original select
  181. $output[] = ' <select class="chzn-done" '.((count($options) == 1) ? ' style="display: none;"' : '').'>';
  182. foreach($options as $option){
  183. $attributes = $option['attributes'];
  184. $divider = (isset($attributes['data-divider']))? $attributes['data-divider'] : "";
  185. $dataValue = (isset($attributes['data-value']))? $attributes['data-value'] : '';
  186. $value = (isset($attributes['value']))? $attributes['value'] : '';
  187. $selected = ((isset($this->data->type) && $dataValue == $this->data->type) || $dataValue == $this->value) ? ' selected="selected" ' : "";
  188. $output[] = ' <option value="' . $value . '" '.$selected.'>' . $option['name'] . '</option>';
  189. }
  190. $output[] = ' </select>';
  191. if (count($options) > 1){
  192. $output[] = ' </div>';
  193. $output[] = "</div>";
  194. }
  195. return implode("\n", $output);
  196. }
  197. /*public function _loadAssets(){
  198. if (!self::$assets_loaded){
  199. $type = strtolower($this->type);
  200. $assets = JURI::root() . 'components/' . JRequest::getString('option') . '/fields/' . $type . '/';
  201. $js = $assets . 'js/' . $type . '.js';
  202. RokCommon_Header::addScript($js);
  203. RokCommon_Header::addInlineScript($this->attachJavaScript());
  204. self::$assets_loaded = true;
  205. }
  206. }
  207. protected function attachJavaScript(){
  208. $js = array();
  209. $js[] = "window.addEvent('domready', function(){";
  210. $js[] = " RokSprocket.articles.addEvent('onModelSuccess', function(response){";
  211. $js[] = " var imagepickers = document.getElements('.articles [data-imagepicker]');";
  212. $js[] = " RokSprocket.imagepicker.attach(imagepickers);";
  213. $js[] = " SqueezeBox.assign(imagepickers.getElement('a.modal'), {parse: 'rel'});";
  214. $js[] = " });";
  215. $js[] = "});";
  216. return implode("\n", $js);
  217. }*/
  218. }