| Current Path : /var/www/html/dosm/backend/modules/contentApplication/controllers/ |
| Current File : /var/www/html/dosm/backend/modules/contentApplication/controllers/ContentApplicationController.php |
<?php
namespace backend\modules\contentApplication\controllers;
use Yii;
use backend\modules\contentApplication\models\ContentApplicationTranslation;
use backend\modules\contentApplication\models\ContentApplication;
use backend\modules\contentApplication\models\ContentOnlineservices;
use backend\modules\contentApplication\models\ContentOnlineservicesSearch;
use backend\modules\contentApplication\models\ContentApplicationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use backend\modules\refdynawebv2\models\Ref;
use yii\filters\AccessControl;
use backend\modules\fileArchive\models\FileArchive;
/**
* ContentApplicationController implements the CRUD actions for ContentApplication model.
*/
class ContentApplicationController extends Controller {
/**
* {@inheritdoc}
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all ContentApplication models.
* @return mixed
*/
public function actionIndex() {
$searchModel = new ContentApplicationSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single ContentApplication 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 ContentApplication model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate() {
$model = new ContentApplication();
//$model->scenario = 'create';
$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;
$model->application_language = 'Malay';
if ($model->load(Yii::$app->request->post())) {
$model->application_img = UploadedFile::getInstance($model, 'application_img');
if (!empty($model->application_img)) {
$application_img = 'uploads/content-links/img_' . date("YmdHis") . '.' . $model->application_img->extension;
if ($model->application_img->saveAs('../' . $application_img))
$model->application_img = $application_img;
}
if ($model->save()) {
$modeltranslation = new ContentApplicationTranslation();
$modeltranslation->application_translation_parent_id = $model->application_id;
$modeltranslation->application_translation_title = $model->application_title;
$modeltranslation->application_translation_language = $model->application_language;
$modeltranslation->application_translation_main = 1;
$modeltranslation->created_at = date("Y-m-d H:i:s");
$modeltranslation->created_by = Yii::$app->user->identity->id;
$modeltranslation->updated_at = date("Y-m-d H:i:s");
$modeltranslation->updated_by = Yii::$app->user->identity->id;
$modeltranslation->save();
return $this->redirect(['index']);
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing ContentApplication 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;
$original_img = $model->application_img;
$translation_id = '';
if (($modeltranslation = ContentApplicationTranslation::findOne(['application_translation_parent_id' => $model->application_id, 'application_translation_main' => 1])) !== null) {
$translation_id = $modeltranslation->application_translation_id;
$model->application_title = $modeltranslation->application_translation_title;
$model->application_language = $modeltranslation->application_translation_language;
}
if ($model->load(Yii::$app->request->post())) {
$model->application_img = UploadedFile::getInstance($model, 'application_img');
if (!empty($model->application_img)) {
$application_img = 'uploads/content-links/img_' . date("YmdHis") . '.' . $model->application_img->extension;
if ($model->application_img->saveAs('../' . $application_img)) {
$model->application_img = $application_img;
if (!empty($original_img)) {
$modelArchive = new FileArchive;
$modelArchive->file_source = 'Links';
$modelArchive->file_name = $model->application_title;
$modelArchive->file_path = $original_img;
$modelArchive->file_updated_at = date("Y-m-d H:i:s");
$modelArchive->file_updated_by = Yii::$app->user->identity->id;
$modelArchive->save(false);
}
}
} else {
$model->application_img = $original_img;
}
if ($model->save()) {
if (($modeltranslation = ContentApplicationTranslation::findOne($translation_id)) !== null) {
$modeltranslation->application_translation_title = $model->application_title;
$modeltranslation->application_translation_language = $model->application_language;
$modeltranslation->updated_at = date("Y-m-d H:i:s");
$modeltranslation->updated_by = Yii::$app->user->identity->id;
$modeltranslation->save();
}
//insert new translation
if (isset($_POST['newtranslation']) && !empty($_POST['newtranslation'])) {
foreach ($_POST['newtranslation'] as $newno) {
$modeltranslationnew = new ContentApplicationTranslation();
$modeltranslationnew->application_translation_parent_id = $model->application_id;
$modeltranslationnew->application_translation_title = $_POST['new_title_' . $newno];
$modeltranslationnew->application_translation_language = $_POST['new_language_' . $newno];
$modeltranslationnew->application_translation_main = 0;
$modeltranslationnew->created_at = date("Y-m-d H:i:s");
$modeltranslationnew->created_by = Yii::$app->user->identity->id;
$modeltranslationnew->updated_at = date("Y-m-d H:i:s");
$modeltranslationnew->updated_by = Yii::$app->user->identity->id;
$modeltranslationnew->save();
}
}
//update translation
if (isset($_POST['oldtranslation']) && !empty($_POST['oldtranslation'])) {
foreach ($_POST['oldtranslation'] as $oldtranslation) {
if (($modeltranslationupdate = ContentApplicationTranslation::findOne($oldtranslation)) !== null) {
$modeltranslationupdate->application_translation_title = $_POST['old_title_' . $oldtranslation];
$modeltranslationupdate->application_translation_language = $_POST['old_language_' . $oldtranslation];
$modeltranslationupdate->updated_at = date("Y-m-d H:i:s");
$modeltranslationupdate->updated_by = Yii::$app->user->identity->id;
$modeltranslationupdate->save();
}
}
}
return $this->redirect(['index']);
}
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing ContentApplication 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);
if (($modeltranslation = ContentApplicationTranslation::findOne(['application_translation_parent_id' => $model->application_id, 'application_translation_main' => 1])) !== null) {
$model->application_title = $modeltranslation->application_translation_title;
}
$modelArchive = new FileArchive;
$modelArchive->file_source = 'Links';
$modelArchive->file_name = $model->application_title;
$modelArchive->file_path = $model->application_img;
$modelArchive->file_updated_at = date("Y-m-d H:i:s");
$modelArchive->file_updated_by = Yii::$app->user->identity->id;
$modelArchive->save(false);
$model->delete();
ContentApplicationTranslation::deleteAll(['application_translation_parent_id' => $id]);
return $this->redirect(['index']);
}
/**
* Finds the ContentApplication model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ContentApplication the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = ContentApplication::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
}
public function actionAddtranslation($id) {
$modeltranslation = new ContentApplicationTranslation();
$modeltranslation->application_translation_parent_id = $id;
$modeltranslation->application_translation_main = 0;
$modeltranslation->created_at = date("Y-m-d H:i:s");
$modeltranslation->created_by = Yii::$app->user->identity->id;
$modeltranslation->updated_at = date("Y-m-d H:i:s");
$modeltranslation->updated_by = Yii::$app->user->identity->id;
if ($modeltranslation->save(false))
return $this->redirect(['update', 'id' => $id]);
}
/* public function actionAddtranslation($no = '') {
$output = '<div class="ibox float-e-margins" id="new-translation-' . $no . '">
<input type="hidden" name="newtranslation[]" value="' . $no . '">
<div class="ibox-title">
<button type="button" class="btn btn-primary btn-sm" onclick="deletenewtranslation(' . $no . ')">Delete</button>
New Application Translation
</div>
<div class="ibox-content">
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<label>Language</label>
' . Html::dropDownList('new_language_' . $no, NULL, ArrayHelper::map(
(new \yii\db\Query())->select(['code', 'descr'])
->from('ref')
->where(['=', 'cat', 'CONTENT_LANGUAGE'])
->orderBy(['sort' => SORT_ASC])
->all(), 'code', 'descr'), [
'class' => 'form-control',]) . '
</div>
</div>
<div class="col-sm-9"><div class="form-group"><label>Application Name</label><input type="text" class="form-control" name="new_title_' . $no . '"></div></div>
</div>
</div>
</div>';
return $output;
} */
public function actionDeletetranslation($id) {
if (($model = ContentApplicationTranslation::findOne($id)) !== null) {
$model->delete();
}
}
public function actionSorting($data = '') {
$count = 0;
$data_arr = explode(',', $data);
if (!empty($data_arr)) {
foreach ($data_arr as $application_id) {
$model = ContentApplication::findOne($application_id);
if (!empty($model)) {
$count++;
$model->application_sort = $count;
$model->save(false);
}
}
}
return $data;
}
public function actionGetsubcategory($parent_code = '') {
$output = '<option value="">-- Please choose --</option>';
if (($model2 = Ref::findOne(['code' => $parent_code, 'cat' => 'APP_CAT'])) !== null) {
$model = Ref::find()->where(['param1' => $model2->id])->orderBy(['descr_en' => SORT_ASC])->all();
if (!empty($model)) {
foreach ($model as $row) {
$output .= '<option value="' . $row->code . '">' . $row->descr_en . '</option>';
}
}
}
return $output;
}
public function actionOsc() {
$searchModel = new ContentOnlineservicesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$model = new ContentOnlineservices();
if ($model->load(Yii::$app->request->post())) {
$model->image_upload_s = UploadedFile::getInstance($model, 'image_upload_s');
if (!empty($model->image_upload_s)) {
$image_upload_s = 'images/modules/onlineservices' . date("YmdHis") . '.' . $model->image_upload_s->extension;
if ($model->image_upload_s->saveAs($image_upload_s))
$model->image_upload_s = $image_upload_s;
}
$model->save(false);
return $this->redirect(['osc']);
}
return $this->render('osc', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model
]);
}
public function actionUpdateosc($id) {
$model = ContentOnlineservices::find()->where(['id' => $id])->one();
$original_image_upload_s = $model->image_upload_s;
if ($model->load(Yii::$app->request->post())) {
$model->image_upload_s = UploadedFile::getInstance($model, 'image_upload_s');
if (!empty($model->image_upload_s)) {
$image_upload_s = 'images/modules/onlineservices' . date("YmdHis") . '.' . $model->image_upload_s->extension;
if ($model->image_upload_s->saveAs($image_upload_s))
$model->image_upload_s = $image_upload_s;
} else {
$model->image_upload_s = $original_image_upload_s;
}
$model->save(false);
return $this->redirect(['osc']);
}
return $this->render('updateosc', [
'model' => $model
]);
}
public function actionDeleteosc($id) {
$model = ContentOnlineservices::find()->where(['id' => $id])->one();
$model->delete();
return $this->redirect(['osc']);
}
}