Guest User

Untitled

a guest
Apr 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. <?php
  2. /*======================================================================*\
  3. || #################################################################### ||
  4. || # Php Ajax Multimedia website framework, or just "Pam" # ||
  5. || # ---------------------------------------------------------------- # ||
  6. || # Copyright ©2008 Digital Media Graphix LLC. All Rights Reserved. # ||
  7. || # ---------------------------------------------------------------- # ||
  8. || # Version 1.0.0 RC1 February 25 2008 # ||
  9. || #################################################################### ||
  10. \*======================================================================*/
  11. /**
  12. * Templates Controller. Segregates the view files from the controller code
  13. * and also determines whether to send HTML out or send XML out for
  14. * javascript Ajax requests.
  15. */
  16. class PAMWF_Templates_prototype extends PAMWF_HTMLOut
  17. {
  18. protected $coreJS = array(
  19. 'lib/prototype',
  20. 'src/scriptaculous',
  21. 'PAMWF'
  22. );
  23.  
  24. protected $localJS = array();
  25.  
  26. /**
  27. * Array of elements to be parsed into the scope of the phtml file.
  28. * Controllers should assign outgoing values into members of this
  29. * class where they can be picked up by the template files and
  30. * sent out.
  31. *
  32. * @var array
  33. */
  34. protected $output = array();
  35.  
  36. /**
  37. * Array of parsed XML tags.
  38. *
  39. * @var array
  40. */
  41. protected $xmlOutput = array();
  42.  
  43. /**
  44. * Print XML output.
  45. *
  46. */
  47. protected function printXML()
  48. {
  49. PAMWF::setHeader('type', 'Content-Type: application/xml;');
  50. $this->printFinalOutput('<pamwf>'.implode("\n",$this->xmlOutput).'</pamwf>');
  51. }
  52.  
  53. /**
  54. * Build an XML tag using the specified template and output.
  55. *
  56. * @param string XML tag
  57. * @param string Template to use. Default PAMWF::$targetFile
  58. * @param array Output to use. Default $this->output
  59. */
  60. protected function buildTag( $tag, $template = null, $output = null )
  61. {
  62. $this->xmlOutput[$tag] = "<$tag><![CDATA[".$this->parseTemplate( $template, $output )."]]></$tag>";
  63. }
  64.  
  65. /**
  66. * Parse a phtml file into an html file.
  67. *
  68. * @param string Template to use, default PAMWF::$targetFile
  69. * @param array Output to use, default $this->output
  70. * @return string
  71. */
  72. protected function parseTemplate( $template = null, $output = null )
  73. {
  74. if ($template == null)
  75. {
  76. $template = PAMWF::$targetFile;
  77. }
  78. else
  79. {
  80. $template = PAMWF_DIR_PROJECT.DIRECTORY_SEPARATOR.$template;
  81. }
  82.  
  83. if (is_array($output))
  84. {
  85. extract($output);
  86. }
  87. else
  88. {
  89. extract($this->output);
  90. }
  91.  
  92. @ob_start();
  93. include( $template );
  94. return @ob_get_clean();
  95. }
  96.  
  97. /**
  98. * Parse out Javascript files used on the clientview of the pages this
  99. * controller is responsible for.
  100. *
  101. * @param array output to use to affect the javascripts of this controller. (default $this->output)
  102. * @return return string
  103. */
  104. protected function parseJS( $output = null )
  105. {
  106. if (is_array($output))
  107. {
  108. extract($output);
  109. }
  110. else
  111. {
  112. extract($this->output);
  113. }
  114.  
  115. @ob_start();
  116. $this->startUpJS();
  117. return @ob_get_clean();
  118. }
  119.  
  120. /**
  121. * Print output to the browser. Ajax requests receive XML from this function.
  122. *
  123. */
  124. public function printOutput()
  125. {
  126. if (!PAMWF::isEmpty($this->xmlOutput))
  127. {
  128. $this->printXML();
  129. }
  130. else
  131. {
  132. $output = $this->parseTemplate();
  133.  
  134. $js = $this->parseJS();
  135.  
  136. if ($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest')
  137. {
  138. if ($js)
  139. {
  140. $js = str_replace('</script>', '', str_replace('<script type="text/javascript">', '', $js));
  141. }
  142.  
  143. $js = $this->linkJS() . '<script type="text/javascript">' . $js . '</script>';
  144.  
  145. $this->printFinalOutput(str_replace('<head>', '<head>'.$js, $output));
  146.  
  147. }
  148. else
  149. {
  150. if ($js)
  151. {
  152. $this->xmlOutput['javascript'] = "<javascript><![CDATA[".$js."]]></javascript>";
  153. }
  154.  
  155. $this->xmlOutput['response'] = "<response><![CDATA[".$output."]]></response>";
  156. $this->printXML();
  157. }
  158. }
  159. }
  160.  
  161. protected function linkJS()
  162. {
  163. $js = array_merge($this->coreJS, $this->localJS);
  164. $output = '';
  165.  
  166. foreach ($js as $script)
  167. {
  168. $output .= '<script type="text/javascript" src="'.PAMWF_WEBROOT.'js/'.$script.".js\"></script>\n";
  169. }
  170.  
  171. return $output;
  172. }
  173.  
  174. // This empty function is in place so children can extend it as necessary, and place their
  175. // attendant javascripts in the controller where they can be read alongside the php side
  176. // customizations the page requires.
  177. protected function startUpJS() {}
  178. }
  179. ?>
Add Comment
Please, Sign In to add comment