PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/atk4/lib/ApiFrontend.php

https://github.com/git86/todo
PHP | 132 lines | 82 code | 14 blank | 36 comment | 10 complexity | 54d59263b962eb26ab4a1bc6e68a06a0 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-2011 Romans Malinovskis <atk@agiletech.ie>
  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 layout_Content(){
  26. // required class prefix depends on the content_type
  27. // This function initializes content. Content is page-dependant
  28. $page=str_replace('/','_',$this->page);
  29. $page=str_replace('-','',$page);
  30. $class=$this->content_type.'_'.$page;
  31. if(method_exists($this,$class)){
  32. // for page we add Page class, for RSS - RSSchannel
  33. // TODO - this place is suspicious. Can it call arbitary function from API?
  34. $this->page_object=$this->add($this->content_type=='page'?$this->page_class:'RSSchannel',$this->page);
  35. $this->$class($this->page_object);
  36. }else{
  37. try{
  38. loadClass($class);
  39. }catch(PathFinder_Exception $e){
  40. // page not found, trying to load static content
  41. try{
  42. $this->loadStaticPage($this->page);
  43. }catch(PathFinder_Exception $e2){
  44. $class_parts=explode('_',$page);
  45. $funct_parts=array();
  46. while($class_parts){
  47. array_unshift($funct_parts,array_pop($class_parts));
  48. $fn='page_'.join('_',$funct_parts);
  49. $in='page_'.join('_',$class_parts);
  50. try {
  51. loadClass($in);
  52. }catch(PathFinder_Exception $e2){
  53. continue;
  54. }
  55. // WorkAround for PHP5.2.12+ PHP bug #51425
  56. $tmp=new $in;
  57. if(!method_exists($tmp,$fn) && !method_exists($tmp,'subPageHandler'))continue;
  58. $this->page_object=$this->add($in,$page);
  59. if(method_exists($tmp,$fn)){
  60. $this->page_object->$fn();
  61. }elseif(method_exists($tmp,'subPageHandler')){
  62. if($this->page_object->subPageHandler(join('_',$funct_parts))===false)break;
  63. }
  64. return;
  65. }
  66. // throw original error
  67. $this->pageNotFound($e);
  68. }
  69. return;
  70. }
  71. // i wish they implemented "finally"
  72. $this->page_object=$this->add($class,$page,'Content');
  73. if(method_exists($this->page_object,'initMainPage'))$this->page_object->initMainPage();
  74. if(method_exists($this->page_object,'page_index'))$this->page_object->page_index();
  75. }
  76. }
  77. /** This method is called as a last resort, when page is not found. It receives the exception with the actual error */
  78. function pageNotFound($e){
  79. throw $e;
  80. }
  81. /** Attempts to load static page. Raises exception if not found */
  82. protected function loadStaticPage($page){
  83. try{
  84. $t='page/'.str_replace('_','/',strtolower($page));
  85. $this->template->findTemplate($t);
  86. $this->page_object=$this->add($this->page_class,$page,'Content',array($t));
  87. }catch(PathFinder_Exception $e2){
  88. $t='page/'.strtolower($page);
  89. $this->template->findTemplate($t);
  90. $this->page_object=$this->add($this->page_class,$page,'Content',array($t));
  91. }
  92. return $this->page_object;
  93. }
  94. // }}}
  95. // {{{ Obsolete functions
  96. /** @obsolete - does nothing */
  97. function execute(){
  98. try{
  99. parent::execute();
  100. }catch(Exception $e){
  101. $this->caughtException($e);
  102. }
  103. }
  104. /** @obsolete */
  105. function getRSSURL($rss,$args=array()){
  106. $tmp=array();
  107. foreach($args as $arg=>$val){
  108. if(!isset($val) || $val===false)continue;
  109. if(is_array($val)||is_object($val))$val=serialize($val);
  110. $tmp[]="$arg=".urlencode($val);
  111. }
  112. return
  113. $rss.'.xml'.($tmp?'?'.join('&',$tmp):'');
  114. }
  115. // }}}
  116. }