3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
14 * Gestion générique de modification des objets éditoriaux
16 * @package SPIP\Core\Edition
19 if (!defined('_ECRIRE_INC_VERSION')) {
24 * Point d'entrée d'édition d'un objet
26 * On ne peut entrer que par un appel en fournissant $id et $objet
27 * ou avec un argument d'action sécurisée de type "objet/id"
30 * @param string $objet
34 function action_editer_objet_dist($id = null, $objet = null, $set = null) {
36 // appel direct depuis une url avec arg = "objet/id"
37 if (is_null($id) or is_null($objet)) {
38 $securiser_action = charger_fonction('securiser_action', 'inc');
39 $arg = $securiser_action();
40 list($objet, $id) = array_pad(explode("/", $arg, 2), 2, null);
43 // appel incorrect ou depuis une url erronnée interdit
44 if (is_null($id) or is_null($objet)) {
45 include_spip('inc/minipres');
46 echo minipres(_T('info_acces_interdit'));
50 // si id n'est pas un nombre, c'est une creation
51 // mais on verifie qu'on a toutes les donnees qu'il faut.
52 if (!$id = intval($id)) {
53 // on ne sait pas si un parent existe mais on essaye
54 $id_parent = _request('id_parent');
55 $id = objet_inserer($objet, $id_parent);
58 if (!($id = intval($id)) > 0) {
59 return array($id, _L('echec enregistrement en base'));
62 // Enregistre l'envoi dans la BD
63 $err = objet_modifier($objet, $id, $set);
65 return array($id, $err);
69 * Appelle toutes les fonctions de modification d'un objet
70 * $err est un message d'erreur eventuelle
72 * @param string $objet
74 * @param array|null $set
75 * @return mixed|string
77 function objet_modifier($objet, $id, $set = null) {
78 if (include_spip('action/editer_' . $objet)
79 and function_exists($modifier = $objet . "_modifier")
81 return $modifier($id, $set);
84 $table_sql = table_objet_sql($objet);
85 $trouver_table = charger_fonction('trouver_table', 'base');
86 $desc = $trouver_table($table_sql);
87 if (!$desc or !isset($desc['field'])) {
88 spip_log("Objet $objet inconnu dans objet_modifier", _LOG_ERREUR
);
90 return _L("Erreur objet $objet inconnu");
92 include_spip('inc/modifier');
95 if (isset($desc['date']) and $desc['date']) {
96 $champ_date = $desc['date'];
97 } elseif (isset($desc['field']['date'])) {
101 $white = array_keys($desc['field']);
102 // on ne traite pas la cle primaire par defaut, notamment car
103 // sur une creation, id_x vaut 'oui', et serait enregistre en id_x=0 dans la base
104 $white = array_diff($white, array($desc['key']['PRIMARY KEY']));
106 if (isset($desc['champs_editables']) and is_array($desc['champs_editables'])) {
107 $white = $desc['champs_editables'];
109 $c = collecter_requests(
113 array($champ_date, 'statut', 'id_parent', 'id_secteur'),
114 // donnees eventuellement fournies
118 // Si l'objet est publie, invalider les caches et demander sa reindexation
119 if (objet_test_si_publie($objet, $id)) {
120 $invalideur = "id='$objet/$id'";
127 if ($err = objet_modifier_champs($objet, $id,
131 'invalideur' => $invalideur,
132 'indexation' => $indexation,
133 // champ a mettre a date('Y-m-d H:i:s') s'il y a modif
134 'date_modif' => (isset($desc['field']['date_modif']) ?
'date_modif' : '')
141 // Modification de statut, changement de rubrique ?
142 // FIXME: Ici lorsqu'un $set est passé, la fonction collecter_requests() retourne tout
143 // le tableau $set hors black liste, mais du coup on a possiblement des champs en trop.
144 $c = collecter_requests(array($champ_date, 'statut', 'id_parent'), array(), $set);
145 $err = objet_instituer($objet, $id, $c);
151 * Insere en base un objet generique
153 * @param string $objet
154 * @param int $id_parent
155 * @param array|null $set
156 * @global array $GLOBALS ['visiteur_session']
157 * @global array $GLOBALS ['meta']
158 * @global string $GLOBALS ['spip_lang']
161 function objet_inserer($objet, $id_parent = null, $set = null) {
162 if (include_spip('action/editer_' . $objet)
163 and function_exists($inserer = $objet . "_inserer")
165 return $inserer($id_parent, $set);
168 $table_sql = table_objet_sql($objet);
169 $trouver_table = charger_fonction('trouver_table', 'base');
170 $desc = $trouver_table($table_sql);
171 if (!$desc or !isset($desc['field'])) {
177 if (isset($desc['field']['id_rubrique'])) {
178 // Si id_rubrique vaut 0 ou n'est pas definie, creer l'objet
179 // dans la premiere rubrique racine
180 if (!$id_rubrique = intval($id_parent)) {
181 $row = sql_fetsel("id_rubrique, id_secteur, lang", "spip_rubriques", "id_parent=0", '', '0+titre,titre', "1");
182 $id_rubrique = $row['id_rubrique'];
184 $row = sql_fetsel("lang, id_secteur", "spip_rubriques", "id_rubrique=" . intval($id_rubrique));
187 $champs['id_rubrique'] = $id_rubrique;
188 if (isset($desc['field']['id_secteur'])) {
189 $champs['id_secteur'] = $row['id_secteur'];
191 $lang_rub = $row['lang'];
194 // La langue a la creation : si les liens de traduction sont autorises
195 // dans les rubriques, on essaie avec la langue de l'auteur,
196 // ou a defaut celle de la rubrique
197 // Sinon c'est la langue de la rubrique qui est choisie + heritee
198 if (isset($desc['field']['lang']) and !empty($GLOBALS['meta']['multi_objets']) and in_array($table_sql,
199 explode(',', $GLOBALS['meta']['multi_objets']))
201 lang_select($GLOBALS['visiteur_session']['lang']);
202 if (in_array($GLOBALS['spip_lang'],
203 explode(',', $GLOBALS['meta']['langues_multilingue']))) {
204 $champs['lang'] = $GLOBALS['spip_lang'];
205 if (isset($desc['field']['langue_choisie'])) {
206 $champs['langue_choisie'] = 'oui';
209 } elseif (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) {
210 $champs['lang'] = ($lang_rub ?
$lang_rub : $GLOBALS['meta']['langue_site']);
211 $champs['langue_choisie'] = 'non';
214 if (isset($desc['field']['statut'])) {
215 if (isset($desc['statut_textes_instituer'])) {
216 $cles_statut = array_keys($desc['statut_textes_instituer']);
217 $champs['statut'] = reset($cles_statut);
219 $champs['statut'] = 'prepa';
224 if ((isset($desc['date']) and $d = $desc['date']) or isset($desc['field'][$d = 'date'])) {
225 $champs[$d] = date('Y-m-d H:i:s');
229 $champs = array_merge($champs, $set);
232 // Envoyer aux plugins
233 $champs = pipeline('pre_insertion',
236 'table' => $table_sql,
242 $id = sql_insertq($table_sql, $champs);
245 // controler si le serveur n'a pas renvoye une erreur
246 // et associer l'auteur sinon
247 // si la table n'a pas deja un champ id_auteur
248 // et si le form n'a pas poste un id_auteur (meme vide, ce qui sert a annuler cette auto association)
250 and !isset($desc['field']['id_auteur'])
252 $id_auteur = ((is_null(_request('id_auteur')) and isset($GLOBALS['visiteur_session']['id_auteur'])) ?
253 $GLOBALS['visiteur_session']['id_auteur']
254 : _request('id_auteur'));
256 include_spip('action/editer_auteur');
257 auteur_associer($id_auteur, array($objet => $id));
261 pipeline('post_insertion',
264 'table' => $table_sql,
278 * Modifie le statut et/ou la date d'un objet
280 * @param string $objet
283 * $c est un array ('statut', 'id_parent' = changement de rubrique)
284 * statut et rubrique sont lies, car un admin restreint peut deplacer
285 * un objet publie vers une rubrique qu'il n'administre pas
286 * @param bool $calcul_rub
289 function objet_instituer($objet, $id, $c, $calcul_rub = true) {
290 if (include_spip('action/editer_' . $objet)
291 and function_exists($instituer = $objet . "_instituer")
293 return $instituer($id, $c, $calcul_rub);
296 $table_sql = table_objet_sql($objet);
297 $trouver_table = charger_fonction('trouver_table', 'base');
298 $desc = $trouver_table($table_sql);
299 if (!$desc or !isset($desc['field'])) {
300 return _L("Impossible d'instituer $objet : non connu en base");
303 include_spip('inc/autoriser');
304 include_spip('inc/rubriques');
305 include_spip('inc/modifier');
308 $sel[] = (isset($desc['field']['statut']) ?
"statut" : "'' as statut");
311 if (isset($desc['date']) and $desc['date']) {
312 $champ_date = $desc['date'];
313 } elseif (isset($desc['field']['date'])) {
314 $champ_date = 'date';
317 $sel[] = ($champ_date ?
"$champ_date as date" : "'' as date");
318 $sel[] = (isset($desc['field']['id_rubrique']) ?
'id_rubrique' : "0 as id_rubrique");
320 $row = sql_fetsel($sel, $table_sql, id_table_objet($objet) . '=' . intval($id));
322 $id_rubrique = $row['id_rubrique'];
323 $statut_ancien = $statut = $row['statut'];
324 $date_ancienne = $date = $row['date'];
327 $d = ($date and isset($c[$champ_date])) ?
$c[$champ_date] : null;
328 $s = (isset($desc['field']['statut']) and isset($c['statut'])) ?
$c['statut'] : $statut;
330 // cf autorisations dans inc/instituer_objet
331 if ($s != $statut or ($d and $d != $date)) {
333 autoriser('publierdans', 'rubrique', $id_rubrique)
335 autoriser('instituer', $objet, $id, null, array('statut' => $s))
337 $statut = $champs['statut'] = $s;
339 if ($s != 'publie' and autoriser('modifier', $objet, $id)) {
340 $statut = $champs['statut'] = $s;
342 spip_log("editer_objet $id refus " . join(' ', $c));
346 // En cas de publication, fixer la date a "maintenant"
347 // sauf si $c commande autre chose
348 // ou si l'objet est deja date dans le futur
349 // En cas de proposition d'un objet (mais pas depublication), idem
351 if ($champs['statut'] == 'publie'
352 or ($champs['statut'] == 'prop' and !in_array($statut_ancien, array('publie', 'prop')))
355 if ($d or strtotime($d = $date) > time()) {
356 $champs[$champ_date] = $date = $d;
358 $champs[$champ_date] = $date = date('Y-m-d H:i:s');
364 // Verifier que la rubrique demandee existe et est differente
365 // de la rubrique actuelle
367 and isset($c['id_parent'])
368 and $id_parent = $c['id_parent']
369 and $id_parent != $id_rubrique
370 and (sql_fetsel('1', "spip_rubriques", "id_rubrique=" . intval($id_parent)))
372 $champs['id_rubrique'] = $id_parent;
374 // si l'objet etait publie
375 // et que le demandeur n'est pas admin de la rubrique
376 // repasser l'objet en statut 'propose'.
377 if ($statut == 'publie'
378 and !autoriser('publierdans', 'rubrique', $id_rubrique)
380 $champs['statut'] = 'prop';
385 // Envoyer aux plugins
386 $champs = pipeline('pre_edition',
389 'table' => $table_sql,
391 'action' => 'instituer',
392 'statut_ancien' => $statut_ancien,
393 'date_ancienne' => $date_ancienne,
394 'id_parent_ancien' => $id_rubrique,
400 if (!count($champs)) {
404 // Envoyer les modifs.
405 objet_editer_heritage($objet, $id, $id_rubrique, $statut_ancien, $champs, $calcul_rub);
407 // Invalider les caches
408 include_spip('inc/invalideur');
409 suivre_invalideur("id='$objet/$id'");
413 $t = strtotime($date);
414 $p = @$GLOBALS['meta']['date_prochain_postdate'];
415 if ($t > time() AND (!$p OR ($t < $p))) {
416 ecrire_meta('date_prochain_postdate', $t);
421 pipeline('post_edition',
424 'table' => $table_sql,
426 'action' => 'instituer',
427 'statut_ancien' => $statut_ancien,
428 'date_ancienne' => $date_ancienne,
429 'id_parent_ancien' => $id_rubrique,
436 if ($notifications = charger_fonction('notifications', 'inc')) {
437 $notifications("instituer$objet", $id,
438 array('statut' => $statut, 'statut_ancien' => $statut_ancien, 'date' => $date, 'date_ancienne' => $date_ancienne)
442 return ''; // pas d'erreur
446 * Fabrique la requete d'institution de l'objet, avec champs herites
448 * @param string $objet
450 * @param int $id_rubrique
451 * @param string $statut
452 * @param array $champs
456 function objet_editer_heritage($objet, $id, $id_rubrique, $statut, $champs, $cond = true) {
457 $table_sql = table_objet_sql($objet);
458 $trouver_table = charger_fonction('trouver_table', 'base');
459 $desc = $trouver_table($table_sql);
461 // Si on deplace l'objet
462 // changer aussi son secteur et sa langue (si heritee)
463 if (isset($champs['id_rubrique'])) {
465 $row_rub = sql_fetsel("id_secteur, lang", "spip_rubriques", "id_rubrique=" . sql_quote($champs['id_rubrique']));
466 $langue = $row_rub['lang'];
468 if (isset($desc['field']['id_secteur'])) {
469 $champs['id_secteur'] = $row_rub['id_secteur'];
472 if (isset($desc['field']['lang']) and isset($desc['field']['langue_choisie'])) {
473 if (sql_fetsel('1', $table_sql,
474 id_table_objet($objet) . "=" . intval($id) . " AND langue_choisie<>'oui' AND lang<>" . sql_quote($langue))) {
475 $champs['lang'] = $langue;
483 sql_updateq($table_sql, $champs, id_table_objet($objet) . '=' . intval($id));
485 // Changer le statut des rubriques concernees
487 include_spip('inc/rubriques');
488 //$postdate = ($GLOBALS['meta']["post_dates"] == "non" AND isset($champs['date']) AND (strtotime($champs['date']) < time()))?$champs['date']:false;
490 calculer_rubriques_if($id_rubrique, $champs, $statut, $postdate);