<?php
/**************************************************************************
 * mxFormular for pragmaMx
 * Copyright (c) 2010 pyr0 - http://module-factory.net
 * Version 0.03 
 * Date 10-12-2010
 * Author: pyr0 - pyr0@module-factory.net
 *  
 *  
 * 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 3 of the License, or
 * (at your option) any later version.
 *  
 **************************************************************************
 * 
 * pragmaMx - Web Content Management System.
 * Copyright by pragmaMx Developer Team - http://www.pragmamx.org
 *
 * pragmaMx 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 3 of the License, or
 * (at your option) any later version.
 *************************************************************************/
 
defined('mxMainFileLoaded') or die('access denied');

if (!MX_IS_ADMIN) {
    die ("Access Denied");
}

/**************************
* Neues Formular anlegen  *
**************************/
function NewFormStep(){
  global $prefix,$module_name;
 
  include_once("modules/$module_name/admin/tbl-view.inc.php");
  include_once("modules/$module_name/config.inc.php");
  FormTblViewStart($formular_id=0);
 
  // Einbinden von Validator Klasse zur besseren Ueberpruefung der Eingaben
  include("modules/$module_name/include/validator.class.php");
  
  // Variablen anmelden damit es keine Fehler gibt
  global $form_name,$form_title,$form_info_msg,$form_email,$form_email_abs,$form_subject,$formular_message;
    
  // Testen ob alle noetigen Angaben gemacht wurden
  // und gleich bereinigen dieser Angaben
  if (!empty($_POST)) 
  {  
    $fehler = array();
    
    // Start: Formularname intern 
    $formular = new Validator();
    if(empty($_POST['form_name'])) { 
        $fehler['form_name'] = ''._mxFormular_form_name.' '._mxFormular_error_msg_ende.'';
        $form_name = '';
    }
     else
     {  
        $form_name = $_POST['form_name'];
        $formular->pruefeText( $form_name, ''._mxFormular_form_name.' ', 3, 50 );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_name'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende
    
            
    // Start: Formulartitle
    $formular = new Validator(); 
    if(empty($_POST['form_title'])) {
        $fehler['form_title'] = ''._mxFormular_form_title.' '._mxFormular_error_msg_ende.'';        
    }
     else
     {  
        $form_title = $_POST['form_title'];
        $formular->pruefeText( $form_title, ''._mxFormular_form_title.' ', 3, 50 );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_title'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende
    
    // Start: Info Nachricht pruefen falls vorhanden
    $formular = new Validator();
    if(isset($_POST['form_info_msg'])) {
        $form_info_msg = $_POST['form_info_msg'];
         $formular->pruefeText( $form_info_msg, ''._mxFormular_form_info_msg.' ');
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_info_msg'] = $feld . ': ' .$fehlera. "<br />\n";
      	}
      } 
    // Ende
  
    // Start: Email Empfaenger pruefen
    $formular = new Validator();
    if(empty($_POST['form_email'])) {
        $fehler['form_email'] = ''._mxFormular_form_email.' '._mxFormular_error_msg_ende.'';        
    }
     else
     {  
        $form_email = $_POST['form_email'];
        $formular->pruefeEmail( $form_email, 'Email' );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_email'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende

    // Start: Email Abssender pruefen
    $formular = new Validator();
    if(empty($_POST['form_email_abs'])) {
        $fehler['form_email_abs'] = ''._mxFormular_form_email_abs.' '._mxFormular_error_msg_ende.'';        
    }
     else
     {  
        $form_email = $_POST['form_email_abs'];
        $formular->pruefeEmail( $form_email, 'Email' );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_email_abs'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende
        
    // Start: Betreff der Email
    $formular = new Validator();
    if(empty($_POST['form_subject'])) {
        $fehler['form_subject'] = ''._mxFormular_form_subject.' '._mxFormular_error_msg_ende.'';        
    }
     else
     {  
        #$formulara = new Validator(); 
        $form_subject = $_POST['form_subject'];
        $formular->pruefeText( $form_subject, ''._mxFormular_form_subject.' ', 3, 50 );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['form_subject'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende 
      
    // Start: Nachricht verschickt Infotext
    $formular = new Validator();
    if(empty($_POST['formular_message'])) {
        $fehler['formular_message'] = ''._mxFormular_form_send.' '._mxFormular_error_msg_ende.'';        
    }
     else
     {  
        $formular_message = $_POST['formular_message'];
        $formular->pruefeText( $formular_message, ''._mxFormular_form_send.' ', 5,999 );
     }
     // Fehler in den Fehleraarray stecken
     if (count( $formular->getError() ) > 0)
      {
      	foreach ($formular->getError() as $feld => $fehlera)
      	{
          $fehler['formular_message'] = $feld. ': ' .$fehlera. "<br />\n";
      	}
      }  
    // Ende 
      
     // Wenn Verarbeitung war erfolgreich
     if (empty($fehler)) 
     {
        $form_title = chop(mxAddSlashesForSQL($_POST['form_title']));
        $form_name = chop(mxAddSlashesForSQL($_POST['form_name']));
        $form_info_msg = chop(mxAddSlashesForSQL($_POST['form_info_msg']));
        $form_email = chop(mxAddSlashesForSQL($_POST['form_email']));
        $form_email_abs = chop(mxAddSlashesForSQL($_POST['form_email_abs'])); 
        $form_subject = chop(mxAddSlashesForSQL($_POST['form_subject']));
        $formular_message = chop(mxAddSlashesForSQL($_POST['formular_message']));
        
        $form_spam = intval($_POST['form_spam']);  
        $is_date = time();
        
        $qry = "Insert into " . $prefix . "_mxForm (ID,form_name,form_title,form_info_msg,form_email,form_email_abs,form_subject,formular_message,form_date,form_activ,form_spam,count_field) value ('','$form_name','$form_title','$form_info_msg','$form_email','$form_email_abs','$form_subject','$formular_message','$is_date','1','$form_spam','0')";
        #$formular_id = mysql_insert_id();
	 
    	 	 $result = sql_query($qry);
    	 	 // ID holen vom Eintragen wird benoetigt 
    	   $formular_id = mysql_insert_id();
    	   
        // Fehler beim schreiben in DB dann abbrechen
        if(!$result) 
        {
           mxErrorScreen('Database Error<br /><br />' . mysql_error());
           exit;
        }
          else
          {
            FormTblViewEnde(); 
            mxRedirect('admin.php?op=mxFormular_viewall_formfeld&formular_id='.$formular_id.'', _CHANGESAREOK , $delay = $redi_sek);
              
          }
         
        exit;
 
     }
      // Wenn Verarbeitung nicht erfolgreich war, Ausgabe der gesammelten Fehlermeldungen
      else
      {
        openTableAl();
          echo ' <h2>' . _mxFormular_error . '</h2><br />';
          echo "<ul class='errors'>\n";
          foreach ($fehler as $feldname => $meldung) {
              printf("<li>%s</li>\n", $meldung);
          }
          echo "</ul>\n";
        closeTableAl();
        echo '<br />';
      }
  
  }
  
  // Zeige das Formular an und verschicke es an sich selbst zum Testen
   
  echo '<form  method="post" action="admin.php?op=mxFormular_new_formular">';
  echo '<div align="center">';
  echo '<fieldset style="width: 95%; align=left"><legend>1. '._mxFormular_data.'</legend>';
  echo '<div align="left">'._mxFormular_pflicht.'</div><br />';
 echo '<table class="blind" width="100%">'
          .' <tr class="alternate-a" style="height: 40px">'
					    .'<td width="35%"><b>'._mxFormular_form_name.'</b> *<br />'._mxFormular_form_name_intern.'</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_name_tip.' '._mxFormular_form_name_intern.'"></a></td>'
					    .'<td width="60%" align="center"><input type="text" name="form_name" style="width: 95%" value="'.$form_name.'"></td>'
					  .'</tr>'
					  .' <tr style="height: 40px">'
					    .'<td width="35%"><b>'._mxFormular_form_title.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_title_tip.'"></a></td>'
					    .'<td  width="60%" align="center"><input type="text" name="form_title" style="width: 95%" value="'.$form_title.'"></td>'
					  .'</tr>'
					.'</table>'
					// InfoNachricht	
						.'<table class="blind" width="100%">'
            .' <tr class="alternate-a" style="height: 30px">'
					    .'<td width="35%"><b>'._mxFormular_form_info_msg.'</b></td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_info_msg_tip.'"></a></td>'
					    .'<td width="60%" align="center">&nbsp;</td>'
					  .'</tr>'
					  .'<tr class="alternate-a">'
  						.'<td width="100%" colspan="3" align="center">';
								if ($spaweditor == "1")
								{
              		include_once('spaw/spaw_control.class.php');
        					$sw = new SPAW_Wysiwyg('form_info_msg', $form_info_msg, _DOC_LANGUAGE,'' ,'mini', '100%', '100');
        					$sw->show();
					    	}
									else
									{
					        	echo '<textarea  name="form_info_msg"  style="width: 98%" rows = "'.$textarearows.'" >' . htmlspecialchars($form_info_msg) . '</textarea><br />';
					    		}					    	
						echo'<br /></td>'
  					.'</tr>'          
          .'</table>'
          // Email Sachen
					.'<table class="blind" width="100%">'
          	.'<tr style="height: 40px">'
					    .'<td width="35%" ><b>'._mxFormular_form_email.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_email_tip.'"></a></td>'
					    .'<td width="60%" align="center"><input type="text" name="form_email" style="width: 95%" value="'.$form_email.'"></td>'
					  .'</tr>'
          	.'<tr class="alternate-a" style="height: 40px">'
					    .'<td width="35%" ><b>'._mxFormular_form_email_abs.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_email_abs_tip.'"></a></td>'
					    .'<td width="60%" align="center"><input type="text" name="form_email_abs" style="width: 95%" value="'.$form_email_abs.'"></td>'
					  .'</tr>'
          	.'<tr style="height: 40px">'
					    .'<td width="35%" ><b>'._mxFormular_form_subject.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_subject_tip.'"></a></td>'
					    .'<td width="60%" align="center"><input type="text" name="form_subject" style="width: 95%" value="'.$form_subject.'"></td>'
					  .'</tr>'		
						.'</table>'
						//Nachricht bei erfolgreichem Versenden
						.'<table class="blind" width="100%">'
            .' <tr class="alternate-a" style="height: 30px">'
					    .'<td width="35%"><b>'._mxFormular_form_send.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_send_tip.'"></a></td>'
					    .'<td width="60%" align="center">&nbsp;</td>'
					  .'</tr>'
					  .'<tr class="alternate-a">'
  						.'<td width="100%" colspan="3" align="center">';
								if ($spaweditor == "1")
								{
              		include_once('spaw/spaw_control.class.php');
        					$sw = new SPAW_Wysiwyg('formular_message', $formular_message, _DOC_LANGUAGE,'' ,'mini', '100%', '100');
        					$sw->show();
					    	}
									else
									{
					        	echo '<textarea  name="formular_message"  style="width: 98%" rows = "'.$textarearows.'">' . htmlspecialchars($formular_message) . '</textarea><br />';
					    		}					    	
						echo'<br /></td>'
  					.'</tr>'          
          .'</table>';						
					// Spamschutz
					$form_spam ='0';
					 if(isset($_POST['form_spam']))
            {
              if($_POST['form_spam'] == "1")
              {
                $check_no = '';
                $check_yes = 'checked="checked"';
              }
                else
                {
                  $check_yes = '';
                  $check_no = 'checked="checked"';
                }
              }
              else{ 
                if($form_spam == "0")
              {
                $check_no = '';
                $check_yes = 'checked="checked"';
              }
                else
                {
                  $check_yes = '';
                  $check_no = 'checked="checked"';
                }
              }
				echo'<table class="blind" width="100%">'
          	.'<tr style="height: 40px">'
					    .'<td width="35%" ><b>'._mxFormular_form_spam.'</b> *</td>'
					    .'<td width="5%" align="center"><a href="#"><img src="modules/' .$module_name. '/images/help.png" alt="'._mxFormular_tooltip_img.'" border="0" title="'._mxFormular_form_spam_tip.'"></a></td>'
					    .'<td width="60%" align="left"><input type="radio" value="1" '.$check_yes.'  name="form_spam"> '._mxFormular_yes.'<br /><input type="radio" '.$check_no.' value="0"  name="form_spam"> '._mxFormular_no.'<br /></td>'
					  .'</tr>'	
							.'<tr class="alternate-a" style="height: 40px">'
					    .'<td width="35%" >&nbsp;</td>'
					    .'<td width="5%" align="center">&nbsp;</td>'
					    .'<td width="60%" align="left"><input style="width: 200px" type="submit"  value="'._mxFormular_bottun_edit.'" /></td>'
					  .'</tr>'	
        .'</table>';
  echo '</fieldset></div>';
  echo '</form>'; 
         
  FormTblViewEnde();
}




?>