<?php

# $Id: function.admin_tab_templates.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;

###############################################################################
# program start 
###############################################################################

$template_type = $this->_get_template_type();

$default_templates = array();
$default_template_types = array();

foreach (array_keys($template_type) as $k) {
    $template_type_key = 'default_'. $k . '_template';
    $template_type_value = $this->GetPreference($template_type_key);
    
    $default_template_types[$k] = $k;
    
    if (isset($template_type_value)) {
        $default_templates[$template_type_value] = $template_type_key; 
    }
    
}

// delete deprecated template preferences

foreach ($default_template_types as $k) {
    $_pref_name = 'default_'. $k . '_template';    
    $this->RemovePreference($_pref_name);
}

foreach ($default_templates as $k => $v) {
    $this->SetPreference($v, $k);
}

if (isset($params['submit_massdelete_templates'])) {

    if( isset($params['pkeys']) && is_array($params['pkeys']) &&
count($params['pkeys']) > 0 ) {

        foreach ($params['pkeys'] as $pkey) {            
            $this->DeleteTemplate($pkey);
            
            if (isset($default_templates[$pkey])) {
                $template_type_key = $default_templates[$pkey];                
                $this->RemovePreference($template_type_key);
            }
            
        }
        
        $params = array('templates_message'=> 's_deleted,templates_s', 'active_tab' => 'templates');
        $this->Redirect($id, 'defaultadmin', $returnid, $params);

    }

}

$alltemplates = $this->ListTemplates();

// remove email_registered_message is used in the settings tab
// remove email_registered_message_winner is used in the settings tab

foreach ($alltemplates as $k => $v) {
    if ($v == 'email_registered_message') {
        unset($alltemplates[$k]);        
    }
    if ($v == 'email_registered_message_winner') {
        unset($alltemplates[$k]);        
    }
}

natcasesort($alltemplates);

$entryarray = array();

foreach( $alltemplates as $tpl ) {
    
    $onerow = new StdClass();
    
    $pkey = $tpl;
    
    $onerow->name = preg_replace('/^(' . join("|", array_keys($template_type)) . ')_/', '', $tpl);

    foreach (array_keys($template_type) as $k)  {
        if ( preg_match("/^$k/", $pkey, $matches) ) {
            $onerow->template_type = $template_type[$k];
            break;
        }
    }
    
    $onerow->editurl = $this->CreateLink($id, 'template_add_edit', $returnid, '', array('pkey'=>$pkey), '', 'true');
    
    $onerow->edit_link = $this->CreateLink(
        $id, 
        'template_add_edit', 
        $returnid,
        $admintheme->DisplayImage('icons/system/edit.gif',
        $this->Lang('edit'),'','','systemicon'),
        array('pkey'=>$pkey)
    );

    $onerow->delete_link = $this->CreateLink(
        $id, 
        'template_delete', 
        $returnid, 
        $admintheme->DisplayImage('icons/system/delete.gif',
        $this->Lang('delete'),'','','systemicon'),
        array('pkey'=>$pkey), 
        $this->Lang('areyousure')
    );
    
    $onerow->select = $this->CreateInputCheckbox($id, 'pkeys[]', $pkey);
    
    // change_default
    
    $default = isset($default_templates[$pkey]) ? '1' : '0';
    
    if ( $default == '1' ) {
        $onerow->default = $this->CreateLink($id, 'template_change_default', $returnid, $admintheme->DisplayImage('icons/system/true.gif', $this->Lang('revert_default'), '', '', 'systemicon'), array('default' => 0, 'pkey' => $pkey));
    } else {
        $onerow->default = $this->CreateLink($id, 'template_change_default', $returnid, $admintheme->DisplayImage('icons/system/false.gif', $this->Lang('approve_default'), '', '', 'systemicon'), array('default' => 1, 'pkey' => $pkey));    
    }
    			    
    $entryarray[] = $onerow;
	
}

$this->smarty->assign_by_ref('items', $entryarray);
$this->smarty->assign('itemcount', count($entryarray));

###############################################################################
# define text for labels
###############################################################################	

$smarty->assign('name_text', $this->Lang('name'));	
$smarty->assign('default_text', $this->Lang('default'));	
$smarty->assign('template_type_text', $this->Lang('template_type'));	

###############################################################################
# setup buttons and links
###############################################################################

$form_params = array();
$form_params['active_tab'] = 'templates';

if (isset($params['templates_message'])) {
        $templates_message = explode(",", $params['templates_message']);
        $smarty->assign('templates_message',$this->ShowMessage($this->Lang($templates_message[0], array($this->Lang($templates_message[1])))));
}

$smarty->assign('startform',$this->CreateFormStart($id, 'defaultadmin', $returnid, 'post', '', false, '', $form_params, ''));

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

$smarty->assign('submit_massdelete',
$this->CreateInputSubmit($id, 'submit_massdelete_templates', $this->Lang('delete_selected_items'), '', '', $this->Lang('areyousure')));

$this->smarty->assign('add_link', $this->CreateLink(
    $id, 
    'template_add_edit', 
    $returnid, 
    $admintheme->DisplayImage('icons/system/newobject.gif', 
    $this->Lang('s_add', array($this->Lang('templates_s'))),
    '',
    '',
    'systemicon'), 
    array(), 
    '', 
    false, 
    false, 
    '') .' '. $this->CreateLink(
        $id, 
        'template_add_edit', 
        $returnid,
        $this->Lang('s_add', array($this->Lang('templates_s'))),
        array(),
        '', 
        false, 
        false, 
        'class="pageoptions"')
    );

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

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

#
# EOF
#
?>
