311 lines
6.5 KiB
PHP
311 lines
6.5 KiB
PHP
<?php
|
|
/**
|
|
* Fonctions utiles au plugin GamuForm
|
|
*
|
|
* @plugin GamuForm
|
|
* @copyright 2020
|
|
* @author tofulm
|
|
* @licence GNU/GPL
|
|
* @package SPIP\Gamuform\Fonctions
|
|
*/
|
|
|
|
if (!defined('_ECRIRE_INC_VERSION')) {
|
|
return;
|
|
}
|
|
|
|
|
|
function gamuform_lang(?string $chaine):string {
|
|
if (strpos($chaine, ':>') !== false) {
|
|
$chaine = substr($chaine, 2, -2);
|
|
$chaine = _T($chaine);
|
|
}
|
|
return $chaine ?? '';
|
|
}
|
|
|
|
/**
|
|
* filtre generale qui appelle un filtre spécifique, déclaré dans l'array saisies avec la cle : gamutable/filtre
|
|
* si cette fonction commence par _array_ ex: _array_maSuperFonction => elle aura en parametre l'array de la ligne
|
|
* sinon la valeur
|
|
* cf gamuform/exemple.php
|
|
*
|
|
* @param String $objet
|
|
* @param String $champ
|
|
* @param String $valeur
|
|
*
|
|
* @return String valeur modifiée ou pas par le filtre
|
|
*/
|
|
function gamufiltre($objet, $champ, $ligne) {
|
|
$valeur = $ligne[$champ] ?? '';
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
if (
|
|
(!empty($s['gamutable']) or !empty($s['options']))
|
|
and $champ_tab = $s['gamutable']['champ'] ?? $s['options']['nom']
|
|
and $champ_tab === $champ
|
|
and !empty($s['gamutable']['fonction'])
|
|
) {
|
|
$filtre = $s['gamutable']['fonction'];
|
|
if (strpos($filtre, '_array_') !== false) {
|
|
$f = substr($filtre, 7);
|
|
if (function_exists($f)) {
|
|
return $f($ligne) ?? '';
|
|
} else if(function_exists($filtre)) {
|
|
return $filtre($ligne) ?? '';
|
|
} else {
|
|
return '';
|
|
}
|
|
} else {
|
|
return $filtre($valeur) ?? '';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $valeur;
|
|
}
|
|
|
|
/**
|
|
* recuperation du tableau des saisies d'un objet
|
|
* dans : gamuform/mon_objet.php
|
|
*
|
|
* @param string $objet
|
|
*
|
|
* @return array
|
|
*
|
|
*/
|
|
function gamuform_recup_tableau_objet(string $objet, bool $all = true):array {
|
|
$f = charger_fonction($objet, 'gamuform', true);
|
|
if (!$f) {
|
|
return [];
|
|
}
|
|
$T = $f();
|
|
if (empty($T)) {
|
|
$T = [];
|
|
}
|
|
// on filtre les entrées vides, qui peuvent arriver si on on des autorisations
|
|
$T = array_filter($T, function($d){
|
|
return !empty($d);
|
|
});
|
|
$options = [];
|
|
if (!empty($T['options'])) {
|
|
$options = $T['options'];
|
|
unset($T['options']);
|
|
}
|
|
|
|
if (!$all) {
|
|
$T = array_filter($T, function($d){
|
|
return !empty($d['saisie']);
|
|
});
|
|
$T = array_merge(['options' => $options], $T);
|
|
}
|
|
return $T;
|
|
}
|
|
|
|
function gamuform_objet_autoriser(string $objet):bool {
|
|
if (
|
|
!empty($GLOBALS['gamuform_objets'])
|
|
and !empty($objet)
|
|
and in_array($objet, array_keys($GLOBALS['gamuform_objets']))
|
|
) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function gamuform_nom_objet($objet, $quoi = 'pluriel'):string {
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
if (is_array($GLOBALS['gamuform_objets'][$objet])) {
|
|
return $GLOBALS['gamuform_objets'][$objet][$quoi];
|
|
} else {
|
|
|
|
if ($quoi === 'creer') {
|
|
return 'Nouveau';
|
|
} else {
|
|
return $GLOBALS['gamuform_objets'][$objet];
|
|
}
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
/**
|
|
* recuperation des champs de l'objet pour les saisies
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_champs_gamutable(string $objet):array {
|
|
$c = [];
|
|
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
$c[] = $s['gamutable']['champ'] ?? $s['options']['nom'];
|
|
}
|
|
}
|
|
}
|
|
return $c;
|
|
}
|
|
|
|
/**
|
|
* recuperation des champs de l'objet pour les saisies
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_champs_saisies(string $objet):array {
|
|
$c = [];
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet, false);
|
|
if (array_key_exists('options', $T)) {
|
|
unset($T['options']);
|
|
}
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
$c[] = $s['options']['nom'];
|
|
}
|
|
}
|
|
}
|
|
return $c;
|
|
}
|
|
|
|
/**
|
|
* recuperation du header de l'objet que l'on veut afficher dans gamutable
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_header(string $objet):array {
|
|
$c = [];
|
|
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
$champ = $s['gamutable']['champ'] ?? $s['options']['nom'];
|
|
$c[$champ] = $s['gamutable']['header'] ?? $s['options']['nom'];
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return $c;
|
|
}
|
|
/**
|
|
* recuperation des filtreCol de l'objet que l'on veut afficher dans gamutable
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_crayons(string $objet):array {
|
|
$c = [];
|
|
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
if (
|
|
!empty($s['gamutable']['crayons'])
|
|
and $champ = $s['gamutable']['champ'] ?? $s['options']['nom']
|
|
) {
|
|
$c[$champ] = $s['gamutable']['crayons'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $c;
|
|
}
|
|
/**
|
|
* recuperation des filtreCol de l'objet que l'on veut afficher dans gamutable
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_filtreCol(string $objet):array {
|
|
$c = [];
|
|
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
if (
|
|
!empty($s['gamutable']['filtreCol'])
|
|
and in_array($s['gamutable']['filtreCol'], ['input', 'select'])
|
|
and $champ = $s['gamutable']['champ'] ?? $s['options']['nom']
|
|
) {
|
|
$c[$champ] = $s['gamutable']['filtreCol'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $c;
|
|
}
|
|
|
|
/**
|
|
* recuperation des classes de l'objet que l'on veut afficher dans gamutable
|
|
*
|
|
* @param String $objet
|
|
*
|
|
* @return Array
|
|
*/
|
|
function gamuform_recup_classes(string $objet):array {
|
|
$c = [];
|
|
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$T = gamuform_recup_tableau_objet($objet);
|
|
|
|
if (!empty($T)) {
|
|
foreach ($T as $s) {
|
|
$champ = $s['gamutable']['champ'] ?? $s['options']['nom'];
|
|
$c[$champ] = $s['gamutable']['classes'] ?? '';
|
|
}
|
|
}
|
|
}
|
|
return $c;
|
|
}
|
|
|
|
/**
|
|
* recuperation de(s) ligne(s) de l'objet
|
|
*
|
|
* @param String $objet
|
|
* @param String $id_objet (optional)
|
|
*
|
|
* @return array
|
|
*/
|
|
function gamuform_recup_valeurs($objet, $id_objet = 0):array {
|
|
$r = [];
|
|
if (gamuform_objet_autoriser($objet)) {
|
|
$table = table_objet_sql($objet);
|
|
$id = id_table_objet($objet);
|
|
// $s = gamuform_recup_champs_gamutable($objet);
|
|
|
|
// $s[] = $id;
|
|
if (intval($id_objet)) {
|
|
$valeurs = sql_fetsel('*', $table, "$id=".intval($id_objet));
|
|
$valeurs['id'] = $valeurs[$id];
|
|
if (!empty($valeurs)) {
|
|
$r[0] = $valeurs;
|
|
}
|
|
} else {
|
|
$T = sql_allfetsel('*', $table, 1);
|
|
if (!empty($T)) {
|
|
foreach ($T as $t) {
|
|
$t['id'] = $t[$id];
|
|
$r[] = $t;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $r;
|
|
}
|