PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/atk4/lib/ApiFrontend.php

https://github.com/intuititve/lostandfound
PHP | 140 lines | 88 code | 16 blank | 36 comment | 11 complexity | 0483266cab180804322042c88d8b9579 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /**
  3. * This is the most appropriate API file for your web application. It builds on top of ApiWeb
  4. * and introduces concept of "Pages" on top of "Layout" concept defined in ApiWeb.
  5. *
  6. * @link http://agiletoolkit.org/learn/understand/api
  7. * @link http://agiletoolkit.org/learn/template
  8. *//*
  9. ==ATK4===================================================
  10. This file is part of Agile Toolkit 4
  11. http://agiletoolkit.org/
  12. (c) 2008-2012 Romans Malinovskis <romans@agiletoolkit.org>
  13. Distributed under Affero General Public License v3
  14. See http://agiletoolkit.org/about/license
  15. =====================================================ATK4=*/
  16. class ApiFrontend extends ApiWeb{
  17. /** When page is determined, it's class instance is created and stored in here */
  18. public $page_object=null;
  19. /** @depreciated (might be refactored) - type of returned content */
  20. public $content_type='page'; // content type: rss/page/etc
  21. /** Class which is used for static pages */
  22. public $page_class='Page';
  23. // {{{ Layout Implementation
  24. /** Content in the global (shared.html) template is rendered by page object. This method loads either class or static file */
  25. function initLayout(){
  26. parent::initLayout();
  27. $this->addLayout('Content');
  28. $this->upgradeChecker();
  29. }
  30. function layout_Content(){
  31. // required class prefix depends on the content_type
  32. // This function initializes content. Content is page-dependant
  33. $page=str_replace('/','_',$this->page);
  34. $page=str_replace('-','',$page);
  35. $class=$this->content_type.'_'.$page;
  36. if($this->api->page_object)return; // page is already initialized;
  37. if(method_exists($this,$class)){
  38. // for page we add Page class, for RSS - RSSchannel
  39. // TODO - this place is suspicious. Can it call arbitary function from API?
  40. $this->page_object=$this->add($this->content_type=='page'?$this->page_class:'RSSchannel',$this->page);
  41. $this->$class($this->page_object);
  42. }else{
  43. try{
  44. loadClass($class);
  45. }catch(PathFinder_Exception $e){
  46. // page not found, trying to load static content
  47. try{
  48. $this->loadStaticPage($this->page);
  49. }catch(PathFinder_Exception $e2){
  50. $class_parts=explode('_',$page);
  51. $funct_parts=array();
  52. while($class_parts){
  53. array_unshift($funct_parts,array_pop($class_parts));
  54. $fn='page_'.join('_',$funct_parts);
  55. $in='page_'.join('_',$class_parts);
  56. try {
  57. loadClass($in);
  58. }catch(PathFinder_Exception $e2){
  59. continue;
  60. }
  61. // WorkAround for PHP5.2.12+ PHP bug #51425
  62. $tmp=new $in;
  63. if(!method_exists($tmp,$fn) && !method_exists($tmp,'subPageHandler'))continue;
  64. $this->page_object=$this->add($in,$page);
  65. if(method_exists($tmp,$fn)){
  66. $this->page_object->$fn();
  67. }elseif(method_exists($tmp,'subPageHandler')){
  68. if($this->page_object->subPageHandler(join('_',$funct_parts))===false)break;
  69. }
  70. return;
  71. }
  72. // throw original error
  73. $this->pageNotFound($e);
  74. }
  75. return;
  76. }
  77. // i wish they implemented "finally"
  78. $this->page_object=$this->add($class,$page,'Content');
  79. if(method_exists($this->page_object,'initMainPage'))$this->page_object->initMainPage();
  80. if(method_exists($this->page_object,'page_index'))$this->page_object->page_index();
  81. }
  82. }
  83. /** This method is called as a last resort, when page is not found. It receives the exception with the actual error */
  84. function pageNotFound($e){
  85. throw $e;
  86. }
  87. /** Attempts to load static page. Raises exception if not found */
  88. protected function loadStaticPage($page){
  89. try{
  90. $t='page/'.str_replace('_','/',strtolower($page));
  91. $this->template->findTemplate($t);
  92. $this->page_object=$this->add($this->page_class,$page,'Content',array($t));
  93. }catch(PathFinder_Exception $e2){
  94. $t='page/'.strtolower($page);
  95. $this->template->findTemplate($t);
  96. $this->page_object=$this->add($this->page_class,$page,'Content',array($t));
  97. }
  98. return $this->page_object;
  99. }
  100. // }}}
  101. // {{{ Obsolete functions
  102. /** @obsolete - does nothing */
  103. function execute(){
  104. try{
  105. parent::execute();
  106. }catch(Exception $e){
  107. $this->caughtException($e);
  108. }
  109. }
  110. /** @obsolete */
  111. function getRSSURL($rss,$args=array()){
  112. $tmp=array();
  113. foreach($args as $arg=>$val){
  114. if(!isset($val) || $val===false)continue;
  115. if(is_array($val)||is_object($val))$val=serialize($val);
  116. $tmp[]="$arg=".urlencode($val);
  117. }
  118. return
  119. $rss.'.xml'.($tmp?'?'.join('&',$tmp):'');
  120. }
  121. // }}}
  122. }