[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / plan / action / deplacer_objets.php
1 <?php
2
3 /**
4 * Action retournant un morceau du plan du site (en ajax)
5 *
6 * @plugin Plan du site dans l’espace privé
7 * @copyright 2015
8 * @author Matthieu Marcillaud
9 * @licence GNU/GPL
10 * @package SPIP\Plan\Action
11 */
12
13 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16
17
18 function action_deplacer_objets_dist() {
19
20 include_spip('inc/autoriser');
21 if (!autoriser('ecrire')) {
22 return plan_json_erreur(_T('plan:erreur_autorisation_insuffisante') . ' ' . _T('plan:erreur_deplacement_impossible'));
23 }
24
25 include_spip('base/objets');
26
27 $objet = objet_type(_request('objet'));
28 $table = table_objet_sql($objet);
29 $_id_table = id_table_objet($table);
30
31 $ids = _request('id_objet');
32 $id_rubrique_old = _request('id_rubrique_source');
33 $id_rubrique_new = _request('id_rubrique_destination');
34
35 if (!is_array($ids) or !$objet) {
36 return plan_json_erreur(_T('plan:erreur_aucun_identifiant') . ' ' . _T('plan:erreur_deplacement_impossible'));
37 }
38 if ($id_rubrique_old == $id_rubrique_new) {
39 return plan_json_erreur(_T('plan:erreur_rubriques_parentes_incorrectes') . ' ' . _T('plan:erreur_deplacement_impossible'));
40 }
41 if ($objet != 'rubrique' and !$id_rubrique_new) {
42 return plan_json_erreur(_T('plan:erreur_rubriques_parentes_incorrectes') . ' ' . _T('plan:erreur_deplacement_impossible'));
43 }
44
45 $ids = array_filter($ids);
46
47 if ($objet == 'rubrique') {
48 $champ = 'id_parent';
49 } else {
50 $champ = 'id_rubrique';
51 }
52
53 // ne modifier que si les emplacements n'ont pas déjà changé !
54 $ids = sql_allfetsel($_id_table, $table, array(sql_in($_id_table, $ids), $champ . '=' . sql_quote($id_rubrique_old)));
55 $ids = array_map('array_shift', $ids);
56
57 include_spip('action/editer_objet');
58
59 $errors = $success = array();
60 $modifs = array('id_parent' => $id_rubrique_new);
61
62 foreach ($ids as $id) {
63 if (autoriser('modifier', $objet, $id)) {
64 if ($err = objet_modifier($objet, $id, $modifs)) {
65 $errors["$objet-$id"] = $err;
66 } else {
67 $success["$objet-$id"] = true;
68 }
69 } else {
70 $errors["$objet-$id"] = _T('plan:erreur_autorisation_insuffisante') . ' ' . _T('plan:erreur_deplacement_impossible');
71 }
72 }
73
74 // dans certains cas… on ne reçoit pas d'erreur… et pourtant !
75 if (!$errors) {
76 // on verifie qu'il n'y a plus d'objets à l'ancien emplacement
77 $ids = sql_allfetsel(
78 $_id_table,
79 $table,
80 array(sql_in($_id_table, $ids), $champ . '=' . sql_quote($id_rubrique_old))
81 );
82 $ids = array_map('array_shift', $ids);
83 if ($ids) {
84 foreach ($ids as $id) {
85 $errors["$objet-$id"] = _T('plan:erreur_deplacement');
86 unset($success["$objet-$id"]);
87 }
88 }
89 }
90
91 return plan_json_envoi(array(
92 'done' => true,
93 'success' => $success,
94 'errors' => $errors,
95 ));
96 }
97
98 function plan_json_envoi($data) {
99 header('Content-Type: application/json; charset=' . $GLOBALS['meta']['charset']);
100 echo json_encode($data);
101 }
102
103 function plan_json_erreur($msg) {
104 return plan_json_envoi(array(
105 'done' => false,
106 'success' => array(),
107 'errors' => array($msg)
108 ));
109 }