SocialActivity render method cleanup

This commit is contained in:
Lucas Bartholemy 2016-05-14 15:10:33 +02:00
parent 824e1afc2b
commit 5ecbe7636a
5 changed files with 110 additions and 71 deletions

View File

@ -27,25 +27,26 @@ use humhub\modules\content\components\ContentAddonActiveRecord;
*/
abstract class SocialActivity extends \yii\base\Component implements \yii\base\ViewContextInterface
{
const OUTPUT_WEB = 'web';
const OUTPUT_MAIL = 'mail';
const OUTPUT_MAIL_PLAINTEXT = 'mail_plaintext';
const OUTPUT_TEXT = 'text';
/**
* User which performed the activity.
*
* @var \humhub\modules\user\models\User
*/
public $originator;
/**
* The source instance which created this activity
*
* @var \yii\db\ActiveRecord
*/
public $source;
/**
* The content container this activity belongs to.
*
@ -55,13 +56,13 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
* @var ContentContainerActiveRecord
*/
public $container = null;
/**
* @var string the module id which this activity belongs to (required)
*/
public $moduleId = "";
/**
/**
* The notification record this notification belongs to
*
* @var Notification
@ -74,39 +75,44 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
* @var string
*/
public $viewName = null;
/**
* Layout file for web version
*
* @var string
*/
protected $layoutWeb;
/**
* Layout file for mail version
*
* @var string
*/
protected $layoutMail;
/**
* Layout file for mail plaintext version
*
* @var string
*/
protected $layoutMailPlaintext;
/**
* Assambles all parameter required for rendering the view.
*
* @return array all view parameter
*/
protected function getViewParams()
protected function getViewParams($params = [])
{
return [];
$params['originator'] = $this->originator;
$params['source'] = $this->source;
$params['contentContainer'] = $this->container;
$params['record'] = $this->record;
$params['url'] = $this->getUrl();
return $params;
}
/**
* Renders the notification
*
@ -114,31 +120,61 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
*/
public function render($mode = self::OUTPUT_WEB, $params = [])
{
$params['originator'] = $this->originator;
$params['source'] = $this->source;
$params['contentContainer'] = $this->container;
$params['record'] = $this->record;
$params['url'] = $this->getUrl();
$params = array_merge($params, $this->getViewParams());
$viewFile = $this->getViewPath() . '/' . $this->viewName . '.php';
$viewFile = $this->getViewFile($mode);
$viewParams = $this->getViewParams($params);
// Switch to extra mail view file - if exists (otherwise use web view)
if ($mode == self::OUTPUT_MAIL || $mode == self::OUTPUT_MAIL_PLAINTEXT) {
$viewMailFile = $this->getViewPath() . '/mail/' . ($mode == self::OUTPUT_MAIL_PLAINTEXT ? 'plaintext/' : '') . $this->viewName . '.php';
if (file_exists($viewMailFile)) {
$viewFile = $viewMailFile;
}
} elseif ($mode == self::OUTPUT_TEXT) {
$html = Yii::$app->getView()->renderFile($viewFile, $params, $this);
return strip_tags($html);
$result = Yii::$app->getView()->renderFile($viewFile, $viewParams, $this);
if ($mode == self::OUTPUT_TEXT) {
return strip_tags($result);
}
$params['content'] = Yii::$app->getView()->renderFile($viewFile, $params, $this);
return Yii::$app->getView()->renderFile(($mode == self::OUTPUT_WEB) ? $this->layoutWeb : ($mode == self::OUTPUT_MAIL_PLAINTEXT ? $this->layoutMailPlaintext : $this->layoutMail), $params, $this);
$viewParams['content'] = $result;
return Yii::$app->getView()->renderFile($this->getLayoutFile($mode), $viewParams, $this);
}
/**
* Returns the correct view file
*
* @param string $mode the output mode
* @return string the view file
*/
protected function getViewFile($mode)
{
$viewFile = $this->getViewPath() . '/' . $this->viewName . '.php';
$alternativeViewFile = "";
// Lookup alternative view file based on view mode
if ($mode == self::OUTPUT_MAIL) {
$alternativeViewFile = $this->getViewPath() . '/mail/' . $this->viewName . '.php';
} elseif ($mode === self::OUTPUT_MAIL_PLAINTEXT) {
$alternativeViewFile = $this->getViewPath() . '/mail/plaintext/' . $this->viewName . '.php';
}
if ($alternativeViewFile != "" && file_exists($alternativeViewFile)) {
$viewFile = $alternativeViewFile;
}
return $viewFile;
}
/**
* Returns the layout file
*
* @param string $mode the output mode
* @return string the layout file
*/
protected function getLayoutFile($mode)
{
if ($mode == self::OUTPUT_MAIL_PLAINTEXT) {
return $this->layoutMailPlaintext;
} elseif ($mode == self::OUTPUT_MAIL) {
return $this->layoutMail;
}
return $this->layoutWeb;
}
/**
* Returns the directory containing the view files for this event.
* The default implementation returns the 'views' subdirectory under the directory containing the notification class file.
@ -149,7 +185,7 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
$class = new \ReflectionClass($this);
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
}
/**
* Url of the origin of this notification
* If source is a Content / ContentAddon / ContentContainer this will automatically generated.
@ -173,7 +209,7 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
return $url;
}
/**
* Build info text about a content
*
@ -189,4 +225,5 @@ abstract class SocialActivity extends \yii\base\Component implements \yii\base\V
' "' .
\humhub\widgets\RichText::widget(['text' => $content->getContentDescription(), 'minimal' => true, 'maxLength' => 60]) . '"';
}
}

View File

@ -9,7 +9,6 @@
namespace humhub\modules\activity;
use Yii;
use humhub\models\Setting;
use humhub\modules\user\models\User;
use humhub\modules\content\components\MailUpdateSender;

View File

@ -62,9 +62,14 @@ abstract class BaseActivity extends \humhub\components\SocialActivity
parent::init();
}
public function getViewParams()
/**
* @inheritdoc
*/
public function getViewParams($params = [])
{
return ['clickable' => $this->clickable];
$params['clickable'] = $this->clickable;
return parent::getViewParams($params);
}
/**
@ -81,18 +86,18 @@ abstract class BaseActivity extends \humhub\components\SocialActivity
if (!$this->source instanceof \yii\db\ActiveRecord) {
throw new \yii\base\InvalidConfigException("Invalid source object given!");
}
if ($this->container == null) {
$this->container = $this->getContentContainerFromSource();
if($this->container == null) {
if ($this->container == null) {
throw new \yii\base\InvalidConfigException("Could not determine content container for activity!");
}
}
$this->saveModelInstance();
}
protected function getContentContainerFromSource()
{
if ($this->hasContentSource()) {
@ -101,13 +106,12 @@ abstract class BaseActivity extends \humhub\components\SocialActivity
return $this->source;
}
}
protected function hasContentSource()
{
return $this->source instanceof ContentActiveRecord
|| $this->source instanceof ContentAddonActiveRecord;
return $this->source instanceof ContentActiveRecord || $this->source instanceof ContentAddonActiveRecord;
}
private function saveModelInstance()
{
$model = new Activity();
@ -119,20 +123,20 @@ abstract class BaseActivity extends \humhub\components\SocialActivity
$model->content->visibility = $this->getContentVisibility();
$model->content->created_by = $this->getOriginatorId();
if($model->content->created_by == null) {
if ($model->content->created_by == null) {
throw new \yii\base\InvalidConfigException("Could not determine originator for activity!");
}
if (!$model->validate() || !$model->save()) {
throw new \yii\base\Exception("Could not save activity!" . $model->getErrors());
}
}
protected function getContentVisibility()
{
return $this->hasContentSource() ? $this->source->content->visibility : $this->visibility;
}
protected function getOriginatorId()
{
if ($this->originator !== null) {
@ -141,6 +145,7 @@ abstract class BaseActivity extends \humhub\components\SocialActivity
return $this->source->content->created_by;
} elseif ($this->source instanceof ContentAddonActiveRecord) {
return $this->source->created_by;
}
}
}
}

View File

@ -8,9 +8,9 @@
namespace humhub\modules\notification;
use Yii;
use humhub\modules\user\models\User;
use humhub\modules\notification\models\Notification;
use humhub\models\Setting;
use humhub\modules\content\components\MailUpdateSender;
/**
@ -32,7 +32,7 @@ class Module extends \humhub\components\Module
{
$notifications = [];
$receive_email_notifications = Yii::$app->getModule('notification')->contentContainer($user)->settings->get('receive_email_notifications');
$receive_email_notifications = Yii::$app->getModule('notification')->settings->contentContainer($user)->get('receive_email_notifications');
if ($receive_email_notifications === null) {
// Use Default
$receive_email_notifications = Yii::$app->getModule('notification')->settings->get('receive_email_notifications');

View File

@ -8,7 +8,6 @@
namespace humhub\modules\notification\components;
use yii\helpers\Url;
use humhub\modules\notification\models\Notification;
use humhub\modules\user\models\User;
@ -22,6 +21,7 @@ use humhub\modules\content\components\ContentAddonActiveRecord;
*/
abstract class BaseNotification extends \humhub\components\SocialActivity
{
/**
* Space this notification belongs to. (Optional)
* If source is a Content, ContentAddon or ContentContainer this will be
@ -45,7 +45,7 @@ abstract class BaseNotification extends \humhub\components\SocialActivity
*/
protected $layoutMail = "@humhub/modules/notification/views/layouts/mail.php";
/**
/**
* Layout file for mail plaintext version
*
* @var string
@ -56,19 +56,17 @@ abstract class BaseNotification extends \humhub\components\SocialActivity
* @var boolean automatically mark notification as seen after click on it
*/
public $markAsSeenOnClick = true;
/**
* Renders the notification
*
* @return string
* @inheritdoc
*/
public function getViewParams()
public function getViewParams($params = [])
{
return [
'url' => Url::to(['/notification/entry', 'id' => $this->record->id], true),
'space' => $this->space,
'isNew' => ($this->record->seen != 1)
];
$params['url'] = Url::to(['/notification/entry', 'id' => $this->record->id], true);
$params['space'] = $this->space;
$params['isNew'] = ($this->record->seen != 1);
return parent::getViewParams($params);
}
/**
@ -164,8 +162,8 @@ abstract class BaseNotification extends \humhub\components\SocialActivity
$this->record->seen = 1;
$this->record->save();
}
/**
/**
* Should be overwritten by subclasses. This method provides a user friendly
* title for the different notification types.
* @return type
@ -174,5 +172,5 @@ abstract class BaseNotification extends \humhub\components\SocialActivity
{
return null;
}
}