| Total Complexity | 354 |
| Total Lines | 2171 |
| 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 |
||
| 49 | class Contact extends CommonObject |
||
| 50 | { |
||
| 51 | use CommonSocialNetworks; |
||
| 52 | use CommonPeople; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string ID to identify managed object |
||
| 56 | */ |
||
| 57 | public $element = 'contact'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string Name of table without prefix where object is stored |
||
| 61 | */ |
||
| 62 | public $table_element = 'socpeople'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
| 66 | */ |
||
| 67 | public $picto = 'contact'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password') |
||
| 71 | * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)" |
||
| 72 | * 'label' the translation key. |
||
| 73 | * 'enabled' is a condition when the field must be managed. |
||
| 74 | * 'position' is the sort order of field. |
||
| 75 | * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0). |
||
| 76 | * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing) |
||
| 77 | * 'noteditable' says if field is not editable (1 or 0) |
||
| 78 | * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created. |
||
| 79 | * 'index' if we want an index in database. |
||
| 80 | * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommended to name the field fk_...). |
||
| 81 | * 'searchall' is 1 if we want to search in this field when making a search from the quick search button. |
||
| 82 | * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8). |
||
| 83 | * 'css' is the CSS style to use on field. For example: 'maxwidth200' |
||
| 84 | * 'help' is a string visible as a tooltip on field |
||
| 85 | * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record |
||
| 86 | * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code. |
||
| 87 | * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel") |
||
| 88 | * 'comment' is not used. You can store here any text of your choice. It is not used by application. |
||
| 89 | * |
||
| 90 | * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor. |
||
| 91 | */ |
||
| 92 | |||
| 93 | // BEGIN MODULEBUILDER PROPERTIES |
||
| 94 | /** |
||
| 95 | * @var array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}> Array with all fields and their property. Do not use it as a static var. It may be modified by constructor. |
||
|
|
|||
| 96 | */ |
||
| 97 | public $fields = array( |
||
| 98 | 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -2, 'noteditable' => 1, 'notnull' => 1, 'index' => 1, 'position' => 1, 'comment' => 'Id', 'css' => 'left'), |
||
| 99 | 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => 3, 'notnull' => 1, 'position' => 30, 'index' => 1), |
||
| 100 | 'ref_ext' => array('type' => 'varchar(255)', 'label' => 'Ref ext', 'enabled' => 1, 'visible' => 3, 'position' => 35), |
||
| 101 | 'civility' => array('type' => 'varchar(6)', 'label' => 'Civility', 'enabled' => 1, 'visible' => 3, 'position' => 40), |
||
| 102 | 'lastname' => array('type' => 'varchar(50)', 'label' => 'Lastname', 'enabled' => 1, 'visible' => 1, 'position' => 45, 'showoncombobox' => 1, 'searchall' => 1), |
||
| 103 | 'name_alias' => array('type' => 'varchar(255)', 'label' => 'Name alias', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'searchall' => 1), |
||
| 104 | 'firstname' => array('type' => 'varchar(50)', 'label' => 'Firstname', 'enabled' => 1, 'visible' => 1, 'position' => 50, 'showoncombobox' => 1, 'searchall' => 1), |
||
| 105 | 'poste' => array('type' => 'varchar(80)', 'label' => 'PostOrFunction', 'enabled' => 1, 'visible' => -1, 'position' => 52), |
||
| 106 | 'address' => array('type' => 'varchar(255)', 'label' => 'Address', 'enabled' => 1, 'visible' => -1, 'position' => 55), |
||
| 107 | 'zip' => array('type' => 'varchar(25)', 'label' => 'Zip', 'enabled' => 1, 'visible' => -1, 'position' => 60), |
||
| 108 | 'town' => array('type' => 'varchar(50)', 'label' => 'Town', 'enabled' => 1, 'visible' => -1, 'position' => 65), |
||
| 109 | 'fk_departement' => array('type' => 'integer', 'label' => 'Fk departement', 'enabled' => 1, 'visible' => 3, 'position' => 70), |
||
| 110 | 'fk_pays' => array('type' => 'integer', 'label' => 'Fk pays', 'enabled' => 1, 'visible' => 3, 'position' => 75), |
||
| 111 | 'fk_soc' => array('type' => 'integer', 'label' => 'ThirdParty', 'enabled' => 1, 'visible' => 1, 'position' => 77, 'searchall' => 1), |
||
| 112 | 'birthday' => array('type' => 'date', 'label' => 'Birthday', 'enabled' => 1, 'visible' => -1, 'position' => 80), |
||
| 113 | 'phone' => array('type' => 'varchar(30)', 'label' => 'Phone', 'enabled' => 1, 'visible' => 1, 'position' => 90, 'searchall' => 1), |
||
| 114 | 'phone_perso' => array('type' => 'varchar(30)', 'label' => 'PhonePerso', 'enabled' => 1, 'visible' => -1, 'position' => 95, 'searchall' => 1), |
||
| 115 | 'phone_mobile' => array('type' => 'varchar(30)', 'label' => 'PhoneMobile', 'enabled' => 1, 'visible' => 1, 'position' => 100, 'searchall' => 1), |
||
| 116 | 'fax' => array('type' => 'varchar(30)', 'label' => 'Fax', 'enabled' => 1, 'visible' => -1, 'position' => 105, 'searchall' => 1), |
||
| 117 | 'email' => array('type' => 'varchar(255)', 'label' => 'Email', 'enabled' => 1, 'visible' => 1, 'position' => 110, 'searchall' => 1), |
||
| 118 | 'socialnetworks' => array('type' => 'text', 'label' => 'SocialNetworks', 'enabled' => 1, 'visible' => 3, 'position' => 115), |
||
| 119 | 'photo' => array('type' => 'varchar(255)', 'label' => 'Photo', 'enabled' => 1, 'visible' => 3, 'position' => 170), |
||
| 120 | 'priv' => array('type' => 'smallint(6)', 'label' => 'ContactVisibility', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 175), |
||
| 121 | 'fk_stcommcontact' => array('type' => 'integer', 'label' => 'ProspectStatus', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 220), |
||
| 122 | 'fk_prospectcontactlevel' => array('type' => 'varchar(12)', 'label' => 'ProspectLevel', 'enabled' => 1, 'visible' => -1, 'position' => 255), |
||
| 123 | //no more used. Replace by a scan of email into mailing_unsubscribe. 'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180), |
||
| 124 | 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 3, 'position' => 195, 'searchall' => 1), |
||
| 125 | 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 3, 'position' => 200, 'searchall' => 1), |
||
| 126 | 'default_lang' => array('type' => 'varchar(6)', 'label' => 'Default lang', 'enabled' => 1, 'visible' => 3, 'position' => 205), |
||
| 127 | 'canvas' => array('type' => 'varchar(32)', 'label' => 'Canvas', 'enabled' => 1, 'visible' => 3, 'position' => 210), |
||
| 128 | 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 300), |
||
| 129 | 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 305), |
||
| 130 | 'fk_user_creat' => array('type' => 'integer', 'label' => 'UserAuthor', 'enabled' => 1, 'visible' => 3, 'position' => 310), |
||
| 131 | 'fk_user_modif' => array('type' => 'integer', 'label' => 'UserModif', 'enabled' => 1, 'visible' => 3, 'position' => 315), |
||
| 132 | 'statut' => array('type' => 'tinyint(4)', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 500), |
||
| 133 | 'import_key' => array('type' => 'varchar(14)', 'label' => 'ImportId', 'enabled' => 1, 'visible' => -1, 'position' => 1000), |
||
| 134 | ); |
||
| 135 | |||
| 136 | public $civility_id; // In fact we store civility_code |
||
| 137 | public $civility_code; |
||
| 138 | public $civility; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string gender |
||
| 142 | */ |
||
| 143 | public $gender; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var int egroupware_id |
||
| 147 | */ |
||
| 148 | //private $egroupware_id; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var int birthday_alert |
||
| 152 | */ |
||
| 153 | public $birthday_alert; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var string The civilite code, not an integer |
||
| 157 | * @deprecated |
||
| 158 | * @see $civility_code |
||
| 159 | */ |
||
| 160 | public $civilite; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @var string fullname |
||
| 164 | */ |
||
| 165 | public $fullname; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @var string Name alias |
||
| 169 | */ |
||
| 170 | public $name_alias; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @var string Address |
||
| 174 | */ |
||
| 175 | public $address; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @var string zip code |
||
| 179 | */ |
||
| 180 | public $zip; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @var string Town |
||
| 184 | */ |
||
| 185 | public $town; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @var int Id of department |
||
| 189 | */ |
||
| 190 | public $state_id; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @var string Code of department |
||
| 194 | */ |
||
| 195 | public $state_code; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @var string Label of department |
||
| 199 | */ |
||
| 200 | public $state; |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @var string Job Position |
||
| 204 | */ |
||
| 205 | public $poste; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @var int Thirdparty ID |
||
| 209 | */ |
||
| 210 | public $socid; // both socid and fk_soc are used |
||
| 211 | public $fk_soc; // both socid and fk_soc are used |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @var string Thirdparty name |
||
| 215 | */ |
||
| 216 | public $socname; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @var int Status 0=inactive, 1=active |
||
| 220 | */ |
||
| 221 | public $statut; |
||
| 222 | |||
| 223 | public $code; |
||
| 224 | |||
| 225 | /** |
||
| 226 | |||
| 227 | * @var string |
||
| 228 | */ |
||
| 229 | public $email; |
||
| 230 | |||
| 231 | /** |
||
| 232 | |||
| 233 | * @var string |
||
| 234 | * @deprecated |
||
| 235 | * @see $email |
||
| 236 | */ |
||
| 237 | public $mail; |
||
| 238 | |||
| 239 | /** |
||
| 240 | * URL |
||
| 241 | * @var string |
||
| 242 | */ |
||
| 243 | public $url; |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Unsubscribe all : 1 = contact has globally unsubscribed of all mass emailing |
||
| 247 | * @var int |
||
| 248 | * @deprecated Has been replaced by a search into llx_mailing_unsubscribe |
||
| 249 | */ |
||
| 250 | public $no_email; |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Array of social-networks |
||
| 254 | * @var array |
||
| 255 | */ |
||
| 256 | public $socialnetworks; |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @var string filename for photo |
||
| 260 | */ |
||
| 261 | public $photo; |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @var string phone pro (professional/business) |
||
| 265 | */ |
||
| 266 | public $phone_pro; |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @var string phone perso (personal/private) |
||
| 270 | */ |
||
| 271 | public $phone_perso; |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @var string phone mobile |
||
| 275 | */ |
||
| 276 | public $phone_mobile; |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @var string fax |
||
| 280 | */ |
||
| 281 | public $fax; |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Private or public |
||
| 285 | * @var int |
||
| 286 | */ |
||
| 287 | public $priv; |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @var int|string Date |
||
| 291 | */ |
||
| 292 | public $birthday; |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @var string language for contact communication -- only with multilanguage enabled |
||
| 296 | */ |
||
| 297 | public $default_lang; |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @var int Number of invoices for which he is contact |
||
| 301 | */ |
||
| 302 | public $ref_facturation; |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @var int Number of contracts for which he is contact |
||
| 306 | */ |
||
| 307 | public $ref_contrat; |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @var int Number of orders for which he is contact |
||
| 311 | */ |
||
| 312 | public $ref_commande; |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @var int Number of proposals for which he is contact |
||
| 316 | */ |
||
| 317 | public $ref_propal; |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @var int user ID |
||
| 321 | */ |
||
| 322 | public $user_id; |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @var string user login |
||
| 326 | */ |
||
| 327 | public $user_login; |
||
| 328 | |||
| 329 | // END MODULEBUILDER PROPERTIES |
||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * Old copy |
||
| 334 | * @var static |
||
| 335 | */ |
||
| 336 | public $oldcopy; // To contains a clone of this when we need to save old properties of object |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @var array roles |
||
| 340 | */ |
||
| 341 | public $roles; |
||
| 342 | |||
| 343 | public $cacheprospectstatus = array(); |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @var string Prospect level. ie: 'PL_LOW', 'PL...' |
||
| 347 | */ |
||
| 348 | public $fk_prospectlevel; |
||
| 349 | |||
| 350 | public $stcomm_id; |
||
| 351 | |||
| 352 | public $statut_commercial; |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @var string picto |
||
| 356 | */ |
||
| 357 | public $stcomm_picto; |
||
| 358 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * Constructor |
||
| 362 | * |
||
| 363 | * @param DoliDB $db Database handler |
||
| 364 | */ |
||
| 365 | public function __construct($db) |
||
| 366 | { |
||
| 367 | $this->db = $db; |
||
| 368 | $this->statut = 1; // By default, status is enabled |
||
| 369 | $this->ismultientitymanaged = 1; |
||
| 370 | $this->isextrafieldmanaged = 1; |
||
| 371 | |||
| 372 | if (!isModEnabled('mailing')) { |
||
| 373 | $this->fields['no_email']['enabled'] = 0; |
||
| 374 | } |
||
| 375 | // typical ['s.nom'] is used for third-parties |
||
| 376 | if (!getDolGlobalString('SOCIETE_DISABLE_CONTACTS')) { |
||
| 377 | $this->fields['fk_soc']['enabled'] = 0; |
||
| 378 | $this->fields['fk_soc']['searchall'] = 0; |
||
| 379 | } |
||
| 380 | |||
| 381 | // If THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES not set, there is no prospect level on contact level, only on thirdparty |
||
| 382 | if (getDolGlobalString('SOCIETE_DISABLE_PROSPECTS') || !getDolGlobalString('THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES')) { // Default behaviour |
||
| 383 | $this->fields['fk_stcommcontact']['enabled'] = 0; |
||
| 384 | $this->fields['fk_prospectcontactlevel']['enabled'] = 0; |
||
| 385 | } |
||
| 386 | |||
| 387 | // Unset fields that are disabled |
||
| 388 | foreach ($this->fields as $key => $val) { |
||
| 389 | if (isset($val['enabled']) && empty($val['enabled'])) { |
||
| 390 | unset($this->fields[$key]); |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | // Translate some data of arrayofkeyval |
||
| 395 | /*if (is_object($langs)) |
||
| 396 | { |
||
| 397 | foreach($this->fields as $key => $val) |
||
| 398 | { |
||
| 399 | if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) |
||
| 400 | { |
||
| 401 | foreach($val['arrayofkeyval'] as $key2 => $val2) |
||
| 402 | { |
||
| 403 | $this->fields[$key]['arrayofkeyval'][$key2]=$langs->trans($val2); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | }*/ |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Load indicators into this->nb for board |
||
| 412 | * |
||
| 413 | * @return int Return integer <0 if KO, >0 if OK |
||
| 414 | */ |
||
| 415 | public function loadStateBoard() |
||
| 416 | { |
||
| 417 | global $user, $hookmanager; |
||
| 418 | |||
| 419 | $this->nb = array(); |
||
| 420 | $clause = "WHERE"; |
||
| 421 | |||
| 422 | $sql = "SELECT count(sp.rowid) as nb"; |
||
| 423 | $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as sp"; |
||
| 424 | if (!$user->hasRight('societe', 'client', 'voir')) { |
||
| 425 | $sql .= ", " . MAIN_DB_PREFIX . "societe as s"; |
||
| 426 | $sql .= ", " . MAIN_DB_PREFIX . "societe_commerciaux as sc"; |
||
| 427 | $sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = " . ((int) $user->id); |
||
| 428 | $clause = "AND"; |
||
| 429 | } |
||
| 430 | $sql .= " " . $clause . " sp.entity IN (" . getEntity($this->element) . ")"; |
||
| 431 | $sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat = " . ((int) $user->id) . "))"; |
||
| 432 | if ($user->socid > 0) { |
||
| 433 | $sql .= " AND sp.fk_soc = " . ((int) $user->socid); |
||
| 434 | } |
||
| 435 | // Add where from hooks |
||
| 436 | if (is_object($hookmanager)) { |
||
| 437 | $parameters = array(); |
||
| 438 | $reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $this); // Note that $action and $object may have been modified by hook |
||
| 439 | $sql .= $hookmanager->resPrint; |
||
| 440 | } |
||
| 441 | |||
| 442 | $resql = $this->db->query($sql); |
||
| 443 | if ($resql) { |
||
| 444 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 445 | $this->nb["contacts"] = $obj->nb; |
||
| 446 | } |
||
| 447 | $this->db->free($resql); |
||
| 448 | return 1; |
||
| 449 | } else { |
||
| 450 | dol_print_error($this->db); |
||
| 451 | $this->error = $this->db->lasterror(); |
||
| 452 | return -1; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Add a contact into database |
||
| 458 | * |
||
| 459 | * @param User $user Object user that create |
||
| 460 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
| 461 | * @return int Return integer <0 if KO, >0 if OK |
||
| 462 | */ |
||
| 463 | public function create($user, $notrigger = 0) |
||
| 464 | { |
||
| 465 | global $conf; |
||
| 466 | |||
| 467 | $error = 0; |
||
| 468 | $now = dol_now(); |
||
| 469 | |||
| 470 | $this->db->begin(); |
||
| 471 | |||
| 472 | // Clean parameters |
||
| 473 | $this->name_alias = trim($this->name_alias); |
||
| 474 | $this->lastname = $this->lastname ? trim($this->lastname) : trim($this->name); |
||
| 475 | $this->firstname = trim($this->firstname); |
||
| 476 | $this->setUpperOrLowerCase(); |
||
| 477 | if (empty($this->socid)) { |
||
| 478 | $this->socid = 0; |
||
| 479 | } |
||
| 480 | if (empty($this->priv)) { |
||
| 481 | $this->priv = 0; |
||
| 482 | } |
||
| 483 | if (empty($this->statut)) { |
||
| 484 | $this->statut = 0; // This is to convert '' into '0' to avoid bad sql request |
||
| 485 | } |
||
| 486 | |||
| 487 | $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity); |
||
| 488 | |||
| 489 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "socpeople ("; |
||
| 490 | $sql .= " datec"; |
||
| 491 | $sql .= ", fk_soc"; |
||
| 492 | $sql .= ", name_alias"; |
||
| 493 | $sql .= ", lastname"; |
||
| 494 | $sql .= ", firstname"; |
||
| 495 | $sql .= ", fk_user_creat"; |
||
| 496 | $sql .= ", priv"; |
||
| 497 | $sql .= ", fk_stcommcontact"; |
||
| 498 | $sql .= ", statut"; |
||
| 499 | $sql .= ", canvas"; |
||
| 500 | $sql .= ", entity"; |
||
| 501 | $sql .= ", ref_ext"; |
||
| 502 | $sql .= ", import_key"; |
||
| 503 | $sql .= ") VALUES ("; |
||
| 504 | $sql .= "'" . $this->db->idate($now) . "',"; |
||
| 505 | if ($this->socid > 0) { |
||
| 506 | $sql .= " " . ((int) $this->socid) . ","; |
||
| 507 | } else { |
||
| 508 | $sql .= "null,"; |
||
| 509 | } |
||
| 510 | $sql .= "'" . $this->db->escape($this->name_alias) . "',"; |
||
| 511 | $sql .= "'" . $this->db->escape($this->lastname) . "',"; |
||
| 512 | $sql .= "'" . $this->db->escape($this->firstname) . "',"; |
||
| 513 | $sql .= " " . ($user->id > 0 ? ((int) $user->id) : "null") . ","; |
||
| 514 | $sql .= " " . ((int) $this->priv) . ","; |
||
| 515 | $sql .= " 0,"; |
||
| 516 | $sql .= " " . ((int) $this->statut) . ","; |
||
| 517 | $sql .= " " . (!empty($this->canvas) ? "'" . $this->db->escape($this->canvas) . "'" : "null") . ","; |
||
| 518 | $sql .= " " . ((int) $this->entity) . ","; |
||
| 519 | $sql .= "'" . $this->db->escape($this->ref_ext) . "',"; |
||
| 520 | $sql .= " " . (!empty($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null"); |
||
| 521 | $sql .= ")"; |
||
| 522 | |||
| 523 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
| 524 | $resql = $this->db->query($sql); |
||
| 525 | if ($resql) { |
||
| 526 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "socpeople"); |
||
| 527 | |||
| 528 | if (!$error) { |
||
| 529 | $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ... |
||
| 530 | if ($result < 0) { |
||
| 531 | $error++; |
||
| 532 | $this->error = $this->db->lasterror(); |
||
| 533 | } |
||
| 534 | } |
||
| 535 | |||
| 536 | if (!$error) { |
||
| 537 | $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update |
||
| 538 | if ($result < 0) { |
||
| 539 | $error++; |
||
| 540 | $this->error = $this->db->lasterror(); |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | if (!$error && !$notrigger) { |
||
| 545 | // Call trigger |
||
| 546 | $result = $this->call_trigger('CONTACT_CREATE', $user); |
||
| 547 | if ($result < 0) { |
||
| 548 | $error++; |
||
| 549 | } |
||
| 550 | // End call triggers |
||
| 551 | } |
||
| 552 | |||
| 553 | if (!$error) { |
||
| 554 | $this->db->commit(); |
||
| 555 | return $this->id; |
||
| 556 | } else { |
||
| 557 | $this->db->rollback(); |
||
| 558 | dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); |
||
| 559 | return -2; |
||
| 560 | } |
||
| 561 | } else { |
||
| 562 | $this->error = $this->db->lasterror(); |
||
| 563 | |||
| 564 | $this->db->rollback(); |
||
| 565 | dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); |
||
| 566 | return -1; |
||
| 567 | } |
||
| 568 | } |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Update information into database |
||
| 572 | * |
||
| 573 | * @param int $id Id of contact/address to update |
||
| 574 | * @param User $user Object user making change |
||
| 575 | * @param int $notrigger 0=no, 1=yes |
||
| 576 | * @param string $action Current action for hookmanager |
||
| 577 | * @param int $nosyncuser No sync linked user (external users and contacts are linked) |
||
| 578 | * @return int Return integer <0 if KO, >0 if OK |
||
| 579 | */ |
||
| 580 | public function update($id, $user = null, $notrigger = 0, $action = 'update', $nosyncuser = 0) |
||
| 581 | { |
||
| 582 | global $conf; |
||
| 583 | |||
| 584 | $error = 0; |
||
| 585 | |||
| 586 | $this->id = $id; |
||
| 587 | |||
| 588 | $this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity); |
||
| 589 | |||
| 590 | // Clean parameters |
||
| 591 | $this->ref_ext = (empty($this->ref_ext) ? '' : trim($this->ref_ext)); |
||
| 592 | $this->name_alias = trim($this->name_alias); |
||
| 593 | $this->lastname = trim($this->lastname) ? trim($this->lastname) : trim($this->lastname); |
||
| 594 | $this->firstname = trim($this->firstname); |
||
| 595 | $this->email = trim($this->email); |
||
| 596 | $this->phone_pro = trim($this->phone_pro); |
||
| 597 | $this->phone_perso = trim($this->phone_perso); |
||
| 598 | $this->phone_mobile = trim($this->phone_mobile); |
||
| 599 | $this->photo = trim($this->photo); |
||
| 600 | $this->fax = trim($this->fax); |
||
| 601 | $this->zip = (empty($this->zip) ? '' : trim($this->zip)); |
||
| 602 | $this->town = (empty($this->town) ? '' : trim($this->town)); |
||
| 603 | $this->country_id = (empty($this->country_id) || $this->country_id < 0) ? 0 : $this->country_id; |
||
| 604 | if (empty($this->statut)) { |
||
| 605 | $this->statut = 0; |
||
| 606 | } |
||
| 607 | if (empty($this->civility_code) && !is_numeric($this->civility_id)) { |
||
| 608 | $this->civility_code = $this->civility_id; // For backward compatibility |
||
| 609 | } |
||
| 610 | $this->setUpperOrLowerCase(); |
||
| 611 | |||
| 612 | $this->db->begin(); |
||
| 613 | |||
| 614 | $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople SET"; |
||
| 615 | if ($this->socid > 0) { |
||
| 616 | $sql .= " fk_soc = " . ((int) $this->socid) . ","; |
||
| 617 | } elseif ($this->socid == -1) { |
||
| 618 | $sql .= " fk_soc = NULL,"; |
||
| 619 | } |
||
| 620 | $sql .= " civility='" . $this->db->escape($this->civility_code) . "'"; |
||
| 621 | $sql .= ", name_alias='" . $this->db->escape($this->name_alias) . "'"; |
||
| 622 | $sql .= ", lastname='" . $this->db->escape($this->lastname) . "'"; |
||
| 623 | $sql .= ", firstname='" . $this->db->escape($this->firstname) . "'"; |
||
| 624 | $sql .= ", address='" . $this->db->escape($this->address) . "'"; |
||
| 625 | $sql .= ", zip='" . $this->db->escape($this->zip) . "'"; |
||
| 626 | $sql .= ", town='" . $this->db->escape($this->town) . "'"; |
||
| 627 | $sql .= ", ref_ext = " . (!empty($this->ref_ext) ? "'" . $this->db->escape($this->ref_ext) . "'" : "NULL"); |
||
| 628 | $sql .= ", fk_pays=" . ($this->country_id > 0 ? $this->country_id : 'NULL'); |
||
| 629 | $sql .= ", fk_departement=" . ($this->state_id > 0 ? $this->state_id : 'NULL'); |
||
| 630 | $sql .= ", poste='" . $this->db->escape($this->poste) . "'"; |
||
| 631 | $sql .= ", fax='" . $this->db->escape($this->fax) . "'"; |
||
| 632 | $sql .= ", email='" . $this->db->escape($this->email) . "'"; |
||
| 633 | $sql .= ", socialnetworks = '" . $this->db->escape(json_encode($this->socialnetworks)) . "'"; |
||
| 634 | $sql .= ", photo='" . $this->db->escape($this->photo) . "'"; |
||
| 635 | $sql .= ", birthday=" . ($this->birthday ? "'" . $this->db->idate($this->birthday) . "'" : "null"); |
||
| 636 | $sql .= ", note_private = " . (isset($this->note_private) ? "'" . $this->db->escape($this->note_private) . "'" : "NULL"); |
||
| 637 | $sql .= ", note_public = " . (isset($this->note_public) ? "'" . $this->db->escape($this->note_public) . "'" : "NULL"); |
||
| 638 | $sql .= ", phone = " . (isset($this->phone_pro) ? "'" . $this->db->escape($this->phone_pro) . "'" : "NULL"); |
||
| 639 | $sql .= ", phone_perso = " . (isset($this->phone_perso) ? "'" . $this->db->escape($this->phone_perso) . "'" : "NULL"); |
||
| 640 | $sql .= ", phone_mobile = " . (isset($this->phone_mobile) ? "'" . $this->db->escape($this->phone_mobile) . "'" : "NULL"); |
||
| 641 | $sql .= ", priv = '" . $this->db->escape($this->priv) . "'"; |
||
| 642 | $sql .= ", fk_prospectlevel = '" . $this->db->escape($this->fk_prospectlevel) . "'"; |
||
| 643 | if (isset($this->stcomm_id)) { |
||
| 644 | $sql .= ", fk_stcommcontact = " . ($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0"); |
||
| 645 | } |
||
| 646 | $sql .= ", statut = " . ((int) $this->statut); |
||
| 647 | $sql .= ", fk_user_modif=" . ($user->id > 0 ? "'" . $this->db->escape($user->id) . "'" : "NULL"); |
||
| 648 | $sql .= ", default_lang=" . ($this->default_lang ? "'" . $this->db->escape($this->default_lang) . "'" : "NULL"); |
||
| 649 | $sql .= ", entity = " . ((int) $this->entity); |
||
| 650 | $sql .= " WHERE rowid = " . ((int) $id); |
||
| 651 | |||
| 652 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
| 653 | $result = $this->db->query($sql); |
||
| 654 | if ($result) { |
||
| 655 | unset($this->country_code); |
||
| 656 | unset($this->country); |
||
| 657 | unset($this->state_code); |
||
| 658 | unset($this->state); |
||
| 659 | |||
| 660 | $action = 'update'; |
||
| 661 | |||
| 662 | // Actions on extra fields |
||
| 663 | if (!$error) { |
||
| 664 | $result = $this->insertExtraFields(); |
||
| 665 | if ($result < 0) { |
||
| 666 | $error++; |
||
| 667 | } |
||
| 668 | } |
||
| 669 | |||
| 670 | if (!$error) { |
||
| 671 | $result = $this->updateRoles(); |
||
| 672 | if ($result < 0) { |
||
| 673 | $error++; |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | if (!$error && $this->user_id > 0) { |
||
| 678 | // If contact is linked to a user |
||
| 679 | $tmpobj = new User($this->db); |
||
| 680 | $tmpobj->fetch($this->user_id); |
||
| 681 | $usermustbemodified = 0; |
||
| 682 | if ($tmpobj->office_phone != $this->phone_pro) { |
||
| 683 | $tmpobj->office_phone = $this->phone_pro; |
||
| 684 | $usermustbemodified++; |
||
| 685 | } |
||
| 686 | if ($tmpobj->office_fax != $this->fax) { |
||
| 687 | $tmpobj->office_fax = $this->fax; |
||
| 688 | $usermustbemodified++; |
||
| 689 | } |
||
| 690 | if ($tmpobj->address != $this->address) { |
||
| 691 | $tmpobj->address = $this->address; |
||
| 692 | $usermustbemodified++; |
||
| 693 | } |
||
| 694 | if ($tmpobj->town != $this->town) { |
||
| 695 | $tmpobj->town = $this->town; |
||
| 696 | $usermustbemodified++; |
||
| 697 | } |
||
| 698 | if ($tmpobj->zip != $this->zip) { |
||
| 699 | $tmpobj->zip = $this->zip; |
||
| 700 | $usermustbemodified++; |
||
| 701 | } |
||
| 702 | if ($tmpobj->zip != $this->zip) { |
||
| 703 | $tmpobj->state_id = $this->state_id; |
||
| 704 | $usermustbemodified++; |
||
| 705 | } |
||
| 706 | if ($tmpobj->country_id != $this->country_id) { |
||
| 707 | $tmpobj->country_id = $this->country_id; |
||
| 708 | $usermustbemodified++; |
||
| 709 | } |
||
| 710 | if ($tmpobj->email != $this->email) { |
||
| 711 | $tmpobj->email = $this->email; |
||
| 712 | $usermustbemodified++; |
||
| 713 | } |
||
| 714 | if (!empty(array_diff($tmpobj->socialnetworks, $this->socialnetworks))) { |
||
| 715 | $tmpobj->socialnetworks = $this->socialnetworks; |
||
| 716 | $usermustbemodified++; |
||
| 717 | } |
||
| 718 | if ($usermustbemodified) { |
||
| 719 | $result = $tmpobj->update($user, 0, 1, 1, 1); |
||
| 720 | if ($result < 0) { |
||
| 721 | $error++; |
||
| 722 | } |
||
| 723 | } |
||
| 724 | } |
||
| 725 | |||
| 726 | if (!$error && !$notrigger) { |
||
| 727 | // Call trigger |
||
| 728 | $result = $this->call_trigger('CONTACT_MODIFY', $user); |
||
| 729 | if ($result < 0) { |
||
| 730 | $error++; |
||
| 731 | } |
||
| 732 | // End call triggers |
||
| 733 | } |
||
| 734 | |||
| 735 | if (!$error) { |
||
| 736 | $this->db->commit(); |
||
| 737 | return 1; |
||
| 738 | } else { |
||
| 739 | dol_syslog(get_class($this) . "::update Error " . $this->error, LOG_ERR); |
||
| 740 | $this->db->rollback(); |
||
| 741 | return -$error; |
||
| 742 | } |
||
| 743 | } else { |
||
| 744 | $this->error = $this->db->lasterror() . ' sql=' . $sql; |
||
| 745 | $this->db->rollback(); |
||
| 746 | return -1; |
||
| 747 | } |
||
| 748 | } |
||
| 749 | |||
| 750 | |||
| 751 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 752 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 753 | /** |
||
| 754 | * Return DN string complete in the LDAP directory for the object |
||
| 755 | * |
||
| 756 | * @param array $info Info string loaded by _load_ldap_info |
||
| 757 | * @param int $mode 0=Return full DN (uid=qqq,ou=xxx,dc=aaa,dc=bbb) |
||
| 758 | * 1=Return DN without key inside (ou=xxx,dc=aaa,dc=bbb) |
||
| 759 | * 2=Return key only (uid=qqq) |
||
| 760 | * @return string DN |
||
| 761 | */ |
||
| 762 | public function _load_ldap_dn($info, $mode = 0) |
||
| 763 | { |
||
| 764 | // phpcs:enable |
||
| 765 | global $conf; |
||
| 766 | $dn = ''; |
||
| 767 | if ($mode == 0) { |
||
| 768 | $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=" . $info[getDolGlobalString('LDAP_KEY_CONTACTS')] . "," . getDolGlobalString('LDAP_CONTACT_DN'); |
||
| 769 | } elseif ($mode == 1) { |
||
| 770 | $dn = getDolGlobalString('LDAP_CONTACT_DN'); |
||
| 771 | } elseif ($mode == 2) { |
||
| 772 | $dn = getDolGlobalString('LDAP_KEY_CONTACTS') . "=" . $info[getDolGlobalString('LDAP_KEY_CONTACTS')]; |
||
| 773 | } |
||
| 774 | return $dn; |
||
| 775 | } |
||
| 776 | |||
| 777 | |||
| 778 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 779 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore |
||
| 780 | /** |
||
| 781 | * Initialize info table (LDAP attributes table) |
||
| 782 | * |
||
| 783 | * @return array Attributes info table |
||
| 784 | */ |
||
| 785 | public function _load_ldap_info() |
||
| 786 | { |
||
| 787 | // phpcs:enable |
||
| 788 | global $conf, $langs; |
||
| 789 | |||
| 790 | $info = array(); |
||
| 791 | |||
| 792 | // Object classes |
||
| 793 | $info["objectclass"] = explode(',', getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS')); |
||
| 794 | |||
| 795 | $this->fullname = $this->getFullName($langs); |
||
| 796 | |||
| 797 | // Fields |
||
| 798 | if ($this->fullname && getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')) { |
||
| 799 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME')] = $this->fullname; |
||
| 800 | } |
||
| 801 | if ($this->lastname && getDolGlobalString('LDAP_CONTACT_FIELD_NAME')) { |
||
| 802 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_NAME')] = $this->lastname; |
||
| 803 | } |
||
| 804 | if ($this->firstname && getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')) { |
||
| 805 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME')] = $this->firstname; |
||
| 806 | } |
||
| 807 | |||
| 808 | if ($this->poste) { |
||
| 809 | $info["title"] = $this->poste; |
||
| 810 | } |
||
| 811 | if ($this->socid > 0) { |
||
| 812 | $soc = new Societe($this->db); |
||
| 813 | $soc->fetch($this->socid); |
||
| 814 | |||
| 815 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY')] = $soc->name; |
||
| 816 | if ($soc->client == 1) { |
||
| 817 | $info["businessCategory"] = "Customers"; |
||
| 818 | } |
||
| 819 | if ($soc->client == 2) { |
||
| 820 | $info["businessCategory"] = "Prospects"; |
||
| 821 | } |
||
| 822 | if ($soc->fournisseur == 1) { |
||
| 823 | $info["businessCategory"] = "Suppliers"; |
||
| 824 | } |
||
| 825 | } |
||
| 826 | if ($this->address && getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')) { |
||
| 827 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS')] = $this->address; |
||
| 828 | } |
||
| 829 | if ($this->zip && getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')) { |
||
| 830 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_ZIP')] = $this->zip; |
||
| 831 | } |
||
| 832 | if ($this->town && getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')) { |
||
| 833 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_TOWN')] = $this->town; |
||
| 834 | } |
||
| 835 | if ($this->country_code && getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')) { |
||
| 836 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY')] = $this->country_code; |
||
| 837 | } |
||
| 838 | if ($this->phone_pro && getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')) { |
||
| 839 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_PHONE')] = $this->phone_pro; |
||
| 840 | } |
||
| 841 | if ($this->phone_perso && getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')) { |
||
| 842 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE')] = $this->phone_perso; |
||
| 843 | } |
||
| 844 | if ($this->phone_mobile && getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')) { |
||
| 845 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE')] = $this->phone_mobile; |
||
| 846 | } |
||
| 847 | if ($this->fax && getDolGlobalString('LDAP_CONTACT_FIELD_FAX')) { |
||
| 848 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_FAX')] = $this->fax; |
||
| 849 | } |
||
| 850 | if ($this->note_private && getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')) { |
||
| 851 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_DESCRIPTION')] = dol_string_nohtmltag($this->note_private, 2); |
||
| 852 | } |
||
| 853 | if ($this->email && getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')) { |
||
| 854 | $info[getDolGlobalString('LDAP_CONTACT_FIELD_MAIL')] = $this->email; |
||
| 855 | } |
||
| 856 | |||
| 857 | if (getDolGlobalString('LDAP_SERVER_TYPE') == 'egroupware') { |
||
| 858 | $info["objectclass"][4] = "phpgwContact"; // compatibilite egroupware |
||
| 859 | |||
| 860 | $info['uidnumber'] = $this->id; |
||
| 861 | |||
| 862 | $info['phpgwTz'] = 0; |
||
| 863 | $info['phpgwMailType'] = 'INTERNET'; |
||
| 864 | $info['phpgwMailHomeType'] = 'INTERNET'; |
||
| 865 | |||
| 866 | $info["phpgwContactTypeId"] = 'n'; |
||
| 867 | $info["phpgwContactCatId"] = 0; |
||
| 868 | $info["phpgwContactAccess"] = "public"; |
||
| 869 | |||
| 870 | /* |
||
| 871 | if (dol_strlen($this->egroupware_id) == 0) { |
||
| 872 | $this->egroupware_id = 1; |
||
| 873 | } |
||
| 874 | $info["phpgwContactOwner"] = $this->egroupware_id; |
||
| 875 | */ |
||
| 876 | $info["phpgwContactOwner"] = 1; |
||
| 877 | |||
| 878 | if ($this->email) { |
||
| 879 | $info["rfc822Mailbox"] = $this->email; |
||
| 880 | } |
||
| 881 | if ($this->phone_mobile) { |
||
| 882 | $info["phpgwCellTelephoneNumber"] = $this->phone_mobile; |
||
| 883 | } |
||
| 884 | } |
||
| 885 | |||
| 886 | return $info; |
||
| 887 | } |
||
| 888 | |||
| 889 | |||
| 890 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 891 | /** |
||
| 892 | * Update field alert birthday |
||
| 893 | * |
||
| 894 | * @param int $id Id of contact |
||
| 895 | * @param User $user User asking to change alert or birthday |
||
| 896 | * @param int $notrigger 0=no, 1=yes |
||
| 897 | * @return int Return integer <0 if KO, >=0 if OK |
||
| 898 | */ |
||
| 899 | public function update_perso($id, $user = null, $notrigger = 0) |
||
| 900 | { |
||
| 901 | // phpcs:enable |
||
| 902 | $error = 0; |
||
| 903 | $result = false; |
||
| 904 | |||
| 905 | $this->db->begin(); |
||
| 906 | |||
| 907 | // Update the contact |
||
| 908 | $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople SET"; |
||
| 909 | $sql .= " birthday = " . ($this->birthday ? "'" . $this->db->idate($this->birthday) . "'" : "null"); |
||
| 910 | $sql .= ", photo = " . ($this->photo ? "'" . $this->db->escape($this->photo) . "'" : "null"); |
||
| 911 | if ($user) { |
||
| 912 | $sql .= ", fk_user_modif = " . ((int) $user->id); |
||
| 913 | } |
||
| 914 | $sql .= " WHERE rowid = " . ((int) $id); |
||
| 915 | |||
| 916 | dol_syslog(get_class($this) . "::update_perso this->birthday=" . $this->birthday . " -", LOG_DEBUG); |
||
| 917 | $resql = $this->db->query($sql); |
||
| 918 | if (!$resql) { |
||
| 919 | $error++; |
||
| 920 | $this->error = $this->db->lasterror(); |
||
| 921 | } |
||
| 922 | |||
| 923 | if ($user) { |
||
| 924 | // Update birthday alert |
||
| 925 | if (!empty($this->birthday_alert)) { |
||
| 926 | //check existing |
||
| 927 | $sql_check = "SELECT rowid FROM " . MAIN_DB_PREFIX . "user_alert WHERE type = 1 AND fk_contact = " . ((int) $id) . " AND fk_user = " . ((int) $user->id); |
||
| 928 | $result_check = $this->db->query($sql_check); |
||
| 929 | if (!$result_check || ($this->db->num_rows($result_check) < 1)) { |
||
| 930 | //insert |
||
| 931 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "user_alert(type, fk_contact, fk_user) "; |
||
| 932 | $sql .= "VALUES (1," . ((int) $id) . "," . ((int) $user->id) . ")"; |
||
| 933 | $result = $this->db->query($sql); |
||
| 934 | if (!$result) { |
||
| 935 | $error++; |
||
| 936 | $this->error = $this->db->lasterror(); |
||
| 937 | } |
||
| 938 | } else { |
||
| 939 | $result = true; |
||
| 940 | } |
||
| 941 | } else { |
||
| 942 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "user_alert "; |
||
| 943 | $sql .= "WHERE type=1 AND fk_contact=" . ((int) $id) . " AND fk_user=" . ((int) $user->id); |
||
| 944 | $result = $this->db->query($sql); |
||
| 945 | if (!$result) { |
||
| 946 | $error++; |
||
| 947 | $this->error = $this->db->lasterror(); |
||
| 948 | } |
||
| 949 | } |
||
| 950 | } |
||
| 951 | |||
| 952 | if (!$error && !$notrigger) { |
||
| 953 | // Call trigger |
||
| 954 | $result = $this->call_trigger('CONTACT_MODIFY', $user); |
||
| 955 | if ($result < 0) { |
||
| 956 | $error++; |
||
| 957 | } |
||
| 958 | // End call triggers |
||
| 959 | } |
||
| 960 | |||
| 961 | if (!$error) { |
||
| 962 | $this->db->commit(); |
||
| 963 | return 1; |
||
| 964 | } else { |
||
| 965 | dol_syslog(get_class($this) . "::update Error " . $this->error, LOG_ERR); |
||
| 966 | $this->db->rollback(); |
||
| 967 | return -$error; |
||
| 968 | } |
||
| 969 | } |
||
| 970 | |||
| 971 | |||
| 972 | /** |
||
| 973 | * Load object contact. |
||
| 974 | * |
||
| 975 | * @param int $id Id of contact |
||
| 976 | * @param ?User $user Load also alerts of this user (subscribing to alerts) that want alerts about this contact |
||
| 977 | * @param string $ref_ext External reference, not given by Dolibarr |
||
| 978 | * @param string $email Email |
||
| 979 | * @param int $loadalsoroles Load also roles. Try to always 0 here and load roles with a separate call of fetchRoles(). |
||
| 980 | * @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found. |
||
| 981 | */ |
||
| 982 | public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0) |
||
| 983 | { |
||
| 984 | global $langs; |
||
| 985 | |||
| 986 | dol_syslog(get_class($this) . "::fetch id=" . $id . " ref_ext=" . $ref_ext . " email=" . $email, LOG_DEBUG); |
||
| 987 | |||
| 988 | if (empty($id) && empty($ref_ext) && empty($email)) { |
||
| 989 | $this->error = 'BadParameter'; |
||
| 990 | return -1; |
||
| 991 | } |
||
| 992 | |||
| 993 | $langs->loadLangs(array("dict", "companies")); |
||
| 994 | |||
| 995 | $sql = "SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_code, c.name_alias, c.lastname, c.firstname,"; |
||
| 996 | $sql .= " c.address, c.statut, c.zip, c.town,"; |
||
| 997 | $sql .= " c.fk_pays as country_id,"; |
||
| 998 | $sql .= " c.fk_departement as state_id,"; |
||
| 999 | $sql .= " c.birthday,"; |
||
| 1000 | $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,"; |
||
| 1001 | $sql .= " c.socialnetworks,"; |
||
| 1002 | $sql .= " c.photo,"; |
||
| 1003 | $sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,"; |
||
| 1004 | $sql .= " c.fk_prospectlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,"; |
||
| 1005 | $sql .= " c.import_key,"; |
||
| 1006 | $sql .= " c.datec as date_creation, c.tms as date_modification,"; |
||
| 1007 | $sql .= " co.label as country, co.code as country_code,"; |
||
| 1008 | $sql .= " d.nom as state, d.code_departement as state_code,"; |
||
| 1009 | $sql .= " u.rowid as user_id, u.login as user_login,"; |
||
| 1010 | $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang"; |
||
| 1011 | $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as c"; |
||
| 1012 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_country as co ON c.fk_pays = co.rowid"; |
||
| 1013 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_departements as d ON c.fk_departement = d.rowid"; |
||
| 1014 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "user as u ON c.rowid = u.fk_socpeople"; |
||
| 1015 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe as s ON c.fk_soc = s.rowid"; |
||
| 1016 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_stcommcontact as st ON c.fk_stcommcontact = st.id'; |
||
| 1017 | if ($id) { |
||
| 1018 | $sql .= " WHERE c.rowid = " . ((int) $id); |
||
| 1019 | } else { |
||
| 1020 | $sql .= " WHERE c.entity IN (" . getEntity($this->element) . ")"; |
||
| 1021 | if ($ref_ext) { |
||
| 1022 | $sql .= " AND c.ref_ext = '" . $this->db->escape($ref_ext) . "'"; |
||
| 1023 | } |
||
| 1024 | if ($email) { |
||
| 1025 | $sql .= " AND c.email = '" . $this->db->escape($email) . "'"; |
||
| 1026 | } |
||
| 1027 | } |
||
| 1028 | |||
| 1029 | $resql = $this->db->query($sql); |
||
| 1030 | if ($resql) { |
||
| 1031 | $num = $this->db->num_rows($resql); |
||
| 1032 | if ($num > 1) { |
||
| 1033 | $this->error = 'Fetch found several records. Rename one of contact to avoid duplicate.'; |
||
| 1034 | dol_syslog($this->error, LOG_ERR); |
||
| 1035 | |||
| 1036 | return 2; |
||
| 1037 | } elseif ($num) { // $num = 1 |
||
| 1038 | $obj = $this->db->fetch_object($resql); |
||
| 1039 | |||
| 1040 | $this->id = $obj->rowid; |
||
| 1041 | $this->entity = $obj->entity; |
||
| 1042 | $this->ref = $obj->rowid; |
||
| 1043 | $this->ref_ext = $obj->ref_ext; |
||
| 1044 | |||
| 1045 | $this->civility_code = $obj->civility_code; |
||
| 1046 | $this->civility = $obj->civility_code ? ($langs->trans("Civility" . $obj->civility_code) != "Civility" . $obj->civility_code ? $langs->trans("Civility" . $obj->civility_code) : $obj->civility_code) : ''; |
||
| 1047 | |||
| 1048 | $this->name_alias = $obj->name_alias; |
||
| 1049 | $this->lastname = $obj->lastname; |
||
| 1050 | $this->firstname = $obj->firstname; |
||
| 1051 | $this->address = $obj->address; |
||
| 1052 | $this->zip = $obj->zip; |
||
| 1053 | $this->town = $obj->town; |
||
| 1054 | |||
| 1055 | $this->date_creation = $this->db->jdate($obj->date_creation); |
||
| 1056 | $this->date_modification = $this->db->jdate($obj->date_modification); |
||
| 1057 | |||
| 1058 | $this->state_id = $obj->state_id; |
||
| 1059 | $this->state_code = $obj->state_code; |
||
| 1060 | $this->state = $obj->state; |
||
| 1061 | |||
| 1062 | $this->country_id = $obj->country_id; |
||
| 1063 | $this->country_code = $obj->country_id ? $obj->country_code : ''; |
||
| 1064 | $this->country = $obj->country_id ? ($langs->trans('Country' . $obj->country_code) != 'Country' . $obj->country_code ? $langs->transnoentities('Country' . $obj->country_code) : $obj->country) : ''; |
||
| 1065 | |||
| 1066 | $this->fk_soc = $obj->fk_soc; // Both fk_soc and socid are used |
||
| 1067 | $this->socid = $obj->fk_soc; // Both fk_soc and socid are used |
||
| 1068 | $this->socname = $obj->socname; |
||
| 1069 | $this->poste = $obj->poste; |
||
| 1070 | $this->statut = $obj->statut; |
||
| 1071 | |||
| 1072 | $this->fk_prospectlevel = $obj->fk_prospectlevel; |
||
| 1073 | |||
| 1074 | $transcode = $langs->trans('StatusProspect' . $obj->fk_stcommcontact); |
||
| 1075 | $libelle = ($transcode != 'StatusProspect' . $obj->fk_stcommcontact ? $transcode : $obj->stcomm); |
||
| 1076 | $this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial |
||
| 1077 | $this->statut_commercial = $libelle; // libelle statut commercial |
||
| 1078 | $this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial |
||
| 1079 | |||
| 1080 | $this->phone_pro = trim($obj->phone); |
||
| 1081 | $this->fax = trim($obj->fax); |
||
| 1082 | $this->phone_perso = trim($obj->phone_perso); |
||
| 1083 | $this->phone_mobile = trim($obj->phone_mobile); |
||
| 1084 | |||
| 1085 | $this->email = $obj->email; |
||
| 1086 | $this->socialnetworks = ($obj->socialnetworks ? (array) json_decode($obj->socialnetworks, true) : array()); |
||
| 1087 | $this->photo = $obj->photo; |
||
| 1088 | $this->priv = $obj->priv; |
||
| 1089 | $this->mail = $obj->email; |
||
| 1090 | |||
| 1091 | $this->birthday = $this->db->jdate($obj->birthday); |
||
| 1092 | $this->note = $obj->note_private; // deprecated |
||
| 1093 | $this->note_private = $obj->note_private; |
||
| 1094 | $this->note_public = $obj->note_public; |
||
| 1095 | $this->default_lang = $obj->default_lang; |
||
| 1096 | $this->user_id = $obj->user_id; |
||
| 1097 | $this->user_login = $obj->user_login; |
||
| 1098 | $this->canvas = $obj->canvas; |
||
| 1099 | |||
| 1100 | $this->import_key = $obj->import_key; |
||
| 1101 | |||
| 1102 | // Define gender according to civility |
||
| 1103 | $this->setGenderFromCivility(); |
||
| 1104 | |||
| 1105 | // Search Dolibarr user linked to this contact |
||
| 1106 | $sql = "SELECT u.rowid "; |
||
| 1107 | $sql .= " FROM " . MAIN_DB_PREFIX . "user as u"; |
||
| 1108 | $sql .= " WHERE u.fk_socpeople = " . ((int) $this->id); |
||
| 1109 | |||
| 1110 | $resql = $this->db->query($sql); |
||
| 1111 | if ($resql) { |
||
| 1112 | if ($this->db->num_rows($resql)) { |
||
| 1113 | $uobj = $this->db->fetch_object($resql); |
||
| 1114 | |||
| 1115 | $this->user_id = $uobj->rowid; |
||
| 1116 | } |
||
| 1117 | $this->db->free($resql); |
||
| 1118 | } else { |
||
| 1119 | $this->error = $this->db->error(); |
||
| 1120 | return -1; |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | // Retrieve all extrafield |
||
| 1124 | // fetch optionals attributes and labels |
||
| 1125 | $this->fetch_optionals(); |
||
| 1126 | |||
| 1127 | // Load also alerts of this user |
||
| 1128 | if ($user) { |
||
| 1129 | $sql = "SELECT fk_user"; |
||
| 1130 | $sql .= " FROM " . MAIN_DB_PREFIX . "user_alert"; |
||
| 1131 | $sql .= " WHERE fk_user = " . ((int) $user->id) . " AND fk_contact = " . ((int) $id); |
||
| 1132 | |||
| 1133 | $resql = $this->db->query($sql); |
||
| 1134 | if ($resql) { |
||
| 1135 | if ($this->db->num_rows($resql)) { |
||
| 1136 | $obj = $this->db->fetch_object($resql); |
||
| 1137 | |||
| 1138 | $this->birthday_alert = 1; |
||
| 1139 | } |
||
| 1140 | $this->db->free($resql); |
||
| 1141 | } else { |
||
| 1142 | $this->error = $this->db->error(); |
||
| 1143 | return -1; |
||
| 1144 | } |
||
| 1145 | } |
||
| 1146 | |||
| 1147 | // Load also roles of this address |
||
| 1148 | if ($loadalsoroles) { |
||
| 1149 | $resultRole = $this->fetchRoles(); |
||
| 1150 | if ($resultRole < 0) { |
||
| 1151 | return $resultRole; |
||
| 1152 | } |
||
| 1153 | } |
||
| 1154 | |||
| 1155 | return 1; |
||
| 1156 | } else { |
||
| 1157 | $this->error = $langs->trans("RecordNotFound"); |
||
| 1158 | return 0; |
||
| 1159 | } |
||
| 1160 | } else { |
||
| 1161 | $this->error = $this->db->error(); |
||
| 1162 | return -1; |
||
| 1163 | } |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | |||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Set the property "gender" of this class, based on the property "civility_id" |
||
| 1170 | * or use property "civility_code" as fallback, when "civility_id" is not available. |
||
| 1171 | * |
||
| 1172 | * @return void |
||
| 1173 | */ |
||
| 1174 | public function setGenderFromCivility() |
||
| 1175 | { |
||
| 1176 | unset($this->gender); |
||
| 1177 | |||
| 1178 | if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR'))) { |
||
| 1179 | $this->gender = 'man'; |
||
| 1180 | } elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE'))) { |
||
| 1181 | $this->gender = 'woman'; |
||
| 1182 | } |
||
| 1183 | } |
||
| 1184 | |||
| 1185 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1186 | /** |
||
| 1187 | * Load number of elements the contact is used as a link for |
||
| 1188 | * ref_facturation |
||
| 1189 | * ref_contrat |
||
| 1190 | * ref_commande (for order and/or shipments) |
||
| 1191 | * ref_propale |
||
| 1192 | * |
||
| 1193 | * @return int Return integer <0 if KO, >=0 if OK |
||
| 1194 | */ |
||
| 1195 | public function load_ref_elements() |
||
| 1196 | { |
||
| 1197 | // phpcs:enable |
||
| 1198 | // Count the elements for which it is contact |
||
| 1199 | $sql = "SELECT tc.element, count(ec.rowid) as nb"; |
||
| 1200 | $sql .= " FROM " . MAIN_DB_PREFIX . "element_contact as ec, " . MAIN_DB_PREFIX . "c_type_contact as tc"; |
||
| 1201 | $sql .= " WHERE ec.fk_c_type_contact = tc.rowid"; |
||
| 1202 | $sql .= " AND fk_socpeople = " . ((int) $this->id); |
||
| 1203 | $sql .= " AND tc.source = 'external'"; |
||
| 1204 | $sql .= " GROUP BY tc.element"; |
||
| 1205 | |||
| 1206 | dol_syslog(get_class($this) . "::load_ref_elements", LOG_DEBUG); |
||
| 1207 | |||
| 1208 | $resql = $this->db->query($sql); |
||
| 1209 | if ($resql) { |
||
| 1210 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 1211 | if ($obj->nb) { |
||
| 1212 | if ($obj->element == 'facture') { |
||
| 1213 | $this->ref_facturation = $obj->nb; |
||
| 1214 | } elseif ($obj->element == 'contrat') { |
||
| 1215 | $this->ref_contrat = $obj->nb; |
||
| 1216 | } elseif ($obj->element == 'commande') { |
||
| 1217 | $this->ref_commande = $obj->nb; |
||
| 1218 | } elseif ($obj->element == 'propal') { |
||
| 1219 | $this->ref_propal = $obj->nb; |
||
| 1220 | } |
||
| 1221 | } |
||
| 1222 | } |
||
| 1223 | $this->db->free($resql); |
||
| 1224 | return 0; |
||
| 1225 | } else { |
||
| 1226 | $this->error = $this->db->lasterror(); |
||
| 1227 | return -1; |
||
| 1228 | } |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * Delete a contact from database |
||
| 1233 | * |
||
| 1234 | * @param User $user User making the delete |
||
| 1235 | * @param int $notrigger Disable all trigger |
||
| 1236 | * @return int Return integer <0 if KO, >0 if OK |
||
| 1237 | */ |
||
| 1238 | public function delete($user, $notrigger = 0) |
||
| 1239 | { |
||
| 1240 | $error = 0; |
||
| 1241 | |||
| 1242 | $this->db->begin(); |
||
| 1243 | |||
| 1244 | if (!$error && !$notrigger) { |
||
| 1245 | // Call trigger |
||
| 1246 | $result = $this->call_trigger('CONTACT_DELETE', $user); |
||
| 1247 | if ($result < 0) { |
||
| 1248 | $error++; |
||
| 1249 | } |
||
| 1250 | // End call triggers |
||
| 1251 | } |
||
| 1252 | |||
| 1253 | if (!$error) { |
||
| 1254 | // Get all rowid of element_contact linked to a type that is link to llx_socpeople |
||
| 1255 | $sql = "SELECT ec.rowid"; |
||
| 1256 | $sql .= " FROM " . MAIN_DB_PREFIX . "element_contact ec,"; |
||
| 1257 | $sql .= " " . MAIN_DB_PREFIX . "c_type_contact tc"; |
||
| 1258 | $sql .= " WHERE ec.fk_socpeople=" . ((int) $this->id); |
||
| 1259 | $sql .= " AND ec.fk_c_type_contact=tc.rowid"; |
||
| 1260 | $sql .= " AND tc.source='external'"; |
||
| 1261 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1262 | $resql = $this->db->query($sql); |
||
| 1263 | if ($resql) { |
||
| 1264 | $num = $this->db->num_rows($resql); |
||
| 1265 | |||
| 1266 | $i = 0; |
||
| 1267 | while ($i < $num && !$error) { |
||
| 1268 | $obj = $this->db->fetch_object($resql); |
||
| 1269 | |||
| 1270 | $sqldel = "DELETE FROM " . MAIN_DB_PREFIX . "element_contact"; |
||
| 1271 | $sqldel .= " WHERE rowid = " . ((int) $obj->rowid); |
||
| 1272 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1273 | $result = $this->db->query($sqldel); |
||
| 1274 | if (!$result) { |
||
| 1275 | $error++; |
||
| 1276 | $this->error = $this->db->error() . ' sql=' . $sqldel; |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | $i++; |
||
| 1280 | } |
||
| 1281 | } else { |
||
| 1282 | $error++; |
||
| 1283 | $this->error = $this->db->error() . ' sql=' . $sql; |
||
| 1284 | } |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | if (!$error) { |
||
| 1288 | // Remove Roles |
||
| 1289 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_contacts WHERE fk_socpeople = " . ((int) $this->id); |
||
| 1290 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1291 | $resql = $this->db->query($sql); |
||
| 1292 | if (!$resql) { |
||
| 1293 | $error++; |
||
| 1294 | $this->error .= $this->db->lasterror(); |
||
| 1295 | $errorflag = -1; |
||
| 1296 | } |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | if (!$error) { |
||
| 1300 | // Remove Notifications |
||
| 1301 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "notify_def WHERE fk_contact = " . ((int) $this->id); |
||
| 1302 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1303 | $resql = $this->db->query($sql); |
||
| 1304 | if (!$resql) { |
||
| 1305 | $error++; |
||
| 1306 | $this->error .= $this->db->lasterror(); |
||
| 1307 | $errorflag = -1; |
||
| 1308 | } |
||
| 1309 | } |
||
| 1310 | |||
| 1311 | if (!$error) { |
||
| 1312 | // Remove category |
||
| 1313 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "categorie_contact WHERE fk_socpeople = " . ((int) $this->id); |
||
| 1314 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1315 | $resql = $this->db->query($sql); |
||
| 1316 | if (!$resql) { |
||
| 1317 | $error++; |
||
| 1318 | $this->error .= $this->db->lasterror(); |
||
| 1319 | $errorflag = -1; |
||
| 1320 | } |
||
| 1321 | } |
||
| 1322 | |||
| 1323 | if (!$error) { |
||
| 1324 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "socpeople"; |
||
| 1325 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 1326 | dol_syslog(__METHOD__, LOG_DEBUG); |
||
| 1327 | $result = $this->db->query($sql); |
||
| 1328 | if (!$result) { |
||
| 1329 | $error++; |
||
| 1330 | $this->error = $this->db->error() . ' sql=' . $sql; |
||
| 1331 | } |
||
| 1332 | } |
||
| 1333 | |||
| 1334 | // Remove extrafields |
||
| 1335 | if (!$error) { |
||
| 1336 | // For avoid conflicts if trigger used |
||
| 1337 | $result = $this->deleteExtraFields(); |
||
| 1338 | if ($result < 0) { |
||
| 1339 | $error++; |
||
| 1340 | } |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | if (!$error) { |
||
| 1344 | $this->db->commit(); |
||
| 1345 | return 1; |
||
| 1346 | } else { |
||
| 1347 | $this->db->rollback(); |
||
| 1348 | dol_syslog("Error " . $this->error, LOG_ERR); |
||
| 1349 | return -1; |
||
| 1350 | } |
||
| 1351 | } |
||
| 1352 | |||
| 1353 | |||
| 1354 | /** |
||
| 1355 | * Load contact information from the database |
||
| 1356 | * |
||
| 1357 | * @param int $id Id of the contact to load |
||
| 1358 | * @return void |
||
| 1359 | */ |
||
| 1360 | public function info($id) |
||
| 1361 | { |
||
| 1362 | $sql = "SELECT c.rowid, c.datec as datec, c.fk_user_creat,"; |
||
| 1363 | $sql .= " c.tms as tms, c.fk_user_modif"; |
||
| 1364 | $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as c"; |
||
| 1365 | $sql .= " WHERE c.rowid = " . ((int) $id); |
||
| 1366 | |||
| 1367 | $resql = $this->db->query($sql); |
||
| 1368 | if ($resql) { |
||
| 1369 | if ($this->db->num_rows($resql)) { |
||
| 1370 | $obj = $this->db->fetch_object($resql); |
||
| 1371 | |||
| 1372 | $this->id = $obj->rowid; |
||
| 1373 | |||
| 1374 | $this->user_creation_id = $obj->fk_user_creat; |
||
| 1375 | $this->user_modification_id = $obj->fk_user_modif; |
||
| 1376 | $this->date_creation = $this->db->jdate($obj->datec); |
||
| 1377 | $this->date_modification = $this->db->jdate($obj->tms); |
||
| 1378 | } |
||
| 1379 | |||
| 1380 | $this->db->free($resql); |
||
| 1381 | } else { |
||
| 1382 | print $this->db->error(); |
||
| 1383 | } |
||
| 1384 | } |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * Return number of mass Emailing received by these contacts with its email |
||
| 1388 | * |
||
| 1389 | * @return int Number of EMailings |
||
| 1390 | */ |
||
| 1391 | public function getNbOfEMailings() |
||
| 1392 | { |
||
| 1393 | $sql = "SELECT count(mc.email) as nb"; |
||
| 1394 | $sql .= " FROM " . MAIN_DB_PREFIX . "mailing_cibles as mc, " . MAIN_DB_PREFIX . "mailing as m"; |
||
| 1395 | $sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = '" . $this->db->escape($this->email) . "' "; |
||
| 1396 | $sql .= " AND m.entity IN (" . getEntity($this->element) . ") AND mc.statut NOT IN (-1,0)"; // -1 error, 0 not sent, 1 sent with success |
||
| 1397 | |||
| 1398 | $resql = $this->db->query($sql); |
||
| 1399 | if ($resql) { |
||
| 1400 | $obj = $this->db->fetch_object($resql); |
||
| 1401 | $nb = $obj->nb; |
||
| 1402 | |||
| 1403 | $this->db->free($resql); |
||
| 1404 | return $nb; |
||
| 1405 | } else { |
||
| 1406 | $this->error = $this->db->error(); |
||
| 1407 | return -1; |
||
| 1408 | } |
||
| 1409 | } |
||
| 1410 | |||
| 1411 | /** |
||
| 1412 | * getTooltipContentArray |
||
| 1413 | * @param array $params params to construct tooltip data |
||
| 1414 | * @since v18 |
||
| 1415 | * @return array |
||
| 1416 | */ |
||
| 1417 | public function getTooltipContentArray($params) |
||
| 1418 | { |
||
| 1419 | global $conf, $langs, $user; |
||
| 1420 | |||
| 1421 | $datas = []; |
||
| 1422 | |||
| 1423 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
| 1424 | return ['optimize' => $langs->trans("ShowContact")]; |
||
| 1425 | } |
||
| 1426 | if (!empty($this->photo) && class_exists('Form')) { |
||
| 1427 | $photo = '<div class="photointooltip floatright">'; |
||
| 1428 | $photo .= Form::showphoto('contact', $this, 0, 40, 0, 'photoref', 'mini', 0); // Important, we must force height so image will have height tags and if image is inside a tooltip, the tooltip manager can calculate height and position correctly the tooltip. |
||
| 1429 | $photo .= '</div>'; |
||
| 1430 | $datas['photo'] = $photo; |
||
| 1431 | } |
||
| 1432 | |||
| 1433 | $datas['picto'] = img_picto('', $this->picto) . ' <u class="paddingrightonly">' . $langs->trans("Contact") . '</u> ' . $this->getLibStatut(4); |
||
| 1434 | $datas['name'] = '<br><b>' . $langs->trans("Name") . ':</b> ' . $this->getFullName($langs); |
||
| 1435 | // if ($this->civility_id) $datas['civility'] = '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate civilty_id code |
||
| 1436 | if (!empty($this->poste)) { |
||
| 1437 | $datas['job'] = '<br><b>' . $langs->trans("Poste") . ':</b> ' . $this->poste; |
||
| 1438 | } |
||
| 1439 | $datas['email'] = '<br><b>' . $langs->trans("EMail") . ':</b> ' . $this->email; |
||
| 1440 | $phonelist = array(); |
||
| 1441 | $country_code = empty($this->country_code) ? '' : $this->country_code; |
||
| 1442 | if ($this->phone_pro) { |
||
| 1443 | $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', ' ', 'phone'); |
||
| 1444 | } |
||
| 1445 | if ($this->phone_mobile) { |
||
| 1446 | $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', ' ', 'mobile'); |
||
| 1447 | } |
||
| 1448 | if ($this->phone_perso) { |
||
| 1449 | $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', ' ', 'phone'); |
||
| 1450 | } |
||
| 1451 | $datas['phonelist'] = '<br><b>' . $langs->trans("Phone") . ':</b> ' . implode(' ', $phonelist); |
||
| 1452 | $datas['address'] = '<br><b>' . $langs->trans("Address") . ':</b> ' . dol_format_address($this, 1, ' ', $langs); |
||
| 1453 | |||
| 1454 | return $datas; |
||
| 1455 | } |
||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Return name of contact with link (and eventually picto) |
||
| 1459 | * Use $this->id, $this->lastname, $this->firstname, this->civility_id |
||
| 1460 | * |
||
| 1461 | * @param int $withpicto Include picto with link (0=no picto, 1=picto + name, 2=picto only, -1=photo+name, -2=photo only) |
||
| 1462 | * @param string $option Where the link point to |
||
| 1463 | * @param int $maxlen Max length of |
||
| 1464 | * @param string $moreparam Add more param into URL |
||
| 1465 | * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
||
| 1466 | * @param int $notooltip 1=Disable tooltip |
||
| 1467 | * @param string $morecss Add more css on link |
||
| 1468 | * @return string String with URL |
||
| 1469 | */ |
||
| 1470 | public function getNomUrl($withpicto = 0, $option = '', $maxlen = 0, $moreparam = '', $save_lastsearch_value = -1, $notooltip = 0, $morecss = '') |
||
| 1471 | { |
||
| 1472 | global $conf, $langs, $hookmanager; |
||
| 1473 | |||
| 1474 | if (!empty($conf->dol_no_mouse_hover)) { |
||
| 1475 | $notooltip = 1; // Force disable tooltips |
||
| 1476 | } |
||
| 1477 | |||
| 1478 | $result = ''; |
||
| 1479 | $params = [ |
||
| 1480 | 'id' => $this->id, |
||
| 1481 | 'objecttype' => $this->element, |
||
| 1482 | 'option' => $option, |
||
| 1483 | ]; |
||
| 1484 | $classfortooltip = 'classfortooltip'; |
||
| 1485 | $dataparams = ''; |
||
| 1486 | if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) { |
||
| 1487 | $classfortooltip = 'classforajaxtooltip'; |
||
| 1488 | $dataparams = ' data-params="' . dol_escape_htmltag(json_encode($params)) . '"'; |
||
| 1489 | $label = ''; |
||
| 1490 | } else { |
||
| 1491 | $label = implode($this->getTooltipContentArray($params)); |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | $url = constant('BASE_URL') . '/contact/card.php?id=' . $this->id; |
||
| 1495 | |||
| 1496 | if ($option !== 'nolink') { |
||
| 1497 | // Add param to save lastsearch_values or not |
||
| 1498 | $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
||
| 1499 | if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
||
| 1500 | $add_save_lastsearch_values = 1; |
||
| 1501 | } |
||
| 1502 | if ($url && $add_save_lastsearch_values) { |
||
| 1503 | $url .= '&save_lastsearch_values=1'; |
||
| 1504 | } |
||
| 1505 | } |
||
| 1506 | |||
| 1507 | $url .= $moreparam; |
||
| 1508 | |||
| 1509 | $linkclose = ''; |
||
| 1510 | if (empty($notooltip)) { |
||
| 1511 | if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) { |
||
| 1512 | $label = $langs->trans("ShowContact"); |
||
| 1513 | $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"'; |
||
| 1514 | } |
||
| 1515 | $linkclose .= ($label ? ' title="' . dol_escape_htmltag($label, 1) . '"' : ' title="tocomplete"'); |
||
| 1516 | $linkclose .= $dataparams . ' class="' . $classfortooltip . ($morecss ? ' ' . $morecss : '') . '"'; |
||
| 1517 | } else { |
||
| 1518 | $linkclose = ($morecss ? ' class="' . $morecss . '"' : ''); |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | if ($option == 'nolink' || empty($url)) { |
||
| 1522 | $linkstart = '<span'; |
||
| 1523 | } else { |
||
| 1524 | $linkstart = '<a href="' . $url . '"'; |
||
| 1525 | } |
||
| 1526 | $linkstart .= $linkclose . '>'; |
||
| 1527 | if ($option == 'nolink' || empty($url)) { |
||
| 1528 | $linkend = '</span>'; |
||
| 1529 | } else { |
||
| 1530 | $linkend = '</a>'; |
||
| 1531 | } |
||
| 1532 | |||
| 1533 | $result .= $linkstart; |
||
| 1534 | |||
| 1535 | if ($withpicto) { |
||
| 1536 | if ($withpicto < 0) { |
||
| 1537 | $result .= '<!-- picto photo contact --><span class="nopadding userimg' . ($morecss ? ' ' . $morecss : '') . '">' . Form::showphoto('contact', $this, 0, 0, 0, 'userphoto' . ($withpicto == -3 ? 'small' : ''), 'mini', 0, 1) . '</span>'; |
||
| 1538 | if ($withpicto != 2 && $withpicto != -2) { |
||
| 1539 | $result .= ' '; |
||
| 1540 | } |
||
| 1541 | } else { |
||
| 1542 | $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="pictofixedwidth valignmiddle"' : '') : 'class="' . (($withpicto != 2) ? 'pictofixedwidth valignmiddle' : '') . '"'), 0, 0, $notooltip ? 0 : 1); |
||
| 1543 | } |
||
| 1544 | } |
||
| 1545 | if ($withpicto != 2 && $withpicto != -2) { |
||
| 1546 | $result .= '<span class="valigmiddle">' . ($maxlen ? dol_trunc($this->getFullName($langs), $maxlen) : $this->getFullName($langs)) . '</span>'; |
||
| 1547 | } |
||
| 1548 | |||
| 1549 | $result .= $linkend; |
||
| 1550 | |||
| 1551 | global $action; |
||
| 1552 | $hookmanager->initHooks(array('contactdao')); |
||
| 1553 | $parameters = array('id' => $this->id, 'getnomurl' => &$result); |
||
| 1554 | $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 1555 | if ($reshook > 0) { |
||
| 1556 | $result = $hookmanager->resPrint; |
||
| 1557 | } else { |
||
| 1558 | $result .= $hookmanager->resPrint; |
||
| 1559 | } |
||
| 1560 | |||
| 1561 | return $result; |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Return civility label of contact |
||
| 1566 | * |
||
| 1567 | * @return string Translated name of civility |
||
| 1568 | */ |
||
| 1569 | public function getCivilityLabel() |
||
| 1570 | { |
||
| 1571 | global $langs; |
||
| 1572 | |||
| 1573 | $code = ($this->civility_code ? $this->civility_code : (!empty($this->civility_id) ? $this->civility : (!empty($this->civilite) ? $this->civilite : ''))); |
||
| 1574 | if (empty($code)) { |
||
| 1575 | return ''; |
||
| 1576 | } |
||
| 1577 | |||
| 1578 | $langs->load("dict"); |
||
| 1579 | return $langs->getLabelFromKey($this->db, "Civility" . $code, "c_civility", "code", "label", $code); |
||
| 1580 | } |
||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * Return the label of the status |
||
| 1584 | * |
||
| 1585 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 1586 | * @return string Label of status |
||
| 1587 | */ |
||
| 1588 | public function getLibStatut($mode) |
||
| 1589 | { |
||
| 1590 | return $this->LibStatut($this->statut, $mode); |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1594 | /** |
||
| 1595 | * Return the label of a given status |
||
| 1596 | * |
||
| 1597 | * @param int $status Id status |
||
| 1598 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
||
| 1599 | * @return string Label of status |
||
| 1600 | */ |
||
| 1601 | public function LibStatut($status, $mode) |
||
| 1602 | { |
||
| 1603 | // phpcs:enable |
||
| 1604 | global $langs; |
||
| 1605 | |||
| 1606 | $labelStatus = array( |
||
| 1607 | 0 => 'ActivityCeased', |
||
| 1608 | 1 => 'InActivity', |
||
| 1609 | 4 => 'InActivity', |
||
| 1610 | 5 => 'ActivityCeased', |
||
| 1611 | ); |
||
| 1612 | $labelStatusShort = array( |
||
| 1613 | 0 => 'ActivityCeased', |
||
| 1614 | 1 => 'InActivity', |
||
| 1615 | 4 => 'InActivity', |
||
| 1616 | 5 => 'ActivityCeased', |
||
| 1617 | ); |
||
| 1618 | |||
| 1619 | $statusType = 'status4'; |
||
| 1620 | if ($status == 0 || $status == 5) { |
||
| 1621 | $statusType = 'status5'; |
||
| 1622 | } |
||
| 1623 | |||
| 1624 | $label = $langs->transnoentitiesnoconv($labelStatus[$status]); |
||
| 1625 | $labelshort = $langs->transnoentitiesnoconv($labelStatusShort[$status]); |
||
| 1626 | |||
| 1627 | return dolGetStatus($label, $labelshort, '', $statusType, $mode); |
||
| 1628 | } |
||
| 1629 | |||
| 1630 | |||
| 1631 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 1632 | /** |
||
| 1633 | * Return translated label of Public or Private |
||
| 1634 | * |
||
| 1635 | * @param int $status Type (0 = public, 1 = private) |
||
| 1636 | * @return string Label translated |
||
| 1637 | */ |
||
| 1638 | public function LibPubPriv($status) |
||
| 1639 | { |
||
| 1640 | // phpcs:enable |
||
| 1641 | global $langs; |
||
| 1642 | if ($status == '1') { |
||
| 1643 | return $langs->trans('ContactPrivate'); |
||
| 1644 | } else { |
||
| 1645 | return $langs->trans('ContactPublic'); |
||
| 1646 | } |
||
| 1647 | } |
||
| 1648 | |||
| 1649 | |||
| 1650 | /** |
||
| 1651 | * Initialise an instance with random values. |
||
| 1652 | * Used to build previews or test instances. |
||
| 1653 | * id must be 0 if object instance is a specimen. |
||
| 1654 | * |
||
| 1655 | * @return int >0 if ok |
||
| 1656 | */ |
||
| 1657 | public function initAsSpecimen() |
||
| 1658 | { |
||
| 1659 | // Get first id of existing company and save it into $socid |
||
| 1660 | $socid = 0; |
||
| 1661 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "societe ORDER BY rowid LIMIT 1"; |
||
| 1662 | $resql = $this->db->query($sql); |
||
| 1663 | if ($resql) { |
||
| 1664 | $obj = $this->db->fetch_object($resql); |
||
| 1665 | if ($obj) { |
||
| 1666 | $socid = $obj->rowid; |
||
| 1667 | } |
||
| 1668 | } |
||
| 1669 | |||
| 1670 | // Initialise parameters |
||
| 1671 | $this->id = 0; |
||
| 1672 | $this->entity = 1; |
||
| 1673 | $this->specimen = 1; |
||
| 1674 | $this->lastname = 'DOLIBARR'; |
||
| 1675 | $this->firstname = 'SPECIMEN'; |
||
| 1676 | $this->address = '21 jump street'; |
||
| 1677 | $this->zip = '99999'; |
||
| 1678 | $this->town = 'MyTown'; |
||
| 1679 | $this->country_id = 1; |
||
| 1680 | $this->country_code = 'FR'; |
||
| 1681 | $this->country = 'France'; |
||
| 1682 | $this->email = '[email protected]'; |
||
| 1683 | $this->socialnetworks = array( |
||
| 1684 | 'skype' => 'tom.hanson', |
||
| 1685 | 'twitter' => 'tomhanson', |
||
| 1686 | 'linkedin' => 'tomhanson', |
||
| 1687 | ); |
||
| 1688 | $this->phone_pro = '0909090901'; |
||
| 1689 | $this->phone_perso = '0909090902'; |
||
| 1690 | $this->phone_mobile = '0909090903'; |
||
| 1691 | $this->fax = '0909090909'; |
||
| 1692 | |||
| 1693 | $this->note_public = 'This is a comment (public)'; |
||
| 1694 | $this->note_private = 'This is a comment (private)'; |
||
| 1695 | |||
| 1696 | $this->socid = $socid; |
||
| 1697 | $this->statut = 1; |
||
| 1698 | |||
| 1699 | return 1; |
||
| 1700 | } |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * Change status of a user |
||
| 1704 | * |
||
| 1705 | * @param int $status Status to set |
||
| 1706 | * @return int Return integer <0 if KO, 0 if nothing is done, >0 if OK |
||
| 1707 | */ |
||
| 1708 | public function setstatus($status) |
||
| 1709 | { |
||
| 1710 | global $conf, $langs, $user; |
||
| 1711 | |||
| 1712 | $error = 0; |
||
| 1713 | |||
| 1714 | // Check parameters |
||
| 1715 | if ($this->statut == $status) { |
||
| 1716 | return 0; |
||
| 1717 | } else { |
||
| 1718 | $this->statut = $status; |
||
| 1719 | } |
||
| 1720 | |||
| 1721 | $this->db->begin(); |
||
| 1722 | |||
| 1723 | // User disable |
||
| 1724 | $sql = "UPDATE " . MAIN_DB_PREFIX . "socpeople"; |
||
| 1725 | $sql .= " SET statut = " . ((int) $this->statut); |
||
| 1726 | $sql .= ", fk_user_modif = " . ((int) $user->id); |
||
| 1727 | $sql .= " WHERE rowid = " . ((int) $this->id); |
||
| 1728 | $result = $this->db->query($sql); |
||
| 1729 | |||
| 1730 | dol_syslog(get_class($this) . "::setstatus", LOG_DEBUG); |
||
| 1731 | if ($result) { |
||
| 1732 | // Call trigger |
||
| 1733 | $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user); |
||
| 1734 | if ($result < 0) { |
||
| 1735 | $error++; |
||
| 1736 | } |
||
| 1737 | // End call triggers |
||
| 1738 | } |
||
| 1739 | |||
| 1740 | if ($error) { |
||
| 1741 | $this->db->rollback(); |
||
| 1742 | return -$error; |
||
| 1743 | } else { |
||
| 1744 | $this->db->commit(); |
||
| 1745 | return 1; |
||
| 1746 | } |
||
| 1747 | } |
||
| 1748 | |||
| 1749 | /** |
||
| 1750 | * Sets object to supplied categories. |
||
| 1751 | * |
||
| 1752 | * Deletes object from existing categories not supplied. |
||
| 1753 | * Adds it to non existing supplied categories. |
||
| 1754 | * Existing categories are left untouch. |
||
| 1755 | * |
||
| 1756 | * @param int[]|int $categories Category or categories IDs |
||
| 1757 | * @return int Return integer <0 if KO, >0 if OK |
||
| 1758 | */ |
||
| 1759 | public function setCategories($categories) |
||
| 1760 | { |
||
| 1761 | require_once constant('DOL_DOCUMENT_ROOT') . '/categories/class/categorie.class.php'; |
||
| 1762 | return parent::setCategoriesCommon($categories, Categorie::TYPE_CONTACT); |
||
| 1763 | } |
||
| 1764 | |||
| 1765 | /** |
||
| 1766 | * Function used to replace a thirdparty id with another one. |
||
| 1767 | * |
||
| 1768 | * @param DoliDB $dbs Database handler, because function is static we name it $dbs not $db to avoid breaking coding test |
||
| 1769 | * @param int $origin_id Old thirdparty id |
||
| 1770 | * @param int $dest_id New thirdparty id |
||
| 1771 | * @return bool |
||
| 1772 | */ |
||
| 1773 | public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id) |
||
| 1780 | } |
||
| 1781 | |||
| 1782 | /** |
||
| 1783 | * Fetch roles (default contact of some companies) for the current contact. |
||
| 1784 | * This load the array ->roles. |
||
| 1785 | * |
||
| 1786 | * @return int Return integer <0 if KO, Nb of roles found if OK |
||
| 1787 | * @see updateRoles() |
||
| 1788 | */ |
||
| 1789 | public function fetchRoles() |
||
| 1790 | { |
||
| 1791 | global $langs; |
||
| 1792 | $error = 0; |
||
| 1793 | $num = 0; |
||
| 1794 | |||
| 1795 | $sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid"; |
||
| 1796 | $sql .= " FROM " . MAIN_DB_PREFIX . "societe_contacts as sc, " . MAIN_DB_PREFIX . "c_type_contact as tc"; |
||
| 1797 | $sql .= " WHERE tc.rowid = sc.fk_c_type_contact"; |
||
| 1798 | $sql .= " AND tc.source = 'external' AND tc.active=1"; |
||
| 1799 | $sql .= " AND sc.fk_socpeople = " . ((int) $this->id); |
||
| 1800 | $sql .= " AND sc.entity IN (" . getEntity('societe') . ')'; |
||
| 1801 | |||
| 1802 | $resql = $this->db->query($sql); |
||
| 1803 | if ($resql) { |
||
| 1804 | $this->roles = array(); |
||
| 1805 | |||
| 1806 | $num = $this->db->num_rows($resql); |
||
| 1807 | if ($num > 0) { |
||
| 1808 | while ($obj = $this->db->fetch_object($resql)) { |
||
| 1809 | $transkey = "TypeContact_" . $obj->element . "_" . $obj->source . "_" . $obj->code; |
||
| 1810 | $libelle_element = $langs->trans('ContactDefault_' . $obj->element); |
||
| 1811 | $this->roles[$obj->contactroleid] = array('id' => $obj->rowid, 'socid' => $obj->socid, 'element' => $obj->element, 'source' => $obj->source, 'code' => $obj->code, 'label' => $libelle_element . ' - ' . ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->label)); |
||
| 1812 | } |
||
| 1813 | } |
||
| 1814 | } else { |
||
| 1815 | $error++; |
||
| 1816 | $this->error = $this->db->lasterror(); |
||
| 1817 | $this->errors[] = $this->db->lasterror(); |
||
| 1818 | } |
||
| 1819 | |||
| 1820 | if (empty($error)) { |
||
| 1821 | return $num; |
||
| 1822 | } else { |
||
| 1823 | return $error * -1; |
||
| 1824 | } |
||
| 1825 | } |
||
| 1826 | |||
| 1827 | /** |
||
| 1828 | * Get thirdparty contact roles of a given contact |
||
| 1829 | * |
||
| 1830 | * @param string $element Element type |
||
| 1831 | * @return array|int Array of contact roles or -1 |
||
| 1832 | * @throws Exception |
||
| 1833 | */ |
||
| 1834 | public function getContactRoles($element = '') |
||
| 1870 | } |
||
| 1871 | } |
||
| 1872 | |||
| 1873 | /** |
||
| 1874 | * Updates all roles (default contact for companies) according to values inside the ->roles array. |
||
| 1875 | * This is called by update of contact. |
||
| 1876 | * |
||
| 1877 | * @return int |
||
| 1878 | * @see fetchRoles() |
||
| 1879 | */ |
||
| 1880 | public function updateRoles() |
||
| 1881 | { |
||
| 1882 | global $conf; |
||
| 1883 | |||
| 1884 | $error = 0; |
||
| 1885 | |||
| 1886 | if (!isset($this->roles)) { |
||
| 1887 | return 0; // Avoid to loose roles when property not set |
||
| 1888 | } |
||
| 1889 | |||
| 1890 | $this->db->begin(); |
||
| 1891 | |||
| 1892 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "societe_contacts WHERE fk_socpeople=" . ((int) $this->id) . " AND entity IN (" . getEntity("contact") . ")"; |
||
| 1893 | |||
| 1894 | $result = $this->db->query($sql); |
||
| 1895 | if (!$result) { |
||
| 1896 | $this->errors[] = $this->db->lasterror() . ' sql=' . $sql; |
||
| 1897 | $error++; |
||
| 1898 | } else { |
||
| 1899 | if (count($this->roles) > 0) { |
||
| 1900 | foreach ($this->roles as $keyRoles => $valRoles) { |
||
| 1901 | $idrole = 0; |
||
| 1902 | if (is_array($valRoles)) { |
||
| 1903 | $idrole = $valRoles['id']; |
||
| 1904 | } else { |
||
| 1905 | $idrole = $valRoles; |
||
| 1906 | } |
||
| 1907 | |||
| 1908 | $socid = 0; |
||
| 1909 | if (is_array($valRoles)) { |
||
| 1910 | $socid = $valRoles['socid']; |
||
| 1911 | } else { |
||
| 1912 | $socid = $this->socid; |
||
| 1913 | } |
||
| 1914 | |||
| 1915 | if ($socid > 0) { |
||
| 1916 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_contacts"; |
||
| 1917 | $sql .= " (entity,"; |
||
| 1918 | $sql .= "date_creation,"; |
||
| 1919 | $sql .= "fk_soc,"; |
||
| 1920 | $sql .= "fk_c_type_contact,"; |
||
| 1921 | $sql .= "fk_socpeople) "; |
||
| 1922 | $sql .= " VALUES (" . $conf->entity . ","; |
||
| 1923 | $sql .= "'" . $this->db->idate(dol_now()) . "',"; |
||
| 1924 | $sql .= $socid . ", "; |
||
| 1925 | $sql .= $idrole . " , "; |
||
| 1926 | $sql .= $this->id; |
||
| 1927 | $sql .= ")"; |
||
| 1928 | |||
| 1929 | $result = $this->db->query($sql); |
||
| 1930 | if (!$result) { |
||
| 1931 | $this->errors[] = $this->db->lasterror() . ' sql=' . $sql; |
||
| 1932 | $error++; |
||
| 1933 | } |
||
| 1934 | } |
||
| 1935 | } |
||
| 1936 | } |
||
| 1937 | } |
||
| 1938 | if (empty($error)) { |
||
| 1939 | $this->db->commit(); |
||
| 1940 | return 1; |
||
| 1941 | } else { |
||
| 1942 | $this->error = implode(' ', $this->errors); |
||
| 1943 | $this->db->rollback(); |
||
| 1944 | return $error * -1; |
||
| 1945 | } |
||
| 1946 | } |
||
| 1947 | |||
| 1948 | /** |
||
| 1949 | * Load array of prospect status |
||
| 1950 | * |
||
| 1951 | * @param int $active 1=Active only, 0=Not active only, -1=All |
||
| 1952 | * @return int Return integer <0 if KO, >0 if OK |
||
| 1953 | */ |
||
| 1954 | public function loadCacheOfProspStatus($active = 1) |
||
| 1955 | { |
||
| 1956 | global $langs; |
||
| 1957 | |||
| 1958 | $sql = "SELECT id, code, libelle as label, picto FROM " . MAIN_DB_PREFIX . "c_stcommcontact"; |
||
| 1959 | if ($active >= 0) { |
||
| 1960 | $sql .= " WHERE active = " . ((int) $active); |
||
| 1961 | } |
||
| 1962 | $resql = $this->db->query($sql); |
||
| 1963 | $num = $this->db->num_rows($resql); |
||
| 1964 | $i = 0; |
||
| 1965 | while ($i < $num) { |
||
| 1966 | $obj = $this->db->fetch_object($resql); |
||
| 1967 | $this->cacheprospectstatus[$obj->id] = array('id' => $obj->id, 'code' => $obj->code, 'label' => ($langs->trans("ST_" . strtoupper($obj->code)) == "ST_" . strtoupper($obj->code)) ? $obj->label : $langs->trans("ST_" . strtoupper($obj->code)), 'picto' => $obj->picto); |
||
| 1968 | $i++; |
||
| 1969 | } |
||
| 1970 | return 1; |
||
| 1971 | } |
||
| 1972 | |||
| 1973 | /** |
||
| 1974 | * Return prospect level |
||
| 1975 | * |
||
| 1976 | * @return string Label |
||
| 1977 | */ |
||
| 1978 | public function getLibProspLevel() |
||
| 1979 | { |
||
| 1980 | return $this->libProspLevel($this->fk_prospectlevel); |
||
| 1981 | } |
||
| 1982 | |||
| 1983 | /** |
||
| 1984 | * Return label of prospect level |
||
| 1985 | * |
||
| 1986 | * @param string $fk_prospectlevel Prospect level |
||
| 1987 | * @return string label of level |
||
| 1988 | */ |
||
| 1989 | public function libProspLevel($fk_prospectlevel) |
||
| 1990 | { |
||
| 1991 | global $langs; |
||
| 1992 | |||
| 1993 | $lib = $langs->trans("ProspectLevel" . $fk_prospectlevel); |
||
| 1994 | // If lib not found in language file, we get label from cache/database |
||
| 1995 | if ($lib == "ProspectLevel" . $fk_prospectlevel) { |
||
| 1996 | $lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label'); |
||
| 1997 | } |
||
| 1998 | return $lib; |
||
| 1999 | } |
||
| 2000 | |||
| 2001 | |||
| 2002 | /** |
||
| 2003 | * Set prospect level |
||
| 2004 | * |
||
| 2005 | * @param User $user User who defines the discount |
||
| 2006 | * @return int Return integer <0 if KO, >0 if OK |
||
| 2007 | * @deprecated Use update function instead |
||
| 2008 | */ |
||
| 2009 | public function setProspectLevel(User $user) |
||
| 2010 | { |
||
| 2011 | return $this->update($this->id, $user); |
||
| 2012 | } |
||
| 2013 | |||
| 2014 | /** |
||
| 2015 | * Return status of prospect |
||
| 2016 | * |
||
| 2017 | * @param int $mode 0=label long, 1=label short, 2=Picto + Label short, 3=Picto, 4=Picto + Label long |
||
| 2018 | * @param string $label Label to use for status for added status |
||
| 2019 | * @return string Label |
||
| 2020 | */ |
||
| 2021 | public function getLibProspCommStatut($mode = 0, $label = '') |
||
| 2022 | { |
||
| 2023 | return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto); |
||
| 2024 | } |
||
| 2025 | |||
| 2026 | /** |
||
| 2027 | * Return label of a given status |
||
| 2028 | * |
||
| 2029 | * @param int|string $statut Id or code for prospection status |
||
| 2030 | * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto |
||
| 2031 | * @param string $label Label to use for status for added status |
||
| 2032 | * @param string $picto Name of image file to show ('filenew', ...) |
||
| 2033 | * If no extension provided, we use '.png'. Image must be stored into theme/xxx/img directory. |
||
| 2034 | * Example: picto.png if picto.png is stored into htdocs/theme/mytheme/img |
||
| 2035 | * Example: picto.png@mymodule if picto.png is stored into htdocs/mymodule/img |
||
| 2036 | * Example: /mydir/mysubdir/picto.png if picto.png is stored into htdocs/mydir/mysubdir (pictoisfullpath must be set to 1) |
||
| 2037 | * @return string Label of status |
||
| 2038 | */ |
||
| 2039 | public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '') |
||
| 2040 | { |
||
| 2041 | global $langs; |
||
| 2042 | $langs->load('customers'); |
||
| 2043 | |||
| 2044 | if ($mode == 2) { |
||
| 2045 | if ($statut == '-1' || $statut == 'ST_NO') { |
||
| 2046 | return img_action($langs->trans("StatusProspect-1"), -1, $picto) . ' ' . $langs->trans("StatusProspect-1"); |
||
| 2047 | } elseif ($statut == '0' || $statut == 'ST_NEVER') { |
||
| 2048 | return img_action($langs->trans("StatusProspect0"), 0, $picto) . ' ' . $langs->trans("StatusProspect0"); |
||
| 2049 | } elseif ($statut == '1' || $statut == 'ST_TODO') { |
||
| 2050 | return img_action($langs->trans("StatusProspect1"), 1, $picto) . ' ' . $langs->trans("StatusProspect1"); |
||
| 2051 | } elseif ($statut == '2' || $statut == 'ST_PEND') { |
||
| 2052 | return img_action($langs->trans("StatusProspect2"), 2, $picto) . ' ' . $langs->trans("StatusProspect2"); |
||
| 2053 | } elseif ($statut == '3' || $statut == 'ST_DONE') { |
||
| 2054 | return img_action($langs->trans("StatusProspect3"), 3, $picto) . ' ' . $langs->trans("StatusProspect3"); |
||
| 2055 | } else { |
||
| 2056 | return img_action(($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label, 0, $picto) . ' ' . (($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label); |
||
| 2057 | } |
||
| 2058 | } |
||
| 2059 | if ($mode == 3) { |
||
| 2060 | if ($statut == '-1' || $statut == 'ST_NO') { |
||
| 2061 | return img_action($langs->trans("StatusProspect-1"), -1, $picto); |
||
| 2062 | } elseif ($statut == '0' || $statut == 'ST_NEVER') { |
||
| 2063 | return img_action($langs->trans("StatusProspect0"), 0, $picto); |
||
| 2064 | } elseif ($statut == '1' || $statut == 'ST_TODO') { |
||
| 2065 | return img_action($langs->trans("StatusProspect1"), 1, $picto); |
||
| 2066 | } elseif ($statut == '2' || $statut == 'ST_PEND') { |
||
| 2067 | return img_action($langs->trans("StatusProspect2"), 2, $picto); |
||
| 2068 | } elseif ($statut == '3' || $statut == 'ST_DONE') { |
||
| 2069 | return img_action($langs->trans("StatusProspect3"), 3, $picto); |
||
| 2070 | } else { |
||
| 2071 | return img_action(($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label, 0, $picto); |
||
| 2072 | } |
||
| 2073 | } |
||
| 2074 | if ($mode == 4) { |
||
| 2075 | if ($statut == '-1' || $statut == 'ST_NO') { |
||
| 2076 | return img_action($langs->trans("StatusProspect-1"), -1, $picto) . ' ' . $langs->trans("StatusProspect-1"); |
||
| 2077 | } elseif ($statut == '0' || $statut == 'ST_NEVER') { |
||
| 2078 | return img_action($langs->trans("StatusProspect0"), 0, $picto) . ' ' . $langs->trans("StatusProspect0"); |
||
| 2079 | } elseif ($statut == '1' || $statut == 'ST_TODO') { |
||
| 2080 | return img_action($langs->trans("StatusProspect1"), 1, $picto) . ' ' . $langs->trans("StatusProspect1"); |
||
| 2081 | } elseif ($statut == '2' || $statut == 'ST_PEND') { |
||
| 2082 | return img_action($langs->trans("StatusProspect2"), 2, $picto) . ' ' . $langs->trans("StatusProspect2"); |
||
| 2083 | } elseif ($statut == '3' || $statut == 'ST_DONE') { |
||
| 2084 | return img_action($langs->trans("StatusProspect3"), 3, $picto) . ' ' . $langs->trans("StatusProspect3"); |
||
| 2085 | } else { |
||
| 2086 | return img_action(($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label, 0, $picto) . ' ' . (($langs->trans("StatusProspect" . $statut) != "StatusProspect" . $statut) ? $langs->trans("StatusProspect" . $statut) : $label); |
||
| 2087 | } |
||
| 2088 | } |
||
| 2089 | |||
| 2090 | return "Error, mode/status not found"; |
||
| 2091 | } |
||
| 2092 | |||
| 2093 | |||
| 2094 | /** |
||
| 2095 | * Set "blacklist" mailing status |
||
| 2096 | * |
||
| 2097 | * @param int $no_email 1=Do not send mailing, 0=Ok to receive mailing |
||
| 2098 | * @return int Return integer <0 if KO, >0 if OK |
||
| 2099 | */ |
||
| 2100 | public function setNoEmail($no_email) |
||
| 2101 | { |
||
| 2102 | $error = 0; |
||
| 2103 | |||
| 2104 | // Update mass emailing flag into table mailing_unsubscribe |
||
| 2105 | if ($this->email) { |
||
| 2106 | $this->db->begin(); |
||
| 2107 | |||
| 2108 | if ($no_email) { |
||
| 2109 | $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe WHERE entity IN (" . getEntity('mailing', 0) . ") AND email = '" . $this->db->escape($this->email) . "'"; |
||
| 2110 | $resql = $this->db->query($sql); |
||
| 2111 | if ($resql) { |
||
| 2112 | $obj = $this->db->fetch_object($resql); |
||
| 2113 | $noemail = $obj->nb; |
||
| 2114 | if (empty($noemail)) { |
||
| 2115 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "mailing_unsubscribe(email, entity, date_creat) VALUES ('" . $this->db->escape($this->email) . "', " . getEntity('mailing', 0) . ", '" . $this->db->idate(dol_now()) . "')"; |
||
| 2116 | $resql = $this->db->query($sql); |
||
| 2117 | if (!$resql) { |
||
| 2118 | $error++; |
||
| 2119 | $this->error = $this->db->lasterror(); |
||
| 2120 | $this->errors[] = $this->error; |
||
| 2121 | } |
||
| 2122 | } |
||
| 2123 | } else { |
||
| 2124 | $error++; |
||
| 2125 | $this->error = $this->db->lasterror(); |
||
| 2126 | $this->errors[] = $this->error; |
||
| 2127 | } |
||
| 2128 | } else { |
||
| 2129 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe WHERE email = '" . $this->db->escape($this->email) . "' AND entity IN (" . getEntity('mailing', 0) . ")"; |
||
| 2130 | $resql = $this->db->query($sql); |
||
| 2131 | if (!$resql) { |
||
| 2132 | $error++; |
||
| 2133 | $this->error = $this->db->lasterror(); |
||
| 2134 | $this->errors[] = $this->error; |
||
| 2135 | } |
||
| 2136 | } |
||
| 2137 | |||
| 2138 | if (empty($error)) { |
||
| 2139 | $this->no_email = $no_email; |
||
| 2140 | $this->db->commit(); |
||
| 2141 | return 1; |
||
| 2142 | } else { |
||
| 2143 | $this->db->rollback(); |
||
| 2144 | return $error * -1; |
||
| 2145 | } |
||
| 2146 | } |
||
| 2147 | |||
| 2148 | return 0; |
||
| 2149 | } |
||
| 2150 | |||
| 2151 | /** |
||
| 2152 | * get "blacklist" mailing status |
||
| 2153 | * set no_email attribute to 1 or 0 |
||
| 2154 | * |
||
| 2155 | * @return int Return integer <0 if KO, >0 if OK |
||
| 2156 | */ |
||
| 2157 | public function getNoEmail() |
||
| 2158 | { |
||
| 2159 | if ($this->email) { |
||
| 2160 | $sql = "SELECT COUNT(rowid) as nb FROM " . MAIN_DB_PREFIX . "mailing_unsubscribe WHERE entity IN (" . getEntity('mailing') . ") AND email = '" . $this->db->escape($this->email) . "'"; |
||
| 2161 | $resql = $this->db->query($sql); |
||
| 2162 | if ($resql) { |
||
| 2163 | $obj = $this->db->fetch_object($resql); |
||
| 2164 | $this->no_email = $obj->nb; |
||
| 2165 | return 1; |
||
| 2166 | } else { |
||
| 2167 | $this->error = $this->db->lasterror(); |
||
| 2168 | $this->errors[] = $this->error; |
||
| 2169 | return -1; |
||
| 2170 | } |
||
| 2171 | } |
||
| 2172 | return 0; |
||
| 2173 | } |
||
| 2174 | |||
| 2175 | |||
| 2176 | /** |
||
| 2177 | * Return clickable link of object (with eventually picto) |
||
| 2178 | * |
||
| 2179 | * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link) |
||
| 2180 | * @param array $arraydata Array of data |
||
| 2181 | * @return string HTML Code for Kanban thumb. |
||
| 2182 | */ |
||
| 2183 | public function getKanbanView($option = '', $arraydata = null) |
||
| 2220 | } |
||
| 2221 | } |
||
| 2222 |