Your IP : 216.73.216.124


Current Path : /var/www/html/project/controllers/
Upload File :
Current File : /var/www/html/project/controllers/ProductsController.php

<?php

namespace app\controllers;

use Yii;
use app\models\Products;
use app\models\ProductsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\Log;
use app\models\LogSearch;
use app\models\Tasks;

/**
 * ProductsController implements the CRUD actions for Products model.
 */
class ProductsController extends Controller {

    /**
     * {@inheritdoc}
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Products models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new ProductsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Products model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Products model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new Products();
        $model->status = 1;
        $model->project_id = $_POST['project_id'];
        $model->created_dt = date("Y-m-d H:i:s");
        $model->created_by = Yii::$app->user->identity->id;
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->save()) {

            $Log = new Log();
            $Log->insertlog('product', 'create', $model->id);

            return '<div class="row mb-3" id="row_' . $model->id . '">
                <input type="hidden" value="' . $model->id . '" name="product_id[]">
                <div class="col-3"><input name="brand_name_' . $model->id . '" type="text" class="form-control brand_name"></div>
                <div class="col-8"><input name="product_name_' . $model->id . '" type="text" class="form-control product_name"></div>
                <div class="col-1">
                    <button type="button" class="btn btn-outline-hover-danger btn-icon">
                        <i class="far fa-trash-alt kt-font-danger" onclick="deleteproduct(' . $model->id . ')"></i>
                    </button>
                </div>
            </div>';
        }
    }

    public function actionDelete() {
        $id = $_POST['id'];
        $status = 'failed';
        $model = Products::findOne($id);
        if (!empty($model)) {
            $model->status = 0;
            if ($model->save(false)) {

                Tasks::updateAll(['deleted_status' => 1,], ['AND', ['=', 'task_type', 'product'], ['=', 'data_id', $model->id]]);

                $Log = new Log();
                $Log->insertlog('product', 'delete', $model->id);
                $status = 'success';
            }
        }
        return $status;
    }

    /**
     * Updates an existing Products model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
                    'model' => $model,
        ]);
    }

    /**
     * Finds the Products model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Products the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Products::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

}