Source for file Controls.class.php

Documentation is available at Controls.class.php

  1. <?php
  2. /**
  3. * XOAD_Controls file.
  4. *
  5. * <p>This file defines the {@link XOAD_Controls} Class.</p>
  6. * <p>Example:</p>
  7. * <code>
  8. *
  9. * XOAD_Controls::register('xoad', array('Panel', 'PanelTitle', 'PanelContent'), 'xoad.controls.panel.js');
  10. *
  11. * </code>
  12. *
  13. * @author Stanimir Angeloff
  14. *
  15. * @package XOAD
  16. *
  17. * @subpackage XOAD_Controls
  18. *
  19. * @version 0.6.0.0
  20. *
  21. */
  22.  
  23. /**
  24. * Holds a list with the controls that are already registered.
  25. */
  26. $GLOBALS['_XOAD_CONTROLS_LIST'] = array();
  27.  
  28. /**
  29. * Holds a list with the scripts that are already included.
  30. */
  31. $GLOBALS['_XOAD_CONTROLS_SCRIPT'] = array();
  32.  
  33. /**
  34. * XOAD_Controls Class.
  35. *
  36. * <p>This class allows you to register custom client controls.</p>
  37. * <p>Each control includes a JS file that defines the class that will handle
  38. * the control. Additionally, you can attach a server class and a HTML code.</p>
  39. * <p>Example:</p>
  40. * <code>
  41. *
  42. * XOAD_Controls::register('xoad', array('Panel', 'PanelTitle', 'PanelContent'), 'xoad.controls.panel.js');
  43. *
  44. * </code>
  45. *
  46. * @author Stanimir Angeloff
  47. *
  48. * @package XOAD
  49. *
  50. * @subpackage XOAD_Controls
  51. *
  52. * @version 0.6.0.0
  53. *
  54. */
  55. class XOAD_Controls
  56. {
  57. /**
  58. * Gets the absolute path to the file.
  59. *
  60. * @access private
  61. *
  62. * @return string
  63. *
  64. */
  65. function getFileName($fileName)
  66. {
  67. if (strpos($fileName, DIRECTORY_SEPARATOR) === 0) {
  68.  
  69. return $fileName;
  70. }
  71.  
  72. if (strlen($fileName) >= 3) {
  73.  
  74. if (
  75. ($fileName{1} == ':') &&
  76. ($fileName{2} == DIRECTORY_SEPARATOR)) {
  77.  
  78. return $fileName;
  79. }
  80. }
  81.  
  82. return XOAD_CONTROLS_BASE . '/library/' . $fileName;
  83. }
  84.  
  85. /**
  86. * Registers a custom client control.
  87. *
  88. * <p>Each control includes a JS file that defines the class that
  89. * will handle the control. Additionally, you can attach a server class and
  90. * a HTML code.</p>
  91. *
  92. * @access public
  93. *
  94. * @param string $tagPrefix The tag prefix for the control, required.
  95. * @param mixed $tagName The tag name for the control, required.
  96. * @param string $jsFile The relative path to the JS file that
  97. * defines the class that will handle the
  98. * control, required.
  99. * @param string $phpFile The relative/absolute path to the PHP file
  100. * that defines the server class that is
  101. * associated with the control, optional.
  102. * @param string $url The callback URL for the server class,
  103. * optional.
  104. * @param string $htmlFile The relative/absolute path to the HTML file
  105. * that defines the code that is associated
  106. * with the control, optional.
  107. *
  108. * @return string HTML code to register the custom control.
  109. *
  110. * @static
  111. *
  112. */
  113. function register($tagPrefix = 'xoad', $tagName = null, $jsFile = null, $phpFile = null, $url = null, $htmlFile = null)
  114. {
  115. if (
  116. (empty($phpFile)) &&
  117. (empty($jsFile))) {
  118.  
  119. return null;
  120. }
  121.  
  122. if (XOAD_Utilities::getType($tagName) == 's_array') {
  123.  
  124. $returnValue = '';
  125.  
  126. foreach ($tagName as $name) {
  127.  
  128. $returnValue .= XOAD_Controls::register($tagPrefix, $name, $jsFile, $phpFile, $url, $htmlFile);
  129. }
  130.  
  131. return $returnValue;
  132. }
  133.  
  134. if (XOAD_Utilities::getType($tagName) == 'a_array') {
  135.  
  136. $returnValue = '';
  137.  
  138. foreach ($tagName as $prefix => $name) {
  139.  
  140. $returnValue .= XOAD_Controls::register($prefix, $name, $jsFile, $phpFile, $url, $htmlFile);
  141. }
  142.  
  143. return $returnValue;
  144. }
  145.  
  146. if (
  147. (empty($tagPrefix)) ||
  148. (empty($tagName))) {
  149.  
  150. return null;
  151. }
  152.  
  153. $registerAttribute = ($tagName == '@');
  154.  
  155. $controlName = strtolower($tagPrefix) . ':' . strtolower($tagName);
  156.  
  157. if (
  158. ( ! $registerAttribute) &&
  159. (in_array($controlName, $GLOBALS['_XOAD_CONTROLS_LIST']))) {
  160.  
  161. return null;
  162. }
  163.  
  164. $phpControlName = $tagPrefix . '_Controls_' . $tagName;
  165. $jsControlName = $tagPrefix . '.controls.' . $tagName;
  166.  
  167. $includeScript = '';
  168. $returnValue = '';
  169.  
  170. if ( ! empty($phpFile)) {
  171.  
  172. require_once(XOAD_Controls::getFileName($phpFile));
  173.  
  174. if (empty($url)) {
  175.  
  176. $url = XOAD_Utilities::getRequestUrl();
  177. }
  178.  
  179. $phpObject = new $phpControlName();
  180.  
  181. XOAD_Client::privateMethods($phpObject, array('getJSCode', 'getHtmlCode'));
  182.  
  183. $returnValue .= XOAD_Client::register($phpControlName, $url) . ';';
  184. }
  185.  
  186. if ( ! empty($jsFile)) {
  187.  
  188. if ( ! in_array($jsFile, $GLOBALS['_XOAD_CONTROLS_SCRIPT'])) {
  189.  
  190. $includeScript .= '<script type="text/javascript" src="' . htmlspecialchars($jsFile) . '"></script>';
  191.  
  192. $GLOBALS['_XOAD_CONTROLS_SCRIPT'][] = $jsFile;
  193. }
  194.  
  195. } else {
  196.  
  197. if (
  198. (isset($phpObject)) &&
  199. (method_exists($phpObject, 'getJSCode'))) {
  200.  
  201. $returnValue .= $phpObject->getJSCode($jsControlName, $phpControlName);
  202. }
  203. }
  204.  
  205. $controlHtml = null;
  206.  
  207. if ( ! empty($htmlFile)) {
  208.  
  209. $controlHtml = @join(null, @file(XOAD_Controls::getFileName($htmlFile)));
  210.  
  211. } else {
  212.  
  213. if (
  214. (isset($phpObject)) &&
  215. (method_exists($phpObject, 'getHtmlCode'))) {
  216.  
  217. $controlHtml = $phpObject->getHtmlCode($jsControlName, $phpControlName);
  218. }
  219. }
  220.  
  221. if ( ! $registerAttribute) {
  222.  
  223. $returnValue .= 'xoad.controls.list[' . sizeof($GLOBALS['_XOAD_CONTROLS_LIST']) . '] = {';
  224. $returnValue .= 'tagName:' . XOAD_Client::register($controlName);
  225.  
  226. if ( ! empty($jsFile)) {
  227.  
  228. $returnValue .= ',clientClass:' . XOAD_Client::register($jsControlName);
  229. }
  230.  
  231. if ( ! empty($phpFile)) {
  232.  
  233. $returnValue .= ',serverClass:' . XOAD_Client::register($phpControlName);
  234. }
  235.  
  236. if ( ! empty($controlHtml)) {
  237.  
  238. $returnValue .= ',html:' . XOAD_Client::register($controlHtml);
  239. }
  240.  
  241. $returnValue .= '};';
  242. }
  243.  
  244. if ( ! empty($returnValue)) {
  245.  
  246. $returnValue = '<script type="text/javascript">' . $returnValue . '</script>';
  247. }
  248.  
  249. $returnValue = $includeScript . $returnValue;
  250.  
  251. $GLOBALS['_XOAD_CONTROLS_LIST'][] = $controlName;
  252.  
  253. return $returnValue;
  254. }
  255. }
  256.  
  257. ?>

Documentation generated on Sat, 12 Nov 2005 20:23:59 +0200 by phpDocumentor 1.3.0RC3