<?php

# $Id: action.show_register.php 5 2011-06-20 10:11:23Z arnoud $ 

#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: UpCoupon (c) 2011 by Arnoud van Susteren (arnoud@upservice.nl)
#
# An addon module for CMS Made Simple to setup a Coupon Codes Lottery
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

if(!is_object(cmsms())) exit;

###############################################################################
# objects
###############################################################################

//require(cms_join_path(dirname(__FILE__), 'lib', 'classes', 'UpCookie.class.php'));

$cookie = new UpCookie('31536000');
$ip = UpHelper::get_ip();
$customerdb = new coupon_customerDAO();

###############################################################################
# retrieve existing customer_data
###############################################################################

$modus = 'insert';

if ( ! isset($params['submit']) ) {
    $checksum = $cookie->get('token');
    
    $customer_data = array();
    
    if (strlen($checksum) == '32') {
        $customer_data = $customerdb->fetch_by_ip_checksum($ip, $checksum);
        
        if ($customer_data['0']['checksum'] == $checksum) {
            $modus = 'edit';
            
            foreach ($customer_data[0] as $k => $v) {
                $$k = $v;        
            }
        }
        
    }
    
}

###############################################################################
# fetch params
###############################################################################

$all_templates = $this->ListTemplates();

$template = '0';

if ( isset($params['template']) ) {      
    $template = UpHelper::alpha_numeric_lower($params['template']);
    $template = 'register_'. $template;
    
    $tmp = '0';
    
    foreach ($all_templates as $k => $v) {
        if ($template == $v) {
            $tmp = '1';
        }
    }
    
    $template = ($tmp == '1') ? $template : '0';
    
}

if ( isset($params['first_name']) ) { 
    $first_name = trim(filter_var($params['first_name'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES));
}

if ( isset($params['last_name']) ) {
    $last_name = trim(filter_var($params['last_name'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES));
}

if ( isset($params['email']) ) {    
    $email = trim(filter_var($params['email'], FILTER_SANITIZE_EMAIL));   
}

if ( isset($params['phone']) ) {    
    $phone = trim(preg_replace('/[^0-9]/', '', $params['phone']));
}

if ( isset($params['coupon_code_01']) ) {    
    $coupon_code_01 = trim(preg_replace('/[^0-9]/', '', $params['coupon_code_01']));    
}

if ( isset($params['coupon_code_02']) ) {    
    $coupon_code_02 = trim(preg_replace('/[^0-9]/', '', $params['coupon_code_02']));  
}

if ( isset($params['coupon_code_03']) ) {    
    $coupon_code_03 = trim(preg_replace('/[^0-9]/', '', $params['coupon_code_03']));  
}

if ( isset($params['coupon_code_04']) ) {    
    $coupon_code_04 = trim(preg_replace('/[^0-9]/', '', $params['coupon_code_04']));  
}

$coupon_code = $coupon_code_01. $coupon_code_02. $coupon_code_03. $coupon_code_04;

if ( isset($params['terms_and_conditions']) ) {    
    $terms_and_conditions = trim(preg_replace('/[^1]/', '', $params['terms_and_conditions'])); 
}

if ( isset($params['newsletter']) ) {    
    $newsletter = trim(preg_replace('/[^1]/', '', $params['newsletter']));
}

if ( ! isset($params['submit']) && $modus != 'edit') {
    $newsletter = '1';
}    
    
###############################################################################
# check params
###############################################################################

$error_array = array('1');

if ( isset($params['submit']) ) {
    
    $error_array = array();
    
    if ( empty($first_name) ) {
        $error_array['first_name'] = '1';
    }
        
    if ( empty($last_name) ) {
        $error_array['last_name'] = '1';
    }
    
    if ( !is_email($email) ) {
        $error_array['email'] = '1';
    }
    
    if ( strlen($phone) != '10' ) {
        $error_array['phone'] =  '1';
    }
    
    if ( strlen($coupon_code) != '16' ) {
        $error_array['coupon_code'] = '1';
    }
    
    if ( empty($terms_and_conditions) ) {
        $error_array['terms_and_conditions'] = '1';
    }
    
}

    if ( count($error_array) >= 1 ) {
        $smarty->assign('error_array', $error_array);
    } else {
 
        $checksum = $cookie->get('token');
                
        if (strlen($checksum) == '32') {
            $customer_data = $customerdb->fetch_by_ip_checksum($ip, $checksum);

            if ($customer_data['0']['checksum'] == $checksum) {
                $modus = 'edit';
                $coupon_customer_id = $customer_data['0']['coupon_customer_id'];
            }
        }     
        
        if ($modus == 'edit') {
            
            $db_array = array(
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $email,
                'phone' => $phone,
                'newsletter' => $newsletter,
                'timestamp' => $customerdb->unix_time_stamp(),
                'coupon_customer_id' => $coupon_customer_id
            );
            
            $customerdb->update($db_array);
        
        } else {
            
            $password = UpHelper::random_password();
            $salt = rand(1000000,99999999);
            $checksum = md5($password.$salt);
            
            $db_array = array(
                'first_name' => $first_name,
                'last_name' => $last_name,
                'email' => $email,
                'phone' => $phone,
                'newsletter' => $newsletter,
                'ip' => $ip, 
                'status' => 1,
                'password' => $password,
                'salt' => $salt,
                'checksum' => $checksum,
                'registration_date' => $customerdb->db_time_stamp(),
                'timestamp' => $customerdb->unix_time_stamp()
            );
            
            $db_array['coupon_customer_id'] = $customerdb->insert($db_array);
            
        }
        
        $cookie->set('token', $checksum); 

        $codedb = new coupon_codeDAO();        
        $coupon_data = $codedb->fetch($coupon_code, 'coupon_code');
        
        if ($coupon_data['0']['coupon_customer_id'] == 0) {
            $is_taken = '0';   
        } else {
            $is_taken = '1';
        }
                
        if ( ($coupon_data['0']['coupon_code'] == $coupon_code) && (!$is_taken) ) {

            $db_array_02 = array(
                'timestamp' => $codedb->unix_time_stamp(),
                'coupon_customer_id' => $db_array['coupon_customer_id'],
                'coupon_code_id' => $coupon_data['0']['coupon_code_id']
            );
            
            $codedb->update($db_array_02);
                        
            if ($this->GetPreference('email_registered_true_false',0) == 1) {
                $mail = $this->GetModuleInstance('CMSMailer');
                if ($mail !== FALSE) {
                    $mail->reset();
                    $mail->SetSubject($this->GetPreference('email_registered_subject'));
                                        
                    $email_array = array(
                        'first_name' => $first_name,
                        'last_name' => $last_name,
                        'email' => $email,
                        'phone' => $phone,
                        'newsletter' => $newsletter,
                        'timestamp' => $codedb->unix_time_stamp(), 
                        'db_timestamp' => $codedb->db_time_stamp(),
                        'dashed_coupon_code' => implode('-', str_split($coupon_code, 4)),
                        'coupon_code' => $coupon_code
                    );
                                        
                    $this->smarty->assign('email', $email_array);
                    
                    $body = $this->ProcessTemplateFromData($this->GetTemplate('email_registered_message'));
                    
                    $mail->SetBody($body);
                    
                    $mail->SetFrom($this->GetPreference('email_registered_address_email'));
                    $mail->SetFromName($this->GetPreference('email_registered_address_name'));
                                        
                    $full_name = $db_array['first_name'] . ' '. $db_array['last_name']; 
                    
                    $mail->AddAddress($db_array['email'], $full_name);
                    
                    $res = $mail->Send();
                }
            }

        }
             
        $this->smarty->assign('is_registered', '1');
        $this->smarty->assign('s_s_coupon_code_registered_text', $this->Lang('s_s_coupon_code_registered', array($first_name, $last_name)));
        
        $terms_and_conditions = '0';
        $coupon_code_01 = '';
        $coupon_code_02 = '';
        $coupon_code_03 = '';
        $coupon_code_04 = '';
           
    }

###############################################################################
# build and process template
###############################################################################

$this->smarty->assign('form_id', $id);

$startform = $this->CreateFormStart($id, 'show_register', $returnid, 'post');

preg_match_all('/(id)=("[^"]*")/i', $startform, $matches);
$full_form_id = str_replace('"', '', $matches[2][0]);

$this->smarty->assign('full_form_id', $full_form_id);

$smarty->assign('startform', $startform);

$delete_coupon_code_cookie_link = $this->CreateFrontendLink($id, $returnid, 'show_delete_cookie', 'contents', '', '', 'true', 'true', 'addtext', 'false');

$this->smarty->assign('delete_coupon_code_cookie_link', $delete_coupon_code_cookie_link);

$this->smarty->assign('delete_coupon_code_cookie_text', $this->Lang('s_delete_coupon_code_cookie', array($delete_coupon_code_cookie_link)));

$this->smarty->assign('delete_text', $this->Lang('delete'));

$this->smarty->assign('register_coupon_code_text', $this->Lang('register_coupon_code'));

$this->smarty->assign('first_name_text', $this->Lang('first_name'));
$this->smarty->assign('first_name_input', $this->CreateInputText($id, 'first_name', $first_name, 40, 100));

$this->smarty->assign('last_name_text', $this->Lang('last_name'));
$this->smarty->assign('last_name_input', $this->CreateInputText($id, 'last_name', $last_name, 40, 100));

$this->smarty->assign('email_text', $this->Lang('email'));
$this->smarty->assign('email_input', $this->CreateInputText($id, 'email', $email, 40, 45));

$this->smarty->assign('phone_text', $this->Lang('phone'));
$this->smarty->assign('phone_input', $this->CreateInputText($id, 'phone', $phone, 40, 10));

$this->smarty->assign('coupon_code_text', $this->Lang('coupon_code'));

$this->smarty->assign('coupon_code_01_input', preg_replace('/class="cms_textfield"/', 'class="cms_textfield_small"', $this->CreateInputText($id, 'coupon_code_01', $coupon_code_01, 10, 4)));

$this->smarty->assign('coupon_code_02_input', preg_replace('/class="cms_textfield"/', 'class="cms_textfield_small"', $this->CreateInputText($id, 'coupon_code_02', $coupon_code_02, 10, 4)));

$this->smarty->assign('coupon_code_03_input', preg_replace('/class="cms_textfield"/', 'class="cms_textfield_small"', $this->CreateInputText($id, 'coupon_code_03', $coupon_code_03, 10, 4)));

$this->smarty->assign('coupon_code_04_input', preg_replace('/class="cms_textfield"/', 'class="cms_textfield_small"', $this->CreateInputText($id, 'coupon_code_04', $coupon_code_04, 10, 4)));

$this->smarty->assign('terms_and_conditions_text', $this->Lang('terms_and_conditions'));
$this->smarty->assign('terms_and_conditions_agree_text', $this->Lang('terms_and_conditions_agree'));
$smarty->assign('terms_and_conditions_input',$this->CreateInputCheckbox($id,'terms_and_conditions','1', $terms_and_conditions));

$this->smarty->assign('newsletter_text', $this->Lang('newsletter'));
$this->smarty->assign('newsletter_agree_text', $this->Lang('newsletter_agree'));
$smarty->assign('newsletter_input',$this->CreateInputCheckbox($id,'newsletter','1', $newsletter));

$this->smarty->assign('submit', $this->CreateInputSubmit($id, 'submit', $this->Lang('submit')));

$this->smarty->assign('cancel', $this->CreateInputReset($id, 'cancel', $this->Lang('cancel')));


$this->smarty->assign('endform', $this->CreateFormEnd());

###############################################################################
# ProcessTemplate
###############################################################################

if ($template != '0') {
    echo $this->ProcessTemplateFromDatabase($template); 
} else {
    $template_type_key = 'default_register_template';
    $template  = $this->GetPreference($template_type_key);
    
    $tmp = '0';
    
    foreach ($all_templates as $k => $v) {
        if ($template  == $v) {
            $tmp = '1';
        }
    }
    
    $template = ($tmp == '1') ? $template : '0';

    if ($template == '0') {
        echo $this->ProcessTemplate('factory_template_register.tpl');
    } else {
        echo $this->ProcessTemplateFromDatabase($template); 
    }
     
}

#
# EOF
#
?>
