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 |
}
|
2458 |
jpm |
84 |
|
922 |
aurelien |
85 |
$gestionnaire_observation = new GestionObservation($this->config);
|
2458 |
jpm |
86 |
$modification_observation = $gestionnaire_observation->modifierObservation($uid[0],$uid[1],$pairs);
|
|
|
87 |
|
|
|
88 |
if ($champs_etendus != null) {
|
1548 |
aurelien |
89 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
|
|
90 |
$id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
|
1571 |
aurelien |
91 |
foreach($champs_etendus as &$champ_etendu) {
|
2458 |
jpm |
92 |
$objet_champ_etendu = new ChampEtendu();
|
|
|
93 |
$objet_champ_etendu->id = $id_obs;
|
|
|
94 |
if($this->doitGenererCleChampEtendu($champ_etendu)) {
|
|
|
95 |
$champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
|
|
|
96 |
}
|
|
|
97 |
$objet_champ_etendu->cle = $champ_etendu['cle'];
|
|
|
98 |
$objet_champ_etendu->valeur = $champ_etendu['valeur'];
|
|
|
99 |
$champ_etendu = $objet_champ_etendu;
|
|
|
100 |
}
|
|
|
101 |
// ajouterParLots modifie les champs et ajoute ceux qui existent déjà
|
|
|
102 |
$champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
|
1548 |
aurelien |
103 |
}
|
2458 |
jpm |
104 |
return true;
|
416 |
aurelien |
105 |
}
|
|
|
106 |
|
2458 |
jpm |
107 |
public function createElement($pairs) {
|
922 |
aurelien |
108 |
// Controle detournement utilisateur
|
|
|
109 |
$this->controleUtilisateur($pairs['ce_utilisateur']);
|
2458 |
jpm |
110 |
|
1590 |
aurelien |
111 |
$champs_etendus = isset($pairs['obs_etendue']) ? json_decode(stripslashes($pairs['obs_etendue']),true) : null;
|
1571 |
aurelien |
112 |
unset($pairs['obs_etendue']);
|
1628 |
raphael |
113 |
|
2458 |
jpm |
114 |
if (isset($pairs['date_observation']) && !empty($pairs['date_observation'])) {
|
1681 |
raphael |
115 |
$time = strtotime(str_replace("/", "-", $pairs['date_observation']));
|
|
|
116 |
if(!$time || $time > time()) {
|
|
|
117 |
throw new Exception('Date invalide');
|
|
|
118 |
}
|
1628 |
raphael |
119 |
}
|
416 |
aurelien |
120 |
|
2458 |
jpm |
121 |
$gestionnaire_observation = new GestionObservation($this->config);
|
|
|
122 |
$id_nouvelle_obs = $gestionnaire_observation->ajouterObservation($pairs['ce_utilisateur'], $pairs);
|
1571 |
aurelien |
123 |
|
2458 |
jpm |
124 |
if ($champs_etendus != null) {
|
|
|
125 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
|
|
126 |
foreach($champs_etendus as &$champ_etendu) {
|
|
|
127 |
$objet_champ_etendu = new ChampEtendu();
|
|
|
128 |
$objet_champ_etendu->id = $id_nouvelle_obs;
|
|
|
129 |
if($this->doitGenererCleChampEtendu($champ_etendu)) {
|
|
|
130 |
$champ_etendu['cle'] = $gestion_champs_etendus->transformerLabelEnCle($champ_etendu['label']);
|
|
|
131 |
}
|
|
|
132 |
$objet_champ_etendu->cle = $champ_etendu['cle'];
|
|
|
133 |
$objet_champ_etendu->valeur = $champ_etendu['valeur'];
|
|
|
134 |
$champ_etendu = $objet_champ_etendu;
|
|
|
135 |
}
|
|
|
136 |
$champs_supp = $gestion_champs_etendus->ajouterParLots($champs_etendus);
|
|
|
137 |
}
|
|
|
138 |
return true;
|
416 |
aurelien |
139 |
}
|
2458 |
jpm |
140 |
|
922 |
aurelien |
141 |
/**
|
|
|
142 |
* Supprime une observation
|
2458 |
jpm |
143 |
*
|
922 |
aurelien |
144 |
* uid[0] : utilisateur obligatoire
|
|
|
145 |
* uid[1] : ordres de l'observation à supprimer
|
2458 |
jpm |
146 |
*/
|
|
|
147 |
public function deleteElement($uid){
|
922 |
aurelien |
148 |
// Controle detournement utilisateur
|
|
|
149 |
$this->controleUtilisateur($uid[0]);
|
2458 |
jpm |
150 |
|
922 |
aurelien |
151 |
if (!isset($uid[1])) {
|
|
|
152 |
//TODO: envoyer un message d'erreur
|
2458 |
jpm |
153 |
return;
|
922 |
aurelien |
154 |
}
|
2458 |
jpm |
155 |
|
922 |
aurelien |
156 |
$uid[1] = rtrim($uid[1],',');
|
2458 |
jpm |
157 |
|
922 |
aurelien |
158 |
$gestionnaire_observation = new GestionObservation($this->config);
|
1653 |
aurelien |
159 |
$id_obs = $gestionnaire_observation->renvoyerIdPourOrdre($uid[0],$uid[1]);
|
2458 |
jpm |
160 |
$suppression_observation = $gestionnaire_observation->supprimerObservation($uid[0],$uid[1]);
|
|
|
161 |
|
1548 |
aurelien |
162 |
$gestion_champs_etendus = new GestionChampsEtendus($this->config, 'obs');
|
1571 |
aurelien |
163 |
$champs_supp = $gestion_champs_etendus->vider($id_obs);
|
2492 |
aurelien |
164 |
|
|
|
165 |
$gestionnaire_images = new GestionImage($this->config);
|
|
|
166 |
$gestionnaire_images->modifierTransmissionParObs($id_obs, false);
|
2458 |
jpm |
167 |
|
922 |
aurelien |
168 |
if ($suppression_observation) {
|
2458 |
jpm |
169 |
echo "OK";
|
738 |
aurelien |
170 |
}
|
2458 |
jpm |
171 |
exit();
|
738 |
aurelien |
172 |
}
|
2458 |
jpm |
173 |
|
|
|
174 |
private function doitGenererCleChampEtendu($champ_etendu) {
|
1939 |
aurelien |
175 |
return !isset($champ_etendu['cle']) ||
|
2458 |
jpm |
176 |
trim($champ_etendu['cle'] == "" ||
|
|
|
177 |
strpos($champ_etendu['cle'],'tempid_') !== false);
|
1939 |
aurelien |
178 |
}
|
2458 |
jpm |
179 |
}
|