Your IP : 216.73.216.79


Current Path : /var/www/html/persada/backend/modules/menu/models/backend/
Upload File :
Current File : /var/www/html/persada/backend/modules/menu/models/backend/RoleMapping.php

<?php

namespace backend\modules\menu\models\backend;

use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "frontend_role_mapping".
 *
 * @property int $id
 * @property string $role_code
 * @property int $menu_id
 * @property int $parent_id
 * @property string $created_by
 * @property string $created_at
 * @property string $updated_by
 * @property string $updated_at
 *
 * @property FrontendMenu $menu
 */
class RoleMapping extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'backend_role_mapping';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['role_code', 'menu_id', 'parent_id', 'created_by', 'created_at', 'updated_by'], 'required'],
            [['menu_id', 'parent_id'], 'integer'],
            [['created_at', 'updated_at','sort'], 'safe'],
            [['role_code', 'created_by', 'updated_by'], 'string', 'max' => 255],
            [['menu_id'], 'exist', 'skipOnError' => true, 'targetClass' => Menu::className(), 'targetAttribute' => ['menu_id' => 'menu_id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'role_code' => Yii::t('app', 'Role Code'),
            'menu_id' => Yii::t('app', 'Menu ID'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'created_by' => Yii::t('app', 'Created By'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_by' => Yii::t('app', 'Updated By'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMenu()
    {
        return $this->hasOne(Menu::className(), ['menu_id' => 'menu_id']);
    }

    public static function nestedmenu(int $id, string $role, $i=0){
        
        /*model*/
        $model = self::find()->where(['role_code'=>$role,'parent_id'=>$id])->orderBy('sort ASC')->all();

        /*increment level*/
        $i++;
        if(!empty($model)){
            foreach ($model as $menu) {

                if (!$menu->menu->menu_status) {
                    continue;
                }

                echo '<div class="listmenu" style="margin-left:'.($i * 5).'em">
                        <span>'.$menu->menu->menu_name.'</span>
                        <div class="input-group pull-right">
                            <a class="btn btn-default" title="'. Yii::t('app','To top') .'" href="'. URL::to(['/menu/backend/level', 'id' => $menu->id, 'role' => $menu->role_code, 'pos' => '<', 'sort' => 'DESC']) .'"><i class="fa fa-arrow-up"></i></a>
                            <a class="btn btn-default" title="'. Yii::t('app','To bottom') .'" href="'. Url::to(['/menu/backend/level', 'id' => $menu->id, 'role' => $menu->role_code, 'pos' => '>', 'sort' => 'ASC']) .'"><i class="fa fa-arrow-down"></i></a>';
                            
                            // limit to 3 depth
                            if( $i < 2) 
                            {
                                echo '<a class="btn btn-default" title="'.Yii::t('app','Add Submenu').'" href="'. URL::to(['/menu/backend/assign-menu', 'parentid' => $menu->id, 'role' => $menu->role_code] ) .'"><i class="fa fa-plus"></i></a>';
                            }
                            echo        '<a class="btn btn-default" title="'.Yii::t('app','Delete').'" href="'. URL::to(['/menu/backend/roledelete', 'id' => $menu->id]) .'" data-method="post"><i class="fa fa-trash"></i></a>
                        </div>
                        <div class="clearfix"></div>      
                    </div>';

                self::nestedmenu($menu->id, $role, $i);
            }
        }

        return;
    }

    public function beforeSave($insert) 
    {
        if ($insert) {
            $this->created_at = date('Y-m-d');
            $this->created_by = Yii::$app->user->identity->id;
        } else {
            $this->updated_at = date('Y-m-d');
            $this->updated_by = Yii::$app->user->identity->id;
        }
        return parent::beforeSave($insert);
    }    
}