<?php

# $Id: action.period_add_edit.php 7 2011-06-20 18:59:54Z 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;

###############################################################################
# CheckPermission
###############################################################################

if (!$this->CheckPermission('Modify UpCoupon')) {
    echo $this->ShowErrors($this->Lang('accessdenied', array('period')));
    return;
}

###############################################################################
# apply and cancel
###############################################################################

$apply_button_clicked = '0';

if (isset($params['submit'])) {
    if ($params['submit'] == $this->Lang('apply')) {
        $apply_button_clicked = '1';
    }
}

if (isset($params['cancel'])) {
    $params = array('active_tab' => 'period');
    $this->Redirect($id, 'defaultadmin', $returnid, $params);
    return;
}

###############################################################################
# Object
###############################################################################

$actiondb = new coupon_actionDAO();
$perioddb = new coupon_periodDAO();

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

$pkey = '';
$modus = 'add';

if (isset($params['pkey'])) {
    $pkey = $params['pkey'];
    $modus = 'edit';
    
    $rows = $perioddb->fetch($pkey);
    
    foreach ($rows[0] as $key => $value) {
        
        if (preg_match("/date/i", $key)) {
            ${$key} = $perioddb->unix_time_stamp($value);
        } else {
            ${$key} = $value;
        }
        
    }
        
} else {

    $modus = 'add';

    $coupon_action_id = '';    
    $title = '';
    $start_date = time();
    $end_date = time();
    $is_final = '0';
    $status = '1';
}

if ( isset($params['coupon_action_id']) ) {
    $coupon_action_id = $params['coupon_action_id'];
}

if ( isset($params['title']) ) {    
    $title = trim($params['title']);
}

if ( isset($params['start_date_Month']) ) {
    $start_date = mktime($params['start_date_Hour'], $params['start_date_Minute'], $params['start_date_Second'], $params['start_date_Month'], $params['start_date_Day'], $params['start_date_Year']);
}

if ( isset($params['end_date_Month']) ) {
    $end_date = mktime($params['end_date_Hour'], $params['end_date_Minute'], $params['end_date_Second'], $params['end_date_Month'], $params['end_date_Day'], $params['end_date_Year']);
}

if ( isset($params['is_final']) ) {
    $is_final = trim($params['is_final']);
}

if ( isset($params['status']) ) {
    $status = trim($params['status']);
}

###############################################################################
# check params
###############################################################################

if ( isset($params['submit']) ) {
    $error = FALSE;
    
    if ( empty($coupon_action_id) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('action'))));
    }
    
    if ( empty($title) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('title'))));
    }
    
    if ($start_date >= $end_date) {
        $error .= $this->ShowErrors($this->Lang('error_invaliddates'));      
    }
    
    if ( $error == FALSE ) {
    
        $action_data        = $actiondb->fetch($coupon_action_id);
        $action_start_date  = $actiondb->unix_time_stamp($action_data['0']['start_date']);
        $action_end_date    = $actiondb->unix_time_stamp($action_data['0']['end_date']);
    
        if ($start_date < $action_start_date || $end_date > $action_end_date) {
            $error .= $this->ShowErrors($this->Lang('s_s_couponperiod_mismatch', array($action_data['0']['start_date'], $action_data['0']['end_date'])));   
        }
    
    }
    
    if ( $error !== FALSE ) {
        echo $error;
    } else {

        $db_array = array(
                        'coupon_action_id' => $coupon_action_id,
                        'title' => $title,
                        'start_date' => $perioddb->db_time_stamp($start_date),
                        'end_date' => $perioddb->db_time_stamp($end_date), 
                        'is_final' => $is_final,
                        'status' => $status
                        );
        
       
        
        if ($modus == 'edit') {
            $db_array['coupon_period_id'] = $pkey; 
            $perioddb->update($db_array);            
        } else {
            $coupon_period_id = $perioddb->insert($db_array); 
            $db_array['coupon_period_id'] = $coupon_period_id;
        }
         
    }
    
    if ( !$error ) {
        
        if ($modus == 'edit') {
            @$this->SendEvent('UpCouponPeriodEdited', $db_array);
            $params = array('period_message'=> 's_edited,period_s', 'active_tab' => 'period');
        } else {
            @$this->SendEvent('UpCouponPeriodAdded', $db_array);
            $params = array('period_message'=> 's_added,period_s', 'active_tab' => 'period');
        }
        
        if ($apply_button_clicked == '1') {
            $params['pkey'] = $db_array['coupon_period_id'];
            $this->Redirect($id, 'period_add_edit', $returnid, $params);
            return;
        } else {
            $this->Redirect($id, 'defaultadmin', $returnid, $params);
            return;
        }
        
    }
    
} // submit 


###############################################################################
# build and proccess template
###############################################################################

$this->smarty->assign('startform', $this->CreateFormStart($id, 'period_add_edit', $returnid, 'post', 'multipart/form-data'));

$actiondropdown = array();
$actiondropdown = $actiondb->fetch_actiondropdown();

if (empty($actiondropdown)) {
    $params = array('tab_message'=> 'coupon_action_missing', 'active_tab' => 'action');
    $this->Redirect($id, 'defaultadmin', $returnid, $params);
}

$action_input = $this->CreateInputDropdown($id, 'coupon_action_id', array(), -1, $coupon_action_id);
$action_input = UpHelper::modify_input_dropdown($action_input, $actiondropdown, $coupon_action_id);

$this->smarty->assign('action_text', $this->Lang('action'));
$this->smarty->assign('action_input', $action_input);

$this->smarty->assign('title_text', $this->Lang('title'));
$this->smarty->assign('title_input', $this->CreateInputText($id, 'title', $title, 20, 100));

$this->smarty->assign('start_date_text', $this->Lang('start_date'));
$this->smarty->assign('start_date_prefix', $id.'start_date_');
$this->smarty->assign('start_date', $start_date);

$this->smarty->assign('end_date_text', $this->Lang('end_date'));
$this->smarty->assign('end_date_prefix', $id.'end_date_');
$this->smarty->assign('end_date', $end_date);

$is_finaldropdown = array();
$is_finaldropdown[$this->Lang('no')] = '0';
$is_finaldropdown[$this->Lang('yes')] = '1';

$this->smarty->assign('is_final_text', $this->Lang('is_final'));
$this->smarty->assign('is_final_input', $this->CreateInputDropdown($id, 'is_final', $is_finaldropdown, -1, $is_final));

$statusdropdown = array();
$statusdropdown[$this->Lang('inactive')] = '0';
$statusdropdown[$this->Lang('active')] = '1';

$this->smarty->assign('status_text', $this->Lang('status'));
$this->smarty->assign('status_input', $this->CreateInputDropdown($id, 'status', $statusdropdown, -1, $status));

if ($modus == 'edit') {    
    $this->smarty->assign('hidden', $this->CreateInputHidden($id, 'pkey', $pkey));
} else {
    $this->smarty->assign('hidden', '');
}    

if ($modus == 'edit') {
    $this->smarty->assign('submit', $this->CreateInputSubmit($id, 'submit', $this->Lang('edit')));
} else {
    $this->smarty->assign('submit', $this->CreateInputSubmit($id, 'submit', $this->Lang('add')));
}    
    
$this->smarty->assign('cancel', $this->CreateInputSubmit($id, 'cancel', $this->Lang('cancel')));

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

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

echo $this->ProcessTemplate('period_add_edit.tpl');

#
# EOF
#
?>
