Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.0 #132

Merged
merged 21 commits into from
Sep 6, 2023
Merged

3.0.0 #132

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.0'
- name: Checkout
uses: actions/checkout@v3
- name: Cache dependencies
Expand Down
12 changes: 6 additions & 6 deletions _dev/public/iframe.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/* hn editor */


button {
#toolbar button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
Expand All @@ -21,11 +21,11 @@
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
#toolbar button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
#toolbar button:focus,
#toolbar button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

Expand All @@ -34,10 +34,10 @@
color: #213547;
background-color: #ffffff;
}
a:hover {
#toolbar a:hover {
color: #747bff;
}
button {
#toolbar button {
background-color: #f9f9f9;
}
}
Expand Down
1 change: 1 addition & 0 deletions _dev/src/components/FieldRepeater.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const removeTinyNotifications = () => {
<div v-if="f.type == 'select'">
<Choices :choices="f.choices" v-model="f.value" :label="f.label" />
</div>

<div v-if="f.type == 'multiselect'">
<MultiSelect v-model="f.value" :label="f.label" :options="f.choices" searchable="true" mode="tags"></MultiSelect>
</div>
Expand Down
12 changes: 9 additions & 3 deletions _dev/src/components/form/MultiSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ import { trans } from '../../scripts/trans'
const props = defineProps({
label: String,
mode: String,
modelValue: Array,
modelValue: {
type: Array,
default: []
},
options: Array,
searchable: Boolean,
})

defineComponent({
VueFormMultiselect,
})

let currentOptions = ref(props.modelValue)
let currentOptions = ref([])
if(Array.isArray(props.modelValue))
{
currentOptions = ref(props.modelValue)
}
const emit = defineEmits(['update:modelValue'])

function onChange(e) {
Expand Down
11 changes: 11 additions & 0 deletions _dev/src/scripts/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ async loadIframe () {
html.then((data) => {
let domBlock = body.querySelector('[data-id-prettyblocks="' + currentBlock.id_prettyblocks + '"]')
domBlock.innerHTML = data

const tb = new toolbar( body.querySelectorAll('.ptb-title'), doc, iwindow);
this.loadToolBar(tb)
})

})

// when iframe loaded, get blocks
Expand Down Expand Up @@ -297,6 +301,12 @@ async updateTitleComponent(newValue)
{
let id_block = newValue.html.closest('[data-id-prettyblocks]').getAttribute('data-id-prettyblocks')
let field = newValue.html.getAttribute('data-field')
let index = null
if(newValue.html.hasAttribute('data-index'))
{
index = newValue.html.getAttribute('data-index')
}

let element = await Block.loadById(id_block)
// emitter.emit('displayBlockConfig', element)
let context = contextShop()
Expand All @@ -307,6 +317,7 @@ async updateTitleComponent(newValue)
ctx_id_shop: context.id_shop,
field: field,
ajax: true,
index: index,
action: 'updateTitleComponent',
ajax_token: security_app.ajax_token
}
Expand Down
4 changes: 2 additions & 2 deletions classes/HelperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static function hookToArray($hookName, $params = [])
if (!is_array($formField)) {
continue;
}
foreach ($formField as $array) {
$res[] = $array;
foreach ($formField as $key => $array) {
$res[$key] = $array;
}
}
}
Expand Down
245 changes: 244 additions & 1 deletion classes/PrettyBlocksMigrate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use PrestaSafe\PrettyBlocks\Core\FieldCore;
use PrestaSafe\PrettyBlocks\Core\PrettyBlocksField;

class PrettyBlocksMigrate
Expand All @@ -24,7 +25,10 @@ public static function addTemplateField()

public static function migrateConfig()
{
self::addTemplateField();
if (!self::columnExists('prettyblocks', 'template')
&& !self::columnExists('prettyblocks', 'default_params')) {
self::addTemplateField();
}
$langs = \Language::getLanguages();
$res = true;
$fields = [];
Expand Down Expand Up @@ -67,6 +71,245 @@ public static function migrateConfig()
return $res;
}

public static function columnExists($tableName, $columnName)
{
$tableName = _DB_PREFIX_ . $tableName;
$sql = "SHOW COLUMNS FROM `$tableName` LIKE '$columnName'";
$result = Db::getInstance()->executeS($sql);

return !empty($result);
}

public static function tableExists($tableName)
{
$tableName = _DB_PREFIX_ . $tableName;
$sql = "SHOW TABLES LIKE '$tableName'";
$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
* for version 3.0.0
*/
public static function migrateLangTable()
{
if (!self::tableExists('prettyblocks_lang')) {
return true;
}
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) {
$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->state = $block['state'];
$newBlocks->code = $existingBlock->code;
$newBlocks->zone_name = $existingBlock->zone_name;
$newBlocks->position = $existingBlock->position;

$newBlocks->name = $existingBlock->name;
$newBlocks->config = $existingBlock->config;
$newBlocks->template = $existingBlock->template;
$newBlocks->default_params = $existingBlock->default_params;
$newBlocks->save();

$existingBlock->delete();
}

$sql = 'DROP TABLE IF EXISTS ' . _DB_PREFIX_ . 'prettyblocks_lang';
\Db::getInstance()->execute($sql);

return true;
}

/**
* migrateSettings
* migrate settings from config to database
*
* @return bool
*/
public static function migrateSettings()
{
$key = '%\_SETTINGS';

$sql = '
SELECT name, value
FROM ' . _DB_PREFIX_ . 'configuration
WHERE name LIKE "' . pSQL($key) . '"
AND name != "PS_REWRITING_SETTINGS"
';
// get settings value from config values
$configLines = Db::getInstance()->executeS($sql);
$settings_value = [];
if ($configLines) {
foreach ($configLines as $configLine) {
$setting = str_replace('_SETTINGS', '', $configLine['name']);
$setting = strtolower($setting);
$settings_value[$setting] = $configLine['value'];
}
}

// get settings fields:

$settings_on_hooks = \HelperBuilder::hookToArray('ActionRegisterThemeSettings');
$res = [];

foreach ($settings_on_hooks as $key => $field) {
$fieldCore = (new FieldCore($field));
if (isset($settings_value[$key])) {
$fieldCore->setAttribute('value', $settings_value[$key]);
}
$res[$key] = $fieldCore->compile();
}

$theme_name = Context::getContext()->shop->theme_name;
$can_delete_settings = false;
// if (!self::tableExists('prettyblocks_settings')) {
// $prettyblocks = \Module::getInstanceByName('prettyblocks');
// $prettyblocks->makeSettingsTable();
// }

foreach (Shop::getShops() as $shop) {
$id_shop = (int) $shop['id_shop'];

// get settings from database
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'prettyblocks_settings WHERE theme_name = "' . pSQL($theme_name) . '" AND id_shop = ' . (int) $id_shop;
$row = Db::getInstance()->getRow($sql);

if ($row) {
// if settings exists, update
$sql = 'UPDATE ' . _DB_PREFIX_ . 'prettyblocks_settings SET settings = "' . pSQL(json_encode($res, true)) . '", profile = "Theme ' . pSQL($theme_name) . '" WHERE theme_name = "' . pSQL($theme_name) . '" AND id_shop = ' . (int) $id_shop;
$result = Db::getInstance()->execute($sql);
} else {
// if settings not exists, create
$data = [
'theme_name' => pSQL($theme_name),
'settings' => pSQL(json_encode($res, true)),
'id_shop' => (int) $id_shop,
'profile' => 'Theme ' . pSQL($theme_name),
];
$result = Db::getInstance()->insert('prettyblocks_settings', $data);
}

if ($result) {
$can_delete_settings = true;
}
}
if ($can_delete_settings) {
foreach ($configLines as $configLine) {
Configuration::deleteByName($configLine['name']);
}
}

return true;
}

/**
* getConfigurationSettings
*
* @param mixed $with_tabs
* @param mixed $context
*
* @return array
*/
public static function getConfigurationSettings($with_tabs = true, $context = 'front')
{
$theme_settings = Hook::exec('ActionRegisterThemeSettings', [], null, true);
$res = [];
$no_tabs = [];
foreach ($theme_settings as $settings) {
foreach ($settings as $name => $params) {
$tab = $params['tab'] ?? 'general';
$params = self::_setThemeFieldValue($name, $params, $context);
$res[$tab][$name] = $params;
$no_tabs[$name] = $params['value'] ?? false;
}
}
if (!$with_tabs) {
return $no_tabs;
}

return $res;
}

private static function _setThemeFieldValue($name, $params, $context)
{
$params['value'] = self::_formatSettingsField($name, $params['type'], $params, $context, false);

return $params;
}

/**
* Format a field for settings
*
* @param string $name
* @param string $type
* @param array $params
* @param string $context (back of front)
* @param bool|array $block
*
* @return any
*/
private static function _formatSettingsField($name, $type, $params, $context, $block = false)
{
$class = new FieldFormatter();
$class::setSuffix('_settings');

switch ($type) {
case 'editor':
return $class::formatFieldText($name, $params, $block, $context);
break;
case 'text':
return $class::formatFieldText($name, $params, $block, $context);
break;
case 'textarea':
return $class::formatFieldText($name, $params, $block, $context);
break;
case 'color':
return $class::formatFieldText($name, $params, $block, $context);
break;
case 'radio':
return $class::formatFieldBoxes($name, $params, $block, $context);
break;
case 'checkbox':
return $class::formatFieldBoxes($name, $params, $block, $context);
break;
case 'fileupload':
return $class::formatFieldUpload($name, $params, $block, $context);
break;
case 'upload':
return $class::formatFieldUpload($name, $params, $block, $context);
break;
case 'selector':
return $class::formatFieldSelector($name, $params, $block, $context);
break;
case 'select':
return $class::formatFieldSelect($name, $params, $block, $context);
break;
case 'radio_group':
return $class::formatFieldRadioGroup($name, $params, $block, $context);
break;
default:
return '';
}
}

/**
* Get default params in Configuration
*
Expand Down
Loading