/cms/includes/hooks.php

https://github.com/iconifyit/SkyBlue-1.1 · PHP · 215 lines · 96 code · 20 blank · 99 comment · 11 complexity · 3f74bebcd02d097de5f72cc084ad824c MD5 · raw file

  1. <?php defined('SKYBLUE') or die('Bad file request');
  2. /**
  3. * @version 1.1 r247 2010-02-23 20:10:00 $
  4. * @package SkyBlueCanvas
  5. * @copyright Copyright (C) 2005 - 2008 Scott Edwin Lewis. All rights reserved.
  6. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  7. * SkyBlueCanvas is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYING.txt for copyright notices and details.
  12. */
  13. /**
  14. * Creates a JavaScript link tag
  15. * @param String $source The path to the script file
  16. * @return String
  17. */
  18. function make_script_link($source) {
  19. return "<script type=\"text/javascript\" src=\"{$source}\"></script>\n";
  20. }
  21. /**
  22. * Creates a JavaScript element with embedded code
  23. * @param String $code The JavaScript code
  24. * @return String
  25. */
  26. function make_script_element($code) {
  27. return "<script type=\"text/javascript\">{$code}</script>\n";
  28. }
  29. /**
  30. * Creates a stylesheet link tag
  31. * @param String $source The path to the stylesheet file
  32. * @return String
  33. */
  34. function make_style_link($href) {
  35. return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$href}\" />\n";
  36. }
  37. /**
  38. * Creates a style element with embedded code
  39. * @param String $code The CSS code
  40. * @return String
  41. */
  42. function make_style_element($code) {
  43. return "<style type=\"text/css\">{$code}</style>\n";
  44. }
  45. /**
  46. * Uses CSS to hide an element
  47. * @param String $selector The CSS selector for the element(s)
  48. * @return String
  49. */
  50. function hide_element($selector) {
  51. return make_style_element($selector . " { display: none; }");
  52. }
  53. /**
  54. * Uses CSS to show an element
  55. * @param String $selector The CSS selector for the element(s)
  56. * @return String
  57. */
  58. function show_element($selector) {
  59. return make_style_element($selector . " { display: block; }");
  60. }
  61. /**
  62. * Determines if the current page is the home page.
  63. * @param int A page ID to test (optional)
  64. * @return bool
  65. */
  66. function is_home($pageId=null) {
  67. global $Core;
  68. if (is_null($pageId)) {
  69. $pageId = $Core->GetVar($_GET, 'pid', DEFAULT_PAGE);
  70. }
  71. return $pageId === DEFAULT_PAGE;
  72. }
  73. /**
  74. * Determines if the current page is in a list of supplied page ids
  75. * @param Array $pids The array of page ids to search
  76. * @return boolean
  77. */
  78. function in_pagelist($pids=array()) {
  79. global $Core;
  80. return in_array($Core->GetVar($_GET, 'pid', ''), $pids);
  81. }
  82. /**
  83. * Gets the data object for the current page
  84. * @param boolean $refresh Whether or not to refresh the staticallay stored Page object
  85. * @return object A reference to the current Page
  86. */
  87. function current_page($refresh=false) {
  88. global $Core;
  89. static $Page;
  90. if (!is_object($Page) || $refresh) {
  91. $pages = get_pages(true);
  92. $Page = $Core->SelectObj($pages, $Core->GetVar($_GET, 'pid', ''));
  93. }
  94. return $Page;
  95. }
  96. /**
  97. * Gets all the page objects
  98. * @param boolean $refresh Whether or not to refresh the statically stored data
  99. * @return array An array of Page objects
  100. */
  101. function get_pages($refresh=false) {
  102. global $Core;
  103. static $pages;
  104. if (!is_array($pages) || $refresh) {
  105. $pages = $Core->xmlHandler->ParserMain(SB_XML_DIR . "page.xml");
  106. }
  107. return $pages;
  108. }
  109. /**
  110. * Gets a property of the Page object
  111. * @param string The name of the property to get
  112. * @return mixed The value of the Page property
  113. */
  114. function page_info($prop) {
  115. global $Core;
  116. return $Core->GetVar(current_page(), $prop, '');
  117. }
  118. /**
  119. * Reads in and parses an XML file
  120. * @param String $file The file path to the XML file
  121. * @return Array An array of Objects
  122. */
  123. function parse_xml($file) {
  124. global $Core;
  125. if (!file_exists($file)) {
  126. trigger_error(
  127. "{$file} does not exist",
  128. E_USER_ERROR
  129. );
  130. }
  131. else {
  132. return $Core->xmlHandler->ParserMain($file);
  133. }
  134. }
  135. /**
  136. * Converts an array of Objects to an XML document.
  137. * @param Array $objects The Array of objects to convert
  138. * @return String The XML document.
  139. */
  140. function objects_to_xml($objects, $type='') {
  141. global $Core;
  142. return $Core->xmlHandler->ObjsToXML($objects, $type);
  143. }
  144. /**
  145. * Gets the current context (admin, front, etc.)
  146. */
  147. function get_context() {
  148. $context = "unknown";
  149. if (constant('_ADMIN_') == 1) {
  150. $context = "admin";
  151. }
  152. else {
  153. $context = "front";
  154. }
  155. return $context;
  156. }
  157. /**
  158. * Creates a new Service_JSON object but only once.
  159. * @return Object
  160. */
  161. function new_json() {
  162. static $json;
  163. if (!is_object($json)) {
  164. $json = new Services_JSON();
  165. }
  166. return $json;
  167. }
  168. /**
  169. * Encodes an Array or Object as JSON
  170. * @param Mixed $data The PHP data structure
  171. * @return String
  172. */
  173. function encode_json($data) {
  174. $json = new_json();
  175. return $json->encode($data);
  176. }
  177. /**
  178. * Decodes a JSON string to a PHP data structure
  179. * @param Mixed $json The JSON string
  180. * @return Mixed
  181. */
  182. function decode_json($data) {
  183. $json = new_json();
  184. return $json->encode($data);
  185. }
  186. /**
  187. * Sorts an array of objects by comparing a member property.
  188. * @param array The array of objects to sort
  189. * @param string The name of the property to sort on
  190. * @return void
  191. */
  192. function sort_objects(&$objects, $sort_field) {
  193. $sort = Core::LoadPlugin('quicksort');
  194. $sort->_sort($objects, $sort_field);
  195. }