<?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: IndexController.php, v 1.0 2011/06/14 20:52:11
//
class IndexController extends Controller {
    //AFFILIATE DASHBOARD
    public function indexAction() {
        $ObjAffiliate       = new AffiliateModel();
        $ObjFlot            = new Flot();
        $languageIDs        = $this->ObjView->languageIDs;
        $affiliateUserID    = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $settingsDateFormat = $this->getSettingValue('site_settings', 'date_format');
        if ($affiliateUserID == '') {
            $this->setRedirect("login/");
            exit;
        } //$requestUrlKey != '' && $requestUrlKey != 'index' || $this->ObjSession->getSession("AFFILIATE_USER_ID") == ''
        // Quick Over View -------------------------------------------//
        $quickOverview = $ObjAffiliate->getQuickOverView($affiliateUserID);
        //END : Quick Over View---------------------------------------//
        // Demographics -------------------------------------------//
        $demoGraphic   = $ObjAffiliate->getDemographics($affiliateUserID);
        //END : Demographics---------------------------------------//
        // Traffic Sources -------------------------------------------//
        $lastDay       = mktime(0, 0, 0, date("m"), date("d") - 6, date("Y"));
        $dateFrom      = GregorianToJD(date("m", $lastDay), date("d", $lastDay), date("Y", $lastDay));
        $dateTo        = GregorianToJD(date('m'), date('d'), date('Y'));
        $arr           = array();
        $xCordArray    = array();
        for ($dateIndex = $dateFrom; $dateIndex <= $dateTo; $dateIndex++) {
            list($month, $day, $year) = explode('/', JDToGregorian($dateIndex));
            $month = $month < 10 ? '0' . $month : $month;
            $day   = $day < 10 ? '0' . $day : $day;
            ($dateIndex == $dateFrom) ? $colName = 'clickDate' : $colName = '';
            array_push($arr, "SELECT '" . $year . "-" . $month . "-" . $day . "' $colName");
            //$currentDate = date("Y-m-d", strtotime("+1 day", strtotime($currentDate)));
        } //$dateIndex = $dateFrom; $dateIndex <= $dateTo; $dateIndex++
        $trafficSource                                    = $ObjAffiliate->getTrafficSources($affiliateUserID, implode(' UNION ALL ', $arr));
        //END : Traffic Sources---------------------------------------//
        // Traffic Sources : Graphs--------------------------------//
        $sales                                            = $this->getLanguageValue("lblSales");
        $visits                                           = $this->getLanguageValue("lblViists");
        $registration                                     = $this->getLanguageValue("lblRegistrations");
        $subscriptions                                    = $this->getLanguageValue("lblSubscriptions");
        $importedUser                                     = $this->getLanguageValue("lblImportedUser");
        $importedSubscriber                               = $this->getLanguageValue("lblImportedSubscriber");
        $trafficReportSettings['placeholder']             = 'affiliateHolder';
        $trafficReportSettings['fill']                    = 'false';
        $trafficReportSettings['graphLabels']             = "$sales,$visits,$registration,$subscriptions,$importedUser,$importedSubscriber";
        $trafficReportSettings['graphTotalLabels']        = "$sales,$visits,$registration,$subscriptions,$importedUser,$importedSubscriber";
        $trafficReportSettings['graphTotalHolder']        = 'affiliateHoldergraphTotalHolder';
        $trafficReportSettings['graphTotalHolderUlClass'] = 'graphTotalHolderUl';
        $trafficReportSettings['mode']                    = 'time';
        $trafficReportSettings['tootlTipXLabel']          = 'Day';
        $trafficReportSettings['enableSelection']         = false;
        $trafficReportSettings['graphType']               = 'line';
        $trafficReportSettings['enableSelection']         = 'false';
        $trafficReportSettings['ticksForLineGraph']       = $xCordArray;
        $trafficReportSettings['javascript_path']         = $this->ObjView->_common_js_path;
        $line1                                            = array();
        $line2                                            = array();
        $line3                                            = array();
        $line4                                            = array();
        $line5                                            = array();
        $line6                                            = array();
        foreach ($trafficSource as $key => $value) {
            array_push($line1, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['sales']
            ));
            array_push($line2, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['visitors']
            ));
            array_push($line3, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['registration']
            ));
            array_push($line4, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['subscription']
            ));
            array_push($line5, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['ImportedUser']
            ));
            array_push($line6, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['ImportedSubscriber']
            ));
        } //$trafficSource as $key => $value
        $trafficGraphArray[0] = $line1;
        $trafficGraphArray[1] = $line2;
        $trafficGraphArray[2] = $line3;
        $trafficGraphArray[3] = $line4;
        $trafficGraphArray[4] = $line5;
        $trafficGraphArray[5] = $line6;
        $trafficReportGraph   = $ObjFlot->init($trafficGraphArray, 0, $trafficReportSettings);
        //END : Tarffic Sources------------------------------------------------------------------//
        $this->ObjView->assign("quickOverview", $quickOverview);
        $demoGraphMin = array();
        foreach ($demoGraphic as $key => $element) {
            if ($key < 10)
                $demoGraphMin[$key] = $element;
            else {
                $demoGraphMin[10]['location'] = 'Other Cities';
                $demoGraphMin[10]['visitors'] += $element['visitors'];
                $demoGraphMin[10]['subscription'] += $element['subscription'];
                $demoGraphMin[10]['registration'] += $element['registration'];
                $demoGraphMin[10]['importedUser'] += $element['importedUser'];
                $demoGraphMin[10]['importedSubscriber'] += $element['importedSubscriber'];
                $demoGraphMin[10]['revenue'] += $element['importedSubscriber'];
                $demoGraphMin[10]['sales'] += $element['sales'];
                $demoGraphMin[10]['salescount'] += $element['salescount'];
            }
        }
        $this->ObjView->assign("demoGraphic", $demoGraphic);
        $this->ObjView->assign("graphData", $trafficReportGraph);
        $this->ObjView->assign("widgets", $ObjAffiliate->listWidgets($affiliateUserID));
        // ---------------------------- Order Listing -------------------------------------------//
        $POST['limitFrom']       = 0;
        $POST['limitTo']         = 5;
        $POST['affiliateUserID'] = $affiliateUserID;
        $languageLabel[]         = $this->getLanguageValue('lblAdjusted');
        $languageLabel[]         = $this->getLanguageValue('lblNotAdjusted');
        $languageLabel[]         = $this->getLanguageValue('lblCompleted');
        $languageLabel[]         = $this->getLanguageValue('lblCancelledd');
        $languageLabel[]         = $this->getLanguageValue('lblReceived');
        $languageLabel[]         = $this->getLanguageValue('lblPending');
        $resOrders               = $ObjAffiliate->listOrderDetails($POST, $purpose = 'indexorder', $languageLabel);
        // -------------------------------END Order Listing----------------------------------------//
        $this->ObjView->assign('dateFrom', date("Y-m-d", $lastDay));
        $this->ObjView->assign('dateTo', date('Y-m-d'));
        $this->ObjView->assign('dateFormat', $settingsDateFormat);
        $this->ObjView->assign('resOrders', $resOrders);
        $this->ObjView->display('Index.tpl');
    }
    public function ajaxGraphAction() {
        $ObjFlot         = new Flot();
        $ObjAffiliate    = new AffiliateModel();
        $affiliateUserID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $dateFrom        = GregorianToJD(date('m'), 01, date('Y'));
        $dateTo          = GregorianToJD(date('m'), date('d'), date('Y'));
        $fields          = json_decode($this->escape($_POST['fields']));
        if (!empty($fields)) {
            if (in_array('Sales', $fields))
                $newFields[] = $this->getLanguageValue("lblSales");
            if (in_array('Visits', $fields))
                $newFields[] = $this->getLanguageValue("lblViists");
            if (in_array('Registration', $fields))
                $newFields[] = $this->getLanguageValue("lblRegistrations");
            if (in_array('Subscriptions', $fields))
                $newFields[] = $this->getLanguageValue("lblSubscriptions");
            if (in_array('Imported Users', $fields))
                $newFields[] = $this->getLanguageValue("lblImportedUser");
            if (in_array('Imported Subscribers', $fields))
                $newFields[] = $this->getLanguageValue("lblImportedSubscriber");
        }
        $_POST['dateRange'] = $this->escape($_POST['dateRange']);
        if ($_POST['dateRange'] != 'Select Date') {
            if (strpos($_POST['dateRange'], " - ")) {
                list($fromDate, $toDate) = explode(" - ", $_POST['dateRange']);
            } //strpos($_POST['dateRange'], " - ")
            else {
                $fromDate = $toDate = $_POST['dateRange'];
            }
            $fromDate = strtotime(trim($fromDate));
            $toDate   = strtotime(trim($toDate));
            $dateFrom = GregorianToJD(date('m', $fromDate), date('d', $fromDate), date('Y', $fromDate));
            $dateTo   = GregorianToJD(date('m', $toDate), date('d', $toDate), date('Y', $toDate));
        } //$_POST['dateRange'] != 'Select Date'
        $arr        = array();
        $xCordArray = array();
        for ($dateIndex = $dateFrom; $dateIndex <= $dateTo; $dateIndex++) {
            list($month, $day, $year) = explode('/', JDToGregorian($dateIndex));
            $month = $month < 10 ? '0' . $month : $month;
            $day   = $day < 10 ? '0' . $day : $day;
            ($dateIndex == $dateFrom) ? $colName = 'clickDate' : $colName = '';
            array_push($arr, "SELECT '" . $year . "-" . $month . "-" . $day . "' $colName");
        } //$dateIndex = $dateFrom; $dateIndex <= $dateTo; $dateIndex++
        $trafficSource                                    = $ObjAffiliate->getTrafficSources($affiliateUserID, implode(' UNION ALL ', $arr));
        $trafficReportSettings['placeholder']             = 'affiliateHolder';
        $trafficReportSettings['fill']                    = 'false';
        $trafficReportSettings['graphLabels']             = (!empty($newFields)) ? implode(',', $newFields) : '';
        $trafficReportSettings['graphTotalLabels']        = (!empty($newFields)) ? implode(',', $newFields) : '';
        $trafficReportSettings['graphTotalHolder']        = 'affiliateHoldergraphTotalHolder';
        $trafficReportSettings['graphTotalHolderUlClass'] = 'graphTotalHolderUl';
        $trafficReportSettings['mode']                    = 'time';
        $trafficReportSettings['tootlTipXLabel']          = 'Day';
        $trafficReportSettings['enableSelection']         = false;
        $trafficReportSettings['graphType']               = 'line';
        $trafficReportSettings['enableSelection']         = 'false';
        $trafficReportSettings['includeScript']           = 'false';
        $trafficReportSettings['ticksForLineGraph']       = $xCordArray;
        $line1                                            = array();
        $line2                                            = array();
        $line3                                            = array();
        $line4                                            = array();
        $line5                                            = array();
        $line6                                            = array();
        foreach ($trafficSource as $key => $value) {
            array_push($line1, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['sales']
            ));
            array_push($line2, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['visitors']
            ));
            array_push($line3, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['registration']
            ));
            array_push($line4, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['subscription']
            ));
            array_push($line5, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['ImportedUser']
            ));
            array_push($line6, array(
                'graphX' => $value['clickDate'],
                'graphY' => $value['ImportedSubscriber']
            ));
        } //$trafficSource as $key => $value
        $trafficGraphArray = array();
        if (in_array('Sales', $fields))
            $trafficGraphArray[] = $line1;
        if (in_array('Visits', $fields))
            $trafficGraphArray[] = $line2;
        if (in_array('Registration', $fields))
            $trafficGraphArray[] = $line3;
        if (in_array('Subscriptions', $fields))
            $trafficGraphArray[] = $line4;
        if (in_array('Imported Users', $fields))
            $trafficGraphArray[] = $line5;
        if (in_array('Imported Subscribers', $fields))
            $trafficGraphArray[] = $line6;
        if (count($trafficGraphArray))
            echo $ObjFlot->init($trafficGraphArray, 0, $trafficReportSettings);
        else
            echo '<div class="noDataFound">' . $this->getLanguageValue("lblNoData") . '</div>';
    }
    public function ajaxDemoGraphAction() {
        $ObjAffiliate    = new AffiliateModel();
        $affiliateUserID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        if ($_POST['value'] != 'Select') {
            $demoGraphic = $ObjAffiliate->getDemographics($affiliateUserID, $this->escape($_POST['value']));
        } //$_POST['value'] != 'Select'
        else {
            $demoGraphic = $ObjAffiliate->getDemographics($affiliateUserID);
        }
        $this->ObjView->assign("demoGraphic", $demoGraphic);
        $this->ObjView->display('AjaxDemograpic.tpl');
    }
}