| Current Path : /var/www/html/dosm/backend/controllers/ |
| Current File : /var/www/html/dosm/backend/controllers/ContentLifeController.php |
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\components\AccessRule;
use backend\models\LifeExpectancy;
use backend\models\LifeExpectancySearch;
use backend\models\LifeExpectancyRange;
use backend\models\LifeExpectancyRangeSearch;
use backend\models\LifeExpectancyGender;
use backend\models\LifeExpectancyGenderSearch;
use backend\models\LifeExpectancyEthnic;
use backend\models\LifeExpectancyEthnicSearch;
use backend\models\Ref;
use yii\helpers\Html;
use yii\web\UploadedFile;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/**
* ContentAnnouncementController implements the CRUD actions for ContentAnnouncement model.
*/
class ContentLifeController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'ruleConfig' => [
'class' => AccessRule::className(),
],
'rules' => [
[
'actions' => ['updatelife', 'index', 'create', 'update', 'view', 'delete00000', 'demo', 'delete1', 'maklumat_jantina', 'delete_jantina', 'maklumat_ethnic', 'delete_ethnic', 'addnewtranslation', 'uploaddata'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all ContentAnnouncement models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new LifeExpectancySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$model = new LifeExpectancy();
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model
]);
}
public function actionCreate()
{
$model = new LifeExpectancy();
if (isset($_POST['new_themes_translation_no']) && !empty($_POST['new_themes_translation_no'])) {
foreach ($_POST['new_themes_translation_no'] as $new_themes_translation_no) {
$modelAddTranslation = new LifeExpectancy();
$modelAddTranslation->age_from = $_POST['age_from_' . $new_themes_translation_no];
$modelAddTranslation->age_until = $_POST['age_until_' . $new_themes_translation_no];
$modelAddTranslation->jantina = $_POST['jantina_' . $new_themes_translation_no];
$modelAddTranslation->ethnic = $_POST['ethnic_' . $new_themes_translation_no];
$modelAddTranslation->life_expectancy = $_POST['life_expectancy_' . $new_themes_translation_no];
$modelAddTranslation->created_at = date("Y-m-d H:i:s");
$modelAddTranslation->updated_by = Yii::$app->user->identity->id;
$modelAddTranslation->save(false);
}
return $this->redirect(['index']);
}
return $this->render('create_form', [
'model' => $model
]);
}
public function actionDelete00000($id)
{
$checking = LifeExpectancy::find()->where(['id' => $id])->one();
$checking->delete();
Yii::$app->session->setFlash('success', "Data berjaya dibuang");
return $this->redirect(['index']);
}
public function actionDemo()
{
$searchModel = new LifeExpectancySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$model = new LifeExpectancy();
if ($model->load(Yii::$app->request->post())) {
if (isset($_POST['act']) && $_POST['act'] === 'Simpan') {
$kaum = $_POST['LifeExpectancy']['kaum'];
$jantina = $_POST['LifeExpectancy']['jantina'];
$tl_day = $_POST['LifeExpectancy']['kpd'];
$tl_month = $_POST['LifeExpectancy']['kpm'];
$kpd = $_POST['LifeExpectancy']['kpd'];
$kpm = $_POST['LifeExpectancy']['kpm'];
$kpy = $_POST['LifeExpectancy']['kpy'];
//start formula
if ($tl_month == '12') {
if ($tl_day > 30) {
$age = date('Y') - $kpy - 1;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy - 1;
} else {
$age = date('Y') - $kpy;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy;
}
} else {
if ($tl_month > 12) {
$age = date('Y') - $kpy - 1;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy - 1;
}
if ($tl_month < 12) {
$age = date('Y') - $kpy;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy;
}
}
$query = new \yii\db\Query();
$query->select('age_from, age_until, jantina, ethnic, life_expectancy')
->from('life_expectancy_range_age')
->leftJoin('life_expectancy_gender', ' life_expectancy_range_age.id = life_expectancy_gender.id_range')
->innerJoin('life_expectancy_ethnic', ' life_expectancy_gender.id = life_expectancy_ethnic.id_gender');
$command = $query->createCommand();
$checkingFormula = $command->queryAll();
foreach ($checkingFormula as $cf) {
if ($newage >= $cf['age_from'] && $newage <= $cf['age_until']) {
if ($kaum == $cf['ethnic'] && $jantina == $cf['jantina']) {
$life_expectancy = $cf['life_expectancy'];
}
}
}
$model->kpd = $kpd;
$model->kpm = $kpm;
$model->kpy = $kpy;
$model->jantina = $jantina;
$model->dob = $kpd . "/" . $kpm . "/" . $kpy;
$model->age = $newage;
$model->your_life_expectancy = number_format($life_expectancy, 1);
$model->created_at = date('Y-m-d H:i:s');
$model->save(false);
Yii::$app->session->setFlash('success', "Data berjaya disimpan");
return $this->redirect(['demo']);
//end formula
} elseif (isset($_POST['act']) && $_POST['act'] === 'Kemaskini') {
$id_life = $_POST['id_life'];
$checking = LifeExpectancy::find()->where(['id' => $id_life])->one();
$kaum = $_POST['LifeExpectancy']['kaum'];
$jantina = $_POST['LifeExpectancy']['jantina'];
$tl_day = $_POST['LifeExpectancy']['kpd'];
$tl_month = $_POST['LifeExpectancy']['kpm'];
$kpd = $_POST['LifeExpectancy']['kpd'];
$kpm = $_POST['LifeExpectancy']['kpm'];
$kpy = $_POST['LifeExpectancy']['kpy'];
//start formula
if ($tl_month == '12') {
if ($tl_day > 30) {
$age = date('Y') - $kpy - 1;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy - 1;
} else {
$age = date('Y') - $kpy;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy;
}
} else {
if ($tl_month > 12) {
$age = date('Y') - $kpy - 1;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy - 1;
}
if ($tl_month < 12) {
$age = date('Y') - $kpy;
if ($age >= 81) {
$newage = 80;
}
$newage = date('Y') - $kpy;
}
}
$query = new \yii\db\Query();
$query->select('age_from, age_until, jantina, ethnic, life_expectancy')
->from('life_expectancy_range_age')
->leftJoin('life_expectancy_gender', ' life_expectancy_range_age.id = life_expectancy_gender.id_range')
->innerJoin('life_expectancy_ethnic', ' life_expectancy_gender.id = life_expectancy_ethnic.id_gender');
$command = $query->createCommand();
$checkingFormula = $command->queryAll();
foreach ($checkingFormula as $cf) {
if ($newage >= $cf['age_from'] && $newage <= $cf['age_until']) {
if ($kaum == $cf['kaum'] && $jantina == $cf['jantina']) {
$life_expectancy = $cf['your_life_expectancy'];
}
}
}
$checking->kpd = $kpd;
$checking->kpm = $kpm;
$checking->kpy = $kpy;
$checking->jantina = $jantina;
$checking->dob = $kpd . "/" . $kpm . "/" . $kpy;
$checking->age = $newage;
$checking->kaum = $kaum;
$checking->your_life_expectancy = number_format($life_expectancy, 1);
$checking->save(false);
Yii::$app->session->setFlash('success', "Data berjaya dikemaskini");
return $this->redirect(['demo']);
//end formula
}
}
return $this->render('demo', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model
]);
}
public function actionDelete1()
{
$id_life = $_POST['id_life1'];
$checking = LifeExpectancy::find()->where(['id' => $id_life])->one();
$checking->delete();
Yii::$app->session->setFlash('success', "Data berjaya dibuang");
return $this->redirect(['demo']);
}
public function actionMaklumat_jantina($id)
{
$searchModel = new LifeExpectancyGenderSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
$model = new LifeExpectancyGender();
if ($model->load(Yii::$app->request->post())) {
if (isset($_POST['act']) && $_POST['act'] === 'Simpan') {
$checking = LifeExpectancyGender::find()->where(['id_range' => $id])->all();
if (empty($checking)) {
$model->jantina = $_POST['LifeExpectancyGender']['jantina'];
$model->id_range = $id;
$model->updated_at = date('Y-m-d H:i:s');
$model->save(false);
Yii::$app->session->setFlash('success', "Data anda berjaya disimpan");
return $this->redirect(['maklumat_jantina?id=' . $id]);
} else {
foreach ($checking as $ck) {
if (($ck['jantina'] == "Male" && $_POST['LifeExpectancyGender']['jantina'] == "Male") || ($ck['jantina'] == "Female" && $_POST['LifeExpectancyGender']['jantina'] == "Female")) {
Yii::$app->session->setFlash('error', "Maaf, data anda masih wujud");
return $this->redirect(['maklumat_jantina?id=' . $id]);
}
}
$model->jantina = $_POST['LifeExpectancyGender']['jantina'];
$model->id_range = $id;
$model->updated_at = date('Y-m-d H:i:s');
$model->save(false);
Yii::$app->session->setFlash('success', "Data anda berjaya disimpan");
return $this->redirect(['maklumat_jantina?id=' . $id]);
}
} elseif (isset($_POST['act']) && $_POST['act'] === 'Kemaskini') {
$checkingExistinggender = LifeExpectancyGender::find()->where(['id_range' => $id, 'jantina' => $_POST['LifeExpectancyGender']['jantina']])->one();
if (empty($checkingExistinggender)) {
$id_life = $_POST['id_life'];
$checking = LifeExpectancyGender::find()->where(['id' => $id_life])->one();
$checking->jantina = $_POST['LifeExpectancyGender']['jantina'];
$checking->id_range = $id;
$checking->updated_at = date('Y-m-d H:i:s');
$checking->save(false);
Yii::$app->session->setFlash('success', "Data berjaya dikemaskini");
return $this->redirect(['maklumat_jantina?id=' . $id]);
} else {
Yii::$app->session->setFlash('error', "Maaf, data anda masih wujud");
return $this->redirect(['maklumat_jantina?id=' . $id]);
}
}
}
return $this->render('gender', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
'id' => $id
]);
}
public function actionDelete_jantina()
{
$id_life = $_POST['id_life1'];
$checking = LifeExpectancyGender::find()->where(['id' => $id_life])->one();
$checking->delete();
Yii::$app->session->setFlash('success', "Data berjaya dibuang");
return $this->redirect(['maklumat_jantina?id=' . $checking['id_range']]);
}
public function actionMaklumat_ethnic($id)
{
$searchModel = new LifeExpectancyEthnicSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
$model = new LifeExpectancyEthnic();
if ($model->load(Yii::$app->request->post())) {
if (isset($_POST['act']) && $_POST['act'] === 'Simpan') {
$checking = LifeExpectancyEthnic::find()->where(['id_gender' => $id])->all();
if (empty($checking)) {
$model->ethnic = $_POST['LifeExpectancyEthnic']['ethnic'];
$model->life_expectancy = $_POST['LifeExpectancyEthnic']['life_expectancy'];
$model->id_gender = $id;
$model->updated_at = date('Y-m-d H:i:s');
$model->save(false);
Yii::$app->session->setFlash('success', "Data anda berjaya disimpan");
return $this->redirect(['maklumat_ethnic?id=' . $id]);
} else {
$checking2 = LifeExpectancyEthnic::find()->where(['id_gender' => $id, 'ethnic' => $_POST['LifeExpectancyEthnic']['ethnic']])->one();
if (empty($checking2)) {
$model->ethnic = $_POST['LifeExpectancyEthnic']['ethnic'];
$model->life_expectancy = $_POST['LifeExpectancyEthnic']['life_expectancy'];
$model->id_gender = $id;
$model->updated_at = date('Y-m-d H:i:s');
$model->save(false);
Yii::$app->session->setFlash('success', "Data anda berjaya disimpan");
return $this->redirect(['maklumat_ethnic?id=' . $id]);
} else {
Yii::$app->session->setFlash('error', "Maaf, data anda masih wujud");
return $this->redirect(['maklumat_ethnic?id=' . $id]);
}
}
} elseif (isset($_POST['act']) && $_POST['act'] === 'Kemaskini') {
$id_life = $_POST['id_life'];
$checkingExistingEthnic = LifeExpectancyEthnic::find()->where(['id' => $id_life])->one();
$Ref = Ref::find()->where(['descr' => $checkingExistingEthnic['ethnic'], 'descr' => $_POST['LifeExpectancyEthnic']['ethnic']])->one();
if (!empty($Ref)) {
if ($checkingExistingEthnic['life_expectancy'] == $_POST['LifeExpectancyEthnic']['life_expectancy']) {
Yii::$app->session->setFlash('error', "Maaf, data anda masih wujud");
return $this->redirect(['maklumat_ethnic?id=' . $id]);
} else {
$checking = LifeExpectancyEthnic::find()->where(['id' => $id_life])->one();
$checking->life_expectancy = $_POST['LifeExpectancyEthnic']['life_expectancy'];
$checking->ethnic = $checkingExistingEthnic['ethnic'];
$checking->id_gender = $id;
$checking->updated_at = date('Y-m-d H:i:s');
$checking->save(false);
Yii::$app->session->setFlash('success', "Data berjaya dikemaskini");
return $this->redirect(['maklumat_ethnic?id=' . $id]);
}
}
}
}
return $this->render('ethnic', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
'id' => $id
]);
}
public function actionDelete_ethnic()
{
$id_life = $_POST['id_life1'];
$checking = LifeExpectancyEthnic::find()->where(['id' => $id_life])->one();
$checking->delete();
Yii::$app->session->setFlash('success', "Data berjaya dibuang");
return $this->redirect(['maklumat_ethnic?id=' . $checking['id_gender']]);
}
public function actionAddnewtranslation($count)
{
return '<div class="row" style="margin-top:1em;" id="new_translation_wrapper_' . $count . '">
' . Html::input('hidden', 'new_themes_translation_no[]', $count) . '
<div class="col-sm-3">' . Html::input('text', 'age_from_' . $count, '', $options = ['class' => 'form-control', 'placeholder' => 'Age From']) . '</div>
<div class="col-sm-3">' . Html::input('text', 'age_until_' . $count, '', $options = ['class' => 'form-control', 'placeholder' => 'Age Until']) . '</div>
<div class="col-sm-3">' . Html::dropDownList('jantina_' . $count, null, ['Male' => 'Male', 'Female' => 'Female'], ['class' => 'form-control', 'prompt' => '-- Gender --']) . '</div>
<div class="col-sm-3">' . Html::dropDownList('ethnic_' . $count, null, Ref::getEthnic(), ['class' => 'form-control', 'prompt' => '-- Ethnic --']) . '</div>
<div class="col-sm-3">' . Html::input('text', 'life_expectancy_' . $count, '', $options = ['class' => 'form-control', 'placeholder' => 'Life Of Expectancy']) . '</div>
<div class="col-sm-3">' . Html::button('Delete', ['class' => 'btn btn-danger btn-block', 'onclick' => 'deletenewtranslation(' . $count . ')', 'style' => 'width:100px']) . '</div>
</div>
';
}
public function actionUploaddata()
{
$model1 = new LifeExpectancy();
$model1->file_url = UploadedFile::getInstance($model1, 'file_url');
if (!empty($model1->file_url)) {
$file_url = date("YmdHis") . '.' . $model1->file_url->extension;
if ($model1->file_url->saveAs('../uploads/lifeexpectancy/' . $file_url))
$model1->file_url = $file_url;
}
$inputFileType = 'Xlsx';
$inputFileName = '../uploads/lifeexpectancy/' . $file_url;
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$checking = LifeExpectancy::find()->all();
foreach ($checking as $ck) {
$ck->delete();
}
foreach ($sheetData as $dataRow) {
$model = new LifeExpectancy();
$model->age_from = $dataRow['A'];
$model->age_until = $dataRow['B'];
$model->jantina = $dataRow['C'];
$model->ethnic = $dataRow['D'];
$model->life_expectancy = $dataRow['E'];
$model->created_at = date('Y-m-d H:i:s');
$model->updated_by = Yii::$app->user->identity->id;
$model->save(false);
}
Yii::$app->session->setFlash('success', "Data berjaya dimuat naik");
return $this->redirect(['index']);
}
public function actionUpdatelife()
{
$id = $_POST['id_life'];
$checking = LifeExpectancy::find()->where(['id' => $id])->one();
$checking->age_from = $_POST['LifeExpectancy']['age_from'];
$checking->age_until = $_POST['LifeExpectancy']['age_until'];
$checking->jantina = $_POST['LifeExpectancy']['jantina'];
$checking->ethnic = $_POST['LifeExpectancy']['ethnic'];
$checking->life_expectancy = $_POST['LifeExpectancy']['life_expectancy'];
$checking->updated_by = Yii::$app->user->identity->id;
$checking->save(false);
Yii::$app->session->setFlash('success', "Data berjaya dikemaskini");
return $this->redirect(['index']);
}
}