function descriptionAction()
 {
     //      $this->_helper->layout->disableLayout();
     $db = Zend_Registry::get('db');
     $campaign_id = $this->_request->getParam('id');
     $currentTime = date("Y-m-d H:i:s");
     $campaignModel = new Campaign();
     $campaign = $campaignModel->fetchRow('id =' . $campaign_id);
     $campaignInvitation = new CampaignInvitation();
     $invitation = $campaignInvitation->fetchRow("campaign_id = {$campaign_id} and campaign_invitation.consumer_id = " . $this->_currentUser->id);
     if ($campaign->public == 1 && !count($invitation)) {
         $campaignInvitation = new CampaignInvitation();
         $row = $campaignInvitation->createRow();
         $row->consumer_id = $this->_currentUser->id;
         $row->campaign_id = $campaign_id;
         $row->state = 'NEW';
         $row->create_date = $currentTime;
         $row->save();
     }
     if ($campaign->public == 0 && !count($invitation)) {
         $this->_helper->redirector('index', 'home');
     }
     //
     $campaignInvitation = new CampaignInvitation();
     $invitation = $campaignInvitation->fetchRow("campaign_id = {$campaign_id} and campaign_invitation.consumer_id = " . $this->_currentUser->id);
     $this->view->state = $invitation->state;
     //participate
     $select = $db->select();
     $select->from('consumer', 'consumer.*');
     $select->join('campaign_invitation', 'consumer.id = campaign_invitation.consumer_id and campaign_invitation.campaign_id =' . $campaign_id, 'campaign_id');
     $select->join('campaign_participation', 'campaign_invitation.id = campaign_participation.campaign_invitation_id', 'accept_date');
     $this->view->co = $db->fetchAll($select);
     $this->view->campaign_id = $campaign_id;
     //var_dump($this->view->activeMissions);die;
 }
 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);
         }
     }
 }
 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 profileAction()
 {
     $this->_helper->layout->setLayout("layout_admin");
     $uid = $this->_request->getParam('uid');
     $campainId = $this->_request->getParam('campaign');
     if ($campainId != '') {
         $campaignInvitationModel = new CampaignInvitation();
         $data = $campaignInvitationModel->fetchAll('consumer_id = ' . $uid . ' and campaign_id =' . $campainId);
         if (count($data) == 0) {
             $row = $campaignInvitationModel->createRow();
             $row->campaign_id = $campainId;
             $row->consumer_id = $uid;
             $row->create_date = date("Y-m-d H:i:s");
             $row->state = "NEW";
             $row->save();
         }
         $this->_helper->redirector('profile', 'consumer', '', array('uid' => $uid));
     }
     $search = $this->_request->getParam('search');
     $this->view->search = $search;
     $campaignInfo = array();
     $reportsInfo = array();
     $userModel = new Consumer();
     if ($this->_request->getParam('resetpassword')) {
         $userModel->update(array('password' => md5('111111')), 'id =' . $uid);
         $this->_helper->redirector('profile', 'consumer', null, array('uid' => $uid));
     }
     $profile = $userModel->fetchRow('id =' . $uid);
     $userExtraInfo = new ConsumerExtraInfo();
     $extra_profile = $userExtraInfo->fetchRow(' consumer_id = ' . $uid);
     $this->view->profile = $profile;
     $this->view->extra_profile = $extra_profile;
     //		var_dump($extra_profile);die;
     $db = Zend_Registry::get('db');
     $select = $db->select();
     $select->from("campaign", array('campaign.*', 'campaign_participation.state', 'campaign_invitation.state as cstate', 'campaign_invitation.id as ciid'));
     $select->joinLeft("campaign_invitation", "campaign.id=campaign_invitation.campaign_id and campaign_invitation.consumer_id = " . $uid, null);
     $select->joinLeft("campaign_participation", "campaign_invitation.id=campaign_participation.campaign_invitation_id", "campaign_participation.accept_date");
     //$select->where ( "campaign_invitation.state = 'ACCEPTED'" );
     //$select->where ( " campaign_invitation.consumer_id = ?", $uid );
     $select->order('campaign_participation.accept_date desc');
     $campaignsAll = $db->fetchAll($select);
     $this->view->campaigns = $campaignsAll;
     //survey
     $db = Zend_Registry::get('db');
     $select_survey = $db->select();
     $select_survey->from('profile_survey', array('name', 'english_name', 'state'));
     $select_survey->join('poll_participation', 'poll_participation.poll_id = profile_survey.id', 'poll_participation.date');
     $select_survey->join('reward_point_transaction_record', 'reward_point_transaction_record.date = poll_participation.date and reward_point_transaction_record.consumer_id = poll_participation.consumer_id', 'point_amount');
     $select_survey->where('reward_point_transaction_record.transaction_id =3');
     $select_survey->where('poll_participation.consumer_id = ?', $uid);
     $select_survey->order('poll_participation.date desc');
     $surveysAll = $db->fetchAll($select_survey);
     $this->view->surveysall = $surveysAll;
     // point detail
     /*$db = Zend_Registry::get('db');
     		$select_pointdetail = $db->select();
     		$select_pointdetail->from();
     		$select_pointdetail->join();
     		$select_pointdetail->join();
     		$select_pointdetail->where();
     		$select_pointdetail->where();
     		$select_pointdetail->order();
     		$surveysAll = $db->fetchAll($select_pointdetail);*/
     //var_dump($campaignsAll);die;
     $selectTotal = $db->select();
     $selectTotal->from("report", array('count(id) as num', 'campaign_id'));
     $selectTotal->where("consumer_id =" . $uid);
     $selectTotal->group('campaign_id');
     $totalData = $db->fetchAll($selectTotal);
     if (count($totalData)) {
         foreach ($totalData as $val) {
             $reportsInfo[$val['campaign_id']] = $val['num'];
         }
     }
     $this->view->totalreports = $reportsInfo;
     // total points
     $this->view->totalPoints = $db->fetchOne("SELECT sum(point_amount) FROM reward_point_transaction_record WHERE transaction_id!=4 and consumer_id = :temp", array('temp' => $uid));
     if (empty($this->view->totalPoints)) {
         $this->view->totalPoints = 0;
     }
     //redeem points
     $this->view->redeemPoints = $db->fetchOne("SELECT sum(point_amount) FROM reward_point_transaction_record WHERE consumer_id = :temp", array('temp' => $uid));
     if (empty($this->view->redeemPoints)) {
         $this->view->redeemPoints = 0;
     }
     //usable points
     $today = date("Y-m-d", time());
     $this->view->usablePoints = $db->fetchOne("SELECT sum(point_amount) FROM reward_point_transaction_record WHERE (consumer_id = :temp and date <:temp2) or (consumer_id = :temp and date >=:temp2 and transaction_id=4) ", array('temp' => $uid, 'temp2' => date("Y-m-d", strtotime("{$today}   -30   day"))));
     if (empty($this->view->usablePoints)) {
         $this->view->usablePoints = 0;
     }
     $redeemSelect = $db->select();
     $redeemSelect->from('product_order', array('product_order.amount as amount', 'product_order.create_date', 'product_order.state as pstate', 'product_order.id as pid', 'product.name', 'reward_point_transaction_record.point_amount'));
     $redeemSelect->join('product', 'product_order.product_id=product.id');
     $redeemSelect->join('reward_point_transaction_record', 'product_order.reward_point_transaction_record_id=reward_point_transaction_record.id');
     $redeemSelect->where('product_order.consumer_id=?', $uid);
     $this->view->redeem = $db->fetchAll($redeemSelect);
     $translate = new Zend_Translate('array', array("Value is required and can't be empty" => $this->view->translate('validation_null')));
     $form = new ConsumerSearchForm();
     $form->setTranslator($translate);
     if ($this->_request->getParam('search') != null) {
         $search = $this->_request->getParam('search');
         $form->search->setValue($search);
     }
     $this->view->form = $form;
 }
 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');
         }
     }
 }