PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/aqua/Applet.php

https://github.com/agilehands/PHP-AQUA
PHP | 248 lines | 150 code | 44 blank | 54 comment | 20 complexity | 23e98aa3ba2ebf9d3d87f3e2d037cb61 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * #######################################################################
  5. * +----------------------------------------------+
  6. * | THIS FILE IS A PART OF "PHP AQUA" FRAMEWORK |
  7. * +----------------------------------------------+
  8. *
  9. * THIS CODE IS PROTECTED UNDER Apache Software License
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Simply stating :
  13. * Proprietary Software linking: Yes
  14. * Distribution of this software : Yes
  15. * Redistributing of the code with changes: You can
  16. * Compatible with GNU GPL : NO :D
  17. *
  18. * Feel free to change and use but don't need to share the code.
  19. *
  20. *
  21. *
  22. * ################ http://www.phpaqua.com ###############
  23. */
  24. /**
  25. * !!!!!! THIS DOCUMENTATION IS NOT MAINTAINED AND MAY BE OBSOLET !!!!!
  26. * FOR LATEST DOCUMENT PLZ REFER TO SITE: http://www.phpaqua.com
  27. *
  28. *
  29. * Applets are like modules. They are used to implement some functionalities
  30. * and develop the application in a more modular fashion.
  31. *
  32. */
  33. namespace aqua;
  34. use aqua\exception\AquaException;
  35. class Applet extends Controller{
  36. /**
  37. * ID of the applet, used in making the url
  38. */
  39. private $_id;
  40. private $_dir;
  41. public $_appletZone;
  42. /**
  43. * @param $name The name of the applet
  44. */
  45. public function __construct( $id, $dir ){
  46. parent::__construct();
  47. $this->_isApplet = true;
  48. $this->_className = get_class($this);
  49. $this->_id = $id;
  50. $this->_dir = $dir;
  51. }
  52. public function call( $method, $params = array()){
  53. $_GET[ $this->_id ] = base64_encode( json_encode( array('action'=>$method, 'params'=>$params)) );
  54. return App::$instance->url('','',$_GET);
  55. }
  56. public function anchor( $method, $text, $title='', $cssClass='', $params = array() ){
  57. $_GET[ $this->_id ] = base64_encode( json_encode( array('action'=>$method, 'params'=>$params)) );
  58. echo '<a href="'.App::$instance->url('','',$_GET).'" title="'.$title.'" class="'.$cssClass.'" >'.$text.'</a>';
  59. }
  60. public function action( $url = 'index', $params= array() ){
  61. if(empty($url))$url = 'index';
  62. return App::$instance->appletActionURL( $this->_className, $this->_id, $url, $params);
  63. }
  64. function state($key){
  65. if(func_num_args() > 1){// want to set
  66. $this->session($this->_id.$key, func_get_arg(1));
  67. }else{
  68. return $this->session($this->_id.$key);
  69. }
  70. }
  71. function index(){
  72. }
  73. /**
  74. * Use this function to include some arbitary view organized
  75. * in applets
  76. * Applet::view('someview',array('some'=>'params'));
  77. *
  78. * No applet class is required! Just place a view in an applet
  79. * folder and use it!
  80. */
  81. public static function view( $applet, $view, $params = array() ){
  82. $path = App::$instance->getAppletView( $applet, $view, true );
  83. extract($params);
  84. // include the lang file
  85. $appletDir = App::$instance->getAppletDir( $applet, true );
  86. @include( $appletDir . '/lang/'.App::getDefaultLocale() . '.php');
  87. @include( $appletDir . '/lang/'.App::getCurrentLocale() . '.php');
  88. include( $path );
  89. }
  90. public static function render( $applet, $id, $view='', $args=array() ){
  91. $obj = App::$instance->loadAppletObj( $applet, $id );
  92. if( !empty( $view ) ){
  93. $obj->show( $view, $args );
  94. return;
  95. }
  96. $action = $obj->state('__lastAction__');
  97. if( empty( $action) ) $action = 'index';
  98. $params = $obj->state('__lastActionParams__');
  99. if( empty( $params) ) $params = $args;
  100. if( $obj->state( '__executed__') == true ){
  101. $obj->state( '__executed__', false );
  102. }else{
  103. $data = @$_GET[ $id ]; // safe to suppress, checking array key exist takes time
  104. //
  105. if( !empty( $data ) ){
  106. $data = json_decode( base64_decode( $data ), true);
  107. $action = $data['action'];
  108. $params = $data['params'];
  109. }
  110. if( !method_exists( $obj, $action )){
  111. $action = 'index';
  112. }
  113. }
  114. if( App::$instance->willCallAppletMethod( $applet, $id, $action, $params )){
  115. call_user_func_array( array( $obj, $action ), $params);
  116. $obj->state('__lastAction__', $action );
  117. $obj->state('__lastActionParams__', $params );
  118. App::$instance->didCallAppletMethod( $applet, $id, $action, $params );
  119. }
  120. }
  121. /**
  122. * In Applet mode, it is not possible to show any view directly because
  123. * site is redirected to last url.
  124. *
  125. * Call this method to ensure that this method will be called after redirection
  126. */
  127. protected function execute( $method, $params ){
  128. $this->state( '__executed__', true );
  129. $this->state( '__lastAction__', $method );
  130. $this->state( '__lastActionParams__', $params );
  131. }
  132. public static function asset( $applet, $file, $ext, $url = true){
  133. $path = App::$instance->getLocalizedPath( 'applets/'.$applet.'/assets', $file ,$ext );
  134. if( !$path ){
  135. // check in global folder
  136. $prefix = GLOBAL_DIR.'/applets/'.$applet.'/assets';
  137. if( file_exists( $prefix.'/'.App::$instance->currentLocale.'/'.$file.$ext ) ){
  138. $path = $prefix.'/'.App::$instance->currentLocale.'/'.$file.$ext;
  139. }
  140. if( file_exists( $prefix.'/'.App::$instance->defaultLocale.'/'.$file.$ext ) ){
  141. $path = $prefix.'/'.App::$instance->defaultLocale.'/'.$file.$ext;
  142. }
  143. if( file_exists( $prefix.'/'.$file.$ext ) ){
  144. $path = $prefix.'/'.$file.$ext;
  145. }
  146. if( !$path ){
  147. throw new AquaException( AquaException::APPLET_GLOBAL_ASSET_LINK_ASKED, array( $applet, $file ) );
  148. }
  149. }
  150. if( $path){
  151. if( $url ){
  152. return str_replace( App::$instance->directory.'/', App::$instance->baseURL, $path );
  153. }
  154. return $path;
  155. }
  156. throw new AquaException( AquaException::APPLET_ASSET_NOT_FOUND, array( $applet, $file ) );
  157. }
  158. public static function css( $applet, $file, $link = true, $params = array() ){
  159. $path = self::asset( $applet, 'css/'.$file, '', $link );
  160. if( $link ){
  161. print("\n\t");
  162. echo '<link href="'.$path.'" rel="stylesheet" type="text/css" />';
  163. print("\n\r");
  164. }else{
  165. print("\n\t<style type='text/css'>\n");
  166. echo str_replace( array_keys( $params)
  167. , array_values( $params)
  168. , file_get_contents( $path )
  169. );
  170. print("\n\t</style>\n\r");
  171. }
  172. }
  173. public static function js( $applet, $file, $link = true, $params = array() ){
  174. $path = self::asset( $applet, 'js/'.$file, '', $link );
  175. if( $link ){
  176. echo "\n\t<script type='text/javascript' src='"
  177. ,$path
  178. , "'></script>\n\r";
  179. }else{
  180. print("\n\t<script type='text/javascript' >\n\t\t");
  181. echo str_replace( array_keys( $params)
  182. , array_values( $params)
  183. , file_get_contents( $path )
  184. );
  185. print("\n\t</script>\n\r");
  186. }
  187. }
  188. public static function imgURL( $applet, $file ){
  189. return self::asset( $applet, 'images/'.$file, '', true );
  190. }
  191. public static function img( $applet, $file, $title='', $cssClass='', $id = ''){
  192. echo '<img src="'.self::asset( $applet, 'images/'.$file, '', true ).'" class="'.$cssClass.'" title="'.$title.'" id="'.$id.'" />';
  193. }
  194. function once($key){
  195. if(func_num_args() > 1){// want to set
  196. $this->session($this->_id.$key, func_get_arg(1));
  197. }else{
  198. $ret = $this->session($this->_id.$key);
  199. unset($_SESSION[SESSION_PREFIX.$this->_id.$key]);
  200. return $ret;
  201. }
  202. }
  203. }// end class
  204. ?>