Your IP : 216.73.216.79


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

<?php

namespace backend\modules\refdynawebv2\models;

use Yii;
use yii\helpers\ArrayHelper;
use backend\modules\mimin\models\User;

/**
 * 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 \yii\db\ActiveRecord {

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

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['sort'], 'integer'],
            [['code', 'cat', 'descr', 'descr_en'], 'required'],
            [['created_at', 'updated_at', 'deleted_at', 'descr_en', 'alt_code'], 'safe'],
            [['cat'], 'string', 'max' => 25],
            [['descr'], 'string', 'max' => 150],
            [['param1'], 'string', 'max' => 5],
            [['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', 'Label (Malay)'),
            'descr_en' => Yii::t('app', 'Label (English)'),
            'sort' => Yii::t('app', 'Order'),
            'param1' => Yii::t('app', 'Parent'),
            'created_at' => Yii::t('app', 'Created Date'),
            'alt_code' => 'Alternative Code'
        ];
    }

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

    public static function getList($kat, $param1 = '', $choose = true, $sort = 'descr,sort') {
        $arr2 = [];

        if ($choose) {
            // $arr2['0'] = '--Sila Pilih--';
        }

        if (empty($param1)) {
            $arr = self::find()->where("cat = '$kat'")->orderBy($sort)->all();
        } else {
            $arr = self::find()->where("cat = '$kat' AND param1 = '$param1'")->orderBy('descr,sort')->all();
        }

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

    public static function getList2($kat, $code = "", $choose = true) {
        $arr2 = [];

        if ($choose) {
            // $arr2['0'] = '--Sila Pilih--';
        }

        if (empty($code)) {
            $arr = self::find()->where("cat = '$kat'")->orderBy('sort,descr')->all();
        } else {
            $arr = self::find()->where("cat = '$kat' AND code IN ($code)")->orderBy('sort,descr')->all();
        }

        foreach ($arr as $code) {
            $arr2[$code->code] = $code->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 static function getDesc2($cat, $code, $param1, $field = '') {
        $ref = self::find()->where(['cat' => $cat, 'code' => $code, 'param1' => $param1])->one();
        if (!is_null($ref)) {
            if ($field)
                return $ref->$field;
            else
                return $ref->descr;
        } else {
            return '';
        }
    }

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

        return $refarr;
    }

    public static function getdescr($id) {
        if (($model = Ref::findOne($id)) !== null)
            return '' . $model->cat . '<br>' . $model->descr;
        else
            return '';
    }

    public static function getparentid($code, $cat) {
        $output = 0;
        if (($model = Ref::findOne(['code' => $code, 'cat' => $cat])) !== null)
            $output = $model->id;
        return $output;
    }

    public function getSoalanParlimen() {
        return $this->hasMany(\backend\models\SoalanParlimen::className(), ['kat_dewan' => 'code']);
    }

    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;
    }

    public static function getrefmultilevel($cat = '', $indent = '', $parent_group = '') {
        $items = [];
        // for all childs of $parent_group (roots if $parent_group == null)
        $groups = Ref::find()->where(['=', 'cat', $cat])->andwhere(['=', 'param1', $parent_group])->orderBy(['sort' => SORT_ASC])->all();
        foreach ($groups as $group) {
            // add group to items list 
            $items[$group->code] = $indent . $group->descr;
            // recursively add children to the list with indent
            $items = array_merge($items, self::getrefmultilevel($cat, $indent . html_entity_decode('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'), $group->id));
        }
        return $items;
    }
}