From 9691b18c1a00689ba9e11826615092cc5f6ab17d Mon Sep 17 00:00:00 2001 From: nicod_ <nicod@lerebooteux.fr> Date: Wed, 15 Jan 2025 22:09:05 +0100 Subject: [PATCH] refactor: Splitter les fonctions en deux fichiers distincts. * les balises, dans blocks_fonctions.php * les utilitaires, dans inc/blocks.php --- blocks_fonctions.php | 331 --------------------------- formulaires/configurer_blocks.php | 2 +- formulaires/editer_block.php | 2 +- formulaires/exporter_blocktypes.php | 2 + inc/blocks.php | 343 ++++++++++++++++++++++++++++ 5 files changed, 347 insertions(+), 333 deletions(-) create mode 100644 inc/blocks.php diff --git a/blocks_fonctions.php b/blocks_fonctions.php index 27a6213..406f8c7 100644 --- a/blocks_fonctions.php +++ b/blocks_fonctions.php @@ -9,28 +9,6 @@ * @package SPIP\Blocks\Fonctions */ -/** - * Sérialisation de données (saisies ou valeurs) - * - * @param $data - * @return false|string - */ -function blocks_serialize($data) { - return ($data && is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK) : ''; -} - -/** - * Désérialisation de données (saisies ou valeurs) - * - * @param $data - * @return array|null - */ -function blocks_deserialize($data): ?array { - $retour = json_decode($data ?? '', true); - - return is_array($retour) ? $retour : null; -} - /** * Compile la balise `#GENERER_BLOCK` qui génère l'affiche d'un block' * @@ -211,312 +189,3 @@ function generer_TITRE_BLOCK($id_block): string { return $titre; } - -/** - * Chercher tous les types de blocs existant - * On cherche des fichiers blocks/*.yaml dans le PATH - * - * @param bool $exclure_ignores - * @return array - */ -function blocktypes_lister_types(bool $exclure_ignores = true): array { - static $cache; - $hash = ($exclure_ignores ? 1 : 0); - if (isset($cache[$hash])) { - return $cache[$hash] ?: []; - } - - $liste_blocktypes = []; - $match = '[^-]*[.]yaml$'; - $liste = find_all_in_path('blocks/', $match); - - if ($liste) { - include_spip('inc/yaml'); - foreach ($liste as $blocktype => $chemin) { - $blocktype = str_replace('.yaml', '', $blocktype);// - $yaml_data = yaml_charger_inclusions(yaml_decode_file($chemin)); - if (is_array($yaml_data)) { - $yaml_data['titre'] = _T_ou_typo($yaml_data['titre']); - $yaml_data['identifiant'] = $blocktype; - $yaml_data['description'] = _T_ou_typo($yaml_data['description']); - $yaml_data['icone'] = isset($yaml_data['icone']) ? blocktype_find_icone_path($yaml_data['icone']) : ''; - - $liste_blocktypes[$blocktype] = $yaml_data; - } - } - } - - // Puis les trier par ordre alphabétique - uasort($liste_blocktypes, function ($valeur1, $valeur2) { - if ($valeur1['nom'] > $valeur2['nom']) { - return 1; - } else if ($valeur1['nom'] == $valeur2['nom']) { - return 0; - } else { - return -1; - } - }); - - // Retirer les modèles désactivés dans la config - if ($exclure_ignores === true) { - include_spip('inc/config'); - $config_ignorer = lire_config('blocks/ignorer_blocktypes') ?? []; - foreach ($config_ignorer as $ignorer) { - unset($liste_blocktypes[$ignorer]); - } - } - $cache[$hash] = $liste_blocktypes; - - return $liste_blocktypes; -} - -/** - * Retrouver l'icone d'un blocktype, la réduire à 16 px - * - * @param string $blocktype - * @return string chemin - **/ -function blocktype_find_icone_path(string $blocktype): string { - static $cache; - if (isset($cache[$blocktype])) { - return $cache[$blocktype]; - } - if (!$chemin = find_in_path("blocks/$blocktype")) { - if (!$chemin = find_in_path("icones/$blocktype")) { - if (!$chemin = find_in_theme("images/$blocktype")) { - if (!$chemin = find_in_path($blocktype)) { - $chemin = ''; - } - } - } - } - include_spip('inc/filtres'); - include_spip('inc/filtres_images_mini'); - //Au cas où find_in_theme nous return un truc genre gis-xx.svg?16px - $chemin = preg_replace('#\?.*$#', '', $chemin); - //On force la reduction à 16px - //$chemin = extraire_attribut(image_reduire($chemin, 16), 'src'); - $cache[$blocktype] = $chemin; - - return $chemin; -} - -/** - * Obtenir les saisies d'un block - * - * @param int $id_block - * @return array - */ -function block_get_saisies(int $id_block): array { - static $cache; - if (isset($cache[$id_block])) { - return $cache[$id_block]; - } - $saisies = []; - if ($blocktype = sql_getfetsel('blocktype', 'spip_blocks', 'id_block = ' . $id_block)) { - $saisies = blocktype_get_saisies($blocktype); - } else { - spip_log("get_block_saisies($id_block) : pas de blocktype", 'blocks.' . _LOG_DEBUG); - } - $cache[$id_block] = $saisies; - - return $saisies; -} - -/** - * Obtenir toute la config d'un block - * - * @param string $blocktype - * @return array - */ -function blocktype_get_config(string $blocktype): array { - $blocktypes = blocktypes_lister_types(); - - return $blocktypes[$blocktype] ?? []; -} - -/** - * Obtenir les saisies d'un block - * - * @param string $blocktype - * @return array - */ -function blocktype_get_saisies(string $blocktype): array { - $blocktypes = blocktypes_lister_types(); - - return $blocktypes[$blocktype]['saisies'] ?? []; -} - -/** - * Obtenir le titre d'un block - * - * @param string $blocktype - * @return mixed|string - */ -function blocktype_get_titre(string $blocktype) { - $blocktypes = blocktypes_lister_types(); - - return $blocktypes[$blocktype]['titre'] ?? $blocktype; -} - -/** - * Obtenir l'icone d'un block - * - * @param string $blocktype - * @return string - */ -function blocktype_get_icone(string $blocktype): string { - $blocktypes = blocktypes_lister_types(); - - return $blocktypes[$blocktype]['icone'] ?? ''; -} - -/** - * Retourner un tableau des valeurs saisies - * - * @param string $saisies (sérialisées) - * @param string $valeurs (sérialisées) - * - * @return array - */ -function block_get_valeurs(array $saisies, array $valeurs): array { - $retour = []; - include_spip('inc/saisies_lister'); - $saisies_par_nom = saisies_lister_par_nom($saisies); - foreach ($saisies_par_nom as $nom => $saisie) { - $retour[$nom] = $valeurs[$nom] ?? null; - } - - return $retour; -} - -/** - * Chercher le squelette correspondant à un block par son identifiant - * - * @param string $blocktype Identifiant du type de block - * @param bool $force_public Forcer une recherche du squelette public - * @param bool $chemin_complet Retourne le chemin complet du squelette - * @param bool $dist Retourne le squelette par défaut (dist)) - * @return string Chemin du squelette trouvé - */ - -function blocks_trouver_squelette( - string $blocktype, - bool $force_public = false, - bool $chemin_complet = false, - bool $dist = true -): string { - static $cache; - $cle = $blocktype . (int)$force_public . (int)$chemin_complet . (int)$dist; - - if (isset($cache[$cle])) { - return $cache[$cle]; - } - - $squelette = ''; - if ( - !$force_public - && test_espace_prive() - && ($f = find_in_path('blocks/' . $blocktype . '_prive.' . _EXTENSION_SQUELETTES)) - && lire_fichier($f, $contenu) - ) { - $squelette = $chemin_complet ? $f : 'blocks/' . $blocktype . '_prive'; - } else if ( - ($f = find_in_path('blocks/' . $blocktype . '.' . _EXTENSION_SQUELETTES)) - && lire_fichier($f, $contenu) - ) { - $squelette = $chemin_complet ? $f : 'blocks/' . $blocktype; - } else if ( - $dist - && ($f = find_in_path('blocks/dist.' . _EXTENSION_SQUELETTES)) - && lire_fichier($f, $contenu) - ) { - $squelette = $chemin_complet ? $f : 'blocks/dist'; - } - $cache[$cle] = $squelette; - - return $squelette; -} - -/** - * Wrapper sans typage de la fonction blocks_trouver_squelette pour les squelettes SPIP - * Les paramètres peuvent être vides ou des chaines et pas des bool - * - * @param $blocktype - * @param $force_public - * @param $chemin_complet - * @param $dist - * @return string - */ -function filtre_blocktype_squelette( - $blocktype, - $force_public = false, - $chemin_complet = false, - $dist = true -) { - return blocks_trouver_squelette( - (string)$blocktype, - (bool)$force_public, - (bool)$chemin_complet, - (bool)$dist - ); -} - -/** - * Calculer la liste des types de blocks pouvant être créés sous un type de block - * @param $blocktype - * @return array - */ -function bloctypes_trouver_enfants($blocktype): array { - return bloctypes_trouver_role($blocktype, 'enfant'); -} - -/** - * Calculer la liste des types de blocks dans lesquels un type de block peut être créé - * @param $blocktype - * @return array - */ -function bloctypes_trouver_parents($blocktype): array { - return bloctypes_trouver_role($blocktype, 'parent'); -} - -/** - * Calculer une liste de types de blocks enfants ou parents en fonction du rôle - * @param $id_blocktype - * @param $role - * @return array - */ -function bloctypes_trouver_role($blocktype, $role): array { - // TODO : à réécrire !!! - - // include_spip('action/editer_liens'); - // if ( - // $id_blocktype - // && $role - // && ($blocktypes_parents = objet_trouver_liens(['blocktype' => $id_blocktype], ['blocktype' => '*'], ['role' => $role])) - // ) { - // return array_column($blocktypes_parents, 'id_objet'); - // } - - return []; -} - -/** - * Génére la liste des objets (du type `article`) utilisable dans une saisie - * - * @return array - */ -function blocks_objets_config() { - include_spip('base/objets'); - $data = []; - if ($objets = array_filter(lire_config('blocks/objets'))) { - foreach ($objets as $objet) { - $objet_type = objet_type($objet); - $infos = objet_info($objet_type, 'texte_objet'); - $data[$objet_type] = _T($infos); - } - asort($data); - } - - return $data; -} diff --git a/formulaires/configurer_blocks.php b/formulaires/configurer_blocks.php index d7fdcb0..c9906b5 100644 --- a/formulaires/configurer_blocks.php +++ b/formulaires/configurer_blocks.php @@ -6,7 +6,7 @@ if (!defined('_ECRIRE_INC_VERSION')) { function formulaires_configurer_blocks_saisies() { - include_spip('blocks_fonctions'); + include_spip('inc/blocks'); $blocktypes_dispo = blocktypes_lister_types(false); foreach ($blocktypes_dispo as $key => $type) { $blocktypes[$key] = $type['titre']; diff --git a/formulaires/editer_block.php b/formulaires/editer_block.php index 7b087b4..29b9c8e 100644 --- a/formulaires/editer_block.php +++ b/formulaires/editer_block.php @@ -13,7 +13,7 @@ if (!defined('_ECRIRE_INC_VERSION')) { return; } -include_spip('blocks_fonctions'); +include_spip('inc/blocks'); include_spip('inc/actions'); include_spip('inc/editer'); diff --git a/formulaires/exporter_blocktypes.php b/formulaires/exporter_blocktypes.php index 20fd029..da5284e 100644 --- a/formulaires/exporter_blocktypes.php +++ b/formulaires/exporter_blocktypes.php @@ -6,6 +6,8 @@ if (!defined('_ECRIRE_INC_VERSION')) { return; } +include_spip('inc/blocks'); + function formulaires_exporter_blocktypes_charger() { if (!autoriser('webmestre')) { return null; diff --git a/inc/blocks.php b/inc/blocks.php new file mode 100644 index 0000000..dafada8 --- /dev/null +++ b/inc/blocks.php @@ -0,0 +1,343 @@ +<?php + +/** + * Fonctions utiles au plugin Blocks + * + * @plugin Blocks + * @copyright 2023 + * @author nicod_ + * @licence GNU/GPL + * @package SPIP\Blocks\Fonctions + */ + +/** + * Sérialisation de données (saisies ou valeurs) + * + * @param $data + * @return false|string + */ +function blocks_serialize($data) { + return ($data && is_array($data)) ? json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK) : ''; +} + +/** + * Désérialisation de données (saisies ou valeurs) + * + * @param $data + * @return array|null + */ +function blocks_deserialize($data): ?array { + $retour = json_decode($data ?? '', true); + + return is_array($retour) ? $retour : null; +} + + +/** + * Chercher tous les types de blocs existant + * On cherche des fichiers blocks/*.yaml dans le PATH + * + * @param bool $exclure_ignores + * @return array + */ +function blocktypes_lister_types(bool $exclure_ignores = true): array { + static $cache; + $hash = ($exclure_ignores ? 1 : 0); + if (isset($cache[$hash])) { + return $cache[$hash] ?: []; + } + + $liste_blocktypes = []; + $match = '[^-]*[.]yaml$'; + $liste = find_all_in_path('blocks/', $match); + + if ($liste) { + include_spip('inc/yaml'); + foreach ($liste as $blocktype => $chemin) { + $blocktype = str_replace('.yaml', '', $blocktype);// + $yaml_data = yaml_charger_inclusions(yaml_decode_file($chemin)); + if (is_array($yaml_data)) { + $yaml_data['titre'] = _T_ou_typo($yaml_data['titre']); + $yaml_data['identifiant'] = $blocktype; + $yaml_data['description'] = _T_ou_typo($yaml_data['description']); + $yaml_data['icone'] = isset($yaml_data['icone']) ? blocktype_find_icone_path($yaml_data['icone']) : ''; + + $liste_blocktypes[$blocktype] = $yaml_data; + } + } + } + + // Puis les trier par ordre alphabétique + uasort($liste_blocktypes, function ($valeur1, $valeur2) { + if ($valeur1['nom'] > $valeur2['nom']) { + return 1; + } else if ($valeur1['nom'] == $valeur2['nom']) { + return 0; + } else { + return -1; + } + }); + + // Retirer les modèles désactivés dans la config + if ($exclure_ignores === true) { + include_spip('inc/config'); + $config_ignorer = lire_config('blocks/ignorer_blocktypes') ?? []; + foreach ($config_ignorer as $ignorer) { + unset($liste_blocktypes[$ignorer]); + } + } + $cache[$hash] = $liste_blocktypes; + + return $liste_blocktypes; +} + +/** + * Retrouver l'icone d'un blocktype, la réduire à 16 px + * + * @param string $blocktype + * @return string chemin + **/ +function blocktype_find_icone_path(string $blocktype): string { + static $cache; + if (isset($cache[$blocktype])) { + return $cache[$blocktype]; + } + if (!$chemin = find_in_path("blocks/$blocktype")) { + if (!$chemin = find_in_path("icones/$blocktype")) { + if (!$chemin = find_in_theme("images/$blocktype")) { + if (!$chemin = find_in_path($blocktype)) { + $chemin = ''; + } + } + } + } + include_spip('inc/filtres'); + include_spip('inc/filtres_images_mini'); + //Au cas où find_in_theme nous return un truc genre gis-xx.svg?16px + $chemin = preg_replace('#\?.*$#', '', $chemin); + //On force la reduction à 16px + //$chemin = extraire_attribut(image_reduire($chemin, 16), 'src'); + $cache[$blocktype] = $chemin; + + return $chemin; +} + +/** + * Obtenir les saisies d'un block + * + * @param int $id_block + * @return array + */ +function block_get_saisies(int $id_block): array { + static $cache; + if (isset($cache[$id_block])) { + return $cache[$id_block]; + } + $saisies = []; + if ($blocktype = sql_getfetsel('blocktype', 'spip_blocks', 'id_block = ' . $id_block)) { + $saisies = blocktype_get_saisies($blocktype); + } else { + spip_log("get_block_saisies($id_block) : pas de blocktype", 'blocks.' . _LOG_DEBUG); + } + $cache[$id_block] = $saisies; + + return $saisies; +} + +/** + * Obtenir toute la config d'un block + * + * @param string $blocktype + * @return array + */ +function blocktype_get_config(string $blocktype): array { + $blocktypes = blocktypes_lister_types(); + + return $blocktypes[$blocktype] ?? []; +} + +/** + * Obtenir les saisies d'un block + * + * @param string $blocktype + * @return array + */ +function blocktype_get_saisies(string $blocktype): array { + $blocktypes = blocktypes_lister_types(); + + return $blocktypes[$blocktype]['saisies'] ?? []; +} + +/** + * Obtenir le titre d'un block + * + * @param string $blocktype + * @return mixed|string + */ +function blocktype_get_titre(string $blocktype) { + $blocktypes = blocktypes_lister_types(); + + return $blocktypes[$blocktype]['titre'] ?? $blocktype; +} + +/** + * Obtenir l'icone d'un block + * + * @param string $blocktype + * @return string + */ +function blocktype_get_icone(string $blocktype): string { + $blocktypes = blocktypes_lister_types(); + + return $blocktypes[$blocktype]['icone'] ?? ''; +} + +/** + * Retourner un tableau des valeurs saisies + * + * @param string $saisies (sérialisées) + * @param string $valeurs (sérialisées) + * + * @return array + */ +function block_get_valeurs(array $saisies, array $valeurs): array { + $retour = []; + include_spip('inc/saisies_lister'); + $saisies_par_nom = saisies_lister_par_nom($saisies); + foreach ($saisies_par_nom as $nom => $saisie) { + $retour[$nom] = $valeurs[$nom] ?? null; + } + + return $retour; +} + +/** + * Chercher le squelette correspondant à un block par son identifiant + * + * @param string $blocktype Identifiant du type de block + * @param bool $force_public Forcer une recherche du squelette public + * @param bool $chemin_complet Retourne le chemin complet du squelette + * @param bool $dist Retourne le squelette par défaut (dist)) + * @return string Chemin du squelette trouvé + */ + +function blocks_trouver_squelette( + string $blocktype, + bool $force_public = false, + bool $chemin_complet = false, + bool $dist = true +): string { + static $cache; + $cle = $blocktype . (int)$force_public . (int)$chemin_complet . (int)$dist; + + if (isset($cache[$cle])) { + return $cache[$cle]; + } + + $squelette = ''; + if ( + !$force_public + && test_espace_prive() + && ($f = find_in_path('blocks/' . $blocktype . '_prive.' . _EXTENSION_SQUELETTES)) + && lire_fichier($f, $contenu) + ) { + $squelette = $chemin_complet ? $f : 'blocks/' . $blocktype . '_prive'; + } else if ( + ($f = find_in_path('blocks/' . $blocktype . '.' . _EXTENSION_SQUELETTES)) + && lire_fichier($f, $contenu) + ) { + $squelette = $chemin_complet ? $f : 'blocks/' . $blocktype; + } else if ( + $dist + && ($f = find_in_path('blocks/dist.' . _EXTENSION_SQUELETTES)) + && lire_fichier($f, $contenu) + ) { + $squelette = $chemin_complet ? $f : 'blocks/dist'; + } + $cache[$cle] = $squelette; + + return $squelette; +} + +/** + * Wrapper sans typage de la fonction blocks_trouver_squelette pour les squelettes SPIP + * Les paramètres peuvent être vides ou des chaines et pas des bool + * + * @param $blocktype + * @param $force_public + * @param $chemin_complet + * @param $dist + * @return string + */ +function filtre_blocktype_squelette( + $blocktype, + $force_public = false, + $chemin_complet = false, + $dist = true +) { + return blocks_trouver_squelette( + (string)$blocktype, + (bool)$force_public, + (bool)$chemin_complet, + (bool)$dist + ); +} + +/** + * Calculer la liste des types de blocks pouvant être créés sous un type de block + * @param $blocktype + * @return array + */ +function bloctypes_trouver_enfants($blocktype): array { + return bloctypes_trouver_role($blocktype, 'enfant'); +} + +/** + * Calculer la liste des types de blocks dans lesquels un type de block peut être créé + * @param $blocktype + * @return array + */ +function bloctypes_trouver_parents($blocktype): array { + return bloctypes_trouver_role($blocktype, 'parent'); +} + +/** + * Calculer une liste de types de blocks enfants ou parents en fonction du rôle + * @param $id_blocktype + * @param $role + * @return array + */ +function bloctypes_trouver_role($blocktype, $role): array { + // TODO : à réécrire !!! + + // include_spip('action/editer_liens'); + // if ( + // $id_blocktype + // && $role + // && ($blocktypes_parents = objet_trouver_liens(['blocktype' => $id_blocktype], ['blocktype' => '*'], ['role' => $role])) + // ) { + // return array_column($blocktypes_parents, 'id_objet'); + // } + + return []; +} + +/** + * Génére la liste des objets (du type `article`) utilisable dans une saisie + * + * @return array + */ +function blocks_objets_config() { + include_spip('base/objets'); + $data = []; + if ($objets = array_filter(lire_config('blocks/objets'))) { + foreach ($objets as $objet) { + $objet_type = objet_type($objet); + $infos = objet_info($objet_type, 'texte_objet'); + $data[$objet_type] = _T($infos); + } + asort($data); + } + + return $data; +} -- GitLab