<?php

$cs_lang = cs_translate('multislide');

$op_multislide = cs_sql_option(__FILE__, 'multislide');

require_once('mods/categories/functions.php');
$files = cs_files();
$cs_multislide_tpl = array();
$errormsg = '';
$multislide_access = 0;
$levels = 0;

$cs_action_head['head']['mod']      = $cs_lang['mod_name'];
$cs_action_head['head']['action']    = $cs_lang['head_create'];

  $cs_multislide['multislide_picture'] = '';
  $cs_multislide['multislide_link'] = '';
  $cs_multislide['multislide_description'] = '';
  $cs_multislide['multislide_access'] = '';
  $cs_multislide['categories_id'] = 0;

  
if(isset($_POST['submit'])) {
	
  $cs_multislide['categories_id'] = empty($_POST['categories_name']) ? $_POST['categories_id'] : cs_categories_create('multislide',$_POST['categories_name']);
  $cs_multislide['multislide_picture'] = $_POST['multislide_picture'];
  $cs_multislide['multislide_link'] = $_POST['multislide_link'];
  $cs_multislide['multislide_description'] = $_POST['multislide_description'];
  $cs_multislide['multislide_access'] = $_POST['multislide_access'];
  $cs_multislide['categories_id'] = $_POST['categories_id'];
				$error = 0;
			$errormsg = '';
		if(empty($files['picture']['tmp_name']) AND empty($cs_multislide['multislide_picture'])) {
			$error++;
			$errormsg .= $cs_lang['no_picture'] . cs_html_br(1);
		}
		if(empty($cs_multislide['categories_id']) AND empty($_POST['categories_name'])) {
			$error++;
			$errormsg .= $cs_lang['no_cat'] . cs_html_br(1);
		}
		if(empty($error)){
		$multislide_cells = array_keys($cs_multislide);
		$multislide_save = array_values($cs_multislide);
		cs_sql_insert(__FILE__,'multislide',$multislide_cells,$multislide_save);
		
		if(!empty($files['picture']['tmp_name'])) {
    	$img_size = getimagesize($files['picture']['tmp_name']);
			
			switch($img_size[2]) {
    	case 1:
      $extension = 'gif'; break;
    	case 2:
      $extension = 'jpg'; break;
    	case 3:
      $extension = 'png'; break;
    	}
			$getid = cs_sql_select(__FILE__,'multislide','LAST_INSERT_ID() AS multislide_id');
			
			$filename = 'picture-' . $getid['multislide_id'] . '.' . $extension;
			cs_upload('multislide',$filename,$files['picture']['tmp_name']);
			
			$cs_multislide2['multislide_picture'] = 'uploads/multislide/' . $filename;
			$multislide2_cells = array_keys($cs_multislide2);
			$multislide2_save = array_values($cs_multislide2);      
			cs_sql_update(__FILE__,'multislide',$multislide2_cells,$multislide2_save,$getid['multislide_id']);
		}
		
		cs_redirect($cs_lang['create_done'],'multislide');
		}
}  

if(!isset($_POST['submit']) OR !empty($error)) {
  $multislide_access = $cs_multislide['multislide_access'];
  while($levels < 6)
  {
    $multislide_access == $levels ? $sel = 'selected="selected"' : $sel = '';
    $cs_multislide_tpl['access'][$levels]['level_id']    = $levels;
    $cs_multislide_tpl['access'][$levels]['level_name']  = $cs_lang['lev_'.$levels];
    $cs_multislide_tpl['access'][$levels]['selected']    = $sel;
    $levels++;
  }
  if(!empty($error)) {
  $cs_action_head['head']['body']    = $errormsg;
  } else {
  $cs_action_head['head']['body']    = $cs_lang['body_create'];
  }
  
  $cs_multislide_tpl['multislide']['multislide_picture'] = $cs_multislide['multislide_picture'];
	$cs_multislide_tpl['multislide']['multislide_picture_old'] = '';
  $cs_multislide_tpl['multislide']['multislide_link'] = $cs_multislide['multislide_link'];
  $cs_multislide_tpl['multislide']['multislide_description'] = $cs_multislide['multislide_description'];
  $cs_multislide_tpl['multislide']['category'] = cs_categories_dropdown('multislide',$cs_multislide['categories_id']); 
 

  #$cs_multislide_tpl['multislide']['action'] = 'create';
  $cs_multislide_tpl['url']['action'] = cs_url('multislide','create');
  $cs_multislide_tpl['multislide']['id'] = '0';
  $cs_multislide_tpl['multislide']['lang_form'] = $cs_lang['create'];

        $cs_multislide_tpl['abcode']['features'] = cs_abcode_features('multislide_text',1);

  echo cs_subtemplate(__FILE__,$cs_action_head,'multislide','action_head');
  echo cs_subtemplate(__FILE__,$cs_multislide_tpl,'multislide','action_form');
}