Your IP : 216.73.216.79


Current Path : /var/www/html/crm/models/
Upload File :
Current File : /var/www/html/crm/models/ProductCategory.php

<?php

namespace app\models;

use yii\helpers\ArrayHelper;
use Yii;
use yii\helpers\Url;

/**
 * This is the model class for table "product_category".
 *
 * @property int $id
 * @property string $uuid
 * @property string $name
 * @property int $parent
 * @property int $status
 * @property string $created_dt
 * @property int $created_by
 * @property string $updated_dt
 * @property int $updated_by
 */
class ProductCategory extends \yii\db\ActiveRecord {

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

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

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uuid' => 'Uuid',
            'name' => 'Category Name',
            'parent' => 'Parent Category',
            'status' => 'Status',
            'created_dt' => 'Created Dt',
            'created_by' => 'Created By',
            'updated_dt' => 'Updated Dt',
            'updated_by' => 'Updated By',
        ];
    }

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

    public function getcategorylist($arr_type, $level, $parent, $arr, $parentno, $parent_string) {
        $count = 0;
        $level++;
        if ($parentno != '')
            $parent_string .= $parentno . '.';
        $model = static::find()->where(['parent' => $parent])->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $count++;
                if ($arr_type == 'arr1')
                    $arr[$row->id] = $parent_string . $count . '. ' . $row->name;
                if ($arr_type == 'arr2')
                    $arr[$row->id] = ['class' => 'option-level-' . $level];
                $arr = static::getcategorylist($arr_type, $level, $row->id, $arr, $count, $parent_string);
            }
        }
        return $arr;
    }

    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 product_category SET status=$data_status WHERE id=$row->id")->execute();
                static::changestatus($row->id, $data_status);
                $Log = new Log();
                $Log->insertlog(21, 'status' . $data_status, $row->uuid);
            }
        }
    }

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

    public function getcategorytree($parent_id) {
        $output = '';

        $model = static::find()->where(['parent' => $parent_id])->orderBy(['name' => SORT_ASC])->all();
        if (!empty($model)) {
            foreach ($model as $row) {
                $icon_status = 'kt-font-success';
                if ($row->status == 0)
                    $icon_status = 'kt-font-danger';
                $output.= '<li data-jstree=\'{"opened":true,"icon":"fas fa-box ' . $icon_status . '"}\'>
                            <span class="mr-2 kt-font-brand" style="cursor:pointer;" onclick="location.href=\'' . Url::to(['productcategory/update', 'uuid' => $row->uuid, 'returnUrl' => Yii::$app->controller->action->id]) . '\'"><i class="far fa-edit"></i></span>
                            <span class="jstree-cat">' . $row->name . '</span>
                            <span class="ml-2 kt-font-warning" style="cursor:pointer;" onclick="location.href=\'' . Url::to(['productcategory/create', 'parent' => $row->uuid, 'returnUrl' => Yii::$app->controller->action->id]) . '\'"><i class="fas fa-plus"></i></span>';

                $getsubmenu = static::getcategorytree($row->id);
                if ($getsubmenu != '')
                    $output.= $getsubmenu;

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

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

    public function getname($id) {
        if (($model = static::findOne($id)) !== null) {
            return $model->name;
        }
    }

    public function getlevel($parent_id) {
        $count = 1;
        while ($parent_id != 0) {
            $count++;
            if (($model = static::findOne($parent_id)) !== null)
                $parent_id = $model->parent;
        }
        return $count;
    }

}