<?php

# $Id: action.prize_add_edit.php 1 2011-06-12 15:25:27Z 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('prize')));
    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' => 'prize');
    $this->Redirect($id, 'defaultadmin', $returnid, $params);
    return;
}

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

$prizedb = new coupon_prizeDAO();
$actiondb = new coupon_actionDAO();
$perioddb = new coupon_periodDAO();
$mixeddb = new coupon_mixedDAO();

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

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

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

    $modus = 'add';
    
    $coupon_action_id = '';
    $coupon_period_id = '';
    $prize_code = '';
    $title = '';
    $description = '';
    $number = '';
    $status = '1';
    
}

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

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

if ( isset($params['prize_code']) ) {
    $prize_code = UpHelper::alpha_numeric_lower($params['prize_code']);
}

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

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

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

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($coupon_period_id) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('period'))));        
    }

    if ( empty($prize_code) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('prize_code'))));
    } 
    
    if ( empty($title) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('title'))));
    }

    if ( empty($description) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('description'))));
    }
    
    if ( empty($number) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('number'))));
    }
        
    if ( $error !== FALSE ) {
        echo $error;
    } else {

        $db_array = array(
                        'prize_code' => $prize_code,
                        'title' => $title,
                        'description' => $description,
                        'number' => $number,
                        'status' => $status,
                        'coupon_action_id' => $coupon_action_id,
                        'coupon_period_id' => $coupon_period_id
                        );
        
        if ($modus == 'edit') {
            $db_array['coupon_prize_id'] = $pkey;            
            $prizedb->update($db_array);            
        } else {
            $coupon_prize_id = $prizedb->insert($db_array); 
            $db_array['coupon_prize_id'] = $coupon_prize_id;
        }
         
    }
    
    if ( !$error ) {
        
        if ($modus == 'edit') {
            @$this->SendEvent('UpCouponPrizeEdited', $db_array);
            $params = array('prize_message'=> 's_edited,prize_s', 'active_tab' => 'prize');
        } else {
            @$this->SendEvent('UpCouponPrizeAdded', $db_array);
            $params = array('prize_message'=> 's_added,prize_s', 'active_tab' => 'prize');
        }
        
        if ($apply_button_clicked == '1') {
            $params['pkey'] = $db_array['coupon_prize_id'];
            $this->Redirect($id, 'prize_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, 'prize_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, 'id="actiondropdown"');
$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);

$perioddropdown = array();
$perioddropdown = $perioddb->fetch_perioddropdown();

if (empty($perioddropdown)) {
    $params = array('tab_message'=> 'coupon_period_missing', 'active_tab' => 'period');
    $this->Redirect($id, 'defaultadmin', $returnid, $params);
}

$period_input = $this->CreateInputDropdown($id, 'coupon_period_id', array(), -1, $coupon_period_id, 'id="perioddropdown"');
$period_input = UpHelper::modify_input_dropdown($period_input, $perioddropdown, $coupon_period_id);

$action_period_js = $mixeddb->fetch_action_period_js();
$period_js = UpHelper::array_of_array_to_json($action_period_js);

$this->smarty->assign('period_text', $this->Lang('period'));
$this->smarty->assign('period_input', $period_input);
$this->smarty->assign('period_js', $period_js);

if ($modus == 'edit') {  
    $selected_period_js = $rows['0']['coupon_period_id'];
} else {
    $selected_period_js = '';    
}

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

$this->smarty->assign('prize_code_text', $this->Lang('prize_code'));
$this->smarty->assign('prize_code_input', $this->CreateInputText($id, 'prize_code', $prize_code, 20, 45));

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

$this->smarty->assign('description_text', $this->Lang('description'));
$this->smarty->assign('description_input', $this->CreateTextArea(true, $id, $description, 'description'));

$numberdropdown = array();

for ($i = 1; $i <= 25; $i++) {
    $numberdropdown[$i] = $i;    
}

$this->smarty->assign('number_text', $this->Lang('number'));
$this->smarty->assign('number_input', $this->CreateInputDropdown($id, 'number', $numberdropdown, -1, $number));


$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('prize_add_edit.tpl');

#
# EOF
#
?>
