/** * Create a new account or edit an existing one. * * @return void */ public function becomeclientAction() { // Test if the user is already connected. $account = Cible_FunctionsGeneral::getAuthentication(); // Set the default status to an account creation and not editing one $_edit = false; // Instantiate the user profiles $profile = new MemberProfile(); $newsletterProfile = new NewsletterProfile(); $memberData = array(); $accountValidate = true; //Set Default id for states and cities $config = Zend_Registry::get('config'); $current_state = $config->address->default->states; $currentCity = ''; $notifyAdmin = array(); // Get users data if he is already logged if ($account) { if ($account['status'] == 2 || $this->_request->isPost()) { $_edit = true; $memberData = $profile->findMember(array('email' => $account['email'])); $newsletterData = $newsletterProfile->findMember(array('email' => $account['email'])); $oAddress = new AddressObject(); if (!empty($memberData['addrBill'])) { $billAddr = $oAddress->populate($memberData['addrBill'], $this->_defaultInterfaceLanguage); } if (!empty($memberData['addrShip'])) { $shipAddr = $oAddress->populate($memberData['addrShip'], $this->_defaultInterfaceLanguage); } $oRetailer = new RetailersObject(); $onWeb = $oRetailer->getRetailerInfos($memberData['member_id'], $this->_defaultInterfaceLanguage); $memberData['AI_FirstTel'] = $billAddr['AI_FirstTel']; $memberData['AI_SecondTel'] = $billAddr['AI_SecondTel']; $memberData['A_Fax'] = $billAddr['A_Fax']; if (isset($billAddr['AI_WebSite'])) { $memberData['AI_WebSite'] = $billAddr['AI_WebSite']; } $memberData['addressFact'] = $billAddr; if (!$shipAddr['A_Duplicate']) { $shipAddr['duplicate'] = 0; } $memberData['addressShipping'] = $shipAddr; $current_state = $billAddr['A_StateId'] . self::SEPARATOR . $shipAddr['A_StateId'] . self::SEPARATOR; $currentCity = $billAddr['A_CityId'] . self::SEPARATOR . $shipAddr['A_CityId'] . self::SEPARATOR; if ($onWeb && !empty($onWeb['R_AddressId'])) { $webAddr = $oAddress->populate($onWeb['R_AddressId'], $this->_defaultInterfaceLanguage); $webAddr['isDistributeur'] = $onWeb['R_Status']; $memberData['addressDetaillant'] = $webAddr; $current_state .= $webAddr['A_StateId'] . '||'; $currentCity .= $webAddr['A_CityId'] . '||'; } // if (empty($this->view->step)) $this->view->headTitle($this->view->getClientText('account_modify_page_title')); } } $this->view->assign('accountValidate', $accountValidate); $options = $_edit ? array('mode' => 'edit') : array(); $_edit ? $this->view->assign('mode', 'edit') : $this->view->assign('mode', 'add'); // Instantiate the form for account management $form = new FormBecomeClient($options); //$_captcha = $form->getElement('captcha'); $countries = Cible_FunctionsGeneral::getCountries(); $addressFields = array(); $return = $this->_getParam('return'); if ($return && isset($_COOKIE['returnUrl'])) { $returnUrl = $_COOKIE['returnUrl']; $this->view->assign('return', $returnUrl); } $this->view->assign('selectedCity', $currentCity); $this->view->assign('selectedState', $current_state); $addressFields = array_unique($addressFields); // Test if the users has ticked the aggreement checkbox $agreementError = isset($_POST['termsAgreement']) && $_POST['termsAgreement'] != 1 ? true : false; if ($_edit) { $agreementError = false; } $this->view->assign('agreementError', $agreementError); // Actions when form is submitted if ($this->_request->isPost() && array_key_exists('submit', $_POST)) { $formData = $this->_request->getPost(); // Test if the email already exists and password is the same if ($_edit) { $subFormI = $form->getSubForm('identification'); if ($formData['identification']['email'] != $memberData['email']) { $findEmail = $profile->findMember(array('email' => $formData['identification']['email'])); if ($findEmail) { $emailNotFoundInDBValidator = new Zend_Validate_Db_NoRecordExists('GenericProfiles', 'GP_Email'); $emailNotFoundInDBValidator->setMessage($this->view->getClientText('validation_message_email_already_exists'), 'recordFound'); $subFormI->getElement('email')->addValidator($emailNotFoundInDBValidator); } } if (empty($formData['identification']['password']) && empty($formData['identification']['passwordConfirmation'])) { $subFormI->getElement('password')->clearValidators()->setRequired(false); $subFormI->getElement('passwordConfirmation')->clearValidators()->setRequired(false); } } $oAddress = new AddressObject(); // Get the addresses data to insert $addressFact = $formData['addressFact']; $addressShipping = $formData['addressShipping']; // $addressRetailer = $formData['addressDetaillant']; //Remove data form form if the shipping address is the same as bill if ($addressShipping['duplicate'] == 1) { $subFormShip = $form->getSubForm('addressShipping'); foreach ($subFormShip as $key => $value) { $value->clearValidators()->setRequired(false); } unset($formData['addressShipping']); } //If customer doesn't want to add data on website, set to false the field name // if ($addressRetailer['isDistributeur'] == 1) // $form->getSubForm('addressDetaillant')->getElement('AI_Name')->clearValidators()->setRequired(false); if ($form->isValid($formData) && !$agreementError) { //remove addresses unset($formData['addressFact'], $formData['addressShipping'], $formData['addressDetaillant']); // merge all subform fields for the member profile table $formData = $this->_mergeFormData($formData); //get the last data to merge in the billing address // $addressFact['A_Fax'] = $formData['A_Fax']; // $addressFact['AI_FirstTel'] = $formData['AI_FirstTel']; // $addressFact['AI_SecondTel'] = $formData['AI_SecondTel']; // $addressFact['AI_WebSite'] = $formData['AI_WebSite']; if (!empty($formData['password'])) { $password = $formData['password']; $formData['password'] = md5($password); } if (!$_edit) { // Do the processing here $validatedEmail = Cible_FunctionsGeneral::generatePassword(); $path = Zend_Registry::get('web_root') . '/'; $hash = md5(session_id()); $duration = 0; $cookie = array('lastName' => utf8_encode($formData['lastName']), 'firstName' => utf8_encode($formData['firstName']), 'email' => $formData['email'], 'hash' => $hash, 'status' => 0); $formData['hash'] = $hash; $formData['validatedEmail'] = $validatedEmail; /// $this->save($memberId, $data); //Add addresses process and retrive id for memberProfiles $idBillingAddr = $oAddress->insert($addressFact, $this->_defaultInterfaceLanguage); if ($addressShipping['duplicate'] == 1) { $addressFact['A_Duplicate'] = $idBillingAddr; $idShippingAddr = $oAddress->insert($addressFact, $this->_defaultInterfaceLanguage); } else { $idShippingAddr = $oAddress->insert($addressShipping, $this->_defaultInterfaceLanguage); } $formData['addrBill'] = $idBillingAddr; $formData['addrShip'] = $idShippingAddr; $profile->addMember($formData); $memberData = $profile->findMember(array('email' => $formData['email'])); $idMember = $memberData['member_id']; // if ($addressRetailer['isDistributeur'] == 2) // { // $oRetailer = new RetailersObject(); // $idRetailerAddr = $oAddress->insert($addressRetailer, $this->_defaultInterfaceLanguage); // // $retailerData = array( // 'R_GenericProfileId' => $idMember, // 'R_AddressId' => $idRetailAddr, // 'R_Status' => $addressRetailer['isDistributeur'] // ); // // $oRetailer->insert($retailerData, $this->_defaultInterfaceLanguage); // } setcookie("authentication", json_encode($cookie), $duration, $path); $data = array('firstName' => $memberData['firstName'], 'lastName' => $memberData['lastName'], 'email' => $memberData['email'], 'language' => $formData['language'], 'validatedEmail' => $formData['validatedEmail'], 'password' => $password); $options = array('send' => true, 'isHtml' => true, 'to' => $formData['email'], 'moduleId' => $this->_moduleID, 'event' => 'newAccount', 'type' => 'email', 'recipient' => 'client', 'data' => $data); $oNotification = new Cible_Notifications_Email($options); // $notifyAdmin = array(); $this->view->assign('needConfirm', true); $this->renderScript('index/confirm-email.phtml'); } else { if (empty($formData['password'])) { unset($formData['password']); } $oAddress->save($billAddr['A_AddressId'], $addressFact, $this->_defaultInterfaceLanguage); if ($addressShipping['duplicate'] == 1) { $addressFact['A_Duplicate'] = $billAddr['A_AddressId']; $oAddress->save($shipAddr['A_AddressId'], $addressFact, $this->_defaultInterfaceLanguage); } else { $addressShipping['A_Duplicate'] = 0; $oAddress->save($shipAddr['A_AddressId'], $addressShipping, $this->_defaultInterfaceLanguage); } // $retailerData = array( // 'R_Status' => $addressRetailer['isDistributeur']); // // switch ($addressRetailer['isDistributeur']) // { // case 1: // if (!empty($onWeb)) // { // $retailerData = array( // 'R_Status' => $addressRetailer['isDistributeur']); // $oRetailer->save($onWeb['R_ID'], $retailerData, $this->_defaultInterfaceLanguage); // } // break; // case 2: // if (!empty($webAddr)) // { // $retailerData = array( // 'R_Status' => $addressRetailer['isDistributeur']); // $oAddress->save($webAddr['A_AddressId'], $addressRetailer, $this->_defaultInterfaceLanguage); // $oRetailer->save($onWeb['R_ID'], $retailerData, $this->_defaultInterfaceLanguage); // } // else // { // $addressId = $oAddress->insert($addressRetailer, $this->_defaultInterfaceLanguage); // $retailerData = array( // 'R_GenericProfileId' => $memberID, // 'R_AddressId' => $addressId, // 'R_Status' => $addressRetailer['isDistributeur'] // ); // $oRetailer->insert($retailerData, $this->_defaultInterfaceLanguage); // } // break; // default: // break; // } $profile->updateMember($memberData['member_id'], $formData); if ($formData['email'] != $memberData['email']) { $validatedEmail = Cible_FunctionsGeneral::generatePassword(); $formData['validatedEmail'] = $validatedEmail; $profile->updateMember($memberData['member_id'], $formData); $data = array('firstName' => $formData['firstName'], 'lastName' => $formData['lastName'], 'email' => $formData['email'], 'language' => $formData['language'], 'validatedEmail' => $formData['validatedEmail']); $options = array('send' => true, 'isHtml' => true, 'to' => $formData['email'], 'moduleId' => $this->_moduleID, 'event' => 'editResend', 'type' => 'email', 'recipient' => 'client', 'data' => $data); $oNotification = new Cible_Notifications_Email($options); $this->view->assign('needConfirm', true); $this->renderScript('index/confirm-email.phtml'); } else { $authentication = json_decode($_COOKIE['authentication'], true); $path = Zend_Registry::get('web_root') . '/'; $duration = 0; $cookie = array('lastName' => utf8_encode($formData['lastName']), 'firstName' => utf8_encode($formData['firstName']), 'email' => $authentication['email'], 'language' => $formData['language'], 'hash' => $authentication['hash'], 'status' => 2); setcookie("authentication", json_encode($cookie), $duration, $path); $this->view->assign('messages', array($this->view->getCibleText('form_account_modified_message'))); $this->view->assign('updatedName', $formData['firstName']); } $data = array('identification' => $memberData, 'addressFact' => $billAddr, 'addressShipping' => $shipAddr); $notifyAdmin = $this->_testDataForNotification($this->_request->getPost(), $data); // Notify admin if (count($notifyAdmin) > 0) { $data = array('firstName' => $formData['firstName'], 'lastName' => $formData['lastName'], 'email' => $formData['email'], 'NEWID' => $memberData['member_id'], 'language' => $formData['language'], 'validatedEmail' => $formData['validatedEmail'], 'form' => $form->populate($_POST), 'notifyAdmin' => $notifyAdmin); $options = array('send' => true, 'isHtml' => true, 'moduleId' => $this->_moduleID, 'event' => 'editAccount', 'type' => 'email', 'recipient' => 'admin', 'data' => $data); $oNotification = new Cible_Notifications_Email($options); } $this->_redirect(Cible_FunctionsCategories::getPagePerCategoryView(0, 'modify_inscription', $this->_moduleID)); } if (!empty($formData['newsletterSubscription'])) { $newsletterCategory = $formData['newsletterSubscription'] ? $this->_config->newsletter->default->categoryID : ''; $newsletterProfile->updateMember($memberData['member_id'], array('newsletter_categories' => $newsletterCategory)); } } else { $form->populate($_POST); } } else { if ($_edit && empty($this->view->step)) { if (!empty($newsletterData['newsletter_categories'])) { $memberData['newsletterSubscription'] = 1; } $form->populate($memberData); } } $this->view->assign('form', $form); }