da2e65ee68c5fd180c9bec27dfc9db707e316d49
[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 = null;
31 public $url = null;
32 public $email = null;
33 public $holdingid = null;
34 public $NAF_code = null;
35 public $AX_code = null;
36
37 public $tel = null;
38 public $fax = null;
39 public $address = null;
40
41 public $suggestions;
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.';
43
44 // }}}
45 // {{{ constructor
46
47 public function __construct(User &$_user, Profile &$_profile, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0)
48 {
49 parent::__construct($_user, $_profile, false, 'entreprise', $_stamp);
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;
57 $this->address = $_address;
58
59 $_name = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name)));
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 }
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 }
80 }
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 {
104 foreach (array('name', 'acronym', 'url', 'email', 'NAF_code', 'tel', 'fax', 'address') as $field) {
105 $this->$field = (Env::t($field) == '' ? null : Env::t($field));
106 }
107 foreach (array('AX_code', 'holdingid') as $field) {
108 $this->$field = (Env::i($field) == 0 ? null : Env::i($field));
109 }
110
111 return true;
112 }
113
114 // }}}
115 // {{{ function _mail_subj
116
117 protected function _mail_subj()
118 {
119 return '[Polytechnique.org/Entreprises] Demande d\'ajout d\'une entreprise';
120 }
121
122 // }}}
123 // {{{ function _mail_body
124
125 protected function _mail_body($isok)
126 {
127 if ($isok) {
128 return " L'entreprise " . $this->name . " vient d'être ajoutée à ta fiche.";
129 } else {
130 return " La demande que tu avais faite pour l'entreprise " . $this->name .
131 " a été refusée, car elle figure déjà dans notre base.";
132 }
133 }
134
135 // }}}
136 // {{{ function commit()
137
138 public function commit()
139 {
140 $res = XDB::query('SELECT id
141 FROM profile_job_enum
142 WHERE name = {?}',
143 $this->name);
144 if ($res->numRows() != 1) {
145 XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
146 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
147 $this->name, $this->acronym, $this->url, $this->email,
148 $this->holdingid, $this->NAF_code, $this->AX_code);
149
150 $jobid = XDB::insertId();
151 $phone = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 0,
152 'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public'));
153 $fax = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1,
154 'type' => 'fax', 'display' => $this->fax, 'pub' => 'public'));
155 $address = new Address(array('jobid' => $jobid, 'type' => Address::LINK_COMPANY, 'text' => $this->address));
156 $phone->save();
157 $fax->save();
158 $address->save();
159 } else {
160 $jobid = $res->fetchOneCell();
161 }
162
163 XDB::execute('UPDATE profile_job
164 SET jobid = {?}
165 WHERE pid = {?} AND id = {?}',
166 $jobid, $this->profile->id(), $this->id);
167 if (XDB::affectedRows() == 0) {
168 return XDB::execute('INSERT INTO profile_job (jobid, pid, id)
169 VALUES ({?}, {?}, {?})',
170 $jobid, $this->profile->id(), $this->id);
171 }
172 return true;
173 }
174
175 // }}}
176 }
177
178 // }}}
179
180 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
181 ?>