Mettre en place un système permettant de définir un accès restreint pour les points de vente. Utile pour les amaps et autres points de vente dédié à un certain groupe de personnes.master
@@ -10,6 +10,7 @@ use yii\web\Controller; | |||
use yii\web\NotFoundHttpException; | |||
use yii\filters\VerbFilter; | |||
use common\models\User; | |||
use common\models\PointVenteUser ; | |||
/** | |||
* PointVenteController implements the CRUD actions for PointVente model. | |||
@@ -80,11 +81,12 @@ class PointVenteController extends BackendController | |||
if ($model->load(Yii::$app->request->post()) && $model->save()) { | |||
$model->gestionPointFabrication() ; | |||
$model->gestionAccesRestreint() ; | |||
return $this->redirect(['index']); | |||
} else { | |||
return $this->render('create', [ | |||
return $this->render('update', array_merge($this->initForm(),[ | |||
'model' => $model, | |||
]); | |||
])); | |||
} | |||
} | |||
@@ -96,17 +98,35 @@ class PointVenteController extends BackendController | |||
*/ | |||
public function actionUpdate($id) | |||
{ | |||
$model = $this->findModel($id); | |||
$model = PointVente::find() | |||
->with('pointVenteUser') | |||
->where(['id' => $id]) | |||
->one() ; | |||
foreach($model->pointVenteUser as $u) | |||
{ | |||
$model->users[] = $u->id_user ; | |||
} | |||
if ($model->load(Yii::$app->request->post()) && $model->save()) { | |||
$model->gestionPointFabrication() ; | |||
$model->gestionAccesRestreint() ; | |||
return $this->redirect(['index']); | |||
} else { | |||
return $this->render('update', [ | |||
return $this->render('update', array_merge($this->initForm($id),[ | |||
'model' => $model, | |||
]); | |||
])); | |||
} | |||
} | |||
public function initForm($id = 0) | |||
{ | |||
$users = User::find()->orderBy('nom ASC')->all() ; | |||
return [ | |||
'users' => $users | |||
] ; | |||
} | |||
/** | |||
* Deletes an existing PointVente model. | |||
@@ -117,6 +137,7 @@ class PointVenteController extends BackendController | |||
public function actionDelete($id) | |||
{ | |||
$this->findModel($id)->delete(); | |||
PointVenteUser::deleteAll(['id_point_vente' => $id]) ; | |||
return $this->redirect(['index']); | |||
} |
@@ -2,6 +2,7 @@ | |||
use yii\helpers\Html; | |||
use yii\widgets\ActiveForm; | |||
use yii\helpers\ArrayHelper ; | |||
/* @var $this yii\web\View */ | |||
/* @var $model backend\models\PointVente */ | |||
@@ -12,21 +13,32 @@ use yii\widgets\ActiveForm; | |||
<?php $form = ActiveForm::begin(); ?> | |||
<?= $form->field($model, 'point_fabrication')->checkbox()->hint('Cochez cette case si ce point de vente correspond à votre lieu de production.') ?> | |||
<?= $form->field($model, 'nom')->textInput(['maxlength' => 255]) ?> | |||
<?= $form->field($model, 'localite')->textInput(['maxlength' => 255]) ?> | |||
<?= $form->field($model, 'adresse')->textarea(['rows' => 6]) ?> | |||
<div class="col-md-8"> | |||
<?= $form->field($model, 'point_fabrication')->checkbox()->hint('Cochez cette case si ce point de vente correspond à votre lieu de production.') ?> | |||
<?= $form->field($model, 'nom')->textInput(['maxlength' => 255]) ?> | |||
<?= $form->field($model, 'localite')->textInput(['maxlength' => 255]) ?> | |||
<?= $form->field($model, 'adresse')->textarea(['rows' => 6]) ?> | |||
<h2>Horaires</h2> | |||
<div class="alert alert-info">Laisser vide si le point de vente est fermé</div> | |||
<?= $form->field($model, 'horaires_lundi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_mardi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_mercredi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_jeudi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_vendredi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_samedi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_dimanche')->textarea(['rows' => 3]) ?> | |||
</div> | |||
<div class="col-md-4"> | |||
<?= $form->field($model, 'acces_restreint')->checkbox()->hint('Cochez cette case si seulement un groupe restreint d\'utilisateurs peuvent accéder à ce point de vente.') ?> | |||
<div id="users"> | |||
<?= Html::activeCheckboxList($model, 'users', ArrayHelper::map($users, 'id', function($model, $defaultValue) { | |||
return Html::encode($model->nom.' '.$model->prenom) ; | |||
} )) ?> | |||
</div> | |||
</div> | |||
<div class="clr"></div> | |||
<h2>Horaires</h2> | |||
<div class="alert alert-info">Laisser vide si le point de vente est fermé</div> | |||
<?= $form->field($model, 'horaires_lundi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_mardi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_mercredi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_jeudi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_vendredi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_samedi')->textarea(['rows' => 3]) ?> | |||
<?= $form->field($model, 'horaires_dimanche')->textarea(['rows' => 3]) ?> | |||
<div class="form-group"> | |||
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> | |||
</div> |
@@ -16,6 +16,7 @@ $this->params['breadcrumbs'][] = 'Ajouter'; | |||
<?= $this->render('_form', [ | |||
'model' => $model, | |||
'users' => $users, | |||
]) ?> | |||
</div> |
@@ -2,6 +2,7 @@ | |||
use yii\helpers\Html; | |||
use yii\grid\GridView; | |||
use common\models\PointVenteUser ; | |||
/* @var $this yii\web\View */ | |||
/* @var $dataProvider yii\data\ActiveDataProvider */ | |||
@@ -32,6 +33,28 @@ $this->params['breadcrumbs'][] = $this->title; | |||
} | |||
], | |||
[ | |||
'attribute' => 'acces_restreint', | |||
'format' => 'raw', | |||
'value' => function($model) { | |||
$count = PointVenteUser::find()->where(['id_point_vente' => $model->id])->count(); | |||
if($model->acces_restreint) | |||
{ | |||
$html = '<span class="glyphicon glyphicon-lock"></span> ' ; | |||
if($count == 1) | |||
{ | |||
$html .= '1 utilisateur' ; | |||
} | |||
else { | |||
$html .= $count.' utilisateurs' ; | |||
} | |||
return $html ; | |||
} | |||
else { | |||
return '' ; | |||
} | |||
} | |||
], | |||
[ | |||
'class' => 'yii\grid\ActionColumn', | |||
'template' => '{update} {delete}', |
@@ -16,6 +16,7 @@ $this->params['breadcrumbs'][] = 'Modifier'; | |||
<?= $this->render('_form', [ | |||
'model' => $model, | |||
'users' => $users | |||
]) ?> | |||
</div> |
@@ -876,3 +876,16 @@ a:hover, a:focus, a:active { | |||
.commandeauto-form .produits .quantite { | |||
text-align: center; | |||
} | |||
/* points de vente */ | |||
/* line 883, ../sass/screen.scss */ | |||
.point-vente-form #pointvente-users { | |||
display: none; | |||
height: 500px; | |||
overflow-y: scroll; | |||
} | |||
/* line 887, ../sass/screen.scss */ | |||
.point-vente-form #pointvente-users label { | |||
font-weight: normal; | |||
display: block; | |||
} |
@@ -9,11 +9,27 @@ $(document).ready(function() { | |||
chat_index_commandes_points_vente() ; | |||
chat_btn_plus_moins() ; | |||
chat_commandeauto() ; | |||
chat_points_vente_acces() ; | |||
// admin | |||
chat_select_etablissement() ; | |||
}) ; | |||
function chat_points_vente_acces() { | |||
$('#pointvente-acces_restreint').change(function() { | |||
chat_points_vente_acces_event() ; | |||
}) ; | |||
chat_points_vente_acces_event() ; | |||
} | |||
function chat_points_vente_acces_event() { | |||
if($('#pointvente-acces_restreint').prop('checked')) { | |||
$('#pointvente-users').fadeIn() ; | |||
} | |||
else { | |||
$('#pointvente-users').hide() ; | |||
} | |||
} | |||
function chat_select_etablissement() { | |||
$('select[name="select_etablissement"]').change(function() { | |||
window.location.href = 'index.php?r=site/change-etablissement&id='+$(this).val() ; |
@@ -875,7 +875,18 @@ a { | |||
text-align: center ; | |||
} | |||
} | |||
} | |||
} | |||
/* points de vente */ | |||
.point-vente-form { | |||
#pointvente-users { | |||
display: none ; | |||
height: 500px ; | |||
overflow-y: scroll ; | |||
label { | |||
font-weight: normal ; | |||
display: block ; | |||
} | |||
} | |||
} |
@@ -4,6 +4,7 @@ namespace common\models; | |||
use Yii; | |||
use yii\helpers\Html ; | |||
use common\models\PointVenteUser ; | |||
/** | |||
* This is the model class for table "point_vente". | |||
@@ -22,6 +23,7 @@ class PointVente extends \yii\db\ActiveRecord | |||
var $recettes_vrac = 0 ; | |||
var $data_select_commandes ; | |||
var $data_options_commandes ; | |||
var $users = [] ; | |||
/** | |||
* @inheritdoc | |||
@@ -38,12 +40,14 @@ class PointVente extends \yii\db\ActiveRecord | |||
{ | |||
return [ | |||
[['nom'], 'required'], | |||
[['acces_restreint'], 'boolean'], | |||
[['nom'], 'string', 'max' => 255], | |||
[['adresse','localite','horaires_lundi','horaires_mardi','horaires_mercredi','horaires_jeudi','horaires_vendredi','horaires_samedi','horaires_dimanche'], 'string'], | |||
[['point_fabrication','vrac','pain'], 'boolean'], | |||
['point_fabrication', 'default','value'=>0], | |||
['id_etablissement','integer'], | |||
['id_etablissement','required'], | |||
['users','safe'] | |||
]; | |||
} | |||
@@ -67,9 +71,15 @@ class PointVente extends \yii\db\ActiveRecord | |||
'horaires_dimanche' => 'Dimanche', | |||
'vrac' => 'Livraison de vrac', | |||
'pain' => 'Livraison de pain', | |||
'acces_restreint' => 'Accès restreint' | |||
]; | |||
} | |||
public function getPointVenteUser() | |||
{ | |||
return $this->hasMany(PointVenteUser::className(), ['id_point_vente'=>'id']) ; | |||
} | |||
public function initCommandes($commandes) { | |||
$this->commandes = [] ; | |||
@@ -124,4 +134,21 @@ class PointVente extends \yii\db\ActiveRecord | |||
$this->save() ; | |||
} | |||
} | |||
public function gestionAccesRestreint() | |||
{ | |||
PointVenteUser::deleteAll(['id_point_vente' => $this->id]) ; | |||
foreach($this->users as $key => $val) | |||
{ | |||
$user = User::findOne($val) ; | |||
if($user) | |||
{ | |||
$point_vente_user = new PointVenteUser ; | |||
$point_vente_user->id_user = $val ; | |||
$point_vente_user->id_point_vente = $this->id ; | |||
$point_vente_user->save() ; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,44 @@ | |||
<?php | |||
namespace common\models; | |||
use Yii; | |||
/** | |||
* This is the model class for table "point_vente_user". | |||
* | |||
* @property integer $id_point_vente | |||
* @property integer $id_user | |||
*/ | |||
class PointVenteUser extends \yii\db\ActiveRecord | |||
{ | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public static function tableName() | |||
{ | |||
return 'point_vente_user'; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function rules() | |||
{ | |||
return [ | |||
[['id_point_vente', 'id_user'], 'required'], | |||
[['id_point_vente', 'id_user'], 'integer'], | |||
]; | |||
} | |||
/** | |||
* @inheritdoc | |||
*/ | |||
public function attributeLabels() | |||
{ | |||
return [ | |||
'id_point_vente' => 'Id Point Vente', | |||
'id_user' => 'Id User', | |||
]; | |||
} | |||
} |
@@ -0,0 +1,24 @@ | |||
<?php | |||
use yii\db\Migration; | |||
use yii\db\Schema; | |||
class m161205_125942_point_vente_acces_restreint extends Migration | |||
{ | |||
public function up() | |||
{ | |||
$this->addColumn('point_vente', 'acces_restreint', Schema::TYPE_BOOLEAN.' DEFAULT 0') ; | |||
$this->createTable('point_vente_user', [ | |||
'id_point_vente' => Schema::TYPE_INTEGER.' NOT NULL', | |||
'id_user' => Schema::TYPE_INTEGER.' NOT NULL', | |||
]); | |||
$this->addPrimaryKey('point_vente_user_pk', 'point_vente_user', ['id_point_vente', 'id_user']); | |||
} | |||
public function down() | |||
{ | |||
$this->dropColumn('point_vente', 'acces_restreint') ; | |||
$this->dropTable('point_vente_user') ; | |||
} | |||
} |
@@ -68,6 +68,8 @@ class CommandeController extends \yii\web\Controller { | |||
// points de vente | |||
$points_vente = PointVente::find() | |||
->where(['id_etablissement'=>$id_etablissement]) | |||
->andWhere('acces_restreint = 0 OR (acces_restreint = 1 AND (SELECT COUNT(*) FROM point_vente_user WHERE point_vente.id = point_vente_user.id_point_vente AND point_vente_user.id_user = :id_user) > 0)') | |||
->params([':id_user' => Yii::$app->user->identity->id]) | |||
->all(); | |||
$arr_points_vente = $points_vente; | |||