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

/dmCorePlugin/lib/view/html/link/dmBaseLinkTag.php

https://github.com/leszek/diem
PHP | 245 lines | 154 code | 43 blank | 48 comment | 9 complexity | 7e251f657bcf06a07ce73857dc1e63c8 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. abstract class dmBaseLinkTag extends dmHtmlTag
  3. {
  4. protected
  5. $resource,
  6. $requestContext;
  7. protected function initialize(array $options = array())
  8. {
  9. parent::initialize($options);
  10. $this->addAttributeToRemove(array('text', 'anchor', 'current_class', 'parent_class', 'current_span', 'use_beaf'));
  11. $this->addEmptyAttributeToRemove(array('target', 'title'));
  12. $this->addClass('link');
  13. }
  14. public function getDefaultOptions()
  15. {
  16. return array_merge(parent::getDefaultOptions(), array(
  17. 'current_class' => 'dm_current',
  18. 'parent_class' => 'dm_parent',
  19. 'current_span' => false,
  20. 'use_beaf' => false
  21. ));
  22. }
  23. public function isCurrent()
  24. {
  25. return false;
  26. }
  27. public function isParent()
  28. {
  29. return false;
  30. }
  31. /**
  32. * @return string baseHref the href without query string
  33. */
  34. abstract protected function getBaseHref();
  35. public function getHrefPrefix()
  36. {
  37. return sfConfig::get('sf_no_script_name')
  38. ? $this->requestContext['prefix']
  39. : $this->requestContext['script_name'];
  40. }
  41. /**
  42. * Set text
  43. * @return dmLinkTag $this
  44. */
  45. public function text($v)
  46. {
  47. return $this->setOption('text', (string) $v);
  48. }
  49. /**
  50. * Set title
  51. * @return dmLinkTag $this
  52. */
  53. public function title($v)
  54. {
  55. return $this->setOption('title', (string) $v);
  56. }
  57. /**
  58. * Set text and title
  59. * @return dmLinkTag $this
  60. */
  61. public function textTitle($v)
  62. {
  63. return $this->text($v)->title($v);
  64. }
  65. /**
  66. * Set link target
  67. * @return dmLinkTag $this
  68. */
  69. public function target($v)
  70. {
  71. if (in_array($v, array('blank', 'parent', 'self', 'top')))
  72. {
  73. $v = '_'.$v;
  74. }
  75. return $this->setOption('target', strtolower($v));
  76. }
  77. /**
  78. * Add an anchor
  79. * @return dmLinkTag $this
  80. */
  81. public function anchor($v)
  82. {
  83. return $this->setOption('anchor', trim((string) $v, '#'));
  84. }
  85. /**
  86. * Add a request parameter
  87. * @return dmLinkTag $this
  88. */
  89. public function param($key, $value)
  90. {
  91. return $this->params(array($key => $value));
  92. }
  93. /**
  94. * Add request parameters
  95. * @return dmLinkTag $this
  96. */
  97. public function params(array $params)
  98. {
  99. return $this->setOption('params', array_merge($this->get('params', array()), $params));
  100. }
  101. /**
  102. * Whether to display current links with span tag
  103. * @return dmLinkTag $this
  104. */
  105. public function currentSpan($bool)
  106. {
  107. return $this->setOption('current_span', (bool) $bool);
  108. }
  109. /**
  110. * Sets the current css class
  111. * @return dmLinkTag $this
  112. */
  113. public function currentClass($class)
  114. {
  115. return $this->setOption('current_class', (string) $class);
  116. }
  117. /**
  118. * Sets the parent css class
  119. * @return dmLinkTag $this
  120. */
  121. public function parentClass($class)
  122. {
  123. return $this->setOption('parent_class', (string) $class);
  124. }
  125. public function render()
  126. {
  127. return $this->doRender('a', $this->getHtmlAttributes(), $this->renderText());
  128. }
  129. protected function doRender($tag, $htmlAttributes, $text)
  130. {
  131. if($this->options['use_beaf'])
  132. {
  133. return $this->doRenderBeaf($tag, $htmlAttributes, $text);
  134. }
  135. return '<'.$tag.$htmlAttributes.'>'.$text.'</'.$tag.'>';
  136. }
  137. protected function doRenderBeaf($tag, $htmlAttributes, $text)
  138. {
  139. if(in_array('beafh', $this->options['class']) || in_array('beafv', $this->options['class']))
  140. {
  141. return '<'.$tag.$htmlAttributes.'><span class="beafore"></span><span class="beafin">'.$text.'</span><span class="beafter"></span></span></a>';
  142. }
  143. return '<'.$tag.$htmlAttributes.'>'.$text.'</'.$tag.'>';
  144. }
  145. protected function prepareAttributesForHtml(array $attributes)
  146. {
  147. $attributes = parent::prepareAttributesForHtml($attributes);
  148. $attributes['href'] = $this->getBaseHref();
  149. if (array_key_exists('params', $attributes))
  150. {
  151. if (!empty($attributes['params']))
  152. {
  153. $attributes['href'] = $this->buildUrl(
  154. dmString::getBaseFromUrl($attributes['href']),
  155. array_merge(dmString::getDataFromUrl($attributes['href']), $attributes['params'])
  156. );
  157. /*
  158. * if last href char is a =, remove it
  159. * fixes http://github.com/diem-project/diem/issues/#issue/6
  160. */
  161. if('=' === substr($attributes['href'], -1))
  162. {
  163. $attributes['href'] = substr($attributes['href'], 0, strlen($attributes['href']) - 1);
  164. }
  165. }
  166. unset($attributes['params']);
  167. }
  168. if (isset($attributes['anchor']))
  169. {
  170. $attributes['href'] .= '#'.$attributes['anchor'];
  171. }
  172. // makes unit testing easier
  173. ksort($attributes);
  174. return $attributes;
  175. }
  176. public function getHref()
  177. {
  178. return dmArray::get($this->prepareAttributesForHtml($this->options), 'href');
  179. }
  180. public function getText()
  181. {
  182. return $this->renderText();
  183. }
  184. public function getAbsoluteHref()
  185. {
  186. $href = $this->getHref();
  187. $uriPrefix = dm::getRequest()->getUriPrefix();
  188. if (strpos($href, $uriPrefix) !== 0)
  189. {
  190. $href = $uriPrefix.$href;
  191. }
  192. return $href;
  193. }
  194. protected function renderText()
  195. {
  196. return $this->options['text'];
  197. }
  198. protected function buildUrl($base, array $data = array())
  199. {
  200. return $base.'?'.http_build_query($data, null, '&');
  201. }
  202. }