| Conditions | 220 |
| Paths | 0 |
| Total Lines | 850 |
| Code Lines | 554 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 181 | public function card() |
||
| 182 | { |
||
| 183 | global $conf; |
||
| 184 | global $db; |
||
| 185 | global $user; |
||
| 186 | global $hookmanager; |
||
| 187 | global $user; |
||
| 188 | global $menumanager; |
||
| 189 | global $langs; |
||
| 190 | global $mysoc; |
||
| 191 | |||
| 192 | // Load translation files required by the page |
||
| 193 | $langs->loadLangs(["companies", "bills", "members", "users", "other", "paypal"]); |
||
| 194 | |||
| 195 | |||
| 196 | // Get parameters |
||
| 197 | $action = GETPOST('action', 'aZ09'); |
||
| 198 | $cancel = GETPOST('cancel', 'alpha'); |
||
| 199 | $backtopage = GETPOST('backtopage', 'alpha'); |
||
| 200 | $confirm = GETPOST('confirm', 'alpha'); |
||
| 201 | $rowid = GETPOSTINT('rowid'); |
||
| 202 | $id = GETPOST('id') ? GETPOSTINT('id') : $rowid; |
||
| 203 | $typeid = GETPOSTINT('typeid'); |
||
| 204 | $userid = GETPOSTINT('userid'); |
||
| 205 | $socid = GETPOSTINT('socid'); |
||
| 206 | $ref = GETPOST('ref', 'alpha'); |
||
| 207 | |||
| 208 | if (isModEnabled('mailmanspip')) { |
||
| 209 | include_once DOL_DOCUMENT_ROOT . '/mailmanspip/class/mailmanspip.class.php'; |
||
| 210 | |||
| 211 | $langs->load('mailmanspip'); |
||
| 212 | |||
| 213 | $mailmanspip = new MailmanSpip($db); |
||
| 214 | } |
||
| 215 | |||
| 216 | $object = new Adherent($db); |
||
| 217 | $extrafields = new ExtraFields($db); |
||
| 218 | |||
| 219 | // fetch optionals attributes and labels |
||
| 220 | $extrafields->fetch_name_optionals_label($object->table_element); |
||
| 221 | |||
| 222 | $socialnetworks = getArrayOfSocialNetworks(); |
||
| 223 | |||
| 224 | // Get object canvas (By default, this is not defined, so standard usage of dolibarr) |
||
| 225 | $object->getCanvas($id); |
||
| 226 | $canvas = $object->canvas ? $object->canvas : GETPOST("canvas"); |
||
| 227 | $objcanvas = null; |
||
| 228 | if (!empty($canvas)) { |
||
| 229 | require_once DOL_DOCUMENT_ROOT . '/core/class/canvas.class.php'; |
||
| 230 | $objcanvas = new Canvas($db, $action); |
||
| 231 | $objcanvas->getCanvas('adherent', 'membercard', $canvas); |
||
| 232 | } |
||
| 233 | |||
| 234 | // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context |
||
| 235 | $hookmanager->initHooks(['membercard', 'globalcard']); |
||
| 236 | |||
| 237 | // Fetch object |
||
| 238 | if ($id > 0 || !empty($ref)) { |
||
| 239 | // Load member |
||
| 240 | $result = $object->fetch($id, $ref); |
||
| 241 | |||
| 242 | // Define variables to know what current user can do on users |
||
| 243 | $canadduser = ($user->admin || $user->hasRight('user', 'user', 'creer')); |
||
| 244 | // Define variables to know what current user can do on properties of user linked to edited member |
||
| 245 | if ($object->user_id) { |
||
| 246 | // $User is the user who edits, $object->user_id is the id of the related user in the edited member |
||
| 247 | $caneditfielduser = ((($user->id == $object->user_id) && $user->hasRight('user', 'self', 'creer')) |
||
| 248 | || (($user->id != $object->user_id) && $user->hasRight('user', 'user', 'creer'))); |
||
| 249 | $caneditpassworduser = ((($user->id == $object->user_id) && $user->hasRight('user', 'self', 'password')) |
||
| 250 | || (($user->id != $object->user_id) && $user->hasRight('user', 'user', 'password'))); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | // Define variables to determine what the current user can do on the members |
||
| 255 | $canaddmember = $user->hasRight('adherent', 'creer'); |
||
| 256 | // Define variables to determine what the current user can do on the properties of a member |
||
| 257 | if ($id) { |
||
| 258 | $caneditfieldmember = $user->hasRight('adherent', 'creer'); |
||
| 259 | } |
||
| 260 | |||
| 261 | // Security check |
||
| 262 | $result = restrictedArea($user, 'adherent', $object->id, '', '', 'socid', 'rowid', 0); |
||
| 263 | |||
| 264 | if (!$user->hasRight('adherent', 'creer') && $action == 'edit') { |
||
| 265 | accessforbidden('Not enough permission'); |
||
| 266 | } |
||
| 267 | |||
| 268 | $linkofpubliclist = DOL_MAIN_URL_ROOT . '/public/members/public_list.php' . ((isModEnabled('multicompany')) ? '?entity=' . $conf->entity : ''); |
||
| 269 | |||
| 270 | |||
| 271 | /* |
||
| 272 | * Actions |
||
| 273 | */ |
||
| 274 | |||
| 275 | $parameters = ['id' => $id, 'rowid' => $id, 'objcanvas' => $objcanvas, 'confirm' => $confirm]; |
||
| 276 | $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 277 | if ($reshook < 0) { |
||
| 278 | setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); |
||
| 279 | } |
||
| 280 | |||
| 281 | if (empty($reshook)) { |
||
| 282 | $backurlforlist = '/adherents/list.php'; |
||
| 283 | |||
| 284 | if (empty($backtopage) || ($cancel && empty($id))) { |
||
| 285 | if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { |
||
| 286 | if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { |
||
| 287 | $backtopage = $backurlforlist; |
||
| 288 | } else { |
||
| 289 | $backtopage = '/adherents/card.php?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__'); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | if ($cancel) { |
||
| 295 | if (!empty($backtopageforcancel)) { |
||
| 296 | header("Location: " . $backtopageforcancel); |
||
| 297 | exit; |
||
| 298 | } elseif (!empty($backtopage)) { |
||
| 299 | header("Location: " . $backtopage); |
||
| 300 | exit; |
||
| 301 | } |
||
| 302 | $action = ''; |
||
| 303 | } |
||
| 304 | |||
| 305 | if ($action == 'setuserid' && ($user->hasRight('user', 'self', 'creer') || $user->hasRight('user', 'user', 'creer'))) { |
||
| 306 | $error = 0; |
||
| 307 | if (!$user->hasRight('user', 'user', 'creer')) { // If can edit only itself user, we can link to itself only |
||
| 308 | if ($userid != $user->id && $userid != $object->user_id) { |
||
| 309 | $error++; |
||
| 310 | setEventMessages($langs->trans("ErrorUserPermissionAllowsToLinksToItselfOnly"), null, 'errors'); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | if (!$error) { |
||
| 315 | if ($userid != $object->user_id) { // If link differs from currently in database |
||
| 316 | $result = $object->setUserId($userid); |
||
| 317 | if ($result < 0) { |
||
| 318 | dol_print_error($object->db, $object->error); |
||
| 319 | } |
||
| 320 | $action = ''; |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($action == 'setsocid') { |
||
| 326 | $error = 0; |
||
| 327 | if (!$error) { |
||
| 328 | if ($socid != $object->socid) { // If link differs from currently in database |
||
| 329 | $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "adherent"; |
||
| 330 | $sql .= " WHERE socid = " . ((int) $socid); |
||
| 331 | $sql .= " AND entity = " . $conf->entity; |
||
| 332 | $resql = $db->query($sql); |
||
| 333 | if ($resql) { |
||
| 334 | $obj = $db->fetch_object($resql); |
||
| 335 | if ($obj && $obj->rowid > 0) { |
||
| 336 | $othermember = new Adherent($db); |
||
| 337 | $othermember->fetch($obj->rowid); |
||
| 338 | $thirdparty = new Societe($db); |
||
| 339 | $thirdparty->fetch($socid); |
||
| 340 | $error++; |
||
| 341 | setEventMessages($langs->trans("ErrorMemberIsAlreadyLinkedToThisThirdParty", $othermember->getFullName($langs), $othermember->login, $thirdparty->name), null, 'errors'); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | if (!$error) { |
||
| 346 | $result = $object->setThirdPartyId($socid); |
||
| 347 | if ($result < 0) { |
||
| 348 | dol_print_error($object->db, $object->error); |
||
| 349 | } |
||
| 350 | $action = ''; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | // Create user from a member |
||
| 357 | if ($action == 'confirm_create_user' && $confirm == 'yes' && $user->hasRight('user', 'user', 'creer')) { |
||
| 358 | if ($result > 0) { |
||
| 359 | // Creation user |
||
| 360 | $nuser = new User($db); |
||
| 361 | $tmpuser = dol_clone($object); |
||
| 362 | if (GETPOST('internalorexternal', 'aZ09') == 'internal') { |
||
| 363 | $tmpuser->fk_soc = 0; |
||
| 364 | } |
||
| 365 | |||
| 366 | $result = $nuser->create_from_member($tmpuser, GETPOST('login', 'alphanohtml')); |
||
| 367 | |||
| 368 | if ($result < 0) { |
||
| 369 | $langs->load("errors"); |
||
| 370 | setEventMessages($langs->trans($nuser->error), null, 'errors'); |
||
| 371 | } else { |
||
| 372 | setEventMessages($langs->trans("NewUserCreated", $nuser->login), null, 'mesgs'); |
||
| 373 | $action = ''; |
||
| 374 | } |
||
| 375 | } else { |
||
| 376 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | // Create third party from a member |
||
| 381 | if ($action == 'confirm_create_thirdparty' && $confirm == 'yes' && $user->hasRight('societe', 'creer')) { |
||
| 382 | if ($result > 0) { |
||
| 383 | // User creation |
||
| 384 | $company = new Societe($db); |
||
| 385 | $result = $company->create_from_member($object, GETPOST('companyname', 'alpha'), GETPOST('companyalias', 'alpha')); |
||
| 386 | |||
| 387 | if ($result < 0) { |
||
| 388 | $langs->load("errors"); |
||
| 389 | setEventMessages($langs->trans($company->error), null, 'errors'); |
||
| 390 | setEventMessages($company->error, $company->errors, 'errors'); |
||
| 391 | } |
||
| 392 | } else { |
||
| 393 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | if ($action == 'update' && !$cancel && $user->hasRight('adherent', 'creer')) { |
||
| 398 | require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 399 | |||
| 400 | $birthdate = ''; |
||
| 401 | if (GETPOSTINT("birthday") && GETPOSTINT("birthmonth") && GETPOSTINT("birthyear")) { |
||
| 402 | $birthdate = dol_mktime(12, 0, 0, GETPOSTINT("birthmonth"), GETPOSTINT("birthday"), GETPOSTINT("birthyear")); |
||
| 403 | } |
||
| 404 | $lastname = GETPOST("lastname", 'alphanohtml'); |
||
| 405 | $firstname = GETPOST("firstname", 'alphanohtml'); |
||
| 406 | $gender = GETPOST("gender", 'alphanohtml'); |
||
| 407 | $societe = GETPOST("societe", 'alphanohtml'); |
||
| 408 | $morphy = GETPOST("morphy", 'alphanohtml'); |
||
| 409 | $login = GETPOST("login", 'alphanohtml'); |
||
| 410 | if ($morphy != 'mor' && empty($lastname)) { |
||
| 411 | $error++; |
||
| 412 | $langs->load("errors"); |
||
| 413 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Lastname")), null, 'errors'); |
||
| 414 | } |
||
| 415 | if ($morphy != 'mor' && (!isset($firstname) || $firstname == '')) { |
||
| 416 | $error++; |
||
| 417 | $langs->load("errors"); |
||
| 418 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Firstname")), null, 'errors'); |
||
| 419 | } |
||
| 420 | if ($morphy == 'mor' && empty($societe)) { |
||
| 421 | $error++; |
||
| 422 | $langs->load("errors"); |
||
| 423 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Company")), null, 'errors'); |
||
| 424 | } |
||
| 425 | // Check if the login already exists |
||
| 426 | if (!getDolGlobalString('ADHERENT_LOGIN_NOT_REQUIRED')) { |
||
| 427 | if (empty($login)) { |
||
| 428 | $error++; |
||
| 429 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Login")), null, 'errors'); |
||
| 430 | } |
||
| 431 | } |
||
| 432 | // Create new object |
||
| 433 | if ($result > 0 && !$error) { |
||
| 434 | $object->oldcopy = dol_clone($object, 2); |
||
| 435 | |||
| 436 | // Change values |
||
| 437 | $object->civility_id = trim(GETPOST("civility_id", 'alphanohtml')); |
||
| 438 | $object->firstname = trim(GETPOST("firstname", 'alphanohtml')); |
||
| 439 | $object->lastname = trim(GETPOST("lastname", 'alphanohtml')); |
||
| 440 | $object->gender = trim(GETPOST("gender", 'alphanohtml')); |
||
| 441 | $object->login = trim(GETPOST("login", 'alphanohtml')); |
||
| 442 | if (GETPOSTISSET('pass')) { |
||
| 443 | $object->pass = trim(GETPOST("pass", 'none')); // For password, we must use 'none' |
||
| 444 | } |
||
| 445 | |||
| 446 | $object->societe = trim(GETPOST("societe", 'alphanohtml')); // deprecated |
||
| 447 | $object->company = trim(GETPOST("societe", 'alphanohtml')); |
||
| 448 | |||
| 449 | $object->address = trim(GETPOST("address", 'alphanohtml')); |
||
| 450 | $object->zip = trim(GETPOST("zipcode", 'alphanohtml')); |
||
| 451 | $object->town = trim(GETPOST("town", 'alphanohtml')); |
||
| 452 | $object->state_id = GETPOSTINT("state_id"); |
||
| 453 | $object->country_id = GETPOSTINT("country_id"); |
||
| 454 | |||
| 455 | $object->phone = trim(GETPOST("phone", 'alpha')); |
||
| 456 | $object->phone_perso = trim(GETPOST("phone_perso", 'alpha')); |
||
| 457 | $object->phone_mobile = trim(GETPOST("phone_mobile", 'alpha')); |
||
| 458 | $object->email = preg_replace('/\s+/', '', GETPOST("member_email", 'alpha')); |
||
| 459 | $object->url = trim(GETPOST('member_url', 'custom', 0, FILTER_SANITIZE_URL)); |
||
| 460 | $object->socialnetworks = []; |
||
| 461 | foreach ($socialnetworks as $key => $value) { |
||
| 462 | if (GETPOSTISSET($key) && GETPOST($key, 'alphanohtml') != '') { |
||
| 463 | $object->socialnetworks[$key] = trim(GETPOST($key, 'alphanohtml')); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | $object->birth = $birthdate; |
||
| 467 | $object->default_lang = GETPOST('default_lang', 'alpha'); |
||
| 468 | $object->typeid = GETPOSTINT("typeid"); |
||
| 469 | //$object->note = trim(GETPOST("comment", "restricthtml")); |
||
| 470 | $object->morphy = GETPOST("morphy", 'alpha'); |
||
| 471 | |||
| 472 | if (GETPOST('deletephoto', 'alpha')) { |
||
| 473 | $object->photo = ''; |
||
| 474 | } elseif (!empty($_FILES['photo']['name'])) { |
||
| 475 | $object->photo = dol_sanitizeFileName($_FILES['photo']['name']); |
||
| 476 | } |
||
| 477 | |||
| 478 | // Get status and public property |
||
| 479 | $object->statut = GETPOSTINT("statut"); |
||
| 480 | $object->status = GETPOSTINT("statut"); |
||
| 481 | $object->public = GETPOSTINT("public"); |
||
| 482 | |||
| 483 | // Fill array 'array_options' with data from add form |
||
| 484 | $ret = $extrafields->setOptionalsFromPost(null, $object, '@GETPOSTISSET'); |
||
| 485 | if ($ret < 0) { |
||
| 486 | $error++; |
||
| 487 | } |
||
| 488 | |||
| 489 | // Check if we need to also synchronize user information |
||
| 490 | $nosyncuser = 0; |
||
| 491 | if ($object->user_id) { // If linked to a user |
||
| 492 | if ($user->id != $object->user_id && !$user->hasRight('user', 'user', 'creer')) { |
||
| 493 | $nosyncuser = 1; // Disable synchronizing |
||
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | // Check if we need to also synchronize password information |
||
| 498 | $nosyncuserpass = 1; // no by default |
||
| 499 | if (GETPOSTISSET('pass')) { |
||
| 500 | if ($object->user_id) { // If member is linked to a user |
||
| 501 | $nosyncuserpass = 0; // We may try to sync password |
||
| 502 | if ($user->id == $object->user_id) { |
||
| 503 | if (!$user->hasRight('user', 'self', 'password')) { |
||
| 504 | $nosyncuserpass = 1; // Disable synchronizing |
||
| 505 | } |
||
| 506 | } else { |
||
| 507 | if (!$user->hasRight('user', 'user', 'password')) { |
||
| 508 | $nosyncuserpass = 1; // Disable synchronizing |
||
| 509 | } |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 | |||
| 514 | if (!$error) { |
||
| 515 | $result = $object->update($user, 0, $nosyncuser, $nosyncuserpass); |
||
| 516 | |||
| 517 | if ($result >= 0 && !count($object->errors)) { |
||
| 518 | $categories = GETPOST('memcats', 'array'); |
||
| 519 | $object->setCategories($categories); |
||
| 520 | |||
| 521 | // Logo/Photo save |
||
| 522 | $dir = $conf->adherent->dir_output . '/' . get_exdir(0, 0, 0, 1, $object, 'member') . '/photos'; |
||
| 523 | $file_OK = is_uploaded_file($_FILES['photo']['tmp_name']); |
||
| 524 | if ($file_OK) { |
||
| 525 | if (GETPOST('deletephoto')) { |
||
| 526 | require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
| 527 | $fileimg = $conf->adherent->dir_output . '/' . get_exdir(0, 0, 0, 1, $object, 'member') . '/photos/' . $object->photo; |
||
| 528 | $dirthumbs = $conf->adherent->dir_output . '/' . get_exdir(0, 0, 0, 1, $object, 'member') . '/photos/thumbs'; |
||
| 529 | dol_delete_file($fileimg); |
||
| 530 | dol_delete_dir_recursive($dirthumbs); |
||
| 531 | } |
||
| 532 | |||
| 533 | if (image_format_supported($_FILES['photo']['name']) > 0) { |
||
| 534 | dol_mkdir($dir); |
||
| 535 | |||
| 536 | if (@is_dir($dir)) { |
||
| 537 | $newfile = $dir . '/' . dol_sanitizeFileName($_FILES['photo']['name']); |
||
| 538 | if (!dol_move_uploaded_file($_FILES['photo']['tmp_name'], $newfile, 1, 0, $_FILES['photo']['error']) > 0) { |
||
| 539 | setEventMessages($langs->trans("ErrorFailedToSaveFile"), null, 'errors'); |
||
| 540 | } else { |
||
| 541 | // Create thumbs |
||
| 542 | $object->addThumbs($newfile); |
||
| 543 | } |
||
| 544 | } |
||
| 545 | } else { |
||
| 546 | setEventMessages("ErrorBadImageFormat", null, 'errors'); |
||
| 547 | } |
||
| 548 | } else { |
||
| 549 | switch ($_FILES['photo']['error']) { |
||
| 550 | case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini |
||
| 551 | case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form |
||
| 552 | $errors[] = "ErrorFileSizeTooLarge"; |
||
| 553 | break; |
||
| 554 | case 3: //uploaded file was only partially uploaded |
||
| 555 | $errors[] = "ErrorFilePartiallyUploaded"; |
||
| 556 | break; |
||
| 557 | } |
||
| 558 | } |
||
| 559 | |||
| 560 | $rowid = $object->id; |
||
| 561 | $id = $object->id; |
||
| 562 | $action = ''; |
||
| 563 | |||
| 564 | if (!empty($backtopage)) { |
||
| 565 | header("Location: " . $backtopage); |
||
| 566 | exit; |
||
| 567 | } |
||
| 568 | } else { |
||
| 569 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 570 | $action = ''; |
||
| 571 | } |
||
| 572 | } else { |
||
| 573 | $action = 'edit'; |
||
| 574 | } |
||
| 575 | } else { |
||
| 576 | $action = 'edit'; |
||
| 577 | } |
||
| 578 | } |
||
| 579 | |||
| 580 | if ($action == 'add' && $user->hasRight('adherent', 'creer')) { |
||
| 581 | if ($canvas) { |
||
| 582 | $object->canvas = $canvas; |
||
| 583 | } |
||
| 584 | $birthdate = ''; |
||
| 585 | if (GETPOSTISSET("birthday") && GETPOST("birthday") && GETPOSTISSET("birthmonth") && GETPOST("birthmonth") && GETPOSTISSET("birthyear") && GETPOST("birthyear")) { |
||
| 586 | $birthdate = dol_mktime(12, 0, 0, GETPOSTINT("birthmonth"), GETPOSTINT("birthday"), GETPOSTINT("birthyear")); |
||
| 587 | } |
||
| 588 | $datesubscription = ''; |
||
| 589 | if (GETPOSTISSET("reday") && GETPOSTISSET("remonth") && GETPOSTISSET("reyear")) { |
||
| 590 | $datesubscription = dol_mktime(12, 0, 0, GETPOSTINT("remonth"), GETPOSTINT("reday"), GETPOSTINT("reyear")); |
||
| 591 | } |
||
| 592 | |||
| 593 | $typeid = GETPOSTINT("typeid"); |
||
| 594 | $civility_id = GETPOST("civility_id", 'alphanohtml'); |
||
| 595 | $lastname = GETPOST("lastname", 'alphanohtml'); |
||
| 596 | $firstname = GETPOST("firstname", 'alphanohtml'); |
||
| 597 | $gender = GETPOST("gender", 'alphanohtml'); |
||
| 598 | $societe = GETPOST("societe", 'alphanohtml'); |
||
| 599 | $address = GETPOST("address", 'alphanohtml'); |
||
| 600 | $zip = GETPOST("zipcode", 'alphanohtml'); |
||
| 601 | $town = GETPOST("town", 'alphanohtml'); |
||
| 602 | $state_id = GETPOSTINT("state_id"); |
||
| 603 | $country_id = GETPOSTINT("country_id"); |
||
| 604 | |||
| 605 | $phone = GETPOST("phone", 'alpha'); |
||
| 606 | $phone_perso = GETPOST("phone_perso", 'alpha'); |
||
| 607 | $phone_mobile = GETPOST("phone_mobile", 'alpha'); |
||
| 608 | $email = preg_replace('/\s+/', '', GETPOST("member_email", 'alpha')); |
||
| 609 | $url = trim(GETPOST('url', 'custom', 0, FILTER_SANITIZE_URL)); |
||
| 610 | $login = GETPOST("member_login", 'alphanohtml'); |
||
| 611 | $pass = GETPOST("password", 'none'); // For password, we use 'none' |
||
| 612 | $photo = GETPOST("photo", 'alphanohtml'); |
||
| 613 | $morphy = GETPOST("morphy", 'alphanohtml'); |
||
| 614 | $public = GETPOST("public", 'alphanohtml'); |
||
| 615 | |||
| 616 | $userid = GETPOSTINT("userid"); |
||
| 617 | $socid = GETPOSTINT("socid"); |
||
| 618 | $default_lang = GETPOST('default_lang', 'alpha'); |
||
| 619 | |||
| 620 | $object->civility_id = $civility_id; |
||
| 621 | $object->firstname = $firstname; |
||
| 622 | $object->lastname = $lastname; |
||
| 623 | $object->gender = $gender; |
||
| 624 | $object->societe = $societe; // deprecated |
||
| 625 | $object->company = $societe; |
||
| 626 | $object->address = $address; |
||
| 627 | $object->zip = $zip; |
||
| 628 | $object->town = $town; |
||
| 629 | $object->state_id = $state_id; |
||
| 630 | $object->country_id = $country_id; |
||
| 631 | $object->phone = $phone; |
||
| 632 | $object->phone_perso = $phone_perso; |
||
| 633 | $object->phone_mobile = $phone_mobile; |
||
| 634 | $object->socialnetworks = []; |
||
| 635 | if (isModEnabled('socialnetworks')) { |
||
| 636 | foreach ($socialnetworks as $key => $value) { |
||
| 637 | if (GETPOSTISSET($key) && GETPOST($key, 'alphanohtml') != '') { |
||
| 638 | $object->socialnetworks[$key] = GETPOST("member_" . $key, 'alphanohtml'); |
||
| 639 | } |
||
| 640 | } |
||
| 641 | } |
||
| 642 | |||
| 643 | $object->email = $email; |
||
| 644 | $object->url = $url; |
||
| 645 | $object->login = $login; |
||
| 646 | $object->pass = $pass; |
||
| 647 | $object->birth = $birthdate; |
||
| 648 | $object->photo = $photo; |
||
| 649 | $object->typeid = $typeid; |
||
| 650 | //$object->note = $comment; |
||
| 651 | $object->morphy = $morphy; |
||
| 652 | $object->user_id = $userid; |
||
| 653 | $object->socid = $socid; |
||
| 654 | $object->public = $public; |
||
| 655 | $object->default_lang = $default_lang; |
||
| 656 | // Fill array 'array_options' with data from add form |
||
| 657 | $ret = $extrafields->setOptionalsFromPost(null, $object); |
||
| 658 | if ($ret < 0) { |
||
| 659 | $error++; |
||
| 660 | } |
||
| 661 | |||
| 662 | // Check parameters |
||
| 663 | if (empty($morphy) || $morphy == "-1") { |
||
| 664 | $error++; |
||
| 665 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MemberNature")), null, 'errors'); |
||
| 666 | } |
||
| 667 | // Tests if the login already exists |
||
| 668 | if (!getDolGlobalString('ADHERENT_LOGIN_NOT_REQUIRED')) { |
||
| 669 | if (empty($login)) { |
||
| 670 | $error++; |
||
| 671 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Login")), null, 'errors'); |
||
| 672 | } else { |
||
| 673 | $sql = "SELECT login FROM " . MAIN_DB_PREFIX . "adherent WHERE login='" . $db->escape($login) . "'"; |
||
| 674 | $result = $db->query($sql); |
||
| 675 | if ($result) { |
||
| 676 | $num = $db->num_rows($result); |
||
| 677 | } |
||
| 678 | if ($num) { |
||
| 679 | $error++; |
||
| 680 | $langs->load("errors"); |
||
| 681 | setEventMessages($langs->trans("ErrorLoginAlreadyExists", $login), null, 'errors'); |
||
| 682 | } |
||
| 683 | } |
||
| 684 | if (empty($pass)) { |
||
| 685 | $error++; |
||
| 686 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Password")), null, 'errors'); |
||
| 687 | } |
||
| 688 | } |
||
| 689 | if ($morphy == 'mor' && empty($societe)) { |
||
| 690 | $error++; |
||
| 691 | $langs->load("errors"); |
||
| 692 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Company")), null, 'errors'); |
||
| 693 | } |
||
| 694 | if ($morphy != 'mor' && empty($lastname)) { |
||
| 695 | $error++; |
||
| 696 | $langs->load("errors"); |
||
| 697 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Lastname")), null, 'errors'); |
||
| 698 | } |
||
| 699 | if ($morphy != 'mor' && (!isset($firstname) || $firstname == '')) { |
||
| 700 | $error++; |
||
| 701 | $langs->load("errors"); |
||
| 702 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Firstname")), null, 'errors'); |
||
| 703 | } |
||
| 704 | if (!($typeid > 0)) { // Keep () before ! |
||
| 705 | $error++; |
||
| 706 | setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors'); |
||
| 707 | } |
||
| 708 | if (getDolGlobalString('ADHERENT_MAIL_REQUIRED') && !isValidEmail($email)) { |
||
| 709 | $error++; |
||
| 710 | $langs->load("errors"); |
||
| 711 | setEventMessages($langs->trans("ErrorBadEMail", $email), null, 'errors'); |
||
| 712 | } |
||
| 713 | if (!empty($object->url) && !isValidUrl($object->url)) { |
||
| 714 | $langs->load("errors"); |
||
| 715 | setEventMessages($langs->trans("ErrorBadUrl", $object->url), null, 'errors'); |
||
| 716 | } |
||
| 717 | $public = 0; |
||
| 718 | if (isset($public)) { |
||
| 719 | $public = 1; |
||
| 720 | } |
||
| 721 | |||
| 722 | if (!$error) { |
||
| 723 | $db->begin(); |
||
| 724 | |||
| 725 | // Create the member |
||
| 726 | $result = $object->create($user); |
||
| 727 | if ($result > 0) { |
||
| 728 | // Foundation categories |
||
| 729 | $memcats = GETPOST('memcats', 'array'); |
||
| 730 | $object->setCategories($memcats); |
||
| 731 | |||
| 732 | $db->commit(); |
||
| 733 | |||
| 734 | $rowid = $object->id; |
||
| 735 | $id = $object->id; |
||
| 736 | |||
| 737 | $backtopage = preg_replace('/__ID__/', $id, $backtopage); |
||
| 738 | } else { |
||
| 739 | $db->rollback(); |
||
| 740 | |||
| 741 | $error++; |
||
| 742 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 743 | } |
||
| 744 | |||
| 745 | // Auto-create thirdparty on member creation |
||
| 746 | if (getDolGlobalString('ADHERENT_DEFAULT_CREATE_THIRDPARTY')) { |
||
| 747 | if ($result > 0) { |
||
| 748 | // Create third party out of a member |
||
| 749 | $company = new Societe($db); |
||
| 750 | $result = $company->create_from_member($object); |
||
| 751 | if ($result < 0) { |
||
| 752 | $langs->load("errors"); |
||
| 753 | setEventMessages($langs->trans($company->error), null, 'errors'); |
||
| 754 | setEventMessages($company->error, $company->errors, 'errors'); |
||
| 755 | } |
||
| 756 | } else { |
||
| 757 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 758 | } |
||
| 759 | } |
||
| 760 | } |
||
| 761 | $action = ($result < 0 || !$error) ? '' : 'create'; |
||
| 762 | |||
| 763 | if (!$error && $backtopage) { |
||
| 764 | header("Location: " . $backtopage); |
||
| 765 | exit; |
||
| 766 | } |
||
| 767 | } |
||
| 768 | |||
| 769 | if ($user->hasRight('adherent', 'supprimer') && $action == 'confirm_delete' && $confirm == 'yes') { |
||
| 770 | $result = $object->delete($user); |
||
| 771 | if ($result > 0) { |
||
| 772 | setEventMessages($langs->trans("RecordDeleted"), null, 'errors'); |
||
| 773 | if (!empty($backtopage) && !preg_match('/' . preg_quote($_SERVER['PHP_SELF'], '/') . '/', $backtopage)) { |
||
| 774 | header("Location: " . $backtopage); |
||
| 775 | exit; |
||
| 776 | } else { |
||
| 777 | header("Location: list.php"); |
||
| 778 | exit; |
||
| 779 | } |
||
| 780 | } else { |
||
| 781 | setEventMessages($object->error, null, 'errors'); |
||
| 782 | } |
||
| 783 | } |
||
| 784 | |||
| 785 | if ($user->hasRight('adherent', 'creer') && $action == 'confirm_valid' && $confirm == 'yes') { |
||
| 786 | $error = 0; |
||
| 787 | |||
| 788 | $db->begin(); |
||
| 789 | |||
| 790 | $adht = new AdherentType($db); |
||
| 791 | $adht->fetch($object->typeid); |
||
| 792 | |||
| 793 | $result = $object->validate($user); |
||
| 794 | |||
| 795 | if ($result >= 0 && !count($object->errors)) { |
||
| 796 | // Send confirmation email (according to parameters of member type. Otherwise generic) |
||
| 797 | if ($object->email && GETPOST("send_mail")) { |
||
| 798 | $subject = ''; |
||
| 799 | $msg = ''; |
||
| 800 | |||
| 801 | // Send subscription email |
||
| 802 | include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; |
||
| 803 | $formmail = new FormMail($db); |
||
| 804 | // Set output language |
||
| 805 | $outputlangs = new Translate('', $conf); |
||
| 806 | $outputlangs->setDefaultLang(empty($object->thirdparty->default_lang) ? $mysoc->default_lang : $object->thirdparty->default_lang); |
||
| 807 | // Load traductions files required by page |
||
| 808 | $outputlangs->loadLangs(["main", "members", "companies", "install", "other"]); |
||
| 809 | // Get email content from template |
||
| 810 | $arraydefaultmessage = null; |
||
| 811 | $labeltouse = getDolGlobalString('ADHERENT_EMAIL_TEMPLATE_MEMBER_VALIDATION'); |
||
| 812 | |||
| 813 | if (!empty($labeltouse)) { |
||
| 814 | $arraydefaultmessage = $formmail->getEMailTemplate($db, 'member', $user, $outputlangs, 0, 1, $labeltouse); |
||
| 815 | } |
||
| 816 | |||
| 817 | if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) { |
||
| 818 | $subject = $arraydefaultmessage->topic; |
||
| 819 | $msg = $arraydefaultmessage->content; |
||
| 820 | } |
||
| 821 | |||
| 822 | if (empty($labeltouse) || (int) $labeltouse === -1) { |
||
| 823 | //fallback on the old configuration. |
||
| 824 | $langs->load("errors"); |
||
| 825 | setEventMessages('<a href="' . DOL_URL_ROOT . '/adherents/admin/member_emails.php">' . $langs->trans('WarningMandatorySetupNotComplete') . '</a>', null, 'errors'); |
||
| 826 | $error++; |
||
| 827 | } else { |
||
| 828 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 829 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 830 | $subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs); |
||
| 831 | $texttosend = make_substitutions(dol_concatdesc($msg, $adht->getMailOnValid()), $substitutionarray, $outputlangs); |
||
| 832 | |||
| 833 | $moreinheader = 'X-Dolibarr-Info: send_an_email by adherents/card.php' . "\r\n"; |
||
| 834 | |||
| 835 | $result = $object->sendEmail($texttosend, $subjecttosend, [], [], [], "", "", 0, -1, '', $moreinheader); |
||
| 836 | if ($result < 0) { |
||
| 837 | $error++; |
||
| 838 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 839 | } |
||
| 840 | } |
||
| 841 | } |
||
| 842 | } else { |
||
| 843 | $error++; |
||
| 844 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 845 | } |
||
| 846 | |||
| 847 | if (!$error) { |
||
| 848 | $db->commit(); |
||
| 849 | } else { |
||
| 850 | $db->rollback(); |
||
| 851 | } |
||
| 852 | $action = ''; |
||
| 853 | } |
||
| 854 | |||
| 855 | if ($user->hasRight('adherent', 'supprimer') && $action == 'confirm_resiliate') { |
||
| 856 | $error = 0; |
||
| 857 | |||
| 858 | if ($confirm == 'yes') { |
||
| 859 | $adht = new AdherentType($db); |
||
| 860 | $adht->fetch($object->typeid); |
||
| 861 | |||
| 862 | $result = $object->resiliate($user); |
||
| 863 | |||
| 864 | if ($result >= 0 && !count($object->errors)) { |
||
| 865 | if ($object->email && GETPOST("send_mail")) { |
||
| 866 | $subject = ''; |
||
| 867 | $msg = ''; |
||
| 868 | |||
| 869 | // Send subscription email |
||
| 870 | include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; |
||
| 871 | $formmail = new FormMail($db); |
||
| 872 | // Set output language |
||
| 873 | $outputlangs = new Translate('', $conf); |
||
| 874 | $outputlangs->setDefaultLang(empty($object->thirdparty->default_lang) ? $mysoc->default_lang : $object->thirdparty->default_lang); |
||
| 875 | // Load traductions files required by page |
||
| 876 | $outputlangs->loadLangs(["main", "members", "companies", "install", "other"]); |
||
| 877 | // Get email content from template |
||
| 878 | $arraydefaultmessage = null; |
||
| 879 | $labeltouse = getDolGlobalString('ADHERENT_EMAIL_TEMPLATE_CANCELATION'); |
||
| 880 | |||
| 881 | if (!empty($labeltouse)) { |
||
| 882 | $arraydefaultmessage = $formmail->getEMailTemplate($db, 'member', $user, $outputlangs, 0, 1, $labeltouse); |
||
| 883 | } |
||
| 884 | |||
| 885 | if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) { |
||
| 886 | $subject = $arraydefaultmessage->topic; |
||
| 887 | $msg = $arraydefaultmessage->content; |
||
| 888 | } |
||
| 889 | |||
| 890 | if (empty($labeltouse) || (int) $labeltouse === -1) { |
||
| 891 | //fallback on the old configuration. |
||
| 892 | setEventMessages('WarningMandatorySetupNotComplete', null, 'errors'); |
||
| 893 | $error++; |
||
| 894 | } else { |
||
| 895 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 896 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 897 | $subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs); |
||
| 898 | $texttosend = make_substitutions(dol_concatdesc($msg, $adht->getMailOnResiliate()), $substitutionarray, $outputlangs); |
||
| 899 | |||
| 900 | $moreinheader = 'X-Dolibarr-Info: send_an_email by adherents/card.php' . "\r\n"; |
||
| 901 | |||
| 902 | $result = $object->sendEmail($texttosend, $subjecttosend, [], [], [], "", "", 0, -1, '', $moreinheader); |
||
| 903 | if ($result < 0) { |
||
| 904 | $error++; |
||
| 905 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 906 | } |
||
| 907 | } |
||
| 908 | } |
||
| 909 | } else { |
||
| 910 | $error++; |
||
| 911 | |||
| 912 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 913 | $action = ''; |
||
| 914 | } |
||
| 915 | } |
||
| 916 | if (!empty($backtopage) && !$error) { |
||
| 917 | header("Location: " . $backtopage); |
||
| 918 | exit; |
||
| 919 | } |
||
| 920 | } |
||
| 921 | |||
| 922 | if ($user->hasRight('adherent', 'supprimer') && $action == 'confirm_exclude') { |
||
| 923 | $error = 0; |
||
| 924 | |||
| 925 | if ($confirm == 'yes') { |
||
| 926 | $adht = new AdherentType($db); |
||
| 927 | $adht->fetch($object->typeid); |
||
| 928 | |||
| 929 | $result = $object->exclude($user); |
||
| 930 | |||
| 931 | if ($result >= 0 && !count($object->errors)) { |
||
| 932 | if ($object->email && GETPOST("send_mail")) { |
||
| 933 | $subject = ''; |
||
| 934 | $msg = ''; |
||
| 935 | |||
| 936 | // Send subscription email |
||
| 937 | include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php'; |
||
| 938 | $formmail = new FormMail($db); |
||
| 939 | // Set output language |
||
| 940 | $outputlangs = new Translate('', $conf); |
||
| 941 | $outputlangs->setDefaultLang(empty($object->thirdparty->default_lang) ? $mysoc->default_lang : $object->thirdparty->default_lang); |
||
| 942 | // Load traductions files required by page |
||
| 943 | $outputlangs->loadLangs(["main", "members", "companies", "install", "other"]); |
||
| 944 | // Get email content from template |
||
| 945 | $arraydefaultmessage = null; |
||
| 946 | $labeltouse = getDolGlobalString('ADHERENT_EMAIL_TEMPLATE_EXCLUSION'); |
||
| 947 | |||
| 948 | if (!empty($labeltouse)) { |
||
| 949 | $arraydefaultmessage = $formmail->getEMailTemplate($db, 'member', $user, $outputlangs, 0, 1, $labeltouse); |
||
| 950 | } |
||
| 951 | |||
| 952 | if (!empty($labeltouse) && is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) { |
||
| 953 | $subject = $arraydefaultmessage->topic; |
||
| 954 | $msg = $arraydefaultmessage->content; |
||
| 955 | } |
||
| 956 | |||
| 957 | if (empty($labeltouse) || (int) $labeltouse === -1) { |
||
| 958 | //fallback on the old configuration. |
||
| 959 | setEventMessages('WarningMandatorySetupNotComplete', null, 'errors'); |
||
| 960 | $error++; |
||
| 961 | } else { |
||
| 962 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
| 963 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 964 | $subjecttosend = make_substitutions($subject, $substitutionarray, $outputlangs); |
||
| 965 | $texttosend = make_substitutions(dol_concatdesc($msg, $adht->getMailOnExclude()), $substitutionarray, $outputlangs); |
||
| 966 | |||
| 967 | $moreinheader = 'X-Dolibarr-Info: send_an_email by adherents/card.php' . "\r\n"; |
||
| 968 | |||
| 969 | $result = $object->sendEmail($texttosend, $subjecttosend, [], [], [], "", "", 0, -1, '', $moreinheader); |
||
| 970 | if ($result < 0) { |
||
| 971 | $error++; |
||
| 972 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 973 | } |
||
| 974 | } |
||
| 975 | } |
||
| 976 | } else { |
||
| 977 | $error++; |
||
| 978 | |||
| 979 | setEventMessages($object->error, $object->errors, 'errors'); |
||
| 980 | $action = ''; |
||
| 981 | } |
||
| 982 | } |
||
| 983 | if (!empty($backtopage) && !$error) { |
||
| 984 | header("Location: " . $backtopage); |
||
| 985 | exit; |
||
| 986 | } |
||
| 987 | } |
||
| 988 | |||
| 989 | // SPIP Management |
||
| 990 | if ($user->hasRight('adherent', 'supprimer') && $action == 'confirm_del_spip' && $confirm == 'yes') { |
||
| 991 | if (!count($object->errors)) { |
||
| 992 | if (!$mailmanspip->del_to_spip($object)) { |
||
| 993 | setEventMessages($langs->trans('DeleteIntoSpipError') . ': ' . $mailmanspip->error, null, 'errors'); |
||
| 994 | } |
||
| 995 | } |
||
| 996 | } |
||
| 997 | |||
| 998 | if ($user->hasRight('adherent', 'creer') && $action == 'confirm_add_spip' && $confirm == 'yes') { |
||
| 999 | if (!count($object->errors)) { |
||
| 1000 | if (!$mailmanspip->add_to_spip($object)) { |
||
| 1001 | setEventMessages($langs->trans('AddIntoSpipError') . ': ' . $mailmanspip->error, null, 'errors'); |
||
| 1002 | } |
||
| 1003 | } |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | // Actions when printing a doc from card |
||
| 1007 | include DOL_DOCUMENT_ROOT . '/core/actions_printing.inc.php'; |
||
| 1008 | |||
| 1009 | // Actions to build doc |
||
| 1010 | $upload_dir = $conf->adherent->dir_output; |
||
| 1011 | $permissiontoadd = $user->hasRight('adherent', 'creer'); |
||
| 1012 | include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php'; |
||
| 1013 | |||
| 1014 | // Actions to send emails |
||
| 1015 | $triggersendname = 'MEMBER_SENTBYMAIL'; |
||
| 1016 | $paramname = 'id'; |
||
| 1017 | $mode = 'emailfrommember'; |
||
| 1018 | $trackid = 'mem' . $object->id; |
||
| 1019 | include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php'; |
||
| 1020 | } |
||
| 1021 | |||
| 1022 | |||
| 1023 | /* |
||
| 1024 | * View |
||
| 1025 | */ |
||
| 1026 | |||
| 1027 | require_once realpath(BASE_PATH . '/../Dolibarr/Modules/Adherent/Views/card.php'); |
||
| 1028 | |||
| 1029 | $db->close(); |
||
| 1030 | return true; |
||
| 1031 | } |
||
| 2601 |