1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
8
|
|
|
* @link https://vistart.me/ |
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
10
|
|
|
* @license https://vistart.me/license/ |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace rhosocial\organization\web\organization\controllers\my; |
14
|
|
|
|
15
|
|
|
use rhosocial\organization\exceptions\UnauthorizedManageProfileException; |
16
|
|
|
use rhosocial\organization\forms\SettingsForm; |
17
|
|
|
use rhosocial\organization\rbac\permissions\ManageProfile; |
18
|
|
|
use rhosocial\organization\web\organization\Module; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\base\Action; |
21
|
|
|
use yii\bootstrap\ActiveForm; |
22
|
|
|
use yii\web\Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class SettingsAction |
26
|
|
|
* @package rhosocial\organization\web\organization\controllers\my |
27
|
|
|
* @version 1.0 |
28
|
|
|
* @author vistart <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class SettingsAction extends Action |
31
|
|
|
{ |
32
|
|
|
public $updateSuccessMessage; |
33
|
|
|
public $updateFailedMessage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Initialize messages. |
37
|
|
|
*/ |
38
|
|
|
protected function initMessages() |
39
|
|
|
{ |
40
|
|
|
if (!is_string($this->updateSuccessMessage)) { |
41
|
|
|
$this->updateSuccessMessage = Yii::t('user' ,'Updated.'); |
42
|
|
|
} |
43
|
|
|
if (!is_string($this->updateFailedMessage)) { |
44
|
|
|
$this->updateFailedMessage = Yii::t('user', 'Failed to Update.'); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function init() |
49
|
|
|
{ |
50
|
|
|
$this->initMessages(); |
51
|
|
|
parent::init(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check access. |
56
|
|
|
* @param $org |
57
|
|
|
* @param $user |
58
|
|
|
* @return bool |
59
|
|
|
* @throws UnauthorizedManageProfileException |
60
|
|
|
*/ |
61
|
|
|
public static function checkAccess($org, $user) |
62
|
|
|
{ |
63
|
|
|
MemberAction::checkAccess($org, $user); |
64
|
|
|
if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageProfile)->name, ['organization' => $org])) { |
65
|
|
|
throw new UnauthorizedManageProfileException(); |
66
|
|
|
} |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function run($id) |
74
|
|
|
{ |
75
|
|
|
$organization = Module::getOrganization($id); |
76
|
|
|
$user = Yii::$app->user->identity; |
77
|
|
|
static::checkAccess($organization, $user); |
78
|
|
|
$model = new SettingsForm([ |
79
|
|
|
'organization' => $organization, |
80
|
|
|
]); |
81
|
|
|
if (Yii::$app->request->isAjax) { |
82
|
|
|
if ($model->load(Yii::$app->request->post())) { |
83
|
|
|
Yii::$app->response->format = Response::FORMAT_JSON; |
84
|
|
|
return ActiveForm::validate($model); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) { |
89
|
|
|
if ($model->validate() && $model->submit()) { |
90
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS); |
91
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $organization->profile->name . ' ' . $organization->getID() . ') ' . $this->updateSuccessMessage); |
|
|
|
|
92
|
|
|
return $this->controller->redirect(['settings', 'id' => $organization->getID()]); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED); |
95
|
|
|
Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $organization->profile->name . ' ' . $organization->getID() . ') ' . $this->updateFailedMessage); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
return $this->controller->render('settings', [ |
98
|
|
|
'organization' => $organization, |
99
|
|
|
'model' => $model |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.