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

1141 image par défaut dans les formulaires bazar #1143

Merged
27 changes: 16 additions & 11 deletions includes/services/DbService.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chouette, du refacto!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oui, meme plus que du refacto : quand la requette échouait, on ne l'avait pas en "debug"

Expand Up @@ -65,6 +65,14 @@ public function getQueryLog()
return $this->queryLog;
}

public function addQueryLog($query, $time)
{
$this->queryLog[] = array(
'query' => $query,
'time' => $time
);
}

public function prefixTable($tableName)
{
return ' ' . $this->params->get('table_prefix') . $tableName . ' ';
Expand All @@ -87,18 +95,15 @@ public function query($query)
$start = $this->getMicroTime();
}

if (!$result = mysqli_query($this->link, $query)) {
throw new Exception('Query failed: ' . $query . ' (' . mysqli_error($this->link) . ')');
}

if ($this->params->get('debug')) {
$time = $this->getMicroTime() - $start;
$this->queryLog[] = array(
'query' => $query,
'time' => $time
);
try {
if (!$result = mysqli_query($this->link, $query)) {
throw new Exception('Query failed: ' . $query . ' (' . mysqli_error($this->link) . ')');
}
} finally {
if ($this->params->get('debug')) {
$this->addQueryLog($query, $this->getMicroTime() - $start);
}
}

return $result;
}

Expand Down
9 changes: 8 additions & 1 deletion tools/bazar/fields/ImageField.php
Expand Up @@ -23,6 +23,7 @@ class ImageField extends FileField
protected const FIELD_IMAGE_HEIGHT = 5;
protected const FIELD_IMAGE_WIDTH = 6;
protected const FIELD_IMAGE_CLASS = 7;
protected const FIELD_IMAGE_DEFAULT = 8;
mrflos marked this conversation as resolved.
Show resolved Hide resolved

public function __construct(array $values, ContainerInterface $services)
{
Expand All @@ -34,6 +35,7 @@ public function __construct(array $values, ContainerInterface $services)
$this->imageHeight = $values[self::FIELD_IMAGE_HEIGHT];
$this->imageWidth = $values[self::FIELD_IMAGE_WIDTH];
$this->imageClass = $values[self::FIELD_IMAGE_CLASS];
$this->imageDefault = $values[self::FIELD_IMAGE_DEFAULT];

// We can have no default for images
$this->default = null;
Expand Down Expand Up @@ -164,7 +166,12 @@ public function formatValuesBeforeSave($entry)
protected function renderStatic($entry)
{
$value = $this->getValue($entry);

if (!isset($value) || $value == '') {
$default_image_filename = "defaultimage{$entry['id_typeannonce']}_{$this->name}.jpg";
if (file_exists($this->getBasePath(). $default_image_filename)) {
$value=$default_image_filename;
}
}
if (isset($value) && $value != '' && file_exists($this->getBasePath(). $value)) {
return $this->getWiki()->render('@attach/display-image.twig', [
'baseUrl' => $this->getWiki()->GetBaseUrl().'/',
Expand Down
1 change: 1 addition & 0 deletions tools/bazar/lang/bazarjs_en.inc.php
Expand Up @@ -134,6 +134,7 @@
// 'BAZ_FORM_EDIT_IMAGE_WIDTH' => 'Largeur Vignette',
// 'BAZ_FORM_EDIT_IMAGE_WIDTH_RESIZE' => 'Largeur redimension',
// 'BAZ_FORM_EDIT_IMAGE_HEIGHT_RESIZE' => 'Hauteur redimension',
'BAZ_FORM_EDIT_IMAGE_DEFAULT' => 'Default image',
'BAZ_FORM_EDIT_METADATA_THEME_LABEL' => 'Theme name',
'BAZ_FORM_EDIT_METADATA_SQUELETON_LABEL' => 'Squeleton',
'BAZ_FORM_EDIT_METADATA_STYLE_LABEL' => 'Style',
Expand Down
1 change: 1 addition & 0 deletions tools/bazar/lang/bazarjs_fr.inc.php
Expand Up @@ -149,6 +149,7 @@
'BAZ_FORM_EDIT_IMAGE_WIDTH' => 'Largeur vignette',
'BAZ_FORM_EDIT_IMAGE_WIDTH_RESIZE' => 'Largeur redimension',
'BAZ_FORM_EDIT_IMAGE_HEIGHT_RESIZE' => 'Hauteur redimension',
'BAZ_FORM_EDIT_IMAGE_DEFAULT' => 'Image par défaut',
'BAZ_FORM_EDIT_METADATA_THEME_LABEL' => 'Nom du thème',
'BAZ_FORM_EDIT_METADATA_SQUELETON_LABEL' => 'Squelette',
'BAZ_FORM_EDIT_METADATA_STYLE_LABEL' => 'Style',
Expand Down
Expand Up @@ -15,9 +15,16 @@ export default {
resize_height: { label: _t('BAZ_FORM_EDIT_IMAGE_HEIGHT_RESIZE'), value: '600' },
resize_width: { label: _t('BAZ_FORM_EDIT_IMAGE_WIDTH_RESIZE'), value: '800' },
align: {
label: _t('BAZ_FORM_EDIT_IMAGE_ALIGN_LABEL'),
value: 'right',
options: { left: _t('LEFT'), right: _t('RIGHT') }
label: _t("BAZ_FORM_EDIT_IMAGE_ALIGN_LABEL"),
value: "right",
options: { left: _t("LEFT"), right: _t("RIGHT") },
},
default_image: {
label: _t("BAZ_FORM_EDIT_IMAGE_DEFAULT"),
class: "default-file",
value: "",
type: "file",
accept: "image/*",
},
read: readConf,
write: writeconf,
Expand All @@ -28,13 +35,14 @@ export default {
attributesMapping: {
...defaultMapping,
...{
1: 'name',
3: 'thumb_height',
4: 'thumb_width',
5: 'resize_height',
6: 'resize_width',
7: 'align'
}
1: "name",
3: "thumb_height",
4: "thumb_width",
5: "resize_height",
6: "resize_width",
7: "align",
8: "default_image",
mrflos marked this conversation as resolved.
Show resolved Hide resolved
},
},
renderInput(fieldData) {
return { field: '<input type="file"/>' }
Expand Down
Expand Up @@ -69,3 +69,20 @@ export function adjustJqueryBuilderUI($field) {
$field.find('a[type=copy].formbuilder-icon-copy').attr('title', _t('DUPLICATE'))
$field.find('a[type=edit].formbuilder-icon-pencil').attr('title', _t('BAZ_FORM_EDIT_HIDE'))
}

export function convertToBytes(base64content) {
const byteCharacters = base64content;
const sliceSize = 512;
var byteNumbers;
var slice;
const byteArrays = new [].constructor();
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
slice = byteCharacters.slice(offset, offset + sliceSize);
byteNumbers = new [].constructor(slice.length);
for (var idx = 0; idx < slice.length; idx++) {
byteNumbers[idx] = slice.charCodeAt(idx);
}
byteArrays.push(new Uint8Array(byteNumbers));
}
return byteArrays;
}
Expand Up @@ -31,13 +31,15 @@ import {
mapFieldsConf,
addAdvancedAttributesSection,
adjustDefaultAcls,
adjustJqueryBuilderUI
adjustJqueryBuilderUI,
convertToBytes
} from './form-builder-helper.js'
import { initListOrFormIdAttribute } from './attributes/list-form-id-attribute.js'
import I18nOption from './i18n.js'

const $formBuilderTextInput = $('#form-builder-text')
window.formBuilder = undefined
window.defaultImage = {}

// Use window to make it available outside of module, so extension can adds their own fields
window.formBuilderFields = {
Expand Down Expand Up @@ -120,7 +122,43 @@ function initializeFormbuilder() {
}
})
}, 0)
}
},
onOpenFieldEdit() {
// Default image is in base64 in buffer variable => convert it to File in input element
$('input.default-file').each((idx, file_elem) => {
const current_ids = file_elem.attributes.id.value.split('-')
const image_name = current_ids[current_ids.length - 2] + '-' + current_ids[current_ids.length - 1]
if (window.defaultImage[image_name]) {
const image_content = window.defaultImage[image_name].split('|')
if (image_content.length === 2) {
const dataTransfer = new DataTransfer()
dataTransfer.items.add(new File(convertToBytes(image_content[1]), image_content[0]))
file_elem.files = dataTransfer.files
}
}
if (file_elem.parentElement.childElementCount == 1) {
const new_button = document.createElement("i")
new_button.className = "fas fa-remove"
new_button.style = "position:relative;top:-26px;margin-left:auto;margin-right:0;width:20px;display:block;cursor: pointer;"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a voir si c'est pas mieux de créer une className btn-img-remove et de mettre les propriétés css dans styles/yeswiki-base.css, afin de permettre de changer ces propriétés plus facilement dans les themes et PageCss

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

En effet, je regarde cela

new_button.onclick = function() {
file_elem.files = new DataTransfer().files
window.defaultImage[image_name] = ''
}
file_elem.parentElement.append(new_button)
}
})
// When change image, save it to base64 to buffer variable
$('input.default-file').change((event) => {
const current_ids = event.target.attributes.id.value.split('-')
const image_name = current_ids[current_ids.length - 2] + '-' + current_ids[current_ids.length - 1]
const file = event.target.files[0]
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = function () {
window.defaultImage[image_name] = file.name + '|' + reader.result
}
})
},
})

const defaultFieldsName = mapFieldsConf((conf) => conf.defaultIdentifier)
Expand Down Expand Up @@ -153,6 +191,14 @@ function initializeFormbuilder() {
// Update the text field converting form builder content into wiki syntax
if ($('#form-builder-container').is(':visible')) {
const formData = formBuilder.actions.getData()
// save base64 default image from buffer variable
Object.keys(window.defaultImage).forEach((image_name) => {
if (window.defaultImage[image_name] && window.defaultImage[image_name] != '') {
const image_names = image_name.split('-')
const field_idx = Number(image_names[image_names.length - 1]) - 1
formData[field_idx].default_image = window.defaultImage[image_name]
}
})
const wikiText = formatJsonDataIntoWikiText(formData)
if (wikiText) $formBuilderTextInput.val(wikiText)
}
Expand Down Expand Up @@ -251,6 +297,13 @@ function getFieldsIds() {
function initializeBuilderFromTextInput() {
const jsonData = parseWikiTextIntoJsonData($formBuilderTextInput.val())
try {
window.defaultImage = {}
// extract base64 default image to buffer variable
jsonData.forEach((field, index) => {
if (field.type === 'image') {
window.defaultImage['fld-' + (index + 1)] = field.default_image.trim()
}
})
formBuilder.actions.setData(JSON.stringify(jsonData))
} catch (error) {
console.error(error)
Expand Down
33 changes: 33 additions & 0 deletions tools/bazar/services/BazarListService.php
Expand Up @@ -2,6 +2,7 @@

namespace YesWiki\Bazar\Service;

use Attach;
use YesWiki\Bazar\Controller\EntryController;
use YesWiki\Wiki;
use YesWiki\Bazar\Field\BazarField;
Expand Down Expand Up @@ -41,6 +42,37 @@ public function getForms($options): array
return $this->formManager->getAll();
}
}

private function replace_default_image($options, $forms, $entries): array {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dans les conventions de nommage des fonctions en PHP, on utilise plutot camelCase au lieu du snake_case de python

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oups, je vais renomer mes nouvelles fonctions alors ;)

if (! class_exists('attach')) {
include ('tools/attach/libs/attach.lib.php');
}
$attach = new attach($this->wiki);
$basePath = $attach->GetUploadPath();
$basePath = $basePath . (substr($basePath, - 1) != "/" ? "/" : "");

foreach($options['idtypeannonce'] as $idtypeannonce) {
$template = $forms[(int)$idtypeannonce]['template'];
$image_names = array_map(function($item) {return $item[1];},
array_filter($template,
function($item) { return $item[0] == 'image'; }
)
);
foreach($image_names as $image_name) {
$default_image_filename = "defaultimage{$idtypeannonce}_{$image_name}.jpg";
if (file_exists($basePath.$default_image_filename)) {
$image_key = 'image'.$image_name;
foreach($entries as $key=>$entry) {
if (array_key_exists($image_key, $entry) && ($entry[$image_key] == null)) {
$entry[$image_key] = $default_image_filename;
}
$entries[$key] = $entry;
}
}
}
}
return $entries;
}

public function getEntries($options, $forms = null): array
{
Expand Down Expand Up @@ -71,6 +103,7 @@ public function getEntries($options, $forms = null): array
true // use Guard
);
}
$entries = $this->replace_default_image($options, $forms, $entries);

// filter entries on datefilter parameter
if (!empty($options['datefilter'])) {
Expand Down