function editcontactAction()
 {
     $campaign = $this->_request->getParam('cid');
     $campaignModel = new Campaign();
     $cam = $campaignModel->find($campaign)->current();
     $form = new ConsumerContactForm(array('relative' => $cam->relative));
     $consumerModel = new Consumer();
     if ($this->_request->isPost()) {
         //POST
         $formData = $this->_request->getPost();
         //Zend_Debug::dump($form->isValid ( $formData ));die;
         if ($form->isValid($formData)) {
             $id = $this->_currentUser->id;
             $consumer = $consumerModel->find($id)->current();
             $consumer->recipients_name = $form->getValue('recipients_name');
             $consumer->phone = $form->getValue('phone');
             $consumer->company_phone = $form->getValue('telephone');
             $consumer->address1 = $form->getValue('address1');
             $consumer->postalcode = $form->getValue('postalcode');
             if ($formData['qq'] != null) {
                 $consumer->qq = $formData['qq'];
             }
             if ($formData['city'] != null && $formData['province'] != null) {
                 $consumer->city = $formData['city'];
                 $consumer->province = $formData['province'];
             }
             if ($formData['englishcity'] != null) {
                 $consumer->city = $formData['englishcity'];
                 $consumer->province = null;
             }
             if ($formData['birthdate'] != null) {
                 $consumer->birthdate = $formData['birthdate'];
             }
             if ($formData['qq'] != null) {
                 $consumer->qq = $formData['qq'];
             }
             if ($formData['province'] == '' && $formData['englishcity'] == null) {
                 $consumer->city = null;
                 $consumer->province = null;
             }
             $consumer->save();
             $authNamespace = new Zend_Session_Namespace('Zend_Auth');
             $authNamespace->user = $consumer;
             $form->populate($consumer->toArray());
             $consumerextraModel = new ConsumerExtraInfo();
             $consumerextra = $consumerextraModel->fetchRow('consumer_id = ' . $this->_currentUser->id);
             if ($consumerextra == null) {
                 $consumerextra = $consumerextraModel->createRow();
                 $consumerextra->consumer_id = $this->_currentUser->id;
             }
             $consumerextra->gender = isset($formData['gender']) ? $formData['gender'] : null;
             $consumerextra->birthdate = $formData['birthdate'] != null ? $formData['birthdate'] : null;
             $consumerextra->education = $formData['education'];
             $consumerextra->income = $formData['income'];
             $consumerextra->save();
             //2011-05-03 ham.bao add the related friends
             $consumerFriend = new ConsumerFriend();
             $consumerFriend->delete('consumer =' . $consumer->id . ' and campaign=' . $campaign);
             //delete
             /*foreach ( $formData as $key =>$val ){
             			$consumerFriend = new ConsumerFriend();
             			if((substr($key, 0,6) == 'friend')&& ($val != '')){
             				$friend = $consumerFriend->createRow();
             				$friend->consumer = $id;
             				$friend->campaign = $campaign;
             				$friend->friend = $val;
             				$friend->date = date('m-d-Y H:i:s');
             				$friend->save();
             			}
             		}*/
             //2011-05-03 ham.bao add the related friends
             $campaign_model = new Campaign();
             $campaign_campaign = $campaign_model->find($campaign)->current();
             //				Zend_Debug::dump($campaign_campaign->relative);die;
             //new
             for ($i = 1; $i <= $campaign_campaign->relative; $i++) {
                 if ($formData['friend_name_' . $i] && $formData['friend_name_' . $i] != '') {
                     $consumerFriend = new ConsumerFriend();
                     $friend = $consumerFriend->createRow();
                     $friend->consumer = $id;
                     $friend->campaign = $campaign;
                     $friend->name = $formData['friend_name_' . $i];
                     //change column name in db
                     $friend->email = $formData['friend_email_' . $i];
                     //add column in db
                     $friend->message = $formData['friend_message_' . $i];
                     //add column in db
                     $friend->date = date('Y-m-d H:i:s');
                     $friend->save();
                 }
             }
             $this->_helper->redirector('index', 'campaigninvitation');
         } else {
             $this->view->errMessage = "Please fill out all mandatory fields and make sure your emails are correct!";
             $this->_forward('precampaignfinished', 'campaign', null, array('survey' => '643'));
         }
     } else {
     }
 }
 function precampaignfinishedAction()
 {
     $this->view->activeTab = 'Campaign';
     $this->view->title = $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("You_are_in");
     //precampaignsurvey的css使用的是layout_survey
     $this->_helper->layout->setLayout("layout_survey");
     $id = (int) $this->_request->getParam('survey');
     $campaignModel = new Campaign();
     $this->view->campaign = $campaignModel->fetchRow("pre_campaign_survey=" . $id . " or " . "pre_campaign_survey_en=" . $id);
     $db = Zend_Registry::get('db');
     if ($this->view->campaign != null) {
         $campaignId = $this->view->campaign->id;
     }
     $consumer = $this->_currentUser;
     //TOCHECK
     //$select_extra_info = $db->select ();
     //$select_extra_info->from ( "consumer",array ('consumer.*', 'consumer_extra_info.gender as gender','consumer_extra_info.education as education','consumer_extra_info.income as income'  ));
     //$select_extra_info->joinLeft ( "consumer_extra_info","consumer.id = consumer_extra_info.consumer_id",null);
     //$select_extra_info->where("consumer.id = ".$this->_currentUser->id );
     //$consumer2 = $db->fetchRow ( $select_extra_info );
     //Zend_Debug::dump($consumer2);die;
     if ($this->view->campaign != null && $campaignId != null && $campaignId > 0) {
         // check if precampaign poll is finished
         //			$select1 = $db->select();
         //			$select1->from('campaign', 'pre_campaign_survey');
         //			$select1->where('campaign.id = ?',$campaignId);
         //			$previewCamSurvey = $db->fetchOne($select1);
         //			$indicate2Connect = new Indicate2_Connect();
         //			$ids = array($previewCamSurvey);
         //			$wsResult = $indicate2Connect->getAnswerSetCount($consumer->email,$ids);
         //
         //			if ($wsResult==0){
         //				$this->_redirect('campaign/precampaign/survey/'.$previewCamSurvey);
         //			}else{
         $campaignInvitationModel = new CampaignInvitation();
         $campaignInvitation = $campaignInvitationModel->fetchRow("campaign_id=" . $campaignId . " and consumer_id=" . $consumer->id);
         if ($campaignInvitation == null) {
             $this->_redirect('http://community.wildfire.asia/public/home');
         }
         $id = $campaignInvitation->id;
         //Zend_Debug::dump($campaignInvitation);
         $campaignInvitation->state = "ACCEPTED";
         $campaignInvitation->save();
         //2011-05-19 ham.bao add the badge
         //			$consumerBadgeModel = new ConsumerBadge();
         //			$notificationModel = new Notification();
         //			$consumerBadgeData  = $consumerBadgeModel->fetchRow('badge='.$this->view->campaign->badge .' and consumer='.$consumer->id);
         //			if(!count($consumerBadgeData)){
         //				$row = $consumerBadgeModel->createRow();
         //				$row->consumer = $consumer->id;
         //				$row->badge    = $this->view->campaign->badge;
         //				$row->create_date = date("Y-m-d H:i:s");
         //				$row->save();
         //				// add notification
         //				$notificationModel->createRecord("CONSUMER_BADGE",$consumer->id);
         //
         //			}
         //2011-05-19 ham.bao add the badge
         $result = $db->fetchOne("SELECT COUNT(*) FROM campaign_participation WHERE campaign_invitation_id=:t1", array('t1' => $id));
         if ($result == 0) {
             //2011-02-22 ham.bao add the logic to calculate the number of participation
             $campaignModel->update(array('participation' => $this->view->campaign->participation + 1), 'id = ' . $this->view->campaign->id);
             if ($this->view->campaign->participation + 1 >= $this->view->campaign->total) {
                 $campaignInvitationModel->update(array('state' => 'EXPIRED'), ' state = "NEW" and campaign_id =' . $this->view->campaign->id);
                 $signauthcodeModel = new SignupAuthCode();
                 $signauthcodeModel->update(array('auto_invitation' => 0), 'auto_invitation =' . $this->view->campaign->id);
             }
             //create participation
             $campaignParticipationModel = new CampaignParticipation();
             $currentTime = date("Y-m-d H:i:s");
             $row = $campaignParticipationModel->createRow();
             $row->campaign_invitation_id = $id;
             $row->accept_date = $currentTime;
             $row->state = 'NEW';
             $row->save();
             //send "welcome to campaign" mail
             //set the content of mail
             $emailSubject = $this->view->translate('Welcome_to_Spark_Campaign_Email_subject_campaign_' . $campaignId);
             $emailBody = $this->view->translate('Welcome_to_Spark_Campaign_Email_body_campaign_' . $campaignId);
             $stringChange = array('?USERNAME?' => $this->_currentUser['name']);
             $emailBody = strtr($emailBody, $stringChange);
             //send...
             $config = Zend_Registry::get('config');
             $smtpSender = new Zend_Mail_Transport_Smtp($config->smtp->welcome->mail->server, array('username' => $config->smtp->welcome->mail->username, 'password' => $config->smtp->welcome->mail->password, 'auth' => $config->smtp->welcome->mail->auth, 'ssl' => $config->smtp->welcome->mail->ssl, 'port' => $config->smtp->welcome->mail->port));
             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->welcome->mail->username, $this->view->translate('Wildfire'));
             $mail->addTo($this->_currentUser['email']);
             $mail->send();
         }
     }
     //edit ConsumerContactForm();
     $form = new ConsumerContactForm(array('relative' => $this->view->campaign->relative));
     $consumer = $this->_currentUser;
     $order = new Zend_Db_Expr(' id ');
     $consumerFriend = new ConsumerFriend();
     $friends = $consumerFriend->fetchAll('consumer= ' . $consumer->id . ' and campaign=' . $this->view->campaign->id, $order);
     $this->view->friendsNum = count($friends);
     //consumer_extra_info
     /*$consumer_extra_info = new ConsumerExtraInfo();
         //TOCHECK
     		$consumerextra = $consumer_extra_info->fetchAll("consumer_id= ".$consumer->id);*/
     $select_consumer_extra_info = $db->select();
     $select_consumer_extra_info->from('consumer_extra_info', '*')->where('consumer_extra_info.consumer_id = ?', $consumer->id);
     $consumerextra = $db->fetchAll($select_consumer_extra_info);
     if ($this->_request->getPost()) {
         $formData = $this->_request->getPost();
         $form->populate($formData);
         $this->view->city = $formData["city"];
         $this->view->province = $formData["province"];
         $this->view->encity = $formData["city"];
     } else {
         $form->populate($consumer->toArray());
         $form->birthdate->setValue($consumerextra[0]['birthdate']);
         $form->gender->setValue($consumerextra[0]['gender']);
         $form->education->setValue($consumerextra[0]['education']);
         $form->income->setValue($consumerextra[0]['income']);
         $form->telephone->setValue($consumer->company_phone);
         // zh city
         if ($consumer["city"] != NULL && $consumer["province"] != NULL) {
             $this->view->city = $consumer["city"];
             $this->view->province = $consumer["province"];
         }
         // en city
         if ($consumer["city"] != NULL && $consumer["province"] == NULL) {
             $this->view->encity = $consumer["city"];
         }
         if (count($friends)) {
             $i = 1;
             foreach ($friends as $friend) {
                 $name = 'friend_name_' . $i;
                 $email = 'friend_email_' . $i;
                 $phone = 'friend_phone_' . $i;
                 $address = 'friend_address_' . $i;
                 $message = 'friend_message_' . $i;
                 $form->{$name}->setValue($friend->name);
                 $form->{$email}->setValue($friend->email);
                 $form->{$phone}->setValue($friend->phone);
                 $form->{$address}->setValue($friend->address);
                 $form->{$message}->setValue($friend->message);
                 $i++;
             }
         }
     }
     //var_dump($form);die;
     $langNamespace = new Zend_Session_Namespace('Lang');
     $this->view->language = $langNamespace->lang;
     $this->view->form = $form;
     $this->view->friendsLimit = $this->view->campaign->relative;
     //Zend_Debug::dump($this->_request->getPost ());
     //Zend_Debug::dump($this->view->friendsLimit);
 }
 function confirmcartAction()
 {
     $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("GIFT_CONFIRM_CART");
     $cartNamespace = new Zend_Session_Namespace('Cart');
     if ($cartNamespace->list == null) {
         $this->_redirect('gift/list');
     }
     $this->view->email = $this->_currentUser->email;
     if ($this->_request->isPost()) {
         $postData = $this->_request->getPost();
         $ids = $postData['id'];
         $list = array();
         $productModel = new Product();
         foreach ($ids as $id) {
             $product = $productModel->find($id)->current();
             if ($postData['amount_' . $id] >= 1) {
                 $list[count($list) + 1] = array("id" => $id, "amount" => floor($postData['amount_' . $id]), "name" => $product->name, "point" => $product->point);
             } else {
                 if ($postData['amount_' . $id] == 0) {
                     unset($list[count($list) + 1]);
                 } else {
                     $list[count($list) + 1] = array("id" => $id, "amount" => 1, "name" => $product->name, "point" => $product->point);
                 }
             }
         }
         $cartNamespace->list = $list;
         if ($postData['type'] == 'modify') {
             $this->_redirect('gift/cart');
         }
     }
     //edit ConsumerContactForm();
     $consumerModel = new Consumer();
     $form = new ConsumerContactForm();
     $this->view->gifts = $cartNamespace->list;
     $consumer = $consumerModel->find($this->_currentUser->id)->current();
     $form->populate($consumer->toArray());
     $langNamespace = new Zend_Session_Namespace('Lang');
     $this->view->language = $langNamespace->lang;
     // zh city
     if ($consumer["city"] != NULL && $consumer["province"] != NULL) {
         $this->view->city = $consumer["city"];
         $this->view->province = $consumer["province"];
     }
     // en city
     if ($consumer["city"] != NULL && $consumer["province"] == NULL) {
         $this->view->encity = $consumer["city"];
     }
     $this->view->form = $form;
     // total point
     $sum = 0;
     foreach ($cartNamespace->list as $gift) {
         $sum += $gift['point'] * $gift['amount'];
     }
     $this->view->sum = $sum;
     $db = Zend_Registry::get('db');
     $selectUsablePoints = $db->select();
     $selectUsablePoints->from('reward_point_transaction_record', 'SUM(point_amount)')->where("consumer_id = ?", $this->_currentUser->id);
     $usablePoints = (int) $db->fetchOne($selectUsablePoints);
     if ($sum > $usablePoints) {
         $this->_redirect('gift/cart');
     }
     // err message
     $messageArray = $this->_flashMessenger->getMessages();
     if ($messageArray != null) {
         $this->view->showMessage = $messageArray[0];
     }
 }