namespace backend\controllers; | namespace backend\controllers; | ||||
use common\helpers\Alwaysdata; | |||||
use common\logic\Order\Order\Model\Order; | use common\logic\Order\Order\Model\Order; | ||||
use common\logic\Producer\Producer\Model\Producer; | use common\logic\Producer\Producer\Model\Producer; | ||||
use common\logic\Product\Product\Model\Product; | use common\logic\Product\Product\Model\Product; | ||||
public function actionUpdate(int $id) | public function actionUpdate(int $id) | ||||
{ | { | ||||
$producerManager = $this->getProducerManager(); | $producerManager = $this->getProducerManager(); | ||||
$producer = $this->findModel($id); | |||||
$producer = $this->findProducer($id); | |||||
if ($producer->load(\Yii::$app->request->post()) && $producerManager->saveCreate($producer)) { | if ($producer->load(\Yii::$app->request->post()) && $producerManager->saveCreate($producer)) { | ||||
$this->setFlash('success', 'Producteur modifié.'); | $this->setFlash('success', 'Producteur modifié.'); | ||||
*/ | */ | ||||
public function actionBilling(int $id) | public function actionBilling(int $id) | ||||
{ | { | ||||
$producerManager = $this->getProducerManager(); | |||||
$producer = $this->findModel($id); | |||||
$producer = $this->findProducer($id); | |||||
return $this->render('billing', [ | return $this->render('billing', [ | ||||
'producer' => $producer, | 'producer' => $producer, | ||||
]); | ]); | ||||
} | } | ||||
public function actionAlwaysdata(int $id) | |||||
{ | |||||
$producer = $this->findProducer($id); | |||||
if($producer->contact_email) { | |||||
Alwaysdata::createProducerSiteShortUrlRedirection($producer); | |||||
Alwaysdata::createProducerEmailRedirection($producer); | |||||
} | |||||
else { | |||||
$this->addFlash('error', "L'adresse email de contact du producteur n'est pas définie."); | |||||
} | |||||
return $this->redirect(['index']); | |||||
} | |||||
public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1) | public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1) | ||||
{ | { | ||||
$producerManager = $this->getProducerManager(); | $producerManager = $this->getProducerManager(); | ||||
/** | /** | ||||
* Recherche un producteur. | * Recherche un producteur. | ||||
*/ | */ | ||||
protected function findModel(int $id) | |||||
protected function findProducer(int $id) | |||||
{ | { | ||||
if (($model = Producer::findOne($id)) !== null) { | |||||
$producerManager = $this->getProducerManager(); | |||||
if (($model = $producerManager->findOneProducerById($id)) !== null) { | |||||
return $model; | return $model; | ||||
} else { | } else { | ||||
throw new NotFoundHttpException('The requested page does not exist.'); | throw new NotFoundHttpException('The requested page does not exist.'); |
namespace backend\controllers; | namespace backend\controllers; | ||||
use common\helpers\Alwaysdata; | |||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use common\logic\Distribution\Distribution\Model\Distribution; | use common\logic\Distribution\Distribution\Model\Distribution; | ||||
use common\logic\Order\Order\Model\Order; | use common\logic\Order\Order\Model\Order; |
<?php $form = ActiveForm::begin(); ?> | <?php $form = ActiveForm::begin(); ?> | ||||
<h3>Général</h3> | <h3>Général</h3> | ||||
<?= $form->field($model, 'slug') ?> | |||||
<?= $form->field($model, 'name') ?> | <?= $form->field($model, 'name') ?> | ||||
<?= $form->field($model, 'type')->textInput(['placeholder' => 'Boulangerie, brasserie, ferme ...']); ?> | <?= $form->field($model, 'type')->textInput(['placeholder' => 'Boulangerie, brasserie, ferme ...']); ?> | ||||
<?= $form->field($model, 'postcode') ?> | <?= $form->field($model, 'postcode') ?> | ||||
<?= $form->field($model, 'city') ?> | <?= $form->field($model, 'city') ?> | ||||
<?= $form->field($model, 'contact_email') ?> | |||||
<?= $form->field($model, 'latitude') ?> | <?= $form->field($model, 'latitude') ?> | ||||
<?= $form->field($model, 'longitude') ?> | <?= $form->field($model, 'longitude') ?> | ||||
<?= $form->field($model, 'code')->label('Code d\'accès') ?> | <?= $form->field($model, 'code')->label('Code d\'accès') ?> |
], | ], | ||||
[ | [ | ||||
'class' => 'yii\grid\ActionColumn', | 'class' => 'yii\grid\ActionColumn', | ||||
'template' => '{update} {billing}', | |||||
'template' => '{update} {billing} {alwaysdata}', | |||||
'headerOptions' => ['class' => 'column-actions'], | 'headerOptions' => ['class' => 'column-actions'], | ||||
'contentOptions' => ['class' => 'column-actions'], | 'contentOptions' => ['class' => 'column-actions'], | ||||
'buttons' => [ | 'buttons' => [ | ||||
] | ] | ||||
); | ); | ||||
}, | }, | ||||
'alwaysdata' => function ($url, $model) { | |||||
return Html::a( | |||||
'<span class="glyphicon glyphicon-road"></span>', | |||||
$url, | |||||
[ | |||||
'title' => 'Redirections Alwaysdata (email et url courte)', | |||||
'class' => 'btn btn-default' | |||||
] | |||||
); | |||||
}, | |||||
], | ], | ||||
], | ], | ||||
], | ], |
<?php | |||||
namespace common\helpers; | |||||
use common\logic\Producer\Producer\Model\Producer; | |||||
use GuzzleHttp\Client; | |||||
use yii\base\ErrorException; | |||||
class Alwaysdata | |||||
{ | |||||
public static function createProducerEmailRedirection(Producer $producer) | |||||
{ | |||||
return self::post('mailbox',[ | |||||
'domain' => \Yii::$app->params['alwaysdataDomainOpendistribId'], | |||||
'name' => $producer->slug, | |||||
'password' => Password::generate(13), | |||||
'redirect_enabled' => true, | |||||
'redirect_to' => $producer->contact_email | |||||
]); | |||||
} | |||||
public static function createProducerSiteShortUrlRedirection(Producer $producer) | |||||
{ | |||||
return self::post('site', [ | |||||
'addresses' => [$producer->slug.'.opendistrib.net'], | |||||
'type' => 'redirect', | |||||
'url' => 'https://producteurs.opendistrib.net/'.$producer->slug, | |||||
'redirect_type' => 'PERMANENT', | |||||
'annotation' => 'Opendistrib / '.$producer->name | |||||
]); | |||||
} | |||||
private static function post(string $resource, array $data) | |||||
{ | |||||
$client = self::getClient($resource); | |||||
return $client->request('POST', '', [ | |||||
'body' => json_encode($data) | |||||
]); | |||||
} | |||||
private static function getClient(string $resource) | |||||
{ | |||||
self::checkConfiguration(); | |||||
return new Client([ | |||||
'base_uri' => \Yii::$app->params['alwaysdataApiUrl'].$resource.'/', | |||||
'auth' => [\Yii::$app->params['alwaysdataApiKey'].' account='.\Yii::$app->params['alwaysdataAccount'], ''], | |||||
]); | |||||
} | |||||
private static function checkConfiguration() | |||||
{ | |||||
if(!isset(\Yii::$app->params['alwaysdataApiUrl']) | |||||
|| !isset(\Yii::$app->params['alwaysdataAccount']) | |||||
|| !isset(\Yii::$app->params['alwaysdataApiKey']) | |||||
|| !isset(\Yii::$app->params['alwaysdataDomainOpendistribId'])) { | |||||
throw new ErrorException('Configuration API Alwaysdata absente ou incomplète dans params-local.php'); | |||||
} | |||||
} | |||||
} |
'latest_version_opendistrib', | 'latest_version_opendistrib', | ||||
'option_csv_separator', | 'option_csv_separator', | ||||
'option_point_sale_wording', | 'option_point_sale_wording', | ||||
'option_testimony' | |||||
'option_testimony', | |||||
'contact_email' | |||||
], | ], | ||||
'string' | 'string' | ||||
], | ], | ||||
'longitude' => 'Longitude', | 'longitude' => 'Longitude', | ||||
'option_testimony' => 'Témoignage', | 'option_testimony' => 'Témoignage', | ||||
'option_time_saved' => 'Temps gagné / semaine', | 'option_time_saved' => 'Temps gagné / semaine', | ||||
'contact_email' => 'Email de contact' | |||||
]; | ]; | ||||
} | } | ||||
<?php | |||||
use yii\db\Migration; | |||||
use yii\db\Schema; | |||||
/** | |||||
* Class m230823_093646_add_column_producer_contact_email | |||||
*/ | |||||
class m230823_093646_add_column_producer_contact_email extends Migration | |||||
{ | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeUp() | |||||
{ | |||||
$this->addColumn('producer', 'contact_email', Schema::TYPE_STRING); | |||||
} | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeDown() | |||||
{ | |||||
$this->dropColumn('producer', 'contact_email'); | |||||
} | |||||
} |