class_mcQuickform2.inc.php 7.98 KB
<?php
/** 
 * @copyright (c) 2014, netz.coop eG
 */

class mcQuickform2 {
	
	private static $DesignOptions = array(
			'size' => 80, 
			'maxlength' => 255
		);
	
	public static function getQuickform( $param_id, $param_method = 'post', $param_url='', $attributes = null, $trackSubmit = true) {
//		$form = new HTML_QuickForm2($param_id, $param_method);
		if(!$param_url) {
			$param_url = 'index.php?action=';
		}
		$form = new HTML_QuickForm2($param_id, $param_method, $param_url );
		return $form;
	}
	/**
	 *
	 * @param HTML_QuickForm2 $param_form
	 * @param array $param_Values -- array( 'keyname1' => 'value', )
	 */
	public static function setValues(HTML_QuickForm2 &$param_form, array $param_Values) {
		if(!empty($param_Values)) {
			$param_form->addDataSource(new HTML_QuickForm2_DataSource_Array($param_Values));			
		}
		
	}
	
	public static function getHiddenForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_value) {
		$el = $param_form->addElement('hidden', $param_keyname)->setValue($param_value);
		
		// anscheinend muss value bei addElement angegeben werden!!
//		if(!is_null($param_value)) {
//			$el->setValue($param_value);
//		}
		return $el;
	}
	
	public static function getFieldset(HTML_QuickForm2 &$param_form, $param_label) {
		return $param_form->addElement('fieldset')->setLabel(mcLanguage::getWord($param_label));
	}
	
	public static function getEmailForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_label, $param_value=null, array $param_options=null, array $param_rules=null) {
		return self::getTextForm($param_form, $param_keyname, $param_label, $param_value, $param_options, $param_rules);
	}
	
	public static function getTextareaForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_label, $param_value=null, array $param_options=null, array $param_rules=null) {
//		$t =& $quickform->createElement("textarea", "comment", 'Kommentar',$attrs);
		$name = $param_form->addElement('textarea', $param_keyname, $param_label);
//		$name->setRows("10");
//		$name->setCols("90");
		return $name;
	}
	public static function getTextForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_label, $param_value=null, array $param_options=null, array $param_rules=null) {
		if(is_null($param_options)) {
			$options = self::$DesignOptions;
		} else {
			$options = $param_options;
		}
		$name = $param_form->addElement('text', $param_keyname, $options);
        $name->setLabel(mcLanguage::getWord($param_label));
		$name->addFilter('trim');
		if(!is_null($param_rules)) {
//			$param_rules = array('required' => 'Please enter your name')
			foreach($param_rules as $rule => $label) {
				$name->addRule($rule, $label);
			}			
		}
		if(!is_null($param_value)) {
			$name->setValue($param_value);
		}
		

		
		return $name;
	}
	
	public static function getSelectForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_label, array $param_values) {
 		$element = $param_form->addElement('select', $param_keyname); 
		$element->loadOptions($param_values);
		$element->setLabel(mcLanguage::getWord($param_label));
		return $element;		
	}
	public static function getFileForm(HTML_QuickForm2 &$param_form, $param_keyname='file', $param_label='File') {
		$element =& $param_form->addElement('file', $param_keyname, 'Datei:');
		$element->setLabel(mcLanguage::getWord($param_label));
		$param_form->setMaxFileSize(mcConfig::getConfig('maxfilesize')); 				
//		$param_form->addRule($param_keyname, mcLanguage::getWord('fileToBig'),'maxfilesize', mcConfig::getConfig('maxfilesize'));
//		$param_form->addRule($param_keyname, mcLanguage::getWord('onlyFileMime').' ('.$param_mimetype,'mimetype'.')', $param_mimetype);
		return $element;
	}
	
	/**
	 *
	 * @param HTML_QuickForm2 $param_form
	 * @param type $param_keyname
	 * @param type $param_label
	 * @param array $param_options
	 * @param type $param_value  -- '2008-04-12 00:00:00' || time() || array('Y'=>'2004', 'm' => '05', 'd' => '05', 'H' => '03', 'i' => '10', 's' => '20')
	 * @return type 
	 */
	public static function getDateForm(HTML_QuickForm2 &$param_form, $param_keyname, $param_label, array $param_options=null, $param_value=null) {
//		$param_form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
//			'testDate'        => time(),
//		)));		
		if(is_null($param_options)) {
			$param_options = array(
					'language'         => 'de',
					'format' => 'Y-m-d H:i:s',
//					'format'           => 'dlY H:i:s'
//					'format' => 'd-F-Y',
					'minYear'          => 2000, // Erstes Jahr 
					'maxYear'          => 2020, // Letztes Jahr 
					'optionIncrement'  => array( 
						'i' => 5, // Schrittweite Min 
						's' => 10 // Schrittweite Sek 
                    ) 				
				);
		} 
		
		$element = $param_form->addElement('date', $param_keyname,'title', $param_options);
		$element->setLabel(mcLanguage::getWord($param_label));
		
		if($param_value) {
			$element->setValue($param_value);
		}
		
		return $element;
	}	
	
	public static function getSubmitForm(HTML_QuickForm2 &$param_form, $param_keyname='submit', $param_label='submit') {
		$element = $param_form->addElement('submit', 'submit');
		$element->setLabel(mcLanguage::getWord($param_label));
		return $element;
	}
}

class mcQuickForm extends mcQuickform2 {
	private static $quickform = null;
	private static $file = null;
	
	public static function start($param_FormName)  {
		self::$quickform = new HTML_QuickForm2($param_FormName, "post", 'index.php?action=' );
	}


	public static function file($param_element_name='datei', $param_maxfilesize=null, $param_mimetype=null) {
		self::$file = self::$quickform->addElement('file', $param_element_name, mcLanguage::getWord('fileForm'));
//		self::$quickform->setMaxFileSize($maxfilesize);

		if($param_maxfilesize!=null) {
			// Regel zum Pruefen der Dateigroesse
			self::$quickform->addRule($param_element_name, mcLanguage::getWord('fileToBig'),'maxfilesize', $param_maxfilesize);
		}

		if($param_mimetype!=null) {
			self::$quickform->addRule($param_element_name, mcLanguage::getWord('onlyFileMime').' ('.$param_mimetype,'mimetype'.')', $param_mimetype);
		}

	}

	public static function procedure() {

//		if (self::$quickform->validate()) {
//
////			if (self::$file->isUploadedFile()) {
//
////				self::$file->moveUploadedFile('/tmp');
//
//				$fileInfo = self::$file->getValue();
//
//				print_r($fileInfo);
//
////			}
//
//		} else {
//
//			self::$quickform->display();
//
//		}


		if (false===self::$quickform->validate()) {
		   self::$quickform->display();
		   echo self::$quickform;
		}
		else
		{
//			D::show(self::$quickform,1,1,1);
		   // Daten auslesen
		   $datei = self::$quickform->getElement('datei');
//		   D::show(self::$quickform,' '.self::$quickform->elementExists('datei'),1,1);
		   $daten= $datei->getValue();
		   // Fehler aufgetreten?
		   if (0!=$datei['error'])
		   {
			  echo 'Ein Fehler ist aufgetreten';
		   }
		   else
		   {
			  // Datei verschieben
			  if (false === $datei->moveUploadedFile('/daten'))
			  {
				 if (false===copy ($daten['tmp_name'],  "/daten/$daten[name]")) {
					die ('Konnte Datei nicht kopieren ');
				 }
			  }
			  // Informationen zur Datei ausgeben
			  echo "Name: $daten[name]<br />";
			  echo "MIME-Type: $daten[type]<br />";
			  echo "Tempor&auml;rer Name: $daten[tmp_name]<br />";
			  echo "Gr&ouml;&szlig;e: $daten[size]<br />";
		   }
		}
	}

	public static function end() {
		self::$quickform->addElement( 'button', 'SubmitButton', array('type' => 'submit'),	 array('content' => mcLanguage::getWord('save'))	);
	}

	public static function getForm() {
		return self::$quickform;
	}
}


//{
//
//		$fsText = $quickform->addElement('fieldset')->setLabel(Language::getWord('edit_Zinsbenachrichtigung_Textfeld'));
//
//		$defaults = array();
//
//		$qf_Subject = $fsText->addElement('textarea', 'edit_Zinsbenachrichtigung_Textfeld', array('style' => self::$Zinsbenachrichtigung_Textfeld_Size ), array('label' => Language::getWord('edit').' '.Language::getWord('Text').':'));
//		$defaults['edit_Zinsbenachrichtigung_Textfeld'] = $TextfeldActiveRecord->text;
//
//		$quickform->addDataSource(new HTML_QuickForm2_DataSource_Array($defaults));
//
//
//
//
//}


?>