From c6485464e3e7726f7102ae1cd68cadb388a8a734 Mon Sep 17 00:00:00 2001 From: deninchoo Date: Tue, 24 Apr 2018 15:35:00 +0200 Subject: [PATCH 1/8] Added code for custom snippet upload and validation --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 52 ++++++++++++++++++- Model/Api.php | 12 +++-- Model/Config.php | 46 ++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 90e1b690..3d7fe199 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -11,6 +11,7 @@ use Fastly\Cdn\Helper\Vcl; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; +use Magento\Framework\Exception\LocalizedException; class Upload extends Action { @@ -83,7 +84,7 @@ public function __construct( /** * Upload VCL snippets * - * @return $resultJsonFactory + * @return $this|\Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface */ public function execute() { @@ -96,6 +97,8 @@ public function execute() $currActiveVersion = $this->vcl->getCurrentVersion($service->versions); $clone = $this->api->cloneVersion($currActiveVersion); $snippets = $this->config->getVclSnippets(); + $customSnippets = $this->config->getCustomVclSnippets(); + foreach ($snippets as $key => $value) { $snippetData = [ 'name' => Config::FASTLY_MAGENTO_MODULE . '_' . $key, @@ -107,6 +110,22 @@ public function execute() $this->api->uploadSnippet($clone->number, $snippetData); } + foreach ($customSnippets as $key => $value) { + $snippetNameData = $this->validateCustomSnippet($key); + $snippetType = $snippetNameData[0]; + $snippetPriority = $snippetNameData[1]; + $snippetShortName = $snippetNameData[2]; + + $customSnippetData = [ + 'name' => Config::FASTLY_MAGENTO_MODULE . '_' . $snippetShortName, + 'type' => $snippetType, + 'priority' => $snippetPriority, + 'content' => $value, + 'dynamic' => '0' + ]; + $this->api->uploadSnippet($clone->number, $customSnippetData); + } + $condition = [ 'name' => Config::FASTLY_MAGENTO_MODULE.'_pass', 'statement' => 'req.http.x-pass', @@ -147,4 +166,35 @@ public function execute() ]); } } + + /** + * Validate custom snippet naming convention + * [vcl_snippet_type]_[priority]_[short_name_description].vcl + * + * @param $customSnippet + * @return array + * @throws LocalizedException + */ + private function validateCustomSnippet($customSnippet) + { + $snippetName = str_replace(' ', '', $customSnippet); + $snippetNameData = explode('_', $snippetName, 3); + $containsEmpty = in_array("", $snippetNameData, true); + $types = ['init', 'recv', 'hit', 'miss', 'pass', 'fetch', 'error', 'deliver', 'none']; + $exception = 'Failed to upload VCL snippets. Please make sure the custom VCL snippets + follow this naming convention: [vcl_snippet_type]_[priority]_[short_name_description].vcl'; + + if (count($snippetNameData) < 3) { + throw new LocalizedException(__($exception)); + } + + $inArray = in_array($snippetNameData[0], $types); + $isNumeric = is_numeric($snippetNameData[1]); + $isAlphanumeric = preg_match('/^[\w]+$/', $snippetNameData[2]); + + if ($containsEmpty || !$inArray || !$isNumeric || !$isAlphanumeric) { + throw new LocalizedException(__($exception)); + } + return $snippetNameData; + } } diff --git a/Model/Api.php b/Model/Api.php index 64782078..eb5dc4b6 100644 --- a/Model/Api.php +++ b/Model/Api.php @@ -126,8 +126,9 @@ private function _getHistoricalEndpoint() /** * Purge a single URL * - * @param string $url + * @param $url * @return bool + * @throws \Zend_Uri_Exception */ public function cleanUrl($url) { @@ -147,6 +148,7 @@ public function cleanUrl($url) * * @param $keys * @return bool + * @throws \Zend_Uri_Exception */ public function cleanBySurrogateKey($keys) { @@ -184,6 +186,7 @@ public function cleanBySurrogateKey($keys) * Purge all of Fastly's CDN content. Can be called only once per request * * @return bool + * @throws \Zend_Uri_Exception */ public function cleanAll() { @@ -224,6 +227,7 @@ public function cleanAll() * @param string $method * @param null $payload * @return bool + * @throws \Zend_Uri_Exception */ private function _purge($uri, $method = \Zend_Http_Client::POST, $payload = null) { @@ -406,10 +410,13 @@ public function activateVersion($version) } /** - * Creating and updating a regular VCL Snippet + * Creating and updating regular VCL snippets + * * @param $version * @param array $snippet * @throws LocalizedException + * @throws \Exception + * @throws \Magento\Framework\Exception\FileSystemException */ public function uploadSnippet($version, array $snippet) { @@ -796,7 +803,6 @@ public function deleteDictionary($version, $name) * Get dictionary item list * @param $dictionaryId * @return bool|mixed - * @throws LocalizedException */ public function dictionaryItemsList($dictionaryId) { diff --git a/Model/Config.php b/Model/Config.php index 067b5981..7a9044b2 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -678,6 +678,14 @@ public function getVclFile($vclTemplatePath) // @codingStandardsIgnoreLine - Unu return strtr($data, $this->getReplacements()); } + /** + * Returns VCL snippet data + * + * @param string $path + * @param null $specificFile + * @return array + * @throws \Magento\Framework\Exception\FileSystemException + */ public function getVclSnippets($path = '/vcl_snippets', $specificFile = null) { $snippetsData = []; @@ -708,6 +716,44 @@ public function getVclSnippets($path = '/vcl_snippets', $specificFile = null) return $snippetsData; } + /** + * Returns custom VCL snippet data + * + * @param string $path + * @param null $specificFile + * @return array + * @throws \Magento\Framework\Exception\FileSystemException + */ + public function getCustomVclSnippets($path = '/vcl_snippets_custom', $specificFile = null) + { + $snippetsData = []; + + $moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Fastly_Cdn') . $path; + $directoryRead = $this->readFactory->create($moduleEtcPath); + if (!$specificFile) { + $files = $directoryRead->read(); + + if (is_array($files)) { + foreach ($files as $file) { + if (substr($file, strpos($file, ".") + 1) !== 'vcl') { + continue; + } + $snippetFilePath = $moduleEtcPath . '/' . $file; + $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); + $type = explode('.', $file)[0]; + $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); + } + } + } else { + $snippetFilePath = $moduleEtcPath . '/' . $specificFile; + $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); + $type = explode('.', $specificFile)[0]; + $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); + } + + return $snippetsData; + } + /** * Prepare data for VCL config * From 2b3c9ca50dbc6560a8bf380a73db3f938a9f0d69 Mon Sep 17 00:00:00 2001 From: deninchoo Date: Wed, 25 Apr 2018 09:19:08 +0200 Subject: [PATCH 2/8] Code refactor --- Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 2 +- Model/Config.php | 38 ------------------- 2 files changed, 1 insertion(+), 39 deletions(-) diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 3d7fe199..1baff6de 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -97,7 +97,7 @@ public function execute() $currActiveVersion = $this->vcl->getCurrentVersion($service->versions); $clone = $this->api->cloneVersion($currActiveVersion); $snippets = $this->config->getVclSnippets(); - $customSnippets = $this->config->getCustomVclSnippets(); + $customSnippets = $this->config->getVclSnippets('/vcl_snippets_custom'); foreach ($snippets as $key => $value) { $snippetData = [ diff --git a/Model/Config.php b/Model/Config.php index 7a9044b2..e0ef7424 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -716,44 +716,6 @@ public function getVclSnippets($path = '/vcl_snippets', $specificFile = null) return $snippetsData; } - /** - * Returns custom VCL snippet data - * - * @param string $path - * @param null $specificFile - * @return array - * @throws \Magento\Framework\Exception\FileSystemException - */ - public function getCustomVclSnippets($path = '/vcl_snippets_custom', $specificFile = null) - { - $snippetsData = []; - - $moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Fastly_Cdn') . $path; - $directoryRead = $this->readFactory->create($moduleEtcPath); - if (!$specificFile) { - $files = $directoryRead->read(); - - if (is_array($files)) { - foreach ($files as $file) { - if (substr($file, strpos($file, ".") + 1) !== 'vcl') { - continue; - } - $snippetFilePath = $moduleEtcPath . '/' . $file; - $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); - $type = explode('.', $file)[0]; - $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); - } - } - } else { - $snippetFilePath = $moduleEtcPath . '/' . $specificFile; - $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); - $type = explode('.', $specificFile)[0]; - $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); - } - - return $snippetsData; - } - /** * Prepare data for VCL config * From e6422ffb1c7923c9cc65aee5f5c4e55299959676 Mon Sep 17 00:00:00 2001 From: deninchoo Date: Thu, 17 May 2018 07:33:28 +0200 Subject: [PATCH 3/8] Added custom snippet upload button and custom snippet list --- .../Config/Form/Field/CustomSnippets.php | 115 ++++++++++++++++++ .../FastlyCdn/Vcl/DeleteCustomSnippet.php | 82 +++++++++++++ .../FastlyCdn/Vcl/GetCustomSnippets.php | 85 +++++++++++++ Controller/Adminhtml/FastlyCdn/Vcl/Upload.php | 14 ++- Model/Config.php | 28 +++++ Model/Config/Backend/CustomSnippetUpload.php | 51 ++++++++ etc/adminhtml/system.xml | 15 +++ .../templates/system/config/dialogs.phtml | 6 +- .../config/form/field/customSnippets.phtml | 72 +++++++++++ view/adminhtml/web/js/uploadvcl.js | 63 ++++++++++ 10 files changed, 529 insertions(+), 2 deletions(-) create mode 100644 Block/System/Config/Form/Field/CustomSnippets.php create mode 100644 Controller/Adminhtml/FastlyCdn/Vcl/DeleteCustomSnippet.php create mode 100644 Controller/Adminhtml/FastlyCdn/Vcl/GetCustomSnippets.php create mode 100644 Model/Config/Backend/CustomSnippetUpload.php create mode 100644 view/adminhtml/templates/system/config/form/field/customSnippets.phtml diff --git a/Block/System/Config/Form/Field/CustomSnippets.php b/Block/System/Config/Form/Field/CustomSnippets.php new file mode 100644 index 00000000..c304bea0 --- /dev/null +++ b/Block/System/Config/Form/Field/CustomSnippets.php @@ -0,0 +1,115 @@ +elementFactory = $elementFactory; + + parent::__construct($context, $data); + } + + /** + * Initialise form fields + * + * @return void + */ + protected function _construct() // @codingStandardsIgnoreLine - required by parent class + { + $this->addColumn('snippet_name', ['label' => __('Name')]); + $this->_addAfter = false; + $this->_template = 'Fastly_Cdn::system/config/form/field/customSnippets.phtml'; + + parent::_construct(); + } + + /** + * Render array cell for prototypeJS template + * + * @param string $columnName + * @return mixed|string + * @throws \Exception + */ + public function renderCellTemplate($columnName) + { + if ($columnName == 'store_id' && isset($this->_columns[$columnName])) { + $options = $this->getOptions(__('-- Select Store --')); + $element = $this->elementFactory->create('select'); + $element->setForm( + $this->getForm() + )->setName( + $this->_getCellInputElementName($columnName) + )->setHtmlId( + $this->_getCellInputElementId('<%- _id %>', $columnName) + )->setValues( + $options + ); + return str_replace("\n", '', $element->getElementHtml()); + } + + return parent::renderCellTemplate($columnName); + } + + /** + * Get list of store views. + * + * @param bool|false $label + * @return array + */ + protected function getOptions($label = false) // @codingStandardsIgnoreLine - required by parent class + { + $options = []; + foreach ($this->_storeManager->getStores() as $store) { + $options[] = ['value' => $store->getId(), 'label' => $store->getName()]; + } + + if ($label) { + array_unshift($options, ['value' => '', 'label' => $label]); + } + + return $options; + } +} diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/DeleteCustomSnippet.php b/Controller/Adminhtml/FastlyCdn/Vcl/DeleteCustomSnippet.php new file mode 100644 index 00000000..0ce476e4 --- /dev/null +++ b/Controller/Adminhtml/FastlyCdn/Vcl/DeleteCustomSnippet.php @@ -0,0 +1,82 @@ +request = $request; + $this->resultJson = $resultJsonFactory; + $this->config = $config; + $this->customSnippetUpload = $customSnippetUpload; + $this->file = $file; + + parent::__construct($context); + } + + public function execute() + { + $result = $this->resultJson->create(); + + try { + $snippet = $this->getRequest()->getParam('snippet_id'); + $customSnippetPath = $this->customSnippetUpload->getUploadDirPath('vcl_snippets_custom'); + + if ($this->file->isExists($customSnippetPath . '/' . $snippet)) { + $this->file->deleteFile($customSnippetPath . '/' . $snippet); + } + return $result->setData([ + 'status' => true + ]); + } catch (\Exception $e) { + return $result->setData([ + 'status' => false, + 'msg' => $e->getMessage() + ]); + } + } +} diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/GetCustomSnippets.php b/Controller/Adminhtml/FastlyCdn/Vcl/GetCustomSnippets.php new file mode 100644 index 00000000..afb21478 --- /dev/null +++ b/Controller/Adminhtml/FastlyCdn/Vcl/GetCustomSnippets.php @@ -0,0 +1,85 @@ +request = $request; + $this->resultJson = $resultJsonFactory; + $this->customSnippetUpload = $customSnippetUpload; + $this->readFactory = $readFactory; + parent::__construct($context); + } + + /** + * Get all backends for active version + * + * @return $this|ResponseInterface|ResultInterface + */ + public function execute() + { + try { + $result = $this->resultJson->create(); + $customSnippetPath = $this->customSnippetUpload->getUploadDirPath('vcl_snippets_custom'); + $directoryRead = $this->readFactory->create($customSnippetPath); + $customSnippets = $directoryRead->read(); + + if (!$customSnippets) { + return $result->setData([ + 'status' => false, + 'msg' => 'Failed to check custom snippet details.' + ]); + } + + return $result->setData([ + 'status' => true, + 'snippets' => $customSnippets + ]); + } catch (\Exception $e) { + return $result->setData([ + 'status' => false, + 'msg' => $e->getMessage() + ]); + } + } +} diff --git a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php index 1baff6de..0e4fbde1 100644 --- a/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php +++ b/Controller/Adminhtml/FastlyCdn/Vcl/Upload.php @@ -9,6 +9,7 @@ use Fastly\Cdn\Model\Config; use Fastly\Cdn\Model\Api; use Fastly\Cdn\Helper\Vcl; +use Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload; use Magento\Framework\Stdlib\DateTime\DateTime; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Framework\Exception\LocalizedException; @@ -40,6 +41,11 @@ class Upload extends Action */ private $vcl; + /** + * @var CustomSnippetUpload + */ + private $customSnippetUpload; + /** * @var DateTime */ @@ -51,13 +57,16 @@ class Upload extends Action private $timezone; /** + * * Upload constructor. + * * @param Context $context * @param Http $request * @param JsonFactory $resultJsonFactory * @param Config $config * @param Api $api * @param Vcl $vcl + * @param CustomSnippetUpload $customSnippetUpload * @param DateTime $time * @param TimezoneInterface $timezone */ @@ -68,6 +77,7 @@ public function __construct( Config $config, Api $api, Vcl $vcl, + CustomSnippetUpload $customSnippetUpload, DateTime $time, TimezoneInterface $timezone ) { @@ -76,6 +86,7 @@ public function __construct( $this->config = $config; $this->api = $api; $this->vcl = $vcl; + $this->customSnippetUpload = $customSnippetUpload; $this->time = $time; $this->timezone = $timezone; parent::__construct($context); @@ -97,7 +108,8 @@ public function execute() $currActiveVersion = $this->vcl->getCurrentVersion($service->versions); $clone = $this->api->cloneVersion($currActiveVersion); $snippets = $this->config->getVclSnippets(); - $customSnippets = $this->config->getVclSnippets('/vcl_snippets_custom'); + $customSnippetPath = $this->customSnippetUpload->getUploadDirPath('vcl_snippets_custom'); + $customSnippets = $this->config->getCustomSnippets($customSnippetPath); foreach ($snippets as $key => $value) { $snippetData = [ diff --git a/Model/Config.php b/Model/Config.php index e0ef7424..dcb6aa7f 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -716,6 +716,34 @@ public function getVclSnippets($path = '/vcl_snippets', $specificFile = null) return $snippetsData; } + public function getCustomSnippets($path, $specificFile = null) + { + $snippetsData = []; + $directoryRead = $this->readFactory->create($path); + if (!$specificFile) { + $files = $directoryRead->read(); + + if (is_array($files)) { + foreach ($files as $file) { + if (substr($file, strpos($file, ".") + 1) !== 'vcl') { + continue; + } + $snippetFilePath = $path . '/' . $file; + $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); + $type = explode('.', $file)[0]; + $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); + } + } + } else { + $snippetFilePath = $path . '/' . $specificFile; + $snippetFilePath = $directoryRead->getRelativePath($snippetFilePath); + $type = explode('.', $specificFile)[0]; + $snippetsData[$type] = $directoryRead->readFile($snippetFilePath); + } + + return $snippetsData; + } + /** * Prepare data for VCL config * diff --git a/Model/Config/Backend/CustomSnippetUpload.php b/Model/Config/Backend/CustomSnippetUpload.php new file mode 100644 index 00000000..16f6a38e --- /dev/null +++ b/Model/Config/Backend/CustomSnippetUpload.php @@ -0,0 +1,51 @@ +getFieldConfig(); + + if (!array_key_exists('upload_dir', $fieldConfig)) { + throw new \Magento\Framework\Exception\LocalizedException( + __('The base directory to upload file is not specified.') + ); + } + + if (is_array($fieldConfig['upload_dir'])) { + $uploadDir = $fieldConfig['upload_dir']['value']; + if (array_key_exists('scope_info', $fieldConfig['upload_dir']) + && $fieldConfig['upload_dir']['scope_info'] + ) { + $uploadDir = $this->_appendScopeInfo($uploadDir); + } + + if (array_key_exists('config', $fieldConfig['upload_dir'])) { + $uploadDir = $this->getUploadDirPath($uploadDir); + } + } else { + $uploadDir = (string)$fieldConfig['upload_dir']; + } + + return $uploadDir; + } + + public function getUploadDirPath($uploadDir) + { + $varDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); + return $varDirectory->getAbsolutePath($uploadDir); + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 8a518d8f..9d0af570 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -267,6 +267,21 @@ 1 + + + + + Fastly\Cdn\Model\Config\Backend\CustomSnippetUpload + vcl_snippets_custom + + + + Fastly\Cdn\Block\System\Config\Form\Field\CustomSnippets + + + + + diff --git a/view/adminhtml/templates/system/config/dialogs.phtml b/view/adminhtml/templates/system/config/dialogs.phtml index 61d0c528..1bcf5118 100644 --- a/view/adminhtml/templates/system/config/dialogs.phtml +++ b/view/adminhtml/templates/system/config/dialogs.phtml @@ -1064,6 +1064,7 @@ $toggleImageSettingUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/pushImageSettin $checkBlockingSettingUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/checkBlockingSetting'); $toggleBlockingSettingUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/blocking'); $fetchBackendsUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/getBackends'); +$getCustomSnippetsUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/getCustomSnippets'); $configureBackendUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/configureBackend'); $isAlreadyConfiguredUrl = $block->getUrl('adminhtml/fastlyCdn_Vcl/isAlreadyConfigured'); $savePageErrorHtml = $block->getUrl('adminhtml/fastlyCdn_Vcl/SaveErrorPageHtml'); @@ -1076,6 +1077,7 @@ $createDictionaryItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary_Item $deleteDictionaryItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Dictionary_Item/Delete'); $createAcl = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/Create'); $deleteAcl = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/Delete'); +$deleteCustomSnippet = $block->getUrl('adminhtml/fastlyCdn_Vcl/DeleteCustomSnippet'); $listAcls = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl/ListAll'); $aclItems = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl_Item/ListAll'); $createAclItem = $block->getUrl('adminhtml/fastlyCdn_Edge_Acl_Item/Create'); @@ -1127,6 +1129,7 @@ $isFastlyEnabled = $block->getConfig()->isFastlyEnabled(); "deleteDictionaryItem": "escapeJsQuote($deleteDictionaryItem);?>", "createAcl": "escapeJsQuote($createAcl);?>", "deleteAcl": "escapeJsQuote($deleteAcl);?>", + "deleteCustomSnippet": "escapeJsQuote($deleteCustomSnippet);?>", "getAcls": "escapeJsQuote($listAcls);?>", "getAclItems": "escapeJsQuote($aclItems);?>", "createAclItem": "escapeJsQuote($createAclItem);?>", @@ -1141,7 +1144,8 @@ $isFastlyEnabled = $block->getConfig()->isFastlyEnabled(); "toggleAuthSettingUrl": "escapeJsQuote($toggleAuthSettingUrl);?>", "checkAuthUsersAvailable": "escapeJsQuote($checkAuthUsersAvailable);?>", "listIoDefaultConfigOptions": "escapeJsQuote($listIoDefaultConfigOptions);?>", - "configureIoUrl": "escapeJsQuote($configureIoUrl);?>" + "configureIoUrl": "escapeJsQuote($configureIoUrl);?>", + "getCustomSnippetsUrl": "escapeJsQuote($getCustomSnippetsUrl);?>" } } } diff --git a/view/adminhtml/templates/system/config/form/field/customSnippets.phtml b/view/adminhtml/templates/system/config/form/field/customSnippets.phtml new file mode 100644 index 00000000..4df50d46 --- /dev/null +++ b/view/adminhtml/templates/system/config/form/field/customSnippets.phtml @@ -0,0 +1,72 @@ + + +getHtmlId() ? $block->getHtmlId() : '_' . uniqid(); +$_colspan = $block->isAddAfter() ? 2 : 1; +?> + +
+
+ + + + getColumns() as $columnName => $column) : ?> + + + + + + +
Action
+
+ + +
+ + + + + + + + diff --git a/view/adminhtml/web/js/uploadvcl.js b/view/adminhtml/web/js/uploadvcl.js index d2bd5b4d..9add329a 100644 --- a/view/adminhtml/web/js/uploadvcl.js +++ b/view/adminhtml/web/js/uploadvcl.js @@ -428,6 +428,21 @@ define([ // TO DO: implement }); + // Fetch custom snippets + vcl.getCustomSnippets(false).done(function (snippetsResp) { + $('.loading-backends').hide(); + if (snippetsResp.status != false) { + if (snippetsResp.snippets.length > 0) { + backends = snippetsResp.snippets; + vcl.processCustomSnippets(snippetsResp.snippets); + } else { + $('.no-snippets').show(); + } + } + }).fail(function () { + // TO DO: implement + }); + // Fetch dictionaries vcl.listDictionaries(active_version, false).done(function (dictResp) { $('.loading-dictionaries').hide(); @@ -575,6 +590,20 @@ define([ } }); + $('body').on('click', 'button.fastly-delete-snippet-icon', function () { + var snippet_id = $(this).data('snippet-id'); + if (confirm("Are you sure you want to delete "+ snippet_id +"?")) { + vcl.deleteCustomSnippet(snippet_id, true).done(function (response) { + if (response.status == true) { + $(self).closest('tr').remove(); + vcl.showSuccessMessage($.mage.__('Custom snippet successfully deleted.')); + } + }).fail(function () { + vcl.showErrorMessage($.mage.__('An error occurred while processing your request. Please try again.')); + }); + } + }); + // delete acl container button $('body').on('click', 'button.fastly-delete-acl-icon', function () { $.ajax({ @@ -1372,6 +1401,18 @@ define([ }); }, + // Retrieve custom snippets + getCustomSnippets: function (loaderVisibility) { + return $.ajax({ + type: "GET", + url: config.getCustomSnippetsUrl, + showLoader: loaderVisibility, + beforeSend: function (xhr) { + $('.loading-backends').show(); + } + }); + }, + // Queries Fastly API to retrive Backends getErrorPageRespObj: function (active_version, loaderVisibility) { return $.ajax({ @@ -1393,6 +1434,16 @@ define([ }); }, + // Process custom snippets + processCustomSnippets: function (snippets) { + $.each(snippets, function (index, snippet) { + var html = ""; + html += ""; + html += "