public static function slugify($string) | public static function slugify($string) | ||||
{ | { | ||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string))); | |||||
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '', $string))); | |||||
} | } | ||||
} | } |
*/ | */ | ||||
public function actionProducerCode($id) | public function actionProducerCode($id) | ||||
{ | { | ||||
$producer = Producer::searchOne($id); | |||||
$producer = Producer::findOne($id); | |||||
if(!$producer) { | if(!$producer) { | ||||
throw new \yii\web\HttpException(404, 'Producteur introuvable'); | throw new \yii\web\HttpException(404, 'Producteur introuvable'); |
['code', function($attribute, $params) { | ['code', function($attribute, $params) { | ||||
$code = $this->$attribute; | $code = $this->$attribute; | ||||
$producer = Producer::findOne($this->id_producer); | $producer = Producer::findOne($this->id_producer); | ||||
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | ||||
$this->addError($attribute, 'Code incorrect'); | $this->addError($attribute, 'Code incorrect'); | ||||
} | } |
$producer->order_deadline = 20; | $producer->order_deadline = 20; | ||||
$producer->order_delay = 1; | $producer->order_delay = 1; | ||||
$producer->free_price = (float) abs($this->free_price); | $producer->free_price = (float) abs($this->free_price); | ||||
$producer->slug = \common\helpers\Url::slugify($this->name_producer) ; | |||||
$cptSlug = 0 ; | |||||
do { | |||||
$slug = \common\helpers\Url::slugify($this->name_producer) ; | |||||
if($cptSlug) { | |||||
$slug .= $cptSlug ; | |||||
} | |||||
$producer->slug = $slug ; | |||||
$cptSlug ++ ; | |||||
} while(Producer::findOne(['slug' => $producer->slug])) ; | |||||
// génération d'un code | // génération d'un code | ||||
do { | do { | ||||
->setFrom([Yii::$app->params['adminEmail'] => 'distrib']) | ->setFrom([Yii::$app->params['adminEmail'] => 'distrib']) | ||||
->setSubject('[distrib] Nouveau producteur') | ->setSubject('[distrib] Nouveau producteur') | ||||
->send(); | ->send(); | ||||
$idProducer = $producer->id ; | |||||
} | |||||
else { | |||||
$idProducer = $this->id_producer ; | |||||
} | } | ||||
$user->setPassword($this->password); | $user->setPassword($this->password); | ||||
if ($user->save()) { | if ($user->save()) { | ||||
// on ajoute l'établissement sélectionnée par l'user en favoris | |||||
if ($this->option_user_producer == 'user' && $this->id_producer) { | |||||
$producer = Producer::find()->where(['id' => $this->id_producer])->one(); | |||||
$producer = Producer::find()->where(['id' => $idProducer])->one(); | |||||
if ($producer) { | |||||
Producer::addUser($user->id, $this->id_producer) ; | |||||
if ($producer) { | |||||
Producer::addUser($user->id, $idProducer) ; | |||||
if($this->option_user_producer == 'user') { | |||||
// envoi d'un email à l'utilisateur | // envoi d'un email à l'utilisateur | ||||
Yii::$app->mailer->compose( | Yii::$app->mailer->compose( | ||||
[ | [ |
'options' => ['id' => 'li-producteurs'] | 'options' => ['id' => 'li-producteurs'] | ||||
], | ], | ||||
[ | [ | ||||
'label' => '<span class="glyphicon glyphicon-flag"></span> Le Chat des Noisettes', | |||||
'label' => '<span class="glyphicon glyphicon-flag"></span> '.($producer ? Html::encode($producer->name) : ''), | |||||
'url' => '#', | 'url' => '#', | ||||
'items' => [ | 'items' => [ | ||||
[ | [ | ||||
->where([ | ->where([ | ||||
'user_producer.id_user' => User::getCurrentId(), | 'user_producer.id_user' => User::getCurrentId(), | ||||
'user_producer.bookmark' => 1, | 'user_producer.bookmark' => 1, | ||||
'producer.active' => 1 | |||||
]) | ]) | ||||
->all() ; | ->all() ; | ||||
?> | ?> |
<h1 class="title-system-order"><?= Html::encode($producer->name) ?></h1> | <h1 class="title-system-order"><?= Html::encode($producer->name) ?></h1> | ||||
<p class="info"><span class="alert alert-warning">Ce producteur est protègé par un code d'accès.</span></p> | <p class="info"><span class="alert alert-warning">Ce producteur est protègé par un code d'accès.</span></p> | ||||
<?php $form = ActiveForm::begin(['id' => 'producer-code','enableClientValidation'=> false]); ?> | <?php $form = ActiveForm::begin(['id' => 'producer-code','enableClientValidation'=> false]); ?> | ||||
<?= $form->field($modelProducerCode, 'code',[ | |||||
<?= $form->field($producerCodeForm, 'code',[ | |||||
'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | 'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | ||||
]) | ]) | ||||
->label('Code du producteur') | ->label('Code du producteur') |