Skip to content

Commit 36953b8

Browse files
authored
Merge pull request #9 from dmstr/feature/context-menu-interface
Implemented context menu interface
2 parents 1b6fe9f + 3ed4ec0 commit 36953b8

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/widgets/HtmlWidget.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace dmstr\modules\prototype\widgets;
1111

12+
use dmstr\modules\backend\interfaces\ContextMenuItemsInterface;
1213
use rmrevin\yii\fontawesome\FA;
1314
use yii\base\Event;
1415
use yii\base\Widget;
@@ -25,12 +26,12 @@
2526
* @property bool $renderEmpty
2627
* @property Html $_model
2728
*/
28-
class HtmlWidget extends Widget
29+
class HtmlWidget extends Widget implements ContextMenuItemsInterface
2930
{
3031
const SETTINGS_SECTION = 'app.html';
3132
const ACCESS_ROLE = 'Editor';
3233

33-
public $key = null;
34+
public $key;
3435
public $enableFlash = false;
3536
public $moduleId = 'prototype';
3637
public $registerMenuItems = true;
@@ -47,6 +48,9 @@ public function init()
4748
}
4849
}
4950

51+
/**
52+
* @return string
53+
*/
5054
public function run()
5155
{
5256
$this->_model = $model = HtmlModel::findOne(['key' => $this->generateKey()]);
@@ -73,6 +77,9 @@ public function run()
7377
return $html;
7478
}
7579

80+
/**
81+
* @return array
82+
*/
7683
public function getMenuItems()
7784
{
7885
return [
@@ -84,6 +91,9 @@ public function getMenuItems()
8491
];
8592
}
8693

94+
/**
95+
* @return null|string
96+
*/
8797
private function generateKey()
8898
{
8999
if ($this->key) {
@@ -94,28 +104,47 @@ private function generateKey()
94104
return \Yii::$app->language.'/'.\Yii::$app->controller->route.($key ? '/'.$key : '');
95105
}
96106

107+
/**
108+
* @return string
109+
*/
97110
private function generateCreateLink()
98111
{
99112

100113
return Html::a(FA::icon(FA::_PLUS_SQUARE) . ' HTML',
101114
['/' . $this->moduleId . '/html/create', 'Html' => ['key' => $this->generateKey()]]);
102115
}
103116

117+
/**
118+
* @param $id
119+
*
120+
* @return string
121+
*/
104122
private function generateEditLink($id)
105123
{
106124
return Html::a($this->moduleId . ' module', ['/' . $this->moduleId . '/html/update', 'id' => $id]);
107125
}
108126

127+
/**
128+
* @return array
129+
*/
109130
private function generateCreateRoute()
110131
{
111132
return ['/' . $this->moduleId . '/html/create', 'Html' => ['key' => $this->generateKey()]];
112133
}
113134

135+
/**
136+
* @param $id
137+
*
138+
* @return array
139+
*/
114140
private function generateEditRoute($id)
115141
{
116142
return ['/' . $this->moduleId . '/html/update', 'id' => $id];
117143
}
118144

145+
/**
146+
* @return string
147+
*/
119148
private function renderEmpty()
120149
{
121150
return '<div class="alert alert-info">'.$this->generateCreateLink().'</div>';

src/widgets/TwigWidget.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace dmstr\modules\prototype\widgets;
1212

1313
use dmstr\db\traits\ActiveRecordAccessTrait;
14+
use dmstr\modules\backend\interfaces\ContextMenuItemsInterface;
1415
use dmstr\modules\prototype\models\Twig;
1516
use rmrevin\yii\fontawesome\FA;
1617
use yii\base\Event;
@@ -29,20 +30,20 @@
2930
* @property bool $localized
3031
* @property string $queryParam
3132
*/
32-
class TwigWidget extends Widget
33+
class TwigWidget extends Widget implements ContextMenuItemsInterface
3334
{
3435
const SETTINGS_SECTION = 'app.html';
3536
const ACCESS_ROLE = 'prototype_twig';
3637
const TEMP_ALIAS = '@runtime/TwigWidget';
3738

3839
public $moduleId = 'prototype';
3940
public $queryParam = 'pageId';
40-
public $key = null;
41+
public $key;
4142
public $localized = true;
4243
public $enableFlash = false;
4344
public $registerMenuItems = true;
4445
public $renderEmpty = true;
45-
public $position = null;
46+
public $position;
4647
public $params = [];
4748

4849
private $_model;
@@ -57,6 +58,9 @@ public function init()
5758
}
5859
}
5960

61+
/**
62+
* @return string
63+
*/
6064
public function run()
6165
{
6266
Url::remember('', $this->generateKey());
@@ -102,6 +106,9 @@ public function run()
102106
}
103107

104108

109+
/**
110+
* @return array
111+
*/
105112
public function getMenuItems()
106113
{
107114
return [
@@ -118,6 +125,7 @@ public function getMenuItems()
118125

119126
/**
120127
* Generates a key based on the current route/queryParam
128+
*
121129
* @return null|string
122130
*/
123131
private function generateKey()
@@ -136,23 +144,37 @@ private function generateKey()
136144
($this->position ? '#' . $this->position : '');
137145
}
138146

147+
/**
148+
* @return string
149+
*/
139150
private function generateCreateLink()
140151
{
141152

142153
return Html::a(FA::icon(FA::_PLUS_SQUARE) . ' ' . $this->generateKey() . ' Twig',
143154
['/' . $this->moduleId . '/twig/create', 'Twig' => ['key' => $this->generateKey()]]);
144155
}
145156

157+
/**
158+
* @return array
159+
*/
146160
private function generateCreateRoute()
147161
{
148162
return ['/' . $this->moduleId . '/twig/create', 'Twig' => ['key' => $this->generateKey()]];
149163
}
150164

165+
/**
166+
* @param $id
167+
*
168+
* @return array
169+
*/
151170
private function generateEditRoute($id)
152171
{
153172
return ['/' . $this->moduleId . '/twig/update', 'id' => $id];
154173
}
155174

175+
/**
176+
* @return string
177+
*/
156178
private function renderEmpty()
157179
{
158180
return '<div class="alert alert-info">' . $this->generateCreateLink() . '</div>';

0 commit comments

Comments
 (0)