From 340bede945c900d98f49bfedfc9b423f6f54b662 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Fri, 26 Aug 2016 14:23:30 +0200 Subject: [PATCH] allow customization of Stream action via events added before and after action events that can be used to modify the stream action filter. --- .../content/components/actions/Stream.php | 50 +++++++++++++++---- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/protected/humhub/modules/content/components/actions/Stream.php b/protected/humhub/modules/content/components/actions/Stream.php index ab46df5728..915b0680d3 100644 --- a/protected/humhub/modules/content/components/actions/Stream.php +++ b/protected/humhub/modules/content/components/actions/Stream.php @@ -10,9 +10,9 @@ namespace humhub\modules\content\components\actions; use Yii; use humhub\modules\content\models\Content; -use humhub\modules\content\models\Wall; use humhub\modules\content\models\WallEntry; use humhub\modules\user\models\User; +use yii\base\ActionEvent; use yii\base\Exception; /** @@ -23,6 +23,16 @@ use yii\base\Exception; */ class Stream extends \yii\base\Action { + /** + * @event ActionEvent Event triggered before this action is run. + * This can be used for example to customize [[activeQuery]] before it gets executed. + */ + const EVENT_BEFORE_RUN = 'beforeRun'; + /** + * @event ActionEvent Event triggered after this action is run. + */ + const EVENT_AFTER_RUN = 'afterRun'; + /** * Constants used for sorting @@ -71,7 +81,7 @@ class Stream extends \yii\base\Action * * @var array */ - public $filters = array(); + public $filters = []; /** * @var \yii\db\ActiveQuery @@ -84,7 +94,8 @@ class Stream extends \yii\base\Action * * @var User */ - public $user = null; + public $user; + public function init() { @@ -92,7 +103,7 @@ class Stream extends \yii\base\Action $this->activeQuery = WallEntry::find(); // If no user is set, take current if logged in - if (!Yii::$app->user->isGuest && $this->user == null) { + if ($this->user === null && !Yii::$app->user->isGuest) { $this->user = Yii::$app->user->getIdentity(); } @@ -139,7 +150,6 @@ class Stream extends \yii\base\Action $this->activeQuery->limit($this->limit); $this->activeQuery->andWhere(['user.status' => User::STATUS_ENABLED]); - $this->activeQuery->one(); /** * Handle Stream Mode (Normal Stream or Activity Stream) @@ -148,7 +158,7 @@ class Stream extends \yii\base\Action $this->activeQuery->andWhere(['content.object_model' => \humhub\modules\activity\models\Activity::className()]); // Dont show own activities - if ($this->user != null) { + if ($this->user !== null) { $this->activeQuery->leftJoin('activity', 'content.object_id=activity.id AND content.object_model=:activityModel', ['activityModel' => \humhub\modules\activity\models\Activity::className()]); $this->activeQuery->andWhere('content.created_by != :userId', array(':userId' => $this->user->id)); } @@ -229,11 +239,10 @@ class Stream extends \yii\base\Action { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; - $this->init(); - $wallEntries = $this->activeQuery->all(); + $wallEntries = $this->getWallEntries(); $output = ""; - $generatedWallEntryIds = array(); + $generatedWallEntryIds = []; $lastEntryId = ""; foreach ($wallEntries as $wallEntry) { @@ -265,4 +274,27 @@ class Stream extends \yii\base\Action ]; } + /** + * This method is called right before `run()` is executed. + * You may override this method to do preparation work for the action run. + * If the method returns false, it will cancel the action. + * + * @return boolean whether to run the action. + */ + protected function beforeRun() + { + $event = new ActionEvent($this); + $this->trigger(self::EVENT_BEFORE_RUN, $event); + return $event->isValid; + } + + /** + * This method is called right after `run()` is executed. + * You may override this method to do post-processing work for the action run. + */ + protected function afterRun() + { + $event = new ActionEvent($this); + $this->trigger(self::EVENT_AFTER_RUN, $event); + } }