Skip to content

Commit

Permalink
Fixed #1797 - Save user mailing settings
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh4 committed Jun 14, 2016
1 parent 698c88b commit 0e0e8c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
29 changes: 4 additions & 25 deletions protected/humhub/modules/user/controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,37 +267,16 @@ public function actionDelete()
*/
public function actionEmailing()
{
$user = Yii::$app->user->getIdentity();
$model = new \humhub\modules\user\models\forms\AccountEmailing();

$model->receive_email_activities = Yii::$app->getModule('activity')->settings->contentContainer($user)->get('receive_email_activities');
if ($model->receive_email_activities === null) {
// Use site default value
$model->receive_email_activities = Yii::$app->getModule('activity')->settings->get('receive_email_activities');
}

$model->receive_email_notifications = Yii::$app->getModule('notification')->settings->contentContainer($user)->get('receive_email_notifications');
if ($model->receive_email_notifications === null) {
// Use site default value
$model->receive_email_notifications = Yii::$app->getModule('notification')->settings->get('receive_email_notifications');
}

$model->enable_html5_desktop_notifications = Yii::$app->getModule('notification')->settings->contentContainer($user)->get('enable_html5_desktop_notifications');
if ($model->enable_html5_desktop_notifications === null) {
// Use site default value
$model->enable_html5_desktop_notifications = Yii::$app->getModule('notification')->settings->get('enable_html5_desktop_notifications');
}

if ($model->load(Yii::$app->request->post()) && $model->validate()) {
Yii::$app->getModule('activity')->settings->contentContainer($user)->get('receive_email_activities', $model->receive_email_activities);
Yii::$app->getModule('notification')->settings->contentContainer($user)->get('receive_email_notifications', $model->receive_email_notifications);
Yii::$app->getModule('notification')->settings->contentContainer($user)->get('enable_html5_desktop_notifications', $model->enable_html5_desktop_notifications);


if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
Yii::$app->getSession()->setFlash('data-saved', Yii::t('UserModule.controllers_AccountController', 'Saved'));
}

return $this->render('emailing', array('model' => $model));
}


/**
* Change Current Password
*
Expand Down
58 changes: 55 additions & 3 deletions protected/humhub/modules/user/models/forms/AccountEmailing.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
class AccountEmailing extends \yii\base\Model
{

public $user;
public $receive_email_activities;
public $receive_email_notifications;
public $enable_html5_desktop_notifications;
Expand All @@ -52,19 +53,70 @@ public function rules()
array('enable_html5_desktop_notifications', 'in', 'range' => array('0', '1')),
);
}

/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
return [
'receive_email_notifications' => Yii::t('UserModule.forms_AccountEmailingForm', 'Send notifications?'),
'receive_email_activities' => Yii::t('UserModule.forms_AccountEmailingForm', 'Send activities?'),
'enable_html5_desktop_notifications' => Yii::t('UserModule.views_account_emailing', 'Receive desktop notifications when you are online.')
);
];
}

/**
* @inheritdoc
*/
public function init()
{
$this->user = Yii::$app->user->getIdentity();

$activityModule = Yii::$app->getModule('activity');
$notificationModule = Yii::$app->getModule('notification');

// Initialize form values
$this->receive_email_activities = $this->getSettingValue($activityModule, 'receive_email_activities');
$this->receive_email_notifications = $this->getSettingValue($notificationModule, 'receive_email_notifications');
$this->enable_html5_desktop_notifications = $this->getSettingValue($notificationModule, 'enable_html5_desktop_notifications');
}

/**
* Retrieves the setting of the given $module for the given $settingKey.
* If existing, this function will return the user specific setting else
* the default site setting.
*
* @param Module $module Module object
* @param string $settingKey Setting key value
* @return boolean
*/
private function getSettingValue($module, $settingKey)
{

$result = $module->settings->contentContainer($this->user)->get($settingKey);
if ($result === null) {
// Use site default value
$result = $module->settings->get($settingKey);
}

return $result;
}

/**
* Saves the given email settings
*/
public function save()
{
$activityModule = Yii::$app->getModule('activity');
$notificationModule = Yii::$app->getModule('notification');

$activityModule->settings->contentContainer($this->user)->set('receive_email_activities', $this->receive_email_activities);
$notificationModule->settings->contentContainer($this->user)->set('receive_email_notifications', $this->receive_email_notifications);
$notificationModule->settings->contentContainer($this->user)->set('enable_html5_desktop_notifications', $this->enable_html5_desktop_notifications);
return true;
}

}

0 comments on commit 0e0e8c1

Please sign in to comment.