ZAction.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * summary: 自定义action事件,对action复用
  4. * author: justin
  5. * date: 2014-4-2
  6. */
  7. class ZAction extends CAction
  8. {
  9. public $behaviors = array();
  10. protected $output = 'json';
  11. protected $attach_behaviors = false;
  12. public function onBeforeRun($event){
  13. $this->raiseEvent('onBeforeRun',$event);
  14. }
  15. public function onAfterRun($event){
  16. $this->raiseEvent('onAfterRun',$event);
  17. }
  18. protected function beforeRun($params = array()){
  19. $controller = $this->getController();
  20. $params['controller'] = $controller;
  21. if (!empty($this->behaviors) && !$this->attach_behaviors){
  22. $this->attach_behaviors = true;
  23. $this->attachBehaviors($this->behaviors);
  24. }
  25. if ($this->hasEventHandler('onBeforeRun')){
  26. $event = new ZActionEvent($this, $params);
  27. $this->onBeforeRun($event);
  28. if (!$event->success){
  29. switch($this->output){
  30. case 'json':
  31. CommonFn::requestAjax($event->success, $event->message);
  32. break;
  33. default:
  34. break;
  35. }
  36. }
  37. return array('success' => $event->success, 'message' => $event->message, 'data' => $event->data);
  38. } else {
  39. return array('success' => true, 'message' => '', 'data' => array());
  40. }
  41. }
  42. protected function afterRun($params = array()){
  43. $controller = $this->getController();
  44. $params['controller'] = $controller;
  45. if (!empty($this->behaviors) && !$this->attach_behaviors){
  46. $this->attach_behaviors = true;
  47. $this->attachBehaviors($this->behaviors);
  48. }
  49. if ($this->hasEventHandler('onAfterRun')){
  50. $event = new ZActionEvent($this, $params);
  51. $this->onAfterRun($event);
  52. if (!$event->success){
  53. switch($this->output){
  54. case 'json':
  55. CommonFn::requestAjax($event->success, $event->message);
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. return array('success' => $event->success, 'message' => $event->message, 'data' => $event->data);
  62. } else {
  63. return array('success' => true, 'message' => '', 'data' => array());
  64. }
  65. }
  66. }