Your IP : 216.73.216.79


Current Path : /var/www/html/dyna-crm/controllers/
Upload File :
Current File : /var/www/html/dyna-crm/controllers/ServerownerController.php

<?php

namespace app\controllers;

use Yii;
use app\models\ServerOwner;
use app\models\ServerOwnerSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use app\models\Log;
use app\models\LogSearch;
use yii\web\UploadedFile;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use app\models\ServerDetails;

/**
 * ServerownerController implements the CRUD actions for ServerOwner model.
 */
class ServerownerController extends Controller {

    public $menu_id = 77;

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

    /**
     * Lists all ServerOwner models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new ServerOwnerSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id,]);
    }

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

    /**
     * Creates a new ServerOwner model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $model = new ServerOwner();
        $model->uuid = Uuid::uuid4();
        $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->load(Yii::$app->request->post())) {
            if ($model->save()) {

                //insert new item into server_details
                if (!empty($_POST['new_item'])) {
                    $item_arr = $_POST['new_item'];
                    foreach ($item_arr as $item_no) {
                        $model2 = new ServerDetails();
                        $model2->owner_id = $model->id;
                        $model2->location = $_POST['location_new_' . $item_no];
                        $model2->type = $_POST['type_new_' . $item_no];
                        $model2->domain = $_POST['domain_new_' . $item_no];
                        $model2->username = $_POST['username_new_' . $item_no];
                        $model2->pwd = $_POST['pwd_new_' . $item_no];
                        $model2->remarks = $_POST['remarks_new_' . $item_no];
                        $model2->created_dt = date("Y-m-d H:i:s");
                        $model2->created_by = Yii::$app->user->identity->id;
                        $model2->updated_dt = date("Y-m-d H:i:s");
                        $model2->updated_by = Yii::$app->user->identity->id;
                        $model2->save();
                    }
                }

                $Log = new Log();
                $Log->insertlog($this->menu_id, 'create', $model->uuid);
                $this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = '" . Url::toRoute('serverowner/index') . "';});");
            }
        }
        return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Updates an existing ServerOwner model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($uuid) {
        $model = $this->findModel($uuid);
        $model->updated_dt = date("Y-m-d H:i:s");
        $model->updated_by = Yii::$app->user->identity->id;
        if ($model->load(Yii::$app->request->post())) {
            if ($model->save()) {

                //insert new item into server_details
                if (!empty($_POST['new_item'])) {
                    $item_arr = $_POST['new_item'];
                    foreach ($item_arr as $item_no) {
                        $model2 = new ServerDetails();
                        $model2->owner_id = $model->id;
                        $model2->location = $_POST['location_new_' . $item_no];
                        $model2->type = $_POST['type_new_' . $item_no];
                        $model2->domain = $_POST['domain_new_' . $item_no];
                        $model2->username = $_POST['username_new_' . $item_no];
                        $model2->pwd = $_POST['pwd_new_' . $item_no];
                        $model2->remarks = $_POST['remarks_new_' . $item_no];
                        $model2->created_dt = date("Y-m-d H:i:s");
                        $model2->created_by = Yii::$app->user->identity->id;
                        $model2->updated_dt = date("Y-m-d H:i:s");
                        $model2->updated_by = Yii::$app->user->identity->id;
                        $model2->save();
                    }
                }

                //update item into server_details
                if (!empty($_POST['old_item'])) {
                    $item_arr2 = $_POST['old_item'];
                    foreach ($item_arr2 as $item_no2) {
                        if (($model3 = ServerDetails::findOne($item_no2)) !== null) {
                            $model3->location = $_POST['location_old_' . $item_no2];
                            $model3->type = $_POST['type_old_' . $item_no2];
                            $model3->domain = $_POST['domain_old_' . $item_no2];
                            $model3->username = $_POST['username_old_' . $item_no2];
                            $model3->pwd = $_POST['pwd_old_' . $item_no2];
                            $model3->remarks = $_POST['remarks_old_' . $item_no2];
                            $model3->updated_dt = date("Y-m-d H:i:s");
                            $model3->updated_by = Yii::$app->user->identity->id;
                            $model3->save();
                        }
                    }
                }

                $Log = new Log();
                $Log->insertlog($this->menu_id, 'create', $model->uuid);
                $this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data has been saved',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = '" . Url::toRoute('serverowner/index') . "';});");
            }
        }
        return $this->render('update', ['model' => $model, 'menu_id' => $this->menu_id]);
    }

    /**
     * Deletes an existing ServerOwner model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @param string $uuid
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete() {
        if (($model = ServerOwner::findOne($_POST['id'])) !== null) {
            $Log = new Log();
            $Log->insertlog($this->menu_id, 'delete', $model->uuid);
            if ($model->delete())
                return 'success';
        }
    }

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

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

}