Пример #1
0
 function registerAction()
 {
     $this->view->title = "Register New Account";
     $this->view->messages = $this->_flashMessenger->getMessages();
     $this->_helper->layout->disableLayout();
     $lang = $this->_request->getParam('lang');
     if (isset($lang)) {
         $langNamespace = new Zend_Session_Namespace('Lang');
         $langNamespace->lang = $lang;
         $this->_helper->redirector->gotoSimple('register', 'register', null, array('a' => $this->_request->getParam('a'), 'i' => $this->_request->getParam('i')));
     }
     $db = Zend_Registry::get('db');
     $currentTime = date("Y-m-d H:i:s");
     $loginform = new LoginForm();
     $this->view->form = $loginform;
     $form = new RegisterForm();
     $this->view->registerForm = $form;
     $signupAuthCodeModel = new SignupAuthCode();
     $auth_code = $this->_request->getParam('a');
     if ($auth_code) {
         $form->auth_code->setValue($auth_code);
         $code = $signupAuthCodeModel->fetchRow("use_date is null and auth_code = '" . $auth_code . "'");
     }
     // auto-fill code and email address
     if (isset($code) && $code->id) {
         $this->view->codeId = $code->id;
         $select1 = $db->select();
         $select1->from("invitation_email", "to");
         $select1->where("invitation_email.signup_auth_code_id = ?", $code->id);
         $toEmail = $db->fetchOne($select1);
         $form->registerEmail->setValue($toEmail);
         $code->view_date = $currentTime;
         $code->save();
     }
     //public link
     $invite_code = $this->_request->getParam('i');
     if ($invite_code) {
         $code2 = $signupAuthCodeModel->fetchRow("public_signup_link = true and auth_code = '" . $invite_code . "'");
         $publicLinkValid = false;
         if (isset($code2)) {
             $select2 = $db->select();
             $select2->from('signup_auth_code', 'count(*)')->where('use_date>date_sub(now(),interval 1 day)')->where('sender =' . $code2->sender)->where('source = "PUBLIC_LINK"')->where('receiver is not null');
             $registered = $db->fetchOne($select2);
             if (intval($registered) < 100) {
                 $publicLinkValid = true;
             }
         }
     }
     if (isset($code2)) {
         if ($publicLinkValid) {
             $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
             $generatedCode = '';
             for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                 $generatedCode = $generatedCode . $codePattern[mt_rand(0, 35)];
             }
             $signupAuthCode = $signupAuthCodeModel->createRow();
             $signupAuthCode->auth_code = $generatedCode;
             $signupAuthCode->create_date = $currentTime;
             $signupAuthCode->sender = $code2->sender;
             $signupAuthCode->source = 'PUBLIC_LINK';
             $signupAuthCode->auto_invitation = $code2->auto_invitation;
             $signupAuthCode->save();
             $form->auth_code->setValue($generatedCode);
         } else {
             $this->_flashMessenger->addMessage($this->view->translate('Sorry_This_register_link_has_been_overused'));
             $this->_helper->redirector('register', 'register');
         }
     }
     if ($this->_request->isPost()) {
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             $db = Zend_Registry::get('db');
             if ($form->getValue('registerPassword') == $form->getValue('repeat')) {
                 // verify auth code
                 $codeModel = new SignupAuthCode();
                 $code = $codeModel->fetchRow("auth_code='" . $form->getValue('auth_code') . "' and use_date is null");
                 if ($code) {
                     //check pest
                     if ($code->sender != null) {
                         $consumerModel = new Consumer();
                         $consumer = $consumerModel->fetchRow("id = " . $code->sender);
                         if ($consumer != null && $consumer->pest == '1') {
                             return;
                         }
                     }
                     //check duplicated email
                     $result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE email = :temp", array('temp' => $form->getValue('registerEmail')));
                     //check duplicated phone
                     $phone_result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE login_phone = :temp", array('temp' => $form->getValue('loginPhone')));
                     if ($result > 0) {
                         $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('registerEmail') . $this->view->translate('Register_email_is_invalid');
                     } else {
                         if ($phone_result > 0) {
                             $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('loginPhone') . $this->view->translate('Register_phone_is_invalid');
                         } else {
                             $currentTime = date("Y-m-d H:i:s");
                             // save new consumer
                             $consumerModel = new Consumer();
                             $row = $consumerModel->createRow();
                             $row->name = $form->getValue('name');
                             $row->email = $form->getValue('registerEmail');
                             $row->login_phone = $form->getValue('loginPhone');
                             $row->password = md5($form->getValue('registerPassword'));
                             $row->save();
                             //expire the auth_code
                             $code->receiver = $row->id;
                             $code->use_date = $currentTime;
                             $code->save();
                             //add points for code sender
                             //		    				if (!empty($code->sender)&& $code->sender!=""){
                             //			    				$pointRecordModel = new RewardPointTransactionRecord();
                             //			    				$point = $pointRecordModel->createRow();
                             //			    				$point->consumer_id = $code->sender;
                             //			    				$point->transaction_id = 2;
                             //			    				$point->date = $currentTime;
                             //			    				$point->point_amount = 5;
                             //			    				$point->save();
                             //		    				}
                             // send auto intivitaion
                             if (!empty($code->auto_invitation) && $code->auto_invitation != 0) {
                                 $campaignInvitationModel = new CampaignInvitation();
                                 $ci = $campaignInvitationModel->createRow();
                                 $ci->consumer_id = $row->id;
                                 $ci->campaign_id = $code->auto_invitation;
                                 $ci->create_date = $currentTime;
                                 $ci->state = "NEW";
                                 $ci->save();
                             }
                             // Login Automatically
                             $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                             $authAdapter->setTableName('consumer');
                             $authAdapter->setIdentityColumn('email');
                             $authAdapter->setCredentialColumn('password');
                             $authAdapter->setIdentity($form->getValue('registerEmail'));
                             $authAdapter->setCredential(md5($form->getValue('registerPassword')));
                             $auth = Zend_Auth::getInstance();
                             $auth->authenticate($authAdapter);
                             $authNamespace = new Zend_Session_Namespace('Zend_Auth');
                             $authNamespace->user = $row;
                             $this->_flashMessenger->addMessage('Welcome!');
                             $this->_helper->redirector('index', 'home');
                         }
                     }
                 } else {
                     $this->view->errMessage = $this->view->translate('Register_err') . $this->view->translate('Register_authcode_is_invalid');
                 }
             } else {
                 $this->view->errMessage = $this->view->translate('Register_err') . $this->view->translate('Register_password_is_invalid');
             }
         } else {
             $form->populate($formData);
         }
     }
 }
Пример #2
0
 /**
  * sendAction 邀请朋友加入(给朋友的邮箱发送邮件)
  */
 function sendAction()
 {
     $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("INVITATION_MAIL_SEND");
     //
     $form = new SendMailForm();
     $consumer = $this->_currentUser;
     $isSentSuccessfully = false;
     if ($consumer['pest'] == '1') {
         $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_Sucessful');
         $isSentSuccessfully = true;
         return;
     }
     if ($this->_request->isPost()) {
         //POST
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             if ($consumer->invitation_limit != null) {
                 $this->invitation_limit = $consumer->invitation_limit;
             } else {
                 $this->invitation_limit = $this->_maxInvitation;
             }
             //get the amount of emails have been sent
             $db = Zend_Registry::get('db');
             $select = $db->select();
             $select->from('invitation_email', 'distinct  (invitation_email.to)');
             $select->where('consumer_id = ?', $this->_currentUser->id);
             //				$select->from('invitation_email', 'count(*)');
             //				$select->where('consumer_id = ?',$consumer->id);
             for ($i = (int) $db->fetchOne($select) + 1; $i <= $this->invitation_limit; $i++) {
                 if ($form->getValue('email' . (string) $i) != '') {
                     //generate rand code
                     $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
                     $signup_auth_code = '';
                     for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                         $signup_auth_code = $signup_auth_code . $codePattern[mt_rand(0, 35)];
                     }
                     //send mail
                     $emailSubject = $this->view->translate('Invitation_Email_subject');
                     $emailBody = $this->view->translate('Invitation_Email_body');
                     $stringChange = array('?USERNAME?' => $this->_currentUser['name'], '?EMAIL?' => $this->_currentUser['email'], '?MESSAGE?' => $form->getValue('message'), '?AUTHCODE?' => (string) $signup_auth_code);
                     $emailSubject = strtr($emailSubject, $stringChange);
                     $emailBody = strtr($emailBody, $stringChange);
                     $config = Zend_Registry::get('config');
                     $smtpSender = new Zend_Mail_Transport_Smtp($config->smtp->friend->mail->server, array('username' => $config->smtp->friend->mail->username, 'password' => $config->smtp->friend->mail->password, 'auth' => $config->smtp->friend->mail->auth, 'ssl' => $config->smtp->friend->mail->ssl, 'port' => $config->smtp->friend->mail->port));
                     //							$smtpSender = new Zend_Mail_Transport_Smtp(
                     //														smtp.163.com',array(
                     //														'username'=>'*****@*****.**',
                     //																			'password'=>'19990402',
                     //																			'auth'=>'login'));
                     Zend_Mail::setDefaultTransport($smtpSender);
                     $mail = new Zend_Mail('utf-8');
                     $langNamespace = new Zend_Session_Namespace('Lang');
                     if ($langNamespace->lang == 'en' || $langNamespace->lang == 'EN') {
                         $mail->setSubject($emailSubject);
                     } else {
                         $mail->setSubject("=?UTF-8?B?" . base64_encode($emailSubject) . "?=");
                     }
                     $mail->setBodyText($emailBody);
                     $mail->setFrom($config->smtp->friend->mail->username, $consumer->name);
                     $mail->addHeader('Reply-To', $consumer->email);
                     //						$mail->setFrom('*****@*****.**',$this->view->translate('Wildfire'));
                     $mail->addTo($form->getValue('email' . (string) $i));
                     //save into DB
                     try {
                         $currentTime = date("Y-m-d H:i:s");
                         //save into signup_auth_code
                         $signupAuthCodeModel = new SignupAuthCode();
                         $signupAuthCode = $signupAuthCodeModel->createRow();
                         $signupAuthCode->auth_code = $signup_auth_code;
                         $signupAuthCode->create_date = $currentTime;
                         $signupAuthCode->sender = $this->_currentUser->id;
                         $signupAuthCode->source = 'FRIENDS';
                         $signupAuthCode->auto_invitation = 0;
                         $signupAuthCode->save();
                         //send mail after saving
                         $mail->send();
                         //save into invitation_email
                         $invitationEmailModel = new InvitationEmail();
                         $invitationEmail = $invitationEmailModel->createRow();
                         $invitationEmail->subject = $emailSubject;
                         $invitationEmail->content = $form->getValue('message');
                         $invitationEmail->consumer_id = $this->_currentUser->id;
                         $invitationEmail->to = $form->getValue('email' . (string) $i);
                         $invitationEmail->signup_auth_code_id = $signupAuthCode->id;
                         $invitationEmail->date = $currentTime;
                         $invitationEmail->save();
                         $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_Sucessful');
                         $isSentSuccessfully = true;
                     } catch (Exception $e) {
                         //roll back...
                         $this->view->showMessage = 'System Error!';
                     }
                 }
             }
             if (!$isSentSuccessfully) {
                 $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_Fail');
             }
         } else {
             $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_DataError');
         }
     }
     //		$this->_helper->redirector('index','invitation',null,array('isSendMailSuccessful'=>$isSendMailSuccessful));
 }
Пример #3
0
 function registerAction()
 {
     $this->view->title = "Register New Account";
     $this->view->messages = $this->_flashMessenger->getMessages();
     $this->_helper->layout->disableLayout();
     $lang = $this->_request->getParam('lang');
     if (isset($lang)) {
         $langNamespace = new Zend_Session_Namespace('Lang');
         $langNamespace->lang = $lang;
         $this->_helper->redirector->gotoSimple('register', 'register', null, array('a' => $this->_request->getParam('a'), 'i' => $this->_request->getParam('i')));
     }
     $db = Zend_Registry::get('db');
     $currentTime = date("Y-m-d H:i:s");
     $loginform = new LoginForm();
     $this->view->form = $loginform;
     $form = new RegisterForm();
     $form->setAttrib('id', 'registerForm');
     $this->view->registerForm = $form;
     $signupAuthCodeModel = new SignupAuthCode();
     $auth_code = $this->_request->getParam('a');
     if ($auth_code) {
         $form->auth_code->setValue($auth_code);
         $code = $signupAuthCodeModel->fetchRow("use_date is null and auth_code = '" . $auth_code . "'");
     }
     // auto-fill code and email address
     if (isset($code) && $code->id) {
         $this->view->codeId = $code->id;
         $select1 = $db->select();
         $select1->from("invitation_email", "to");
         $select1->where("invitation_email.signup_auth_code_id = ?", $code->id);
         $toEmail = $db->fetchOne($select1);
         $form->registerEmail->setValue($toEmail);
         $code->view_date = $currentTime;
         $code->save();
     }
     //public link
     $invite_code = $this->_request->getParam('i');
     if ($invite_code) {
         $code2 = $signupAuthCodeModel->fetchRow("public_signup_link = true and auth_code = '" . $invite_code . "'");
         $publicLinkValid = false;
         if (isset($code2)) {
             $select2 = $db->select();
             $select2->from('signup_auth_code', 'count(*)')->where('use_date>date_sub(now(),interval 1 day)')->where('sender =' . $code2->sender)->where('source = "PUBLIC_LINK"')->where('receiver is not null');
             $registered = $db->fetchOne($select2);
             if (intval($registered) < 100) {
                 $publicLinkValid = true;
             }
         }
     }
     if (isset($code2)) {
         if ($publicLinkValid) {
             $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
             $generatedCode = '';
             for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                 $generatedCode = $generatedCode . $codePattern[mt_rand(0, 35)];
             }
             $signupAuthCode = $signupAuthCodeModel->createRow();
             $signupAuthCode->auth_code = $generatedCode;
             $signupAuthCode->create_date = $currentTime;
             $signupAuthCode->sender = $code2->sender;
             $signupAuthCode->source = 'PUBLIC_LINK';
             $signupAuthCode->auto_invitation = $code2->auto_invitation;
             $signupAuthCode->save();
             $form->auth_code->setValue($generatedCode);
         } else {
             $this->_flashMessenger->addMessage($this->view->translate('Sorry_This_register_link_has_been_overused'));
             $this->_helper->redirector('register', 'register');
         }
     }
     if ($this->_request->isPost()) {
         $this->view->registered = 0;
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             $db = Zend_Registry::get('db');
             if ($form->getValue('registerPassword') == $form->getValue('repeat')) {
                 //2011-04-01 ham register modification
                 if (trim($form->getValue('auth_code')) == '') {
                     //check duplicated email
                     $result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE email = :temp", array('temp' => $form->getValue('registerEmail')));
                     //check duplicated phone
                     $phone_result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE login_phone = :temp", array('temp' => $form->getValue('loginPhone')));
                     //var_dump($result);die;
                     if ($result > 0) {
                         $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('registerEmail') . $this->view->translate('Register_email_is_invalid');
                     } else {
                         if ($phone_result > 0) {
                             $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('loginPhone') . $this->view->translate('Register_phone_is_invalid');
                         } else {
                             $currentTime = date("Y-m-d H:i:s");
                             $email = $form->getValue('registerEmail');
                             //generate enable account  link
                             $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
                             $active_code = '';
                             for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                                 $active_code = $active_code . $codePattern[mt_rand(0, 35)];
                             }
                             $activeLink = $this->view->home . '/public/register/activate/p/' . $active_code;
                             //save link into DB
                             $tomorrow = mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 1, date("Y"));
                             $expire_date = date("Y-m-d H:i:s", $tomorrow);
                             $temporaryLinkModel = new TemporaryLink();
                             $temporaryLink = array("link" => $activeLink, "email" => $email, "expire_date" => $expire_date);
                             $temporaryLink_id = $temporaryLinkModel->insert($temporaryLink);
                             //send mail
                             $emailSubject = $this->view->translate('ENABLE_ACCOUNT_subject');
                             $emailBody = $this->view->translate('ENABLE_ACCOUNT_body');
                             $stringChange = array("?ENABLEACCOUNTLINK?" => $activeLink);
                             $emailBody = strtr($emailBody, $stringChange);
                             $config = Zend_Registry::get('config');
                             $smtpSender = new Zend_Mail_Transport_Smtp($config->smtp->invitation->mail->server, array('username' => $config->smtp->invitation->mail->username, 'password' => $config->smtp->invitation->mail->password, 'auth' => $config->smtp->invitation->mail->auth, 'ssl' => $config->smtp->invitation->mail->ssl, 'port' => $config->smtp->invitation->mail->port));
                             //				$smtpSender = new Zend_Mail_Transport_Smtp(
                             //							'smtp.163.com',array(
                             //							'username'=>'*****@*****.**',
                             //							'password'=>'19990402',
                             //							'auth'=>'login'));
                             Zend_Mail::setDefaultTransport($smtpSender);
                             $mail = new Zend_Mail('utf-8');
                             $langNamespace = new Zend_Session_Namespace('Lang');
                             if ($langNamespace->lang == 'en' || $langNamespace->lang == 'EN') {
                                 $mail->setSubject($emailSubject);
                             } else {
                                 $mail->setSubject("=?UTF-8?B?" . base64_encode($emailSubject) . "?=");
                             }
                             $mail->setBodyText($emailBody);
                             $mail->setFrom($config->smtp->forgetpassword->mail->username, $this->view->translate('Wildfire'));
                             //				$mail->setFrom('*****@*****.**','yun_simon');
                             $mail->addTo($email);
                             $mail->send();
                             // save new consumer
                             $consumerModel = new Consumer();
                             $row = $consumerModel->createRow();
                             $row->name = $form->getValue('name');
                             $row->email = $form->getValue('registerEmail');
                             $row->login_phone = $form->getValue('loginPhone');
                             $row->password = md5($form->getValue('registerPassword'));
                             $row->state = "ACTIVE";
                             $row->save();
                             $this->view->registered = 1;
                         }
                     }
                     //2011-04-01 ham register modification
                 } else {
                     // verify auth code
                     $codeModel = new SignupAuthCode();
                     $code = $codeModel->fetchRow("auth_code='" . $form->getValue('auth_code') . "' and use_date is null");
                     if ($code != NULL) {
                         //check pest
                         if ($code->sender != null) {
                             $consumerModel = new Consumer();
                             $consumer = $consumerModel->fetchRow("id = " . $code->sender);
                             if ($consumer != null && $consumer->pest == '1') {
                                 return;
                             }
                         }
                         //check duplicated email
                         $result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE email = :temp", array('temp' => $form->getValue('registerEmail')));
                         //check duplicated phone
                         $phone_result = $db->fetchOne("SELECT COUNT(*) FROM consumer WHERE login_phone = :temp", array('temp' => $form->getValue('loginPhone')));
                         if ($result > 0) {
                             $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('registerEmail') . $this->view->translate('Register_email_is_invalid');
                         } else {
                             if ($phone_result > 0) {
                                 $this->view->errMessage = $this->view->translate('Register_err') . $form->getValue('loginPhone') . $this->view->translate('Register_phone_is_invalid');
                             } else {
                                 $currentTime = date("Y-m-d H:i:s");
                                 //2011-04-02 ham.bao add the logic of activating the account
                                 $email = $form->getValue('registerEmail');
                                 //generate enable account  link
                                 $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
                                 $active_code = '';
                                 for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                                     $active_code = $active_code . $codePattern[mt_rand(0, 35)];
                                 }
                                 $activeLink = $this->view->home . '/public/register/activate/p/' . $active_code;
                                 //save link into DB
                                 $tomorrow = mktime(date("H"), date("i"), date("s"), date("m"), date("d") + 1, date("Y"));
                                 $expire_date = date("Y-m-d H:i:s", $tomorrow);
                                 $temporaryLinkModel = new TemporaryLink();
                                 $temporaryLink = array("link" => $activeLink, "email" => $email, "expire_date" => $expire_date);
                                 $temporaryLink_id = $temporaryLinkModel->insert($temporaryLink);
                                 //send mail
                                 //2011-04-02 ham.bao add the logic of activating the account
                                 // save new consumer
                                 $consumerModel = new Consumer();
                                 $row = $consumerModel->createRow();
                                 $row->name = $form->getValue('name');
                                 $row->email = $form->getValue('registerEmail');
                                 $row->login_phone = $form->getValue('loginPhone');
                                 $row->password = md5($form->getValue('registerPassword'));
                                 $row->state = "ACTIVE";
                                 $row->save();
                                 //expire the auth_code
                                 $code->receiver = $row->id;
                                 $code->use_date = $currentTime;
                                 $code->save();
                                 //add points for code sender
                                 //		    				if (!empty($code->sender)&& $code->sender!=""){
                                 //			    				$pointRecordModel = new RewardPointTransactionRecord();
                                 //			    				$point = $pointRecordModel->createRow();
                                 //			    				$point->consumer_id = $code->sender;
                                 //			    				$point->transaction_id = 2;
                                 //			    				$point->date = $currentTime;
                                 //			    				$point->point_amount = 5;
                                 //			    				$point->save();
                                 //		    				}
                                 // send auto intivitaion
                                 if (!empty($code->auto_invitation) && $code->auto_invitation != 0) {
                                     $campaignInvitationModel = new CampaignInvitation();
                                     $ci = $campaignInvitationModel->createRow();
                                     $ci->consumer_id = $row->id;
                                     $ci->campaign_id = $code->auto_invitation;
                                     $ci->create_date = $currentTime;
                                     $ci->state = "NEW";
                                     $ci->save();
                                 }
                                 $this->view->registered = 1;
                                 // Login Automatically
                                 $authAdapter = new Zend_Auth_Adapter_DbTable($db);
                                 $authAdapter->setTableName('consumer');
                                 $authAdapter->setIdentityColumn('email');
                                 $authAdapter->setCredentialColumn('password');
                                 $authAdapter->setIdentity($form->getValue('registerEmail'));
                                 $authAdapter->setCredential(md5($form->getValue('registerPassword')));
                                 $auth = Zend_Auth::getInstance();
                                 $auth->authenticate($authAdapter);
                                 $authNamespace = new Zend_Session_Namespace('Zend_Auth');
                                 $authNamespace->user = $row;
                                 $this->_flashMessenger->addMessage('Welcome!');
                                 $this->_helper->redirector('index', 'home');
                             }
                         }
                     } else {
                         $this->view->errMessage = $this->view->translate('Register_err') . $this->view->translate('Register_authcode_is_invalid');
                     }
                 }
             } else {
                 $this->view->errMessage = $this->view->translate('Register_err') . $this->view->translate('Register_repeat_password_is_error');
             }
         } else {
             $form->populate($formData);
         }
     }
 }
 function admindynamicsendinvitationsAction()
 {
     $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("INVITATION_MAIL_SEND");
     $this->view->activeTab = 'Mails';
     //
     $this->_helper->layout->setLayout("layout_admin");
     $form = new CampaignInvitationMailForm();
     $consumer = $this->_currentUser;
     $isSentSuccessfully = false;
     if ($this->_request->isPost()) {
         //POST
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             //				$subjectmessage = $formData ["subject"];
             //				$message = $formData ["message"];
             //				if ($message == null || $message == '') {
             //					$message = $formData ["htmlmessage"];
             //					$useHtmlEmail = true;
             //				} else {
             //					$useHtmlEmail = false;
             //				}
             $useHtmlEmail = true;
             //2011-02-14 ham.bao get email content and subject
             $emailTemplate = new EmailTemplate();
             $email = $emailTemplate->fetchRow('id = ' . $formData['emailTemplate']);
             $subjectmessage = $email->subject;
             $message = $email->message;
             //2011-02-14 ham.bao get email content and subject
             $campaignId = $formData["campaignId"];
             $code_source = $formData["code_source"];
             $emailCategory = $formData['emailCategory'];
             $emailListString = $formData["emailList"];
             //				$emailArray = explode(';',$emailListString);
             // $emailArray= preg_split ( '/[;\s]+[\n\r\t]*/', trim ( $emailListString ) );
             $emailArray = array();
             $rows = explode("\n", $emailListString);
             $i = 0;
             foreach ($rows as $row) {
                 $cells = explode("\t", $row);
                 $j = 0;
                 foreach ($cells as $cell) {
                     $emailArray[$i][$j] = $cell;
                     $j++;
                 }
                 $i++;
             }
             $sentList = "";
             $total = 0;
             $config = Zend_Registry::get('config');
             $smtpSender = new Zend_Mail_Transport_Smtp($config->smtp->invitation->mail->server, array('username' => $config->smtp->invitation->mail->username, 'password' => $config->smtp->invitation->mail->password, 'auth' => $config->smtp->invitation->mail->auth, 'ssl' => $config->smtp->invitation->mail->ssl, 'port' => $config->smtp->invitation->mail->port));
             Zend_Mail::setDefaultTransport($smtpSender);
             $db = Zend_Registry::get('db');
             $langNamespace = new Zend_Session_Namespace('Lang');
             //print_r($emailArray);die;
             foreach ($emailArray as $emailAddress) {
                 $currentTime = date("Y-m-d H:i:s");
                 //$emailAddress = trim ( $emailAddress[0] );
                 if ($emailAddress[0] == null || $emailAddress[0] == '') {
                     continue;
                 }
                 if ($emailCategory == 'Invite non-sparks to join campaign') {
                     // ignore the spark!
                     $consumerModel = new Consumer();
                     $consumer = $consumerModel->fetchRow("email = '" . $emailAddress[0] . "'");
                     if ($consumer != null) {
                         continue;
                     }
                     $selectCode = $db->select();
                     $selectCode->from('signup_auth_code', 'signup_auth_code.auth_code')->joinInner('invitation_email', "invitation_email.signup_auth_code_id = signup_auth_code.id and invitation_email.to ='{$emailAddress['0']}'")->where('signup_auth_code.auto_invitation= ?', $campaignId);
                     $code = $db->fetchOne($selectCode);
                     //generate rand code
                     if ($code == false) {
                         $codePattern = '1234567890ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
                         $signup_auth_code = '';
                         for ($codeCount = 0; $codeCount < 12; $codeCount++) {
                             $signup_auth_code = $signup_auth_code . $codePattern[mt_rand(0, 35)];
                         }
                     } else {
                         $signup_auth_code = $code;
                     }
                     //var_dump($signup_auth_code);die;
                     $stringChange = array('?EMAIL?' => $emailAddress[0], '?AUTHCODE?' => (string) $signup_auth_code);
                     $subject = strtr($subjectmessage, $stringChange);
                     $body = strtr($message, $stringChange);
                 }
                 if ($emailCategory == 'Invite sparks to join campaign') {
                     $select = $db->select();
                     $select->from('consumer', '*')->where('email = ?', $emailAddress[0])->where('pest != 1 or pest is null');
                     $user = $db->fetchRow($select);
                     //ignore the pest!
                     if ($user == null) {
                         continue;
                     }
                     //ignore those have been invited
                     $selectInvitedSpark = $db->select();
                     $selectInvitedSpark->from('campaign_invitation', '*')->where('campaign_id = ?', $campaignId)->where('consumer_id = ?', $user['id']);
                     $invitedSpark = $db->fetchRow($selectInvitedSpark);
                     if ($invitedSpark != null) {
                         continue;
                     }
                     $stringChange = array('?USERNAME?' => $user['name']);
                     $subject = strtr($subjectmessage, $stringChange);
                     $body = strtr($message, $stringChange);
                 }
                 if ($emailCategory == 'Send mail to sparks') {
                     $select = $db->select();
                     $select->from('consumer', '*')->where('email = ?', $emailAddress[0])->where('pest != 1 or pest is null');
                     $user = $db->fetchRow($select);
                     //ignore the pest!
                     if ($user == null) {
                         continue;
                     }
                     $stringChange = array('?USERNAME?' => $user['name']);
                     $subject = strtr($subjectmessage, $stringChange);
                     $body = strtr($message, $stringChange);
                 }
                 $i = 0;
                 $num = count($emailAddress);
                 for ($j = 1; $j <= $num; $j++) {
                     if (isset($emailAddress[$j])) {
                         $body = str_replace('$' . $j, $emailAddress[$j], $body);
                     }
                 }
                 $i++;
                 $mail = new Zend_Mail('utf-8');
                 if ($langNamespace->lang == 'en' || $langNamespace->lang == 'EN') {
                     $mail->setSubject($subject);
                 } else {
                     $mail->setSubject("=?UTF-8?B?" . base64_encode($subject) . "?=");
                 }
                 if ($useHtmlEmail != null && $useHtmlEmail) {
                     $mail->setBodyHtml($body);
                 } else {
                     $mail->setBodyText($body);
                 }
                 $mail->setFrom($config->smtp->invitation->mail->username, $this->view->translate('Wildfire_bi_lang'));
                 //					$mail->setFrom('*****@*****.**',$this->view->translate('Wildfire'));
                 $mail->addTo($emailAddress[0]);
                 $mail->send();
                 //save into DB
                 try {
                     //save into signup_auth_code
                     if ($emailCategory == 'Invite non-sparks to join campaign') {
                         $signupAuthCodeModel = new SignupAuthCode();
                         $signupAuthCode = $signupAuthCodeModel->createRow();
                         $signupAuthCode->auth_code = $signup_auth_code;
                         $signupAuthCode->create_date = $currentTime;
                         if ($code_source == null || $code_source == '') {
                             $signupAuthCode->source = 'SIGNUP';
                         } else {
                             $signupAuthCode->source = $code_source;
                         }
                         $signupAuthCode->auto_invitation = $campaignId;
                         $signupAuthCode->save();
                         //save into invitation_email
                         $invitationEmailModel = new InvitationEmail();
                         $invitationEmail = $invitationEmailModel->createRow();
                         $invitationEmail->subject = $subject;
                         $invitationEmail->content = $body;
                         $invitationEmail->consumer_id = 16693;
                         $invitationEmail->to = $emailAddress[0];
                         $invitationEmail->signup_auth_code_id = $signupAuthCode->id;
                         $invitationEmail->date = $currentTime;
                         $invitationEmail->save();
                     }
                     if ($emailCategory == 'Invite sparks to join campaign') {
                         //save into spark email
                         $sparkEmailModel = new SparkEmail();
                         $sparkEmail = $sparkEmailModel->createRow();
                         $sparkEmail->subject = $subject;
                         $sparkEmail->content = $body;
                         $sparkEmail->to = $user['id'];
                         $sparkEmail->date = date("Y-m-d H:i:s");
                         $sparkEmail->save();
                         //save into campaign_invitation
                         $campaigninvitationModel = new CampaignInvitation();
                         $campaigninvitation = $campaigninvitationModel->createRow();
                         $campaigninvitation->campaign_id = $campaignId;
                         $campaigninvitation->consumer_id = $user['id'];
                         $campaigninvitation->create_date = $currentTime;
                         $campaigninvitation->state = 'NEW';
                         $campaigninvitation->save();
                     }
                     if ($emailCategory == 'Send mail to sparks') {
                         //save into spark email
                         $sparkEmailModel = new SparkEmail();
                         $sparkEmail = $sparkEmailModel->createRow();
                         $sparkEmail->subject = $subject;
                         $sparkEmail->content = $body;
                         $sparkEmail->to = $user['id'];
                         $sparkEmail->date = date("Y-m-d H:i:s");
                         $sparkEmail->save();
                     }
                     $total++;
                     $sentList .= $emailAddress[0] . ", ";
                     $isSentSuccessfully = true;
                 } catch (Exception $e) {
                     //roll back...
                     $this->view->showMessage = 'System Error!';
                 }
             }
             $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_Sucessful');
             $this->view->showsentList = $sentList;
             $this->view->showTotal = "Total: " . $total;
             if (!$isSentSuccessfully) {
                 $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_Fail');
             }
         } else {
             $this->view->showMessage = $this->view->translate('INVITATION_MAIL_LIST_PART1_DataError');
         }
     }
 }