Example #1
0
 /**
  * Add User in the time of adding a customer based on the account settings
  *
  * @param String  $email            user email id
  * @param String  $password         user password
  * @param Boolean $sendLoginDetails status
  *
  * @return boolean
  */
 function addUser($email, $password, $sendLoginDetails = false, $result = array(), $id)
 {
     global $objDatabase, $_CORELANG, $_ARRAYLANG;
     $settings = $this->getSettings();
     if (!isset($this->contact)) {
         $this->contact = new \Cx\Modules\Crm\Model\Entity\CrmContact();
     }
     $objFWUser = \FWUser::getFWUserObject();
     $modify = isset($this->contact->id) && !empty($this->contact->id);
     $accountId = 0;
     if (!empty($id)) {
         $objUsers = $objFWUser->objUser->getUsers($filter = array('id' => intval($id)));
         if ($objUsers) {
             $accountId = $objUsers->getId();
             $email = $objUsers->getEmail();
         }
     } else {
         if (empty($id)) {
             $objUsers = $objFWUser->objUser->getUsers($filter = array('email' => addslashes($email)));
             if ($objUsers) {
                 $accountId = $objUsers->getId();
             }
         }
     }
     if ($modify) {
         $useralExists = $objDatabase->SelectLimit("SELECT id FROM `" . DBPREFIX . "module_{$this->moduleNameLC}_contacts` WHERE user_account = {$accountId}", 1);
         if ($useralExists && !empty($useralExists->fields['id']) && !empty($accountId) && intval($useralExists->fields['id']) != $this->contact->id) {
             $existId = (int) $useralExists->fields['id'];
             $custDetails = $this->getExistCrmDetail($existId);
             $existLink = "<a href='index.php?cmd=" . $this->moduleName . "&act=customers&tpl=showcustdetail&id={$existId}' target='_blank'>{$custDetails['customer_name']} {$custDetails['contact_familyname']}</a>";
             $this->_strErrMessage = sprintf($_ARRAYLANG['TXT_CRM_CONTACT_ALREADY_EXIST_ERROR'], $existLink);
             return false;
         }
         $this->contact->account_id = $objDatabase->getOne("SELECT user_account FROM `" . DBPREFIX . "module_{$this->moduleNameLC}_contacts` WHERE id = {$this->contact->id}");
         if (empty($this->contact->account_id) && !empty($accountId)) {
             $objUser = $objFWUser->objUser->getUser($accountId);
             //            $objUser = new \User($accountId);
         } elseif ((!empty($this->contact->account_id) && ($objUser = $objFWUser->objUser->getUser($this->contact->account_id))) === false) {
             if (!empty($accountId)) {
                 $objUser = $objFWUser->objUser->getUser($accountId);
             } else {
                 $objUser = new \User();
                 $objUser->setPassword($password);
             }
         } elseif (!empty($accountId) && $useralExists && $useralExists->RecordCount() == 0) {
             $objUser = $objFWUser->objUser->getUser($accountId);
         } else {
             if ((!empty($this->contact->account_id) && ($objUser = $objFWUser->objUser->getUser($this->contact->account_id))) === true) {
                 if (empty($accountId)) {
                     $objUser = new \User();
                     $objUser->setPassword($password);
                 } else {
                     $objUser = $objFWUser->objUser->getUser($this->contact->account_id);
                 }
             } else {
                 if (empty($this->contact->account_id) && empty($accountId)) {
                     $objUser = new \User();
                     $objUser->setPassword($password);
                 }
             }
         }
     } else {
         if (empty($accountId)) {
             $objUser = new \User();
             $objUser->setPassword($password);
         } else {
             $userExists = $objDatabase->getOne("SELECT id FROM `" . DBPREFIX . "module_{$this->moduleNameLC}_contacts` WHERE user_account = {$accountId}");
             if (empty($userExists)) {
                 $objUser = $objFWUser->objUser->getUser($accountId);
             } else {
                 $custDetails = $this->getExistCrmDetail($userExists);
                 $existLink = "<a href='index.php?cmd=" . $this->moduleName . "&act=customers&tpl=showcustdetail&id={$userExists}' target='_blank'>{$custDetails['customer_name']} {$custDetails['contact_familyname']}</a>";
                 $this->_strErrMessage = sprintf($_ARRAYLANG['TXT_CRM_CONTACT_ALREADY_EXIST_ERROR'], $existLink);
                 return false;
             }
         }
     }
     //update/insert additional fields
     //company
     if (!empty($result['company'])) {
         $company = $objDatabase->getOne("SELECT customer_name FROM `" . DBPREFIX . "module_{$this->moduleNameLC}_contacts` WHERE id = '" . $result['company'] . "'");
     }
     //get default website
     foreach ($result['contactwebsite'] as $value) {
         if (!empty($value['value']) && $value['primary'] == '1') {
             $website = contrexx_raw2db($value['value']);
         }
     }
     //get default phone
     foreach ($result['contactphone'] as $value) {
         if (!empty($value['value']) && $value['primary'] == '1') {
             $phone = contrexx_input2db($value['value']);
         }
     }
     //get default address
     foreach ($result['contactAddress'] as $value) {
         if ((!empty($value['address']) || !empty($value['city']) || !empty($value['state']) || !empty($value['zip']) || !empty($value['country'])) && $value['primary'] == '1') {
             $address = contrexx_input2db($value['address']);
             $city = contrexx_input2db($value['city']);
             $zip = contrexx_input2db($value['zip']);
             $country = \Cx\Core\Country\Controller\Country::getByName($value['country']);
         }
     }
     $gender = $this->contact->contact_gender == 1 ? 'gender_female' : ($this->contact->contact_gender == 2 ? 'gender_male' : 'gender_undefined');
     $setProfileData = array('firstname' => array(0 => $this->contact->customerName), 'lastname' => array(0 => $this->contact->family_name), 'gender' => array(0 => $gender), 'website' => array(0 => $website), 'company' => array(0 => $company), 'phone_office' => array(0 => $phone), 'address' => array(0 => $address), 'city' => array(0 => $city), 'zip' => array(0 => $zip), 'country' => array(0 => $country['id']));
     //set profile picture
     $picture = $objDatabase->getOne("SELECT profile_picture FROM `" . DBPREFIX . "module_{$this->moduleNameLC}_contacts` WHERE id = '" . $this->contact->id . "'");
     $cx = \Cx\Core\Core\Controller\Cx::instanciate();
     if ($picture && !empty($picture)) {
         if (!file_exists($cx->getWebsiteImagesAccessProfilePath() . '/' . $picture)) {
             $file = $cx->getWebsiteImagesCrmProfilePath() . '/';
             if (($picture = self::moveUploadedImageInToPlace($objUser, $file . $picture, $picture, true)) == true) {
                 // create thumbnail
                 if (self::createThumbnailOfImage($picture, true) !== false) {
                     $setProfileData['picture'] = array();
                     array_push($setProfileData['picture'], $picture);
                 }
             }
         }
     }
     //set group ids
     $defaultUserGroup = $settings['default_user_group'];
     $groups = $objUser->getAssociatedGroupIds();
     if (!empty($defaultUserGroup) && !in_array($defaultUserGroup, $groups)) {
         array_push($groups, $defaultUserGroup);
     }
     $objUser->setGroups($groups);
     $objUser->setUsername($email);
     $objUser->setEmail($email);
     $objUser->setFrontendLanguage($result['contact_language']);
     $objUser->setBackendLanguage($settings['customer_default_language_backend']);
     $objUser->setActiveStatus(true);
     $objUser->setProfile($setProfileData);
     if (empty($objUser->error_msg) && $objUser->store()) {
         if (empty($this->contact->account_id) && $sendLoginDetails) {
             if (trim($objUser->getProfileAttribute('gender')) == 'gender_female') {
                 $saluation = $_ARRAYLANG['TXT_CRM_SALUATION_FEMALE'];
             } else {
                 if (trim($objUser->getProfileAttribute('gender')) == 'gender_male') {
                     $saluation = $_ARRAYLANG['TXT_CRM_SALUATION_MALE'];
                 } else {
                     $saluation = $_ARRAYLANG['TXT_CRM_SALUATION'];
                 }
             }
             $info['substitution'] = array('CRM_CONTACT_FIRSTNAME' => contrexx_raw2xhtml($objUser->getProfileAttribute('firstname')), 'CRM_CONTACT_LASTNAME' => contrexx_raw2xhtml($objUser->getProfileAttribute('lastname')), 'CRM_ASSIGNED_USER_EMAIL' => $objUser->getEmail(), 'CRM_CONTACT_SALUTATION' => contrexx_raw2xhtml($saluation), 'CRM_ASSIGNED_USER_NAME' => contrexx_raw2xhtml(\FWUser::getParsedUserTitle($objUser->getId())), 'CRM_CUSTOMER_COMPANY' => $this->contact->customerName . " " . $this->contact->family_name, 'CRM_DOMAIN' => ASCMS_PROTOCOL . "://{$_SERVER['HTTP_HOST']}" . $cx->getCodeBaseOffsetPath(), 'CRM_CONTACT_EMAIL' => $email, 'CRM_CONTACT_USERNAME' => $email, 'CRM_CONTACT_PASSWORD' => $password);
             //setting email template lang id
             $availableMailTempLangAry = $this->getActiveEmailTemLangId('Crm', CRM_EVENT_ON_USER_ACCOUNT_CREATED);
             $availableLangId = $this->getEmailTempLang($availableMailTempLangAry, $email);
             $info['lang_id'] = $availableLangId;
             $dispatcher = CrmEventDispatcher::getInstance();
             $dispatcher->triggerEvent(CRM_EVENT_ON_USER_ACCOUNT_CREATED, null, $info);
         }
         $this->contact->account_id = $objUser->getId();
         return true;
     } else {
         $objUser->reset();
         $this->_strErrMessage = implode("<br />", $objUser->error_msg);
         return false;
     }
     $this->_strErrMessage = 'Some thing went wrong';
     return false;
 }