From 83751cec7ea6c82248a19a86a17f0af22963e3c2 Mon Sep 17 00:00:00 2001 From: Prestasafe Date: Fri, 4 Aug 2023 08:50:32 +0200 Subject: [PATCH] php-cs-fixer --- classes/PrettyBlocksMigrate.php | 43 +++++----- classes/PrettyBlocksModel.php | 53 ++++++------ classes/prettyblocks/component/Title.php | 10 +-- classes/prettyblocks/core/FieldCore.php | 80 ++++++++++--------- .../prettyblocks/core/PrettyBlocksField.php | 28 +++---- .../core/PrettyBlocksStatesField.php | 47 +++++------ controllers/front/ajax.php | 18 ++--- prettyblocks.php | 8 +- 8 files changed, 133 insertions(+), 154 deletions(-) diff --git a/classes/PrettyBlocksMigrate.php b/classes/PrettyBlocksMigrate.php index 73750bcb..6a5e2088 100644 --- a/classes/PrettyBlocksMigrate.php +++ b/classes/PrettyBlocksMigrate.php @@ -24,9 +24,8 @@ public static function addTemplateField() public static function migrateConfig() { - if(!self::columnExists('prettyblocks', 'template') - && !self::columnExists('prettyblocks', 'default_params')) - { + if (!self::columnExists('prettyblocks', 'template') + && !self::columnExists('prettyblocks', 'default_params')) { self::addTemplateField(); } $langs = \Language::getLanguages(); @@ -71,42 +70,41 @@ public static function migrateConfig() return $res; } - public static function columnExists($tableName, $columnName) { + public static function columnExists($tableName, $columnName) + { $tableName = _DB_PREFIX_ . $tableName; $sql = "SHOW COLUMNS FROM `$tableName` LIKE '$columnName'"; $result = Db::getInstance()->executeS($sql); - + // Retourne true si le tableau de résultat n'est pas vide, sinon false. return !empty($result); } - /** - * Migrate lang table + /** + * Migrate lang table * for version 3.0.0 */ public static function migrateLangTable() { - if(!self::columnExists('prettyblocks', 'id_shop') - && !self::columnExists('prettyblocks', 'id_lang') - && !self::columnExists('prettyblocks', 'state')){ - $sql = " - ALTER TABLE "._DB_PREFIX_."prettyblocks + if (!self::columnExists('prettyblocks', 'id_shop') + && !self::columnExists('prettyblocks', 'id_lang') + && !self::columnExists('prettyblocks', 'state')) { + $sql = ' + ALTER TABLE ' . _DB_PREFIX_ . 'prettyblocks ADD COLUMN id_shop int(11) DEFAULT NULL, ADD COLUMN id_lang int(11) DEFAULT NULL, ADD COLUMN state longtext DEFAULT NULL; - "; + '; \Db::getInstance()->execute($sql); } - - $blocks_lang = \Db::getInstance()->executeS("SELECT * FROM "._DB_PREFIX_."prettyblocks_lang"); - foreach($blocks_lang as $block) - { + $blocks_lang = \Db::getInstance()->executeS('SELECT * FROM ' . _DB_PREFIX_ . 'prettyblocks_lang'); + foreach ($blocks_lang as $block) { $existingBlock = new PrettyBlocksModel($block['id_prettyblocks']); $newBlocks = new PrettyBlocksModel(); $newBlocks->instance_id = $existingBlock->instance_id; - $newBlocks->id_shop = (int)$block['id_shop']; - $newBlocks->id_lang = (int)$block['id_lang']; + $newBlocks->id_shop = (int) $block['id_shop']; + $newBlocks->id_lang = (int) $block['id_lang']; $newBlocks->state = $block['state']; $newBlocks->code = $existingBlock->code; $newBlocks->zone_name = $existingBlock->zone_name; @@ -119,12 +117,9 @@ public static function migrateLangTable() $newBlocks->save(); $existingBlock->delete(); - } - - - - $sql = "DROP TABLE IF EXISTS "._DB_PREFIX_."prettyblocks_lang"; + + $sql = 'DROP TABLE IF EXISTS ' . _DB_PREFIX_ . 'prettyblocks_lang'; \Db::getInstance()->execute($sql); return true; diff --git a/classes/PrettyBlocksModel.php b/classes/PrettyBlocksModel.php index 2c3af842..0f6572b5 100644 --- a/classes/PrettyBlocksModel.php +++ b/classes/PrettyBlocksModel.php @@ -1,9 +1,7 @@ ['type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'], - 'state' => ['type' => self::TYPE_SQL, 'validate' => 'isJson', ], + 'state' => ['type' => self::TYPE_SQL, 'validate' => 'isJson'], 'zone_name' => ['type' => self::TYPE_STRING, 'validate' => 'isCleanHtml'], 'position' => ['type' => self::TYPE_INT, 'validate' => 'isInt'], 'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'], @@ -129,7 +127,6 @@ public function removeConfig() return true; } - /** * Display blocks for one zone * @@ -166,51 +163,56 @@ public static function getInstanceByZone($zone_name, $context = 'back', $id_lang return $blocks; } - + /** * saveConfigField * - * @param mixed $name - * @param mixed $new_value + * @param mixed $name + * @param mixed $new_value + * * @return bool */ public function saveConfigField($name, $new_value) { - if(isset($this->configFields[$name])){ - $newField = $this->configFields[$name]->setAttribute('new_value',$new_value)->compile(); + if (isset($this->configFields[$name])) { + $newField = $this->configFields[$name]->setAttribute('new_value', $new_value)->compile(); $jsonConfig = json_decode($this->config, true); if (!is_null($jsonConfig)) { $json = []; } $json[$name] = $newField; $this->config = json_encode($json, true); + return $this->save(); } + return false; } - /** * saveStateField * - * @param mixed $index - * @param mixed $name - * @param mixed $new_value + * @param mixed $index + * @param mixed $name + * @param mixed $new_value + * * @return bool */ - public function saveStateField($index,$name,$new_value) + public function saveStateField($index, $name, $new_value) { // get state in json, replace it if exist and save model. - if(isset($this->stateFields[$index][$name])){ - $newField = $this->stateFields[$index][$name]->setAttribute('new_value',$new_value)->compile(); + if (isset($this->stateFields[$index][$name])) { + $newField = $this->stateFields[$index][$name]->setAttribute('new_value', $new_value)->compile(); $jsonConfig = json_decode($this->state, true); if (is_null($jsonConfig)) { $jsonConfig = []; } $jsonConfig[$index][$name] = $newField; $this->state = json_encode($jsonConfig, true); + return $this->save(); } + return false; } @@ -259,7 +261,6 @@ public function mergeStateWithFields() $block['extra'] = $res; $block['templates'] = $this->_getBlockTemplate($block); - return $block; } @@ -298,11 +299,11 @@ public function setConfigFields($fields) */ public function assignFields($block = false, $context = 'front', $force_values = false) { - $this->count++; + ++$this->count; if (!$block) { $block = $this->mergeStateWithFields(); } - + $fieldCore = new PrettyBlocksField($block); $this->configFields = $fieldCore->getConfigFields(); $this->stateFields = $fieldCore->getStatesFields(); @@ -361,7 +362,7 @@ private function _setConfigTemplate($block, $template_name) */ private function _formatStateForFront($state, $repeatedFields, $block) { - $empty_state = []; + $empty_state = []; foreach ($state as $s) { $formatted = []; if (empty($s)) { @@ -554,12 +555,13 @@ private function _getBlockTemplate(&$block) return $block['templates'] + $res; } - /** * _getDefaultParams * return default template if not exist * prettyblocks:views/templates/blocks/welcome.tpl - * @param mixed $block + * + * @param mixed $block + * * @return void */ private function _getDefaultParams($block) @@ -577,12 +579,13 @@ private function _getDefaultParams($block) return $defaultParams; } - /** * _formatGetConfig * get field with front value only - * @param mixed $block - * @param mixed $context + * + * @param mixed $block + * @param mixed $context + * * @return array */ private function _formatGetConfig($block, $context = 'front') diff --git a/classes/prettyblocks/component/Title.php b/classes/prettyblocks/component/Title.php index 77039086..7127b35d 100644 --- a/classes/prettyblocks/component/Title.php +++ b/classes/prettyblocks/component/Title.php @@ -34,7 +34,7 @@ class Title implements ComponentInterface private $attributes = []; private $attributesRendered = []; // if in state - private $index = null; + private $index; public function __construct($tag = null, $classes = [], $block, $field) { @@ -62,10 +62,9 @@ public function setValue($value) { $this->value = $value; if ($this->value_from_block) { - if(!is_null($this->index)) - { + if (!is_null($this->index)) { $block_value = $this->block['states'][$this->index][$this->field]['value']; - }else{ + } else { $block_value = $this->block['settings_formatted'][$this->field]['value']; } if (!is_array($block_value)) { @@ -101,8 +100,7 @@ public function render() $smarty->assign('field', $this->field); $smarty->assign('attributes', $this->attributes); $smarty->assign('attributesHTML', $this->renderAttributes()); - if(!is_null($this->index)) - { + if (!is_null($this->index)) { $smarty->assign('index', $this->index); } diff --git a/classes/prettyblocks/core/FieldCore.php b/classes/prettyblocks/core/FieldCore.php index 810e41d2..4614ec4c 100644 --- a/classes/prettyblocks/core/FieldCore.php +++ b/classes/prettyblocks/core/FieldCore.php @@ -20,6 +20,7 @@ */ namespace PrestaSafe\PrettyBlocks\Core; + class FieldCore { public $type; @@ -27,19 +28,20 @@ class FieldCore public $path; public $collection; public $selector; - public $default; + public $default; public $choices; public $force_default_value = true; public $value; - public $new_value = null; + public $new_value; public $allow_html = false; public $id_lang = 0; public $id_shop = 0; - + /** * __construct * - * @param mixed $data + * @param mixed $data + * * @return void */ public function __construct($data = []) @@ -50,47 +52,52 @@ public function __construct($data = []) /** * setAttributeS * - * @param mixed $data + * @param mixed $data + * * @return void */ public function setAttributeS($data) { - foreach($data as $key => $value){ + foreach ($data as $key => $value) { $this->setAttribute($key, $value); } + return $this; } - + /** * setAttribute * - * @param mixed $attribute - * @param mixed $value + * @param mixed $attribute + * @param mixed $value + * * @return void */ public function setAttribute($attribute, $value) { - if(property_exists($this, $attribute)){ + if (property_exists($this, $attribute)) { $this->{$attribute} = $value; } + return $this; } - + /** * getAttribute * - * @param mixed $attribute + * @param mixed $attribute + * * @return any */ public function getAttribute($attribute) { - if(property_exists($this, $attribute)){ + if (property_exists($this, $attribute)) { return $this->{$attribute}; } + return null; } - - + /** * compile * @@ -99,35 +106,36 @@ public function getAttribute($attribute) public function compile() { $data = []; - if($this->type){ + if ($this->type) { $data['type'] = $this->type; } - if($this->label){ + if ($this->label) { $data['label'] = $this->label; } - if($this->path){ + if ($this->path) { $data['path'] = $this->path; } - if($this->collection){ + if ($this->collection) { $data['collection'] = $this->collection; } - if($this->selector){ + if ($this->selector) { $data['selector'] = $this->selector; } - if($this->default){ + if ($this->default) { $data['default'] = $this->default; } - if($this->choices){ + if ($this->choices) { $data['choices'] = $this->choices; } - if($this->force_default_value){ + if ($this->force_default_value) { $data['force_default_value'] = $this->force_default_value; } - + $data['value'] = $this->format(); + return $data; } - + /** * toArray * @@ -137,7 +145,7 @@ public function toArray() { return $this->compile(); } - + /** * getFrontValue * @@ -158,8 +166,6 @@ public function getFormattedValue() return $this->getValue(); } - - /* | |-------------------------------------------------------------------------- @@ -168,7 +174,6 @@ public function getFormattedValue() | */ - /** * format * @@ -185,7 +190,6 @@ public function format() return false; } - /** * formatForFront * @@ -197,7 +201,7 @@ public function formatForFront() if (method_exists($this, $method)) { return $this->{$method}(); } - + return $this->format(); } @@ -208,7 +212,7 @@ public function formatForFront() |-------------------------------------------------------------------------- | */ - + /** * formatFieldTitle * @@ -264,16 +268,16 @@ public function secureTitleEntry($array) return $element; } - + /** * _clearValue * - * @param mixed $value + * @param mixed $value + * * @return void */ public function _clearValue($value) { - $new_value = str_replace(["\r", "\n"], '', $value); $new_value = str_replace('\\n', '', $new_value); @@ -673,8 +677,7 @@ public function secureCollectionEntry($array) */ public function secureTextEntry($string) { - if(is_array($string) && isset($string['value'])) - { + if (is_array($string) && isset($string['value'])) { $string = $string['value']; } if ($this->allow_html) { @@ -708,5 +711,4 @@ public function secureFileUploadEntry($array) return $secure; } - -} \ No newline at end of file +} diff --git a/classes/prettyblocks/core/PrettyBlocksField.php b/classes/prettyblocks/core/PrettyBlocksField.php index 92598fdc..4f3943f0 100644 --- a/classes/prettyblocks/core/PrettyBlocksField.php +++ b/classes/prettyblocks/core/PrettyBlocksField.php @@ -21,8 +21,6 @@ namespace PrestaSafe\PrettyBlocks\Core; -use PrestaShop\PrestaShop\Adapter\Presenter\Object\ObjectPresenter; - class PrettyBlocksField { public $block; @@ -32,8 +30,6 @@ class PrettyBlocksField public $settings = []; public $id_lang = 0; public $id_shop = 0; - - /** * Constructor @@ -52,11 +48,13 @@ public function __construct($block) * Set id_lang * * @param int $id_lang + * * @return self */ public function setIdLang($id_lang) { $this->id_lang = $id_lang; + return $this; } @@ -64,11 +62,13 @@ public function setIdLang($id_lang) * Set id_shop * * @param int $id_shop + * * @return self */ public function setIdShop($id_shop) { $this->id_shop = $id_shop; + return $this; } @@ -77,9 +77,10 @@ public function setFields($fields) $this->setData(); $this->setConfigFields(); $this->setStatesFields(); + return $this; } - + /** * setConfigFields * @@ -87,8 +88,7 @@ public function setFields($fields) */ public function setConfigFields() { - if(!isset($this->block['config']['fields'])) - { + if (!isset($this->block['config']['fields'])) { return []; } @@ -98,14 +98,14 @@ public function setConfigFields() foreach ($this->block['config']['fields'] as $key => $field) { $field['id_lang'] = $this->id_lang; $field['id_shop'] = $this->id_shop; - if(isset($configDaved[$key]['value'])) - { + if (isset($configDaved[$key]['value'])) { // merged value db if exist $field['value'] = $configDaved[$key]['value']; } $fields[$key] = (new FieldCore($field)); } $this->config = $fields; + return $this; } @@ -116,24 +116,22 @@ public function getConfigFields() public function setStatesFields() { - if(!isset($this->block['states_json'])) - { + if (!isset($this->block['states_json'])) { return []; } $fields = []; foreach ($this->block['states_json'] as $index => $data) { $stateFields = []; - foreach($data as $key => $value) - { + foreach ($data as $key => $value) { $value['id_lang'] = $this->id_lang; $value['id_shop'] = $this->id_shop; $stateFields[$key] = (new FieldCore($value)); } $fields[$index] = $stateFields; - } $this->states = $fields; + return $this; } @@ -152,9 +150,9 @@ public function setKey($key) { return $this; } + public function setContext() { return $this; } - } diff --git a/classes/prettyblocks/core/PrettyBlocksStatesField.php b/classes/prettyblocks/core/PrettyBlocksStatesField.php index ed8d8c29..0d8514b3 100644 --- a/classes/prettyblocks/core/PrettyBlocksStatesField.php +++ b/classes/prettyblocks/core/PrettyBlocksStatesField.php @@ -35,7 +35,7 @@ class PrettyBlocksStatesField extends PrettyBlocksField public $label = ''; public $type = ''; - /** + /** * Set all essential Data * * @return $this @@ -63,7 +63,6 @@ public function get() $this->force_default_value = true; } - $this->setValues(); return $this; @@ -71,7 +70,6 @@ public function get() private function _setFieldState() { - if (isset($this->block['repeater_db'][$this->index][$this->key])) { $this->field = $this->block['repeater_db'][$this->index][$this->key]; } @@ -79,7 +77,7 @@ private function _setFieldState() return $this; } - /** + /** * @param string $key * * @return $this @@ -87,10 +85,10 @@ private function _setFieldState() public function setIndex($index) { $this->index = $index; + return $this; } - public function setKey($key) { $this->key = $key; @@ -100,10 +98,8 @@ public function setKey($key) return $this; } - public function setValues() { - // bug $values = $this->getFormattedConfig(); // dump($this->field); @@ -114,26 +110,25 @@ public function setValues() $this->field['value'] = $this->formattedValue; $this->value = $this->field; } + return $this; } public function getFormattedConfig() { $value = []; - + $jsonConfig = $this->model->state; if (!is_null($jsonConfig) && !\Validate::isJson($jsonConfig)) { return $value; } $json = json_decode($jsonConfig, true); + return $json; } - - public function save() { - $json = $this->_setFormattedValue(); $json = json_encode($json, true); $this->model->state = $json; @@ -141,26 +136,22 @@ public function save() $this->_assignValues($this->newValue); } - return $this; } /** - * set formatted value - * - * @return array - */ - public function _setFormattedValue() - { - - $data = $this->getFormattedConfig(); - $data[$this->index][$this->key] = $this->format(); - - $this->formattedValue = $this->formatForFront(); - $this->field['value'] = $this->formattedValue; - - return $data; - } + * set formatted value + * + * @return array + */ + public function _setFormattedValue() + { + $data = $this->getFormattedConfig(); + $data[$this->index][$this->key] = $this->format(); + $this->formattedValue = $this->formatForFront(); + $this->field['value'] = $this->formattedValue; -} + return $data; + } +} diff --git a/controllers/front/ajax.php b/controllers/front/ajax.php index 688b8122..d92d3845 100755 --- a/controllers/front/ajax.php +++ b/controllers/front/ajax.php @@ -18,9 +18,6 @@ * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) * International Registered Trademark & Property of PrestaSafe */ - -use PrestaSafe\PrettyBlocks\Core\PrettyBlocksStatesField; - if (!defined('_PS_VERSION_')) { exit; } @@ -375,21 +372,18 @@ public function displayAjaxupdateTitleComponent() $id_shop = (int) Tools::getValue('ctx_id_shop'); $id_prettyblocks = (int) Tools::getValue('id_prettyblocks'); $element = Tools::getValue('element'); - + $model = new \PrettyBlocksModel($id_prettyblocks, $id_lang, $id_shop); $model->mergeStateWithFields(); - if(Tools::getIsset('index')) - { + if (Tools::getIsset('index')) { // save state - $index = (int)Tools::getValue('index'); + $index = (int) Tools::getValue('index'); - if($model->saveStateField((int)$index, pSQL(Tools::getValue('field')), $element)) - { + if ($model->saveStateField((int) $index, pSQL(Tools::getValue('field')), $element)) { $success = true; } - - } else{ - if($model->saveConfigField(pSQL(Tools::getValue('field')), $element)){ + } else { + if ($model->saveConfigField(pSQL(Tools::getValue('field')), $element)) { $success = true; } } diff --git a/prettyblocks.php b/prettyblocks.php index 9a63b887..4f1dc2e7 100755 --- a/prettyblocks.php +++ b/prettyblocks.php @@ -104,7 +104,6 @@ private function createBlockDb() PRIMARY KEY (`id_prettyblocks`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8mb4;'; - $isOk = true; foreach ($db as $sql) { $isOk &= Db::getInstance()->execute($sql); @@ -249,12 +248,11 @@ public static function renderTitle($params) $block = $params['block']; $classes = $params['classes'] ?? []; - $title = new Title($tag, $classes, $block, $field); - if(isset($params['index'])) - { - $title->setIndex((int)$params['index']); + if (isset($params['index'])) { + $title->setIndex((int) $params['index']); } + return $title->setValueFromBlock(true) ->setValue($value)->render(); }