<commit_before><?php
/**
 * @file      MenuController.php.
 * @date      6/4/2015
 * @time      5:03 AM
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015 WritesDown
 * @license   http://www.writesdown.com/license/
 */

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\Json;

/* MODEL */
use common\models\Menu;
use common\models\PostType;
use common\models\Taxonomy;
use common\models\MenuItem;
use common\models\Post;
use common\models\Term;

/**
 * MenuController implements the CRUD actions for Menu model.
 *
 * @package backend\controllers
 * @author  <NAME> <<EMAIL>>
 * @since   1.0
 */
class MenuController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'update', 'create', 'delete', 'create-menu-item', 'delete-menu-item'],
                        'allow'   => true,
                        'roles'   => ['administrator']
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'delete'           => ['post'],
                    'update'           => ['post'],
                    'create-menu-item' => ['post'],
                    'delete-menu-item' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Menu models.
     *
     * @param null $id
     *
     * @return mixed
     */
    public function actionIndex($id = null)
    {
        $model = new Menu();
        // list all post types
        $postTypes = PostType::find()->where(['post_type_smb' => 1])->all();
        // list all taxonomies
        $taxonomies = Taxonomy::find()->where(['taxonomy_smb' => 1])->all();

        // get available menu
        if ($availableMenu = ArrayHelper::map(Menu::find()->all(), 'id', 'menu_title')) {
            if ($id === null && $availableMenu) {
                foreach ($availableMenu as $key => $menu) {
                    $id = $key;
                    break;
                }
            }
            $selectedMenu = $this->findModel($id);
        }

        return $this->render('index', [
            'model'         => $model,
            'availableMenu' => $availableMenu,
            'selectedMenu'  => isset($selectedMenu) ? $selectedMenu : null,
            'postTypes'     => $postTypes,
            'taxonomies'    => $taxonomies,
        ]);
    }

    /**
     * Creates a new Menu model.
     * If creation is successful, the browser will be redirected to the 'index' page.
     *
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Menu();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->id]);
        } else {
            return $this->redirect(['index']);
        }
    }

    /**
     * Updates an existing Menu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            if ($menuOrder = Yii::$app->request->post('MenuOrder')) {
                $this->saveMenuItem(Json::decode($menuOrder));
            }

        }

        return $this->redirect(['/menu/index', 'id' => $id]);
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *<commit_msg>add alert<commit_after><?php
/**
 * @file      MenuController.php.
 * @date      6/4/2015
 * @time      5:03 AM
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015 WritesDown
 * @license   http://www.writesdown.com/license/
 */

namespace backend\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\components\Json;

/* MODEL */
use common\models\Menu;
use common\models\PostType;
use common\models\Taxonomy;
use common\models\MenuItem;
use common\models\Post;
use common\models\Term;

/**
 * MenuController implements the CRUD actions for Menu model.
 *
 * @package backend\controllers
 * @author  <NAME> <<EMAIL>>
 * @since   1.0
 */
class MenuController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'update', 'create', 'delete', 'create-menu-item', 'delete-menu-item'],
                        'allow'   => true,
                        'roles'   => ['administrator']
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'delete'           => ['post'],
                    'update'           => ['post'],
                    'create-menu-item' => ['post'],
                    'delete-menu-item' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all Menu models.
     *
     * @param null $id
     *
     * @return mixed
     */
    public function actionIndex($id = null)
    {
        $model = new Menu();
        // list all post types
        $postTypes = PostType::find()->where(['post_type_smb' => 1])->all();
        // list all taxonomies
        $taxonomies = Taxonomy::find()->where(['taxonomy_smb' => 1])->all();

        // get available menu
        if ($availableMenu = ArrayHelper::map(Menu::find()->all(), 'id', 'menu_title')) {
            if ($id === null && $availableMenu) {
                foreach ($availableMenu as $key => $menu) {
                    $id = $key;
                    break;
                }
            }
            $selectedMenu = $this->findModel($id);
        }

        return $this->render('index', [
            'model'         => $model,
            'availableMenu' => $availableMenu,
            'selectedMenu'  => isset($selectedMenu) ? $selectedMenu : null,
            'postTypes'     => $postTypes,
            'taxonomies'    => $taxonomies,
        ]);
    }

    /**
     * Creates a new Menu model.
     * If creation is successful, the browser will be redirected to the 'index' page.
     *
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Menu();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->id]);
        } else {
            return $this->redirect(['index']);
        }
    }

    /**
     * Updates an existing Menu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            if ($menuOrder = Yii::$app->request->post('MenuOrder')) {
                $this->saveMenuItem(Json::decode($menuOrder));
            }

        }
        Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Menu successfully saved.'));
        return $this->redirect(['/menu/index', 'id' => $id]);
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
{'fraction_non_alphanumeric': 0.158459045006857, 'fraction_numerical': 0.00423887295848398, 'mean_word_length': 1.6910432740691044, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6800079', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2262, 'n_words': 518}