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
The head ref may contain hidden characters: "1141-image-par-d\u00E9faut-dans-les-formulaires-bazar"
Changes from 4 commits
9165bc8
df3bfdc
92068d1
7fbda07
092c1c4
6605976
61b3670
b5f261d
3078bd7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -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;" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a voir si c'est pas mieux de créer une className There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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) | ||
} | ||
|
@@ -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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace YesWiki\Bazar\Service; | ||
|
||
use Attach; | ||
use YesWiki\Bazar\Controller\EntryController; | ||
use YesWiki\Wiki; | ||
use YesWiki\Bazar\Field\BazarField; | ||
|
@@ -41,6 +42,37 @@ public function getForms($options): array | |
return $this->formManager->getAll(); | ||
} | ||
} | ||
|
||
private function replace_default_image($options, $forms, $entries): array { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
{ | ||
|
@@ -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'])) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chouette, du refacto!
There was a problem hiding this comment.
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"