Przeglądaj źródła

Alwaysdata : création automatique des redirections (email et url courte) #1161

feature/souke
Guillaume Bourgeois 1 rok temu
rodzic
commit
a311a2941f
7 zmienionych plików z 127 dodań i 7 usunięć
  1. +22
    -5
      backend/controllers/ProducerAdminController.php
  2. +1
    -0
      backend/controllers/SiteController.php
  3. +2
    -0
      backend/views/producer-admin/_form.php
  4. +11
    -1
      backend/views/producer-admin/index.php
  5. +62
    -0
      common/helpers/Alwaysdata.php
  6. +3
    -1
      common/logic/Producer/Producer/Model/Producer.php
  7. +26
    -0
      console/migrations/m230823_093646_add_column_producer_contact_email.php

+ 22
- 5
backend/controllers/ProducerAdminController.php Wyświetl plik

@@ -38,6 +38,7 @@

namespace backend\controllers;

use common\helpers\Alwaysdata;
use common\logic\Order\Order\Model\Order;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\Product\Product\Model\Product;
@@ -130,7 +131,7 @@ class ProducerAdminController extends BackendController
public function actionUpdate(int $id)
{
$producerManager = $this->getProducerManager();
$producer = $this->findModel($id);
$producer = $this->findProducer($id);

if ($producer->load(\Yii::$app->request->post()) && $producerManager->saveCreate($producer)) {
$this->setFlash('success', 'Producteur modifié.');
@@ -147,14 +148,28 @@ class ProducerAdminController extends BackendController
*/
public function actionBilling(int $id)
{
$producerManager = $this->getProducerManager();
$producer = $this->findModel($id);
$producer = $this->findProducer($id);

return $this->render('billing', [
'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)
{
$producerManager = $this->getProducerManager();
@@ -226,9 +241,11 @@ class ProducerAdminController extends BackendController
/**
* 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;
} else {
throw new NotFoundHttpException('The requested page does not exist.');

+ 1
- 0
backend/controllers/SiteController.php Wyświetl plik

@@ -38,6 +38,7 @@

namespace backend\controllers;

use common\helpers\Alwaysdata;
use common\helpers\GlobalParam;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Order\Order\Model\Order;

+ 2
- 0
backend/views/producer-admin/_form.php Wyświetl plik

@@ -45,10 +45,12 @@ use common\logic\Producer\Producer\Model\Producer;
<?php $form = ActiveForm::begin(); ?>

<h3>Général</h3>
<?= $form->field($model, 'slug') ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'type')->textInput(['placeholder' => 'Boulangerie, brasserie, ferme ...']); ?>
<?= $form->field($model, 'postcode') ?>
<?= $form->field($model, 'city') ?>
<?= $form->field($model, 'contact_email') ?>
<?= $form->field($model, 'latitude') ?>
<?= $form->field($model, 'longitude') ?>
<?= $form->field($model, 'code')->label('Code d\'accès') ?>

+ 11
- 1
backend/views/producer-admin/index.php Wyświetl plik

@@ -202,7 +202,7 @@ $this->addButton(['label' => 'Nouveau producteur <span class="glyphicon glyphico
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {billing}',
'template' => '{update} {billing} {alwaysdata}',
'headerOptions' => ['class' => 'column-actions'],
'contentOptions' => ['class' => 'column-actions'],
'buttons' => [
@@ -226,6 +226,16 @@ $this->addButton(['label' => 'Nouveau producteur <span class="glyphicon glyphico
]
);
},
'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'
]
);
},
],
],
],

+ 62
- 0
common/helpers/Alwaysdata.php Wyświetl plik

@@ -0,0 +1,62 @@
<?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');
}
}
}

+ 3
- 1
common/logic/Producer/Producer/Model/Producer.php Wyświetl plik

@@ -219,7 +219,8 @@ class Producer extends ActiveRecordCommon
'latest_version_opendistrib',
'option_csv_separator',
'option_point_sale_wording',
'option_testimony'
'option_testimony',
'contact_email'
],
'string'
],
@@ -410,6 +411,7 @@ class Producer extends ActiveRecordCommon
'longitude' => 'Longitude',
'option_testimony' => 'Témoignage',
'option_time_saved' => 'Temps gagné / semaine',
'contact_email' => 'Email de contact'
];
}


+ 26
- 0
console/migrations/m230823_093646_add_column_producer_contact_email.php Wyświetl plik

@@ -0,0 +1,26 @@
<?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');
}
}

Ładowanie…
Anuluj
Zapisz