Total Complexity | 256 |
Total Lines | 1401 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Contact often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Contact, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Contact extends CommonObject |
||
39 | { |
||
40 | /** |
||
41 | * @var string ID to identify managed object |
||
42 | */ |
||
43 | public $element='contact'; |
||
44 | |||
45 | /** |
||
46 | * @var string Name of table without prefix where object is stored |
||
47 | */ |
||
48 | public $table_element='socpeople'; |
||
49 | |||
50 | /** |
||
51 | * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
52 | * @var int |
||
53 | */ |
||
54 | public $ismultientitymanaged = 1; |
||
55 | |||
56 | /** |
||
57 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
58 | */ |
||
59 | public $picto = 'contact'; |
||
60 | |||
61 | |||
62 | // BEGIN MODULEBUILDER PROPERTIES |
||
63 | /** |
||
64 | * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
65 | */ |
||
66 | public $fields=array( |
||
67 | 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'index'=>1, 'position'=>1, 'comment'=>'Id'), |
||
68 | 'lastname' =>array('type'=>'varchar(128)', 'label'=>'Name', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>10, 'searchall'=>1), |
||
69 | 'firstname' =>array('type'=>'varchar(128)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'showoncombobox'=>1, 'index'=>1, 'position'=>11, 'searchall'=>1), |
||
70 | 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'default'=>1, 'notnull'=>1, 'index'=>1, 'position'=>20), |
||
71 | 'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>60), |
||
72 | 'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>61), |
||
73 | 'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>500), |
||
74 | 'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>501), |
||
75 | //'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502), |
||
76 | 'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>510), |
||
77 | 'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'position'=>511), |
||
78 | //'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512), |
||
79 | 'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'notnull'=>-1, 'index'=>1, 'position'=>1000), |
||
80 | ); |
||
81 | |||
82 | public $civility_id; // In fact we store civility_code |
||
83 | public $civility_code; |
||
84 | public $address; |
||
85 | public $zip; |
||
86 | public $town; |
||
87 | |||
88 | /** |
||
89 | * @deprecated |
||
90 | * @see $state_id |
||
91 | */ |
||
92 | public $fk_departement; |
||
93 | /** |
||
94 | * @deprecated |
||
95 | * @see $state_code |
||
96 | */ |
||
97 | public $departement_code; |
||
98 | /** |
||
99 | * @deprecated |
||
100 | * @see $state |
||
101 | */ |
||
102 | public $departement; |
||
103 | public $state_id; // Id of department |
||
104 | public $state_code; // Code of department |
||
105 | public $state; // Label of department |
||
106 | |||
107 | public $poste; // Position |
||
108 | |||
109 | public $socid; // fk_soc |
||
110 | public $statut; // 0=inactif, 1=actif |
||
111 | |||
112 | public $code; |
||
113 | public $email; |
||
114 | public $skype; |
||
115 | public $photo; |
||
116 | public $jabberid; |
||
117 | public $phone_pro; |
||
118 | public $phone_perso; |
||
119 | public $phone_mobile; |
||
120 | public $fax; |
||
121 | |||
122 | public $priv; |
||
123 | |||
124 | public $birthday; |
||
125 | public $default_lang; |
||
126 | |||
127 | public $ref_facturation; // Reference number of invoice for which it is contact |
||
128 | public $ref_contrat; // Nb de reference contrat pour lequel il est contact |
||
129 | public $ref_commande; // Nb de reference commande pour lequel il est contact |
||
130 | public $ref_propal; // Nb de reference propal pour lequel il est contact |
||
131 | |||
132 | public $user_id; |
||
133 | public $user_login; |
||
134 | |||
135 | // END MODULEBUILDER PROPERTIES |
||
136 | |||
137 | |||
138 | public $oldcopy; // To contains a clone of this when we need to save old properties of object |
||
139 | |||
140 | |||
141 | /** |
||
142 | * Constructor |
||
143 | * |
||
144 | * @param DoliDB $db Database handler |
||
145 | */ |
||
146 | function __construct($db) |
||
147 | { |
||
148 | $this->db = $db; |
||
149 | $this->statut = 1; // By default, status is enabled |
||
150 | } |
||
151 | |||
152 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
153 | /** |
||
154 | * Load indicators into this->nb for board |
||
155 | * |
||
156 | * @return int <0 if KO, >0 if OK |
||
157 | */ |
||
158 | function load_state_board() |
||
159 | { |
||
160 | // phpcs:enable |
||
161 | global $user; |
||
162 | |||
163 | $this->nb=array(); |
||
164 | $clause = "WHERE"; |
||
165 | |||
166 | $sql = "SELECT count(sp.rowid) as nb"; |
||
167 | $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as sp"; |
||
168 | if (!$user->rights->societe->client->voir && !$user->societe_id) |
||
169 | { |
||
170 | $sql.= ", ".MAIN_DB_PREFIX."societe as s"; |
||
171 | $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; |
||
172 | $sql.= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id; |
||
173 | $clause = "AND"; |
||
174 | } |
||
175 | $sql.= ' '.$clause.' sp.entity IN ('.getEntity($this->element).')'; |
||
176 | $sql.= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat=".$user->id."))"; |
||
177 | if ($user->societe_id > 0) $sql.=" AND sp.fk_soc = ".$user->societe_id; |
||
178 | |||
179 | $resql=$this->db->query($sql); |
||
180 | if ($resql) |
||
181 | { |
||
182 | while ($obj=$this->db->fetch_object($resql)) |
||
183 | { |
||
184 | $this->nb["contacts"]=$obj->nb; |
||
185 | } |
||
186 | $this->db->free($resql); |
||
187 | return 1; |
||
188 | } |
||
189 | else |
||
190 | { |
||
191 | dol_print_error($this->db); |
||
192 | $this->error=$this->db->lasterror(); |
||
193 | return -1; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Add a contact into database |
||
199 | * |
||
200 | * @param User $user Object user that create |
||
201 | * @return int <0 if KO, >0 if OK |
||
202 | */ |
||
203 | function create($user) |
||
204 | { |
||
205 | global $conf, $langs; |
||
206 | |||
207 | $error=0; |
||
208 | $now=dol_now(); |
||
209 | |||
210 | $this->db->begin(); |
||
211 | |||
212 | // Clean parameters |
||
213 | $this->lastname=$this->lastname?trim($this->lastname):trim($this->name); |
||
214 | $this->firstname=trim($this->firstname); |
||
215 | if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname=ucwords($this->lastname); |
||
216 | if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname=ucwords($this->firstname); |
||
217 | if (empty($this->socid)) $this->socid = 0; |
||
218 | if (empty($this->priv)) $this->priv = 0; |
||
219 | if (empty($this->statut)) $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request |
||
220 | |||
221 | $this->entity = ((isset($this->entity) && is_numeric($this->entity))?$this->entity:$conf->entity); |
||
222 | |||
223 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople ("; |
||
224 | $sql.= " datec"; |
||
225 | $sql.= ", fk_soc"; |
||
226 | $sql.= ", lastname"; |
||
227 | $sql.= ", firstname"; |
||
228 | $sql.= ", fk_user_creat"; |
||
229 | $sql.= ", priv"; |
||
230 | $sql.= ", statut"; |
||
231 | $sql.= ", canvas"; |
||
232 | $sql.= ", entity"; |
||
233 | $sql.= ", ref_ext"; |
||
234 | $sql.= ", import_key"; |
||
235 | $sql.= ") VALUES ("; |
||
236 | $sql.= "'".$this->db->idate($now)."',"; |
||
237 | if ($this->socid > 0) $sql.= " ".$this->db->escape($this->socid).","; |
||
238 | else $sql.= "null,"; |
||
239 | $sql.= "'".$this->db->escape($this->lastname)."',"; |
||
240 | $sql.= "'".$this->db->escape($this->firstname)."',"; |
||
241 | $sql.= " ".($user->id > 0 ? "'".$this->db->escape($user->id)."'":"null").","; |
||
242 | $sql.= " ".$this->db->escape($this->priv).","; |
||
243 | $sql.= " ".$this->db->escape($this->statut).","; |
||
244 | $sql.= " ".(! empty($this->canvas)?"'".$this->db->escape($this->canvas)."'":"null").","; |
||
245 | $sql.= " ".$this->db->escape($this->entity).","; |
||
246 | $sql.= "'".$this->db->escape($this->ref_ext)."',"; |
||
247 | $sql.= " ".(! empty($this->import_key)?"'".$this->db->escape($this->import_key)."'":"null"); |
||
248 | $sql.= ")"; |
||
249 | |||
250 | dol_syslog(get_class($this)."::create", LOG_DEBUG); |
||
251 | $resql=$this->db->query($sql); |
||
252 | if ($resql) |
||
253 | { |
||
254 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."socpeople"); |
||
255 | |||
256 | if (! $error) |
||
257 | { |
||
258 | $result=$this->update($this->id, $user, 1, 'add'); |
||
259 | if ($result < 0) |
||
260 | { |
||
261 | $error++; |
||
262 | $this->error=$this->db->lasterror(); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | if (! $error) |
||
267 | { |
||
268 | $result=$this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update |
||
269 | if ($result < 0) |
||
270 | { |
||
271 | $error++; |
||
272 | $this->error=$this->db->lasterror(); |
||
273 | } |
||
274 | } |
||
275 | |||
276 | if (! $error) |
||
277 | { |
||
278 | // Call trigger |
||
279 | $result=$this->call_trigger('CONTACT_CREATE',$user); |
||
280 | if ($result < 0) { $error++; } |
||
281 | // End call triggers |
||
282 | } |
||
283 | |||
284 | if (! $error) |
||
285 | { |
||
286 | $this->db->commit(); |
||
287 | return $this->id; |
||
288 | } |
||
289 | else |
||
290 | { |
||
291 | $this->db->rollback(); |
||
292 | dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); |
||
293 | return -2; |
||
294 | } |
||
295 | } |
||
296 | else |
||
297 | { |
||
298 | $this->error=$this->db->lasterror(); |
||
299 | |||
300 | $this->db->rollback(); |
||
301 | dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR); |
||
302 | return -1; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Update informations into database |
||
308 | * |
||
309 | * @param int $id Id of contact/address to update |
||
310 | * @param User $user Objet user making change |
||
311 | * @param int $notrigger 0=no, 1=yes |
||
312 | * @param string $action Current action for hookmanager |
||
313 | * @param int $nosyncuser No sync linked user (external users and contacts are linked) |
||
314 | * @return int <0 if KO, >0 if OK |
||
315 | */ |
||
316 | function update($id, $user=null, $notrigger=0, $action='update', $nosyncuser=0) |
||
317 | { |
||
318 | global $conf, $langs, $hookmanager; |
||
319 | |||
320 | $error=0; |
||
321 | |||
322 | $this->id = $id; |
||
323 | |||
324 | $this->entity = ((isset($this->entity) && is_numeric($this->entity))?$this->entity:$conf->entity); |
||
325 | |||
326 | // Clean parameters |
||
327 | $this->lastname=trim($this->lastname)?trim($this->lastname):trim($this->lastname); |
||
328 | $this->firstname=trim($this->firstname); |
||
329 | $this->email=trim($this->email); |
||
330 | $this->phone_pro=trim($this->phone_pro); |
||
331 | $this->phone_perso=trim($this->phone_perso); |
||
332 | $this->phone_mobile=trim($this->phone_mobile); |
||
333 | $this->jabberid=trim($this->jabberid); |
||
334 | $this->skype=trim($this->skype); |
||
335 | $this->photo=trim($this->photo); |
||
336 | $this->fax=trim($this->fax); |
||
337 | $this->zip=(empty($this->zip)?'':$this->zip); |
||
338 | $this->town=(empty($this->town)?'':$this->town); |
||
339 | $this->country_id=($this->country_id > 0?$this->country_id:$this->country_id); |
||
340 | $this->state_id=($this->state_id > 0?$this->state_id:$this->fk_departement); |
||
341 | if (empty($this->statut)) $this->statut = 0; |
||
342 | |||
343 | $this->db->begin(); |
||
344 | |||
345 | $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET "; |
||
346 | if ($this->socid > 0) $sql .= " fk_soc='".$this->db->escape($this->socid)."',"; |
||
347 | else if ($this->socid == -1) $sql .= " fk_soc=null,"; |
||
348 | $sql .= " civility='".$this->db->escape($this->civility_id)."'"; |
||
349 | $sql .= ", lastname='".$this->db->escape($this->lastname)."'"; |
||
350 | $sql .= ", firstname='".$this->db->escape($this->firstname)."'"; |
||
351 | $sql .= ", address='".$this->db->escape($this->address)."'"; |
||
352 | $sql .= ", zip='".$this->db->escape($this->zip)."'"; |
||
353 | $sql .= ", town='".$this->db->escape($this->town)."'"; |
||
354 | $sql .= ", fk_pays=".($this->country_id>0?$this->country_id:'NULL'); |
||
355 | $sql .= ", fk_departement=".($this->state_id>0?$this->state_id:'NULL'); |
||
356 | $sql .= ", poste='".$this->db->escape($this->poste)."'"; |
||
357 | $sql .= ", fax='".$this->db->escape($this->fax)."'"; |
||
358 | $sql .= ", email='".$this->db->escape($this->email)."'"; |
||
359 | $sql .= ", skype='".$this->db->escape($this->skype)."'"; |
||
360 | $sql .= ", twitter='".$this->db->escape($this->twitter)."'"; |
||
361 | $sql .= ", facebook='".$this->db->escape($this->facebook)."'"; |
||
362 | $sql .= ", photo='".$this->db->escape($this->photo)."'"; |
||
363 | $sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null"); |
||
364 | $sql .= ", note_private = ".(isset($this->note_private)?"'".$this->db->escape($this->note_private)."'":"null"); |
||
365 | $sql .= ", note_public = ".(isset($this->note_public)?"'".$this->db->escape($this->note_public)."'":"null"); |
||
366 | $sql .= ", phone = ".(isset($this->phone_pro)?"'".$this->db->escape($this->phone_pro)."'":"null"); |
||
367 | $sql .= ", phone_perso = ".(isset($this->phone_perso)?"'".$this->db->escape($this->phone_perso)."'":"null"); |
||
368 | $sql .= ", phone_mobile = ".(isset($this->phone_mobile)?"'".$this->db->escape($this->phone_mobile)."'":"null"); |
||
369 | $sql .= ", jabberid = ".(isset($this->jabberid)?"'".$this->db->escape($this->jabberid)."'":"null"); |
||
370 | $sql .= ", priv = '".$this->db->escape($this->priv)."'"; |
||
371 | $sql .= ", statut = ".$this->db->escape($this->statut); |
||
372 | $sql .= ", fk_user_modif=".($user->id > 0 ? "'".$this->db->escape($user->id)."'":"NULL"); |
||
373 | $sql .= ", default_lang=".($this->default_lang?"'".$this->db->escape($this->default_lang)."'":"NULL"); |
||
374 | $sql .= ", entity = " . $this->db->escape($this->entity); |
||
375 | $sql .= " WHERE rowid=".$this->db->escape($id); |
||
376 | |||
377 | dol_syslog(get_class($this)."::update", LOG_DEBUG); |
||
378 | $result = $this->db->query($sql); |
||
379 | if ($result) |
||
380 | { |
||
381 | unset($this->country_code); |
||
382 | unset($this->country); |
||
383 | unset($this->state_code); |
||
384 | unset($this->state); |
||
385 | |||
386 | $action='update'; |
||
387 | |||
388 | // Actions on extra fields |
||
389 | if (! $error && empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) |
||
390 | { |
||
391 | $result=$this->insertExtraFields(); |
||
392 | if ($result < 0) |
||
393 | { |
||
394 | $error++; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | if (! $error && $this->user_id > 0) |
||
399 | { |
||
400 | $tmpobj = new User($this->db); |
||
401 | $tmpobj->fetch($this->user_id); |
||
402 | $usermustbemodified = 0; |
||
403 | if ($tmpobj->office_phone != $this->phone_pro) |
||
404 | { |
||
405 | $tmpobj->office_phone = $this->phone_pro; |
||
406 | $usermustbemodified++; |
||
407 | } |
||
408 | if ($tmpobj->office_fax != $this->fax) |
||
409 | { |
||
410 | $tmpobj->office_fax = $this->fax; |
||
411 | $usermustbemodified++; |
||
412 | } |
||
413 | if ($tmpobj->address != $this->address) |
||
414 | { |
||
415 | $tmpobj->address = $this->address; |
||
416 | $usermustbemodified++; |
||
417 | } |
||
418 | if ($tmpobj->town != $this->town) |
||
419 | { |
||
420 | $tmpobj->town = $this->town; |
||
421 | $usermustbemodified++; |
||
422 | } |
||
423 | if ($tmpobj->zip != $this->zip) |
||
424 | { |
||
425 | $tmpobj->zip = $this->zip; |
||
426 | $usermustbemodified++; |
||
427 | } |
||
428 | if ($tmpobj->zip != $this->zip) |
||
429 | { |
||
430 | $tmpobj->state_id=$this->state_id; |
||
431 | $usermustbemodified++; |
||
432 | } |
||
433 | if ($tmpobj->country_id != $this->country_id) |
||
434 | { |
||
435 | $tmpobj->country_id = $this->country_id; |
||
436 | $usermustbemodified++; |
||
437 | } |
||
438 | if ($tmpobj->email != $this->email) |
||
439 | { |
||
440 | $tmpobj->email = $this->email; |
||
441 | $usermustbemodified++; |
||
442 | } |
||
443 | if ($tmpobj->skype != $this->skype) |
||
444 | { |
||
445 | $tmpobj->skype = $this->skype; |
||
446 | $usermustbemodified++; |
||
447 | } |
||
448 | if ($tmpobj->twitter != $this->twitter) |
||
449 | { |
||
450 | $tmpobj->twitter = $this->twitter; |
||
451 | $usermustbemodified++; |
||
452 | } |
||
453 | if ($tmpobj->facebook != $this->facebook) |
||
454 | { |
||
455 | $tmpobj->facebook = $this->facebook; |
||
456 | $usermustbemodified++; |
||
457 | } |
||
458 | if ($usermustbemodified) |
||
459 | { |
||
460 | $result=$tmpobj->update($user, 0, 1, 1, 1); |
||
461 | if ($result < 0) { $error++; } |
||
462 | } |
||
463 | } |
||
464 | |||
465 | if (! $error && ! $notrigger) |
||
466 | { |
||
467 | // Call trigger |
||
468 | $result=$this->call_trigger('CONTACT_MODIFY',$user); |
||
469 | if ($result < 0) { $error++; } |
||
470 | // End call triggers |
||
471 | } |
||
472 | |||
473 | if (! $error) |
||
474 | { |
||
475 | $this->db->commit(); |
||
476 | return 1; |
||
477 | } |
||
478 | else |
||
479 | { |
||
480 | dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR); |
||
481 | $this->db->rollback(); |
||
482 | return -$error; |
||
483 | } |
||
484 | } |
||
485 | else |
||
486 | { |
||
487 | $this->error=$this->db->lasterror().' sql='.$sql; |
||
488 | $this->db->rollback(); |
||
489 | return -1; |
||
490 | } |
||
491 | } |
||
492 | |||
493 | |||
494 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
495 | /** |
||
496 | * Retourne chaine DN complete dans l'annuaire LDAP pour l'objet |
||
497 | * |
||
498 | * @param array $info Info string loaded by _load_ldap_info |
||
499 | * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb) |
||
500 | * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb) |
||
501 | * 2=Return key only (uid=qqq) |
||
502 | * @return string DN |
||
503 | */ |
||
504 | function _load_ldap_dn($info,$mode=0) |
||
505 | { |
||
506 | // phpcs:enable |
||
507 | global $conf; |
||
508 | $dn=''; |
||
509 | if ($mode==0) $dn=$conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN; |
||
510 | elseif ($mode==1) $dn=$conf->global->LDAP_CONTACT_DN; |
||
511 | elseif ($mode==2) $dn=$conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS]; |
||
512 | return $dn; |
||
513 | } |
||
514 | |||
515 | |||
516 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
517 | /** |
||
518 | * Initialise tableau info (tableau des attributs LDAP) |
||
519 | * |
||
520 | * @return array Tableau info des attributs |
||
521 | */ |
||
522 | function _load_ldap_info() |
||
523 | { |
||
524 | // phpcs:enable |
||
525 | global $conf, $langs; |
||
526 | |||
527 | $info = array(); |
||
528 | |||
529 | // Object classes |
||
530 | $info["objectclass"]=explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS); |
||
531 | |||
532 | $this->fullname=$this->getFullName($langs); |
||
533 | |||
534 | // Fields |
||
535 | if ($this->fullname && ! empty($conf->global->LDAP_CONTACT_FIELD_FULLNAME)) $info[$conf->global->LDAP_CONTACT_FIELD_FULLNAME] = $this->fullname; |
||
536 | if ($this->lastname && ! empty($conf->global->LDAP_CONTACT_FIELD_NAME)) $info[$conf->global->LDAP_CONTACT_FIELD_NAME] = $this->lastname; |
||
537 | if ($this->firstname && ! empty($conf->global->LDAP_CONTACT_FIELD_FIRSTNAME)) $info[$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME] = $this->firstname; |
||
538 | |||
539 | if ($this->poste) $info["title"] = $this->poste; |
||
540 | if ($this->socid > 0) |
||
541 | { |
||
542 | $soc = new Societe($this->db); |
||
543 | $soc->fetch($this->socid); |
||
544 | |||
545 | $info[$conf->global->LDAP_CONTACT_FIELD_COMPANY] = $soc->name; |
||
546 | if ($soc->client == 1) $info["businessCategory"] = "Customers"; |
||
547 | if ($soc->client == 2) $info["businessCategory"] = "Prospects"; |
||
548 | if ($soc->fournisseur == 1) $info["businessCategory"] = "Suppliers"; |
||
549 | } |
||
550 | if ($this->address && ! empty($conf->global->LDAP_CONTACT_FIELD_ADDRESS)) $info[$conf->global->LDAP_CONTACT_FIELD_ADDRESS] = $this->address; |
||
551 | if ($this->zip && ! empty($conf->global->LDAP_CONTACT_FIELD_ZIP)) $info[$conf->global->LDAP_CONTACT_FIELD_ZIP] = $this->zip; |
||
552 | if ($this->town && ! empty($conf->global->LDAP_CONTACT_FIELD_TOWN)) $info[$conf->global->LDAP_CONTACT_FIELD_TOWN] = $this->town; |
||
553 | if ($this->country_code && ! empty($conf->global->LDAP_CONTACT_FIELD_COUNTRY)) $info[$conf->global->LDAP_CONTACT_FIELD_COUNTRY] = $this->country_code; |
||
554 | if ($this->phone_pro && ! empty($conf->global->LDAP_CONTACT_FIELD_PHONE)) $info[$conf->global->LDAP_CONTACT_FIELD_PHONE] = $this->phone_pro; |
||
555 | if ($this->phone_perso && ! empty($conf->global->LDAP_CONTACT_FIELD_HOMEPHONE)) $info[$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE] = $this->phone_perso; |
||
556 | if ($this->phone_mobile && ! empty($conf->global->LDAP_CONTACT_FIELD_MOBILE)) $info[$conf->global->LDAP_CONTACT_FIELD_MOBILE] = $this->phone_mobile; |
||
557 | if ($this->fax && ! empty($conf->global->LDAP_CONTACT_FIELD_FAX)) $info[$conf->global->LDAP_CONTACT_FIELD_FAX] = $this->fax; |
||
558 | if ($this->skype && ! empty($conf->global->LDAP_CONTACT_FIELD_SKYPE)) $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype; |
||
559 | if ($this->note_private && ! empty($conf->global->LDAP_CONTACT_FIELD_DESCRIPTION)) $info[$conf->global->LDAP_CONTACT_FIELD_DESCRIPTION] = dol_string_nohtmltag($this->note_private, 2); |
||
560 | if ($this->email && ! empty($conf->global->LDAP_CONTACT_FIELD_MAIL)) $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email; |
||
561 | |||
562 | if ($conf->global->LDAP_SERVER_TYPE == 'egroupware') |
||
563 | { |
||
564 | $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware |
||
565 | |||
566 | $info['uidnumber'] = $this->id; |
||
567 | |||
568 | $info['phpgwTz'] = 0; |
||
569 | $info['phpgwMailType'] = 'INTERNET'; |
||
570 | $info['phpgwMailHomeType'] = 'INTERNET'; |
||
571 | |||
572 | $info["phpgwContactTypeId"] = 'n'; |
||
573 | $info["phpgwContactCatId"] = 0; |
||
574 | $info["phpgwContactAccess"] = "public"; |
||
575 | |||
576 | if (dol_strlen($this->egroupware_id) == 0) |
||
577 | { |
||
578 | $this->egroupware_id = 1; |
||
579 | } |
||
580 | |||
581 | $info["phpgwContactOwner"] = $this->egroupware_id; |
||
582 | |||
583 | if ($this->email) $info["rfc822Mailbox"] = $this->email; |
||
584 | if ($this->phone_mobile) $info["phpgwCellTelephoneNumber"] = $this->phone_mobile; |
||
585 | } |
||
586 | |||
587 | return $info; |
||
588 | } |
||
589 | |||
590 | |||
591 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
592 | /** |
||
593 | * Update field alert birthday |
||
594 | * |
||
595 | * @param int $id Id of contact |
||
596 | * @param User $user User asking to change alert or birthday |
||
597 | * @param int $notrigger 0=no, 1=yes |
||
598 | * @return int <0 if KO, >=0 if OK |
||
599 | */ |
||
600 | function update_perso($id, $user=null, $notrigger=0) |
||
601 | { |
||
602 | // phpcs:enable |
||
603 | $error=0; |
||
604 | $result=false; |
||
605 | |||
606 | $this->db->begin(); |
||
607 | |||
608 | // Mis a jour contact |
||
609 | $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET"; |
||
610 | $sql.= " birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null"); |
||
611 | $sql.= ", photo = ".($this->photo? "'".$this->db->escape($this->photo)."'" : "null"); |
||
612 | if ($user) $sql .= ", fk_user_modif=".$user->id; |
||
613 | $sql.= " WHERE rowid=".$this->db->escape($id); |
||
614 | |||
615 | dol_syslog(get_class($this)."::update_perso this->birthday=".$this->birthday." -", LOG_DEBUG); |
||
616 | $resql = $this->db->query($sql); |
||
617 | if (! $resql) |
||
618 | { |
||
619 | $error++; |
||
620 | $this->error=$this->db->lasterror(); |
||
621 | } |
||
622 | |||
623 | // Mis a jour alerte birthday |
||
624 | if ($this->birthday_alert) |
||
625 | { |
||
626 | //check existing |
||
627 | $sql_check = "SELECT * FROM ".MAIN_DB_PREFIX."user_alert WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id; |
||
628 | $result_check = $this->db->query($sql_check); |
||
629 | if (! $result_check || ($this->db->num_rows($result_check)<1)) |
||
630 | { |
||
631 | //insert |
||
632 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."user_alert(type,fk_contact,fk_user) "; |
||
633 | $sql.= "VALUES (1,".$this->db->escape($id).",".$user->id.")"; |
||
634 | $result = $this->db->query($sql); |
||
635 | if (! $result) |
||
636 | { |
||
637 | $error++; |
||
638 | $this->error=$this->db->lasterror(); |
||
639 | } |
||
640 | } |
||
641 | else |
||
642 | { |
||
643 | $result = true; |
||
644 | } |
||
645 | } |
||
646 | else |
||
647 | { |
||
648 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."user_alert "; |
||
649 | $sql.= "WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id; |
||
650 | $result = $this->db->query($sql); |
||
651 | if (! $result) |
||
652 | { |
||
653 | $error++; |
||
654 | $this->error=$this->db->lasterror(); |
||
655 | } |
||
656 | } |
||
657 | |||
658 | if (! $error && ! $notrigger) |
||
659 | { |
||
660 | // Call trigger |
||
661 | $result=$this->call_trigger('CONTACT_MODIFY',$user); |
||
662 | if ($result < 0) { $error++; } |
||
663 | // End call triggers |
||
664 | } |
||
665 | |||
666 | if (! $error) |
||
667 | { |
||
668 | $this->db->commit(); |
||
669 | return 1; |
||
670 | } |
||
671 | else |
||
672 | { |
||
673 | dol_syslog(get_class($this)."::update Error ".$this->error,LOG_ERR); |
||
674 | $this->db->rollback(); |
||
675 | return -$error; |
||
676 | } |
||
677 | } |
||
678 | |||
679 | |||
680 | /** |
||
681 | * Load object contact |
||
682 | * |
||
683 | * @param int $id id du contact |
||
684 | * @param User $user Utilisateur (abonnes aux alertes) qui veut les alertes de ce contact |
||
685 | * @param string $ref_ext External reference, not given by Dolibarr |
||
686 | * @param string $email Email |
||
687 | * @return int -1 if KO, 0 if OK but not found, 1 if OK |
||
688 | */ |
||
689 | function fetch($id, $user=null, $ref_ext='', $email='') |
||
690 | { |
||
691 | global $langs; |
||
692 | |||
693 | dol_syslog(get_class($this)."::fetch id=".$id, LOG_DEBUG); |
||
694 | |||
695 | if (empty($id) && empty($ref_ext)) |
||
696 | { |
||
697 | $this->error='BadParameter'; |
||
698 | return -1; |
||
699 | } |
||
700 | |||
701 | $langs->load("companies"); |
||
702 | |||
703 | $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_id, c.lastname, c.firstname,"; |
||
704 | $sql.= " c.address, c.statut, c.zip, c.town,"; |
||
705 | $sql.= " c.fk_pays as country_id,"; |
||
706 | $sql.= " c.fk_departement,"; |
||
707 | $sql.= " c.birthday,"; |
||
708 | $sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid, c.skype, c.twitter, c.facebook,"; |
||
709 | $sql.= " c.photo,"; |
||
710 | $sql.= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,"; |
||
711 | $sql.= " c.import_key,"; |
||
712 | $sql.= " c.datec as date_creation, c.tms as date_modification,"; |
||
713 | $sql.= " co.label as country, co.code as country_code,"; |
||
714 | $sql.= " d.nom as state, d.code_departement as state_code,"; |
||
715 | $sql.= " u.rowid as user_id, u.login as user_login,"; |
||
716 | $sql.= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang"; |
||
717 | $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c"; |
||
718 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid"; |
||
719 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid"; |
||
720 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople"; |
||
721 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid"; |
||
722 | if ($id) $sql.= " WHERE c.rowid = ". $id; |
||
723 | else |
||
724 | { |
||
725 | $sql .= " WHERE c.entity IN (".getEntity($this->element).")"; |
||
726 | if ($ref_ext) { |
||
727 | $sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'"; |
||
728 | } |
||
729 | if ($email) { |
||
730 | $sql .= " AND c.email = '".$this->db->escape($email)."'"; |
||
731 | } |
||
732 | } |
||
733 | |||
734 | $resql=$this->db->query($sql); |
||
735 | if ($resql) |
||
736 | { |
||
737 | if ($this->db->num_rows($resql)) |
||
738 | { |
||
739 | $obj = $this->db->fetch_object($resql); |
||
740 | |||
741 | $this->id = $obj->rowid; |
||
742 | $this->entity = $obj->entity; |
||
743 | $this->ref = $obj->rowid; |
||
744 | $this->ref_ext = $obj->ref_ext; |
||
745 | $this->civility_id = $obj->civility_id; |
||
746 | $this->civility_code = $obj->civility_id; |
||
747 | $this->lastname = $obj->lastname; |
||
748 | $this->firstname = $obj->firstname; |
||
749 | $this->address = $obj->address; |
||
750 | $this->zip = $obj->zip; |
||
751 | $this->town = $obj->town; |
||
752 | |||
753 | $this->date_creation = $this->db->jdate($obj->date_creation); |
||
754 | $this->date_modification = $this->db->jdate($obj->date_modification); |
||
755 | |||
756 | $this->fk_departement = $obj->fk_departement; // deprecated |
||
757 | $this->state_id = $obj->fk_departement; |
||
758 | $this->departement_code = $obj->state_code; // deprecated |
||
759 | $this->state_code = $obj->state_code; |
||
760 | $this->departement = $obj->state; // deprecated |
||
761 | $this->state = $obj->state; |
||
762 | |||
763 | $this->country_id = $obj->country_id; |
||
764 | $this->country_code = $obj->country_id?$obj->country_code:''; |
||
765 | $this->country = $obj->country_id?($langs->trans('Country'.$obj->country_code)!='Country'.$obj->country_code?$langs->transnoentities('Country'.$obj->country_code):$obj->country):''; |
||
766 | |||
767 | $this->socid = $obj->fk_soc; |
||
768 | $this->socname = $obj->socname; |
||
769 | $this->poste = $obj->poste; |
||
770 | $this->statut = $obj->statut; |
||
771 | |||
772 | $this->phone_pro = trim($obj->phone); |
||
773 | $this->fax = trim($obj->fax); |
||
774 | $this->phone_perso = trim($obj->phone_perso); |
||
775 | $this->phone_mobile = trim($obj->phone_mobile); |
||
776 | |||
777 | $this->email = $obj->email; |
||
778 | $this->jabberid = $obj->jabberid; |
||
779 | $this->skype = $obj->skype; |
||
780 | $this->twitter = $obj->twitter; |
||
781 | $this->facebook = $obj->facebook; |
||
782 | $this->photo = $obj->photo; |
||
783 | $this->priv = $obj->priv; |
||
784 | $this->mail = $obj->email; |
||
785 | |||
786 | $this->birthday = $this->db->jdate($obj->birthday); |
||
787 | $this->note = $obj->note_private; // deprecated |
||
788 | $this->note_private = $obj->note_private; |
||
789 | $this->note_public = $obj->note_public; |
||
790 | $this->default_lang = $obj->default_lang; |
||
791 | $this->user_id = $obj->user_id; |
||
792 | $this->user_login = $obj->user_login; |
||
793 | $this->canvas = $obj->canvas; |
||
794 | |||
795 | $this->import_key = $obj->import_key; |
||
796 | |||
797 | // Define gender according to civility |
||
798 | $this->setGenderFromCivility(); |
||
799 | |||
800 | // Search Dolibarr user linked to this contact |
||
801 | $sql = "SELECT u.rowid "; |
||
802 | $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; |
||
803 | $sql .= " WHERE u.fk_socpeople = ". $this->id; |
||
804 | |||
805 | $resql=$this->db->query($sql); |
||
806 | if ($resql) |
||
807 | { |
||
808 | if ($this->db->num_rows($resql)) |
||
809 | { |
||
810 | $uobj = $this->db->fetch_object($resql); |
||
811 | |||
812 | $this->user_id = $uobj->rowid; |
||
813 | } |
||
814 | $this->db->free($resql); |
||
815 | } |
||
816 | else |
||
817 | { |
||
818 | $this->error=$this->db->error(); |
||
819 | return -1; |
||
820 | } |
||
821 | |||
822 | // Charge alertes du user |
||
823 | if ($user) |
||
824 | { |
||
825 | $sql = "SELECT fk_user"; |
||
826 | $sql .= " FROM ".MAIN_DB_PREFIX."user_alert"; |
||
827 | $sql .= " WHERE fk_user = ".$user->id." AND fk_contact = ".$this->db->escape($id); |
||
828 | |||
829 | $resql=$this->db->query($sql); |
||
830 | if ($resql) |
||
831 | { |
||
832 | if ($this->db->num_rows($resql)) |
||
833 | { |
||
834 | $obj = $this->db->fetch_object($resql); |
||
835 | |||
836 | $this->birthday_alert = 1; |
||
837 | } |
||
838 | $this->db->free($resql); |
||
839 | } |
||
840 | else |
||
841 | { |
||
842 | $this->error=$this->db->error(); |
||
843 | return -1; |
||
844 | } |
||
845 | } |
||
846 | |||
847 | // Retreive all extrafield |
||
848 | // fetch optionals attributes and labels |
||
849 | $this->fetch_optionals(); |
||
850 | |||
851 | return 1; |
||
852 | } |
||
853 | else |
||
854 | { |
||
855 | $this->error=$langs->trans("RecordNotFound"); |
||
856 | return 0; |
||
857 | } |
||
858 | } |
||
859 | else |
||
860 | { |
||
861 | $this->error=$this->db->error(); |
||
862 | return -1; |
||
863 | } |
||
864 | } |
||
865 | |||
866 | |||
867 | /** |
||
868 | * Set property ->gender from property ->civility_id |
||
869 | * |
||
870 | * @return void |
||
871 | */ |
||
872 | function setGenderFromCivility() |
||
873 | { |
||
874 | unset($this->gender); |
||
875 | if (in_array($this->civility_id, array('MR'))) { |
||
876 | $this->gender = 'man'; |
||
877 | } else if(in_array($this->civility_id, array('MME','MLE'))) { |
||
878 | $this->gender = 'woman'; |
||
879 | } |
||
880 | } |
||
881 | |||
882 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
883 | /** |
||
884 | * Load number of elements the contact is used as a link for |
||
885 | * ref_facturation |
||
886 | * ref_contrat |
||
887 | * ref_commande (for order and/or shipments) |
||
888 | * ref_propale |
||
889 | * |
||
890 | * @return int <0 if KO, >=0 if OK |
||
891 | */ |
||
892 | function load_ref_elements() |
||
893 | { |
||
894 | // phpcs:enable |
||
895 | // Compte les elements pour lesquels il est contact |
||
896 | $sql ="SELECT tc.element, count(ec.rowid) as nb"; |
||
897 | $sql.=" FROM ".MAIN_DB_PREFIX."element_contact as ec, ".MAIN_DB_PREFIX."c_type_contact as tc"; |
||
898 | $sql.=" WHERE ec.fk_c_type_contact = tc.rowid"; |
||
899 | $sql.=" AND fk_socpeople = ". $this->id; |
||
900 | $sql.=" AND tc.source = 'external'"; |
||
901 | $sql.=" GROUP BY tc.element"; |
||
902 | |||
903 | dol_syslog(get_class($this)."::load_ref_elements", LOG_DEBUG); |
||
904 | |||
905 | $resql=$this->db->query($sql); |
||
906 | if ($resql) |
||
907 | { |
||
908 | while($obj=$this->db->fetch_object($resql)) |
||
909 | { |
||
910 | if ($obj->nb) |
||
911 | { |
||
912 | if ($obj->element=='facture') $this->ref_facturation = $obj->nb; |
||
913 | elseif ($obj->element=='contrat') $this->ref_contrat = $obj->nb; |
||
914 | elseif ($obj->element=='commande') $this->ref_commande = $obj->nb; |
||
915 | elseif ($obj->element=='propal') $this->ref_propal = $obj->nb; |
||
916 | } |
||
917 | } |
||
918 | $this->db->free($resql); |
||
919 | return 0; |
||
920 | } |
||
921 | else |
||
922 | { |
||
923 | $this->error=$this->db->lasterror(); |
||
924 | return -1; |
||
925 | } |
||
926 | } |
||
927 | |||
928 | /** |
||
929 | * Efface le contact de la base |
||
930 | * |
||
931 | * @param int $notrigger Disable all trigger |
||
932 | * @return int <0 if KO, >0 if OK |
||
933 | */ |
||
934 | function delete($notrigger=0) |
||
935 | { |
||
936 | global $conf, $langs, $user; |
||
937 | |||
938 | $error=0; |
||
939 | |||
940 | //$this->old_lastname = $obj->lastname; |
||
941 | //$this->old_firstname = $obj->firstname; |
||
942 | |||
943 | $this->db->begin(); |
||
944 | |||
945 | if (! $error) |
||
946 | { |
||
947 | // Get all rowid of element_contact linked to a type that is link to llx_socpeople |
||
948 | $sql = "SELECT ec.rowid"; |
||
949 | $sql.= " FROM ".MAIN_DB_PREFIX."element_contact ec,"; |
||
950 | $sql.= " ".MAIN_DB_PREFIX."c_type_contact tc"; |
||
951 | $sql.= " WHERE ec.fk_socpeople=".$this->id; |
||
952 | $sql.= " AND ec.fk_c_type_contact=tc.rowid"; |
||
953 | $sql.= " AND tc.source='external'"; |
||
954 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
955 | $resql = $this->db->query($sql); |
||
956 | if ($resql) |
||
957 | { |
||
958 | $num=$this->db->num_rows($resql); |
||
959 | |||
960 | $i=0; |
||
961 | while ($i < $num && ! $error) |
||
962 | { |
||
963 | $obj = $this->db->fetch_object($resql); |
||
964 | |||
965 | $sqldel = "DELETE FROM ".MAIN_DB_PREFIX."element_contact"; |
||
966 | $sqldel.=" WHERE rowid = ".$obj->rowid; |
||
967 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
968 | $result = $this->db->query($sqldel); |
||
969 | if (! $result) |
||
970 | { |
||
971 | $error++; |
||
972 | $this->error=$this->db->error().' sql='.$sqldel; |
||
973 | } |
||
974 | |||
975 | $i++; |
||
976 | } |
||
977 | } |
||
978 | else |
||
979 | { |
||
980 | $error++; |
||
981 | $this->error=$this->db->error().' sql='.$sql; |
||
982 | } |
||
983 | } |
||
984 | |||
985 | if (! $error) |
||
986 | { |
||
987 | // Remove category |
||
988 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."categorie_contact WHERE fk_socpeople = ".$this->id; |
||
989 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
990 | $resql=$this->db->query($sql); |
||
991 | if (! $resql) |
||
992 | { |
||
993 | $error++; |
||
994 | $this->error .= $this->db->lasterror(); |
||
995 | $errorflag=-1; |
||
996 | } |
||
997 | } |
||
998 | |||
999 | if (! $error) |
||
1000 | { |
||
1001 | $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople"; |
||
1002 | $sql .= " WHERE rowid=".$this->id; |
||
1003 | dol_syslog(get_class($this)."::delete", LOG_DEBUG); |
||
1004 | $result = $this->db->query($sql); |
||
1005 | if (! $result) |
||
1006 | { |
||
1007 | $error++; |
||
1008 | $this->error=$this->db->error().' sql='.$sql; |
||
1009 | } |
||
1010 | } |
||
1011 | |||
1012 | // Removed extrafields |
||
1013 | if ((! $error) && (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED))) { // For avoid conflicts if trigger used |
||
1014 | $result=$this->deleteExtraFields($this); |
||
1015 | if ($result < 0) $error++; |
||
1016 | } |
||
1017 | |||
1018 | if (! $error && ! $notrigger) |
||
1019 | { |
||
1020 | // Call trigger |
||
1021 | $result=$this->call_trigger('CONTACT_DELETE',$user); |
||
1022 | if ($result < 0) { $error++; } |
||
1023 | // End call triggers |
||
1024 | } |
||
1025 | |||
1026 | if (! $error) |
||
1027 | { |
||
1028 | |||
1029 | $this->db->commit(); |
||
1030 | return 1; |
||
1031 | } |
||
1032 | else |
||
1033 | { |
||
1034 | $this->db->rollback(); |
||
1035 | dol_syslog("Error ".$this->error,LOG_ERR); |
||
1036 | return -1; |
||
1037 | } |
||
1038 | } |
||
1039 | |||
1040 | |||
1041 | /** |
||
1042 | * Charge les informations sur le contact, depuis la base |
||
1043 | * |
||
1044 | * @param int $id Id du contact a charger |
||
1045 | * @return void |
||
1046 | */ |
||
1047 | function info($id) |
||
1048 | { |
||
1049 | $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,"; |
||
1050 | $sql.= " c.tms as tms, c.fk_user_modif"; |
||
1051 | $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c"; |
||
1052 | $sql.= " WHERE c.rowid = ".$this->db->escape($id); |
||
1053 | |||
1054 | $resql=$this->db->query($sql); |
||
1055 | if ($resql) |
||
1056 | { |
||
1057 | if ($this->db->num_rows($resql)) |
||
1058 | { |
||
1059 | $obj = $this->db->fetch_object($resql); |
||
1060 | |||
1061 | $this->id = $obj->rowid; |
||
1062 | |||
1063 | if ($obj->fk_user_creat) { |
||
1064 | $cuser = new User($this->db); |
||
1065 | $cuser->fetch($obj->fk_user_creat); |
||
1066 | $this->user_creation = $cuser; |
||
1067 | } |
||
1068 | |||
1069 | if ($obj->fk_user_modif) { |
||
1070 | $muser = new User($this->db); |
||
1071 | $muser->fetch($obj->fk_user_modif); |
||
1072 | $this->user_modification = $muser; |
||
1073 | } |
||
1074 | |||
1075 | $this->date_creation = $this->db->jdate($obj->datec); |
||
1076 | $this->date_modification = $this->db->jdate($obj->tms); |
||
1077 | } |
||
1078 | |||
1079 | $this->db->free($resql); |
||
1080 | } |
||
1081 | else |
||
1082 | { |
||
1083 | print $this->db->error(); |
||
1084 | } |
||
1085 | } |
||
1086 | |||
1087 | /** |
||
1088 | * Return number of mass Emailing received by this contacts with its email |
||
1089 | * |
||
1090 | * @return int Number of EMailings |
||
1091 | */ |
||
1092 | function getNbOfEMailings() |
||
1093 | { |
||
1094 | $sql = "SELECT count(mc.email) as nb"; |
||
1095 | $sql.= " FROM ".MAIN_DB_PREFIX."mailing_cibles as mc, ".MAIN_DB_PREFIX."mailing as m"; |
||
1096 | $sql.= " WHERE mc.fk_mailing=m.rowid AND mc.email = '".$this->db->escape($this->email)."' "; |
||
1097 | $sql.= " AND m.entity IN (".getEntity($this->element).") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success |
||
1098 | |||
1099 | $resql=$this->db->query($sql); |
||
1100 | if ($resql) |
||
1101 | { |
||
1102 | $obj = $this->db->fetch_object($resql); |
||
1103 | $nb=$obj->nb; |
||
1104 | |||
1105 | $this->db->free($resql); |
||
1106 | return $nb; |
||
1107 | } |
||
1108 | else |
||
1109 | { |
||
1110 | $this->error=$this->db->error(); |
||
1111 | return -1; |
||
1112 | } |
||
1113 | } |
||
1114 | |||
1115 | /** |
||
1116 | * Return name of contact with link (and eventually picto) |
||
1117 | * Use $this->id, $this->lastname, $this->firstname, this->civility_id |
||
1118 | * |
||
1119 | * @param int $withpicto Include picto with link |
||
1120 | * @param string $option Where the link point to |
||
1121 | * @param int $maxlen Max length of |
||
1122 | * @param string $moreparam Add more param into URL |
||
1123 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
1124 | * @param int $notooltip 1=Disable tooltip |
||
1125 | * @return string String with URL |
||
1126 | */ |
||
1127 | function getNomUrl($withpicto=0, $option='', $maxlen=0, $moreparam='', $save_lastsearch_value=-1, $notooltip=0) |
||
1128 | { |
||
1129 | global $conf, $langs, $hookmanager; |
||
1130 | |||
1131 | $result=''; |
||
1132 | |||
1133 | $label = '<u>' . $langs->trans("ShowContact") . '</u>'; |
||
1134 | $label.= '<br><b>' . $langs->trans("Name") . ':</b> '.$this->getFullName($langs); |
||
1135 | //if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate cibilty_id code |
||
1136 | if (! empty($this->poste)) $label.= '<br><b>' . $langs->trans("Poste") . ':</b> '.$this->poste; |
||
1137 | $label.= '<br><b>' . $langs->trans("EMail") . ':</b> '.$this->email; |
||
1138 | $phonelist=array(); |
||
1139 | if ($this->phone_pro) $phonelist[]=$this->phone_pro; |
||
1140 | if ($this->phone_mobile) $phonelist[]=$this->phone_mobile; |
||
1141 | if ($this->phone_perso) $phonelist[]=$this->phone_perso; |
||
1142 | $label.= '<br><b>' . $langs->trans("Phone") . ':</b> '.join(', ',$phonelist); |
||
1143 | $label.= '<br><b>' . $langs->trans("Address") . ':</b> '.dol_format_address($this, 1, ' ', $langs); |
||
1144 | |||
1145 | $url = DOL_URL_ROOT.'/contact/card.php?id='.$this->id; |
||
1146 | |||
1147 | if ($option !== 'nolink') |
||
1148 | { |
||
1149 | // Add param to save lastsearch_values or not |
||
1150 | $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
||
1151 | if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; |
||
1152 | if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; |
||
1153 | } |
||
1154 | |||
1155 | $url .= $moreparam; |
||
1156 | |||
1157 | $linkclose=""; |
||
1158 | if (empty($notooltip)) { |
||
1159 | if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) |
||
1160 | { |
||
1161 | $label=$langs->trans("ShowContact"); |
||
1162 | $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"'; |
||
1163 | } |
||
1164 | $linkclose.= ' title="'.dol_escape_htmltag($label, 1).'"'; |
||
1165 | $linkclose.= ' class="classfortooltip"'; |
||
1166 | |||
1167 | /* |
||
1168 | $hookmanager->initHooks(array('contactdao')); |
||
1169 | $parameters=array('id'=>$this->id); |
||
1170 | $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
1171 | if ($reshook > 0) $linkclose = $hookmanager->resPrint; |
||
1172 | */ |
||
1173 | } |
||
1174 | |||
1175 | $linkstart = '<a href="'.$url.'"'; |
||
1176 | $linkstart.=$linkclose.'>'; |
||
1177 | $linkend='</a>'; |
||
1178 | |||
1179 | if ($option == 'xxx') |
||
1180 | { |
||
1181 | $linkstart = '<a href="'.DOL_URL_ROOT.'/contact/card.php?id='.$this->id.$moreparam.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; |
||
1182 | $linkend='</a>'; |
||
1183 | } |
||
1184 | |||
1185 | $result.=$linkstart; |
||
1186 | if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip valigntextbottom"'), 0, 0, $notooltip?0:1); |
||
1187 | if ($withpicto != 2) $result.=($maxlen?dol_trunc($this->getFullName($langs),$maxlen):$this->getFullName($langs)); |
||
1188 | $result.=$linkend; |
||
1189 | |||
1190 | global $action; |
||
1191 | $hookmanager->initHooks(array('contactdao')); |
||
1192 | $parameters=array('id'=>$this->id, 'getnomurl'=>$result); |
||
1193 | $reshook=$hookmanager->executeHooks('getNomUrl',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
1194 | if ($reshook > 0) $result = $hookmanager->resPrint; |
||
1195 | else $result .= $hookmanager->resPrint; |
||
1196 | |||
1197 | return $result; |
||
1198 | } |
||
1199 | |||
1200 | /** |
||
1201 | * Return civility label of contact |
||
1202 | * |
||
1203 | * @return string Translated name of civility |
||
1204 | */ |
||
1205 | function getCivilityLabel() |
||
1206 | { |
||
1207 | global $langs; |
||
1208 | $langs->load("dict"); |
||
1209 | |||
1210 | $code=(! empty($this->civility_id)?$this->civility_id:(! empty($this->civilite_id)?$this->civilite_id:'')); |
||
|
|||
1211 | if (empty($code)) return ''; |
||
1212 | return $langs->getLabelFromKey($this->db, "Civility".$code, "c_civility", "code", "label", $code); |
||
1213 | } |
||
1214 | |||
1215 | /** |
||
1216 | * Return label of contact status |
||
1217 | * |
||
1218 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
1219 | * @return string Label of contact status |
||
1220 | */ |
||
1221 | function getLibStatut($mode) |
||
1222 | { |
||
1223 | return $this->LibStatut($this->statut,$mode); |
||
1224 | } |
||
1225 | |||
1226 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1227 | /** |
||
1228 | * Renvoi le libelle d'un statut donne |
||
1229 | * |
||
1230 | * @param int $statut Id statut |
||
1231 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
1232 | * @return string Libelle |
||
1233 | */ |
||
1234 | function LibStatut($statut,$mode) |
||
1235 | { |
||
1236 | // phpcs:enable |
||
1237 | global $langs; |
||
1238 | |||
1239 | if ($mode == 0) |
||
1240 | { |
||
1241 | if ($statut==0 || $statut==5) return $langs->trans('Disabled'); |
||
1242 | elseif ($statut==1 || $statut==4) return $langs->trans('Enabled'); |
||
1243 | } |
||
1244 | elseif ($mode == 1) |
||
1245 | { |
||
1246 | if ($statut==0 || $statut==5) return $langs->trans('Disabled'); |
||
1247 | elseif ($statut==1 || $statut==4) return $langs->trans('Enabled'); |
||
1248 | } |
||
1249 | elseif ($mode == 2) |
||
1250 | { |
||
1251 | if ($statut==0 || $statut==5) return img_picto($langs->trans('Disabled'),'statut5', 'class="pictostatus"').' '.$langs->trans('Disabled'); |
||
1252 | elseif ($statut==1 || $statut==4) return img_picto($langs->trans('Enabled'),'statut4', 'class="pictostatus"').' '.$langs->trans('Enabled'); |
||
1253 | } |
||
1254 | elseif ($mode == 3) |
||
1255 | { |
||
1256 | if ($statut==0 || $statut==5) return img_picto($langs->trans('Disabled'),'statut5', 'class="pictostatus"'); |
||
1257 | elseif ($statut==1 || $statut==4) return img_picto($langs->trans('Enabled'),'statut4', 'class="pictostatus"'); |
||
1258 | } |
||
1259 | elseif ($mode == 4) |
||
1260 | { |
||
1261 | if ($statut==0) return img_picto($langs->trans('Disabled'),'statut5', 'class="pictostatus"').' '.$langs->trans('Disabled'); |
||
1262 | elseif ($statut==1 || $statut==4) return img_picto($langs->trans('Enabled'),'statut4', 'class="pictostatus"').' '.$langs->trans('Enabled'); |
||
1263 | } |
||
1264 | elseif ($mode == 5) |
||
1265 | { |
||
1266 | if ($statut==0 || $statut==5) return '<span class="hideonsmartphone">'.$langs->trans('Disabled').' </span>'.img_picto($langs->trans('Disabled'),'statut5', 'class="pictostatus"'); |
||
1267 | elseif ($statut==1 || $statut==4) return '<span class="hideonsmartphone">'.$langs->trans('Enabled').' </span>'.img_picto($langs->trans('Enabled'),'statut4', 'class="pictostatus"'); |
||
1268 | } |
||
1269 | } |
||
1270 | |||
1271 | |||
1272 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
1273 | /** |
||
1274 | * Return translated label of Public or Private |
||
1275 | * |
||
1276 | * @param int $statut Type (0 = public, 1 = private) |
||
1277 | * @return string Label translated |
||
1278 | */ |
||
1279 | function LibPubPriv($statut) |
||
1280 | { |
||
1281 | // phpcs:enable |
||
1282 | global $langs; |
||
1283 | if ($statut=='1') return $langs->trans('ContactPrivate'); |
||
1284 | else return $langs->trans('ContactPublic'); |
||
1285 | } |
||
1286 | |||
1287 | |||
1288 | /** |
||
1289 | * Initialise an instance with random values. |
||
1290 | * Used to build previews or test instances. |
||
1291 | * id must be 0 if object instance is a specimen. |
||
1292 | * |
||
1293 | * @return void |
||
1294 | */ |
||
1295 | function initAsSpecimen() |
||
1296 | { |
||
1297 | // Get first id of existing company and save it into $socid |
||
1298 | $socid = 0; |
||
1299 | $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."societe ORDER BY rowid LIMIT 1"; |
||
1300 | $resql = $this->db->query($sql); |
||
1301 | if ($resql) |
||
1302 | { |
||
1303 | $obj = $this->db->fetch_object($resql); |
||
1304 | if ($obj) $socid=$obj->rowid; |
||
1305 | } |
||
1306 | |||
1307 | // Initialise parameters |
||
1308 | $this->id=0; |
||
1309 | $this->specimen=1; |
||
1310 | $this->lastname = 'DOLIBARR'; |
||
1311 | $this->firstname = 'SPECIMEN'; |
||
1312 | $this->address = '21 jump street'; |
||
1313 | $this->zip = '99999'; |
||
1314 | $this->town = 'MyTown'; |
||
1315 | $this->country_id = 1; |
||
1316 | $this->country_code = 'FR'; |
||
1317 | $this->country = 'France'; |
||
1318 | $this->email = '[email protected]'; |
||
1319 | $this->skype = 'tom.hanson'; |
||
1320 | |||
1321 | $this->phone_pro = '0909090901'; |
||
1322 | $this->phone_perso = '0909090902'; |
||
1323 | $this->phone_mobile = '0909090903'; |
||
1324 | $this->fax = '0909090909'; |
||
1325 | |||
1326 | $this->note_public='This is a comment (public)'; |
||
1327 | $this->note_private='This is a comment (private)'; |
||
1328 | |||
1329 | $this->socid = $socid; |
||
1330 | $this->statut=1; |
||
1331 | } |
||
1332 | |||
1333 | /** |
||
1334 | * Change status of a user |
||
1335 | * |
||
1336 | * @param int $statut Status to set |
||
1337 | * @return int <0 if KO, 0 if nothing is done, >0 if OK |
||
1338 | */ |
||
1339 | function setstatus($statut) |
||
1340 | { |
||
1341 | global $conf,$langs,$user; |
||
1342 | |||
1343 | $error=0; |
||
1344 | |||
1345 | // Check parameters |
||
1346 | if ($this->statut == $statut) return 0; |
||
1347 | else $this->statut = $statut; |
||
1348 | |||
1349 | $this->db->begin(); |
||
1350 | |||
1351 | // Desactive utilisateur |
||
1352 | $sql = "UPDATE ".MAIN_DB_PREFIX."socpeople"; |
||
1353 | $sql.= " SET statut = ".$this->statut; |
||
1354 | $sql.= " WHERE rowid = ".$this->id; |
||
1355 | $result = $this->db->query($sql); |
||
1356 | |||
1357 | dol_syslog(get_class($this)."::setstatus", LOG_DEBUG); |
||
1358 | if ($result) |
||
1359 | { |
||
1360 | // Call trigger |
||
1361 | $result=$this->call_trigger('CONTACT_ENABLEDISABLE',$user); |
||
1362 | if ($result < 0) { $error++; } |
||
1363 | // End call triggers |
||
1364 | } |
||
1365 | |||
1366 | if ($error) |
||
1367 | { |
||
1368 | $this->db->rollback(); |
||
1369 | return -$error; |
||
1370 | } |
||
1371 | else |
||
1372 | { |
||
1373 | $this->db->commit(); |
||
1374 | return 1; |
||
1375 | } |
||
1376 | } |
||
1377 | |||
1378 | /** |
||
1379 | * Sets object to supplied categories. |
||
1380 | * |
||
1381 | * Deletes object from existing categories not supplied. |
||
1382 | * Adds it to non existing supplied categories. |
||
1383 | * Existing categories are left untouch. |
||
1384 | * |
||
1385 | * @param int[]|int $categories Category or categories IDs |
||
1386 | * @return void |
||
1387 | */ |
||
1388 | public function setCategories($categories) |
||
1389 | { |
||
1390 | // Handle single category |
||
1391 | if (!is_array($categories)) { |
||
1392 | $categories = array($categories); |
||
1393 | } |
||
1394 | |||
1395 | // Get current categories |
||
1396 | require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php'; |
||
1397 | $c = new Categorie($this->db); |
||
1398 | $existing = $c->containing($this->id, Categorie::TYPE_CONTACT, 'id'); |
||
1399 | |||
1400 | // Diff |
||
1401 | if (is_array($existing)) { |
||
1402 | $to_del = array_diff($existing, $categories); |
||
1403 | $to_add = array_diff($categories, $existing); |
||
1404 | } else { |
||
1405 | $to_del = array(); // Nothing to delete |
||
1406 | $to_add = $categories; |
||
1407 | } |
||
1408 | |||
1409 | // Process |
||
1410 | foreach ($to_del as $del) { |
||
1411 | if ($c->fetch($del) > 0) { |
||
1412 | $c->del_type($this, 'contact'); |
||
1413 | } |
||
1414 | } |
||
1415 | foreach ($to_add as $add) { |
||
1416 | if ($c->fetch($add) > 0) { |
||
1417 | $c->add_type($this, 'contact'); |
||
1418 | } |
||
1419 | } |
||
1420 | |||
1421 | return; |
||
1422 | } |
||
1423 | |||
1424 | /** |
||
1425 | * Function used to replace a thirdparty id with another one. |
||
1426 | * |
||
1427 | * @param DoliDB $db Database handler |
||
1428 | * @param int $origin_id Old thirdparty id |
||
1429 | * @param int $dest_id New thirdparty id |
||
1430 | * @return bool |
||
1431 | */ |
||
1432 | public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id) |
||
1439 | } |
||
1440 | } |
||
1441 |