Skip to content

Commit

Permalink
php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
PrestaSafe committed Aug 4, 2023
1 parent dd31275 commit 83751ce
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 154 deletions.
43 changes: 19 additions & 24 deletions classes/PrettyBlocksMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
53 changes: 28 additions & 25 deletions classes/PrettyBlocksModel.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

use PrestaSafe\PrettyBlocks\Core\FieldCore;
use Symfony\Component\Validator\Constraints\Valid;
use PrestaSafe\PrettyBlocks\Core\PrettyBlocksField;
use PrestaSafe\PrettyBlocks\Presenter\FieldPresenter;

/**
* Copyright (c) Since 2020 PrestaSafe and contributors
Expand Down Expand Up @@ -62,7 +60,7 @@ class PrettyBlocksModel extends ObjectModel

// multishop
'instance_id' => ['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'],
Expand Down Expand Up @@ -129,7 +127,6 @@ public function removeConfig()
return true;
}


/**
* Display blocks for one zone
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -259,7 +261,6 @@ public function mergeStateWithFields()
$block['extra'] = $res;
$block['templates'] = $this->_getBlockTemplate($block);


return $block;
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand Down
10 changes: 4 additions & 6 deletions classes/prettyblocks/component/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand Down
Loading

0 comments on commit 83751ce

Please sign in to comment.