<?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: UserController.php, v 1.0 2011/06/14 20:52:11
//
class UserController extends Controller {
    public function indexAction() {
        $affiliateUserID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        if (!$affiliateUserID) {
            $this->setRedirect('/affiliate/login/');
        }
        $ObjLanguage           = new LanguageModel();
        $ObjCountry            = new CountryModel();
        $ObjAffiliate          = new AffiliateModel();
        $ObjSettings           = new SettingsModel();
        $languageIDs           = $this->ObjView->languageIDs;
        $resAffiliate          = $ObjAffiliate->getAffiliateDetails($affiliateUserID, 'view', '', $languageIDs);
        //To Get Affiliate Details
        $languageDetails       = $ObjLanguage->getLanguageDetails($resAffiliate['preferredLanguage']);
        //To get Preferred Language Details
        $resCommissionSettings = $ObjAffiliate->getAffCommissionSettings($affiliateUserID);
        //To get Affiliate Commission Value
        $resGlobalSettings     = $ObjAffiliate->getAffiliateGlobalSettings('affiliate_settings', 'parent');
        //To get Affiliate Global Settings
        for ($i = 0; $i < count($resGlobalSettings); $i++) {
            $resGlobalValue[$resGlobalSettings[$i]['settingsKey']] = $resGlobalSettings[$i]['settingsValue'];
        }
        if ($resAffiliate['affiliateStateID'] == 0) {
            $resAffiliate['stateName'] = $resAffiliate['affiliateStateProvince'];
        }
        $this->ObjView->assignValues($resAffiliate);
        $this->ObjView->assignValues($resGlobalValue);
        $this->ObjView->assign('affiliateUserID', $affiliateUserID);
        $this->ObjView->assign('resAffiliate', $resAffiliate);
        $this->ObjView->assign('languageName', $languageDetails['languageName']);
        $this->ObjView->assign('resCommissionSettings', $resCommissionSettings);
        $this->ObjView->display("MyAccount.tpl");
    }
    //ACCOUNT ACTION
    public function accountAction() {
        $affiliateUserID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        if (!$affiliateUserID) {
            $this->setRedirect('/affiliate/login/');
        }
        $ObjFilter        = new Filter();
        $ObjCountry       = new CountryModel();
        $ObjAffiliate     = new AffiliateModel();
        $ObjEmailtemplate = new EmailtemplateModel();
        $ObjLanguage      = new LanguageModel();
        $languageIDs      = $this->ObjView->languageIDs;
        $resAffiliate     = $ObjAffiliate->getAffiliateDetails($affiliateUserID);
        //To Get Affiliate Details
        $resListCountry   = $ObjCountry->listCountries($languageIDs);
        //To List All countries
        if ($this->ObjForm->isValid('formAffiliateProfile', 'submitProfile')) {
            if ((trim($_POST['affiliatePassword']) != '') && (trim($_POST['affiliatePassword']) == trim($_POST['affiliatePasswordnew'])) && (trim($_POST['affiliatePasswordnew']) != '')) {
                $_POST['affiliatePassword'] = trim($_POST['affiliatePasswordnew']);
            }
            $this->ObjCookie->setCookie($ObjLanguage->getSection() . '_CurrentLanguageID', $_POST['preferredLanguage']);
            $affiliateIDRet = $ObjAffiliate->saveAffiliate($_POST, $affiliateUserID, 0, 0);
            //Save  the Updated Details
            //send mail
            if ($affiliateIDRet == true) {
                $this->ObjSession->setSession("AFFILIATE_NAME", $_POST['affiliateFirstName'] . ' ' . $_POST['affiliateLastName']);
                $this->ObjSession->setSession("AFFILIATE_LANG_ID", $_POST['preferredLanguage']);
                if ((!empty($_POST['affiliatePassword'])) && (!empty($_POST['affiliatePasswordnew']))) {
                    $emailbody = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'user-password-changed');
                    $from      = array(
                        $emailbody['emailFromEmail'],
                        $emailbody['emailFromName']
                    );
                    $userName  = $_POST['affiliateFirstName'] . " " . $_POST['affiliateLastName'];
                    $subject   = $emailbody['emailSubject'];
                    $bodytag   = str_replace('{$userName}', $userName, $emailbody['emailHtml']);
                    $bodytag   = str_replace('{$userPassword}', $_POST['affiliatePassword'], $bodytag);
                    $this->ObjView->assign('emailbody', $bodytag);
                    $body                           = $this->ObjCommon->fetch($this->ObjView, "email_templates/Layout.tpl");
                    $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($_POST['affiliateEmail']));
                    $ObjMail->IsHTML();
                    $ObjMail->sendMail($from, $emailID, $subject, $body);
                    if ($ObjMail->Send()) {
                    }
                }
                if ((!empty($_POST['affiliatePassword'])) && (!empty($_POST['affiliatePasswordnew']))) {
                    $this->setMessage("language.msgAffiliateUpdatePasswordCase", "message_success");
                } else {
                    $this->setMessage("language.msgUpdateAffiliateProfile", "success");
                }
                $this->setRedirect("user/");
            }
        }
        $resAffiliate['affiliatePassword'] = '';
        $this->ObjView->assignValues($resAffiliate);
        $this->ObjView->assign('citySelectedID', (isset($resAffiliate['affiliateCityID'])) ? $resAffiliate['affiliateCityID'] : '');
        $this->ObjView->assign('stateSelectedID', $resAffiliate['affiliateStateID']);
        $this->ObjView->assign('affiliateUserID', $affiliateUserID);
        $this->ObjView->assign('resListCountry', $resListCountry);
        $this->ObjView->assign('tab', 'profile');
        $this->ObjView->display('ProfileManage.tpl');
    }
    //LOGOUT ACTION
    public function logoutAction() {
        $ObjAffiliate    = new AffiliateModel();
        $manageSessionID = $this->ObjSession->getSessionID();
        $this->ObjSession->unsetSession("AFFILIATE_USER_ID");
        $this->ObjSession->unsetSession("AFFILIATE_NAME");
        $this->ObjSession->unsetSession("AFFILIATE_USER_IP");
        $this->ObjSession->unsetSession("AFFILIATE_USER_LAST_ACCESS");
        $this->ObjSession->unsetSession("AFFILIATE_LANG_ID");
        $this->ObjSession->unsetSession("TIMEOUT");
        $this->ObjCookie->setCookie($this->ObjView->_section . '_CurrentLanguageID', '');
        $this->setMessage("language.msglogout", "message_success", "User", 'logout_message');
        $this->setRedirect("/login/");
    }
    //AJAX ACTION FOR LIST COUNTRY STATE
    public function ajaxStateAction() {
        $ObjState    = new StateModel();
        $languageIDs = $this->ObjView->languageIDs;
        $countryID   = $_POST['countryID'];
        $stateID     = $_POST['stateID'];
        if ($countryID > 0) {
            $statesList = $ObjState->listStates($languageIDs, $countryID);
            //To List All states
        }
        $this->ObjView->assign('stateID', $stateID);
        $this->ObjView->assign('statesList', $statesList);
        $this->ObjView->display('CityState.tpl');
    }
}