<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: AffiliateController.php, v 1.0 2011/06/14 20:52:11
//
class AffiliaterequestController extends Controller {
    public function __construct() {
        $this->init();
        if (!__AFFILIATE_MODULE) {
            $this->setRedirect('index/');
        }
    }
    //LIST AFFILIATE REQUESTS
    public function indexAction() {
        $ObjAffiliate       = new AffiliateModel();
        $ObjSettings        = new SettingsModel();
        $settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
        $link               = 'affiliaterequest/index/';
        $fromDate           = ($this->getRequestValue("fromDate")) ? $this->urlDate($this->getRequestValue("fromDate"), 'get', $settingsDateFormat) : $ObjSettings->getFormattedDate("01" . date("-m-Y"));
        $toDate             = ($this->getRequestValue("toDate")) ? $this->urlDate($this->getRequestValue("toDate"), 'get', $settingsDateFormat) : $ObjSettings->getFormattedDate(date('d-m-Y'));
        $date               = array();
        if (isset($_POST['submitAffiliateRequestSeach'])) {
            if ($_POST['affiliateReqStatus']) {
                $link .= 'status/' . $this->encodeUrlString(strtolower(trim($_POST['affiliateReqStatus']))) . '/';
            }
            if ($_POST['affiliateSeachName']) {
                $link .= 'keyword/' . $this->encodeUrlString($_POST['affiliateSeachName']) . '/';
            }
            if ($_POST['affReqDate']) {
                $date     = explode(" - ", $this->escape($_POST['affReqDate']));
                $fromdate = $date[0];
                $todate   = isset($date[1]) ? $date[1] : '';
                $link .= "fromDate/" . $this->urlDate($fromdate, 'set', $settingsDateFormat) . "/";
                if ($todate) {
                    $link .= "toDate/" . $this->urlDate($todate, 'set', $settingsDateFormat) . "/";
                }
            }
            $this->setRedirect($link);
        }
        $this->ObjView->assign('link', $link);
        $fromDate   = $this->escape($this->urlDate($this->getRequestValue('fromDate'), 'get', $settingsDateFormat));
        $toDate     = $this->escape($this->urlDate($this->getRequestValue('toDate'), 'get', $settingsDateFormat));
        $affReqDate = $fromDate;
        if ($toDate) {
            $affReqDate .= " - " . $toDate;
        }
        $affiliateSeachName = $this->decodeUrlString($this->getRequestValue('keyword'));
        $affiliateReqStatus = $this->getRequestValue("status");
        $this->ObjView->assign("affReqDate", $affReqDate);
        $affiliateReqStatus = ucfirst($affiliateReqStatus);
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resListAffiliateRequest = $ObjAffiliate->listAffiliateRequests($this->convertDateForSQL($fromDate, $settingsDateFormat), $this->convertDateForSQL($toDate, $settingsDateFormat), $affiliateSeachName, $affiliateReqStatus, $limitFrom, $limitTo);
        $resPagesCount           = $this->setPagesCount($key = 'page');
        $this->ObjView->assign('date_format', $settingsDateFormat);
        $this->ObjView->assign('hiddenAffiliateSearchName', $affiliateSeachName);
        $this->ObjView->assign('hiddenAffiliateReqStatus', $affiliateReqStatus);
        $this->ObjView->assign('resListAffiliateRequest', $resListAffiliateRequest);
        $this->ObjView->assign("declinePrivilege", $this->ObjCommon->hasPrivilege('affiliaterequest/decline/'));
        $this->ObjView->assign("approvePrivilege", $this->ObjCommon->hasPrivilege('affiliate/approveaffiliaterequest/'));
        $this->ObjView->assign("showPrivilege", $this->ObjCommon->hasPrivilege('affiliaterequest/show/'));
        $this->ObjView->display('AffiliateRequestList.tpl');
    }
    //AFFILIATE REQUEST DETAILS VIEW
    public function showAction() {
        $ObjAffiliate            = new AffiliateModel();
        $affiliateReqID          = $this->getRequestValue("id");
        $date_format             = $this->getSettingValue('site_settings', 'date_format');
        $languageIDs             = $this->ObjView->languageIDs;
        $resListAffiliateRequest = $ObjAffiliate->getAffiliateRequestDetails($affiliateReqID);
        if (!$resListAffiliateRequest) {
            $this->setRedirect('/affiliaterequest/index/');
        }
        $previousURl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
        $history     = $this->getPreviousURL();
        $siteUrl     = rtrim($this->getSettingValue('site_settings', 'global_http_url'), '/') . '/manage';
        if (strpos($previousURl, '/affiliaterequest/index/') > 0) {
            $previousURl = str_replace($siteUrl, '', $previousURl);
        } else
            $previousURl = "/affiliaterequest/index/";
        $website    = $resListAffiliateRequest['affiliateReqWebsiteURL'];
        $website    = str_replace("HTTP://", "http://", $website);
        $websiteStr = strstr($website, 'http://');
        if ($websiteStr == "") {
            $website = "http://" . $website;
        }
        $resListAffiliateRequest['website'] = $website;
        $this->ObjView->assign('resListAffiliateRequest', $resListAffiliateRequest);
        $this->ObjView->assign('previousURL', $previousURl);
        $this->ObjView->assign('date_format', $date_format);
        $this->ObjView->display('AffiliateRequestShow.tpl');
    }
    // AFFILIATE REQUEST DECLINE ACTION	
    public function declineAction() {
        $ObjAffiliate     = new AffiliateModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $affiliateReqID   = $this->getRequestValue('id');
        $fromSection      = $this->getRequestValue('from');
        $affiliateRequest = $ObjAffiliate->getAffiliateRequestDetails($affiliateReqID);
        if (empty($affiliateRequest) || $affiliateRequest['affiliateReqStatus'] != "New") {
            echo '<script type="text/javascript">parent.$.fancybox.close();</script>';
            exit;
        }
        $global_url = $this->getSettingValue('site_settings', 'global_http_url');
        if ($this->ObjForm->isValid('frmDeclineRequest', 'submitDeclineRequest')) {
            $DeclinedReason = nl2br($_POST['affiliateDeclineReason']);
            $ObjAffiliate->changeAffiliateRequestStatus($affiliateReqID, 'Declined', $_POST);
            $languageIDs            = $this->ObjView->languageIDs;
            $languageIDs['Current'] = $affiliateRequest['affiliateReqLanguageID'];
            $emailbody              = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'affiliate-request-declined');
            $from                   = array(
                $emailbody['emailFromEmail'],
                $emailbody['emailFromName']
            );
            $siteName               = $this->getSettingValue('site_settings', 'site_name');
            $siteUrl                = $this->getSettingValue('site_settings', 'global_http_url');
            $companyLogo            = $this->getSettingValue('company_information', 'company_logo');
            $userName               = $affiliateRequest['affiliateReqFirstName'] . " " . $affiliateRequest['affiliateReqLastName'];
            $subject                = $emailbody['emailSubject'];
            $bodytag                = str_replace('{$siteName}', $siteName, $emailbody['emailHtml']);
            $bodytag                = str_replace('{$userName}', $userName, $bodytag);
            //$bodytag 		=   str_replace('{$affiliateLogin}',$affiliateRequest['affiliateLogin'],$bodytag);
            $bodytag                = str_replace('{$affiliateDeclineReason}', $DeclinedReason, $bodytag);
            $bodytag                = str_replace('{$companyLogo}', $companyLogo, $bodytag);
            $this->ObjView->assign('emailbody', $bodytag);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('userName', $userName);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $body                           = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
            $mail_settings                  = $this->getSettingValue('mail_settings');
            $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
            $ObjMail                        = new Mail($mail_settings);
            $emailID                        = trim(addslashes($affiliateRequest['affiliateReqEmail']));
            $ObjMail->IsHTML();
            $ObjMail->sendMail($from, $emailID, $subject, $body);
            if ($ObjMail->Send()) {
            }
            $this->setMessage("language.msgAffiliateRequestDecline", "success");
            echo '<script type="text/javascript">parent.location.reload();parent.$.fancybox.close();</script>';
            exit;
        }
        $this->ObjView->assign('curraffiliateReqID', $affiliateReqID);
        $this->ObjView->display('AffiliateRequestDecline.tpl');
    }
}