Smaller cleanups in Content models

This commit is contained in:
Lucas Bartholemy 2017-01-21 12:20:07 +01:00
parent be6fc39ce9
commit 690b6b2d50
3 changed files with 29 additions and 13 deletions

View File

@ -2,7 +2,7 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
@ -94,4 +94,20 @@ class ActiveRecord extends \yii\db\ActiveRecord
return $this->_fileManager;
}
/**
* Returns the errors as string for all attribute or a single attribute.
*
* @since 1.2
* @param string $attribute attribute name. Use null to retrieve errors for all attributes.
* @return string the error message
*/
public function getErrorMessage($attribute = null)
{
$message = '';
foreach ($this->getErrors($attribute) as $attribute => $errors) {
$message .= $attribute . ': ' . implode(', ', $errors) . ', ';
}
return $message;
}
}

View File

@ -2,7 +2,7 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2016 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
@ -47,19 +47,18 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
/**
* @see \humhub\modules\content\widgets\WallEntry
* @var string WallEntry widget class
* @var string the WallEntry widget class
*/
public $wallEntryClass = "";
/**
* Defines if originator automatically follows this content when saved.
* @var type
* @var boolean should the originator automatically follows this content when saved.
*/
public $autoFollow = true;
/**
* The stream channel where this content should displayed.
* Set to null when this content should not appear on the stream.
* Set to null when this content should not appear on streams.
*
* @since 1.2
* @var string|null the stream channel
@ -81,7 +80,8 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
public function __get($name)
{
/**
* Ensure there is always a corresponding Content
* Ensure there is always a corresponding Content record
* @see Content
*/
if ($name == 'content') {
$content = parent::__get('content');
@ -98,7 +98,7 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
/**
* Returns the name of this type of content.
* You need to override this method in your content implementation.
*
*
* @return string the name of the content
*/
public function getContentName()
@ -171,13 +171,11 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
*/
public function beforeSave($insert)
{
if (!$this->content->validate()) {
throw new Exception("Could not validate associated Content Record! (" . print_r($this->content->getErrors(), 1) . ")");
throw new Exception("Could not validate associated Content record! (" . $this->content->getErrorMessage() . ")");
}
$this->content->setAttribute('stream_channel', $this->streamChannel, false);
return parent::beforeSave($insert);
}

View File

@ -2,7 +2,7 @@
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2016 HumHub GmbH & Co. KG
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
@ -152,8 +152,9 @@ class Content extends \humhub\components\ActiveRecord
*/
public function beforeSave($insert)
{
if ($this->object_model == "" || $this->object_id == "")
if ($this->object_model == "" || $this->object_id == "") {
throw new Exception("Could not save content with object_model or object_id!");
}
// Set some default values
@ -194,6 +195,7 @@ class Content extends \humhub\components\ActiveRecord
}
if ($insert && !$contentSource instanceof \humhub\modules\activity\models\Activity) {
$notifyUsers = array_merge($this->notifyUsersOfNewContent, Yii::$app->notification->getFollowers($this));
\humhub\modules\content\notifications\ContentCreated::instance()