You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
saisies/inc/saisies_name.php

104 lines
2.2 KiB
PHP

<?php
/**
* Gestion de name
* Partie commun js/php
*
* @package SPIP\Saisies\Name
**/
/**
* Passer un nom en une valeur compatible avec une classe css
*
* - toto => toto,
* - toto/truc => toto_truc,
* - toto[truc] => toto_truc
*
* @param string $nom
* @return string
**/
function saisie_nom2classe($nom) {
return str_replace(array('/', '[', ']', '&#91;', '&#93;'), array('_', '_', '', '_', ''), $nom);
}
/**
* Ajouter une ou des classes sur la saisie en fonction du type
* @param $type_saisie
* @return string
*/
function saisie_type2classe($type_saisie) {
static $compteur = 0;
$class = "saisie_{$type_saisie}";
if (strpos($type_saisie, 'selecteur') === 0) {
$class .= " selecteur_item";
}
if (!in_array($type_saisie, array('hidden','fieldset'))) {
$class .= ($compteur & 1) ? " editer_even" : " editer_odd";
$compteur = 1 - $compteur;
}
$class = trim($class);
return $class;
}
/**
* Passer un nom en une valeur compatible avec un `name` de formulaire
*
* - toto => toto,
* - toto/truc => toto[truc],
* - toto/truc/ => toto[truc][],
* - toto[truc] => toto[truc]
*
* @see saisie_name2nom() pour l'inverse.
* @param string $nom
* @return string
**/
function saisie_nom2name($nom) {
if (false === strpos($nom, '/')) {
return $nom;
}
$nom = explode('/', $nom);
$premier = array_shift($nom);
$nom = implode('][', $nom);
return $premier . '[' . $nom . ']';
}
/**
* Passer un `name` en un format de nom compris de saisies
*
* - toto => toto,
* - toto[truc] => toto/truc,
* - toto[truc][] => toto/truc/
* - toto/truc => toto/truc
*
* @see saisie_nom2name() pour l'inverse.
* @param string $name
* @return string
**/
function saisie_name2nom($name) {
if (false === strpos($name, '[')) {
return $name;
}
$name = explode('[', str_replace(']', '', $name));
return implode('/', $name);
}
/** Appliquer `saisie_nom2name()` sur les clés d'un tableau
* utilisé pour gérer les erreurs
* @param array $tab
* @return array
**/
function saisies_cles_nom2name($tab) {
if (!is_array($tab)) {
return $tab;
}
foreach ($tab as $k => $v) {
$kbis = saisie_nom2name($k);
if ($kbis !== $k) {
unset ($tab[$k]);
$tab[$kbis] = $v;
}
}
return $tab;
}