PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/core/helpers/html.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 265 lines | 163 code | 38 blank | 64 comment | 62 complexity | fdabfa4ecb984e3208b16405ad8f72b0 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Functions to help with using HTML. Does not include form elements - those have their own helper.
  11. * @package Helpers
  12. * @category Concrete
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class Concrete5_Helper_Html {
  19. protected $legacyJavascript = array(
  20. 'ccm.dialog.js' => 'ccm.app.js',
  21. 'jquery.metadata.js' => 'ccm.app.js',
  22. 'ccm.themes.js' => 'ccm.app.js',
  23. 'ccm.filemanager.js' => 'ccm.app.js',
  24. /*'jquery.rating.js' => 'ccm.app.js',*/
  25. 'jquery.colorpicker.js' => 'ccm.app.js',
  26. 'jquery.liveupdate.js' => 'ccm.app.js',
  27. 'ccm.ui.js' => 'ccm.app.js',
  28. 'ccm.search.js' => 'ccm.app.js'
  29. );
  30. protected $legacyCSS = array(
  31. 'ccm.dialog.css' => 'ccm.app.css',
  32. 'ccm.ui.css' => 'ccm.app.css',
  33. 'ccm.forms.css' => 'ccm.app.css',
  34. 'ccm.menus.css' => 'ccm.app.css',
  35. 'ccm.search.css' => 'ccm.app.css',
  36. 'ccm.filemanager.css' => 'ccm.app.css',
  37. 'ccm.calendar.css' => 'ccm.app.css'
  38. );
  39. /**
  40. * Includes a CSS file. This function looks in several places.
  41. * First, if the item is either a path or a URL it just returns the link to that item (as XHTML-formatted style tag.)
  42. * Then it checks the currently active theme, then if a package is specified it checks there. Otherwise if nothing is found it
  43. * fires off a request to the relative directory CSS directory. If nothing is there, then it checks to the assets directories
  44. *
  45. * @param string $file name of css file
  46. * @param string $pkgHandle handle of the package that the css file is located in (if applicable)
  47. * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same css file (in View::addHeaderItem() and View::addFooterItem()).
  48. * @return CSSOutputObject
  49. */
  50. public function css($file, $pkgHandle = null, $uniqueItemHandle = array()) {
  51. $css = new CSSOutputObject($uniqueItemHandle);
  52. // if the first character is a / then that means we just go right through, it's a direct path
  53. if (substr($file, 0, 1) == '/' || substr($file, 0, 4) == 'http' || strpos($file, DISPATCHER_FILENAME) > -1) {
  54. $css->compress = false;
  55. $css->file = $file;
  56. }
  57. $v = View::getInstance();
  58. // checking the theme directory for it. It's just in the root.
  59. if ($v->getThemeDirectory() != '' && file_exists($v->getThemeDirectory() . '/' . $file)) {
  60. $css->file = $v->getThemePath() . '/' . $file;
  61. } else if (file_exists(DIR_BASE . '/' . DIRNAME_CSS . '/' . $file)) {
  62. $css->file = DIR_REL . '/' . DIRNAME_CSS . '/' . $file;
  63. } else if ($pkgHandle != null) {
  64. if (file_exists(DIR_BASE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file)) {
  65. $css->file = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file;
  66. } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file)) {
  67. $css->file = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_CSS . '/' . $file;
  68. }
  69. }
  70. if ($css->file == '') {
  71. if (isset($this->legacyCSS[$file])) {
  72. $file = $this->legacyCSS[$file];
  73. }
  74. $css->file = ASSETS_URL_CSS . '/' . $file;
  75. }
  76. $css->file .= (strpos($css->file, '?') > -1) ? '&amp;' : '?';
  77. $css->file .= 'v=' . md5(APP_VERSION . PASSWORD_SALT);
  78. // for the javascript addHeaderItem we need to have a full href available
  79. $css->href = $css->file;
  80. if (substr($css->file, 0, 4) != 'http') {
  81. $css->href = BASE_URL . $css->file;
  82. }
  83. return $css;
  84. }
  85. /**
  86. * Includes a JavaScript file. This function looks in several places.
  87. * First, if the item is either a path or a URL it just returns the link to that item (as XHTML-formatted script tag.)
  88. * If a package is specified it checks there. Otherwise if nothing is found it
  89. * fires off a request to the relative directory JavaScript directory.
  90. *
  91. * @param string $file name of javascript file
  92. * @param string $pkgHandle handle of the package that the javascript file is located in (if applicable)
  93. * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same javascript file (in View::addHeaderItem() and View::addFooterItem()).
  94. * @return JavaScriptOutputObject
  95. */
  96. public function javascript($file, $pkgHandle = null, $uniqueItemHandle = array()) {
  97. $js = new JavaScriptOutputObject($uniqueItemHandle);
  98. if (substr($file, 0, 1) == '/' || substr($file, 0, 4) == 'http' || strpos($file, DISPATCHER_FILENAME) > -1) {
  99. $js->compress = false;
  100. $js->file = $file;
  101. }
  102. if (file_exists(DIR_BASE . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
  103. $js->file = DIR_REL . '/' . DIRNAME_JAVASCRIPT . '/' . $file;
  104. } else if ($pkgHandle != null) {
  105. if (file_exists(DIR_BASE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
  106. $js->file = DIR_REL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file;
  107. } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/' . $file)) {
  108. $js->file = ASSETS_URL . '/' . DIRNAME_PACKAGES . '/' . $pkgHandle . '/' . DIRNAME_JAVASCRIPT . '/'. $file;
  109. }
  110. }
  111. if ($js->file == '') {
  112. if (isset($this->legacyJavascript[$file])) {
  113. $file = $this->legacyJavascript[$file];
  114. }
  115. $js->file = ASSETS_URL_JAVASCRIPT . '/' . $file;
  116. }
  117. $js->file .= (strpos($js->file, '?') > -1) ? '&amp;' : '?';
  118. $js->file .= 'v=' . md5(APP_VERSION . PASSWORD_SALT);
  119. // for the javascript addHeaderItem we need to have a full href available
  120. $js->href = $js->file;
  121. return $js;
  122. }
  123. /**
  124. * Includes a JavaScript inline script.
  125. *
  126. * @param string $script javascript code (not including the surrounding <script> tags)
  127. * @param array $uniqueItemHandle contains two elements: 'handle' and 'version' (both strings) -- helps prevent duplicate output of the same script (in View::addHeaderItem() and View::addFooterItem()).
  128. * @return InlineScriptOutputObject
  129. */
  130. public function script($script, $uniqueItemHandle = array()) {
  131. $js = new InlineScriptOutputObject($uniqueItemHandle);
  132. $js->script = $script;
  133. return $js;
  134. }
  135. /**
  136. * Includes an image file when given a src, width and height. Optional attribs array specifies style, other properties.
  137. * First checks the PATH off the root of the site
  138. * Then checks the PATH off the images directory at the root of the site.
  139. * @param string $src
  140. * @param int $width
  141. * @param int $height
  142. * @param array $attribs
  143. * @return string $html
  144. */
  145. public function image($src, $width = false, $height = false, $attribs = null) {
  146. $image = parse_url($src);
  147. $attribsStr = '';
  148. if (is_array($width) && $height == false) {
  149. $attribs = $width;
  150. $width = false;
  151. }
  152. if (is_array($attribs)) {
  153. foreach($attribs as $key => $at) {
  154. $attribsStr .= " {$key}=\"{$at}\" ";
  155. }
  156. }
  157. if ($width == false && $height == false && (!isset($image['scheme']))) {
  158. // if our file is not local we DON'T do getimagesize() on it. too slow
  159. $v = View::getInstance();
  160. if ($v->getThemeDirectory() != '' && file_exists($v->getThemeDirectory() . '/' . DIRNAME_IMAGES . '/' . $src)) {
  161. $s = getimagesize($v->getThemeDirectory() . '/' . DIRNAME_IMAGES . '/' . $src);
  162. $width = $s[0];
  163. $height = $s[1];
  164. $src = $v->getThemePath() . '/' . DIRNAME_IMAGES . '/' . $src;
  165. } else if (file_exists(DIR_BASE . '/' . $src)) {
  166. $s = getimagesize(DIR_BASE . '/' . $src);
  167. $width = $s[0];
  168. $height = $s[1];
  169. } else if (file_exists(DIR_BASE . '/' . DIRNAME_IMAGES . '/' . $src)) {
  170. $s = getimagesize(DIR_BASE . '/' . DIRNAME_IMAGES . '/' . $src);
  171. $width = $s[0];
  172. $height = $s[1];
  173. $src = DIR_REL . '/' . DIRNAME_IMAGES . '/' . $src;
  174. } else if (file_exists(DIR_BASE_CORE . '/' . DIRNAME_IMAGES . '/' . $src)) {
  175. $s = getimagesize(DIR_BASE_CORE . '/' . DIRNAME_IMAGES . '/' . $src);
  176. $width = $s[0];
  177. $height = $s[1];
  178. $src = ASSETS_URL_IMAGES . '/' . $src;
  179. }
  180. }
  181. if ($width > 0) {
  182. $str = '<img src="' . $src . '" width="' . $width . '" border="0" height="' . $height . '" ' . $attribsStr . ' />';
  183. } else {
  184. $str = '<img src="' . $src . '" border="0" ' . $attribsStr . ' />';
  185. }
  186. return $str;
  187. }
  188. }
  189. class Concrete5_Helper_Html_HeaderOutputObject {
  190. public $file = '';
  191. public $href = '';
  192. public $script = '';
  193. public $compress = true;
  194. public $handle = array(); //optional 'handle' and 'version' that the View class can use to avoid duplicate output of conflicting js/css files
  195. /**
  196. * @param optional: pass in handle and version (as array) or just handle (as string; version will be '0')
  197. * to avoid duplicate output of the same js/css items from other blocks/theme code.
  198. */
  199. public function __construct($uniqueItemHandle = array()) {
  200. if (is_array($uniqueItemHandle) && array_key_exists('handle', $uniqueItemHandle) && !empty($uniqueItemHandle['handle'])) {
  201. $this->handle = array(
  202. 'handle' => $uniqueItemHandle['handle'],
  203. 'version' => array_key_exists('version', $uniqueItemHandle) ? $uniqueItemHandle['version'] : '0',
  204. );
  205. } else if (is_string($uniqueItemHandle) && !empty($uniqueItemHandle)) {
  206. $this->handle = array(
  207. 'handle' => $uniqueItemHandle,
  208. 'version' => '0',
  209. );
  210. }
  211. }
  212. }
  213. class Concrete5_Helper_Html_JavascriptOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {
  214. public function __toString() {
  215. return '<script type="text/javascript" src="' . $this->file . '"></script>';
  216. }
  217. }
  218. class Concrete5_Helper_Html_InlinescriptOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {
  219. public function __toString() {
  220. return '<script type="text/javascript">/*<![CDATA[*/'. $this->script .'/*]]>*/</script>';
  221. }
  222. }
  223. class Concrete5_Helper_Html_CSSOutputObject extends Concrete5_Helper_Html_HeaderOutputObject {
  224. public function __toString() {
  225. return '<link rel="stylesheet" type="text/css" href="' . $this->file . '" />';
  226. }
  227. }