tform_tpl_generator.inc.php 9.86 KB
Newer Older
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
<?php

/*
Copyright (c) 2007, 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 {
32 33 34

	function buildHTML($formDef, $tab) {

35
		global $app;
36

37
		$module = $_SESSION["s"]["module"]["name"];
38

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
		$html = '<h2><tmpl_var name="list_head_txt"></h2>
<p><tmpl_var name="list_desc_txt"></p>

<div class="panel panel_'.$formDef['name'].'">

  <div class="pnl_formsarea">
    <fieldset class="inlineLabels"><legend>'.$formDef['tabs'][$tab]['title'].'</legend>';

		$lang = array();
		$html_reqestedelement = "<em>*</em> ";

		foreach($formDef['tabs'][$tab]['fields'] as $key => $field) {
			if ($field['required'] == true ) { $html_reqcode = $html_reqestedelement; } else { $html_reqcode = ''; }

			switch ($field['formtype']) {
54 55
			case 'TEXT':
				$html .= "
56 57 58 59 60
      <div class=\"ctrlHolder\">
      	<label for=\"".$key."\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</label>
        <input name=\"".$key."\" id=\"".$key."\" value=\"{tmpl_var name='".$key."'}\" size=\"".$field['width']."\" maxlength=\"".$field['maxlength']."\" type=\"text\" class=\"textInput\" />
			</div>";
				break;
61 62
			case 'TEXTAREA':
				$html .= "
63 64 65 66 67
      <div class=\"ctrlHolder\">
      	<label for=\"".$key."\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</label>
        <textarea name=\"".$key."\" id=\"".$key."\" rows='".$field['rows']."' cols='".$field['cols']."'>{tmpl_var name='".$key."'}</textarea>
      </div>";
				break;
68 69
			case 'SELECT':
				$html .= "
70 71 72 73 74 75 76
      <div class=\"ctrlHolder\">
      	<label for=\"".$key."\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</label>
        <select name=\"".$key."\" id=\"".$key."\" class=\"selectInput\">
					{tmpl_var name='".$key."'}
				</select>
      </div>";
				break;
77 78
			case 'MULTIPLE':
				$html .= "
79 80 81 82 83 84 85
      <div class=\"ctrlHolder\">
      	<label for=\"".$key."\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</label>
        <select multiple name=\"".$key."\" id=\"".$key."\" class=\"selectInput\">
					{tmpl_var name='".$key."'}
				</select>
      </div>";
				break;
86 87
			case 'PASSWORD':
				$html .= "
88 89 90 91 92
      <div class=\"ctrlHolder\">
      	<label for=\"".$key."\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</label>
        <input name=\"".$key."\" id=\"".$key."\" value=\"{tmpl_var name='".$key."'}\" size=\"".$field['width']."\" maxlength=\"".$field['maxlength']."\" type=\"password\" class=\"textInput\" />
			</div>";
				break;
93 94
			case 'CHECKBOX':
				$html .= "
95 96 97 98 99 100 101
      <div class=\"ctrlHolder\">
				<p class=\"label\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</p>
					<div class=\"multiField\">
						{tmpl_var name='".$key."'}
					</div>
			</div>";
				break;
102 103
			case 'CHECKBOXARRAY':
				$html .= "
104 105 106 107 108 109 110
      <div class=\"ctrlHolder\">
				<p class=\"label\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</p>
					<div class=\"multiField\">
						{tmpl_var name='".$key."'}
					</div>
			</div>";
				break;
111 112
			case 'RADIO':
				$html .= "
113 114 115 116 117 118 119 120
      <div class=\"ctrlHolder\">
				<p class=\"label\">".$html_reqcode."{tmpl_var name='".$key."_txt'}</p>
					<div class=\"multiField\">
						{tmpl_var name='".$key."'}
					</div>
			</div>";
				break;
			}
121

122
			// Language File Eintrag für "Feld-Titel" anlegen
123
			$lang[$key."_txt"] = $key;
124

125
			// language File Eintrag, für error-Text anlegen
126 127 128 129
			if(isset($field["errmsg"]) && $field["errmsg"] != '') {
				$errmsg = $field["errmsg"];
				$lang[$errmsg] = $errmsg;
			}
130

131
		}
132

133 134 135 136 137 138
		$html .= "
    </fieldset>

    <input type=\"hidden\" name=\"id\" value=\"{tmpl_var name='id'}\">

    <div class=\"buttonHolder buttons\">
139 140
      <button class=\"positive iconstxt icoPositive\" type=\"button\" value=\"{tmpl_var name='btn_save_txt'}\" onclick=\"submitForm('pageForm','".$module."/".$formDef["action"]."');\"><span>{tmpl_var name='btn_save_txt'}</span></button>
      <button class=\"negative iconstxt icoNegative\" type=\"button\" value=\"{tmpl_var name='btn_cancel_txt'}\" onclick=\"loadContent('".$module."/".$formDef["list_default"]."');\"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
141 142
    </div>
  </div>
143

144 145 146
</div>
";

147

148
		// speichere Template
149 150 151 152 153 154 155 156
		if (!$handle = fopen($formDef['tabs'][$tab]['template'], 'w')) {
			print "Cannot open file (".$formDef['tabs'][$tab]['template'].")";
			exit;
		}

		if (!fwrite($handle, $html)) {
			print "Cannot write to file ($filename)";
			exit;
157 158
		}
		fclose($handle);
159 160 161

		$this->lng_add($lang, $formDef);

162
		// überprüfe, ob es die Tabelle schon gibt,
163 164
		// ansonsten wird sie angelegt
		$tables = $app->db->getTables();
165 166

		if(!@in_array($formDef['db_table'], $tables)) {
167
			// Datenbank noch nicht vorhanden
168

169
			$columns = array();
170

171
			// füge ID Feld hinzu
172 173 174 175 176 177 178 179 180 181
			$col = array( 'action'   => 'add',
				'name'   => $formDef["db_table_idx"],
				'type'   => 'int64',
				'typeValue'  => '',
				'defaultValue' => false,
				'notNull'  => true,
				'autoInc'  => true,
				'option'  => 'primary'
			);

182 183
			$columns[] = $col;
			$app->db->show_error_messages = true;
184

185
			if($formDef["auth"] == 'yes') {
186 187 188 189 190 191 192 193

				$col = array( 'action'   => 'add',
					'name'   => 'sys_userid',
					'type'   => 'int32',
					'typeValue'  => '',
					'defaultValue' => '0',
					'notNull'  => true
				);
194
				$columns[] = $col;
195 196 197 198 199 200 201
				$col = array( 'action'   => 'add',
					'name'   => 'sys_groupid',
					'type'   => 'int32',
					'typeValue'  => '',
					'defaultValue' => '0',
					'notNull'  => true
				);
202
				$columns[] = $col;
203 204 205 206 207 208 209
				$col = array( 'action'   => 'add',
					'name'   => 'sys_perm_user',
					'type'   => 'varchar',
					'typeValue'  => '5',
					'defaultValue' => 'NULL',
					'notNull'  => true
				);
210
				$columns[] = $col;
211 212 213 214 215 216 217
				$col = array( 'action'   => 'add',
					'name'   => 'sys_perm_group',
					'type'   => 'varchar',
					'typeValue'  => '5',
					'defaultValue' => 'NULL',
					'notNull'  => true
				);
218
				$columns[] = $col;
219 220 221 222 223 224 225
				$col = array( 'action'   => 'add',
					'name'   => 'sys_perm_other',
					'type'   => 'varchar',
					'typeValue'  => '5',
					'defaultValue' => 'NULL',
					'notNull'  => true
				);
226
				$columns[] = $col;
227

228
			}
229 230


231 232 233 234 235 236 237 238 239 240 241 242
			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)
243 244


245 246
					*/
					switch ($field["datatype"]) {
247 248 249 250
					case 'INTEGER':
						$type = 'int32';
						$typevalue = '';
						$defaultValue = ($field["default"] != '')?$field["default"]:'0';
251
						break;
252 253 254 255
					case 'DOUBLE':
						$type = 'double';
						$typevalue = '';
						$defaultValue = ($field["default"] != '')?$field["default"]:'0';
256
						break;
257 258 259 260
					case 'CURRENCY':
						$type = 'double';
						$typevalue = '';
						$defaultValue = ($field["default"] != '')?$field["default"]:'0';
261
						break;
262 263 264 265 266
					case 'VARCHAR':
						$type = 'varchar';
						$typeValue = ($field["maxlength"] > 0 and $field["maxlength"] <= 256)?$field["maxlength"]:255;
						// $defaultValue = ($field["default"] != '')?$field["default"]:'NOT NULL';
						$defaultValue = ($field["default"] != '')?$field["default"]:'NULL';
267
						break;
268 269 270 271
					case 'TEXT':
						$type = 'text';
						$typevalue = '';
						$defaultValue = 'NULL';
272
						break;
273
					case 'DATE':
274
						$type = 'date';
275
						$typevalue = '';
276
						$defaultValue = ($field["default"] != '')?$field["default"]:'0000-00-00';
277 278
						break;
					}
279 280 281 282 283 284 285 286 287 288


					$col = array( 'action'   => 'add',
						'name'   => $name,
						'type'   => $type,
						'typeValue'  => $typeValue,
						'defaultValue' => $defaultValue,
						'notNull'  => true
					);

289 290 291
					$columns[] = $col;
				}
			}
292 293 294

			$app->db->createTable($formDef["db_table"], $columns);

295
		}
tbrehm's avatar
tbrehm committed
296
	}
297 298 299 300

	function lng_add($lang, $formDef) {
		global $go_api, $go_info, $conf;

301 302
		$lng_file = "lib/lang/".$conf["language"]."_".$formDef['name'].".lng";
		if(is_file($lng_file)) {
303
			include $lng_file;
304 305 306
		} else {
			$wb = array();
		}
307 308 309

		$wb_out = array_merge($lang, $wb);

310
		if(is_array($wb_out)) {
311 312
			$fp = fopen($lng_file, "w");
			fwrite($fp, "<?php\n");
313
			foreach($wb_out as $key => $val) {
314
				$new_line = '$wb["'.$key.'"] = '."'$val';\n";
315 316
				fwrite($fp, $new_line);

317
			}
318
			fwrite($fp, "?>");
319 320 321
			fclose($fp);
		}
	}
322

323 324
}

325
?>