Script.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ Bxwv0ljVRyBdepEM6JRZr9lfWQ1XGNA+QoZzlAJc6A0=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * javascript, css etc view helper
  14. */
  15. namespace Cube\View\Helper;
  16. class Script extends AbstractHelper
  17. {
  18. /**
  19. *
  20. * code to be added between the <head> tags of the html code
  21. *
  22. * @var array
  23. */
  24. protected $_headerCode = array();
  25. /**
  26. *
  27. * code to be added between the <body> tags, preferably towards the
  28. * bottom, usable for javascript code etc
  29. *
  30. * @var array
  31. */
  32. protected $_bodyCode = array();
  33. /**
  34. *
  35. * add code to the page header, duplicates will be skipped
  36. *
  37. * @param string $code
  38. *
  39. * @return \Cube\View\Helper\Script
  40. */
  41. public function addHeaderCode($code)
  42. {
  43. $code = (string)$code;
  44. if (!in_array($code, $this->_headerCode)) {
  45. $this->_headerCode[] = $code;
  46. }
  47. return $this;
  48. }
  49. /**
  50. *
  51. * remove header code
  52. *
  53. * @param string $code
  54. *
  55. * @return $this
  56. */
  57. public function removeHeaderCode($code)
  58. {
  59. $code = $this->_addSpecialChars($code);
  60. foreach ($this->_headerCode as $key => $value) {
  61. $value = $this->_addSpecialChars($value);
  62. if ($value == $code) {
  63. unset($this->_headerCode[$key]);
  64. }
  65. }
  66. return $this;
  67. }
  68. /**
  69. *
  70. * clear header code variable
  71. *
  72. * @return $this
  73. */
  74. public function clearHeaderCode()
  75. {
  76. $this->_headerCode = array();
  77. return $this;
  78. }
  79. /**
  80. *
  81. * add code to the page body, duplicates will be skipped
  82. *
  83. * @param string $code
  84. *
  85. * @return $this
  86. */
  87. public function addBodyCode($code)
  88. {
  89. if (is_array($code)) {
  90. foreach ($code as $c) {
  91. $this->addBodyCode($c);
  92. }
  93. }
  94. else {
  95. $code = (string)$code;
  96. if (!in_array($code, $this->_bodyCode)) {
  97. $this->_bodyCode[] = $code;
  98. }
  99. }
  100. return $this;
  101. }
  102. /**
  103. *
  104. * remove body code
  105. *
  106. * @param string $code
  107. *
  108. * @return $this
  109. */
  110. public function removeBodyCode($code)
  111. {
  112. $code = $this->_addSpecialChars($code);
  113. foreach ($this->_bodyCode as $key => $value) {
  114. $value = $this->_addSpecialChars($value);
  115. if ($value == $code) {
  116. unset($this->_bodyCode[$key]);
  117. }
  118. }
  119. return $this;
  120. }
  121. /**
  122. *
  123. * clear body code variable
  124. *
  125. * @return $this
  126. */
  127. public function clearBodyCode()
  128. {
  129. $this->_bodyCode = array();
  130. return $this;
  131. }
  132. /**
  133. *
  134. * method that is called by the reflection class, returns an instance of the object
  135. *
  136. * @return \Cube\View\Helper\Script
  137. */
  138. public function script()
  139. {
  140. return $this;
  141. }
  142. /**
  143. *
  144. * display the header code
  145. *
  146. * @return string
  147. */
  148. public function displayHeaderCode()
  149. {
  150. return implode("\n", $this->_headerCode);
  151. }
  152. /**
  153. *
  154. * display the footer code
  155. *
  156. * @return string
  157. */
  158. public function displayBodyCode()
  159. {
  160. return implode("\n", $this->_bodyCode);
  161. }
  162. /**
  163. *
  164. * add special chars
  165. *
  166. * @param string $input
  167. *
  168. * @return mixed
  169. */
  170. private function _addSpecialChars($input)
  171. {
  172. return str_ireplace(
  173. array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&nbsp;'), array('&', "'", '"', '<', '>', ' '), $input);
  174. }
  175. }