Phone class: new class to access profile_phones.
[platal.git] / include / validations / entreprises.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
24 class EntrReq extends ProfileValidate
25 {
26 // {{{ properties
27
28 public $id;
29 public $name;
30 public $acronym;
31 public $url;
32 public $email;
33 public $holdingid;
34 public $NAF_code;
35 public $AX_code;
36
37 public $tel;
38 public $fax;
39 public $address;
40
41 public $suggestions;
42
43 // }}}
44 // {{{ constructor
45
46 public function __construct(User &$_user, Profile &$_profile, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0)
47 {
48 parent::__construct($_user, $_profile, false, 'entreprise', $_stamp);
49 $this->id = $_id;
50 $this->name = $_name;
51 $this->acronym = $_acronym;
52 $this->url = $_url;
53 $this->email = $_email;
54 $this->tel = $_tel;
55 $this->fax = $_fax;
56 $this->address = $_address;
57
58 $_name = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name)));
59 $name = explode(" ", $_name);
60 $name_array = array_map("trim", $name);
61 $length = count($name_array);
62 $where = "";
63 for ($i = 0; $i < $length; $i++) {
64 if (strlen($name_array[$i]) > 2) {
65 if ($where !== "") {
66 $where .= " OR ";
67 }
68 $where .= "name LIKE '%" . $name_array[$i] . "%'";
69 }
70 }
71 if ($where != '') {
72 $res = XDB::iterator('SELECT name
73 FROM profile_job_enum
74 WHERE ' . $where);
75 $this->suggestions = "| ";
76 while ($sug = $res->next()) {
77 $this->suggestions .= $sug['name'] . " | ";
78 }
79 }
80 }
81
82 // }}}
83 // {{{ function formu()
84
85 public function formu()
86 {
87 return 'include/form.valid.entreprises.tpl';
88 }
89
90 // }}}
91 // {{{ function editor()
92
93 public function editor()
94 {
95 return 'include/form.valid.edit-entreprises.tpl';
96 }
97
98 // }}}
99 // {{{ function handle_editor()
100
101 protected function handle_editor()
102 {
103 if (Env::has('name')) {
104 $this->name = Env::t('name');
105 }
106 if (Env::has('acronym')) {
107 $this->acronym = Env::t('acronym');
108 }
109 if (Env::has('url')) {
110 $this->url = Env::t('url');
111 }
112 if (Env::has('email')) {
113 $this->email = Env::t('email');
114 }
115 if (Env::has('holdingid')) {
116 $this->holdingid = Env::i('holdingid');
117 }
118 if (Env::has('NAF_code')) {
119 $this->NAF_code = Env::t('NAF_code');
120 }
121 if (Env::has('AX_code')) {
122 $this->AX_code = Env::i('AX_code');
123 }
124 if (Env::has('address')) {
125 $this->address['text'] = Env::t('address');
126 }
127 if (Env::has('tel')) {
128 $this->tel = Env::t('tel');
129 }
130 if (Env::has('fax')) {
131 $this->fax = Env::t('fax');
132 }
133 return true;
134 }
135
136 // }}}
137 // {{{ function _mail_subj
138
139 protected function _mail_subj()
140 {
141 return "[Polytechnique.org/Entreprises] Demande d'ajout d'une entreprise : " . $this->name;
142 }
143
144 // }}}
145 // {{{ function _mail_body
146
147 protected function _mail_body($isok)
148 {
149 if ($isok) {
150 return " L'entreprise " . $this->name . " vient d'être ajoutée à ta fiche.";
151 } else {
152 return " La demande que tu avais faite pour l'entreprise " . $this->name .
153 " a été refusée, car elle figure déjà dans notre base.";
154 }
155 }
156
157 // }}}
158 // {{{ function commit()
159
160 public function commit()
161 {
162 // TODO: use address update profile_job_enum once it is done.
163
164 $res = XDB::query('SELECT id
165 FROM profile_job_enum
166 WHERE name = {?}',
167 $this->name);
168 if ($res->numRows() != 1) {
169 require_once 'geocoding.inc.php';
170
171 XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
172 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
173 $this->name, $this->acronym, $this->url, $this->email,
174 $this->holdingid, $this->NAF_code, $this->AX_code);
175
176 $jobid = XDB::insertId();
177 $phone = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 0,
178 'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public'));
179 $fax = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1,
180 'type' => 'fax', 'display' => $this->fax, 'pub' => 'public'));
181 $phone->save();
182 $fax->save();
183
184 $gmapsGeocoder = new GMapsGeocoder();
185 $address = $gmapsGeocoder->getGeocodedAddress($this->address);
186 Geocoder::getAreaId($address, 'administrativeArea');
187 Geocoder::getAreaId($address, 'subAdministrativeArea');
188 Geocoder::getAreaId($address, 'locality');
189 XDB::execute("INSERT INTO profile_addresses (jobid, type, id, accuracy,
190 text, postalText, postalCode, localityId,
191 subAdministrativeAreaId, administrativeAreaId,
192 countryId, latitude, longitude, updateTime,
193 north, south, east, west)
194 VALUES ({?}, 'hq', 0, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
195 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?})",
196 $jobid, $this->address['accuracy'], $this->address['text'], $this->address['postalText'],
197 $this->address['postalCode'], $this->address['localityId'],
198 $this->address['subAdministrativeAreaId'], $this->address['administrativeAreaId'],
199 $this->address['countryId'], $this->address['latitude'], $this->address['longitude'],
200 $this->address['updateTime'], $this->address['north'], $this->address['south'],
201 $this->address['east'], $this->address['west']);
202 } else {
203 $jobid = $res->fetchOneCell();
204 }
205 XDB::execute('UPDATE profile_job
206 SET jobid = {?}
207 WHERE pid = {?} AND id = {?}',
208 $jobid, $this->profile->id(), $this->id);
209 if (XDB::affectedRows() == 0) {
210 return XDB::execute('INSERT INTO profile_job (jobid, pid, id)
211 VALUES ({?}, {?}, {?})',
212 $jobid, $this->profile->id(), $this->id);
213 }
214 return true;
215 }
216
217 // }}}
218 }
219
220 // }}}
221
222 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
223 ?>