Skip to content

Commit

Permalink
📦 1.4.0 - Add Support of PrettyBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Loris committed Jul 18, 2023
1 parent 223f9cf commit e6232a1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 20 deletions.
141 changes: 121 additions & 20 deletions mdn_blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
exit;
}
class Mdn_Blocks extends Module implements WidgetInterface {
/**
* Menu definition
* @var array[]
*/
public $tabs = [
[
'name' => 'Blocs de contenu',
Expand Down Expand Up @@ -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";
Expand All @@ -67,22 +71,39 @@ public function __construct()
}


/**
* Register Models & Hooks
* @return bool
*/
public function install()
{
BlocksModel::createContentTable();
BlocksImageModel::createContentTable();
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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions views/templates/support/prettyblocks/category.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{widget name="mdn_blocks" type="category" hook=$block.settings.key}
1 change: 1 addition & 0 deletions views/templates/support/prettyblocks/image.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{widget name="mdn_blocks" type="image" hook=$block.settings.key}
1 change: 1 addition & 0 deletions views/templates/support/prettyblocks/product.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{widget name="mdn_blocks" type="product" hook=$block.settings.key}
1 change: 1 addition & 0 deletions views/templates/support/prettyblocks/text.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{widget name="mdn_blocks" type="text" hook=$block.settings.key}

0 comments on commit e6232a1

Please sign in to comment.