1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.diemeisterei.de/ |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015 diemeisterei GmbH, Stuttgart |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace app\modules\prototype\widgets; |
11
|
|
|
|
12
|
|
|
use yii\base\Widget; |
13
|
|
|
use yii\helpers\Html; |
14
|
|
|
|
15
|
|
|
class HtmlWidget extends Widget |
16
|
|
|
{ |
17
|
|
|
const SETTINGS_SECTION = 'app.html'; |
18
|
|
|
const ACCESS_ROLE = 'Editor'; |
19
|
|
|
|
20
|
|
|
public $key = null; |
21
|
|
|
public $enableFlash = false; |
22
|
|
|
|
23
|
|
|
public function run() |
24
|
|
|
{ |
25
|
|
|
$html = \app\modules\prototype\models\Html::findOne(['key' => $this->generateKey()]); |
26
|
|
|
|
27
|
|
|
if (\Yii::$app->user->can(self::ACCESS_ROLE)) { |
28
|
|
|
$link = ($html) ? $this->generateEditLink($html->id) : $this->generateCreateLink(); |
29
|
|
|
if ($this->enableFlash) { |
30
|
|
|
\Yii::$app->session->addFlash( |
31
|
|
|
($html) ? 'success' : 'info', |
32
|
|
|
"Edit contents in {$link}, key: <code>{$this->generateKey()}</code>" |
33
|
|
|
); |
34
|
|
|
} else { |
|
|
|
|
35
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $html ? $html->value : null; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function generateKey() |
43
|
|
|
{ |
44
|
|
|
if ($this->key) { |
45
|
|
|
return $this->key; |
46
|
|
|
} else { |
47
|
|
|
$key = \Yii::$app->request->getQueryParam('id'); |
48
|
|
|
} |
49
|
|
|
return \Yii::$app->language.'/'.\Yii::$app->controller->route.($key ? '/'.$key : ''); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function generateCreateLink() |
53
|
|
|
{ |
54
|
|
|
return Html::a('prototype module', ['/prototype/html/create', 'Html' => ['key' => $this->generateKey()]]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function generateEditLink($id) |
58
|
|
|
{ |
59
|
|
|
return Html::a('prototype module', ['/prototype/html/update', 'id' => $id]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.