Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order attributes #170

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/modules/v1/components/ControllerEx.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected function orderCreate(OrderForm $orderForm)
$order->address_id = $address->id;
$order->transit = $orderForm->transit;
$order->packagingNotes = $orderForm->packagingNotes;
$order->order_attributes = $orderForm->orderAttributes;

// Validate the order model itself
if (!$order->validate()) {
Expand Down Expand Up @@ -357,6 +358,7 @@ public function orderUpdate(OrderForm $orderForm, $id)
$order->status_id = $orderForm->status;
$order->transit = $orderForm->transit;
$order->packagingNotes = $orderForm->packagingNotes;
$order->order_attributes = $orderForm->orderAttributes;

// Validate the order model itself
if (!$order->validate()) {
Expand Down
8 changes: 8 additions & 0 deletions api/modules/v1/models/forms/OrderForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
* property = "service_id",
* type = "integer"
* ),
* @SWG\Property(
* property = "orderAttributes",
* type = "string",
* description = "Specific order attributes, separated by a comma. Example: one,two,three"
* ),
* )
*/

Expand Down Expand Up @@ -152,6 +157,8 @@ class OrderForm extends Model
public $transit;
/** @var string */
public $packagingNotes;
/** @var string */
public $orderAttributes;


/**
Expand All @@ -168,6 +175,7 @@ public function rules()
[['poNumber', 'uuid', 'origin', 'customerReference', 'packagingNotes'], 'string', 'length' => [1, 64]],
['orderReference', 'string', 'length' => [1, 45]],
['notes', 'string', 'length' => [1, 6000]],
[['orderAttributes'], 'string'],
['transit', 'integer'],
[['requestedShipDate', 'mustArriveByDate'], 'date', 'format' => 'php:Y-m-d'],
['status', 'required', 'on' => self::SCENARIO_UPDATE, 'message' => '{attribute} is required.'],
Expand Down
4 changes: 4 additions & 0 deletions api/modules/v1/models/order/OrderEx.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class OrderEx extends Order
* @SWG\Property( property = "poNumber", type = "string", description = "PO Number of ecommerce customer" ),
* @SWG\Property( property = "uuid", type = "string", description = "Reference to ecommerce UUID" ),
* @SWG\Property( property = "notes", type = "string", description = "Notes specific to an order" ),
* @SWG\Property( property = "orderAttributes", type = "string", description = "Specific attributes of order separated by comma" ),
* @SWG\Property( property = "origin", type = "string", description = "Origination of order. Such as
SquareSpace or Zoho" ),
* @SWG\Property( property = "transit", type = "integer", description = "Days in transit if available" ),
Expand Down Expand Up @@ -91,6 +92,9 @@ public function fields()
'poNumber' => 'po_number',
'uuid' => 'uuid',
'notes' => 'notes',
'orderAttributes' => function () {
return ($this->order_attributes_array) ? implode(',', $this->order_attributes_array) : null;
},
'origin' => 'origin',
'packages' => 'packages',
'transit' => 'transit',
Expand Down
50 changes: 50 additions & 0 deletions common/behaviors/JsonAttributeBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace common\behaviors;

use yii\base\{Behavior, Event};
use yii\db\ActiveRecord;
use yii\helpers\Json;

class JsonAttributeBehavior extends Behavior
{
public bool $stripSlashes = true;
public bool $stripTags = true;
public string $arraySeparator = ',';

public array $jsonAttributes = [];
public array $convertFromJsonAttributes = [];

public function events(): array
{
return [
ActiveRecord::EVENT_AFTER_FIND => 'eventAfterFind',
ActiveRecord::EVENT_BEFORE_INSERT => 'eventBeforeInsertAndUpdate',
ActiveRecord::EVENT_BEFORE_UPDATE => 'eventBeforeInsertAndUpdate',
];
}

public function eventAfterFind(Event $event): void
{
foreach ($this->convertFromJsonAttributes as $k => $v) {
$this->owner->{$k} = ($this->owner->{$v}) ? Json::decode($this->owner->{$v}) : [];
}
}

public function eventBeforeInsertAndUpdate(Event $event): void
{
foreach ($this->jsonAttributes as $jsonAttribute) {
if ($this->owner->{$jsonAttribute} && !preg_match("/\[(.*?)\]/si", $this->owner->{$jsonAttribute})) {
if ($this->stripTags) {
$this->owner->{$jsonAttribute} = strip_tags($this->owner->{$jsonAttribute});
}

if ($this->stripSlashes) {
$this->owner->{$jsonAttribute} = stripslashes($this->owner->{$jsonAttribute});
}

$this->owner->{$jsonAttribute} = Json::encode(explode($this->arraySeparator, $this->owner->{$jsonAttribute}));
}
}
}
}
15 changes: 14 additions & 1 deletion common/models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace common\models;

use Yii;
use common\behaviors\OrderEventsBehavior;
use common\behaviors\{OrderEventsBehavior, JsonAttributeBehavior};
use api\modules\v1\models\core\AddressEx;
use common\models\base\BaseOrder;
use common\models\query\OrderQuery;
Expand All @@ -24,15 +24,28 @@
* @property OrderHistory[] $history
* @property Carrier $carrier
* @property Service $service
*
* @property array $order_attributes_array
*/
class Order extends BaseOrder
{
/**
* The property is used for storing the `order_attributes` attribute converted from JSON everytime the model is found.
* @var array
*/
public array $order_attributes_array = [];

public function behaviors(): array
{
return [
[
'class' => OrderEventsBehavior::class,
],
[
'class' => JsonAttributeBehavior::class,
'jsonAttributes' => ['order_attributes'],
'convertFromJsonAttributes' => ['order_attributes_array' => 'order_attributes'],
],
];
}

Expand Down
8 changes: 6 additions & 2 deletions common/models/base/BaseOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace common\models\base;

use yii\db\ActiveRecord;

/**
* This is the model class for table "orders".
*
Expand All @@ -15,6 +17,7 @@
* @property string $updated_date
* @property int $address_id
* @property string $notes
* @property string $order_attributes
* @property string $po_number
* @property string $uuid
* @property string $origin
Expand All @@ -36,7 +39,7 @@
* @property int $transit
* @property string $packagingNotes
*/
class BaseOrder extends \yii\db\ActiveRecord
class BaseOrder extends ActiveRecord
{

/**
Expand All @@ -60,7 +63,7 @@ public function rules()
[['customer_reference', 'origin', 'uuid'], 'string', 'max' => 64],
[['notes', 'packagingNotes'], 'string', 'max' => 6000],
[['po_number'], 'string', 'max' => 64],
[['label_data'], 'string'],
[['label_data', 'order_attributes'], 'string'],
[['label_type'], 'string', 'max' => 6],
[
[
Expand Down Expand Up @@ -98,6 +101,7 @@ public function attributeLabels()
'updated_date' => 'Updated Date',
'address_id' => 'Address ID',
'notes' => 'Notes',
'order_attributes' => 'Attributes',
'origin' => 'Origin of Order',
'label_data' => 'Label Data',
'label_type' => 'Label Type',
Expand Down
29 changes: 29 additions & 0 deletions console/migrations/m230315_124607_order_attributes_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use yii\db\Migration;

/**
* Class m230315_124607_order_attributes_field
*/
class m230315_124607_order_attributes_field extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('{{%orders}}', 'order_attributes',
$this
->json()
->defaultValue(null)
->after('notes'));
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('{{%orders}}', 'order_attributes');
}
}
29 changes: 29 additions & 0 deletions frontend/assets/TagsInputAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace frontend\assets;

use yii\web\AssetBundle;

/**
* Tags input for Bootstrap 3
* @package frontend\assets
* @see https://bootstrap-tagsinput.github.io/bootstrap-tagsinput/examples/
*/
class TagsInputAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';

public $css = [
'css/plugins/tagsinput/bootstrap-tagsinput.min.css',
];

public $js = [
'js/plugins/tagsinput/bootstrap-tagsinput.min.js',
];

public $depends = [
// depends on jQuery
'yii\web\YiiAsset',
];
}
2 changes: 2 additions & 0 deletions frontend/models/search/OrderSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function rules()
'created_date',
'updated_date',
'notes',
'order_attributes',
'uuid',
'requested_ship_date',
'origin',
Expand Down Expand Up @@ -170,6 +171,7 @@ public function search($params)
$query->andFilterWhere(['like', 'order_reference', $this->order_reference])
->andFilterWhere(['like', 'tracking', $this->tracking])
->andFilterWhere(['like', 'orders.notes', $this->notes])
->andFilterWhere(['like', 'order_attributes', $this->order_attributes])
->andFilterWhere(['like', 'uuid', $this->uuid])
->andFilterWhere(['like', 'origin', $this->origin])
->andFilterWhere(['like', Address::tableName() . '.name', $this->address]);
Expand Down
25 changes: 18 additions & 7 deletions frontend/views/order/_form.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\web\View;
use frontend\assets\DatePickerAsset;
use yii\bootstrap\ActiveForm;
use yii\helpers\{Html, Url};
use frontend\assets\{DatePickerAsset, TagsInputAsset};
use frontend\models\Item;

/* @var $this yii\web\View */
/* @var $this View */
/* @var $model common\models\forms\OrderForm */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $form ActiveForm */
/* @var $customers array List of customers */
/* @var $statuses array List of order statuses */
/* @var $carriers array List of carriers */
Expand All @@ -18,10 +16,17 @@
/* @var $countries array list of states */

DatePickerAsset::register($this);
TagsInputAsset::register($this);

$item = new Item();
$item->loadDefaultValues();

$this->registerJs("
$('input[data-tags-input]').tagsinput({
trimValue: true,
tagClass: 'label label-default'
});
");
?>

<div class="order-form">
Expand Down Expand Up @@ -95,6 +100,12 @@
'value' => ($model->order->must_arrive_by_date) ? Yii::$app->formatter->asDate($model->order->must_arrive_by_date) : null,
]) ?>

<?= $form->field($model->order, 'order_attributes')->textInput([
'value' => ($model->order->order_attributes_array) ? implode(',', $model->order->order_attributes_array) : null,
'data-tags-input' => true,
'maxlength' => true
]) ?>

</div>
</div>
</div>
Expand Down
14 changes: 14 additions & 0 deletions frontend/views/order/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@
return yii\helpers\StringHelper::truncate($model->notes, 40);
}
],
[
'attribute' => 'order_attributes',
'filter' => true,
'format' => 'raw',
'value' => function ($model) {
if ($model->order_attributes_array) {
return implode(', ', array_map(function ($attr) {
return Html::a(Html::encode($attr), Url::to(['index', 'OrderSearch[order_attributes]' => Html::encode($attr)]));
}, $model->order_attributes_array));
}

return null;
},
],
[
'attribute' => 'status_id',
'options' => ['width' => '10%'],
Expand Down
18 changes: 15 additions & 3 deletions frontend/views/order/view.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\{Html, HtmlPurifier, Url};
use yii\web\YiiAsset;
use yii\widgets\DetailView;
use yii\data\ActiveDataProvider;
use yii\grid\GridView;
use common\models\Status;
use yii\helpers\HtmlPurifier;

/* @var $this yii\web\View */
/* @var $model frontend\models\Order */
Expand Down Expand Up @@ -146,6 +144,20 @@
'attribute' => 'packagingNotes',
'visible' => !$simple,
],
[
'attribute' => 'order_attributes',
'visible' => !$simple,
'format' => 'raw',
'value' => function ($model) {
if ($model->order_attributes_array) {
return implode(', ', array_map(function ($attr) {
return Html::a(Html::encode($attr), Url::to(['index', 'OrderSearch[order_attributes]' => Html::encode($attr)]));
}, $model->order_attributes_array));
}

return null;
},
],
],
]) ?>
</div>
Expand Down
Loading