Newer
Older
<?php
/**
* Fonctions utiles au plugin Blocks
*
* @plugin Blocks
* @copyright 2023
* @author nicod_
* @licence GNU/GPL
* @package SPIP\Blocks\Fonctions
*/
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
/**
* Sérialisation de données (saisies ou valeurs)
*
* @param $data
* @return false|string
*/
function blocks_serialize($data) {
return json_encode($data);
}
/**
* Désérialisation de données (saisies ou valeurs)
*
* @param $data
* @return mixed
*/
function blocks_deserialize($data) {
return json_decode($data ?? '', true);
}
/**
* Compile la balise `#GENERER_BLOCK` qui génère l'affiche d'un bloc'
*
* @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
* #GENERER_BLOCK{3}
* ```
*/
function balise_GENERER_BLOCK_dist($p) {
if (!($id_bloc = interprete_argument_balise(1, $p))) {
if ($p->id_boucle) {
$id_bloc = champ_sql($p->boucles[$p->id_boucle]->primary, $p);
}
}
if (!$id_bloc) {
$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_bloc)";
$p->interdire_scripts = false;
return $p;
}

nicod
a validé
$retour = '';
$where = ['b.id_bloc = ' . (int)$id_bloc];
if (!test_espace_prive()) {
$where[] = 'b.statut = ' . sql_quote('publie');
}
if ($infos = sql_fetsel(
'b.valeurs, bt.saisies, bt.identifiant',
'spip_blocs_types bt join spip_blocs b using(id_blocs_type)',

nicod
a validé
$where
)) {
$contexte = [
'id_bloc' => $id_bloc,
];
// transmettre les valeurs saisies dans le bloc au squelette
$saisies_bloc_type = blocks_deserialize($infos['saisies']);
if ($valeurs_saisies = blocks_deserialize($infos['valeurs'])) {
include_spip('inc/saisies_lister');
$saisies_par_nom = saisies_lister_par_nom($saisies_bloc_type);
foreach ($saisies_par_nom as $nom => $saisie) {

nicod
a validé
}

nicod
a validé
$squelette = blocks_trouver_squelette($infos['identifiant']);

nicod
a validé
$retour = recuperer_fond($squelette, $contexte);
}
return $retour;
/**
* Compile la balise `#GENERER_BLOCKS` qui génère l'affiche des blocs liés à un objet
*
* @balise
* @param Champ $p Pile au niveau de la balise
* @return Champ Pile complétée par le code à générer
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
* @example
* ```
* #GENERER_BLOCKS
* #GENERER_BLOCKS{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($objet, $id_objet) {
$retour = '';

nicod
a validé
$where = [
'bl.objet = ' . sql_quote($objet),
'bl.id_objet = ' . (int)$id_objet,
];
if (!test_espace_prive()) {
$where[] = 'b.statut = ' . sql_quote('publie');
}
$blocs = sql_allfetsel(
'b.id_bloc',
'spip_blocs_liens bl join spip_blocs b using(id_bloc)',
$where,
'',
'bl.rang_lien'
);
foreach ($blocs as $bloc) {
$retour .= _block_charger_block($bloc['id_bloc']);
}

nicod
a validé
return $retour;
}
/**
* Générer le titre d'un bloc
* composé de son type et du titre de l'objet auquel il est lié
*
* @param $id_bloc
* @return mixed|string
*/
'bt.titre, bl.objet, bl.rang_lien, bl.id_objet',
'spip_blocs b join spip_blocs_types bt using(id_blocs_type) left join spip_blocs_liens bl using(id_bloc)',
)) {
$titre = $infos['titre'];
if ($infos['id_objet'] && $infos['objet']) {
$titre = generer_objet_info($infos['id_objet'], $infos['objet'], 'titre') . ' - ' . $titre . ' #' . $infos['rang_lien'];
}
} else {
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Chercher le squelette correspondant à un bloc par son identifiant
*
* @param string $identifiant Identifiant du bloc
* @param bool $force_public Forcer une recherche du squelette public
* @return string Chemin du squelette trouvé
*/
function blocks_trouver_squelette($identifiant, $force_public = false) {
static $cache;
$cle = $identifiant . ($force_public ? 'oui' : 'non');
if (isset($cache[$cle])) {
return $cache[$cle];
}
if (
!$force_public
&& test_espace_prive()
&& ($f = find_in_path($identifiant . '.' . _EXTENSION_SQUELETTES, 'blocks_prive/'))
&& lire_fichier($f, $contenu)
) {
$squelette = 'blocks_prive/' . $identifiant;
} else if (
($f = find_in_path($identifiant . '.' . _EXTENSION_SQUELETTES, 'blocks/'))
&& lire_fichier($f, $contenu)
) {
$squelette = 'blocks/' . $identifiant;
} else {
$squelette = 'blocks/dist';
}
$cache[$cle] = $squelette;
return $squelette;
}