@ -23,257 +23,233 @@ use Yii;
class MenuController extends Controller
class MenuController extends Controller
{
{
public $menu_service;
public $menu_service;
public $menu_item_service;
public $menu_item_service;
public function __construct( string $id, $module, MenuManageService $menu_service, MenuItemManageService $menu_item_service, array $config = [] ) {
public function __construct(
parent::__construct( $id, $module, $config );
string $id,
$module,
$this->menu_service = $menu_service;
MenuManageService $menu_service,
$this->menu_item_service = $menu_item_service;
MenuItemManageService $menu_item_service,
}
array $config = []
) {
public function behaviors(): array
parent::__construct($id, $module, $config);
{
return [
$this->menu_service = $menu_service;
'access' => [
$this->menu_item_service = $menu_item_service;
'class' => AccessControl::class,
}
'rules' => [
[
public function behaviors(): array
'allow' => true,
{
'roles' => ['MenuManagement'],
return [
],
'access' => [
[ // all the action are accessible to admin
'class' => AccessControl::class,
'allow' => true,
'rules' => [
'roles' => ['admin'],
[
],
'allow' => true,
],
'roles' => ['MenuManagement'],
],
],
'verbs' => [
[ // all the action are accessible to admin
'class' => VerbFilter::class,
'allow' => true,
'actions' => [
'roles' => ['admin'],
'delete' => ['POST'],
],
'delete-menu-item' => ['POST'],
],
],
],
],
'verbs' => [
];
'class' => VerbFilter::class,
}
'actions' => [
'delete' => ['POST'],
public function actionIndex($id = null)
'delete-menu-item' => ['POST'],
{
],
$menus = []; // menu list
],
$menu_records = Menu::find()->all();
];
foreach ($menu_records as $menu_record) {
}
$menus[$menu_record->id] = isset($menu_record->translation) & & $menu_record->translation->name ? $menu_record->translation->name : $menu_record->findTranslation(Yii::$app->params['defaultLanguage'])->name;
}
public function actionIndex($id = null)
{
$form = new MenuSelectForm();
$menus = []; // menu list
if ($form->load(Yii::$app->request->get()) & & $form->validate()) {
$menu_records = Menu::find()->all();
return $this->redirect(['menu/index', 'id' => $form->id]);
foreach ($menu_records as $menu_record) {
}
$menus[$menu_record->id] = isset($menu_record->translation) & & $menu_record->translation->name ? $menu_record->translation->name : $menu_record->findTranslation(Yii::$app->params['defaultLanguage'])->name;
elseif ($id) {
}
$this->createMenuItem(); // create menu item if MenuItemForm sent
$form = new MenuSelectForm();
$menu = $this->findModel($id);
if ($form->load(Yii::$app->request->get()) & & $form->validate()) {
return $this->redirect(['menu/index', 'id' => $form->id]);
$creatorWidgets = $this->getCreatorWidgets($menu->id);
} elseif ($id) {
$this->createMenuItem(); // create menu item if MenuItemForm sent
return $this->render('menu', [
'model' => $form,
$menu = $this->findModel($id);
'menus' => $menus,
'menu' => $menu,
$creatorWidgets = $this->getCreatorWidgets($menu->id);
'creator' => $creatorWidgets,
]);
return $this->render('menu', [
}
'model' => $form,
else {
'menus' => $menus,
return $this->render('select_menu', [
'menu' => $menu,
'model' => $form,
'creator' => $creatorWidgets,
'menus' => $menus,
]);
]);
} else {
}
return $this->render('select_menu', [
}
'model' => $form,
'menus' => $menus,
public function actionCreate()
]);
{
}
$form = new MenuForm();
}
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
try {
public function actionCreate()
$menu = $this->menu_service->create($form);
{
return $this->redirect(['index', 'id' => $menu->id]);
$form = new MenuForm();
} catch (\DomainException $e) {
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
Yii::$app->errorHandler->logException($e);
try {
Yii::$app->session->setFlash('error', $e->getMessage());
$menu = $this->menu_service->create($form);
}
}
return $this->redirect(['index', 'id' => $menu->id]);
return $this->render('create', [
} catch (\DomainException $e) {
'model' => $form,
Yii::$app->errorHandler->logException($e);
]);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
}
public function actionUpdate($id)
{
return $this->render('create', [
$menu = $this->findModel($id);
'model' => $form,
]);
$form = new MenuForm($menu);
}
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
try {
public function actionUpdate($id)
$this->menu_service->edit($menu->id, $form);
{
return $this->redirect(['index', 'id' => $menu->id]);
$menu = $this->findModel($id);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
$form = new MenuForm($menu);
Yii::$app->session->setFlash('error', $e->getMessage());
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
}
try {
}
$this->menu_service->edit($menu->id, $form);
return $this->render('update', [
'model' => $form,
return $this->redirect(['index', 'id' => $menu->id]);
'menu' => $menu,
} catch (\DomainException $e) {
]);
Yii::$app->errorHandler->logException($e);
}
Yii::$app->session->setFlash('error', $e->getMessage());
}
public function actionDelete($id)
}
{
try {
return $this->render('update', [
$this->menu_service->remove($id);
'model' => $form,
} catch (\DomainException $e) {
'menu' => $menu,
Yii::$app->errorHandler->logException($e);
]);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->redirect(['index']);
public function actionDelete($id)
}
{
try {
public function actionSaveItem($id)
$this->menu_service->remove($id);
{
} catch (\DomainException $e) {
$item = $this->findItemModel($id);
Yii::$app->errorHandler->logException($e);
$form = new MenuItemForm($item);
Yii::$app->session->setFlash('error', $e->getMessage());
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
}
try {
$this->menu_item_service->edit($item->id, $form);
return $this->redirect(['index']);
return $this->redirect(['index', 'id' => $item->menu_id]);
}
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
public function actionSaveItem($id)
Yii::$app->session->setFlash('error', $e->getMessage());
{
}
$item = $this->findItemModel($id);
}
$form = new MenuItemForm($item);
return $this->redirect(['index', 'id' => $item->menu_id]);
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
}
try {
$this->menu_item_service->edit($item->id, $form);
public function actionDeleteMenuItem()
{
return $this->redirect(['index', 'id' => $item->menu_id]);
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
} catch (\DomainException $e) {
if (Yii::$app->request->isAjax) {
Yii::$app->errorHandler->logException($e);
try {
Yii::$app->session->setFlash('error', $e->getMessage());
$id = Yii::$app->request->post('id');
}
$item = $this->findItemModel($id);
}
//$this->deleteItem($item);
return $this->redirect(['index', 'id' => $item->menu_id]);
$item->delete();
}
Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Deleted'));
public function actionDeleteMenuItem()
{
return [ 'result' => 'success' ];
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
}
if (Yii::$app->request->isAjax) {
catch (\RuntimeException $e) {
try {
return [ 'result' => 'error', 'message' => $e->getMessage() ];
$id = Yii::$app->request->post('id');
}
$item = $this->findItemModel($id);
}
$item->delete();
return ['result' => 'error', 'message' => 'Request error'];
}
Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Deleted'));
/*
return ['result' => 'success'];
public function actionSaveMenuItemData()
} catch (\RuntimeException $e) {
{
return ['result' => 'error', 'message' => $e->getMessage()];
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
}
if (Yii::$app->request->isAjax) {
}
try {
$id = Yii::$app->request->post('id');
return ['result' => 'error', 'message' => 'Request error'];
$name = Yii::$app->request->post('name');
}
$title = Yii::$app->request->post('title');
$style = Yii::$app->request->post('style');
public function actionSaveMenuItems()
$css = Yii::$app->request->post('css');
{
$url = Yii::$app->request->post('url');
$json = Yii::$app->request->post('json');
$target = Yii::$app->request->post('target');
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if (Yii::$app->request->isAjax) {
$item = $this->findItemModel($id);
try {
$item->name = $name;
$order = [];
$item->title_attr = $title;
$items = Json::decode($json, true);
$item->style = $style;
foreach ($items as $item) {
$item->css = $css;
$order[$item[1]] = isset($order[$item[1]]) ? $order[$item[1]] + 1 : 0;
$item->url = $url;
$this->menu_item_service->setPosition($item, $order[$item[1]]);
$item->target = $target;
}
$item->save();
Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Saved'));
Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Item Saved'));
return ['result' => 'success'];
} catch (\RuntimeException $e) {
return [ 'result' => 'success' ];
return ['result' => 'error', 'message' => $e->getMessage()];
}
}
catch (\RuntimeException $e) {
}
return [ 'result' => 'error', 'message' => $e->getMessage() ];
}
return ['result' => 'error', 'message' => 'Request error'];
}
}
return ['result' => 'error', 'message' => 'Request error'];
}
private function getCreatorWidgets($menu_id): array
*/
{
$widgets = [];
public function actionSaveMenuItems()
$modules = ModuleRecord::find()->active()->all();
{
foreach ($modules as $module) {
$json = Yii::$app->request->post('json');
if (method_exists($module->class, 'getMenuItemCreator')) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$module_widgets = call_user_func_array($module->class . '::getMenuItemCreator', [$menu_id]);
if (Yii::$app->request->isAjax) {
$widgets = is_array($module_widgets) ? array_merge($widgets, $module_widgets) : $widgets;
try {
}
$order = [];
}
$items = Json::decode($json, true);
foreach ($items as $item) {
return $widgets;
$order[$item[1]] = isset($order[$item[1]]) ? $order[$item[1]]+1 : 0;
}
$this->menu_item_service->setPosition($item, $order[$item[1]]);
}
private function createMenuItem()
Yii::$app->session->setFlash('success', Yii::t('menu', 'Menu Saved'));
{
return [ 'result' => 'success' ];
$form = new MenuItemForm();
}
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
catch (\RuntimeException $e) {
try {
return [ 'result' => 'error', 'message' => $e->getMessage() ];
$this->menu_item_service->create($form);
}
} catch (\DomainException $e) {
}
Yii::$app->errorHandler->logException($e);
return ['result' => 'error', 'message' => 'Request error'];
}
}
}
}
private function getCreatorWidgets($menu_id): array
{
protected function findModel($id): Menu
$widgets = [];
{
$modules = ModuleRecord::find()->active()->all();
if (($model = Menu::findOne($id)) !== null) {
foreach ($modules as $module) {
return $model;
if (method_exists($module->class, 'getMenuItemCreator')) {
}
$module_widgets = call_user_func_array($module->class . '::getMenuItemCreator', [$menu_id]);
throw new NotFoundHttpException('The requested menu does not exist.');
$widgets = is_array($module_widgets) ? array_merge($widgets, $module_widgets) : $widgets;
}
}
}
protected function findItemModel($id): MenuItem
return $widgets;
{
}
if (($model = MenuItem::findOne($id)) !== null) {
return $model;
private function createMenuItem()
}
{
throw new NotFoundHttpException('The requested menu item does not exist.');
$form = new MenuItemForm();
}
if ($form->load(Yii::$app->request->post()) & & $form->validate()) {
}
try {
$this->menu_item_service->create($form);
} catch (\DomainException $e) {
Yii::$app->errorHandler->logException($e);
}
}
}
protected function findModel($id): Menu
{
if (($model = Menu::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested menu does not exist.');
}
protected function findItemModel($id): MenuItem
{
if (($model = MenuItem::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested menu item does not exist.');
}
}