/framework/app_logic/logic2/page.class.php

https://github.com/cclark61/phpOpenFW · PHP · 334 lines · 202 code · 30 blank · 102 comment · 47 complexity · 41486e8a88e06b1a0a862589d8730c53 MD5 · raw file

  1. <?php
  2. //*************************************************************************
  3. /**
  4. * A simple core class to construct the basic page framework
  5. *
  6. * @package phpOpenFW
  7. * @subpackage Application-Logic-2-Structure
  8. * @author Christian J. Clark
  9. * @copyright Copyright (c) Christian J. Clark
  10. * @license http://www.gnu.org/licenses/gpl-2.0.txt
  11. * @version Started: 2/18/2013, Last updated: 1/13/2016
  12. **/
  13. //*************************************************************************
  14. //*************************************************************************
  15. /**
  16. * Page Class
  17. * @package phpOpenFW
  18. * @subpackage Application-Logic-2-Structure
  19. */
  20. //*************************************************************************
  21. class page
  22. {
  23. //*************************************************************************
  24. // Class Variables
  25. //*************************************************************************
  26. protected $root_node;
  27. protected $data_format;
  28. protected $data_formats;
  29. protected $template;
  30. protected $data;
  31. protected $show_data_only;
  32. protected $no_escape_elements;
  33. protected $js_files;
  34. protected $css_files;
  35. //*************************************************************************
  36. // Constructor Function
  37. //*************************************************************************
  38. public function __construct($root_node='page')
  39. {
  40. $this->set_root_node($root_node);
  41. $this->data_formats = array('xml', 'json', 'array');
  42. $this->data_format = 'xml';
  43. $this->template = false;
  44. $this->data = array();
  45. $this->show_data_only = false;
  46. $this->no_escape_elements = array(
  47. 'render-function' => 'render-function',
  48. 'skip-controller' => 'skip-controller',
  49. 'no-render' => 'no-render'
  50. );
  51. }
  52. //*************************************************************************
  53. // Destructor Function
  54. //*************************************************************************
  55. public function __destruct() {}
  56. //*************************************************************************
  57. // Object Conversion to String Function
  58. //*************************************************************************
  59. public function __toString()
  60. {
  61. ob_start();
  62. $this->render();
  63. return ob_get_clean();
  64. }
  65. //*************************************************************************
  66. // Display Error Function
  67. //*************************************************************************
  68. protected function display_error($function, $error_msg)
  69. {
  70. $tmp_msg = 'Class [' . __CLASS__ . "]::{$function}() - ";
  71. $tmp_msg .= "Error: {$error_msg}";
  72. trigger_error($tmp_msg);
  73. }
  74. //*************************************************************************
  75. // Dump Data Function
  76. //*************************************************************************
  77. public function dump_data()
  78. {
  79. if (function_exists('print_array')) {
  80. print_array($this->data);
  81. }
  82. else {
  83. var_dump($this->data);
  84. }
  85. }
  86. //*************************************************************************
  87. // Set Root Node Function
  88. //*************************************************************************
  89. public function set_root_node($root_node)
  90. {
  91. $new_rn = (string)$root_node;
  92. if ($new_rn == '' || is_numeric($new_rn)) {
  93. $msg = "Invalid root node name '{$new_rn}'. Root node name must be a string and not entirely numeric.";
  94. $this->display_error(__FUNCTION__, $msg);
  95. return false;
  96. }
  97. $this->root_node = $new_rn;
  98. return true;
  99. }
  100. //*************************************************************************
  101. // Set Data Format Function
  102. //*************************************************************************
  103. public function set_data_format($format)
  104. {
  105. if (!$format) { return false; }
  106. $format = strtolower($format);
  107. if (!in_array($format, $this->data_formats)) {
  108. $data_formats = implode(', ', $this->data_formats);
  109. $msg = "Invalid data format '{$format}'. Data format must be one of the following: {$data_formats}";
  110. $this->display_error(__FUNCTION__, $msg);
  111. return false;
  112. }
  113. $this->data_format = $format;
  114. return true;
  115. }
  116. //*************************************************************************
  117. // No Escape Function
  118. //*************************************************************************
  119. public function no_escape_element($e)
  120. {
  121. settype($e, 'string');
  122. if ($e !== '') {
  123. $this->no_escape_elements[$e] = $e;
  124. return true;
  125. }
  126. return false;
  127. }
  128. //*************************************************************************
  129. // Set Template Function
  130. //*************************************************************************
  131. public function set_template($template)
  132. {
  133. $this->template = (string)$template;
  134. return true;
  135. }
  136. //*************************************************************************
  137. // Set Page Data Function
  138. //*************************************************************************
  139. public function set_data($node, $val, $append=false)
  140. {
  141. if (isset($this->data[$node]) && $append) {
  142. $this->data[$node] .= $val;
  143. }
  144. else { $this->data[$node] = $val; }
  145. return true;
  146. }
  147. //*************************************************************************
  148. // Get Data Function
  149. //*************************************************************************
  150. public function get_data($node)
  151. {
  152. return (isset($this->data[$node])) ? ($this->data[$node]) : (false);
  153. }
  154. //*************************************************************************
  155. // Get All Data Function
  156. //*************************************************************************
  157. public function get_all_data($node)
  158. {
  159. return $this->data;
  160. }
  161. //*************************************************************************
  162. // Delete Data Function
  163. //*************************************************************************
  164. public function delete_data($node)
  165. {
  166. if (isset($this->data[$node])) {
  167. unset($this->data[$node]);
  168. return true;
  169. }
  170. return false;
  171. }
  172. //*************************************************************************
  173. // Set Show Data Only Function
  174. //*************************************************************************
  175. public function set_show_data_only($flag=true) { $this->show_data_only = (bool)$flag; }
  176. //*************************************************************************
  177. /**
  178. * Add a Javascript File to be included
  179. * @param string Javascript File
  180. **/
  181. //*************************************************************************
  182. public function add_js_file($file)
  183. {
  184. if ($file) { $this->js_files[] = $file; }
  185. }
  186. //*************************************************************************
  187. /**
  188. * Add a CSS File to be included
  189. * @param array CSS link attributes
  190. **/
  191. //*************************************************************************
  192. public function add_css_file($file_attrs)
  193. {
  194. if (is_array($file_attrs)) {
  195. if (!isset($file_attrs['rel'])) { $file_attrs['rel'] = 'stylesheet'; }
  196. if (!isset($file_attrs['type'])) { $file_attrs['type'] = 'text/css'; }
  197. if (!isset($file_attrs['media'])) { $file_attrs['media'] = 'all'; }
  198. $this->css_files[] = $file_attrs;
  199. }
  200. else {
  201. settype($file_attrs, 'string');
  202. $css_file = $file_attrs;
  203. $file_attrs = array();
  204. $file_attrs['href'] = $css_file;
  205. $file_attrs['rel'] = 'stylesheet';
  206. $file_attrs['type'] = 'text/css';
  207. $file_attrs['media'] = 'all';
  208. $this->css_files[] = $file_attrs;
  209. }
  210. }
  211. //*************************************************************************
  212. //*************************************************************************
  213. // Render Function
  214. //*************************************************************************
  215. //*************************************************************************
  216. public function render()
  217. {
  218. //-------------------------------------------------------------
  219. // No Render?
  220. //-------------------------------------------------------------
  221. if ($this->get_data('no-render') || (defined('POFW_SKIP_RENDER') && POFW_SKIP_RENDER)) {
  222. return true;
  223. }
  224. //-------------------------------------------------------------
  225. // JavaScript / CSS Add-in Files
  226. //-------------------------------------------------------------
  227. if (!empty($this->js_files)) {
  228. $this->set_data('js_files', $this->js_files);
  229. }
  230. if (!empty($this->css_files)) {
  231. $this->set_data('css_files', $this->css_files);
  232. }
  233. //-------------------------------------------------------------
  234. // Escape Data (or not)
  235. //-------------------------------------------------------------
  236. if ($this->data_format == 'xml') {
  237. foreach ($this->data as $dkey => &$dval) {
  238. if (!isset($this->no_escape_elements[$dkey])) {
  239. $dval = xml_escape_array($dval);
  240. }
  241. }
  242. }
  243. //-------------------------------------------------------------
  244. // Create Data
  245. //-------------------------------------------------------------
  246. if ($this->data_format == 'xml') {
  247. $data = array2xml($this->root_node, $this->data);
  248. }
  249. else if ($this->data_format == 'json') {
  250. $data = json_encode($this->data);
  251. }
  252. else {
  253. $data = $this->data;
  254. }
  255. //-------------------------------------------------------------
  256. // Output
  257. //-------------------------------------------------------------
  258. if ($this->show_data_only) {
  259. if (is_array($data)) {
  260. print_array($data);
  261. }
  262. else {
  263. print $data;
  264. }
  265. return true;
  266. }
  267. else {
  268. $render_function = $this->get_data('render-function');
  269. //----------------------------------------------------
  270. // XML
  271. //----------------------------------------------------
  272. if ($this->data_format == 'xml') {
  273. if ($render_function) {
  274. return $render_function($data, $this->template);
  275. }
  276. else {
  277. if (file_exists($this->template)) {
  278. return xml_transform($data, $this->template);
  279. }
  280. else {
  281. if (empty($this->template)) {
  282. print "No template file has been specified.";
  283. }
  284. else {
  285. print "Invalid template file specified.";
  286. }
  287. }
  288. }
  289. }
  290. //----------------------------------------------------
  291. // JSON / Array
  292. //----------------------------------------------------
  293. else {
  294. if ($render_function) {
  295. return $render_function($data, $this->template);
  296. }
  297. else {
  298. print "No valid render function was specified.";
  299. }
  300. }
  301. }
  302. return false;
  303. }
  304. }