Skip to content
Extraits de code Groupes Projets
blocks_pipelines.php 3,01 ko
Newer Older
<?php
/**
 * Utilisations de pipelines par Blocks
 *
 * @plugin     Blocks
 * @copyright  2023
 * @author     nicod_
 * @licence    GNU/GPL
 * @package    SPIP\Blocks\Pipelines
 */

if (!defined('_ECRIRE_INC_VERSION')) {
	return;
}

function blocks_header_prive($flux) {
	$flux .= '<link rel="stylesheet" href="'.timestamp(find_in_path('css/blocks_prive.css')).'" type="text/css" />';
	return $flux;
}

/**
 * Afficher la gestion des blocs sur la vue des objets
 *
 * @pipeline afficher_contenu_objet
 * @param array $flux Données du pipeline
 * @return array      Données du pipeline
 **/
function blocks_afficher_contenu_objet($flux) {
	if (
		($objet = $flux['args']['type'])
		&& ($args = $flux['args'] ?? [])
		&& ($id_objet = $args['id_objet'] ?? null)
		&& ($objets = lire_config('blocks/objets'))
		&& in_array(table_objet_sql($objet), $objets)
	) {
		$texte = recuperer_fond(
			'prive/squelettes/inclure/blocs_objet',
			[
				'objet' => $objet,
				'id_objet' => $id_objet
			],
		);
		if ($p = strpos($flux['data'], '<!--affiche_milieu-->')) {
			$flux['data'] = substr_replace($flux['data'], $texte, $p, 0);
		} else {
			$flux['data'] .= $texte;
		}
	}
	return $flux;
}

/**
 * Ajouter les objets sur les vues des parents directs
 *
 * @pipeline affiche_enfants
 * @param array $flux Données du pipeline
 * @return array       Données du pipeline
 **/
function blocks_affiche_enfants($flux) {
	if (
		$e = trouver_objet_exec($flux['args']['exec'])
		and $e['edition'] === false
	) {
		$id_objet = $flux['args']['id_objet'];

		if ($e['type'] === 'blocs_type') {
			$flux['data'] .= recuperer_fond(
				'prive/objets/liste/blocs',
				[
					'titre'         => _T('bloc:titre_blocs'),
					'id_blocs_type' => $id_objet,
				]
			);

		}
	}
	return $flux;
}

/**
 * Compter les enfants d'un objet
 *
 * @pipeline objets_compte_enfants
 * @param array $flux Données du pipeline
 * @return array       Données du pipeline
 **/
function blocks_objet_compte_enfants($flux) {
	if ($flux['args']['objet'] == 'blocs_type' and $id_blocs_type = intval($flux['args']['id_objet'])) {
		// juste les publiés ?
		if (array_key_exists('statut', $flux['args']) and ($flux['args']['statut'] == 'publie')) {
			$flux['data']['blocs'] = sql_countsel('spip_blocs', 'id_blocs_type= ' . intval($id_blocs_type) . " AND (statut = 'publie')");
		} else {
			$flux['data']['blocs'] = sql_countsel('spip_blocs', 'id_blocs_type= ' . intval($id_blocs_type) . " AND (statut <> 'poubelle')");
		}
	}
	return $flux;
}

/**
 * Optimiser la base de données
 *
 * Supprime les liens orphelins de l'objet vers quelqu'un et de quelqu'un vers l'objet.
 * Supprime les objets à la poubelle.
 *
 * @pipeline optimiser_base_disparus
 * @param array $flux Données du pipeline
 * @return array       Données du pipeline
 */
function blocks_optimiser_base_disparus($flux) {

	include_spip('action/editer_liens');
	$flux['data'] += objet_optimiser_liens(['bloc' => '*'], '*');

	sql_delete('spip_blocs', "statut='poubelle' AND maj < " . $flux['args']['date']);
	return $flux;
}