tform_tpl_generator.inc.php 9.35 KB
Newer Older
tbrehm's avatar
tbrehm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
<?php

/*
Copyright (c) 2005, Till Brehm, projektfarm Gmbh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


class tform_tpl_generator {
	
	function buildHTML($formDef,$tab) {
		
		global $app;
		
		$html = '<table width="500" border="0" cellspacing="0" cellpadding="2">';
		$lang = array();
		
		foreach($formDef['tabs'][$tab]['fields'] as $key => $field) {
			switch ($field['formtype']) {
				case 'TEXT':
					$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\"><input name=\"".$key."\" type=\"text\" class=\"text\" value=\"{tmpl_var name='".$key."'}\" size=\"".$field['width']."\" maxlength=\"".$field['maxlength']."\"></td>
  </tr>";
				break;
				case 'TEXTAREA':
					$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\"><textarea name='$key' cols='".$field['cols']."' rows='".$field['rows']."'>{tmpl_var name='".$key."'}</textarea></td>
  </tr>";
				break;
				case 'SELECT':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\">
		<select name=\"".$key."\" class=\"text\">
			{tmpl_var name='".$key."'}
		</select>
	</td>
  </tr>";
				break;
				case 'MULTIPLE':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\">
		<select name=\"".$key."\" class=\"text\" size=\"".$field['rows']."\" multiple>
			{tmpl_var name='".$key."'}
		</select>
	</td>
  </tr>";
				break;
				case 'PASSWORD':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\"><input name=\"".$key."\" type=\"password\" class=\"text\" value=\"{tmpl_var name='".$key."'}\" size=\"".$field['width']."\" maxlength=\"".$field['maxlength']."\"></td>
  </tr>";
				break;
				case 'CHECKBOX':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\">{tmpl_var name='".$key."'}</td>
  </tr>";
				break;
				case 'CHECKBOXARRAY':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\">{tmpl_var name='".$key."'}</td>
  </tr>";
				break;
				case 'RADIO':
				$html .= "
  <tr>
    <td width=\"126\" class=\"frmText11\">{tmpl_var name='".$key."_txt'}:</td>
    <td width=\"366\" class=\"frmText11\">{tmpl_var name='".$key."'}</td>
  </tr>";
				break;
			}
			
			// Language File Eintrag fr "Feld-Titel" anlegen
			$lang[$key."_txt"] = $key;
			
			// language File Eintrag, fr error-Text anlegen
			if($field["errmsg"] != '') {
				$errmsg = $field["errmsg"];
				$lang[$errmsg] = $errmsg;
			}
			
			
		}
		
		$html .= "  <tr>
    <td class=\"frmText11\">&nbsp;</td>
    <td class=\"frmText11\">&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><input name=\"btn_save\" type=\"submit\" class=\"button\" value=\"{tmpl_var name='btn_save_txt'}\">
      <input name=\"btn_cancel\" type=\"button\" class=\"button\" value=\"{tmpl_var name='btn_cancel_txt'}\" onClick=\"self.location.href='".$formDef["list_default"]."';\">
    </td>
  </tr>";
		
		$lang['btn_save_txt'] = "Save";
		$lang['btn_cancel_txt'] = "Cancel";
		
		$html .= "\r\n</table>\r\n<input type=\"hidden\" name=\"id\" value=\"{tmpl_var name='id'}\">";
		
		// speichere Template
		if (!$handle = fopen($formDef['tabs'][$tab]['template'], 'w')) { 
        	print "Cannot open file ($filename)"; 
        	exit; 
   		} 
 
   		if (!fwrite($handle, $html)) { 
			print "Cannot write to file ($filename)"; 
			exit; 
		}
		fclose($handle);
		
		// speichere language Datei
		$this->lng_add($lang,$formDef);
		
		// berprfe, ob es die Tabelle schon gibt,
		// ansonsten wird sie angelegt
		$tables = $app->db->getTables();
		
tbrehm's avatar
tbrehm committed
156
		if(!@in_array($formDef['db_table'],$tables)) {
tbrehm's avatar
tbrehm committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
			// Datenbank noch nicht vorhanden
			
			$columns = array();
			
			// fge ID Feld hinzu
			$col = array(	'action' 		=> 'add',
							'name'			=> $formDef["db_table_idx"],
							'type'			=> 'int64',
							'typeValue'		=> '',
							'defaultValue'	=> '',
							'notNull'		=> true,
							'autoInc'		=> true,
							'option'		=> 'primary'
						);
					
			$columns[] = $col;
			$app->db->show_error_messages = true;
			
tbrehm's avatar
tbrehm committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
			if($formDef["auth"] == 'yes') {
				
				$col = array(	'action' 		=> 'add',
								'name'			=> 'sys_userid',
								'type'			=> 'int32',
								'typeValue'		=> '',
								'defaultValue'	=> '',
								'notNull'		=> true
							);
				$columns[] = $col;
				$col = array(	'action' 		=> 'add',
								'name'			=> 'sys_groupid',
								'type'			=> 'int32',
								'typeValue'		=> '',
								'defaultValue'	=> '',
								'notNull'		=> true
							);
				$columns[] = $col;
				$col = array(	'action' 		=> 'add',
								'name'			=> 'sys_perm_user',
								'type'			=> 'varchar',
								'typeValue'		=> '5',
								'defaultValue'	=> '',
								'notNull'		=> true
							);
				$columns[] = $col;
				$col = array(	'action' 		=> 'add',
								'name'			=> 'sys_perm_group',
								'type'			=> 'varchar',
								'typeValue'		=> '5',
								'defaultValue'	=> '',
								'notNull'		=> true
							);
				$columns[] = $col;
				$col = array(	'action' 		=> 'add',
								'name'			=> 'sys_perm_other',
								'type'			=> 'varchar',
								'typeValue'		=> '5',
								'defaultValue'	=> '',
								'notNull'		=> true
							);
				$columns[] = $col;
			
			}
			
			
tbrehm's avatar
tbrehm committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
			foreach($formDef['tabs'] as $tab) {
				foreach($tab["fields"] as $name => $field) {
					/*
				       $columns = array(action =>   add | alter | drop
				                        name =>     Spaltenname
				                        name_new => neuer Spaltenname, nur bei 'alter' belegt
				                        type =>     42go-Meta-Type: int16, int32, int64, double, char, varchar, text, blob
				                        typeValue => Wert z.B. bei Varchar
				                        defaultValue =>  Default Wert
				                        notNull =>   true | false
				                        autoInc =>   true | false
				                        option =>   unique | primary | index)
				       
				       
					*/
					switch ($field["datatype"]) {
						case 'INTEGER':
							$type = 'int32';
							$typevalue = '';
						break;
						case 'DOUBLE':
							$type = 'double';
							$typevalue = '';
						break;
						case 'CURRENCY':
							$type = 'double';
							$typevalue = '';
						break;
						case 'VARCHAR':
							$type = 'varchar';
							$typeValue = ($field["maxlength"] > 0 and $field["maxlength"] <= 256)?$field["maxlength"]:255;
						break;
						case 'TEXT':
							$type = 'text';
							$typevalue = '';
						break;
						case 'DATE':
							$type = 'int64';
							$typevalue = '';
						break;
					}
					
					
					$col = array(	'action' 		=> 'add',
									'name'			=> $name,
									'type'			=> $type,
									'typeValue'		=> $typeValue,
									'defaultValue'	=> $field["default"],
									'notNull'		=> true
									);
					
					$columns[] = $col;
				}
			}
		
		$app->db->createTable($formDef["db_table"],$columns);
		
		}
    }
	
	function lng_add($lang,$formDef) {
		global $go_api, $go_info;
		
		$lng_file = "lib/lang/en_".$formDef['name'].".lng";
		if(is_file($lng_file)) {
			include($lng_file);
		} else {
			$wb = array();
		}
		
		$wb_out = array_merge($wb,$lang);
		
		if(is_array($wb_out)) {
			$fp = fopen ($lng_file, "w");
			fwrite($fp,"<?php\r\n");
			foreach($wb_out as $key => $val) {
				$new_line = '$wb["'.$key.'"] = '."'$val';\r\n";
				fwrite($fp,$new_line);
				
			}
			fwrite($fp,"?>");
			fclose($fp);
		}
	}
}

?>