function indexAction()
 {
     $this->view->title = $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("CAMPAIGNS");
     $this->view->activeTab = 'Campaigns';
     $consumerModel = new Consumer();
     $consumer = $consumerModel->find($this->_currentUser->id)->current();
     $currentTime = date("Y-m-d H:i:s");
     $db = Zend_Registry::get('db');
     $select = $db->select();
     $select->from('campaign', '*');
     $select->where('campaign_invitation.consumer_id = ?', $consumer->id);
     $select->where('campaign.expire_date > ?', $currentTime);
     $select->join('campaign_invitation', 'campaign.id = campaign_invitation.campaign_id');
     $select->join('campaign_participation', 'campaign_invitation.id = campaign_participation.campaign_invitation_id', 'accept_date');
     $select->where("campaign_participation.state != 'COMPLETED'");
     $this->view->activeCampaigns = $db->fetchAll($select);
     $select2 = $db->select();
     $select2->from('campaign_invitation', array('campaign_invitation.*', 'campaign.*'));
     $select2->join('campaign', 'campaign.id = campaign_invitation.campaign_id and campaign.type="campaign"', 'name');
     $select2->where('campaign_invitation.consumer_id = ?', $this->_currentUser->id);
     $select2->where('campaign.expire_date > ?', $currentTime);
     $select2->where('campaign_invitation.state = ?', 'NEW');
     $select2->order('campaign_invitation.create_date DESC');
     $this->view->campaignInvitations = $db->fetchAll($select2);
     //	Zend_Debug::dump($this->view->campaignInvitations);
 }
 public function loginAction()
 {
     $request = $this->getRequest();
     $config = Zend_Registry::get('config');
     // Check if we have a POST request
     if (!$request->isPost()) {
         $this->_helper->redirector('index', 'index');
     }
     $lang = $this->getRequest()->getPost('lang');
     if (isset($lang) && $lang != null) {
         $langNamespace = new Zend_Session_Namespace('Lang');
         $langNamespace->lang = $lang;
     }
     // Get our form and validate it
     $form = new LoginForm();
     if (!$form->isValid($request->getPost())) {
         // Invalid entries
         $this->_flashMessenger->addMessage('Email or Password is required and its length should between 6 and 20');
         $this->view->form = $form;
         $this->_helper->redirector('loginfailed', 'index');
     }
     // Get our authentication adapter and check credentials
     $adapter = new LoginAuthAdapter($form->getValue('email'), $form->getValue('password'));
     $auth = Zend_Auth::getInstance();
     $result = $auth->authenticate($adapter);
     if ($result->isValid()) {
         // We're authenticated! Redirect to the home page
         $db = Zend_Registry::get('db');
         $consumer_id = $db->fetchOne("SELECT id FROM consumer WHERE email = :temp or login_phone = :temp and state='ACTIVE'", array('temp' => $form->getValue('email')));
         $consumerModel = new Consumer();
         $consumer = $consumerModel->find($consumer_id)->current();
         $authNamespace = new Zend_Session_Namespace('Zend_Auth');
         $authNamespace->user = $consumer;
         $authNamespace->role = 'consumer';
         //log
         $logModel = new Log();
         $logId = $logModel->insert(array('consumer_id' => $consumer->id, 'date' => date("Y-m-d H:i:s"), 'event' => 'LOGIN'));
         $url = $form->getValue('url');
         if (isset($url) && !empty($url)) {
             $this->_redirector = $this->_helper->getHelper('Redirector');
             $this->_redirector->gotoUrl($url);
         } else {
             $this->_helper->redirector('index', 'home');
         }
     } else {
         // Invalid credentials
         $this->_flashMessenger->addMessage('Invalid credentials provided');
         $this->view->form = $form;
         $this->_helper->redirector('loginfailed', 'index');
     }
 }
 function adminreportbatchreplyAction()
 {
     $reportId = $this->_request->getParam('report_id');
     $this->view->batchId = $this->_request->getParam('batch_id');
     $reportModel = new Report();
     $report = $reportModel->find($reportId)->current();
     $this->view->report_id = $reportId;
     $consumerModel = new Consumer();
     $this->view->consumer = $consumerModel->find($report['consumer_id'])->current();
     $campaignModel = new Campaign();
     $campaign = $campaignModel->find($report['campaign_id'])->current();
     $this->view->campaign_name = $campaign->name;
     $this->view->campaign_id = $campaign->id;
     //get new report
     $config = Zend_Registry::get('config');
     $url = $config->indicate2->home . "/report/showAnswer/accessCode/" . $report['accesscode'];
     //		$contents = file_get_contents($url); // deprecated by ice, for performance reason
     //		$handle = @fopen($url, "r");
     //		stream_set_timeout($handle, 0, 500);// 500  ms
     //		$contents = stream_get_contents($handle);
     //		$info = stream_get_meta_data($handle);
     //		fclose($handle);
     $this->view->url = $url;
     $this->view->title = "Reply Reports";
     $this->view->activeTab = "Reply Reports";
     $this->view->mailForm = new ReplyReportForm();
     $this->view->mailForm->email->setValue($this->view->consumer['email']);
     $db = Zend_Registry::get('db');
     $selectReportSourceAndPoint = $db->select();
     $selectReportSourceAndPoint->from('report', array('source', 'campaign_id'))->joinLeft('reward_point_transaction_record', 'report.reward_point_transaction_record_id = reward_point_transaction_record.id', 'point_amount')->where('report.id = ?', $reportId);
     $reportSourceAndPoint = $db->fetchAll($selectReportSourceAndPoint);
     //		Zend_Debug::dump($reportSourceAndPoint);
     if (isset($reportSourceAndPoint)) {
         $this->view->reportSource = $reportSourceAndPoint[0]['source'];
         if ($this->view->reportSource == 'sms' || empty($this->view->consumer['email'])) {
             $this->view->reportSource = 'sms';
             $this->view->mailForm->email->setLabel($this->view->translate('Phone:'));
             $this->view->mailForm->email->setValue($this->view->consumer['phone']);
             $this->view->mailForm->message->setLabel($this->view->translate('Admin_Reply_Message_Sms_Note'));
             $this->view->mailForm->message->setAttribs(array('rows' => 2, 'cols' => 50));
         }
     }
     if (isset($reportSourceAndPoint) && $reportSourceAndPoint[0]['point_amount'] != null) {
         $this->view->mailForm->grade->setValue($reportSourceAndPoint[0]['point_amount']);
     }
     //tag for report
     $selectTags = $db->select();
     $selectTags->from('tags', array('name', 'id', 'sort'))->where("module = 'REPORT' and (campaign_id is null or campaign_id=" . $reportSourceAndPoint[0]['campaign_id'] . ")")->order('sort');
     $this->view->tags = $db->fetchAll($selectTags);
     $selectSelectedTags = $db->select();
     $selectSelectedTags->from('report_tag', array('tag_id'))->where('report_id = ?', $reportId);
     $selectedTags = $db->fetchAll($selectSelectedTags);
     $this->view->selectedTagsArray = array();
     foreach ($selectedTags as $tag) {
         $this->view->selectedTagsArray[$tag['tag_id']] = '1';
     }
     //		Zend_Debug::dump($this->view->selectedTagsArray);
     $replyModel = new Reply();
     $reply = $replyModel->fetchRow('report_id = ' . $reportId);
     if ($reply != null) {
         $this->view->mailForm->message->setValue($reply['content']);
         $this->view->status = $reply['status'];
     }
     $this->view->usetime = $reply['usetime'];
     var_dump($reply['usetime']);
     $this->view->mailForm->subject->setValue($this->view->translate('Admin_Reply_WOM_Report_Subject'));
     $this->_helper->layout->setLayout("layout_admin");
     //organize tag list
     $tagHash = array();
     foreach ($this->view->tags as $tag) {
         $tagHash[$tag['id']] = $tag['name'];
     }
     // get old reports of this campaign
     $select = $db->select();
     $select->from('report', array('id', 'accesscode', 'create_date'))->where('consumer_id = ?', $this->view->consumer['id'])->where('campaign_id = ?', $campaign->id)->order('create_date desc');
     $oldreportArray = $db->fetchAll($select);
     $this->view->oldreports = array();
     $i = 1;
     foreach ($oldreportArray as $oldreport) {
         $oldTags = '';
         if ($report['accesscode'] != $oldreport["accesscode"]) {
             $this->view->oldreports[$oldreport["accesscode"]]['url'] = $config->indicate2->home . "/report/showAnswer/accessCode/" . $oldreport["accesscode"];
             $reply = $replyModel->fetchRow('report_id = ' . $oldreport['id']);
             if ($reply['admin_id'] != null) {
                 $adminModel = new Admin();
                 $admin = $adminModel->fetchRow('id = ' . $reply['admin_id']);
                 $adminname = $admin['name'];
             } else {
                 $adminname = '';
             }
             $this->view->oldreports[$oldreport["accesscode"]]['id'] = $oldreport['id'];
             $this->view->oldreports[$oldreport["accesscode"]]['create_date'] = $oldreport['create_date'];
             $this->view->oldreports[$oldreport["accesscode"]]['adminname'] = $adminname;
             $this->view->oldreports[$oldreport["accesscode"]]['replydate'] = $reply['date'];
             $this->view->oldreports[$oldreport["accesscode"]]['replycontent'] = $reply['content'];
             //tag
             $oldreportTagSelect = $db->select();
             $oldreportTagSelect->from('report_tag', 'tag_id')->where('report_tag.report_id = ?', $oldreport['id']);
             $oldreportTag = $db->fetchAll($oldreportTagSelect);
             foreach ($oldreportTag as $tag) {
                 $oldTags .= $this->view->translate('Report_Tag_' . $tagHash[$tag['tag_id']]) . " ";
             }
             $this->view->oldreports[$oldreport["accesscode"]]['tag'] = $oldTags;
         }
     }
     // get old reports for other campaigns
     $select = $db->select();
     $select->from('report', array('id', 'accesscode', 'create_date'))->where('consumer_id = ?', $this->view->consumer['id'])->where('campaign_id != ?', $campaign->id)->order('create_date desc');
     $oldreportArray = $db->fetchAll($select);
     $this->view->otheroldreports = array();
     $i = 1;
     foreach ($oldreportArray as $oldreport) {
         $oldTags = '';
         if ($report['accesscode'] != $oldreport["accesscode"]) {
             $this->view->otheroldreports[$oldreport["accesscode"]]['url'] = $config->indicate2->home . "/report/showAnswer/accessCode/" . $oldreport["accesscode"];
             $reply = $replyModel->fetchRow('report_id = ' . $oldreport['id']);
             if ($reply['admin_id'] != null) {
                 $adminModel = new Admin();
                 $admin = $adminModel->fetchRow('id = ' . $reply['admin_id']);
                 $adminname = $admin['name'];
             } else {
                 $adminname = '';
             }
             $this->view->otheroldreports[$oldreport["accesscode"]]['id'] = $oldreport['id'];
             $this->view->otheroldreports[$oldreport["accesscode"]]['create_date'] = $oldreport['create_date'];
             $this->view->otheroldreports[$oldreport["accesscode"]]['adminname'] = $adminname;
             $this->view->otheroldreports[$oldreport["accesscode"]]['replydate'] = $reply['date'];
             $this->view->otheroldreports[$oldreport["accesscode"]]['replycontent'] = $reply['content'];
             //tag
             $oldreportTagSelect = $db->select();
             $oldreportTagSelect->from('report_tag', 'tag_id')->where('report_tag.report_id = ?', $oldreport['id']);
             $oldreportTag = $db->fetchAll($oldreportTagSelect);
             foreach ($oldreportTag as $tag) {
                 $oldTags .= $this->view->translate('Report_Tag_' . $tagHash[$tag['tag_id']]) . " ";
             }
             $this->view->otheroldreports[$oldreport["accesscode"]]['tag'] = $oldTags;
         }
     }
     $reportImages = new ReportImages();
     $reportImagesData = $reportImages->fetchAll('report=' . $reportId . ' and consumer=' . $report['consumer_id']);
     $this->view->reportImages = $reportImagesData;
 }
 function callbackAction()
 {
     // 		if($this->_request->getParam('state')== $_SESSION['state']) //csrf
     // 	    {
     $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&" . "client_id=" . $_SESSION["appid"] . "&redirect_uri=" . urlencode($_SESSION["callback"]) . "&client_secret=" . $_SESSION["appkey"] . "&code=" . $_REQUEST["code"];
     $response = get_url_contents($token_url);
     if (strpos($response, "callback") !== false) {
         $lpos = strpos($response, "(");
         $rpos = strrpos($response, ")");
         $response = substr($response, $lpos + 1, $rpos - $lpos - 1);
         $msg = json_decode($response);
         if (isset($msg->error)) {
             echo "<h3>error:</h3>" . $msg->error;
             echo "<h3>msg  :</h3>" . $msg->error_description;
             exit;
         }
     }
     $params = array();
     parse_str($response, $params);
     //debug
     //print_r($params);
     //set access token to session
     $_SESSION["access_token"] = $params["access_token"];
     include_once "user/get_user_info.php";
     $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=" . $_SESSION['access_token'];
     $str = get_url_contents($graph_url);
     if (strpos($str, "callback") !== false) {
         $lpos = strpos($str, "(");
         $rpos = strrpos($str, ")");
         $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
     }
     $me = json_decode($str);
     if (isset($me->error)) {
         echo "<h3>error:</h3>" . $me->error;
         echo "<h3>msg  :</h3>" . $me->error_description;
         exit;
     }
     //debug
     //echo("Hello " . $user->openid);
     //set openid to session
     $_SESSION["openid"] = $me->openid;
     $user = get_user_info();
     $uid = $me->openid;
     $adapter = new QQLoginAuthAdapter($uid);
     $auth = Zend_Auth::getInstance();
     $result = $auth->authenticate($adapter);
     $consumerModel = new Consumer();
     $db = Zend_Registry::get('db');
     $consumer_id = $db->fetchOne("SELECT id FROM consumer WHERE qqid = :temp and state='ACTIVE'", array('temp' => $uid));
     $consumer = $consumerModel->find($consumer_id)->current();
     if ($result->isValid()) {
         $authNamespace = new Zend_Session_Namespace('Zend_Auth');
         $authNamespace->user = $consumer;
         $authNamespace->role = 'consumer';
         $logModel = new Log();
         $logId = $logModel->insert(array('consumer_id' => $consumer->id, 'date' => date("Y-m-d H:i:s"), 'event' => 'LOGIN'));
         $this->_helper->redirector('index', 'home');
     } else {
         $this->_helper->redirector('register', 'register');
     }
     // 	    }
     // 	    else
     // 	    {
     // 	        echo("The state does not match. You may be a victim of CSRF.");
     // 	    }
 }
 function first()
 {
     $db = Zend_Registry::get('db');
     $str = $_COOKIE;
     $uid = substr($str["weibojs_1864117054"], -10);
     if (isset($uid)) {
         $adapter = new WeiboLoginAuthAdapter($uid);
         $auth = Zend_Auth::getInstance();
         $result = $auth->authenticate($adapter);
         $consumerModel = new Consumer();
         $consumer_id = $db->fetchOne("SELECT id FROM consumer WHERE weiboid = :temp and state='ACTIVE'", array('temp' => $uid));
         $consumer = $consumerModel->find($consumer_id)->current();
         if ($result->isValid()) {
             $authNamespace = new Zend_Session_Namespace('Zend_Auth');
             $authNamespace->user = $consumer;
             $authNamespace->role = 'consumer';
             $logModel = new Log();
             $logId = $logModel->insert(array('consumer_id' => $consumer->id, 'date' => date("Y-m-d H:i:s"), 'event' => 'LOGIN'));
             $this->_helper->redirector('index', 'tag');
         }
     }
 }
 function changepasswordAction()
 {
     $this->view->activeTab = 'Profile';
     $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("Change_Password");
     $form = new PasswordForm();
     $consumerModel = new Consumer();
     if ($this->_request->isPost()) {
         //POST
         $formData = $this->_request->getPost();
         if ($form->isValid($formData)) {
             if ($formData['newpassword'] == $formData['repeat']) {
                 $id = $this->_currentUser->id;
                 $consumer = $consumerModel->find($id)->current();
                 if ($consumer->password == md5($formData['oldpassword'])) {
                     $consumer->password = md5($form->getValue('newpassword'));
                     $consumer->save();
                     $form->populate($consumer->toArray());
                     $this->view->showMessage = $this->view->translate('Save_Successfully');
                 } else {
                     $this->view->showMessage = $this->view->translate('Password_is_wrong');
                 }
             } else {
                 $this->view->showMessage = $this->view->translate('New_password_and_repeat_must_be_consistent');
             }
         }
         //			Zend_Debug::dump($this->veiw->showMessage);
     } else {
         //GET
         $email = $this->_currentUser->email;
         $form->setDefault('email', $email);
     }
     $this->view->form = $form;
 }
 function redeemAction()
 {
     $this->view->title = $this->view->translate("Wildfire") . " - " . $this->view->translate("GIFT_REDEEM");
     if ($this->_request->isPost()) {
         $formData = $this->_request->getPost();
         $cartNamespace = new Zend_Session_Namespace('Cart');
         if ($cartNamespace->list == null) {
             $this->_redirect('gift/list');
             return;
         }
         // validate consumer info
         $consumerModel = new Consumer();
         $consumer = $consumerModel->fetchRow("email = '" . $this->_currentUser->email . "' and password = MD5('" . $formData['password'] . "')");
         if ($consumer == null) {
             $this->_flashMessenger->addMessage($this->view->translate("Gift_consumer_info_incorrect"));
             $this->_flashMessenger->addMessage(true);
             $this->_redirect('gift/confirmcart');
             return;
         }
         // check redeem condition
         $db = Zend_Registry::get('db');
         $selectTotalCompletedCampaign = $db->select();
         $selectTotalCompletedCampaign->from('campaign_participation', 'count(*)')->join('campaign_invitation', 'campaign_participation.campaign_invitation_id = campaign_invitation.id', null)->where('campaign_invitation.consumer_id = ?', $this->_currentUser->id);
         $this->view->completedCampaignAmount = $db->fetchOne($selectTotalCompletedCampaign);
         //			$selectTotalSubmittedReport = $db->select();
         //			$selectTotalSubmittedReport->from('report', 'count(*)')
         //			->where('state = "APPROVED"')
         //			->where('consumer_id = ?', $this->_currentUser->id);
         //			$this->view->submittedReportAmount = $db->fetchOne($selectTotalSubmittedReport);
         if ($this->view->completedCampaignAmount < 1 || $this->_currentUser->pest != null && $this->_currentUser->pest == 1) {
             $this->_flashMessenger->addMessage($this->view->translate("Gift_can_not_redeem_gift"));
             $this->_flashMessenger->addMessage(true);
             $this->_redirect('gift/confirmcart');
             return;
         }
         // check the point
         $selectUsablePoints = $db->select();
         $selectUsablePoints->from('reward_point_transaction_record', 'SUM(point_amount)')->where("consumer_id = ?", $this->_currentUser->id);
         $usablePoints = (int) $db->fetchOne($selectUsablePoints);
         $amountSelectedProductPoint = 0;
         foreach ($cartNamespace->list as $product) {
             $selectSelectedProductPoint = $db->select();
             $selectSelectedProductPoint->from('product', 'point')->where("id = " . $product['id']);
             $selectedProductPoint = (int) $db->fetchOne($selectSelectedProductPoint);
             $amountSelectedProductPoint += $product['amount'] * $selectedProductPoint;
         }
         if ($amountSelectedProductPoint > $usablePoints) {
             $this->_flashMessenger->addMessage($this->view->translate("Gift_have_no_enough_point"));
             $this->_flashMessenger->addMessage(true);
             $this->_redirect('gift/confirmcart');
             return;
         }
         // save shipping info
         $consumerModel = new Consumer();
         $id = $this->_currentUser->id;
         $consumer = $consumerModel->find($id)->current();
         $consumer->recipients_name = $formData['recipients_name'];
         $consumer->phone = $formData['phone'];
         $consumer->address1 = $formData['address1'];
         $consumer->postalcode = $formData['postalcode'];
         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['province'] == '' && $formData['englishcity'] == null) {
             $consumer->city = null;
             $consumer->province = null;
         }
         $consumer->save();
         // save exchange records
         $currentTime = date("Y-m-d H:i:s");
         $rewardPointTransactionRecordModel = new RewardPointTransactionRecord();
         $prodcutOrderModel = new ProductOrder();
         $notificationModel = new Notification();
         $total_redeem_point = 0;
         foreach ($cartNamespace->list as $product) {
             // add records to reward_point_transaction_record table
             $rewardPointTransactionRecord = array("consumer_id" => $this->_currentUser->id, "DATE" => $currentTime, "transaction_id" => '4', "point_amount" => -$product['amount'] * $product['point']);
             $transactionRecordId = $rewardPointTransactionRecordModel->insert($rewardPointTransactionRecord);
             // add records to product_order table
             $prodcutOrder = array('consumer_id' => $this->_currentUser->id, 'product_id' => $product['id'], 'create_date' => $currentTime, 'state' => 'NEW', 'reward_point_transaction_record_id' => $transactionRecordId, 'amount' => $product['amount']);
             $prodcutOrderId = $prodcutOrderModel->insert($prodcutOrder);
             // roll back if an exception occurred
             // ...
             $total_redeem_point += $product['amount'] * $product['point'];
         }
         // add notification
         $notificationModel->createRecord("REDEEM_POINT", $this->_currentUser->id, $total_redeem_point);
         $this->paidGifts = $cartNamespace->list;
         $cartNamespace->list = null;
         // show redeem.phtml with "... Successfully"
         $this->_flashMessenger->addMessage("Gift_submit_orders_successfully");
         $this->_flashMessenger->addMessage(false);
         $this->_flashMessenger->addMessage($this->paidGifts);
         $this->_redirect('gift/thankyou');
     } else {
         $this->_redirect('gift/list');
     }
 }