416 |
aurelien |
1 |
<?php
|
2458 |
jpm |
2 |
// declare(encoding='UTF-8');
|
922 |
aurelien |
3 |
/**
|
2458 |
jpm |
4 |
* Service CRUD sur une observation.
|
|
|
5 |
*
|
|
|
6 |
* @internal Mininum PHP version : 5.2
|
|
|
7 |
* @category CEL
|
|
|
8 |
* @package Services
|
|
|
9 |
* @subpackage Observations
|
|
|
10 |
* @version 0.1
|
|
|
11 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
|
|
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
|
|
13 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
|
|
14 |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
|
|
|
15 |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
|
|
|
16 |
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
|
|
|
17 |
*/
|
922 |
aurelien |
18 |
class Inventory extends Cel {
|
416 |
aurelien |
19 |
|
2458 |
jpm |
20 |
public function getRessource() {
|
|
|
21 |
|
416 |
aurelien |
22 |
}
|
|
|
23 |
|
2458 |
jpm |
24 |
public function getElement($uid){
|
|
|
25 |
// Controle detournement utilisateur
|
|
|
26 |
$this->controleUtilisateur($uid[0]);
|
|
|
27 |
if (!isset($uid[0])) {
|
|
|
28 |
return;
|
|
|
29 |
}
|
416 |
aurelien |
30 |
|
2458 |
jpm |
31 |
$parametres_recherche = array(array('ce_utilisateur',$uid[0]),array('ordre',$uid[1]));
|
416 |
aurelien |
32 |
|
2458 |
jpm |
33 |
$chercheur_observations = new RechercheObservation($this->config);
|
|
|
34 |
$retour_recherche = $chercheur_observations->rechercherObservations($uid[0], $parametres_recherche, 0, 1)->get();
|
416 |
aurelien |
35 |
|
2458 |
jpm |
36 |
$observation = array();
|
|
|
37 |
if (is_array($retour_recherche) && count($retour_recherche) > 0) {
|
|
|
38 |
$observation = $retour_recherche[0];
|
922 |
aurelien |
39 |
}
|
2458 |
jpm |
40 |
$observation = $this->formaterObservationVersTableauSequentiel($observation);
|
|
|
41 |
$sortie = json_encode($observation);
|
|
|
42 |
$this->envoyerJson($sortie);
|
|
|
43 |
return true;
|
416 |
aurelien |
44 |
}
|
2458 |
jpm |
45 |
|
1598 |
raphael |
46 |
private function formaterObservationVersTableauSequentiel(&$observation) {
|
2458 |
jpm |
47 |
if ($observation['date_observation']!="0000-00-00 00:00:00") {
|
|
|
48 |
list($year,$month,$day)= explode('-',$observation['date_observation']);
|
|
|
49 |
list($day)= explode(' ',$day);
|
|
|
50 |
$observation['date_observation']=$day."/".$month."/".$year;
|
|
|
51 |
}
|
416 |
aurelien |
52 |
|
2458 |
jpm |
53 |
$observation = array($observation['nom_sel'], $observation['nom_sel_nn'],
|
|
|
54 |
$observation['nom_ret'], $observation['nom_ret_nn'],
|
|
|
55 |
$observation['nt'], $observation['famille'], $observation['zone_geo'],
|
|
|
56 |
$observation['ce_zone_geo'], $observation['ordre'],
|
|
|
57 |
$observation['date_observation'], $observation['lieudit'],
|
|
|
58 |
$observation['station'], $observation['milieu'],
|
|
|
59 |
$observation['commentaire'], $observation['latitude'],
|
|
|
60 |
$observation['longitude']);
|
|
|
61 |
return $observation;
|
416 |
aurelien |
62 |
}
|
|
|
63 |
|
2458 |
jpm |
64 |
public function updateElement($uid,$pairs) {
|
922 |
aurelien |
65 |
// Controle detournement utilisateur
|
2458 |
jpm |
66 |
$this->controleUtilisateur($uid[0]);
|
416 |
aurelien |
67 |
|
2458 |
jpm |
68 |
if (!isset($uid[1])) {
|
922 |
aurelien |
69 |
//TODO: envoyer un message d'erreur
|
2458 |
jpm |
70 |
return;
|
922 |
aurelien |
71 |
}
|
2458 |
jpm |
72 |
|
922 |
aurelien |
73 |
$uid[1] = rtrim($uid[1],',');
|
2458 |
jpm |
74 |
|
|
|
75 |
$champs_etendus = isset($pairs['obs_etendue']) ? json_decode(stripslashes($pairs['obs_etendue']),true) : null;
|
1571 |
aurelien |
76 |
unset($pairs['obs_etendue']);
|
1628 |
raphael |
77 |
|
2458 |
jpm |
78 |
if (isset($pairs['date_observation']) && !empty($pairs['date_observation'])) {
|
1653 |
aurelien |
79 |
$time = strtotime(str_replace("/", "-", $pairs['date_observation']));
|
|
|
80 |
if(!$time || $time > time()) {
|
|
|
81 |
throw new Exception('Date invalide');
|
|
|
82 |
}
|
1628 |
raphael |
83 |
}
|
3130 |
delphine |
84 |
// un nom de bdtfxr est en fait un nom de bdtfx, de même pour les sous-référentiels d'afrique
|
2924 |
mathias |
85 |
if ($pairs['nom_referentiel'] == 'bdtfxr') {
|
|
|
86 |
$pairs['nom_referentiel'] = 'bdtfx';
|
3130 |
delphine |
87 |
} elseif (in_array($pairs['nom_referentiel'], array('apdfna', 'apdfta', 'apdfsa'))) {
|
|
|
88 |
$pairs['nom_referentiel'] == 'apd';
|
2924 |
mathias |
89 |
}
|
922 |
aurelien |
90 |
$gestionnaire_observation = new GestionObservation($this->config);
|
2458 |
jpm |
91 |
$modification_observation = $gestionnaire_observation->modifierObservation($uid[0],$uid[1],$pairs);
|
|
|
92 |
|
|
|
93 |
if ($champs_etendus != null) {
|
1548 |
aurelien |
94 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
|
|
95 |
$id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
|
1571 |
aurelien |
96 |
foreach($champs_etendus as &$champ_etendu) {
|
2458 |
jpm |
97 |
$objet_champ_etendu = new ChampEtendu();
|
|
|
98 |
$objet_champ_etendu->id = $id_obs;
|
|
|
99 |
if($this->doitGenererCleChampEtendu($champ_etendu)) {
|
|
|
100 |
$champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
|
|
|
101 |
}
|
|
|
102 |
$objet_champ_etendu->cle = $champ_etendu['cle'];
|
|
|
103 |
$objet_champ_etendu->valeur = $champ_etendu['valeur'];
|
|
|
104 |
$champ_etendu = $objet_champ_etendu;
|
|
|
105 |
}
|
|
|
106 |
// ajouterParLots modifie les champs et ajoute ceux qui existent déjà
|
|
|
107 |
$champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
|
1548 |
aurelien |
108 |
}
|
2458 |
jpm |
109 |
return true;
|
416 |
aurelien |
110 |
}
|
|
|
111 |
|
2458 |
jpm |
112 |
public function createElement($pairs) {
|
922 |
aurelien |
113 |
// Controle detournement utilisateur
|
|
|
114 |
$this->controleUtilisateur($pairs['ce_utilisateur']);
|
2458 |
jpm |
115 |
|
1590 |
aurelien |
116 |
$champs_etendus = isset($pairs['obs_etendue']) ? json_decode(stripslashes($pairs['obs_etendue']),true) : null;
|
1571 |
aurelien |
117 |
unset($pairs['obs_etendue']);
|
1628 |
raphael |
118 |
|
2458 |
jpm |
119 |
if (isset($pairs['date_observation']) && !empty($pairs['date_observation'])) {
|
1681 |
raphael |
120 |
$time = strtotime(str_replace("/", "-", $pairs['date_observation']));
|
|
|
121 |
if(!$time || $time > time()) {
|
|
|
122 |
throw new Exception('Date invalide');
|
|
|
123 |
}
|
1628 |
raphael |
124 |
}
|
416 |
aurelien |
125 |
|
2458 |
jpm |
126 |
$gestionnaire_observation = new GestionObservation($this->config);
|
|
|
127 |
$id_nouvelle_obs = $gestionnaire_observation->ajouterObservation($pairs['ce_utilisateur'], $pairs);
|
1571 |
aurelien |
128 |
|
2458 |
jpm |
129 |
if ($champs_etendus != null) {
|
|
|
130 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
|
|
131 |
foreach($champs_etendus as &$champ_etendu) {
|
|
|
132 |
$objet_champ_etendu = new ChampEtendu();
|
|
|
133 |
$objet_champ_etendu->id = $id_nouvelle_obs;
|
|
|
134 |
if($this->doitGenererCleChampEtendu($champ_etendu)) {
|
|
|
135 |
$champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
|
|
|
136 |
}
|
|
|
137 |
$objet_champ_etendu->cle = $champ_etendu['cle'];
|
|
|
138 |
$objet_champ_etendu->valeur = $champ_etendu['valeur'];
|
|
|
139 |
$champ_etendu = $objet_champ_etendu;
|
|
|
140 |
}
|
|
|
141 |
$champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
|
|
|
142 |
}
|
|
|
143 |
return true;
|
416 |
aurelien |
144 |
}
|
2458 |
jpm |
145 |
|
922 |
aurelien |
146 |
/**
|
|
|
147 |
* Supprime une observation
|
2458 |
jpm |
148 |
*
|
922 |
aurelien |
149 |
* uid[0] : utilisateur obligatoire
|
|
|
150 |
* uid[1] : ordres de l'observation à supprimer
|
2458 |
jpm |
151 |
*/
|
|
|
152 |
public function deleteElement($uid){
|
922 |
aurelien |
153 |
// Controle detournement utilisateur
|
|
|
154 |
$this->controleUtilisateur($uid[0]);
|
2458 |
jpm |
155 |
|
922 |
aurelien |
156 |
if (!isset($uid[1])) {
|
|
|
157 |
//TODO: envoyer un message d'erreur
|
2458 |
jpm |
158 |
return;
|
922 |
aurelien |
159 |
}
|
2458 |
jpm |
160 |
|
922 |
aurelien |
161 |
$uid[1] = rtrim($uid[1],',');
|
2458 |
jpm |
162 |
|
922 |
aurelien |
163 |
$gestionnaire_observation = new GestionObservation($this->config);
|
1653 |
aurelien |
164 |
$id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
|
2458 |
jpm |
165 |
$suppression_observation = $gestionnaire_observation->supprimerObservation($uid[0],$uid[1]);
|
|
|
166 |
|
1548 |
aurelien |
167 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
1571 |
aurelien |
168 |
$champs_supp = $gestion_champs_etendus->vider($id_obs);
|
2492 |
aurelien |
169 |
|
|
|
170 |
$gestionnaire_images = new GestionImage($this->config);
|
|
|
171 |
$gestionnaire_images->modifierTransmissionParObs($id_obs, false);
|
2458 |
jpm |
172 |
|
922 |
aurelien |
173 |
if ($suppression_observation) {
|
2458 |
jpm |
174 |
echo "OK";
|
738 |
aurelien |
175 |
}
|
2458 |
jpm |
176 |
exit();
|
738 |
aurelien |
177 |
}
|
2458 |
jpm |
178 |
|
|
|
179 |
private function doitGenererCleChampEtendu($champ_etendu) {
|
1939 |
aurelien |
180 |
return !isset($champ_etendu['cle']) ||
|
2458 |
jpm |
181 |
trim($champ_etendu['cle'] == "" ||
|
|
|
182 |
strpos($champ_etendu['cle'],'tempid_') !== false);
|
1939 |
aurelien |
183 |
}
|
2458 |
jpm |
184 |
}
|