PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Header/AbstractHeader.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 230 lines | 150 code | 35 blank | 45 comment | 20 complexity | 4c8e600ad27af4b1cb912e8e73eef2c3 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. abstract class RokCommon_Header_AbstractHeader implements RokCommon_IHeader
  9. {
  10. /**
  11. * The Composite context name that holds the paths for script files
  12. */
  13. const SCRIPT_CONTEXT = 'rokcommon_scripts';
  14. /**
  15. * The Composite context name that holds the paths for style files
  16. */
  17. const STYLE_CONTEXT = 'rokcommon_styles';
  18. /**
  19. * @var RokCommon_Service_Container The main RokCommon DI Container
  20. */
  21. protected $container;
  22. protected $script_files = array();
  23. protected $style_files = array();
  24. protected $inline_scripts = array();
  25. protected $inline_styles = array();
  26. protected $domready_scripts = array();
  27. protected $loadevent_scripts = array();
  28. public function __construct()
  29. {
  30. $this->container = RokCommon_Service::getContainer();
  31. }
  32. public function addScriptPath($path, $priority = self::DEFAULT_PRIORITY)
  33. {
  34. /** @var $platforminfo RokCommon_IPlatformInfo */
  35. $platforminfo = $this->container->getService('platforminfo');
  36. foreach($platforminfo->getPathChecks() as $append)
  37. {
  38. RokCommon_Composite::addPackagePath(self::SCRIPT_CONTEXT, $path.$append, $priority);
  39. }
  40. }
  41. public function addStylePath($path, $priority = self::DEFAULT_PRIORITY)
  42. {
  43. /** @var $platforminfo RokCommon_IPlatformInfo */
  44. $platforminfo = $this->container->getService('platforminfo');
  45. foreach($platforminfo->getPathChecks() as $append)
  46. {
  47. RokCommon_Composite::addPackagePath(self::STYLE_CONTEXT, $path.$append, $priority);
  48. }
  49. }
  50. public function addScript($file, $order = self::DEFAULT_ORDER)
  51. {
  52. /** @var $platforminfo RokCommon_IPlatformInfo */
  53. $platforminfo = $this->container->getService('platforminfo');
  54. if (empty($file)) return;
  55. // If it is a full path or url file check if its external
  56. if ($platforminfo->isLinkExternal($file)) {
  57. // Its an external url just pass it through
  58. $this->registerScriptPath($file, $order);
  59. } else {
  60. // its a local url or path see if we can convert it to a local URL
  61. $path = $platforminfo->getPathForUrl($file);
  62. if ($path !== false) {
  63. // Local path and file exists add it as a local url
  64. $this->registerScriptPath($platforminfo->getUrlForPath($path), $order);
  65. } else {
  66. // cant find the file or not local really just pass it through
  67. $this->registerScriptPath($file, $order);
  68. }
  69. }
  70. }
  71. protected function registerScriptPath($path, $order)
  72. {
  73. $this->addIfHigherOrder($path, $path, $order, $this->script_files);
  74. ksort($this->script_files);
  75. }
  76. public function addInlineScript($text, $order = self::DEFAULT_ORDER)
  77. {
  78. if (!empty($text)) {
  79. $md5 = md5($text);
  80. $this->addIfHigherOrder($text, $md5, $order, $this->inline_scripts);
  81. ksort($this->inline_scripts);
  82. }
  83. }
  84. public function addStyle($file, $order = self::DEFAULT_ORDER)
  85. {
  86. /** @var $platforminfo RokCommon_IPlatformInfo */
  87. $platforminfo = $this->container->getService('platforminfo');
  88. if (empty($file)) return;
  89. // If it is a full path or url file check if its external
  90. if ($platforminfo->isLinkExternal($file)) {
  91. // Its an external url just pass it through
  92. $this->registerStylePath($file, $order);
  93. } else {
  94. // its a local url or path see if we can convert it to a local URL
  95. $path = $platforminfo->getPathForUrl($file);
  96. if ($path !== false) {
  97. // Local path and file exists add it as a local url
  98. $this->registerStylePath($platforminfo->getUrlForPath($path), $order);
  99. } else {
  100. // cant find the file or not local really just pass it through
  101. $this->registerStylePath($file, $order);
  102. }
  103. }
  104. }
  105. protected function registerStylePath($path, $order)
  106. {
  107. $this->addIfHigherOrder($path, $path, $order, $this->style_files);
  108. ksort($this->style_files);
  109. }
  110. public function addInlineStyle($text, $order = self::DEFAULT_ORDER)
  111. {
  112. if (!empty($text)) {
  113. $md5 = md5($text);
  114. $this->addIfHigherOrder($text, $md5, $order, $this->inline_styles);
  115. ksort($this->inline_styles);
  116. }
  117. }
  118. public function addDomReadyScript($js, $order = self::DEFAULT_ORDER)
  119. {
  120. if (!empty($js)) {
  121. $md5 = md5($js);
  122. $this->addIfHigherOrder($js, $md5, $order, $this->domready_scripts);
  123. ksort($this->domready_scripts);
  124. }
  125. }
  126. public function addLoadScript($js, $order = self::DEFAULT_ORDER)
  127. {
  128. if (!empty($js)) {
  129. $md5 = md5($js);
  130. $this->addIfHigherOrder($js, $md5, $order, $this->loadevent_scripts);
  131. ksort($this->loadevent_scripts);
  132. }
  133. }
  134. protected function addIfHigherOrder($data, $key, $order, &$array)
  135. {
  136. $found_priority = $this->checkForOrderedEntry($key, $array);
  137. if ($found_priority !== false) {
  138. if ($found_priority < $order) {
  139. // remove duplicate entry
  140. unset($array[$found_priority][$key]);
  141. if (count($array[$found_priority]) == 0) {
  142. // remove the priority lev le if its empty
  143. unset($array[$found_priority]);
  144. }
  145. $array[$order][$key] = $data;
  146. }
  147. } else {
  148. $array[$order][$key] = $data;
  149. }
  150. }
  151. protected function checkForOrderedEntry($key, &$array)
  152. {
  153. foreach ($array as $order => $order_array) {
  154. foreach ($order_array as $searching_key => $searching_entry) {
  155. if ($key === $searching_key) {
  156. return $order;
  157. }
  158. }
  159. }
  160. return false;
  161. }
  162. public function reset()
  163. {
  164. $this->script_files = array();
  165. $this->inline_scripts = array();
  166. $this->style_files = array();
  167. $this->inline_styles = array();
  168. $this->domready_scripts = array();
  169. $this->loadevent_scripts = array();
  170. }
  171. /**
  172. * @param $file
  173. * @param bool $keep_path
  174. *
  175. * @internal param $filename
  176. *
  177. * @return array
  178. */
  179. protected function getBrowserChecks($file, $keep_path = false)
  180. {
  181. $ext = substr($file, strrpos($file, '.'));
  182. $path = ($keep_path) ? dirname($file) . DS : '';
  183. $filename = basename($file, $ext);
  184. /** @var $browser RokCommon_Browser */
  185. $browser = $this->container->getService('browser');
  186. /** @var $platforminfo RokCommon_IPlatformInfo */
  187. $platforminfo = $this->container->getService('platforminfo');
  188. $checks = $browser->getChecks($file, $keep_path);
  189. //TODO turn on RTL when RokCommon RTL is enabled and the platform is currently RTL
  190. // if ($platforminfo->isRTL() && $this->get('rtl-enabled')) {
  191. // $checks[] = $path . $filename . '-rtl' . $ext;
  192. // }
  193. return $checks;
  194. }
  195. }