Skip to content

Commit

Permalink
Merge branch 'craft-4' of https://github.com/verbb/gift-voucher into …
Browse files Browse the repository at this point in the history
…craft-5

# Conflicts:
#	composer.json
#	src/controllers/DownloadsController.php
#	src/elements/Code.php
  • Loading branch information
engram-design committed Mar 4, 2024
2 parents 8b6f957 + d0b5263 commit d985b3d
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 52 deletions.
52 changes: 52 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# Changelog

## 3.0.12 - 2024-03-04

### Added
- Add support for programatically setting a code’s `codeKey`.

### Fixed
- Fix order of operations when uninstalling the plugin.

## 3.0.11 - 2024-01-30

### Added
- PDFs now support using the current site’s locale language and formatting.

## 3.0.10 - 2023-12-08

### Added
- Add currently-applied voucher codes to manage for modal when editing an order in the control panel.
- Add `codeKey` as an available variable for the `voucherCodesPdfFilenameFormat` setting.

### Changed
- Voucher modal now shows message instead of refreshing, ensuring unsaved changes to the order aren’t discarded.

### Fixed
- Fix “enter” key not submitting the gift voucher modal when editing an order in the control panel.
- Fix `codeKey` reference when rendering PDFs.

## 3.0.9 - 2023-10-25

### Fixed
- Implement `Element::trackChanges()` for Blitz compatibility.

## 3.0.8 - 2023-10-08

### Fixed
- Fix an issue when creating new sites and not propagating voucher types correctly.

## 3.0.7 - 2023-03-09

### Changed
Expand Down Expand Up @@ -66,6 +102,22 @@
- Fix an error fetching new redemption records.
- Fix a type error with redemption codes.

## 2.7.5 - 2023-12-08

### Added
- Add currently-applied voucher codes to manage for modal when editing an order in the control panel.

### Changed
- Voucher modal now shows message instead of refreshing, ensuring unsaved changes to the order aren’t discarded.

### Fixed
- Fix “enter” key not submitting the gift voucher modal when editing an order in the control panel.

## 2.7.4 - 2023-10-08

### Fixed
- Fix an issue when creating new sites and not propagating voucher types correctly.

## 2.7.3 - 2022-10-23

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Gift Voucher plugin for Craft CMS
<img width="500" src="https://verbb.imgix.net/plugins/gift-voucher/gift-voucher-social-card.png?v=1">
<img width="500" src="https://verbb.imgix.net/plugins/gift-voucher/gift-voucher-social-card.png?v=2">

Gift Voucher is a Craft CMS plugin allowing your customers to purchase digital gift vouchers from your Craft Commerce store. Vouchers can be redeemed by other customers during checkout for a discount on their order. Supports intelligent redemptions, PDF generation, customer-entered amounts, and lots more!

Expand Down
26 changes: 24 additions & 2 deletions src/controllers/DownloadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace verbb\giftvoucher\controllers;

use verbb\giftvoucher\GiftVoucher;
use verbb\giftvoucher\helpers\Locale;

use Craft;
use craft\web\Controller;
Expand Down Expand Up @@ -38,6 +39,15 @@ public function actionPdf(): Response|string
$format = $this->request->getParam('format');
$attach = $this->request->getParam('attach');

$siteHandle = $this->request->getParam('site');
$site = Craft::$app->getSites()->getPrimarySite();

if ($siteHandle) {
if ($requestedSite = Craft::$app->getSites()->getSiteByHandle($siteHandle)) {
$site = $requestedSite;
}
}

if ($number) {
$order = Commerce::getInstance()->getOrders()->getOrderByNumber($number);

Expand All @@ -55,16 +65,28 @@ public function actionPdf(): Response|string
$order = $codes[0]->order;
}

// Switch to use the correct site/language
$originalLanguage = Craft::$app->language;
$originalFormattingLocale = Craft::$app->formattingLocale;

Locale::switchAppLanguage($site->language);

$pdf = GiftVoucher::$plugin->getPdf()->renderPdf($codes, $order, $lineItem, $option);

// Set previous language back
Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale);

$filenameFormat = GiftVoucher::$plugin->getSettings()->voucherCodesPdfFilenameFormat;

$fileName = $this->getView()->renderObjectTemplate($filenameFormat, $order);
$fileName = $this->getView()->renderObjectTemplate($filenameFormat, $order, [
'codeKey' => $codes[0]->codeKey ?? null,
]);

if (!$fileName) {
if ($order) {
$fileName = 'Voucher-' . $order->number;
} else if ($codes) {
$fileName = 'Voucher-' . $code[0]->codeKey;
$fileName = 'Voucher-' . $codes[0]->codeKey;
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/controllers/VouchersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

use Throwable;

use craft\commerce\elements\Order;

class VouchersController extends Controller
{
// Public Methods
Expand Down Expand Up @@ -253,6 +255,29 @@ public function actionDuplicate()
return $this->runAction('save', ['duplicate' => true]);
}

public function actionGetModalBody()
{
$this->requirePostRequest();
$this->requireAcceptsJson();

$orderId = $this->request->getParam('orderId');
$order = Order::find()->id($orderId)->one();

$codes = GiftVoucher::$plugin->getCodeStorage()->getCodeKeys($order);

$variables = [
'order' => $order,
'codes' => $codes,
];

$html = $this->getView()->renderTemplate('gift-voucher/vouchers/_modal', $variables);

return $this->asJson([
'success' => true,
'html' => $html,
]);
}


// Protected Methods
// =========================================================================
Expand Down
15 changes: 14 additions & 1 deletion src/elements/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class Code extends Element
// Static Methods
// =========================================================================

public static function trackChanges(): bool
{
return true;
}

public static function hasContent(): bool
{
return true;
}

public static function hasStatuses(): bool
{
return true;
Expand Down Expand Up @@ -391,7 +401,10 @@ public function afterSave(bool $isNew): void
$codeRecord->lineItemId = $this->lineItemId;
$codeRecord->orderId = $this->orderId;
$codeRecord->voucherId = $this->voucherId;
$codeRecord->codeKey = $this->generateCodeKey();

// Generate a code key if not already set
$codeRecord->codeKey = $this->codeKey ?? $this->generateCodeKey();

// set the codeKey to the Code as well to use it directly
$this->codeKey = $codeRecord->codeKey;
}
Expand Down
25 changes: 25 additions & 0 deletions src/helpers/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace verbb\giftvoucher\helpers;

use Craft;

class Locale
{
// Static Methods
// =========================================================================

public static function switchAppLanguage(string $toLanguage, ?string $formattingLocale = null): void
{
Craft::$app->language = $toLanguage;

$locale = Craft::$app->getI18n()->getLocaleById($toLanguage);

Craft::$app->set('locale', $locale);

if ($formattingLocale !== null) {
$locale = Craft::$app->getI18n()->getLocaleById($formattingLocale);
}

Craft::$app->set('formattingLocale', $locale);
}
}
2 changes: 1 addition & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function safeUp(): bool

public function safeDown(): bool
{
$this->dropProjectConfig();
$this->dropForeignKeys();
$this->dropTables();
$this->dropProjectConfig();

return true;
}
Expand Down
89 changes: 64 additions & 25 deletions src/resources/src/js/gift-voucher.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Craft.GiftVoucher.CpAddVoucher = Garnish.Base.extend({
var $menubtn = $('.menubtn[data-icon="settings"]').data('menubtn');

if ($menubtn) {
var $newOption = $('<li><a data-action="apply-gift-voucher">' + Craft.t('gift-voucher', 'Add voucher code') + '</a></li>');
var $newOption = $('<li><a data-action="gift-vouchers">' + Craft.t('gift-voucher', 'Manage gift vouchers') + '</a></li>');

// Add the option to the menubtn
$menubtn.menu.addOptions($newOption.children());
Expand All @@ -41,49 +41,78 @@ Craft.GiftVoucher.CpAddVoucher = Garnish.Base.extend({
var $option = $(ev.selectedOption);

// Just action our option
if ($option.data('action') == 'apply-gift-voucher') {
new Craft.GiftVoucher.AddCodeModal();
if ($option.data('action') == 'gift-vouchers') {
new Craft.GiftVoucher.GiftVouchersModal();
}
},
});

Craft.GiftVoucher.AddCodeModal = Garnish.Modal.extend({
Craft.GiftVoucher.GiftVouchersModal = Garnish.Modal.extend({
init: function() {
this.$form = $('<form class="modal fitted add-code-modal" method="post" accept-charset="UTF-8"/>').appendTo(Garnish.$bod);
this.$body = $('<div class="pane"></div>').appendTo(this.$form);
this.$form = $('<form class="modal fitted gift-voucher-modal" method="post" accept-charset="UTF-8"/>').appendTo(Garnish.$bod);
this.$body = $('<div class="body"><div class="spinner big"></div></div>').appendTo(this.$form);

Craft.ui.createTextField({
label: Craft.t('gift-voucher', 'Voucher Code'),
instructions: Craft.t('gift-voucher', 'Enter the gift voucher code to be applied on the order.'),
id: 'voucher-code',
name: 'voucherCode'
}).appendTo(this.$body);

// Fetch Order ID from URL
var pathArray = window.location.pathname.split('/');
var orderId = pathArray[pathArray.length - 1];

$('<input type="hidden" name="orderId" value="' + orderId + '">').appendTo(this.$body);

var $footer = $('<div class="footer"/>').appendTo(this.$body);
var $footer = $('<div class="footer"/>').appendTo(this.$form);
var $mainBtnGroup = $('<div class="buttons right"/>').appendTo($footer);
this.$cancelBtn = $('<input type="button" class="btn" value="' + Craft.t('commerce', 'Cancel') + '"/>').appendTo($mainBtnGroup);
this.$updateBtn = $('<input type="button" class="btn submit" value="' + Craft.t('gift-voucher', 'Add voucher code') + '"/>').appendTo($mainBtnGroup);
this.$cancelBtn = $('<input type="button" class="btn cancel" value="' + Craft.t('gift-voucher', 'Cancel') + '"/>').appendTo($mainBtnGroup);
this.$saveBtn = $('<input type="submit" class="btn submit" value="' + Craft.t('gift-voucher', 'Add voucher code') + '"/>').appendTo($mainBtnGroup);
this.$footerSpinner = $('<div class="spinner right hidden"/>').appendTo($footer);

Craft.initUiElements(this.$form);

this.addListener(this.$cancelBtn, 'click', 'onFadeOut');
this.addListener(this.$updateBtn, 'click', 'onSubmit');
this.addListener(this.$saveBtn, 'click', 'onSubmit');

this.base(this.$form);

var data = {
orderId: this.getOrderId(),
};

Craft.sendActionRequest('POST', 'gift-voucher/vouchers/get-modal-body', { data })
.then((response) => {
this.$body.html(response.data.html);
});

this.base(this.$form, settings);
$(this.$form).on('click', '[data-code]', $.proxy(this.removeCode, this));
},

onFadeOut: function() {
onFadeOut() {
this.$form.remove();
this.$shade.remove();
},

getOrderId() {
// Fetch Order ID from URL
var pathArray = window.location.pathname.split('/');

return pathArray[pathArray.length - 1];
},

removeCode: function(e) {
e.preventDefault();

var data = {
voucherCode: e.currentTarget.getAttribute('data-code'),
orderId: this.getOrderId(),
};

this.$footerSpinner.removeClass('hidden');

Craft.sendActionRequest('POST', 'gift-voucher/cart/remove-code', { data })
.then((response) => {
if (response.data.success) {
Craft.cp.displayNotice(Craft.t('gift-voucher', 'Voucher code removed.'));

this.onFadeOut();
} else {
Craft.cp.displayError(response.data.error);

this.$footerSpinner.addClass('hidden');
}
});
},

onSubmit: function(e) {
e.preventDefault();

Expand All @@ -105,6 +134,16 @@ Craft.GiftVoucher.AddCodeModal = Garnish.Modal.extend({
})
.finally(() => {
this.$footerSpinner.addClass('hidden');

if (response.data.success) {
Craft.cp.displayNotice(Craft.t('gift-voucher', 'Voucher code applied.'));

this.onFadeOut();
} else {
Craft.cp.displayError(response.data.error);

this.$footerSpinner.addClass('hidden');
}
});

},
Expand Down
9 changes: 9 additions & 0 deletions src/resources/src/scss/gift-voucher.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@

@import "welcome";

.gift-voucher-modal .body {
height: calc(100% - 62px);
overflow: auto;
}

.gift-voucher-modal .spinner.big {
margin: auto;
display: flex;
}
6 changes: 6 additions & 0 deletions src/services/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,24 @@ class Pdf extends Component

public function getPdfUrl(Order $order, LineItem $lineItem = null, $option = null): string
{
$currentSite = Craft::$app->getSites()->getCurrentSite();

return UrlHelper::actionUrl('gift-voucher/downloads/pdf', array_filter([
'number' => $order->number ?? null,
'option' => $option ?? null,
'lineItemId' => $lineItem->id ?? null,
'site' => $currentSite->handle,
]));
}

public function getPdfUrlForCode($code, $option = null): string
{
$currentSite = Craft::$app->getSites()->getCurrentSite();

return UrlHelper::actionUrl('gift-voucher/downloads/pdf', array_filter([
'codeId' => $code->id ?? null,
'option' => $option ?? null,
'site' => $currentSite->handle,
]));
}

Expand Down
Loading

0 comments on commit d985b3d

Please sign in to comment.