<?php /** * Fonctions utiles au plugin Blocks * * @plugin Blocks * @copyright 2023 * @author nicod_ * @licence GNU/GPL * @package SPIP\Blocks\Fonctions */ include_spip('inc/blocks'); /** * Compile la balise `#GENERER_BLOCK` qui génère l'affiche d'un block' * * @uses _block_charger_block() * @balise * @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; } /** * Fonction interne à la balise GENERER_BLOCK * * @param int $id_block * @return string */ function _block_charger_block(int $id_block): string { $html = ''; $where = ['id_block = ' . $id_block]; if (!test_espace_prive()) { $where[] = 'statut = ' . sql_quote('publie'); } $saisies = block_get_saisies($id_block); $valeurs = blocks_deserialize(sql_getfetsel('valeurs', 'spip_blocks', $where)); if ($saisies && $valeurs) { $contexte = array_merge( [ 'id_block' => $id_block, ], block_get_valeurs($saisies, $valeurs) ); $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_info('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; } } } return $html; } /** * Compile la balise `#GENERER_BLOCKS` qui génère l'affiche des blocks liés à un objet * * @uses _block_charger_blocks() * @balise * @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; } /** * Fonction interne à la balise GENERER_BLOCKS * * @param string $objet * @param int $id_objet * @return string */ function _block_charger_blocks(string $objet, int $id_objet): string { $retour = ''; $where = [ 'objet = ' . sql_quote($objet), 'id_objet = ' . (int)$id_objet, ]; if (!test_espace_prive()) { $where[] = 'statut = ' . sql_quote('publie'); } $blocks = sql_allfetsel( 'id_block', 'spip_blocks b', $where, '', 'rang_lien' ); foreach ($blocks as $block) { $retour .= _block_charger_block($block['id_block']); } return $retour; } /** * Générer le titre d'un block * composé de son type et du titre de l'objet auquel il est lié * * @param $id_block * @return string */ function generer_TITRE_BLOCK($id_block): string { if ($infos = sql_fetsel( 'objet, id_objet, rang_lien, blocktype', 'spip_blocks', 'id_block = ' . (int)$id_block )) { $titre = blocktype_info($infos['blocktype'], 'titre'); 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; } return $titre; }