Newer
Older
<?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
*/
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
/**
* Compile la balise `#GENERER_BLOCK` qui génère l'affiche d'un block'
* @param Champ $p Pile au niveau de la balise
* @return Champ Pile complétée par le code à générer
* @example
* #GENERER_BLOCK génère le block de la boucle(BLOCK) en cours
* #GENERER_BLOCK{3} génère le block 3
*/
function balise_GENERER_BLOCK_dist($p) {
if (!($id_block = interprete_argument_balise(1, $p))) {
if ($p->id_boucle) {
$id_block = champ_sql($p->boucles[$p->id_boucle]->primary, $p);
}
}
if (!$id_block) {
$msg = _T('zbug_balise_sans_argument', ['balise' => ' GENERER_BLOCK']);
erreur_squelette($msg, $p);
$p->interdire_scripts = true;
return $p;
}
$p->code = "_block_charger_block($id_block)";
$p->interdire_scripts = false;
return $p;
}
function _block_charger_block(int $id_block): string {
$html = '';

nicod
a validé
if (!test_espace_prive()) {

nicod
a validé
}
$saisies = block_get_saisies($id_block);
$valeurs = blocks_deserialize(sql_getfetsel('valeurs', 'spip_blocks', $where));
if ($saisies && $valeurs) {

nicod
a validé
$contexte = array_merge(
[
'id_block' => $id_block,
],

nicod
a validé
);
$html = recuperer_fond('inclure/block', $contexte);
if ($blocs_enfants = sql_allfetsel(
'id_block, blocktype, valeurs',
'spip_blocks',
'objet="block" and id_objet = ' . $id_block
)) {
$html_enfants = '';
foreach ($blocs_enfants as $bloc_enfant) {
$contexte = array_merge(
[
'id_block' => $bloc_enfant['id_block'],
],
block_get_valeurs(
blocktype_get_saisies($bloc_enfant['type']),
blocks_deserialize($bloc_enfant['valeurs'])
)
);
$squelette = test_espace_prive() ? 'prive/squelettes/inclure/block_objet' : 'inclure/block';
$html_enfants .= recuperer_fond($squelette, $contexte);
}
if (str_contains($html, '<!--blocks-->')) {
$html = str_replace('<!--blocks-->', $html_enfants, $html);
} else {
$html .= $html_enfants;
}
}

nicod
a validé
}
/**
* Compile la balise `#GENERER_BLOCKS` qui génère l'affiche des blocks liés à un objet
* @param Champ $p Pile au niveau de la balise
* @return Champ Pile complétée par le code à générer
* @example
* #GENERER_BLOCKS génère les blocks de l'objet de la bouvle en cours
* #GENERER_BLOCKS{article,3} génère les blocks de l'article 3
*/
function balise_GENERER_BLOCKS_dist($p) {
if ($objet = interprete_argument_balise(1, $p)) {
$id_objet = interprete_argument_balise(2, $p);
} else {
$id_objet = null;
if ($p->id_boucle) {
$id_objet = champ_sql($p->boucles[$p->id_boucle]->primary, $p);
$objet = "objet_type('" . $p->boucles[$p->id_boucle]->id_table . "')";
}
}
if (!$objet || !$id_objet) {
$msg = _T('zbug_balise_sans_argument', ['balise' => ' GENERER_BLOCKS']);
erreur_squelette($msg, $p);
$p->interdire_scripts = true;
return $p;
}
$p->code = "_block_charger_blocks($objet, $id_objet)";
$p->interdire_scripts = false;
return $p;
}
function _block_charger_blocks(string $objet, int $id_objet): string {
$retour = '';

nicod
a validé
$where = [
'objet = ' . sql_quote($objet),
'id_objet = ' . (int)$id_objet,

nicod
a validé
];
if (!test_espace_prive()) {
$where[] = 'statut = ' . sql_quote('publie');

nicod
a validé
}
$blocks = sql_allfetsel(

nicod
a validé
$where,
'',

nicod
a validé
);
foreach ($blocks as $block) {
$retour .= _block_charger_block($block['id_block']);

nicod
a validé
return $retour;
}
* Générer le titre d'un block
* @param $id_block
'objet, id_objet, rang_lien, blocktype',
'spip_blocks',
'id_block = ' . (int)$id_block
if ($infos['id_objet'] && $infos['objet']) {
$titre = generer_objet_info($infos['id_objet'], $infos['objet'], 'titre') . ' - ' . $titre . ' #' . $infos['rang_lien'];
}
} else {
$titre = _T('block:titre_block') . ' ' . $id_block;
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* 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'] ?? '';
}

nicod
a validé
/**
* 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 {

nicod
a validé
$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;

nicod
a validé
}

nicod
a validé
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 {
$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';
($f = find_in_path('blocks/' . $blocktype . '.' . _EXTENSION_SQUELETTES))
&& lire_fichier($f, $contenu)
) {
$squelette = $chemin_complet ? $f : 'blocks/' . $blocktype;
} else if (
&& ($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
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éé
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 = [];
foreach ($objets as $objet) {
$objet_type = objet_type($objet);
$data[$objet_type] = _T($infos);
}
asort($data);
}