<?php

# $Id: action.template_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 Site Preferences')) {
    echo $this->ShowErrors($this->Lang('accessdenied', array('templates')));
    return;
}

################################################################################
# factory, apply and cancel
################################################################################

if (isset($params['factory'])) {
    $factory_template = 'factory_template_'. $params['template_type'];
    $params['template_code'] = $this->GetTemplateFromFile($factory_template);
}

$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' => 'templates');
    $this->Redirect($id, 'defaultadmin', '', $params);
    return;
}

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

$template_type_list = $this->_get_template_type();

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

if (isset($params['pkey'])) {
    $pkey = $params['pkey'];
    $modus = 'edit';
        
    $name = preg_replace('/^(' . join("|", array_keys($template_type_list)) . ')_/', '', $pkey);
      
    foreach (array_keys($template_type_list) as $k)  {
        if ( preg_match("/^$k/", $pkey, $matches) ) {
            $template_type = $template_type_list[$k];
            $template_type_edit = $k;
            break;
        }
    }
    
    $template_code = $this->GetTemplate($pkey);
    
} else {
    
    $modus = 'add';
    
    $name = '';
    $template_type = 'register';
    $template_code = '';
    
}

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

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

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

if ( isset($params['error']) ) {
    $error_msg = $this->Lang($params['error']);
    echo $this->ShowErrors($error_msg);
}

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

if ( isset($params['submit']) ) {
    $error = FALSE;
    
    if ( empty($name) ) {
        $error .= $this->ShowErrors($this->Lang('no_s_given', array($this->Lang('name'))));
    }

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

    if ( $error !== FALSE ) {
        echo $error;
    } else {

        $db_array = array(
                        'name' => $name,
                        'template_type' => $template_type,
                        'template_code' => $template_code
                        );
        
        // strip unwanted chars from $name
        
        $template_name = $template_type . '_' . $name;
                
        if ($modus == 'edit') {
            
            // only verify the template when the name will change!
            
            $txt = '';
            
            if ($pkey != $template_name) {
                $txt = trim($this->GetTemplate($template_name));
            }
            
            if ($txt != "") {
            
                $db_array['name'] = '';
                $db_array['pkey'] = $pkey;
                $db_array['error'] = 'template_exists';
                
                $this->Redirect($id,'template_add_edit','', $db_array);
                return;
            }
            
            $this->DeleteTemplate($pkey);
            $this->SetTemplate($template_name, $template_code);
            
        } else {
                        
            $txt = trim($this->GetTemplate($template_name));
            
            if ($txt != "") {

                $db_array['name'] = '';
                $db_array['error'] = 'template_exists';
                
                $this->Redirect($id,'template_add_edit','', $db_array);
                return;
            }
            
            $db_array['pkey'] = $template_name;
            $this->SetTemplate($template_name, $template_code);
            
        }
         
    }
    
    if ( !$error ) {
        
        if ($modus == 'edit') {
            @$this->SendEvent('UpCouponTemplateEdited', $db_array);
            $params = array('templates_message'=> 's_edited,templates_s', 'active_tab' => 'templates');
        } else {
            @$this->SendEvent('UpCouponTemplateAdded', $db_array);
            $params = array('templates_message'=> 's_added,templates_s', 'active_tab' => 'templates');
        }
        
        if ($apply_button_clicked == '1') {
            $params['pkey'] = $template_name;
            $this->Redirect($id, 'template_add_edit', $returnid, $params);
            return;
        } else {
            $this->Redirect($id, 'defaultadmin', $returnid, $params);
            return;
        }

    }
        
 
} // submit 

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

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

$this->smarty->assign('name_text', $this->Lang('name'));
$this->smarty->assign('name_input', $this->CreateInputText($id, 'name', $name, 20, 100));

$template_type_dropdown = array();
$template_type_dropdown[$this->Lang('template_register')] = 'register';
$template_type_dropdown[$this->Lang('template_winner')] = 'winner';
$template_type_dropdown[$this->Lang('template_prize')] = 'prize';

$this->smarty->assign('template_type_text', $this->Lang('template_type'));

if ($modus == 'edit') {
    
    if (isset($params['factory'])) {
        $template_type_edit = $template_type;        
    }
    
    $this->smarty->assign('template_type_input', $this->CreateInputDropdown($id, 'template_type', $template_type_dropdown, -1, $template_type_edit));
} else {
    $this->smarty->assign('template_type_input', $this->CreateInputDropdown($id, 'template_type', $template_type_dropdown, -1, $template_type));
}

$this->smarty->assign('template_code_text', $this->Lang('template_code'));
$this->smarty->assign('template_code_input', $this->CreateTextArea(false, $id, $template_code, 'template_code'));

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

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

#
# EOF
#
?>