Commit bb85e931 authored by Sylvain's avatar Sylvain

Ajout de Action Menu

parent 17118e68
<?php
namespace Goldenscarab\Modulus\Helpers;
use Illuminate\Support\Facades\Auth;
/**
* Constuction de tableau de paramétrage pour constuction dynamique de boutons
*
* $can_prefix = 'test';
* $url_prefix = url('home');
*
*
* $am = new ActionMenu($can_prefix, $url_prefix);
* $am->make('[UPDATE]');
* $am->make('[DUPLICATE]');
* $am->make('[DELETE]');
* $am->make('[EXPORT]');
* $am->make('[RESET-POSITION]');
* $am->make('[SEPARATOR]');
* $am->make('[TRUNCATE]');
* $am->make('[READ]', ['attributes.class' => 'btn-sm btn-success text-nowrap']);
*/
class ActionMenu
{
private $can_prefix;
private $url_prefix;
public function __construct($can_prefix, $url_prefix)
{
$this->can_prefix = $can_prefix;
$this->url_prefix = $url_prefix;
}
private function can($action)
{
return (Auth::user())->can($this->can_prefix . '-' . $action);
}
public function make($alias, $attrs = [])
{
$default_actions = array(
'[READ]' => [
'label' => '<i class="fa fa-eye mr-2"></i>' . __('Consulter'),
'href' => ['condition' => [
'value' => $this->can('read'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/read/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[SHOW]' => [
'label' => '<i class="fa fa-eye mr-2"></i>' . __('Consulter'),
'href' => ['condition' => [
'value' => $this->can('read'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/read/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[EDIT]' => [
'label' => '<i class="fa fa-pencil-square-o mr-2"></i>' . __('Modifier'),
'href' => ['condition' => [
'value' => $this->can('update'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/edit/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[UPDATE]' => [
'label' => '<i class="fa fa-pencil-square-o mr-2"></i>' . __('Modifier'),
'href' => ['condition' => [
'value' => $this->can('update'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/update/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[DUPLICATE]' => [
'label' => '<i class="fa fa-clone mr-2"></i>' . __('Dupliquer'),
'href' => ['condition' => [
'value' => $this->can('create'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/duplicate/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[DESTROY]' => [
'label' => '<i class="fa fa-trash mr-2"></i>' . __('Supprimer'),
'href' => '#',
'attributes' => [
'data-href' => ['condition' => [
'value' => $this->can('delete'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/destroy/%s',
'args' => [$this->url_prefix, ':id']
]],
]],
'class' => $this->can('read') ? 'text-danger delete' : ' disabled',
]
],
'[CREATE]' => [
'label' => '<i class="fa fa-plus mr-2"></i>' . __('Ajouter'),
'href' => ['condition' => [
'value' => $this->can('create'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/create',
'args' => [$this->url_prefix]
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[ADD]' => [
'label' => '<i class="fa fa-plus mr-2"></i>' . __('Ajouter'),
'href' => ['condition' => [
'value' => $this->can('create'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/create',
'args' => [$this->url_prefix]
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[EXPORT]' => [
'label' => '<i class="fa fa-file-excel-o mr-2"></i>' . __('Exporter'),
'href' => ['condition' => [
'value' => $this->can('read'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/export',
'args' => [$this->url_prefix]
]],
]],
'attributes' => [
'class' => $this->can('read') ? '' : ' disabled',
]
],
'[TRUNCATE]' => [
'label' => '<i class="fa fa-times mr-2"></i>' . __('Tout supprimer'),
'href' => '#',
'attributes' => [
'data-href' => ['condition' => [
'value' => $this->can('delete'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/truncate',
'args' => [$this->url_prefix]
]],
]],
'class' => $this->can('read') ? 'text-danger confirm' : ' disabled',
]
],
'[RESET-POSITION]' => [
'label' => '<i class="fa fa-list-ol mr-2"></i>' . __('Reset position'),
'href' => '#',
'attributes' => [
'data-href' => ['condition' => [
'value' => $this->can('update'),
'false' => '#',
'true' => ['template' => [
'format' => '%s/reset-position',
'args' => [$this->url_prefix]
]],
]],
'class' => $this->can('read') ? 'confirm' : ' disabled',
]
],
'[SEPARATOR]' => '<div class="dropdown-divider"></div>'
);
if (array_key_exists($alias, $default_actions)) {
$item = $default_actions[$alias];
// Mise à jour de l'élément avant son retour
foreach ($attrs as $key => $value) {
$item = data_set($item, $key, $value);
}
return $item;
} else {
throw new \InvalidArgumentException("Unknow alias name : " . $alias);
}
}
}
......@@ -46,3 +46,28 @@ if ( !function_exists('array_contains')) {
return false;
}
}
if ( !function_exists('array_to_string')) {
/**
* Converti un tableau PHP en texte php
*
* @param array Le tableau à convertir
* @return bool
*/
function array_to_string(array $array): string
{
echo "sdfjkh";
$string = '[';
foreach($array as $key => $item) {
if (is_array($item)) {
echo "array";
$string .= array_to_string($item);
} else {
echo "string";
$string .= '"' . $key . '" => "' . $item .'"';
}
}
$string .= ']';
return $string;
}
}
......@@ -180,17 +180,40 @@ if ( !function_exists('array_to_string')) {
* @param array $array Le tableau à convertir
* @return string La chaine de caractères résultante
*/
function array_to_string(array $array)
function array_to_string(array $array): string
{
$template = '[%s]';
$string = '';
$items = '';
// Si tableau indexé simple
if (is_indexed_array($array)) {
if (count($array)) {
$items = "'" . implode("', '", $array) . "'";
$string = '"' . implode('", "', $array) . '"';
} else {
$values = [];
// Traitement d'un tableau associatif
foreach($array as $key => $item) {
$value = '';
if (is_array($item)) {
$value = array_to_string($item);
} else if (is_string($item)) {
$value = '"' . $item . '"';
} else if (is_bool($item)) {
$value = $item ? 'true' : 'false';
} else {
$value = $item;
}
$values[] = vsprintf('"%s" => %s', [$key, $value]);
}
$string = implode(', ', $values);
}
return sprintf($template, $items);
return sprintf($template, $string);
}
}
......
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