PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Presenter.class.php

https://github.com/mrbmc/erector
PHP | 271 lines | 215 code | 43 blank | 13 comment | 21 complexity | c92b49f091fc8aeb35b702557f1462d4 MD5 | raw file
  1. <?php
  2. /**
  3. * PRESENTER CLASS
  4. * @author brian@mrbmc.com
  5. * @description Loads a template engine and generates the output.
  6. * @todo Create an abstract class for each format
  7. */
  8. class Presenter
  9. {
  10. private $controller;
  11. private $view;
  12. private $format;
  13. private $smarty;
  14. private $OUTPUT;
  15. private $media;
  16. private function __construct () {
  17. $this->controller = Dispatcher::instance()->controllerInstance;
  18. $this->format = (isset($_GET['format'])) ? $_GET['format'] : Dispatcher::instance()->format;
  19. }
  20. private static $_instance;
  21. public static function instance () {
  22. if (!isset(self::$_instance)) {
  23. $_classname = __CLASS__;
  24. self::$_instance = new $_classname;
  25. }
  26. return self::$_instance;
  27. }
  28. public function present() {
  29. $method = $this->controller->format;
  30. $this->$method();
  31. if(!isset($this->controller->redirect))
  32. Session::instance()->set('feedback',"");
  33. }
  34. private function validateTemplate () {
  35. $this->view = $this->controller->view;
  36. if(stristr($this->view,".tpl")===false)
  37. $this->view .= ".tpl";
  38. if(!file_exists(APP . "/views/" . $this->view)) {
  39. Debugger::trace("Template could not be found",$this->view);
  40. $this->view = "./errors/404.tpl";
  41. }
  42. }
  43. //Compile the view
  44. private function compile () {
  45. include_once LIB.'/smarty/Smarty.class.php';//Template engine
  46. $this->smarty = new Smarty();
  47. $this->smarty->debugging = DEBUG;
  48. $this->smarty->template_dir = APP.'/views';
  49. $this->smarty->compile_dir = $this->smarty->cache_dir = LIB.'/../cache';
  50. $this->smarty->config_dir = LIB.'/smarty/configs';
  51. $this->smarty->caching = !DEBUG;
  52. $this->smarty->force_compile = DEBUG;
  53. //Debugger::trace($this->smarty);
  54. $this->validateTemplate();
  55. $this->smarty->assign('MEDIA', $this->media);
  56. $this->smarty->assign('DISPATCHER', obj_to_arr(Dispatcher::instance()));
  57. $_config = obj_to_arr(Config::instance());
  58. $c = new ReflectionClass(Config::instance());
  59. $cc = ($c->getConstants());
  60. foreach($cc as $k=>$v) $_config[$k] = $v;
  61. $this->smarty->assign('CONFIG', $_config);
  62. $this->smarty->assign('DOCROOT', "http://".$_SERVER['HTTP_HOST']);
  63. $this->smarty->assign('DATA', (array)($this->controller));
  64. $this->smarty->assign('USER', (array)($this->controller->user));
  65. $this->OUTPUT = $this->smarty->fetch($this->view);
  66. if(DEBUG)
  67. $this->OUTPUT .= Debugger::$console;
  68. }
  69. private function isNotAssocArray($arr)
  70. {
  71. return (0 !== array_reduce(
  72. array_keys($arr),
  73. create_function('$a, $b', 'return ($b === $a ? $a + 1 : 0);'),
  74. 0
  75. )
  76. );
  77. }
  78. private function objToXML ($obj) {
  79. $str = "";
  80. if(is_object($obj)) {
  81. $str .= "<". strtolower(get_class($obj)) . ">";
  82. foreach($obj as $k => $v)
  83. {
  84. if(is_object($v))
  85. $str .= $this->objToXML($v);
  86. else
  87. if(is_array($v))
  88. $str .= "<$k>".$this->objToXML($v)."</$k>";
  89. else {
  90. $str .= "<$k>";
  91. if(!is_numeric($v)) $str .= "<![CDATA[";
  92. $str .= $v;
  93. if(!is_numeric($v)) $str .= "]]>";
  94. $str .= "</$k>";
  95. }
  96. }
  97. $str .= "</". strtolower(get_class($obj)) . ">";
  98. } elseif (is_array($obj) && $this->isNotAssocArray($obj)) {
  99. foreach ($obj as $k=>$v)
  100. {
  101. $str .= is_array($v) ? "<$v>".$this->objToXML($v)."</$v>" : (is_object($v) ? $this->objToXML($v) : "<$v />");
  102. }
  103. } elseif (!$this->isNotAssocArray($obj)) {
  104. foreach($obj as $k => $v)
  105. {
  106. $str .= "<$k>";
  107. if(!is_numeric($v)) $str .= "<![CDATA[";
  108. $str .= $v;
  109. if(!is_numeric($v)) $str .= "]]>";
  110. $str .= "</$k>";
  111. }
  112. } else {
  113. $str .= "<$k>$v</$k>";
  114. }
  115. return $str;
  116. }
  117. private function xml () {
  118. header("Content-type: text/xml");
  119. $this->OUTPUT .= "<".Dispatcher::instance()->controller.">";
  120. $this->OUTPUT .= $this->objToXML($this->controller->data);
  121. $this->OUTPUT .= "</".Dispatcher::instance()->controller.">";
  122. print $this->OUTPUT;
  123. }
  124. private function json () {
  125. header("Content-type: text/plain");
  126. $this->OUTPUT = json_encode($this->controller->data);
  127. print $this->OUTPUT;
  128. }
  129. private function is_box($obj) {
  130. return (is_array($obj) || is_object($obj));
  131. }
  132. private function objToCSV ($obj) {
  133. $str = "";
  134. foreach($obj as $o)
  135. {
  136. $hdr = "";
  137. foreach($o as $k => $v)
  138. {
  139. $hdr .= "\"$k\",";
  140. $str .= $this->is_box($v) ? "\n".$this->objToCSV($v) : "\"$v\",";
  141. }
  142. $str .= "\n";
  143. }
  144. return $hdr."\n".$str;
  145. }
  146. private function xls () {
  147. $this->OUTPUT .= $this->objToCSV($this->controller->data);
  148. header("Pragma: public"); // required
  149. header("Expires: 0");
  150. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  151. header("Cache-Control: private",false); // required for certain browsers
  152. header("Content-Type: application/vnd.ms-excel");
  153. header("Content-Disposition: attachment; filename=\"".$this->controller->title.".csv\";" );
  154. header("Content-Transfer-Encoding: binary");
  155. print $this->OUTPUT;
  156. }
  157. private function csv () {
  158. $this->OUTPUT .= $this->objToCSV($this->controller->data);
  159. // header("Content-type: text/plain");
  160. header("Pragma: public"); // required
  161. header("Expires: 0");
  162. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  163. header("Cache-Control: private",false); // required for certain browsers
  164. header("Content-type: application/octet-stream");
  165. header("Content-Disposition: attachment; filename=\"".Dispatcher::instance()->action."-".date("Ymd").".csv\"");
  166. print $this->OUTPUT;
  167. }
  168. private function pdf () {
  169. include_once LIB.'/dompdf/dompdf_config.inc.php';
  170. $dompdf = new DOMPDF();
  171. $dompdf->set_paper("letter","portrait");
  172. $dompdf->load_html($this->OUTPUT);
  173. $dompdf->render();
  174. $dompdf->stream($this->controller->title . ".pdf",array("Attachment"=>0));
  175. }
  176. private function email () {
  177. include_once LIB.'/mailers/PHPMailer.class.php';
  178. $this->compile();
  179. $mail = new PHPMailer();
  180. $mail->Host = "localhost";
  181. //$mail->Mailer = "smtp";
  182. $mail->Body = $this->OUTPUT;
  183. $mail->AltBody = strip_tags($this->OUTPUT);
  184. $mail->Subject = $this->controller->title;
  185. $mail->From = isset($this->controller->EMAIL_FROM) ? $this->controller->EMAIL_FROM : Config::instance()->EMAIL_ADDRESS;
  186. $mail->FromName = isset($this->controller->EMAIL_FROM) ? $this->controller->EMAIL_FROM : Config::instance()->EMAIL_NAME;
  187. // if(isset($_REQUEST['EMAIL'])) {
  188. // array_push($this->controller->EMAIL_LIST,array($_REQUEST['EMAIL']));
  189. // }
  190. foreach($this->controller->EMAIL_LIST as $toAddy)
  191. {
  192. $mail->AddAddress($toAddy);
  193. if(!$mail->Send())
  194. die("There has been a mail error sending to " . $toAddy);
  195. $mail->ClearAddresses();
  196. $mail->ClearAttachments();
  197. }
  198. if(isset($this->controller->redirect)) {
  199. header("Location: ".$this->controller->redirect);
  200. } elseif($this->controller->view_post) {
  201. $this->controller->view = $this->controller->view_post;
  202. $this->html();
  203. } else {
  204. header("Content-type: text/html");
  205. print $this->OUTPUT;
  206. }
  207. }
  208. private function text () {
  209. $this->compile();
  210. header("Content-type: text/plain");
  211. print strip_tags($this->OUTPUT);
  212. }
  213. private function txt() {
  214. $this->text();
  215. }
  216. private function html () {
  217. if(isset($this->controller->redirect))
  218. return header("Location: ".$this->controller->redirect);
  219. else {
  220. include_once LIB.'/mobile_device_detect.php';
  221. $this->media = mobile_device_detect() ? "mobile" : "screen";
  222. $this->compile();
  223. header("Content-type: text/html");
  224. print $this->OUTPUT;
  225. }
  226. }
  227. }
  228. ?>