PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/view/dmHelper.php

https://github.com/leszek/diem
PHP | 335 lines | 269 code | 54 blank | 12 comment | 31 complexity | 0c7bc24bbaa90e4ade1695189a3b4134 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class dmHelper extends dmConfigurable
  3. {
  4. protected
  5. $context,
  6. $serviceContainer;
  7. public function __construct(dmContext $context, array $options = array())
  8. {
  9. $this->context = $context;
  10. $this->serviceContainer = $context->getServiceContainer();
  11. $this->initialize($options);
  12. }
  13. public function initialize(array $options)
  14. {
  15. $this->configure($options);
  16. }
  17. public function getDefaultOptions()
  18. {
  19. return array(
  20. 'use_beaf' => false,
  21. 'empty_elements' => array('br', 'hr', 'img', 'input')
  22. );
  23. }
  24. public function renderPartial($moduleName, $actionName, $vars = array())
  25. {
  26. /*
  27. * partial -> _partial
  28. * dir/partial -> dir/partial
  29. */
  30. if (!strpos($actionName, '/'))
  31. {
  32. $actionName = '_'.$actionName;
  33. }
  34. $class = sfConfig::get('mod_'.strtolower($moduleName).'_partial_view_class', 'sf').'PartialView';
  35. $view = new $class($this->context, $moduleName, $actionName, '');
  36. $view->setPartialVars($vars);
  37. return $view->render();
  38. }
  39. public function renderComponent($moduleName, $componentName, $vars = array())
  40. {
  41. $this->context->getConfiguration()->loadHelpers('Partial');
  42. return get_component($moduleName, $componentName, $vars);
  43. }
  44. public function open($tagName, array $opt = array())
  45. {
  46. return $this->tag($tagName, $opt, false, false);
  47. }
  48. public function £o($tagName, array $opt = array())
  49. {
  50. return $this->open($tagName, $opt);
  51. }
  52. public function close($tagName)
  53. {
  54. if ($pos = strpos($tagName, '.') !== false)
  55. {
  56. $classes = substr($tagName, $pos+1);
  57. $tagName = substr($tagName, 0, $pos);
  58. if ($this->options['use_beaf'] && (strpos($classes, 'beafh') !== false || strpos($classes, 'beafv') !== false))
  59. {
  60. if (in_array($tagName, array('span', 'a', 'p')))
  61. {
  62. $beafTag = 'span';
  63. }
  64. else
  65. {
  66. $beafTag = 'div';
  67. }
  68. return '</'.$beafTag.'><'.$beafTag.' class="beafter"></'.$beafTag.'></'.$tagName.'>';
  69. }
  70. }
  71. return '</'.$tagName.'>';
  72. }
  73. public function £c($tagName)
  74. {
  75. return $this->close($tagName);
  76. }
  77. public function tag($tagName, $opt = array(), $content = false, $openAndClose = true)
  78. {
  79. if (!($tagName = trim($tagName)))
  80. {
  81. return '';
  82. }
  83. $tagOpt = array();
  84. // separate tag name from attribues in $tagName
  85. if ($firstSpacePos = strpos($tagName, ' '))
  86. {
  87. $tagNameOpt = substr($tagName, $firstSpacePos + 1);
  88. $tagName = substr($tagName, 0, $firstSpacePos);
  89. // DMS STYLE - string opt in name
  90. dmString::retrieveOptFromString($tagNameOpt, $tagOpt);
  91. }
  92. // JQUERY STYLE - css expression
  93. dmString::retrieveCssFromString($tagName, $tagOpt);
  94. // ARRAY STYLE - array opt
  95. if (is_array($opt) && !empty($opt))
  96. {
  97. if (isset($opt['json']))
  98. {
  99. $tagOpt['class'][] = json_encode($opt['json']);
  100. unset($opt['json']);
  101. }
  102. if (isset($opt['class']))
  103. {
  104. $tagOpt['class'][] = is_array($opt['class']) ? implode(' ', $opt['class']) : $opt['class'];
  105. unset($opt['class']);
  106. }
  107. $tagOpt = array_merge($tagOpt, $opt);
  108. }
  109. // SYMFONY STYLE - string opt
  110. elseif (is_string($opt) && $content)
  111. {
  112. $opt = sfToolkit::stringToArray($opt);
  113. if (isset($opt['class']))
  114. {
  115. $tagOpt['class'][] = explode(' ', $opt['class']);
  116. unset($opt['class']);
  117. }
  118. $tagOpt = array_merge($tagOpt, $opt);
  119. }
  120. if (!$content)
  121. {
  122. if (!is_array($opt))
  123. {
  124. $content = $opt;
  125. }
  126. else // No opt
  127. {
  128. $content = null;
  129. }
  130. }
  131. $class = isset($tagOpt['class']) ? $tagOpt['class'] : array();
  132. if ($this->options['use_beaf'] && (in_array('beafh', $class) || in_array('beafv', $class)))
  133. {
  134. $isBeaf = true;
  135. $tagOpt['class'][] = 'clearfix';
  136. $beafTag = in_array($tagName, array('span', 'a', 'p')) ? 'span' : 'div';
  137. }
  138. else
  139. {
  140. $isBeaf = false;
  141. }
  142. if(isset($tagOpt['lang']))
  143. {
  144. if($tagOpt['lang'] === $this->context->getUser()->getCulture())
  145. {
  146. unset($tagOpt['lang']);
  147. }
  148. }
  149. if (isset($tagOpt['class']) && is_array($tagOpt['class']))
  150. {
  151. $tagOpt['class'] = implode(' ', array_unique($tagOpt['class']));
  152. }
  153. $optHtml = '';
  154. foreach ($tagOpt as $key => $val)
  155. {
  156. $optHtml .= ' '.$key.'="'.htmlentities($val, ENT_COMPAT, 'UTF-8').'"';
  157. }
  158. if(in_array($tagName, $this->options['empty_elements']))
  159. {
  160. $tag = '<'.$tagName.$optHtml.' />';
  161. }
  162. elseif ($openAndClose)
  163. {
  164. if ($isBeaf)
  165. {
  166. $tag = '<'.$tagName.$optHtml.'><'.$beafTag.' class="beafore"></'.$beafTag.'><'.$beafTag.' class="beafin">'.$content.'</'.$beafTag.'><'.$beafTag.' class="beafter"></'.$beafTag.'></'.$tagName.'>';
  167. }
  168. else
  169. {
  170. $tag = '<'.$tagName.$optHtml.'>'.$content.'</'.$tagName.'>';
  171. }
  172. }
  173. else
  174. {
  175. if ($isBeaf)
  176. {
  177. $tag = '<'.$tagName.$optHtml.'><'.$beafTag.' class="beafore"></'.$beafTag.'><'.$beafTag.' class="beafin">';
  178. }
  179. else
  180. {
  181. $tag = '<'.$tagName.$optHtml.'>';
  182. }
  183. }
  184. return $tag;
  185. }
  186. public function £($tagName, $opt = array(), $content = false, $openAndClose = true)
  187. {
  188. return $this->tag($tagName, $opt, $content, $openAndClose);
  189. }
  190. public function link($source = null)
  191. {
  192. return $this->serviceContainer->getService('link_tag_factory')->buildLink($source);
  193. }
  194. public function £link($source = null)
  195. {
  196. return $this->link($source);
  197. }
  198. public function media($source)
  199. {
  200. try
  201. {
  202. $this->serviceContainer->setParameter(
  203. 'media_tag.source',
  204. $resource = $this->serviceContainer->getService('media_resource')->initialize($source)
  205. );
  206. }
  207. catch(Exception $e)
  208. {
  209. $this->context->getLogger()->err($e->getMessage());
  210. if (sfConfig::get('dm_debug'))
  211. {
  212. throw $e;
  213. }
  214. return $this->media(null);
  215. }
  216. $serviceName = 'media_tag_'.$resource->getMime();
  217. if (!$this->serviceContainer->hasService($serviceName))
  218. {
  219. throw new dmException('helper->media can not display '.$source.': missing service '.$serviceName);
  220. }
  221. if (!class_exists($this->serviceContainer->getParameter($serviceName.'.class')))
  222. {
  223. throw new dmException('helper->media can not display '.$source.': missing service '.$serviceName);
  224. }
  225. $media = $this->serviceContainer->getService($serviceName);
  226. foreach($media->getStylesheets() as $stylesheet)
  227. {
  228. $this->context->getResponse()->addStylesheet($stylesheet);
  229. }
  230. foreach($media->getJavascripts() as $javascript)
  231. {
  232. $this->context->getResponse()->addJavascript($javascript);
  233. }
  234. return $media;
  235. }
  236. public function £media($source)
  237. {
  238. return $this->media($source);
  239. }
  240. public function table($opt = null)
  241. {
  242. return $this->serviceContainer->get('table_tag')->set($opt);
  243. }
  244. public function £table($opt = null)
  245. {
  246. return $this->table($opt);
  247. }
  248. public function getStylesheetWebPath($asset)
  249. {
  250. return $this->context->getRequest()->getRelativeUrlRoot().$this->context->getResponse()->calculateAssetPath('css', $asset);
  251. }
  252. public function getStylesheetFullPath($asset)
  253. {
  254. return dmOs::join(sfConfig::get('sf_web_dir'), $this->context->getResponse()->calculateAssetPath('css', $asset));
  255. }
  256. public function getJavascriptWebPath($asset)
  257. {
  258. return $this->context->getRequest()->getRelativeUrlRoot().$this->context->getResponse()->calculateAssetPath('js', $asset);
  259. }
  260. public function getJavascriptFullPath($asset)
  261. {
  262. return dmOs::join(sfConfig::get('sf_web_dir'), $this->context->getResponse()->calculateAssetPath('js', $asset));
  263. }
  264. public function getOtherAssetWebPath($asset)
  265. {
  266. return $this->context->getRequest()->getRelativeUrlRoot().$this->context->getResponse()->calculateAssetPath('other', $asset);
  267. }
  268. public function __call($method, $arguments)
  269. {
  270. $event = new sfEvent($this, 'dm.helper.method_not_found', array('method' => $method, 'arguments' => $arguments));
  271. // calls all listeners until one is able to implement the $method
  272. $this->context->getEventDispatcher()->notifyUntil($event);
  273. // no listener was able to proces the event? The method does not exist
  274. if (!$event->isProcessed())
  275. {
  276. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  277. }
  278. // return the listener returned value
  279. return $event->getReturnValue();
  280. }
  281. }