From e6232a17eb681f5a6804c141a08376421e1839cd Mon Sep 17 00:00:00 2001 From: Loris Date: Tue, 18 Jul 2023 10:21:13 +0200 Subject: [PATCH] :package: 1.4.0 - Add Support of PrettyBlocks --- mdn_blocks.php | 141 +++++++++++++++--- .../support/prettyblocks/category.tpl | 1 + .../templates/support/prettyblocks/image.tpl | 1 + .../support/prettyblocks/product.tpl | 1 + views/templates/support/prettyblocks/text.tpl | 1 + 5 files changed, 125 insertions(+), 20 deletions(-) create mode 100644 views/templates/support/prettyblocks/category.tpl create mode 100644 views/templates/support/prettyblocks/image.tpl create mode 100644 views/templates/support/prettyblocks/product.tpl create mode 100644 views/templates/support/prettyblocks/text.tpl diff --git a/mdn_blocks.php b/mdn_blocks.php index a1b1887..cc83005 100644 --- a/mdn_blocks.php +++ b/mdn_blocks.php @@ -13,6 +13,10 @@ exit; } class Mdn_Blocks extends Module implements WidgetInterface { + /** + * Menu definition + * @var array[] + */ public $tabs = [ [ 'name' => 'Blocs de contenu', @@ -57,7 +61,7 @@ public function __construct() $this->name = 'mdn_blocks'; $this->author = 'Maison du Net - Loris'; $this->tab = 'front_office_features'; - $this->version = '1.3.1'; + $this->version = '1.4.0'; $this->bootstrap = true; $this->ps_versions_compliancy = array('min' => '1.7.7.0', 'max' => _PS_VERSION_); $this->displayName = "Blocs éditables"; @@ -67,6 +71,10 @@ public function __construct() } + /** + * Register Models & Hooks + * @return bool + */ public function install() { BlocksModel::createContentTable(); @@ -74,15 +82,28 @@ public function install() BlocksCategoryModel::createContentTable(); BlocksProductModel::createContentTable(); - return parent::install(); // TODO: Change the autogenerated stub + return parent::install() && $this->registerHook("ActionRegisterBlock"); } + /** + * Unregister Hooks (we keep models) + * @return bool + */ public function uninstall() { - return parent::uninstall(); // TODO: Change the autogenerated stub + return parent::uninstall() && $this->unregisterHook("ActionRegisterBlock"); } + /** + * Render widget + * @param $hookName + * @param array $configuration + * @return false|string|null + * @throws PrestaShopDatabaseException + * @throws PrestaShopException + * @throws ReflectionException + */ public function renderWidget($hookName = null, array $configuration = []) { if (!$this->active) { @@ -110,28 +131,23 @@ public function renderWidget($hookName = null, array $configuration = []) } + /** + * Get widget variable based on inputed configuration + * @param $hookName + * @param array $configuration + * @return array|void + * @throws PrestaShopDatabaseException + * @throws PrestaShopException + * @throws ReflectionException + */ public function getWidgetVariables($hookName, array $configuration) { $lang = $this->context->language->id ?? Configuration::get('PS_LANG_DEFAULT'); - $model = 'BlocksModel'; - switch ($configuration['type']) { - case "image": - $model = 'BlocksImageModel'; - break; - case "category": - $model = 'BlocksCategoryModel'; - break; - case "text": - $model = 'BlocksModel'; - break; - case "product": - $model = 'BlocksProductModel'; - break; - } + $model = $this->getModelFromType($configuration['type']); - $Slider = new PrestaShopCollection($model); - $results = $Slider + $Collection = new PrestaShopCollection($model); + $results = $Collection ->where('active_block', '=', '1') ->where('technical_id', '=', ($hookName)) ->getAll(); @@ -221,4 +237,89 @@ public function getWidgetVariables($hookName, array $configuration) }, $results->getResults()), 'lang' => $lang]; } } + + /** + * Register Block for compatibility with PRETTYBLOCKS module + * @return array + * @throws PrestaShopException + */ + public function hookActionRegisterBlock() + { + $blocks = []; + + foreach (['text', 'image', 'category', 'product'] as $item) { + $model = $this->getModelFromType($item); + + // get all of collection + $Collection = new PrestaShopCollection($model); + $results = $Collection + ->getAll(); + + // if collection is empty we don't want any block to be display + if(count($results) == 0) + continue; + + // work around for the module unless they fix + $choices = [ + 'default' => 'default', + ]; + + // add any line + foreach ($results as $result) { + $choices[$result->technical_id] = $result->technical_id; + } + + $blocks[] = [ + 'name' => "@mdn_blocks - " .$item, + 'description' => "Support for MDN Blocks Widget Injection ".$item, + 'code' => 'mdn_blocks_'.$item, + 'tab' => 'general', + 'icon' => 'ArchiveBoxIcon', + 'need_reload' => true, + 'templates' => [ + // dynamic template + 'default' => 'module:'.$this->name.'/views/templates/support/prettyblocks/' . $item . '.tpl' + ], + 'config' => [ + 'fields' => [ + 'key' => [ + 'type' => 'select', // type of field + 'label' => 'Choose a value', // label to display + 'default' => count($choices) >= 1 ? array_values($choices)[0] : null, // default value based on first choice + 'choices' => $choices + ], + ], + ], + ]; + } + + // get block listing + return $blocks; + } + + /** + * Transform a type into a model + * @param $type + * @return string|null + */ + public function getModelFromType($type) { + $model = null; + + switch ($type) { + case "image": + $model = 'BlocksImageModel'; + break; + case "category": + $model = 'BlocksCategoryModel'; + break; + case "text": + $model = 'BlocksModel'; + break; + case "product": + $model = 'BlocksProductModel'; + break; + } + + return $model; + } } \ No newline at end of file diff --git a/views/templates/support/prettyblocks/category.tpl b/views/templates/support/prettyblocks/category.tpl new file mode 100644 index 0000000..b76a13d --- /dev/null +++ b/views/templates/support/prettyblocks/category.tpl @@ -0,0 +1 @@ +{widget name="mdn_blocks" type="category" hook=$block.settings.key} \ No newline at end of file diff --git a/views/templates/support/prettyblocks/image.tpl b/views/templates/support/prettyblocks/image.tpl new file mode 100644 index 0000000..9232e34 --- /dev/null +++ b/views/templates/support/prettyblocks/image.tpl @@ -0,0 +1 @@ +{widget name="mdn_blocks" type="image" hook=$block.settings.key} \ No newline at end of file diff --git a/views/templates/support/prettyblocks/product.tpl b/views/templates/support/prettyblocks/product.tpl new file mode 100644 index 0000000..a68d8e7 --- /dev/null +++ b/views/templates/support/prettyblocks/product.tpl @@ -0,0 +1 @@ +{widget name="mdn_blocks" type="product" hook=$block.settings.key} \ No newline at end of file diff --git a/views/templates/support/prettyblocks/text.tpl b/views/templates/support/prettyblocks/text.tpl new file mode 100644 index 0000000..9f218cc --- /dev/null +++ b/views/templates/support/prettyblocks/text.tpl @@ -0,0 +1 @@ +{widget name="mdn_blocks" type="text" hook=$block.settings.key} \ No newline at end of file