Your IP : 216.73.216.79


Current Path : /var/www/html/ebookingdbkl/backend/modules/refdynawebv2/models/
Upload File :
Current File : /var/www/html/ebookingdbkl/backend/modules/refdynawebv2/models/Ref.php

<?php

namespace backend\modules\refdynawebv2\models;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "ost_ref".
 *
 * @property integer $id
 * @property string $cat
 * @property string $code
 * @property string $descr
 * @property integer $sort
 * @property string $param1
 */
class Ref extends \common\models\ModelBehaviours {

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

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['sort'], 'integer'],
            [['code', 'cat'], 'required'],
            [['created_at', 'updated_at', 'deleted_at'], 'safe'],
            [['cat', 'code'], 'string', 'max' => 25],
            [['descr','descr_en'], 'string', 'max' => 150],
            [['param1'], 'string', 'max' => 250],
            [['icon_name'], 'string', 'max' =>50],
            [['created_by', 'updated_by'], 'string', 'max' => 50],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'cat' => Yii::t('app','Category'),
            'code' => Yii::t('app','Code'),
            'descr' => Yii::t('app','Description [MY]'),
            'descr_en' => Yii::t('app','Description [EN]'),
            'sort' => Yii::t('app','Order'),
            'param1' => Yii::t('app','Parent'),
            'created_at' => Yii::t('app','Created Date'),
        ];
    }

    public static function getList($cat, $param = '', $sort = 'sort DESC') {
        $arr2 = [];

        if (empty($param)) {
            $arr = self::find()->where(['cat' => $cat])->orderBy($sort)->all();
        } else {
            $arr = self::find()->where(['cat' => $cat, 'param1' => $param])->orderBy($sort)->all();
        }

        foreach ($arr as $param) {
            $arr2[$param->code] = $param->descr;
        }

        return $arr2;
    }

    public static function getDesc($cat, $code, $field = '') {
        $ref = self::find()->where(['cat' => $cat, 'code' => $code])->one();
        if (!is_null($ref)) {
            if ($field)
                return $ref->$field;
            else
                return $ref->descr; 
        } else {
            return '';
        }
    }

    public function parent(){
        $ref = self::find()->all();
        $refarr = [];
        array_walk($ref,function($v) use (&$refarr){
            return $refarr[$v->id] = $v->descr;
        });

        return $refarr;
    }

    public static function getparentid($code, $cat) {
        $output = 0;
        if (($model = Ref::findOne(['code' => $code, 'cat' => $cat])) !== null)
            $output = $model->id;
        return $output;
    }
    
    public static function getbranchlist() {
        $output = [];
        /*if (User::userrole() == 'admin-branch') {
            $output = ArrayHelper::map(
                            (new \yii\db\Query())->select(['code', 'descr'])
                                    ->from('ref')
                                    ->where(['=', 'cat', 'PORTAL'])
                                    ->andwhere(['=', 'code', Yii::$app->user->identity->branch])
                                    ->orderBy(['descr' => SORT_ASC])
                                    ->all(), 'code', 'descr');
        } else {*/
            $output = ArrayHelper::map(
                            (new \yii\db\Query())->select(['code', 'descr'])
                                    ->from('ref')
                                    ->where(['=', 'cat', 'PORTAL'])
                                    ->orderBy(['descr' => SORT_ASC])
                                    ->all(), 'code', 'descr');
        /*} */
        return $output;
    }
}