| Current Path : /var/www/html/protegev2/backend/modules/contentDirectory/controllers/ |
| Current File : /var/www/html/protegev2/backend/modules/contentDirectory/controllers/ContentDirectoryController.php |
<?php
namespace backend\modules\contentDirectory\controllers;
use Yii;
use backend\modules\contentDirectory\models\ContentDirectory;
use backend\modules\contentDirectory\models\ContentDirectorySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use backend\modules\contentDirectory\Module as M;
use yii\web\Response;
use yii\widgets\ActiveForm;
/**
* ContentDirectoryController implements the CRUD actions for ContentDirectory model.
*/
class ContentDirectoryController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all ContentDirectory models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new ContentDirectorySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single ContentDirectory 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 ContentDirectory model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new ContentDirectory();
$model->created_at = date("Y-m-d H:i:s");
$model->created_by = Yii::$app->user->identity->id;
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
//upload file image
$model->employee_image = UploadedFile::getInstance($model, 'employee_image');
if (!empty($model->employee_image)) {
$employee_image = 'uploads/content-directory/' . date('YmdHis') . '.' . $model->employee_image->extension;
if ($model->employee_image->saveAs('../' . $employee_image))
$model->employee_image = $employee_image;
}
// file is uploaded successfully
if ($model->save(false)) {
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing ContentDirectory 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);
$model->updated_at = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$employee_image = $model->employee_image;
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ($model->load(Yii::$app->request->post())) {
$model->employee_image = UploadedFile::getInstance($model, 'employee_image');
if (!empty($model->employee_image)) {
$employee_image = 'uploads/content-directory/' . date('YmdHis') . '.' . $model->employee_image->extension;
if ($model->employee_image->saveAs('../' . $employee_image))
$model->employee_image = $employee_image;
} else
$model->employee_image = $employee_image;
// file is uploaded successfully
if ($model->save(false)) {
return $this->redirect(['index']);
}
}
return $this->render('update', ['model' => $model,]);
}
/**
* Deletes an existing ContentDirectory model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id) {
$model = $this->findModel($id);
$model->delete();
return $this->redirect(['index']);
}
/**
* Finds the ContentDirectory model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ContentDirectory the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = ContentDirectory::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(M::t('app', 'PageNotExist'));
}
public function actionGetsector($parent_code = '') {
switch (Yii::$app->language) {
case 'en':
$desc = 'descr_en';
break;
default:
$desc = 'descr';
break;
}
$output = '';
$arr = ArrayHelper::map(
(new \yii\db\Query())->select(['code', $desc])
->from('ref')
->where(['=', 'cat', 'DEPARTMENT'])
->andwhere(['=', 'param1', $parent_code])
->orderBy(['sort' => SORT_ASC])
->all(), 'code', $desc);
if (!empty($arr)) {
foreach ($arr as $code => $label) {
$output .= '<option value="' . $code . '">' . $label . '</option>';
}
}
return $output;
}
public function actionSorting($data = '') {
$count = 0;
$data_arr = explode(',', $data);
if (!empty($data_arr)) {
foreach ($data_arr as $id) {
$model = ContentDirectory::findOne($id);
if (!empty($model)) {
$count++;
$model->sorting = $count;
$model->save(false);
}
}
}
return $data;
}
}