06af35b13702077bf1b9093cfa4946fa60915aed
[platal.git] / include / validations / entreprises.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 public $requireAdmin = false;
45
46 // }}}
47 // {{{ constructor
48
49 public function __construct(User $_user, Profile $_profile, $_id, $_name, $_acronym, $_url, $_email, $_tel, $_fax, $_address, $_stamp = 0)
50 {
51 parent::__construct($_user, $_profile, false, 'entreprise', $_stamp);
52 $this->id = $_id;
53 $this->name = $_name;
54 $this->acronym = $_acronym;
55 $this->url = $_url;
56 $this->email = $_email;
57 $this->tel = $_tel;
58 $this->fax = $_fax;
59 $this->address = $_address;
60
61 $_name = preg_replace('/[^0-9a-z]/i', ' ', strtolower(replace_accent($_name)));
62 $name = explode(" ", $_name);
63 $name_array = array_map("trim", $name);
64 $length = count($name_array);
65 $where = "";
66 for ($i = 0; $i < $length; $i++) {
67 if (strlen($name_array[$i]) > 2) {
68 if ($where !== "") {
69 $where .= " OR ";
70 }
71 $where .= "name LIKE '%" . $name_array[$i] . "%'";
72 }
73 }
74 if ($where != '') {
75 $res = XDB::iterator('SELECT name
76 FROM profile_job_enum
77 WHERE ' . $where);
78 $this->suggestions = "| ";
79 while ($sug = $res->next()) {
80 $this->suggestions .= $sug['name'] . " | ";
81 }
82 }
83 }
84
85 // }}}
86 // {{{ function formu()
87
88 public function formu()
89 {
90 return 'include/form.valid.entreprises.tpl';
91 }
92
93 // }}}
94 // {{{ function editor()
95
96 public function editor()
97 {
98 return 'include/form.valid.edit-entreprises.tpl';
99 }
100
101 // }}}
102 // {{{ function handle_editor()
103
104 protected function handle_editor()
105 {
106 foreach (array('name', 'acronym', 'url', 'email', 'NAF_code', 'tel', 'fax', 'address') as $field) {
107 $this->$field = (Env::t($field) == '' ? null : Env::t($field));
108 }
109 foreach (array('AX_code', 'holdingid') as $field) {
110 $this->$field = (Env::i($field) == 0 ? null : Env::i($field));
111 }
112
113 return true;
114 }
115
116 // }}}
117 // {{{ function _mail_subj
118
119 protected function _mail_subj()
120 {
121 return '[Polytechnique.org/Entreprises] Demande d\'ajout d\'une entreprise';
122 }
123
124 // }}}
125 // {{{ function _mail_body
126
127 protected function _mail_body($isok)
128 {
129 if ($isok) {
130 return " L'entreprise " . $this->name . " vient d'être ajoutée à ta fiche.";
131 } else {
132 return ' La demande que tu avais faite pour l\'entreprise ' . $this->name . ' a été refusée.';
133 }
134 }
135
136 // }}}
137 // {{{ function commit()
138
139 public function commit()
140 {
141 $res = XDB::query('SELECT id
142 FROM profile_job_enum
143 WHERE name = {?}',
144 $this->name);
145 if ($res->numRows() != 1) {
146 XDB::execute('INSERT INTO profile_job_enum (name, acronym, url, email, holdingid, NAF_code, AX_code)
147 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?})',
148 $this->name, $this->acronym, $this->url, $this->email,
149 $this->holdingid, $this->NAF_code, $this->AX_code);
150
151 $jobid = XDB::insertId();
152 $phone = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 0,
153 'type' => 'fixed', 'display' => $this->tel, 'pub' => 'public'));
154 $fax = new Phone(array('link_type' => 'hq', 'link_id' => $jobid, 'id' => 1,
155 'type' => 'fax', 'display' => $this->fax, 'pub' => 'public'));
156 $address = new Address(array('jobid' => $jobid, 'type' => Address::LINK_COMPANY, 'text' => $this->address));
157 $phone->save();
158 $fax->save();
159 $address->save();
160 } else {
161 $jobid = $res->fetchOneCell();
162 }
163
164 XDB::execute('UPDATE profile_job
165 SET jobid = {?}
166 WHERE pid = {?} AND id = {?}',
167 $jobid, $this->profile->id(), $this->id);
168 if (XDB::affectedRows() == 0) {
169 return XDB::execute('INSERT INTO profile_job (jobid, pid, id)
170 VALUES ({?}, {?}, {?})',
171 $jobid, $this->profile->id(), $this->id);
172 }
173 return true;
174 }
175
176 // }}}
177 }
178
179 // }}}
180
181 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
182 ?>