Commit | Line | Data |
---|---|---|
b814a8b8 SJ |
1 | <?php |
2 | /*************************************************************************** | |
d4c08d89 | 3 | * Copyright (C) 2003-2010 Polytechnique.org * |
b814a8b8 SJ |
4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | // {{{ class EntrReq | |
23 | ||
c3d1e6b6 | 24 | class EntrReq extends ProfileValidate |
b814a8b8 SJ |
25 | { |
26 | // {{{ properties | |
27 | ||
28 | public $id; | |
29 | public $name; | |
30 | public $acronym; | |
31 | public $url; | |
32 | public $email; | |
8768e5af | 33 | public $holdingid = null; |
c67b874f | 34 | public $NAF_code = null; |
8768e5af | 35 | public $AX_code = null; |
b814a8b8 SJ |
36 | |
37 | public $tel; | |
38 | public $fax; | |
4d7d27fc | 39 | public $address; |
b814a8b8 | 40 | |
543b59cc | 41 | public $suggestions; |
bdc3c3d2 | 42 | public $rules = 'Si l\'entreprise est déjà présente sous un autre nom dans la liste des suggestions, remplacer son nom par celui-ci avant de valider. Laisser les autres champs tels quels.'; |
b814a8b8 SJ |
43 | |
44 | // }}} | |
45 | // {{{ constructor | |
46 | ||
c3d1e6b6 | 47 | public function __construct(User &$_user, Profile &$_profile, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0) |
b814a8b8 | 48 | { |
c3d1e6b6 | 49 | parent::__construct($_user, $_profile, false, 'entreprise', $_stamp); |
543b59cc SJ |
50 | $this->id = $_id; |
51 | $this->name = $_name; | |
52 | $this->acronym = $_acronym; | |
53 | $this->url = $_url; | |
54 | $this->email = $_email; | |
55 | $this->tel = $_tel; | |
56 | $this->fax = $_fax; | |
4d7d27fc | 57 | $this->address = $_address; |
543b59cc | 58 | |
4cf8b468 | 59 | $_name = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name))); |
543b59cc SJ |
60 | $name = explode(" ", $_name); |
61 | $name_array = array_map("trim", $name); | |
62 | $length = count($name_array); | |
63 | $where = ""; | |
64 | for ($i = 0; $i < $length; $i++) { | |
65 | if (strlen($name_array[$i]) > 2) { | |
66 | if ($where !== "") { | |
67 | $where .= " OR "; | |
68 | } | |
69 | $where .= "name LIKE '%" . $name_array[$i] . "%'"; | |
70 | } | |
71 | } | |
8cc857c7 SJ |
72 | if ($where != '') { |
73 | $res = XDB::iterator('SELECT name | |
74 | FROM profile_job_enum | |
75 | WHERE ' . $where); | |
76 | $this->suggestions = "| "; | |
77 | while ($sug = $res->next()) { | |
78 | $this->suggestions .= $sug['name'] . " | "; | |
79 | } | |
543b59cc | 80 | } |
b814a8b8 SJ |
81 | } |
82 | ||
83 | // }}} | |
84 | // {{{ function formu() | |
85 | ||
86 | public function formu() | |
87 | { | |
88 | return 'include/form.valid.entreprises.tpl'; | |
89 | } | |
90 | ||
91 | // }}} | |
92 | // {{{ function editor() | |
93 | ||
94 | public function editor() | |
95 | { | |
96 | return 'include/form.valid.edit-entreprises.tpl'; | |
97 | } | |
98 | ||
99 | // }}} | |
100 | // {{{ function handle_editor() | |
101 | ||
102 | protected function handle_editor() | |
103 | { | |
b814a8b8 | 104 | if (Env::has('name')) { |
c3d1e6b6 | 105 | $this->name = Env::t('name'); |
b814a8b8 | 106 | } |
36270bc2 SJ |
107 | if (Env::has('acronym')) { |
108 | $this->acronym = Env::t('acronym'); | |
109 | } | |
110 | if (Env::has('url')) { | |
111 | $this->url = Env::t('url'); | |
112 | } | |
113 | if (Env::has('email')) { | |
114 | $this->email = Env::t('email'); | |
115 | } | |
116 | if (Env::has('holdingid')) { | |
117 | $this->holdingid = Env::i('holdingid'); | |
118 | } | |
119 | if (Env::has('NAF_code')) { | |
120 | $this->NAF_code = Env::t('NAF_code'); | |
121 | } | |
122 | if (Env::has('AX_code')) { | |
123 | $this->AX_code = Env::i('AX_code'); | |
124 | } | |
125 | if (Env::has('address')) { | |
126 | $this->address['text'] = Env::t('address'); | |
127 | } | |
128 | if (Env::has('tel')) { | |
129 | $this->tel = Env::t('tel'); | |
130 | } | |
131 | if (Env::has('fax')) { | |
132 | $this->fax = Env::t('fax'); | |
133 | } | |
134 | return true; | |
b814a8b8 SJ |
135 | } |
136 | ||
137 | // }}} | |
138 | // {{{ function _mail_subj | |
139 | ||
140 | protected function _mail_subj() | |
141 | { | |
2381efd0 | 142 | return '[Polytechnique.org/Entreprises] Demande d\'ajout d\'une entreprise'; |
b814a8b8 SJ |
143 | } |
144 | ||
145 | // }}} | |
146 | // {{{ function _mail_body | |
147 | ||
148 | protected function _mail_body($isok) | |
149 | { | |
150 | if ($isok) { | |
151 | return " L'entreprise " . $this->name . " vient d'être ajoutée à ta fiche."; | |
152 | } else { | |
153 | return " La demande que tu avais faite pour l'entreprise " . $this->name . | |
154 | " a été refusée, car elle figure déjà dans notre base."; | |
155 | } | |
156 | } | |
157 | ||
158 | // }}} | |
159 | // {{{ function commit() | |
160 | ||
161 | public function commit() | |
162 | { | |
0b6c8b36 | 163 | // TODO: use address update profile_job_enum once it is done. |
ecad8a08 | 164 | |
b814a8b8 SJ |
165 | $res = XDB::query('SELECT id |
166 | FROM profile_job_enum | |
167 | WHERE name = {?}', | |
168 | $this->name); | |
169 | if ($res->numRows() != 1) { | |
4d7d27fc | 170 | require_once 'geocoding.inc.php'; |
b814a8b8 SJ |
171 | |
172 | XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code) | |
173 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})', | |
174 | $this->name, $this->acronym, $this->url, $this->email, | |
175 | $this->holdingid, $this->NAF_code, $this->AX_code); | |
e0ee3120 | 176 | |
b814a8b8 | 177 | $jobid = XDB::insertId(); |
0b6c8b36 SJ |
178 | $phone = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 0, |
179 | 'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public')); | |
180 | $fax = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1, | |
181 | 'type' => 'fax', 'display' => $this->fax, 'pub' => 'public')); | |
182 | $phone->save(); | |
183 | $fax->save(); | |
e0ee3120 SJ |
184 | |
185 | $gmapsGeocoder = new GMapsGeocoder(); | |
186 | $address = $gmapsGeocoder->getGeocodedAddress($this->address); | |
187 | Geocoder::getAreaId($address, 'administrativeArea'); | |
188 | Geocoder::getAreaId($address, 'subAdministrativeArea'); | |
189 | Geocoder::getAreaId($address, 'locality'); | |
4d7d27fc SJ |
190 | XDB::execute("INSERT INTO profile_addresses (jobid, type, id, accuracy, |
191 | text, postalText, postalCode, localityId, | |
192 | subAdministrativeAreaId, administrativeAreaId, | |
193 | countryId, latitude, longitude, updateTime, | |
194 | north, south, east, west) | |
195 | VALUES ({?}, 'hq', 0, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, | |
196 | {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?})", | |
197 | $jobid, $this->address['accuracy'], $this->address['text'], $this->address['postalText'], | |
198 | $this->address['postalCode'], $this->address['localityId'], | |
199 | $this->address['subAdministrativeAreaId'], $this->address['administrativeAreaId'], | |
200 | $this->address['countryId'], $this->address['latitude'], $this->address['longitude'], | |
201 | $this->address['updateTime'], $this->address['north'], $this->address['south'], | |
202 | $this->address['east'], $this->address['west']); | |
b814a8b8 SJ |
203 | } else { |
204 | $jobid = $res->fetchOneCell(); | |
b814a8b8 | 205 | } |
a38c0777 SJ |
206 | XDB::execute('UPDATE profile_job |
207 | SET jobid = {?} | |
208 | WHERE pid = {?} AND id = {?}', | |
209 | $jobid, $this->profile->id(), $this->id); | |
210 | if (XDB::affectedRows() == 0) { | |
211 | return XDB::execute('INSERT INTO profile_job (jobid, pid, id) | |
212 | VALUES ({?}, {?}, {?})', | |
213 | $jobid, $this->profile->id(), $this->id); | |
214 | } | |
215 | return true; | |
b814a8b8 SJ |
216 | } |
217 | ||
218 | // }}} | |
219 | } | |
220 | ||
221 | // }}} | |
222 | ||
223 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
224 | ?> |