Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-hr/models/
Upload File :
Current File : /var/www/html/dyna-hr/models/Menu.php

<?php

namespace app\models;

use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use Yii;
use app\models\UserAccess;
use app\models\MenuAccess;

/**
 * This is the model class for table "menu".
 *
 * @property int $id
 * @property string $uuid
 * @property string $title
 * @property int $parent
 * @property int $sort
 * @property string $url
 * @property string $icon
 * @property int $status
 * @property int $separator
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class Menu extends \yii\db\ActiveRecord {

    public $user_access;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return 'menu';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['uuid', 'title', 'left_menu', 'user_access'], 'required'],
            [['parent', 'sort', 'status', 'separator', 'created_by', 'updated_by'], 'integer'],
            [['created_dt', 'updated_dt'], 'safe'],
            [['title', 'url', 'icon'], 'string', 'max' => 255],
            [['uuid'], 'unique'],
            [['sort', 'parent'], 'default', 'value' => 0],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'title' => 'Menu Title',
            'parent' => 'Menu Parent',
            'sort' => 'Sort',
            'url' => 'Menu URL',
            'icon' => 'Icon',
            'status' => 'Status',
            'separator' => 'Separator?',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
            'left_menu' => 'Left Menu?'
        ];
    }

    public function getmenulist($arr_type) {
        $arr = [];
        $arr2 = [];
        $count = 0;
        $model = static::find()->where(['parent' => 0, 'separator' => 0, 'left_menu' => 1])->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                $arr[$row->id] = $count . '. ' . $row->title;
                $arr2[$row->id] = ['class' => 'option-level1'];

                //submenu
                $count2 = 0;
                $model2 = static::find()->where(['parent' => $row->id, 'separator' => 0, 'left_menu' => 1])->orderBy(['sort' => SORT_ASC])->all();
                if (!empty($model2)) {
                    foreach ($model2 as $row2) {
                        $count2++;
                        $arr[$row2->id] = $count . '.' . $count2 . '. ' . $row2->title;
                        $arr2[$row2->id] = ['class' => 'option-level-2'];
                    }
                }
            }
        }

        if ($arr_type == 'arr1')
            return $arr;
        if ($arr_type == 'arr2')
            return $arr2;
    }

    public function getParent() {
        if (($model = static::findOne(['id' => $this->parent])) !== null) {
            return $model->title;
        } else {
            return '';
        }
    }

    public function getid($uuid) {
        if (($model = static::findOne(['uuid' => $uuid])) !== null) {
            return $model->id;
        }
    }

    public function getmenutree($parent_id, $menu_type, $level, $status) {
        $level++;
        $output = '';
        $leftmenu = 0;
        if ($menu_type == 'leftmenu')
            $leftmenu = 1;

        $model = static::find()->where(['parent' => $parent_id, 'left_menu' => $leftmenu])->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {

                if ($row->status == 0)
                    $status = 0;

                $icon_status = 'kt-font-success';
                if ($row->status == 0)
                    $icon_status = 'kt-font-danger';

                $separator_label = '';
                if ($row->separator == 1)
                    $separator_label = ' <small class="text-light-gray">(Separator)</small>';

                $addsubmenu_btn = '';

                if ($row->left_menu == 1 && $row->status == 1 && $level != 3 && $row->separator == 0)
                    $addsubmenu_btn = '<span class="ml-2 kt-font-warning" style="cursor:pointer;" onclick="location.href=\'' . Url::to(['menu/create', 'parent' => $row->uuid, 'returnUrl' => Yii::$app->controller->action->id]) . '\'"><i class="fas fa-plus"></i></span>';

                $output .= '<li data-jstree=\'{"opened":true,"icon":"fas fa-bars ' . $icon_status . '"}\'>
                            <span class="mr-2 kt-font-brand" style="cursor:pointer;" onclick="location.href=\'' . Url::to(['menu/update', 'uuid' => $row->uuid, 'returnUrl' => Yii::$app->controller->action->id]) . '\'"><i class="far fa-edit"></i></span>
                            <span class="jstree-cat">' . $row->title . $separator_label . '</span>' . $addsubmenu_btn;

                $getsubmenu = static::getmenutree($row->id, $menu_type, $level, $status);
                if ($getsubmenu != '')
                    $output .= $getsubmenu;

                $output .= '</li>';
            }
        }

        if ($output != '')
            return '<ul>' . $output . '</ul>';
    }

    public function changestatus($parent, $data_status) {
        $model = static::find()->where(['parent' => $parent])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                Yii::$app->db->createCommand("UPDATE menu SET status=$data_status WHERE id=$row->id")->execute();
                static::changestatus($row->id, $data_status);
            }
        }
    }

    public function getmenuforuser($parent, $count, $level, $user_id, $checked_list, $role) {
        $level++;
        $output = '';
        $model = static::find()->where(['parent' => $parent, 'status' => 1, 'left_menu' => 1])->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {

                //check if menu can be access by the role
                $MenuAccess = new MenuAccess();
                $menu_access = $MenuAccess->checkmenuaccess($row->id, $role);
                if ($menu_access == 1) {
                    if ($parent == 0)
                        $output .= '<div class="col-3 p-3 mb-2" style="border:1px solid #e2e5ec;border-radius:4px;">';
                    $margin_left = '';
                    if ($level != 1)
                        $margin_left = 'ml-' . ($level + 2);
                    //$menu_num = Menu::getmenunumber($row->id);

                    $checked = '';
                    if (!empty($checked_list)) {
                        if (in_array($row->id, $checked_list)) {
                            $checked = 'checked';
                        }
                    } else {
                        if ($user_id != 0) {
                            $UserAccess = new UserAccess();
                            $checked = $UserAccess->checkuseraccess($user_id, $row->id);
                        }
                    }

                    $output .= '<label class="kt-checkbox ' . $margin_left . '">
                                <input type="checkbox" name="User[user_access][]" value="' . $row->id . '" ' . $checked . '>' . $row->title . '
                                <span></span>
                               </label>';
                    $count++;
                    $output .= static::getmenuforuser($row->id, 1, $level, $user_id, $checked_list, $role);
                    if ($parent == 0)
                        $output .= '</div>';
                }
            }
        }

        if ($output != '') {
            return $output;
        }
    }

    public function getmenunumber($id) {
        $output = '';
        if (($model = static::findOne($id)) !== null) {
            if ($model->parent != 0)
                $output .= static::getmenunumber($model->parent);
            $output .= $model->sort . '.';
        }
        return $output;
    }

    public function getmenutitle($id) {
        if (($model = static::findOne(['id' => $id])) !== null) {
            return $model->title;
        }
    }

    public function getbreadcrumbs($id, $output) {
        $parent = '';
        if (($model = static::findOne(['id' => $id])) !== null) {
            $parent = $model->parent;
            if ($model->parent != 0)
                $output .= static::getbreadcrumbs($model->parent, $output);
            $output .= '<span class="kt-subheader__breadcrumbs-separator"></span><span class="kt-subheader__breadcrumbs-link">' . static::getmenutitle($model->id) . '</span>';
        }
        if ($parent == 0)
            $output = '<span class="kt-subheader__breadcrumbs-separator"></span><span class="kt-subheader__breadcrumbs-link">Home</span>' . $output;
        return $output;
    }

    public function getleftmenu() {
        $output = '';
        $model = static::find()
                        ->innerJoin('menu_access', 'menu.id=menu_access.menu_id')
                        ->where(['=', 'parent', 0])
                        ->andwhere(['=', 'status', 1])
                        ->andwhere(['=', 'left_menu', 1])
                        ->andwhere(['=', 'user_role', Yii::$app->user->identity->role])
                        ->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {

                if ($row->separator == 1) {
                    $output .= '<li class="kt-menu__section">
                                    <h4 class="kt-menu__section-text">' . $row->title . '</h4>
                                    <i class="kt-menu__section-icon flaticon-more-v2"></i>
                                </li>';
                } else {
                    //check ada anak level 2 tak
                    $submenu_level2 = static::getleftmenu2($row->id);
                    if ($submenu_level2 == '') {
                        //no second level menu
                        $output .= '<li class="kt-menu__item kt-menu__item--rel">
                                            <a href="' . $row->url . '" class="kt-menu__link">
                                                <span class="kt-menu__link-text">' . $row->title . '</span>
                                            </a>
                                        </li>';
                    } else {
                        //has second level menu
                        $output .= '<li class="kt-menu__item  kt-menu__item--submenu kt-menu__item--rel" data-ktmenu-submenu-toggle="click" aria-haspopup="true">
                                            <a href="index.php?r=dashboard/index" class="kt-menu__link kt-menu__toggle">
                                                <span class="kt-menu__link-text">' . $row->title . '</span>
                                                <i class="kt-menu__ver-arrow la la-angle-right"></i>
                                                <i class="fas fa-sort-down ml-3 text-white"></i>
                                            </a>
                                            <div class="kt-menu__submenu kt-menu__submenu--classic kt-menu__submenu--left">
                                                <ul class="kt-menu__subnav">' . $submenu_level2 . '</ul>
                                            </div>
                                        </li>';
                    }
                }
            }
        }
        return $output;
    }

    public function getleftmenu2($parent) {
        $output = '';
        $model = static::find()
                        ->innerJoin('menu_access', 'menu.id=menu_access.menu_id')
                        ->where(['=', 'parent', $parent])
                        ->andwhere(['=', 'status', 1])
                        ->andwhere(['=', 'left_menu', 1])
                        ->andwhere(['=', 'user_role', Yii::$app->user->identity->role])
                        ->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $submenu_level3 = static::getleftmenu3($row->id);
                if ($submenu_level3 == '') {
                    //no 3 level menu
                    $output .= '<li class="kt-menu__item " aria-haspopup="true">
                                                        <a href="' . $row->url . '" class="kt-menu__link ">
                                                            <i class="kt-menu__link-bullet kt-menu__link-bullet--dot"><span></span></i>
                                                            <span class="kt-menu__link-text">' . $row->title . '</span>
                                                        </a>
                                                    </li>';
                }
            }
        }
        return $output;
    }

    public function getleftmenu3($parent) {
        $output = '';

        $model = static::find()
                        ->innerJoin('menu_access', 'menu.id=menu_access.menu_id')
                        ->where(['=', 'parent', $parent])
                        ->andwhere(['=', 'status', 1])
                        ->andwhere(['=', 'left_menu', 1])
                        ->andwhere(['=', 'user_role', Yii::$app->user->identity->role])
                        ->orderBy(['sort' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $output .= '<li class="kt-menu__item kt-menu__selected_' . $row->id . '" aria-haspopup="true">
                                    <a href="' . $row->url . '" class="kt-menu__link ">
                                        <i class="kt-menu__link-bullet kt-menu__link-bullet--dot"><span></span></i>
                                        <span class="kt-menu__link-text">' . $row->title . '</span>
                                    </a>
                               </li>';
            }
        }
        return $output;
    }

}