Merge branch 'platal-0.10.2'
[platal.git] / include / validations / entreprises.inc.php
CommitLineData
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
24class EntrReq extends Validate
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;
4d7d27fc 39 public $address;
b814a8b8 40
543b59cc 41 public $suggestions;
b814a8b8
SJ
42
43 // }}}
44 // {{{ constructor
45
4d7d27fc 46 public function __construct(User &$_user, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0)
b814a8b8 47 {
067b6a28 48 parent::__construct($_user, false, 'entreprise', $_stamp);
543b59cc
SJ
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;
4d7d27fc 56 $this->address = $_address;
543b59cc 57
4cf8b468 58 $_name = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name)));
543b59cc
SJ
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 }
e0ee3120
SJ
71 $res = XDB::iterator('SELECT name
72 FROM profile_job_enum
73 WHERE ' . $where);
543b59cc
SJ
74 $this->suggestions = "| ";
75 while ($sug = $res->next()) {
543b59cc
SJ
76 $this->suggestions .= $sug['name'] . " | ";
77 }
b814a8b8
SJ
78 }
79
80 // }}}
81 // {{{ function formu()
82
83 public function formu()
84 {
85 return 'include/form.valid.entreprises.tpl';
86 }
87
88 // }}}
89 // {{{ function editor()
90
91 public function editor()
92 {
93 return 'include/form.valid.edit-entreprises.tpl';
94 }
95
96 // }}}
97 // {{{ function handle_editor()
98
99 protected function handle_editor()
100 {
101 if (Env::has('holdingid')) {
102 $this->holdingid = trim(Env::v('holdingid'));
103 }
104 if (Env::has('name')) {
105 $this->name = trim(Env::v('name'));
106 if (Env::has('acronym')) {
107 $this->acronym = trim(Env::v('acronym'));
108 if (Env::has('url')) {
109 $this->url = trim(Env::v('url'));
110 if (Env::has('NAF_code')) {
111 $this->NAF_code = trim(Env::v('NAF_code'));
112 if (Env::has('AX_code')) {
113 $this->AX_code = trim(Env::v('AX_code'));
114 return true;
115 }
116 }
117 }
118 }
119 }
120 return false;
121 }
122
123 // }}}
124 // {{{ function _mail_subj
125
126 protected function _mail_subj()
127 {
128 return "[Polytechnique.org/Entreprises] Demande d'ajout d'une entreprise : " . $this->name;
129 }
130
131 // }}}
132 // {{{ function _mail_body
133
134 protected function _mail_body($isok)
135 {
136 if ($isok) {
137 return " L'entreprise " . $this->name . " vient d'être ajoutée à ta fiche.";
138 } else {
139 return " La demande que tu avais faite pour l'entreprise " . $this->name .
140 " a été refusée, car elle figure déjà dans notre base.";
141 }
142 }
143
144 // }}}
145 // {{{ function commit()
146
147 public function commit()
148 {
ecad8a08
SJ
149 // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
150
b814a8b8
SJ
151 $res = XDB::query('SELECT id
152 FROM profile_job_enum
153 WHERE name = {?}',
154 $this->name);
155 if ($res->numRows() != 1) {
4d7d27fc
SJ
156 require_once 'profil.func.inc.php';
157 require_once 'geocoding.inc.php';
b814a8b8
SJ
158
159 XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
160 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
161 $this->name, $this->acronym, $this->url, $this->email,
162 $this->holdingid, $this->NAF_code, $this->AX_code);
e0ee3120 163
b814a8b8
SJ
164 $jobid = XDB::insertId();
165 $display_tel = format_display_number($this->tel, $error_tel);
4d7d27fc 166 $display_fax = format_display_number($this->fax, $error_fax);
ce0b2c6f 167 XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id, tel_type,
b814a8b8 168 search_tel, display_tel, pub)
e0ee3120
SJ
169 VALUES ({?}, 'hq', 0, 0, 'fixed', {?}, {?}, 'public'),
170 ({?}, 'hq', 0, 1, 'fax', {?}, {?}, 'public')",
171 $jobid, format_phone_number($this->tel), $display_tel,
172 $jobid, format_phone_number($this->fax), $display_fax);
173
174 $gmapsGeocoder = new GMapsGeocoder();
175 $address = $gmapsGeocoder->getGeocodedAddress($this->address);
176 Geocoder::getAreaId($address, 'administrativeArea');
177 Geocoder::getAreaId($address, 'subAdministrativeArea');
178 Geocoder::getAreaId($address, 'locality');
4d7d27fc
SJ
179 XDB::execute("INSERT INTO profile_addresses (jobid, type, id, accuracy,
180 text, postalText, postalCode, localityId,
181 subAdministrativeAreaId, administrativeAreaId,
182 countryId, latitude, longitude, updateTime,
183 north, south, east, west)
184 VALUES ({?}, 'hq', 0, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
185 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?})",
186 $jobid, $this->address['accuracy'], $this->address['text'], $this->address['postalText'],
187 $this->address['postalCode'], $this->address['localityId'],
188 $this->address['subAdministrativeAreaId'], $this->address['administrativeAreaId'],
189 $this->address['countryId'], $this->address['latitude'], $this->address['longitude'],
190 $this->address['updateTime'], $this->address['north'], $this->address['south'],
191 $this->address['east'], $this->address['west']);
b814a8b8
SJ
192 } else {
193 $jobid = $res->fetchOneCell();
194 $success = true;
195 }
196 return XDB::execute('UPDATE profile_job
197 SET jobid = {?}
ce0b2c6f
FB
198 WHERE pid = {?} AND id = {?}',
199 $jobid, $this->user->profile()->id(), $this->id);
b814a8b8
SJ
200 }
201
202 // }}}
203}
204
205// }}}
206
207// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
208?>