Newer
Older
<?php
/**
* Fonctions utiles au plugin Blocks
*
* @plugin Blocks
* @copyright 2023
* @author nicod_
* @licence GNU/GPL
* @package SPIP\Blocks\Fonctions
*/
* 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;