Subversion Repositories eFlore/Applications.cel

Rev

Rev 3166 | Rev 3283 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3092 delphine 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Service fournissant des informations sur les widgets existants et permettant d'en créer d'autres.
5
 *
6
 * Format du service :
7
 * /CelWidgetExport/format
8
 * /CelWidgetExport/csv
9
 *
10
 * Les paramêtres :
11
 *  - "start" indique le numéro du premier item à afficher
12
 *  - "limit" nombre d'items à afficher
13
 *
14
 * @internal   Mininum PHP version : 5.2
15
 * @category   CEL
16
 * @package    Services
17
 * @subpackage Widget
18
 * @version    0.1
19
 * @author     Delphine CAUQUIL <delphine@tela-botanica.org>
20
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
21
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
22
 * @copyright  1999-2017 Tela Botanica <accueil@tela-botanica.org>
23
 */
24
 
25
 
26
class CelWidgetManager extends Cel {
27
 
3193 delphine 28
	// correspondance entre les champs de l'interface et les champs de la BD (format 2018 / 2019)
3092 delphine 29
	private $parametres_autorises = array(
3193 delphine 30
			'projet' => 'project',
31
			'type' => 'type',
32
			'esttype' => 'istype',
33
			'langue' => 'language',
34
			'order' => 'order'
3092 delphine 35
	);
3193 delphine 36
 
37
	private $correspondance_config_widget= array(
38
			'projet' =>  'project',
39
			'langue' =>  'language',
40
			'titre' =>  'title',
41
			'logo' =>  'logo',
42
			'description' =>  'description',
43
			'type' =>  'type',
44
			'est_type' =>  'is_type',
45
			'style_css' =>  'css_style',
46
			'image_fond' =>  'image_font',
47
			'date_creation' =>  'date_created',
48
			'type_especes' =>  'taxo_restriction_type',
49
			'referentiel' =>  'taxo_restriction_value',
50
			'type_localisation' =>  'location_type',
51
			'localisation' =>  'location',
52
			'milieux' =>  'environments',
53
			'motscles' =>  'project_tag_name'
54
	);
55
 
56
	private $correspondance_champs_etendus = array(
57
			"key" => "field_id",
58
			"element" => "data_type",
59
			"mandatory" => "is_mandatory",
60
			"unit" => "unit"
61
	);
62
 
63
	private $correspondance_champs_etendus_trad = array(
64
			"key" => "extended_field_id",
65
			"name" => "label",
66
			"description" => "description",
67
			"fieldValues" => "default_value"
68
	);
3092 delphine 69
 
70
	public function getRessource() {
71
		return $this->getElement(array());
72
	}
73
 
74
	/**
75
	 * Méthode appelée avec une requête de type GET.
76
	 */
3193 delphine 77
	public function getElement($params = array()) {
78
		switch(@strtolower($params[0])) {
3092 delphine 79
		case 'widget':
80
			$this->getWidget();
81
			break;
3193 delphine 82
		case 'champsetendus';
83
			$this->getChampsEtendus();
84
			break;
3092 delphine 85
		default:
86
			$this->getWidget();
87
		}
88
	}
3193 delphine 89
 
90
 
3092 delphine 91
	public function createElement($requeteDonnees) {
92
		unset($requeteDonnees['signup_submit']);
3193 delphine 93
		if (isset($requeteDonnees['champs-supp'])) {
94
			$gestionchamps = new GestionChampsEtendus2($this->config);
95
			$champs = $this->traiterCorrespondanceChampsEtendusInterfaceBD($requeteDonnees); //var_dump($champs);exit;
96
			$liste = $gestionchamps->ajouterParProjet($champs);
97
			unset($requeteDonnees['champs-supp']);
98
		}
3092 delphine 99
		$requeteDonnees['est_type'] = (isset($requeteDonnees['est_type']) && $requeteDonnees['est_type']== "on") ? '1' : '0';
100
		$manager = new GestionWidget($this->config);
3193 delphine 101
		$requeteDonnees = $this->traiterCorresponceConfigWidgetInterfaceBD($requeteDonnees);
3092 delphine 102
		$retour = $manager->ajouterWidget($requeteDonnees);
3193 delphine 103
 
3092 delphine 104
		$this->envoyerJson($retour);
105
 
106
	}
3193 delphine 107
 
3121 delphine 108
	public function updateElement($uid,$params) {
109
		$retour = "";
110
		if (isset($params['projet']) && isset($params['langue'])) {
111
			$manager = new GestionWidget($this->config);
112
			$params['est_type'] = (isset($params['est_type']) && $params['est_type']== "on") ? '1' : '0';
3193 delphine 113
			$requeteDonnees = $this->traiterCorresponceConfigWidgetInterfaceBD($params);
114
			$retour = $manager->modifierWidget($params['projet'], $params['langue'], $requeteDonnees);
115
 
3121 delphine 116
		}
117
		$this->envoyerJson($retour);
118
		return "ff";
3193 delphine 119
 
3092 delphine 120
	}
121
 
122
	private function getWidget() {
3193 delphine 123
		$criteres = $this->traiterParametresAutorises($_GET);
3092 delphine 124
		$manager = new GestionWidget($this->config);
125
		$liste = $manager->obtenirWidget($criteres);
3193 delphine 126
		$liste = $this->traiterCorresponceConfigWidgetBDInterface($liste);
3092 delphine 127
 
128
		$this->envoyerJson($liste);
129
	}
3193 delphine 130
 
131
	private function getChampsEtendus() {
132
		$champs = new GestionChampsEtendus2($this->config);
133
		$projet = (isset($_GET['projet'])) ? $_GET['projet'] : "";
134
		$langue = (isset($_GET['langue'])) ? $_GET['langue'] : "fr";
135
		$liste = $champs->consulterProjetChampsEtendus($projet, $langue);
136
		$liste = $this->traiterCorrespondanceChampsEtendusBDInterface($liste);
137
 
138
		$this->envoyerJson($liste);
139
	}
3092 delphine 140
 
141
 
142
	protected function traiterParametresAutorises(Array $parametres) {
143
		$parametres_traites = array();
3193 delphine 144
 
3092 delphine 145
		foreach($parametres as $cle => $valeur) {
146
			if(is_string($valeur) && !trim($valeur)) continue;
147
			if(isset($this->parametres_autorises[$cle])) {
148
				$parametres_traites[$this->parametres_autorises[$cle]] = $valeur;
149
			}
150
		}
151
		return $parametres_traites;
152
	}
3193 delphine 153
 
154
	protected function traiterCorresponceConfigWidgetBDInterface(Array $widgets) {
155
		$parametres_traites = array();
156
		$corresp = array_flip($this->correspondance_config_widget);
157
		foreach($widgets as $id => $widget) {
158
			foreach($widget as $cle => $valeur) {
159
				if(is_string($valeur) && !trim($valeur)) continue;
160
				if(isset($corresp[$cle])) {
161
					$parametres_traites[$id][$corresp[$cle]] = $valeur;
162
				}
163
			}
164
		}
165
		return $parametres_traites;
166
	}
167
 
168
	protected function traiterCorresponceConfigWidgetInterfaceBD(Array $parametres) {
169
		$parametres_traites = array();
170
		$corresp = $this->correspondance_config_widget;
171
		foreach($parametres as $cle => $valeur) {
172
				if(is_string($valeur) && !trim($valeur)) continue;
173
				if(isset($corresp[$cle])) {
174
					$parametres_traites[$corresp[$cle]] = $valeur;
175
				}
176
			}
177
		return $parametres_traites;
178
	}
179
 
180
	protected function traiterCorrespondanceChampsEtendusInterfaceBD(Array $champsinterface) {
181
		$champsrequete = array(); $i = 0;
182
		$champssupp = json_decode($champsinterface['champs-supp'], true);
183
		// pour chaque champs-supp, mettre correspondance extended field et extended field trad
184
		foreach ($champssupp as $champ) {
185
			$champsrequete['ce'][$i]['project'] = $champsrequete['cet'][$i]['project'] = $champsinterface['projet'];
186
			$champsrequete['cet'][$i]['language_iso_code'] = $champsinterface['langue'];
187
			// pour la table extended fiels champs interface => champs bd
188
			foreach ($this->correspondance_champs_etendus as $ci => $ce) {
189
				if (isset($champ[$ci]) && $champ[$ci] != "") {
190
					$champsrequete['ce'][$i][$ce] = $champ[$ci];
191
				} else {
192
					if ($ci == "mandatory") {
193
						$champsrequete['ce'][$i][$ce] = "0";
194
					} else {
195
						$champsrequete['ce'][$i][$ce] = "";
196
					}
197
				}
198
			}
199
			// pour la table extended fiels trad champs interface => champs bd
200
			foreach ($this->correspondance_champs_etendus_trad as $cit => $cr) {
201
				if (isset($champ[$cit]) && is_array($champ[$cit])) {
202
					$champsrequete['cet'][$i][$cr] = json_encode($champ[$cit]);
203
				} elseif (isset($champ[$cit]) && $champ[$cit] != "") {
204
					$champsrequete['cet'][$i][$cr] = $champ[$cit];
205
				} else {
206
					$champsrequete['cet'][$i][$cr] = "";
207
				}
208
			}
209
			$i++;
210
		}
211
		return $champsrequete;
212
	}
213
	protected function traiterCorrespondanceChampsEtendusBDInterface(Array $champsrequete) {
214
		$champssupp = array(); $i = 0;
215
		foreach ($champsrequete as $champ) {
216
			$champssupp[$champ['project']]['projet'] = $champ['project'];
217
			$champssupp[$champ['project']]['langue'] = $champ['language_iso_code'];
218
			// pour la table extended fiels champs interface => champs bd
219
			foreach ($this->correspondance_champs_etendus as $ci => $ce) {
220
				if (isset($champ[$ce]) && $champ[$ce] != "") {
221
					$champssupp[$champ['project']]['champs-supp'][$i][$ci] = $champ[$ce];
222
				} else {
223
					$champssupp[$champ['project']]['champs-supp'][$i][$ci]= "";
224
				}
225
			}
226
			// pour la table extended fiels trad champs interface => champs bd
227
			foreach ($this->correspondance_champs_etendus_trad as $cet => $cr) {
228
				if (isset($champ[$cr]) && $champ[$cr] != "") {
229
					$champssupp[$champ['project']]['champs-supp'][$i][$cet]= $champ[$cr];
230
				} else {
231
					$champssupp[$champ['project']]['champs-supp'][$i][$cet]="";
232
				}
233
			}$i++;
234
		}
235
		return $champssupp;
236
	}
3092 delphine 237
 
238
 
239
}
3193 delphine 240
?>