summaryrefslogtreecommitdiff
path: root/php/admin/include/form.class.php
blob: 518509342d89e20a154f584cba7bb719bf76bdaa (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*
 *  Copyright (c) 2004 Klarälvdalens Datakonsult AB
 *
 *    Writen by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
 *
 *  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 2, or
 *  (at your option) any later version.
 *
 *  This program is  distributed in the hope that it will be useful,
 *  but WITHOUT  ANY WARRANTY; without even the  implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You can view the  GNU General Public License, online, at the GNU
 *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
 */

require_once('mysmarty.php');

class KolabForm {
  /*
   * $entries should be an array of the form:
   * array( "fieldname" => array( "name" => "uservisible text",
   *                              "value" => "some value",
   *                              "comment" => "some text",
   *                              "type" => "input/textarea/...",
   *                              "validation" => "notempty/<callbackfnct>" ),
   *        "fieldname2" => array( ... ) )
   *
   * The "callbackfnct" function should be a global function with 3 parameters:
   * (form, key, value)
   */
  function KolabForm( $name, $template, $entries ) {
    $this->name = $name;
    $this->template = $template;
    $this->entries = $entries;
    $this->submittext = _('Submit');
    foreach( $this->entries as $key => $value ) {
      if( !isset( $value['type'] ) ) $this->entries[$key]['type'] = '';
      if( !isset( $value['comment'] ) ) $this->entries[$key]['comment'] = '';
      if( !isset( $value['attrs'] ) ) $this->entries[$key]['attrs'] = '';
    }
  }

  /*
   * this function will be called if an instance of KolabForm will be treated
   * as a string
   */
  function __toString() {
    return sprintf("Debuginformation for class: %s; name: %s; template: %s;", get_class($this), $this->name, $this->template);
   }

  /*private*/function comment_helper( &$value ) {
	  $ast = '';
	  if( isset($value['validation'])){
		  if( is_array( $value['validation'] ) && in_array( 'notempty', $value['validation']) ) {
			  $ast = '<span class="required_asterisk">*</span> ';
		  } else if( $value['validation'] == 'notempty' ) {
			  $ast = '<span class="required_asterisk">*</span> ';			  
		  }
	  }
	  return $ast.$value['comment'];
  }
  
  function outputForm() {
	  debug_var_dump($this->entries);
    $str = '<div class="contentform">';
    /*
    if( count( $this->errors ) > 0 ) {
      $str .= '<div class="contenterror">';
      foreach( $this->errors as $err ) {
	$str .= $err.'<br/>';
      }
      $str .= '</div>';
    }*/
    $str .= '<form name="'.$this->name.'" method="post">';
    $str .= '<table class="contentformtable">';
    $str .= _('<tr><th>Attribute</th><th>Value</th><th>Comment</th></tr>')."\n";
    
    $size = 60;
    foreach( $this->entries as $key => $value ) {
      if( !isset( $value['type'] ) || $value['type']=='' ) $value['type'] = 'text';
      if( !isset( $value['comment'] ) ) $value['comment'] = '';
      if( !isset( $value['attrs'] ) ) $value['attrs'] = '';
      if( !isset( $value['value'] ) ) $value['value'] = '';
      switch( $value['type'] ) {
      case 'hidden': continue;
      case 'password':
		if( ereg( 'readonly', $value['attrs'] ) ) {		  
		  // If readonly, skip it -- passwords are at most write-only
		  break;
		}
      case 'input':
      case 'text':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><p class="ctrl">'.MySmarty::htmlentities($value['value']).'</p><input name="'
			.$key.'" type="hidden" value="'.MySmarty::htmlentities($value['value']).'" /></td>';
		} else {
		  $str .= '<td><input name="'.$key.'" type="'.$value['type'].'" value="'.MySmarty::htmlentities($value['value']).'" '
			.MySmarty::htmlentities($value['attrs']).' size="'.$size.'" /></td>';
		}
		$str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;
	  case 'email':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( strpos($value['value'],'@')===false ) {
		  $uname = $value['value'];
		  $domain = '';
		} else {
		  list($uname,$domain) = split('@',$value['value']);
		}
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><p class="ctrl">'.MySmarty::htmlentities($value['value']).'</p><input name="user_'.$key.'" type="hidden" value="'.
			MySmarty::htmlentities($uname).'" /><input name="domain_'.$key.'" type="hidden" value="'.
			MySmarty::htmlentities($domain).'" /></td>';
		} else {
		  $str .= '<td><input name="user_'.$key.'" type="text" value="'.MySmarty::htmlentities($uname).'" '
			.$value['attrs'].' size="'.($size-40).'" />';
		  if( count($value['domains']) == 1 ) {
			$str .= '<input name="domain_'.$key.'" type="hidden" value="'.MySmarty::htmlentities($value['domains'][0]).'" />';
			$str .= '<span class="ctrl">@'.MySmarty::htmlentities($value['domains'][0]).'</span></td>';
		  } else {
			$str .= '@<select name="domain_'.$key.'" '.$value['attrs']." >\n";
			foreach( $value['domains'] as $dom ) {
			  if( $dom == $domain ) $s = 'selected';
			  else $s = '';
			  $str .= '<option value="'.MySmarty::htmlentities($dom).'" '.$s.'>'.MySmarty::htmlentities($dom).'</option>'."\n";
			}
			$str .= '</select>';
			$str .= '</td>';
		  }
		}
		$str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;
	  case 'comment':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		$str .= '<td>'.$value['value'].'</td>';
		$str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;
      case 'textarea':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><p class="ctrl">'.MySmarty::htmlentities($value['value']).'</p></td>';
		} else {
		  $str .= '<td><textarea name="'.$key.'" rows="5" cols="'.$size.'" '.$value['attrs'].' onkeypress="javascript:textareakeypress()">'.MySmarty::htmlentities($value['value']).'</textarea></td>';
		}
		$str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;	
      case 'checkbox':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><span class="ctrl">'.($value['value']?_('Yes'):_('No')).'</span></td>';
		} else {
		  $str .= '<td><input name="'.$key.'" type="'.$value['type'].'" value="on" '.($value['value']?'checked':'').' '.$value['attrs'].' /></td>';
		}
        $str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;	
	  case 'checklist':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><span class="ctrl">'.(join('<br/>',$value['options'])).'</span></td>';
		} else {
		  $str .= '<td><table class="contentform">';
		  foreach( $value['options'] as $opt ) {
			if( is_array( $value['value'] ) ) $checked = ( in_array($opt ,$value['value'] ))?"checked":"";
			else $checked = "";
			//debug("Checking if $opt is in ".join(",",$value['value'])." :$checked");
			$str .= '<tr><td><input name="'.$key.'[]" type="checkbox" value="'.MySmarty::htmlentities($opt).'" '.$value['attrs']." $checked /></td><td>"
			  .MySmarty::htmlentities($opt).'</td></tr>';
		  }
		  $str .= '</table></td>';
		}
        $str .= '<td>'.$value['comment'].'</td>';
		$str .= '</tr>'."\n";
		break;			
	  case 'select':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><p class="ctrl">'.MySmarty::htmlentities($value['options'][$value['value']]).
			'<input type="hidden" name="'.$key.'" value="'.MySmarty::htmlentities($value['value']).'" /></p></td>';
		} else {
		  $str .= '<td><select name="'.$key.'" '.$value['attrs'].' >'."\n";

		  for( $i = 0; $i < count($value['options']); ++$i) {
			if( $i == $value['value'] ) $s = 'selected';
			else $s = '';
			$str .= '<option value="'.$i.'" '.$s.'>'.MySmarty::htmlentities($value['options'][$i]).'</option>'."\n";
		  }
		  $str .= '</select>';
		  $str .= '</td>';
		}
        $str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;
	  case 'foldertypeselect':
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
                $entries = array ( '' => _('Unspecified'), 'mail' => _('Mails'), 'task' => _('Tasks'), 
								   'journal' => _('Journals'), 'event' => _('Events'), 
								   'contact' => _('Contacts'), 'note' => _('Notes'));
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  $str .= '<td><p class="ctrl">'.MySmarty::htmlentities($entries[$value['value']]).
			'<input type="hidden" name="'.$key.'" value="'.MySmarty::htmlentities($value['value']).'" /></p></td>';
		} else {
		  $str .= '<td><select name="'.$key.'" '.$value['attrs'].' >'."\n";
		  foreach ($entries as $id => $title) {
		  	if ( $value['value'] == $id )
				$s = 'selected';
			else
				$s = '';

		  	$str .= '<option value="'.$id.'" '.$s.'>'.MySmarty::htmlentities($title).'</option>'."\n";
		  }
		  $str .= '</select>';
		  $str .= '</td>';
		}
        	$str .= '<td>'.$value['comment'].'</td>';
		$str .= '</tr>'."\n";
		break;
      case 'aclselect': // Special Kolab entry for ACLs
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		if( ereg( 'readonly', $value['attrs'] ) ) {
		  if( $value['user'] ) $str .= '<td><span class="ctrl">'.MySmarty::htmlentities($value['user']).'</span> <span class="ctrl">'.$value['perm'].'</span></td>';
		} else {
		  $str .= '<td><input name="user_'.$key.'" type="'.$value['type'].'" size="'.($size-15).'" value="'
			.MySmarty::htmlentities($value['user']).'" '.$value['attrs'].' />';
		  $str .= '<select name="perm_'.$key.'">'."\n";
		  if( $value['perm'] ) $selected_perm = $value['perm'];
		  else $selected_perm = 'all';
		  foreach( array( 'none', 
						  'post', 
						  'read', 'read/post', 
						  'append', 
						  'write', 
						  'read anon', 
						  'read anon/post', 
						  'read hidden', 
						  'read hidden/post', 
						  'all' ) as $perm ) {
			if( $perm == $selected_perm ) $s = 'selected';
			else $s = '';
			$str .= '<option value="'.$perm.'"'.$s.' >'.$perm.'</option>'."\n";
		  }
		  $str .= '</select>';
		  $str .= '</td>';
		}
		$str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";	
		break;
	  case 'resourcepolicy': // Special Kolab entry for group/resource policies
		debug("resourcepolicy");
		$ro = ereg( 'readonly', $value['attrs'] );
		$str .= '<tr>';
		$str .= '<td>'.$value['name'].'</td>';
		$str .= '<td>';
		$str .= '<table>';
		$i = 0;
		$tmppol = $value['policies'];
		unset($tmppol['']);
		ksort($tmppol);
		$tmppol[''] = 0;
		$policies = array( _('Always accept'), 
						   _('Always reject'), 
						   _('Reject if conflicts'), 
						   _('Manual if conflicts'),
						   _('Manual') );
		foreach( $tmppol as $user => $pol ) {
		  debug("form: ".$user." => ".$pol);		  
		  if( $ro ) {
			if( !$user ) continue;
			$str .= '<tr><td>';
			if( $user == 'anyone' ) $str .= '<p class="ctrl">'._('Anyone').'</p>';
			else $str .= '<p class="ctrl">'.MySmarty::htmlentities($user).'</p>';
			$str .= '</td><td><p class="ctrl">'.MySmarty::htmlentities($policies[$pol]).'</p></td></tr>'."\n";
		  } else {
			$str .= '<tr><td>';
			if( $user == 'anyone' ) {
			  $str .= _('Anyone').'<input type="hidden" name="user_'.$key.'_'.$i.'" value="'.MySmarty::htmlentities($user).'" '.$value['attrs'].' />';
			} else {
			  $str .= '<input name="user_'.$key.'_'.$i.'" type="text" size="'.($size-20)
				.'" value="'.MySmarty::htmlentities($user).'" '.$value['attrs'].' />';
			}
			$str .= '</td><td><select name="policy_'.$key.'_'.$i.'">'."\n";
			$j = 0;
			foreach( $policies as $p ) {
			  if( $j == $pol ) {
				$str .= '<option value="'.$j++.'" selected>'.$p.'</option>'."\n";			
			  } else {
				$str .= '<option value="'.$j++.'">'.$p.'</option>'."\n";
			  }
			}
			$i++;
			$str .= '</select></td></tr>'."\n";
		  }
		}
		$str .= '</table></td>';
        $str .= '<td>'.KolabForm::comment_helper($value).'</td>';
		$str .= '</tr>'."\n";
		break;
      }
    }
    $str .= '<tr><td colspan="3" align="center"><input type="submit" name="submit_'.$this->name.'" value="'
      .$this->submittext.'" '.$value['attrs'].' /></td></tr>';
    $str .= '</table>'."\n";
    foreach( $this->entries as $key => $value ) {
      if( !isset( $value['type'] ) ) $value['type'] = '';
      if( !isset( $value['comment'] ) ) $value['comment'] = '';
      if( !isset( $value['attrs'] ) ) $value['attrs'] = '';
      if( $value['type'] == 'hidden' ) {
		$str .= '<input name="'.$key.'" type="hidden" value="'.MySmarty::htmlentities($value['value']).'" '.$value['attrs'].' />';
      }
    }
    $str .= '</form>';
	$str .= '<div class="required_asterisk">'._('* Required field.').'</div>';
    $str .= '</div>';
    return $str;
  }

  function validate() {
    $this->errors = array();
    foreach( $this->entries as $key => $value ) {
      if( !empty( $value['validation'] ) && !ereg( 'readonly', $value['attrs'] ) ) {
		$vv = $value['validation'];
		if( !is_array($vv) ) $va = array($vv);
		else $va = $vv;
		foreach( $va as $v ) {
		  //print "validating using $v <br/>";
		  if( $v == 'notempty' ) {
			debug("checking nonemptiness of $key: ".KolabForm::getRequestVar($key)." len=".strlen(trim(KolabForm::getRequestVar($key))));
			if( $value['type'] == 'aclselect' ) {
			  // ignore
			} else if( $value['type'] == 'email' ) {
			  debug('Checking '.$value['name'].': '.$_REQUEST['user_'.$key]);
			  if( strlen(trim($_REQUEST['user_'.$key])) == 0 ) {
				  $this->errors[] = _('Required field ').$value['name']._(' is empty');
			  }
			} else if( strlen( trim($_REQUEST[$key]) ) == 0 ) {
			  $this->errors[] = _('Required field ').$value['name']._(' is empty');
			}
		  } else {
			if( $value['type'] == 'aclselect' ) {
			  $data = $_REQUEST['user_'.$key].' '.$_REQUEST['perm_'.$key];
			} else if( $value['type'] == 'email' ) {
			  $data = trim($_REQUEST['user_'.$key]).'@'.trim($_REQUEST['domain_'.$key]);
			} else if( $value['type'] == 'resourcepolicy' ) {
			  $i = 0;
			  $data = array();
			  while( isset($_REQUEST['user_'.$key.'_'.$i] ) ) {
				$data[] = $_REQUEST['user_'.$key.'_'.$i++];
			  }
			} else {
			  $data = $_REQUEST[$key];
			}
			$errstr = $v( $this, $key, $data );
			if( !empty( $errstr ) ) {
			  $this->errors[] = $errstr;
			}
		  }
		}
      }
    }
    //print_r( $this->errors );
    return (count($this->errors) == 0);
  }

  function isSubmitted() {
    return isset( $_REQUEST['submit_'.$this->name] );
  }

  function value( $key ) {
    if( isset( $_REQUEST[$key] ) ) {
      return $_REQUEST[$key];
    } else {
      return $this->entries[$key]['value'];
    }
  }

  function setValues() {
    foreach( $this->entries as $k => $v ) {
      if( $this->entries[$k]['type'] == 'aclselect' ) {
		$this->entries[$k]['user'] = trim($this->value('user_'.$k));
		$this->entries[$k]['perm'] = $this->value('perm_'.$k);
	  } else if( $this->entries[$k]['type'] == 'resourcepolicy' ) {
		$i = 0;
		$pols = array();
		while( isset($_REQUEST['user_'.$k.'_'.$i]) ) {
		  $pols[trim($_REQUEST['user_'.$k.'_'.$i])]
			= trim($_REQUEST['policy_'.$k.'_'.$i]);
		  $i++;
		}
		$this->entries[$k]['policies'] = $pols;
      } else if( $this->entries[$k]['type'] == 'checkbox' ) {
		$this->entries[$k]['value'] = isset( $_REQUEST[$k] );
      } else if( $this->entries[$k]['type'] == 'checklist' ) {
		$this->entries[$k]['value'] = $_REQUEST[$k];
      } else if( $this->entries[$k]['type'] == 'password' ) {
		$this->entries[$k]['value'] = $this->value($k);		
	  } else if( $this->entries[$k]['type'] == 'email' ) {
		$this->entries[$k]['value'] = trim($this->value('user_'.$k)).'@'.trim($this->value('domain_'.$k));
      } else {
		$this->entries[$k]['value'] = trim($this->value($k));
      }
    }    
  }

  /* static */ function getRequestVar($var, $default = false)
  {
	if( isset($_REQUEST[$var]) ) return $_REQUEST[$var];
	else return $default;
  }


  var $name;
  var $template;
  var $errors;
  var $entries;
  var $submittext;
};

/*
  Local variables:
  mode: php
  indent-tabs-mode: t
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8:
 */
?>