PageRenderTime 66ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Bal/Controller/Action/Helper/App.php

https://github.com/balupton/balphp
PHP | 387 lines | 186 code | 64 blank | 137 comment | 37 complexity | b574e2fed29a22984c978bec080a090e MD5 | raw file
  1. <?php
  2. class Bal_Controller_Action_Helper_App extends Bal_Controller_Action_Helper_Abstract {
  3. # ========================
  4. # VARIABLES
  5. protected $_options = array(
  6. 'logged_out_forward' => array(
  7. array('action'=>'login')
  8. ),
  9. 'logged_in_forward' => array(
  10. array('action'=>'login')
  11. )
  12. );
  13. # ========================
  14. # CONSTRUCTORS
  15. /**
  16. * Construct
  17. * @param array $options
  18. */
  19. public function __construct ( array $options = array() ) {
  20. # Prepare
  21. $result = true;
  22. # Done
  23. return method_exists(get_parent_class($this),$parent_method = __FUNCTION__) ? parent::$parent_method($options) : $result;
  24. }
  25. # ========================
  26. # NAVIGATION
  27. /**
  28. * Make an Nagivation Item active
  29. * @param string $menu
  30. * @param string $id
  31. * @param boolean $prefix [optional] Whether or not to prefix the id with the menu name
  32. * @return bool
  33. */
  34. public function applyNavigation ( ) {
  35. # Prepare
  36. $View = $this->getActionControllerView();
  37. $Request = $this->getActionControllerRequest();
  38. $NavData = array();
  39. # Module Config
  40. $module = $Request->getModuleName();
  41. $module_path = Bal_App::getFrontController()->getModuleDirectory($module);
  42. $module_config_path = $module_path . '/config';
  43. $config_path = Bal_App::getConfig('config_path');
  44. # Navigation
  45. $NavData = Bal_App::parseDataFile($module_config_path.'/nav',$module_config_path.'/compiled/nav.data');
  46. # Navigation Override
  47. $NavDataOver = Bal_App::parseAdvancedYamlFile(NAVIGATION_FILE_PATH,NAVIGATION_COMPILED_FILE_PATH);
  48. if ( !empty($NavDataOver) ) {
  49. $NavDataOver = delve($NavDataOver,$module);
  50. if ( !empty($NavDataOver) ) {
  51. $NavData = array_merge_recursive_keys($NavData, $NavDataOver);
  52. }
  53. }
  54. # Apply Navigation Menus
  55. $View->Navigation = array();
  56. foreach ( $NavData as $key => $value ) {
  57. $code = $Menu = null;
  58. if ( is_array($value) && !isset($value[0]) ) {
  59. foreach ( $value as $_key => $_value ) {
  60. $code = $key.'.'.$_key;
  61. $Menu = new Zend_Navigation($_value);
  62. $this->ApplyNavigationMenu($code, $Menu);
  63. }
  64. }
  65. else {
  66. $code = $key;
  67. $Menu = new Zend_Navigation($value);
  68. $this->ApplyNavigationMenu($code, $Menu);
  69. }
  70. }
  71. # ACL
  72. Zend_View_Helper_Navigation::setDefaultAcl($this->getAcl());
  73. Zend_View_Helper_Navigation::setDefaultRole($this->getRole());
  74. # Chain
  75. return $this;
  76. }
  77. public function applyNavigationMenu ( $code, Zend_Navigation $Navigation ) {
  78. # Prepare
  79. $View = $this->getActionControllerView();
  80. # Apply
  81. array_apply($View, 'Navigation.'.$code, $Navigation);
  82. # Done
  83. return true;
  84. }
  85. /**
  86. * Make an Nagivation Item active
  87. * @param string $menu
  88. * @param string $id
  89. * @param boolean $prefix [optional] Whether or not to prefix the id with the menu name
  90. * @return bool
  91. */
  92. public function activateNavigationItem ( $code, $id, $prefix = false, $error = true ) {
  93. # Prepare
  94. $View = $this->getActionControllerView();
  95. # Find
  96. $NavigationMenu = delve($View, 'Navigation.'.$code);
  97. if ( !$NavigationMenu ) throw new Zend_Exception('Could not find Navigation Menu: '.$code);
  98. # Prefix
  99. if ( $prefix ) {
  100. $id = str_replace('.','-',$code).'-'.$id;
  101. }
  102. # Activiate
  103. $result = $this->activateNavigationMenuItem($NavigationMenu, $id);
  104. if ( !$result && $error ) throw new Zend_Exception('Could not find Navigation Menu Item: '.$code.' -> '.$id);
  105. # Return Result
  106. return $result;
  107. }
  108. /**
  109. * Has an Nagivation Item?
  110. * @param string $menu
  111. * @param string $id
  112. * @param boolean $prefix [optional] Whether or not to prefix the id with the menu name
  113. * @return bool
  114. */
  115. public function hasNavigationItem ( $code, $id, $prefix = false ) {
  116. # Prepare
  117. $View = $this->getActionControllerView();
  118. # Find
  119. $NavigationMenu = delve($View, 'Navigation.'.$code);
  120. if ( !$NavigationMenu ) throw new Zend_Exception('Could not find Navigation Menu: '.$code);
  121. # Prefix
  122. if ( $prefix ) {
  123. $id = str_replace('.','-',$code).'-'.$id;
  124. }
  125. # Activiate
  126. $result = $this->hasNavigationMenuItem($NavigationMenu, $id);
  127. # Return Result
  128. return $result;
  129. }
  130. /**
  131. * Has a Navigation Menu Item?
  132. * @return
  133. */
  134. public function hasNavigationMenuItem ( Zend_Navigation $Menu, $id ) {
  135. # Find Current
  136. $Item = $Menu->findBy('id', $id);
  137. # Check
  138. if ( !$Item ) {
  139. return false;
  140. }
  141. # Done
  142. return true;
  143. }
  144. /**
  145. * Activate a Navigation Menu Item
  146. * @return
  147. */
  148. public function activateNavigationMenuItem ( Zend_Navigation $Menu, $id, $parents = true ) {
  149. # Find Current
  150. $Item = $Menu->findBy('id', $id);
  151. # Check
  152. if ( !$Item ) {
  153. return false;
  154. }
  155. # Check Permission
  156. if ( !Bal_App::getView()->navigation()->accept($Item) ) {
  157. throw new Zend_Exception('Identity does not have permission to activate that menu item: '.$id);
  158. }
  159. # Active Current
  160. $Item->active = true;
  161. # Activate Parents
  162. if ( $parents ) {
  163. $tmpItem = $Item;
  164. while ( !empty($tmpItem->parent) ) {
  165. $tmpItem = $tmpItem->parent;
  166. $tmpItem->active = true;
  167. }
  168. }
  169. # Done
  170. return true;
  171. }
  172. # ========================
  173. # AUTHENTICATION
  174. /**
  175. * Logout the User
  176. * @param mixed $redirect
  177. */
  178. public function logout ( $redirect = true ) {
  179. # Logout
  180. $this->getApp()->logout();
  181. # Forward
  182. if ( $redirect ) $this->forwardOut($redirect);
  183. # Done
  184. return $this;
  185. }
  186. /**
  187. * Login the User and forward
  188. * @see forwardIn
  189. * @see forwardOut
  190. * @param string $username
  191. * @param string $password
  192. * @param string $locale
  193. * @param string $remember
  194. * @param bool $logged_out_forward
  195. * @param bool $logged_in_forward
  196. * @return bool
  197. */
  198. public function loginForward ( $username, $password, $locale = null, $remember = null, $logged_out_forward = false, $logged_in_forward = false ) {
  199. $this->getApp()->login($username, $password, $locale, $remember);
  200. return $this->authenticate($logged_out_forward, $logged_in_forward);
  201. }
  202. /**
  203. * Forward the Request
  204. * @param mixed $redirect
  205. */
  206. public function forward ($redirect) {
  207. $Redirector = $this->getActionController()->getHelper('Redirector');
  208. call_user_func_array(array($Redirector,'gotoRoute'), $redirect);
  209. return $this;
  210. }
  211. /**
  212. * Forward the Request if Logged In
  213. * @see forward
  214. * @param mixed $redirect
  215. */
  216. public function forwardIn ($redirect = true) {
  217. if ( $redirect === true ) $redirect = $this->getOption('logged_in_forward');
  218. return $this->forward($redirect);
  219. }
  220. /**
  221. * Forward the Request if Logged Out
  222. * @see forward
  223. * @param mixed $redirect
  224. */
  225. public function forwardOut ($redirect = true) {
  226. if ( $redirect === true ) $redirect = $this->getOption('logged_out_forward');
  227. return $this->forward($redirect);
  228. }
  229. /**
  230. * Authenticate and Forward if need be
  231. * @see forwardIn
  232. * @see forwardOut
  233. * @param bool $logged_out_forward
  234. * @param bool $logged_in_forward
  235. * @return bool
  236. */
  237. public function authenticate ($logged_out_forward = false, $logged_in_forward = false) {
  238. # Prepare
  239. $result = null;
  240. # Fetch
  241. $hasIdentity = $this->getApp()->hasIdentity();
  242. # Check Login Status
  243. if ( $hasIdentity ) {
  244. # Logged In
  245. # Forward
  246. if ( $logged_in_forward ) {
  247. $this->forwardIn($logged_in_forward);
  248. }
  249. # Done
  250. $result = true;
  251. }
  252. else {
  253. # Logged Out
  254. # Forward
  255. if ( $logged_out_forward ) {
  256. $this->forwardOut($logged_out_forward);
  257. }
  258. # Done
  259. $result = false;
  260. }
  261. # Done
  262. return $result;
  263. }
  264. /**
  265. * Returns @see Bal_Controller_Plugin_App
  266. */
  267. public function getApp(){
  268. return Bal_Controller_Plugin_App::getInstance();
  269. }
  270. /**
  271. * Magic
  272. * @return mixed
  273. */
  274. function __call ( $method, $args ) {
  275. $Parent = $this->getApp();
  276. if ( method_exists($Parent, $method) ) {
  277. return call_user_func_array(array($Parent, $method), $args);
  278. } else {
  279. throw new Zend_Exception('Could not find the method: '.$method);
  280. }
  281. return false;
  282. }
  283. # ========================
  284. # LOG
  285. public function prepareLog ( $store = null, $log_request = null ) {
  286. # Prepare
  287. $Log = Bal_App::getLog();
  288. if ( $store === null || $log_request === null ) {
  289. $xhr = $this->getActionControllerRequest()->isXmlHttpRequest();
  290. if ( !$xhr && !empty($_REQUEST['ajax']) ) $xhr = true;
  291. # Apply
  292. if ( $xhr ) {
  293. if ( $store === null ) $store = false;
  294. if ( $log_request === null ) $log_request = false;
  295. } else {
  296. if ( $store === null ) $store = $this->getConfig('log.store');
  297. if ( $log_request === null ) $log_request = $this->getConfig('log.request');
  298. }
  299. }
  300. # Enable Sore
  301. if ( $store ) {
  302. $Log->enableStore($store);
  303. }
  304. # Log Request Details
  305. if ( $log_request ) {
  306. global $_SESSION;
  307. $details = array(
  308. 'server' => $_SERVER,
  309. 'request' => array(
  310. 'get' => $_GET,
  311. 'post' => $_POST,
  312. 'files' => $_FILES,
  313. 'session' => $_SESSION,
  314. 'cookie' => $_COOKIE,
  315. 'params' => $_REQUEST,
  316. )
  317. );
  318. $Log->log('log-request_details', Bal_Log::DEBUG, array('details'=>$details));
  319. }
  320. # Chain
  321. return $this;
  322. }
  323. }