Commit 7ddad67c authored by Sylvain's avatar Sylvain

Ajout du Field Bt3

parent 56a2cec3
......@@ -28,7 +28,8 @@
],
"aliases": {
"Field": "Goldenscarab\\Modulus\\Service\\Field\\Facades\\Field",
"Field2": "Goldenscarab\\Modulus\\Service\\Field\\Facades\\Field2"
"Field2": "Goldenscarab\\Modulus\\Service\\Field\\Facades\\Field2",
"FieldBt3": "Goldenscarab\\Modulus\\Service\\Field\\Facades\\FieldBt3"
}
}
},
......
<?php
namespace Goldenscarab\Modulus\Service\Field\Compose;
use GuzzleHttp\Client;
use Illuminate\Support\Str;
use \Illuminate\Support\HtmlString;
use Illuminate\Support\Facades\App;
use Goldenscarab\Modulus\Helpers\Dynamik;
class Field2Bt3
{
private $label;
private $name;
private $value;
private $prefix;
private $suffix;
private $attributes;
private $indice;
private $size;
private $inline;
private $required;
private $help;
private $popover;
private $options;
private $is_multiple;
public function __construct(array $args)
{
$this->label = data_get($args, 'label');
$this->name = data_get($args, 'name');
$this->value = data_get($args, 'value');
$this->prefix = data_get($args, 'prefix');
$this->suffix = data_get($args, 'suffix');
$this->attributes = data_get($args, 'attributes', []);
$this->indice = data_get($args, 'indice');
$this->size = data_get($args, 'size');
$this->inline = data_get($args, 'inline');
$this->required = data_get($args, 'required');
$this->help = data_get($args, 'help');
$this->popover = data_get($args, 'popover');
$this->options = data_get($args, 'options');
}
/**
* Retourne le rendu du champs Code Source
*
* @return string Le rendu du champs
*/
public function renderCodeEditor():string
{
$template = '<div id="editor-%s"%s>%s</div>' . PHP_EOL;
$template .= '<textarea id="%s" name="%s" style="display: none;">%s</textarea>';
$attrs_default = [
'class' => $this->makeClasses('code-editor'),
'data-target' => '#' . $this->makeID(),
'style' => 'min-height: 300px;',
'data-language' => 'html'
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->makeID(),
$this->renderAttributes($this->attributes),
e($this->makeValue()),
$this->makeID(),
$this->makeName(),
e($this->makeValue()),
]);
return $render;
}
/**
* Retourne le rendu du champs File
*
* @return string Le rendu du champs
*/
public function renderFile(): string
{
$template = '<div class="custom-file">' . PHP_EOL;
$template .= ' <input%s />' . PHP_EOL;
$template .= ' <label class="custom-file-label" for="%s">%s</label>' . PHP_EOL;
$template .= ' <script type="text/javascript">' . PHP_EOL;
$template .= ' document.getElementById(\'%s\').addEventListener(\'change\', function(e) {' . PHP_EOL;
$template .= ' if (e.target.files[0]) {' . PHP_EOL;
$template .= ' var fileName = e.target.files[0].name;' . PHP_EOL;
$template .= ' var nextSibling = e.target.nextElementSibling;' . PHP_EOL;
$template .= ' nextSibling.innerText = fileName' . PHP_EOL;
$template .= ' }' . PHP_EOL;
$template .= ' });' . PHP_EOL;
$template .= ' </script>' . PHP_EOL;
$template .= '</div>';
$attrs_default = [
'id' => $this->makeID('id'),
'class' => $this->makeClasses('custom-file-input'),
'type' => 'file',
'name' => $this->makeName(),
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes),
$this->makeID('id'),
$this->getAttr('placeholder'),
$this->makeID('id'),
]);
return $render;
}
/**
* Retourne le rendu du champs PDF
*
* @return string Le rendu du champs
*/
public function renderPdf(): string
{
$template = '<div class="input-group img-group">' . PHP_EOL;
$template .= ' <div class="input-group-prepend">' . PHP_EOL;
//$template .= ' <div class="input-group-text">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
//$template .= ' </div>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' <div class="input-group-append">' . PHP_EOL;
$template .= ' <button class="btn btn-outline-secondary popupfinder" type="button" id="btn-%s" data-field="%s" data-mimes="%s">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </button>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= '</div>';
$render = vsprintf($template, [
$this->prefix ?? '<img class="image-thumb" src="'.asset('images/icons/pdf.svg').'">',
$this->renderInput(),
$this->makeID('id'),
'#' . $this->makeID('id'),
'application/pdf',
$this->suffix ?? '<i class="fa fa-file-pdf-o mr-2"></i>Choisir PDF'
]);
$this->prefix = null;
$this->suffix = null;
return $render;
}
/**
* Retourne le rendu du champs Image
*
* @return string Le rendu du champs
*/
public function renderImage(): string
{
$template = '<div class="input-group img-group">' . PHP_EOL;
$template .= ' <div class="input-group-prepend">' . PHP_EOL;
$template .= ' <img id="thumb-%s" class="image-thumb" src="%s" alt="thumb">' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' <div class="input-group-append">' . PHP_EOL;
$template .= ' <button class="btn btn-outline-secondary popupfinder" type="button" id="btn-%s" data-thumb="%s" data-field="%s" data-mimes="%s">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </button>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= '</div>';
$render = vsprintf($template, [
$this->makeID('id'),
empty($this->makeValue()) ? url('images/default.svg') : url($this->makeValue()),
$this->renderInput(),
$this->makeID('id'),
'#thumb-' . $this->makeID('id'),
'#' . $this->makeID('id'),
'image',
$this->suffix ?? '<i class="fa fa-picture-o mr-2"></i>Choisir image'
]);
$this->prefix = null;
$this->suffix = null;
return $render;
}
/**
* Retourne le rendu du champs Video
*
* @return string Le rendu du champs
*/
public function renderVideo(): string
{
$template = '<div class="input-group">' . PHP_EOL;
$template .= ' <div class="input-group-prepend">' . PHP_EOL;
$template .= ' <div class="input-group-text">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' <div class="input-group-append">' . PHP_EOL;
$template .= ' <button class="btn btn-outline-secondary popupfinder" type="button" id="btn-%s" data-field="%s" data-mimes="%s">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </button>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= '</div>';
$render = vsprintf($template, [
$this->prefix ?? '<i class="fa fa-video-camera"></i>',
$this->renderInput(),
$this->makeID('id'),
'#' . $this->makeID('id'),
'video',
$this->suffix ?? '<i class="fa fa-file-video-o mr-2"></i>Choisir vidéo'
]);
$this->prefix = null;
$this->suffix = null;
return $render;
}
/**
* Retourne le rendu d'un champs Folder
*
* @return string
*/
public function renderFolder(): string
{
$render = '';
$template = '<div class="input-group img-group">' . PHP_EOL;
$template .= ' <div class="input-group-prepend">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' <div class="input-group-append">' . PHP_EOL;
$template .= ' <button class="btn btn-outline-secondary popupfinder" type="button" id="btn-%s" data-field="%s" data-folder="true">' . PHP_EOL;
$template .= ' %s' . PHP_EOL;
$template .= ' </button>' . PHP_EOL;
$template .= ' </div>' . PHP_EOL;
$template .= '</div>';
$render = vsprintf($template, [
$this->prefix ?? '<img class="image-thumb" src="'.url('images/folder.jpg').'" alt="thumb">',
$this->renderInput(),
$this->makeID('id'),
'#' . $this->makeID('id'),
$this->suffix ?? '<i class="fa fa-picture-o mr-2"></i>Choisir image'
]);
$this->prefix = null;
$this->suffix = null;
return $render;
}
/**
* Retourne le rendu d'un champs Toogle
*
* @return string
*/
public function renderToggle(): string
{
$render = '';
$template = '<div class="toogle">' . PHP_EOL;
$template .= ' <input value="1"%s />' . PHP_EOL;
$template .= '</div>';
$attrs_default = [
'id' => $this->makeID(),
'class' => $this->makeClasses(),
'name' => $this->makeName(),
'type' => 'checkbox',
'checked' => $this->makeValue() == 1
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes)
]);
return $render;
}
/**
* Retourne le rendu d'un champs de type Radio
*
* @return string
*/
public function renderRadio(): string
{
$render = '';
$source = data_get($this->options, 'source');
$attrs_default = [
'class' => $this->makeClasses('form-check-input'),
'type' => 'radio',
'name' => $this->makeName(),
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = $this->renderOptions('checkbox');
return $render;
}
/**
* Retourne le rendu d'un champs de type Checkbox
*
* @return string
*/
public function renderCheckbox(): string
{
$render = '';
$source = data_get($this->options, 'source');
$this->is_multiple = count(data_get($this->options, 'source', [])) > 1;
$attrs_default = [
'class' => $this->makeClasses('form-check-input'),
'type' => 'checkbox',
'name' => $this->makeName(),
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = $this->renderOptions('checkbox');
return $render;
}
/**
* Retourne le rendu d'un champs de type range
*
* @return string
*/
public function renderRange(): string
{
$render = '';
$func_name = Str::camel($this->makeName()) . 'ShowValue';
$template = '<input%s oninput="%s(this.value)" />' . PHP_EOL;
$template .= '<span id="%s">%s&nbsp;</span>'. PHP_EOL;
$template .= '<script type="text/javascript">' . PHP_EOL;
$template .= ' function %s(newValue) {' . PHP_EOL;
$template .= ' document.getElementById("%s").innerHTML=newValue;' . PHP_EOL;
$template .= ' }' . PHP_EOL;
$template .= '</script>';
$attrs_default = [
'id' => $this->makeID(),
'class' => $this->makeClasses(),
'type' => 'range',
'name' => $this->makeName(),
'value' => $this->makeValue()
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes),
$func_name,
$this->makeID() . '-value',
$this->makeValue(),
$func_name,
$this->makeID() . '-value',
]);
return $render;
}
/**
* Retourne le rendu d'un champs de type Textarea
*
* @return string
*/
public function renderTextarea(): string
{
$render = '';
$template = '<textarea %s>%s</textarea>';
$attrs_default = [
'id' => $this->makeID(),
'class' => $this->makeClasses(),
'name' => $this->makeName(),
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes),
$this->makeValue()
]);
return $render;
}
/**
* Retourne le rendu d'un champs de type Select
*
* @return string
*/
public function renderSelect(): string
{
$render = '';
$template = '<select%s>' . PHP_EOL;
$template .= '%s';
$template .= '</select>';
$attrs_default = [
'id' => $this->makeID(),
'class' => $this->makeClasses(),
'name' => $this->makeName(),
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes),
str_indent($this->renderOptions('select'), 1)
]);
return $render;
}
/**
* Retourne le rendu d'un champs de type Input
*
* @return string
*/
public function renderInput(): string
{
$render = '';
$template = '<input%s />';
$attrs_default = [
'id' => $this->makeID(),
'class' => $this->makeClasses(),
'type' => 'text',
'name' => $this->makeName(),
'value' => $this->makeValue()
];
$this->attributes = $this->updateAttributes($attrs_default);
$render = vsprintf($template, [
$this->renderAttributes($this->attributes)
]);
return $render;
}
private function renderOptions($type): string
{
$render = '';
if (!array_key_exists('source', $this->options)) {
throw new \InvalidArgumentException("The `source` key is required in options");
}
switch ($type) {
case 'select':
$template = '<option value="[VALUE]"[ATTRIBUTES][SELECTED]>[LABEL]</option>' . PHP_EOL;
$sub_template = '<optgroup label="[LABEL]">' . PHP_EOL;
$sub_template .= '[VALUE]';
$sub_template .= '</optgroup>' . PHP_EOL;
$attr_selected = 'selected';
break;
case 'checkbox':
$template = '<div class="form-check[INLINE]">' . PHP_EOL;
$template .= ' <input id="[ID]-[VALUE]" name="[NAME]" value="[VALUE]"[ATTRIBUTES][SELECTED]/>' . PHP_EOL;
$template .= ' <label class="form-check-label" for="[ID]-[VALUE]">[LABEL]</label>' . PHP_EOL;
$template .= '</div>' . PHP_EOL;
$sub_template = '';
$attr_selected = 'checked';
$template = str_replace('[INLINE]', ($this->inline ? ' form-check-inline' : ''), $template);
$template = str_replace('[ID]', $this->makeID(), $template);
$template = str_replace('[NAME]', $this->makeName(), $template);
// Merge des attributs
$options_attrs = data_get($this->options, 'attributes', []);
$attributes = $this->updateAttributes($options_attrs);
data_set($this->options, 'attributes', $attributes);
case 'radio':
break;
default:
exit;
break;
}
// Detection options dynamiques
if(array_keys_exists(['value', 'label'], $this->options)) {
$render = $this->renderDynamicOptions($template, $sub_template, $attr_selected);
} else {
$render = $this->renderStaticOptions($template, $sub_template, $attr_selected);
}
return $render;
}
private function renderDynamicOptions($template, $sub_template, $attr_selected): string
{
$render = '';
$source = data_get($this->options, 'source');
$value = data_get($this->options, 'value');
$label = data_get($this->options, 'label');
$attrs = data_get($this->options, 'attributes', []);
// Ajout de la valeur par défaut
$render .= $this->renderDefaultOption();
$datas = $this->getSourceData($source);
// Ajout des options
foreach ($datas as $data) {
$dynamik = new Dynamik($data);
// Préparation de l'option
$option_value = (string) $dynamik->getValue($value);
$option_label = (string) $dynamik->getValue($label);
// Préparation des attributs
$attributes = [];
foreach($attrs as $name => $val) {
$attr_val = $dynamik->getValue($val);
$attributes[$name] = is_array($attr_val) ? array_filter($attr_val) : $attr_val;
}
$option_attrs = $this->renderAttributes(array_filter($attributes));
// Valeur sélectionnée ?
$selected = $this->isActiveOption($option_value) ? ' ' . $attr_selected : '';
$temp = str_replace('[VALUE]', $option_value, $template);
$temp = str_replace('[ATTRIBUTES]', $option_attrs, $temp);
$temp = str_replace('[SELECTED]', $selected, $temp);
$render .= str_replace('[LABEL]', $option_label, $temp);
}
return $render;
}
private function getSourceData(iterable $source): Iterable
{
$data = $source;
if (is_array($source) && array_key_exists('call', $source)) {
$call = data_get($source, 'call');
$params = data_get($source, 'params', []);
$target = data_get($source, 'target');
$request = new \Illuminate\Http\Request($params);
$response = App::call($call, ['request' => $request])->getData();
$data = data_get($response, $target);
}
return $data;
}
private function renderStaticOptions($template, $sub_template, $attr_selected): string
{
$render = '';
$datas = data_get($this->options, 'source');
$attributes = data_get($this->options, 'attributes', []);
// Ajout de la valeur par défaut
$render .= $this->renderDefaultOption();
if (is_array($datas)) {
foreach($datas as $value => $label) {
// Préparation des attributs
$attrs = $this->renderAttributes(array_filter($attributes));
// Cas d'un sous niveau
if (is_array($label)) {
$opt_group = '';
foreach ($label as $_value => $_label) {
if (is_iterable($_label) || (is_object($_label) && !method_exists($value, '__toString'))) {
throw new \InvalidArgumentException("Invalid value of options attribute : " . $_value);
}
$selected = $this->isActiveOption($_value) ? ' ' . $attr_selected : '';
$temp = str_replace('[VALUE]', $_value, $template);
$temp = str_replace('[ATTRIBUTES]', $attrs, $temp);
$temp = str_replace('[SELECTED]', $selected, $temp);
$opt_group .= str_replace('[LABEL]', $_label, $temp);
}
$temp = str_replace('[LABEL]', $value, $sub_template);
$render .= str_replace('[VALUE]', str_indent($opt_group, 1), $temp);
} else {
$selected = $this->isActiveOption($value) ? ' ' . $attr_selected : '';
$temp = str_replace('[VALUE]', $value, $template);
$temp = str_replace('[ATTRIBUTES]', $attrs, $temp);
$temp = str_replace('[SELECTED]', $selected, $temp);
$render .= str_replace('[LABEL]', $label, $temp);
}
}
}
return $render;
}
private function renderDefaultOption()
{
$render = '';
$template = '<option value="%s"%s%s>%s</option>' . PHP_EOL;
$default = data_get($this->options, 'default', []);
// Ajout de la valeur par défaut
if (!empty($default)) {
$render .= vsprintf($template, [
data_get($default, 'value'),
$this->getAttr('required') ? ' disabled hidden' : '',
$this->isActiveOption(data_get($default, 'value')) ? ' selected' : '',
data_get($default, 'label')
]);
}
return $render;
}
private function isActiveOption($data)
{
$value = $this->makeValue();
if (is_array($value)) {
return in_array($data, $value);
} else {
return strval($value) === strval($data);
}
}
/**
* Mise à jour des attributs défini par l'utilisateur avec les attributs par défaut du champs
*
* @param array $attrs
* @param array $default
* @return array Un tableau des attributes du champs
*/
private function updateAttributes($default = [])
{
$attrs = $this->attributes;
// Etape 1 - Nettoyage des attributs indésirables (name et value, n'ont rien à faire là)
unset($attrs['name'], $attrs['value']);
// Etape 2 - Fusion des attributs avec les valeurs par défaut
$attributes = array_merge($default, $attrs);
// Etape 3 - 2ème passe avec fusion des valeurs de type tableau
foreach ($attributes as $name => $value) {
if (is_array(data_get($default, $name)) || is_array(data_get($attrs, $name))) {
$attr_value = is_array(data_get($attrs, $name)) ? data_get($attrs, $name) : [data_get($attrs, $name)];
$default_value = is_array(data_get($default, $name)) ? data_get($default, $name) : [data_get($default, $name)];
$attributes[$name] = array_filter(array_merge($default_value, $attr_value));
}
}
// Etape 4 - Suppression des valeurs null
foreach($attributes as $name => $value) {
if (is_null($value) || $value === false || $value === '') {
unset($attributes[$name]);
}
}
return $attributes;
}
/**
* Constuction de l'ID du champs
*
* @return string
*/
private function makeID()
{
$id = '';
$id .= 'field-';
$id .= Str::slug($this->name);
if (!empty($this->indice) && !is_bool($this->indice)) {
$id .= '-' . $this->indice;
}
return $id;
}
/**
* Construction du nom de du champs
*
* @return string
*/
private function makeName(): string
{
$name = $this->name;
if ($this->indice === true) {
$name .= '[]';
} else if (!is_null($this->indice) && !is_bool($this->indice)) {
$name .= '[' . $this->indice . ']';
}
if ($this->getAttr('multiple')) {
$name .= '[]';
}
if ($this->is_multiple) {
$name .= '[]';
}
return $name;
}
/**
* Récupération de la valeur du champs
*
* @return Mixte
*/
private function makeValue()
{
$getter_name = $this->makeGetterName();
return old($getter_name, $this->value);
}
/**
* Construction des classes du champs
*
* @param string $default
* @return array
*/
private function makeClasses($default = 'form-control'): array
{
$classes = [];
$classes[] = $default;
$errors = session('errors', collect());
$getter_name = $this->makeGetterName();
if ($errors->has($getter_name)) {
$classes[] = 'is-invalid';
} else if (!is_null(old($getter_name))) {
$classes[] = 'is-valid';
}
// Si une taille est spécifiée && aucun préfixe ou suffixe
if (!empty($this->size) && empty($this->prefix) && empty($this->suffix)) {
$classes[] = 'form-control-' . $this->size;
}
$classes = array_filter($classes);
return $classes;
}
/**
* Retourne le nom de type getter pour un champs (utile pour old(), $errors, ...)
*
* @return string
*/
private function makeGetterName(): string
{
$name = $this->name;
if ($this->indice === true) {
$name .= '.*';
} else if (!is_null($this->indice)) {
$name .= '.' . $this->indice;
}
return $name;
}
/**
* Retourne le rendu d'un tableau d'attributs avec leur valeurs
*
* @param array $attrs
* @return string
*/
private function renderAttributes(array $attrs): string
{
$render = '';
foreach ($attrs as $name => $value) {
if ($value === true) {
$render .= vsprintf(' %s', [$name]);
} else if (is_array($value)) {
$render .= vsprintf(' %s="%s"', [$name, implode(' ', $value)]);
}else {
$render .= vsprintf(' %s="%s"', [$name, $value]);
}
}
return $render;
}
/**
* Retourne le rendu d'un message d'erreur de validation
*
* @return string Le rendu du message
*/
private function renderValidationMessage()
{
$errors = session('errors', collect());
$render = null;
if ($errors->has($this->makeGetterName())) {
$template = '<div class="invalid-feedback">%s</div>' . PHP_EOL;
$render = sprintf($template, $errors->first($this->makeGetterName()));
}
return $render;
}
/**
* Retourne le rendu de la partie aide d'un champs (sous le champs)
*
* @return string Le rendu de la partie aide
*/
private function renderHelp(): string
{
$render = '';
if (!empty($this->help)) {
$template = '<small class="form-text text-muted text-right mt-0">%s</small>' . PHP_EOL;
$render = sprintf($template, $this->help);
}
return $render;
}
/**
* Retoune le rendu de la partie prefixe du champs
*
* @return string Le rendu du prefixe
*/
private function renderPrefix(): string
{
$render = '';
if (!empty($this->prefix)) {
$template = '<div class="input-group-prepend">' . PHP_EOL;
if (preg_match('/<(button|img).*/', $this->prefix)) {
$template .= ' %s' . PHP_EOL;
} else {
$template .= ' <div class="input-group-text">%s</div>' . PHP_EOL;
}
$template .= '</div>' . PHP_EOL;
$render = sprintf($template, $this->prefix);
}
return $render;
}
/**
* Retourne le rendu de la partie suffixe du champs
*
* @return string Le rendu du suffixe
*/
private function renderSuffix(): string
{
$render = '';
if (!empty($this->suffix)) {
$template = '<div class="input-group-append">' . PHP_EOL;
if (preg_match('/<(button|img).*/', $this->suffix)) {
$template .= ' %s' . PHP_EOL;
} else {
$template .= ' <div class="input-group-text">%s</div>' . PHP_EOL;
}
$template .= '</div>' . PHP_EOL;
$render = sprintf($template, $this->suffix);
}
return $render;
}
/**
* Retourne le rendu de la popover (description du champs)
*
* @return string Le rendu de la popover
*/
private function renderPopover(): string
{
$render = '';
if (!empty($this->popover)) {
$template = '<span class="text-info popup-help" data-toggle="popover" data-placement="%s" title="%s" data-content="%s">' . PHP_EOL;
$template .= ' <i class="fa fa-info-circle"></i>' . PHP_EOL;
$template .= '</span>' . PHP_EOL;
$render = vsprintf($template, [
data_get($this->popover, 'placement', 'right'),
data_get($this->popover, 'title', 'Informations'),
data_get($this->popover, 'content'),
]);
}
return $render;
}
/**
* Retourne le rendu du label du champs
*
* @return string Le rendu du label
*/
private function renderLabel(): string
{
$render = '';
$class = '';
if ($this->getAttr('required')) {
$class .= 'required';
}
if ($this->inline && ($this->getAttr('type') != 'checkbox' || $this->getAttr('type') != 'radio')) {
$class .= ' col-sm-2';
}
if (!empty($this->label)) {
$template = '<label%s%s>%s</label>' . PHP_EOL;
$render = vsprintf($template, [
$this->getAttr('id') ? ' for="' . $this->getAttr('id') . '"' : '',
strlen($class) > 0 ? ' class="' . trim($class) . '"' : '',
$this->label
]);
}
return $render;
}
/**
* Retourne le rendu d'un champs avec son environnement
*
* @param string $renderField Le rendu du champs
* @return string Le rendu de l'environnement du champs
*/
public function renderWrapFieldEnvironment(string $renderField, $noindent = false): string
{
$render = $renderField;
$template = '';
$templateInline = '<div class="form-group row">' . PHP_EOL;
$templateInline .= '%s%s';
$templateInline .= ' <div class="col-sm-10">' . PHP_EOL;
$templateInline .= '%s';
$templateInline .= ' </div>' . PHP_EOL;
$templateInline .= '</div>' . PHP_EOL;
$templateClassic = '<div class="form-group">' . PHP_EOL;
$templateClassic .= '%s%s' ;
$templateClassic .= '%s';
$templateClassic .= '</div>' . PHP_EOL;
$templatePrefix = '<div class="input-group%s">'. PHP_EOL;
$templatePrefix .= '%s%s%s%s';
$templatePrefix .= '</div>'. PHP_EOL;
// Si prefixe ou suffixe
if (!empty($this->prefix) || !empty($this->suffix)) {
$class_size = empty($this->size) ? '' : ' input-group-' . $this->size;
$render = vsprintf($templatePrefix, [
$class_size,
str_indent($this->renderPrefix(), 1),
str_indent($render, $noindent ? 0 : 1),
str_indent($this->renderSuffix(), 1),
$this->renderValidationMessage()
]);
$render .= $this->renderHelp();
} else {
$render .= $this->renderHelp() . $this->renderValidationMessage();
}
// Affichage en mode inline
if ($this->inline && ($this->getAttr('type') != 'checkbox' || $this->getAttr('type') != 'radio')) {
$render = str_indent($render, $noindent ? 0 : 2);
$render = vsprintf($templateInline, [
str_indent($this->renderLabel(), 1),
str_indent($this->renderPopover(), 1),
$render,
]);
} else {
$render = str_indent($render, $noindent ? 0 : 1);
$render = vsprintf($templateClassic, [
str_indent($this->renderLabel(), 1),
str_indent($this->renderPopover(), 1),
$render,
]);
}
return $render;
}
/**
* Récupère la valeur d'un attribut dont le nom est passé en paramètre
*
* @param string $name Nom de l'attribut
* @return Mixte
*/
private function getAttr($name)
{
return data_get($this->attributes, $name);
}
private function setAttr($name, $value)
{
$this->attrbutes[$name] = $value;
}
/**
* Retourne le rendu d'une chaine de caractère en HTML
*
* @param String $string La chaines de caractères à traiter
* @return String Le rendu HTML
*/
public function renderHTML(string $string): HtmlString
{
return new HtmlString($string);
}
}
<?php
namespace Goldenscarab\Modulus\Service\Field;
use Illuminate\Support\Facades\Request;
use Goldenscarab\Modulus\Service\Field\Compose\Field2Bt3 as Field;
class FieldBt3
{
/**
* Retourne un champs code source
*
* @param array $attrs Attributs du champs
* @return string Le HTML du champs code source
*/
public function code($attrs): string
{
$field = new Field($attrs);
$code = $field->renderCodeEditor();
$render = $field->renderWrapFieldEnvironment($code, true);
return $field->renderHTML($render);
}
/**
* Retourne un champs select de choix de publication
* @param boolean $status La valeur du champs
* @return string Le HTML du champs de publication
*/
public function publish($status): string
{
$render = $this->select([
'label' => 'Visibilité',
'name' => 'status',
'prefix' => '<i class="fa fa-globe"></i>',
'value' => $status,
'attributes' => ['required' => true],
'options' => ['source' => [0 => 'Brouillon', 1 => 'En ligne']]
]);
return $render;
}
/**
* Retourne un champs de recherche dans une liste
* @return string Le HTML du champs de recherche
*/
public function search(): string
{
$render = $this->input([
'name' => 'search',
'value' => Request::get('search'),
'attributes' => [
'placeholder' => 'Recherche...',
'class' => 'form-control-sm',
],
'suffix' => '<button class="btn btn-outline-secondary btn-sm" type="submit" id="button-search"><i class="fa fa-search"></i></button>',
]);
return $render;
}
/**
* Retourne un champs select pour choisir le nombre d'éléments d'une liste à afficher
* @return string Le HTML du menu déroulant
*/
public function perpage(): string
{
$render = $this->select([
'label' => 'Lignes par page',
'value' => Request::fullUrlWithQuery(["perpage" => Request::get('perpage')]),
'attributes' => [
'class' => 'ml-2',
'placeholder' => 'Choisir...',
'onchange' => 'return document.location.href = this.value;'
],
'options' => [
'source' => [
Request::fullUrlWithQuery(["perpage" => 10]) => '10',
Request::fullUrlWithQuery(["perpage" => 15]) => '15',
Request::fullUrlWithQuery(["perpage" => 50]) => '50',
Request::fullUrlWithQuery(["perpage" => 100]) => '100',
Request::fullUrlWithQuery(["perpage" => 200]) => '200',
Request::fullUrlWithQuery(["perpage" => 500]) => '500',
Request::fullUrlWithQuery(["perpage" => 1000]) => '1000'
]
]
]);
return $render;
}
/**
* Retourne un champs file avec son environnement
* @param array $attrs Les attributs du champs
* @return string le HTML du champs et de son environnement
*/
public function file($attrs = []): string
{
$field = new Field($attrs);
$file = $field->renderFile();
$render = $field->renderWrapFieldEnvironment($file);
return $field->renderHTML($render);
}
/**
* Retourne un champs pdf avec son environnement
* @param array $attrs Les attributs du champs
* @return string le HTML du champs et de son environnement
*/
public function pdf($attrs = []): string
{
$field = new Field($attrs);
$image = $field->renderPdf();
$render = $field->renderWrapFieldEnvironment($image);
return $field->renderHTML($render);
}
/**
* Retourne un champs video avec son environnement
* @param array $attrs Les attributs du champs
* @return string le HTML du champs et de son environnement
*/
public function video($attrs = []): string
{
$field = new Field($attrs);
$image = $field->renderVideo();
$render = $field->renderWrapFieldEnvironment($image);
return $field->renderHTML($render);
}
/**
* Retourne un champs image avec son environnement
* @param array $attrs Les attributs du champs
* @return string le HTML du champs et de son environnement
*/
public function image($attrs = []): string
{
$field = new Field($attrs);
$image = $field->renderImage();
$render = $field->renderWrapFieldEnvironment($image);
return $field->renderHTML($render);
}
/**
* Retourne un champs dossier avec son environnement
* @param array $attrs Les attributs du champs
* @return string le HTML du champs et de son environnement
*/
public function folder($attrs = []): string
{
$field = new Field($attrs);
$folder = $field->renderFolder();
$render = $field->renderWrapFieldEnvironment($folder);
return $field->renderHTML($render);
}
/**
* Retourne un champs toggle avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function toggle($attrs = []): string
{
$field = new Field($attrs);
$toggle = $field->renderToggle();
$render = $field->renderWrapFieldEnvironment($toggle);
return $field->renderHTML($render);
}
/**
* Retourne un champs radio avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function radio($attrs = []): string
{
$field = new Field($attrs);
$radio = $field->renderRadio();
$render = $field->renderWrapFieldEnvironment($radio);
return $field->renderHTML($render);
}
/**
* Retourne un champs checkbox avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function checkbox($attrs = []): string
{
$field = new Field($attrs);
$checkbox = $field->renderCheckbox();
$render = $field->renderWrapFieldEnvironment($checkbox);
return $field->renderHTML($render);
}
/**
* Retourne un champs range avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function range($attrs = []): string
{
$field = new Field($attrs);
$range = $field->renderRange();
$render = $field->renderWrapFieldEnvironment($range);
return $field->renderHTML($render);
}
/**
* Retourne un champs pot de miel avec son environnement
* @return string Le HTML du champs et son environnement
*/
public function honeypot(): string
{
$field = new Field([
'name' => 'my-name',
'attributes' => ['class' => 'd-none']
]);
$input1 = $field->renderInput();
$field = new Field([
'name' => 'my-time',
'value' => encrypt(time()),
'attributes' => ['class' => 'd-none']
]);
$input2 = $field->renderInput();
return $field->renderHTML($input1 . $input2);
}
/**
* Retourne un champs textarea avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function textarea($attrs = []): string
{
$field = new Field($attrs);
$textarea = $field->renderTextarea();
$render = $field->renderWrapFieldEnvironment($textarea, true);
return $field->renderHTML($render);
}
/**
* Retourne un champs select avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function select($attrs = []): string
{
$field = new Field($attrs);
$select = $field->renderSelect();
$render = $field->renderWrapFieldEnvironment($select);
return $field->renderHTML($render);
}
/**
* Retourne un champs input avec son environnement
* @param array $attrs Les attributs du champs
* @return string Le HTML du champs et de son environnement
*/
public function input($attrs = []): string
{
$field = new Field($attrs);
$input = $field->renderInput();
$render = $field->renderWrapFieldEnvironment($input);
return $field->renderHTML($render);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment