-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release early version containing basic features
- Loading branch information
Showing
28 changed files
with
1,399 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
<?php | ||
|
||
namespace backend\controllers; | ||
|
||
use Yii; | ||
use common\models\User; | ||
use common\models\UserSearch; | ||
use yii\web\Controller; | ||
use yii\web\NotFoundHttpException; | ||
use yii\filters\VerbFilter; | ||
use yii\filters\AccessControl; | ||
|
||
/** | ||
* UserController implements the CRUD actions for User model. | ||
*/ | ||
class UserController extends Controller | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function behaviors() | ||
{ | ||
return [ | ||
'access' => [ | ||
'class' => AccessControl::className(), | ||
'rules' => [ | ||
[ | ||
'actions' => ['index', 'view', 'edit', 'create', 'update', 'delete', 'multipledelete', 'profile'], | ||
'allow' => true, | ||
'roles' => ['@'], | ||
'matchCallback' => function ($rule, $action) | ||
{ | ||
return | ||
Yii::$app->user->identity['role'] == User::ROLE_SUPERADMIN || | ||
Yii::$app->user->identity['role'] == User::ROLE_ADMIN; | ||
} | ||
], | ||
], | ||
], | ||
'verbs' => [ | ||
'class' => VerbFilter::className(), | ||
'actions' => [ | ||
'delete' => ['POST'], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Lists all User models. | ||
* @return mixed | ||
*/ | ||
public function actionIndex() | ||
{ | ||
$searchModel = new UserSearch(); | ||
$dataProvider = $searchModel->search(Yii::$app->request->queryParams); | ||
|
||
return $this->render('index', [ | ||
'searchModel' => $searchModel, | ||
'dataProvider' => $dataProvider, | ||
]); | ||
} | ||
|
||
/** | ||
* Displays a single User model. | ||
* @param integer $id | ||
* @return mixed | ||
*/ | ||
public function actionView($id) | ||
{ | ||
return $this->render('view', [ | ||
'model' => $this->findModel($id), | ||
]); | ||
} | ||
|
||
/** | ||
* Creates a new User model. | ||
* If creation is successful, the browser will be redirected to the 'view' page. | ||
* @return mixed | ||
*/ | ||
public function actionCreate() | ||
{ | ||
$model = new User(); | ||
|
||
if ($model->load(Yii::$app->request->post())) | ||
{ | ||
if (strlen($model->password) < 6) | ||
{ | ||
$model->addError('password', 'Password should contain at least 6 characters.'); | ||
return $this->render('create', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
$model->setPassword($model->password); | ||
$model->generateAuthKey(); | ||
|
||
if ($model->save()) | ||
return $this->redirect(['view', 'id' => $model->id]); | ||
else | ||
{ | ||
return $this->render('create', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
} | ||
else | ||
return $this->render('create', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
/** | ||
* Updates an existing User model. | ||
* If update is successful, the browser will be redirected to the 'view' page. | ||
* @param integer $id | ||
* @return mixed | ||
*/ | ||
public function actionUpdate($id) | ||
{ | ||
$model = $this->findModel($id); | ||
|
||
if (Yii::$app->request->post()) | ||
{ | ||
$post_user = Yii::$app->request->post('User'); | ||
|
||
$model->username = $post_user['username']; | ||
$model->email = $post_user['email']; | ||
$model->role = $post_user['role']; | ||
$model->status = $post_user['status']; | ||
|
||
// update password | ||
if ( ! empty($post_user['password'])) | ||
{ | ||
if (strlen($model->password) < 6) | ||
{ | ||
$model->addError('password', 'Password should contain at least 6 characters.'); | ||
return $this->render('update', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
$model->setPassword($post_user['password']); | ||
} | ||
|
||
if ($model->save()) | ||
return $this->redirect(['view', 'id' => $model->id]); | ||
else | ||
return $this->render('update', ['model' => $model,]); | ||
} | ||
else | ||
return $this->render('update', ['model' => $model]); | ||
} | ||
|
||
/** | ||
* Updates the logged in user's profile | ||
* @param boolean $success | ||
* @return mixed | ||
*/ | ||
public function actionProfile($success = false) | ||
{ | ||
$model = $this->findModel(Yii::$app->user->id); | ||
|
||
if (Yii::$app->request->post()) | ||
{ | ||
$post_user = Yii::$app->request->post('User'); | ||
|
||
$model->username = $post_user['username']; | ||
|
||
// update password | ||
if ( ! empty($post_user['password'])) | ||
{ | ||
if (strlen($model->password) < 6) | ||
{ | ||
$model->addError('password', 'Password should contain at least 6 characters.'); | ||
return $this->render('update', [ | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
$model->setPassword($post_user['password']); | ||
} | ||
|
||
if ($model->save()) | ||
return $this->redirect(['profile', 'success' => true]); | ||
else | ||
return $this->render('profile', ['model' => $model]); | ||
} | ||
else | ||
return $this->render('profile', ['model' => $model, 'success' => $success]); | ||
} | ||
|
||
/** | ||
* Deletes an existing User model. | ||
* If deletion is successful, the browser will be redirected to the 'index' page. | ||
* @param integer $id | ||
* @return mixed | ||
*/ | ||
public function actionDelete($id) | ||
{ | ||
$this->findModel($id)->delete(); | ||
|
||
return $this->redirect(['index']); | ||
} | ||
|
||
/** | ||
* Delete multiplede IDs | ||
* @return mixed | ||
*/ | ||
public function actionMultipledelete() | ||
{ | ||
if (Yii::$app->request->isAjax) | ||
{ | ||
$selected_ids = Yii::$app->request->post('selectedItems'); | ||
foreach ($selected_ids as $id) | ||
$this->findModel($id)->delete(); | ||
} | ||
} | ||
|
||
/** | ||
* Finds the User model based on its primary key value. | ||
* If the model is not found, a 404 HTTP exception will be thrown. | ||
* @param integer $id | ||
* @return User the loaded model | ||
* @throws NotFoundHttpException if the model cannot be found | ||
*/ | ||
protected function findModel($id) | ||
{ | ||
if (($model = User::findOne($id)) !== null) { | ||
return $model; | ||
} else { | ||
throw new NotFoundHttpException('The requested page does not exist.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<script type="text/javascript"> | ||
$(document).ready(function (){ | ||
var selectedItems; | ||
|
||
$('#delete_selected_items_btn').click(function (){ | ||
selectedItems = $('.grid-view').yiiGridView('getSelectedRows'); | ||
|
||
if ( ! selectedItems.length) | ||
{ | ||
alert('Please select at least one item to be deleted'); | ||
return false; | ||
} | ||
|
||
if ( ! confirm('Are you sure to delete ' + selectedItems.length + ' items?')) return false; | ||
|
||
var multipledeleteUrl = "<?=Yii::$app->urlManager->createUrl(Yii::$app->controller->id . '/multipledelete');?>"; | ||
$.ajax({ | ||
type: "POST", | ||
url: multipledeleteUrl, | ||
data: {selectedItems : selectedItems}, | ||
success: (function (e){ | ||
$.pjax.reload({container : '#w0'}); | ||
selectedItems = []; | ||
|
||
}), | ||
error: (function (e) { | ||
alert("Can not delete selected items"); | ||
}) | ||
}); | ||
}) | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use common\models\User; | ||
use yii\helpers\Html; | ||
use yii\widgets\ActiveForm; | ||
|
||
/* @var $this yii\web\View */ | ||
/* @var $model common\models\User */ | ||
/* @var $form yii\widgets\ActiveForm */ | ||
?> | ||
|
||
<div class="user-form"> | ||
|
||
<?php $form = ActiveForm::begin(); ?> | ||
|
||
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?> | ||
|
||
<?= $form->field($model, 'email')->input('email', ['maxlength' => true]) ?> | ||
|
||
<?= $form->field($model, 'password')->passwordInput(['placeholder' => 'Leave this field blank if you do not want to change the password', 'minlength' => 6]) ?> | ||
|
||
<?= $form->field($model, 'role')->dropDownList(User::getRoleAsArray()); ?> | ||
|
||
<?= $form->field($model, 'status')->dropDownList(User::getStatusAsArray()); ?> | ||
|
||
<div class="form-group"> | ||
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | ||
</div> | ||
|
||
<?php ActiveForm::end(); ?> | ||
|
||
</div> |
Oops, something went wrong.