setTemplate('unsubscribe', 'users'); $this->setTitle(_t('site', 'Unsubscribe Page')); } public function data() { $data = parent::data(); $user = $this->getUserByHash(); # Unsubscribe if ($user && ! $this->request->isRefresh()) { $this->unsubscribe($user['user_id'], $user['enotify']); } # Step $data['step'] = ($user ? 'subscribe' : 'error'); return $data; } public function getUserByHash($hash = null) { $hash = Users::userHashValidate( $hash ?? $this->request->get('h', TYPE_STR, '') ); $user = Users::model()->userData($hash['user_id'], ['user_id', 'enotify']); if (empty($user)) { return false; } $user['enotify'] = intval($user['enotify']); return $user; } public function onSubmitAction() { $response = []; do { $user = $this->getUserByHash(); if (!$user || !$this->security->validateReferer()) { $this->errors->reloadPage(); break; } # Resubscribe $this->unsubscribe($user['user_id'], $user['enotify'], true); $response['title'] = _t('users', 'Thank you!'); $response['message'] = _t('users', 'You have successfully subscribed'); $response['message'] .= '
'; $response['message'] .= _t('users', 'We promise not to spam and write only on business!'); } while (false); return $this->ajaxResponseForm($response); } /** * Unsubscribe/resubscribe * @param int $userId * @param int $enotify * @param bool $resubscribe * @return bool */ public function unsubscribe($userId, $enotify, $resubscribe = false) { if ($resubscribe) { # Подписываем на рассылку if (! ($enotify & $this->notificationsGroup)) { $enotify += $this->notificationsGroup; Users::model()->userSave($userId, ['enotify' => $enotify]); } } else { if ($enotify & $this->notificationsGroup) { # Отписываем от рассылки $enotify -= $this->notificationsGroup; Users::model()->userSave($userId, ['enotify' => $enotify]); } } return true; } public function seo() { $this->seo->index(false, 'users unsubscribe page'); $this->seoApply(); } public function seoSettings() { } }