| Current Path : /var/www/html/hr/controllers/ |
| Current File : /var/www/html/hr/controllers/UserController.php |
<?php
namespace app\controllers;
use Yii;
use app\models\User;
use app\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use yii\helpers\Url;
use app\models\Menu;
use app\models\ParamList;
use app\models\Log;
use app\models\LogSearch;
use yii\helpers\ArrayHelper;
use yii\data\ArrayDataProvider;
use yii\web\UploadedFile;
class UserController extends Controller {
public $menu_id = 8;
public function behaviors() {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => \yii\filters\AccessControl::className(),
'only' => ['index', 'create', 'update', 'view'],
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
];
}
public function actionIndex() {
$searchModel = new UserSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'menu_id' => $this->menu_id]);
}
public function actionView($uuid, $show = '') {
$data = [];
$data['tab1'] = '';
$data['tab2'] = '';
$data['tab3'] = '';
$data['tab4'] = '';
$data['tab5'] = '';
$data['tab6'] = '';
$data['tab7'] = '';
$data['tab8'] = '';
if ($show == 'tab1' || $show == '')
$data['tab1'] = 'active';
if ($show == 'tab2')
$data['tab2'] = 'active';
if ($show == 'tab3')
$data['tab3'] = 'active';
if ($show == 'tab4')
$data['tab4'] = 'active';
if ($show == 'tab5')
$data['tab5'] = 'active';
if ($show == 'tab6')
$data['tab6'] = 'active';
if ($show == 'tab7')
$data['tab7'] = 'active';
if ($show == 'tab8')
$data['tab8'] = 'active';
return $this->render('view', ['model' => $this->findModel($uuid), 'menu_id' => $this->menu_id, 'data' => $data]);
}
public function actionCreate() {
$model = new User();
$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;
$model->uuid = Uuid::uuid4();
$model->status = 1;
$pwd = Yii::$app->security->generateRandomString(8);
$model->pwd = \Yii::$app->security->generatePasswordHash($pwd);
$model->auth_key = \Yii::$app->security->generateRandomString();
$model->role = 'staff';
$model->race = 'malay';
$model->religion = 'Islam';
$model->nationality = 'MY';
$model->staff_start = date("Y-m-d");
$model->staff_location = 'SGR';
if ($model->load(Yii::$app->request->post())) {
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'create', $model->uuid);
$ParamList = new ParamList();
//send email
$email_content = '<html>
<body>
<div style="font-family: \'Century Gothic\', CenturyGothic, AppleGothic, sans-serif; font-size: 14px;background-color:rgb(234,234,234);padding:2em 0;">
<div style="width:70%;background-color:white;margin:0 auto;padding:5em;border:1px solid #ccc;">
<p>Hi ' . $model->name . ',</p>
<p>Welcome to ' . Yii::$app->params['systemName'] . '!</p>
<p>You now can access the system as a ' . $ParamList->getlabel(2, $model->role) . ' using below details:</p>
<p>URL: <a href="' . Url::base(true) . '" target="_blank">' . Url::base(true) . '</a></p>
<p>Email: ' . $model->email . '</p>
<p>Password: ' . $pwd . '</p>
<p>- Admin -</p>
</div>
</div>
</body>
</html>';
/*Yii::$app->mailer->compose()
->setFrom(Yii::$app->params['senderEmail'])
->setTo($model->email)
->setSubject('HR System Login Details')
->setHtmlBody($email_content)
->send();*/
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
mail($model->email, 'HR System Login Details', $email_content, $headers);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'User have been created and login details have been send to user email',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=user/view&uuid=" . $model->uuid . "';});");
}
}
return $this->render('create', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdatepersonal($uuid) {
$model = $this->findModel($uuid);
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$original_img = $model->img_url;
if ($model->load(Yii::$app->request->post())) {
$model->img_url = UploadedFile::getInstance($model, 'img_url');
if (!empty($model->img_url)) {
$file_name = date("YmdHis") . '.' . $model->img_url->extension;
if ($model->img_url->saveAs('uploads/user_images/' . $file_name))
$model->img_url = $file_name;
} else {
$model->img_url = $original_img;
}
if ($model->save()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data have been updated',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=user/view&uuid=" . $model->uuid . "';});");
}
}
return $this->render('updatepersonal', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdateemployment($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()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data have been updated',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=user/view&uuid=" . $model->uuid . "&show=tab2';});");
}
}
return $this->render('updateemployment', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdateaddress($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()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data have been updated',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=user/view&uuid=" . $model->uuid . "&show=tab3';});");
}
}
return $this->render('updateaddress', ['model' => $model, 'menu_id' => $this->menu_id]);
}
public function actionUpdatespouse($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()) {
$Log = new Log();
$Log->insertlog($this->menu_id, 'update', $model->uuid);
$this->getView()->registerJs("swal.fire({position: 'center-center',type: 'success',title: 'Data have been updated',showConfirmButton: false,timer: 1500,}).then(function(result){window.location = 'index.php?r=user/view&uuid=" . $model->uuid . "&show=tab5';});");
}
}
return $this->render('updatespouse', ['model' => $model, 'menu_id' => $this->menu_id]);
}
protected function findModel($uuid) {
if (($model = User::findOne(['uuid' => $uuid])) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
public function actionChangerole() {
$role = $_POST['role'];
$Menu = new Menu();
return $Menu->getmenuforuser($parent = 0, $count = 1, $level = 0, $user_id = 0, $checked_list = [], $role);
}
public function actionProfile() {
if (($model = User::findOne(Yii::$app->user->identity->id)) !== null) {
if ($model->load(Yii::$app->request->post())) {
if ($model->save(false)) {
$Log = new Log();
$Log->insertlog('8', 'update profile', $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('user/profile') . "';});");
}
}
return $this->render('profile', ['model' => $model]);
}
}
public function actionUpdatepassword() {
if (($model = User::findOne(Yii::$app->user->identity->id)) !== null) {
return $this->render('updatepassword', ['model' => $model]);
}
}
public function actionChangepwd($new_pwd) {
$output = "";
if (($model = User::findOne(Yii::$app->user->identity->id)) !== null) {
$model->updated_dt = date("Y-m-d H:i:s");
$model->updated_by = Yii::$app->user->identity->id;
$model->pwd = \Yii::$app->security->generatePasswordHash($new_pwd);
if ($model->save(false)) {
$Log = new Log();
$Log->insertlog('8', 'update password', $model->id);
$output = 'success';
} else
$output = 'failed';
}
return $output;
}
}