console-seolan /class.xoptions.inc

Language PHP Lines 735
MD5 Hash 2ddc334676d0db1059e43b4ef6378967
Repository https://github.com/jcplat/console-seolan.git View Raw File
  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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<?php
/****c* tzr-5/XOptions
 * NAME
 *   XOptions -- gestion d'un ensemble d'options
 * DESCRIPTION
 * Classe permettant la gestion d'un ensemble d'options dans un bloc de données
 * Utilisation: options des champs, options des modules, etc...
 * SYNOPSIS
 ****/
class XOptions {
  private $_s = NULL;
  private $_p = array();
  function __construct($id = null) {
    if (!is_null($id)) $this->setId($id);
  }

  public function setId($id) {
    $this->id=$id;
  }

  /// Déclaration d'une nouvelle option
  public function setOpt($label, $option, $type, $opts=NULL, $def=NULL, $group='Specific', $level='admin') {
    if(XShell::admini_mode()) $this->_p[$option]=array('label'=>$label, 'type'=>$type, 'options'=>$opts, 'def'=>$def, 'group'=>$group, 'level'=>$level);
    else $this->_p[$option]=array('type'=>$type, 'options'=>$opts, 'def'=>$def, 'level'=>$level);
  }

  /// Applique la valeur d'une option à un objet
  public function set(&$s,$field,$value) {
    // Recupere la valeur par defaut dans l'objet si celle ci n'est pas renseignée dans l'option
    if($this->_p[$field]['def']===NULL && isset($s->$field)) $this->_p[$field]['def']=$s->$field;
    if(@$this->_p[$field]['type']=='boolean') {
      if(!isset($value)) $value=@$this->_p[$field]['def'];
      if($value==1) $s->$field=true;
      else $s->$field=false;
      return;
    }elseif(@$this->_p[$field]['type']=='ttext'){
      $lang=XShell::getLangData();
      $txts=XLabels::getAMsg($this->id.':'.$field,$lang,false);
      $s->$field=$txts;
      return;
    }else{
      if($value==='' || $value===NULL) $value=@$this->_p[$field]['def'];
      $s->$field=$value;
    }
  }

  /// Positionne l'option en lecture seule
  public function setRO($field) {
    $this->_p[$field]['options']['read-only']=true;
  }

  public function delOpt($option) {
    unset($this->_p[$option]);
  }
  public function clearOpts() {
    $this->_p=array();
  }
  public function get($s, $field) {
    return $s->$field;
  }
  public function getValues($s) {
    foreach($this->_p as $n => $p) {
      $r[$n]=$this->get($s, $n);
    }
    return $r;
  }
  public function setValues(&$s1, $s=NULL) {
    if(!is_array($s)) $s=array();
    foreach($this->_p as $n =>$v) {
      if(!isset($s[$n])) $s[$n]=NULL;
      $this->set($s1, $n, $s[$n]);
    }
  }
  public function setLabel($label,$option) {
    if($this->_p[$option])
      $this->_p[$option]['label']=$label;
  }
  public function setComment($comment,$option) {
    if($this->_p[$option])
      $this->_p[$option]['comment']=$comment;
  }
  public function setGroupComment($comment,$option) {
    if($this->_p[$option])
      $this->_p[$option]['groupcomment']=$comment;
  }
  public function getDialog(&$o, $ar=NULL,$block='options',$level='admin') {
    $lang = XShell::getLangUser();
    $ui=array();
    $groups=array();
    foreach($this->_p as $n =>$v) {
      if(!empty($v['group']) && !in_array($v['group'],$groups)) {
	$groups[]=$v['group'];
      }
    }
    if(empty($groups)) $groups[]='Specific';
    foreach($groups as $i1=>$g) {
      foreach($this->_p as $n =>$v) {
	if(empty($v['group'])) $v['group']='Specific';
	if($v['group']==$g) {
	  if((@$v['options']['read-only']) || (($level!='admin') && ($v['level']=='admin'))) {
	    $ui[]=array('field'=>$n,
			'group'=>@$v['group'],
			'label'=>@$v['label'],
			'options'=>@$v['options'],
			'comment'=>@$v['comment'],
			'groupcomment'=>@$v['groupcomment'],
			'edit'=>$this->_getView($block, $n,$this->get($o, $n))
			);
	  } else {
	    $ui[]=array('field'=>$n, 'group'=>@$v['group'],
			'label'=>@$v['label'],
			'options'=>@$v['options'],
			'comment'=>@$v['comment'],
			'groupcomment'=>@$v['groupcomment'],
			'edit'=>$this->_getUI($block, $n, $o)
			);
	  }
	}
      }
    }
    return $ui;
  }
  /**
   * Enregistre les propriétés passées en paramètre
   * @param &$obj Objet dont les propriétés vont être modifiées
   * @param &$values Nouvelles valeurs à insérer dans l'objet passé en paramètre précédent
   */
  public function procDialog(&$obj,&$values) {
    $lang = XShell::getLangUser();
    $ui=array();
    foreach($this->_p as $n =>$v) {
      if(isset($values[$n])){
	$val=$values[$n];
        if(method_exists($this, '_proc'.$this->_p[$n]['type'])) {
          $m='_proc'.$this->_p[$n]['type'];
          $this->$m($obj, $n, $val);
        }
        $this->set($obj, $n, $val);
        $values[$n]=$val;
      }
    }
  }
  private function _procdependency($s, $n, &$v) {
    // suppression des quotes protégeant l'opérateur pour export excel
    $v['op'] = preg_replace('/"(.*)"/', '$1', $v['op']);
  }
  private function _procttext($s, $n, &$v) {
    XLabels::updateAMsg($this->id.':'.$n, $v);
    $v=$this->id.':'.$n;
  }
  private function _proctemplate($s, $n, &$v) {
    if(is_array($v)) {
      $v=implode('',$v);
    }
  }
  private function _procdate($s, $n, &$v) {
    if(empty($this->_p[$n]['options']['free'])){
      $xdate=new XDateDef();
      $ret=$xdate->post_edit($v);
      $v=$ret->raw;
    }
  }
  public function getView(&$o, $ar=NULL,$block='opt') {
    $lang = XShell::getLangUser();
    $ui=array();
    $groups=array();
    foreach($this->_p as $n =>$v) {
      if(!empty($v['group']) && !in_array($v['group'],$groups)) {
	$groups[]=$v['group'];
      }
    }
    if(empty($groups)) $groups[]='Specific';
    foreach($groups as $i1=>$g) {
      foreach($this->_p as $n =>$v) {
	if(empty($v['group'])) $v['group']='Specific';
	if($v['group']==$g) {
	  $ui[]=array('field'=>$n,
		      'group'=>$v['group'],
		      'options'=>$v['options'],
		      'label'=>$v['label'],
		      'view'=>$this->_getView($block, $n,$this->get($o, $n)));
	}
      }
    }
    return $ui;
  }
  private function _getUI($block,$n,&$o) {
    $v=$this->get($o,$n);
    $type=$this->_p[$n]['type'];
    $opts=$this->_p[$n]['options'];
    $txt='';
    if($block) $name=$block.'['.$n.']';
    else $name=$n;
    $onchange = (!empty($opts['validate'])?'onchange="if(this.form.onsubmit()) this.form.submit();"':'');
    switch($type) {
    case 'ttext':
      if(is_array($v)) $txts=$v;
      else{
	$txts=XLabels::getAMsg($this->id.':'.$n,NULL,false);
      }
      $txt="<table>";
      $size=(!empty($opts['size'])?$opts['size']:30);
      if(!empty($opts['rows'])) {
	$cols=$opts['cols'];
	$rows=$opts['rows'];
      }
      $varid=uniqid('v');
      $js='';
      foreach($GLOBALS['TZR_LANGUAGES'] as $lang=>$iso) {
	$txt.='<tr><th>'.$lang.'</th><td>';
	$v=str_replace('<br />','',$txts[$lang]);
	if($opts['rows']) {
	  if($lang == TZR_DEFAULT_LANG){
	    $js .= "jQuery('#".$varid.$lang."').blur(function(){var val = jQuery(this).val();var oldval= jQuery(this).attr('oldval'); jQuery('textarea[id^=$varid]').each(function(){if(jQuery(this).val()==oldval) jQuery(this).val(val)})}).focus(function(){jQuery(this).attr('oldval',jQuery(this).val());});";
	  }
	  $txt.='<textarea id="'.$varid.$lang.'" name="'.$block.'['.$n.']['.$lang.']" cols="'.$cols.'" rows="'.$rows.'" '.$onchange.'>'.$v.'</textarea>';
	} else {
	  $txt.='<input id="'.$varid.$lang.'" type="text" name="'.$block.'['.$n.']['.$lang.']" value="'.$v.'" size="'.$size.'" '.$onchange.'/>';
	  if($lang == TZR_DEFAULT_LANG){
	    $js .= "jQuery('#".$varid.$lang."').blur(function(){var val = jQuery(this).val();var oldval= jQuery(this).attr('oldval'); jQuery('input[id^=$varid]').each(function(){if(jQuery(this).val()==oldval) jQuery(this).val(val)})}).focus(function(){jQuery(this).attr('oldval',jQuery(this).val());});";
	  }
	}
	$txt.='</td></tr>';
	if(!empty($opts['compulsory'])){
	  $js.='TZR.addValidator(["'.$varid.$lang.'",/(.+)/,"","'.XIni::get('error_color').'","XShortTextDef"]);';
	}
      }
      $txt.='</table>';
      if($js !='' ) $txt.='<script type="text/javascript">'.$js.'</script>';
      break;
    case 'text':
      $varid=uniqid('v');
      $size=(!empty($opts['size'])?$opts['size']:30);
      if(@$opts['rows']) {
	$cols=$opts['cols'];
	$rows=$opts['rows'];
	$txt='<textarea id="'.$varid.'" name="'.$block.'['.$n.']" cols="'.$cols.'" rows="'.$rows.'" '.$onchange.'>'.$v.'</textarea>';
      } else {
	$txt='<input id="'.$varid.'" type="text" name="'.$block.'['.$n.']" value="'.htmlentities($v,ENT_COMPAT,TZR_INTERNAL_CHARSET).'" size="'.$size.'" '.@$onchange.'/>';
      }
      if(!empty($opts['compulsory'])){
	$txt.='<script type="text/javascript">TZR.addValidator(["'.$varid.'",/(.+)/,"","'.XIni::get('error_color').'","XShortTextDef"]);</script>';
      }
      break;
    case 'label':
      $size=(!empty($opts['size'])?$opts['size']:30);
      if(!empty($opts['rows'])) {
	$cols=$opts['cols'];
	$rows=$opts['rows'];
	$txt="<textarea name=\"".$block."[$n]\" cols=\"$cols\" rows=\"$rows\" $onchange >$v</textarea>";
      } else {
	$txt="<input type=\"text\" name=\"".$block."[$n]\" value=\"$v\" size=\"$size\" $onchange />";
      }
      break;
    case 'boolean':
      $txt='<select name="'.$block."[$n]\"><option value=\"1\" ".($v?'selected':'').'>'.
	XLabels::getSysLabel('general','yes').'</option>'.
	'<option value="0" '.(!$v?'selected':'').'>'.XLabels::getSysLabel('general','no').'</option>';
      $txt.='</select>';
      break;
    case 'date':
      $txt=$this->_getDateUI($block.'['.$n.']',$v,$opts);
      break;
    case 'object':
      if(empty($opts['table'])) return '';
      $txt=$this->_getLinkUI($block.'['.$n.']',$v,$opts,$opts['table']);
      break;
    case 'template':
      if(!preg_match('/([0-9]+)/',$opts['moid']) && !empty($opts['moid'])) {
	$f=$opts['moid'];
	$opts['moid']=$o->$f;
      }
      $txt=$this->_getLinkUI($block.'['.$n.']',$v,$opts,'TEMPLATES');
      break;
    case 'letter':
      $txt=$this->_getLinkUI($block."[$n]",$v,$opts,'LETTERS');
      break;
    case 'table':
      $k = new Kernel;
      $txt=$k->baseSelector(array('fieldname'=>$name,'value'=>$v,'misc'=>$onchange,'emptyok'=>$opts['emptyok']));
      break;
    case 'module':
      if(!array_key_exists('emptyok',$opts)) $opts['emptyok']=true;
      if(is_object($v)) $v=$v->_moid;
      $txt=XModule::moduleSelector(array('fieldname'=>$name,'value'=>$v,'misc'=>$onchange,'emptyok'=>$opts['emptyok'],
					 'toid'=>@$opts['toid'],'table'=>@$opts['table']));
      break;
    case 'list':
      $txt =$this->_getListUI($block."[$n]",$v,$opts['labels'],$opts['values'],$onchange);
      break;
    case 'multiplelist':
      $txt =$this->_getMultipleListUI($block."[$n]",$v,$opts['labels'],$opts['values'],$onchange);
      break;
    case 'field':
      $t=$opts['table'];
      if(empty($t)) $t='table';
      $table=$this->get($o,$t);
      if(empty($table)) $table=$t;
      if(empty($table) || $table=='%') {
	$txt='';
      } else {
	$x=XDataSource::objectFactoryHelper8('BCLASS=XDSTable&SPECS='.$table);
	$txt=$x->fieldSelector(array('fieldname'=>$block."[$n]",
				     'compulsory'=>$opts['compulsory'],
				     'value'=>$v,'type'=>$opts['type']));
      }
      break;
    case 'dependency':
      $t=$opts['table'];
      if(empty($t)) {
	$t='table';
      }
      $table=$this->get($o,$t);
      if(empty($table)) {
	$txt='';
	break;
      }
      $x = XDataSource::objectFactoryHelper8('BCLASS=XDSTable&SPECS='.$table);
      list($foo,$field)=explode(':',$this->id);
      if(!isset($x->desc[$field])){
	$txt='';
	break;
      }
      $txt=$x->fieldSelector(array('fieldname'=>$block."[$n][f]",'compulsory'=>false,'value'=>$v['f'],
				   'type'=>array('XBoolDef','XLinkDef','XStringSetDef'),
				   'filter'=>array('multivalued'=>false,'field'=>array('!=',$o->field))));
      $txt=str_replace('<select ','<select onchange="document.getElementById(\'ddep_'.$block.'_'.$n.'\').innerHTML=\'\';" ',$txt);
      if(strpos($txt,$x->desc[$v['f']]->field)===false) return $txt;
      if(!empty($v['f'])){
	$xd=clone($x->desc[$v['f']]);
	$xd->multivalued=false;
	$xf=clone($x->desc[$field]);
	$xf->compulsory=false;
	if($xd->ftype=='XBoolDef'){
	  $xd->listbox=true;
	}
	$txt.='<div id="ddep_'.$block.'_'.$n.'">';
	$txt.='<table id="tdep_'.$block.'_'.$n.'"><tr><td></td><th>Valeur</th><th>Style</th><th>Nouvelle valeur</th></tr>';
	$txt.='<tr><td></td><td></td><td></td><td></td><td></td></tr>';
	foreach($v['dval'] as $i=>$foo){
	  $uniq=uniqid();
	  $ui=$this->_getDependencyUI($block."[$n][xxx][$uniq]",$table,$field,$v['f'],
				      $v['op'][$i],$v['dval'][$i],$v['style'][$i],$v['val'][$i],$v['nochange'][$i]);
	  $txt.='<tr>';
	  $txt.='<td><a onclick="TZR.delLine(this); return false;">'.XLabels::getSyslabel('general','delete').'</a></td>';
	  $txt.='<td>'.$ui[0].'</td>';
	  $txt.='<td>'.$ui[1].'</td>';
	  $txt.='<td>'.$ui[2].'</td>';
	}
	$txt.='</table>';
	$txt.='<a href="#" onclick="TZR.addTableLine(\'tdep_'.$block.'_'.$n.'\','.$n.'tds,1); return false;">Ajouter</a>';

	$ui=$this->_getDependencyUI($block."[$n][xxx][xidxid]",$table,$field,$v['f']);
	$txt.='<script type="text/javascript">';
	$txt.='var '.$n.'tds=new Array();';
	$txt.=$n.'tds[0]="'.escapeJavascript('<a onclick="TZR.delLine(this); return false;">'.XLabels::getSyslabel('general','delete').
					     '</a>').'";';
	$txt.=$n.'tds[1]="'.escapeJavascript($ui[0]).'";';
	$txt.=$n.'tds[2]="'.escapeJavascript($ui[1]).'";';
	$txt.=$n.'tds[3]="'.escapeJavascript($ui[2]).'";';
	$txt.='</script>';
	$txt.='</div>';
      }
      break;
    }
    return $txt;
  }
  private function _getView($block, $n,$v) {
    $lang = XShell::getLangUser();
    $type = $this->_p[$n]['type'];
    $opts = $this->_p[$n]['options'];
    $txt=$v;
    switch($type) {
    case 'boolean':
      $txt = ($v?XLabels::getSysLabel('general','yes'):XLabels::getSysLabel('general','no'));
      break;
    case 'date':
      $txt = $v;
      break;
    case 'object':
      if(empty($opts['table'])) return '';
      $txt = $this->_getLinkView($block."[$n]",$v,$opts['table']);
      break;
    case 'template':
      $txt = $this->_getLinkView($block."[$n]",$v,'TEMPLATES');
      break;
    case 'letter':
      $txt = $this->_getLinkView($block."[$n]",$v,'LETTERS');
      break;
    case 'list':
      $txt = $v;
      break;
    case 'table':
      if(!empty($v)) {
	$x = XDataSource::objectFactoryHelper8('BCLASS=XDSTable&SPECS='.$v);
	$txt=$x->getLabel();
      }
      break;
    case 'module':
      $txt='( '.XLabels::getSysLabel('general.empty').' )';
      if(!empty($v)) {
	$x = &XModule::objectFactory(array('moid'=>$v,'tplentry'=>TZR_RETURN_DATA));
	if(!empty($x)) $txt=$x->getLabel();
      }
      break;
    }
    return $txt;
  }
  public function serialize(&$o) {
    $a=$this->getValues($o);
    return serialize($a);
  }

  private function _getDependencyUI($name,$table,$f,$d,$op=NULL,$valued=NULL,$style=NULL,$value=NULL,$nochange=NULL){
    $x=XDataSource::objectFactoryHelper8('BCLASS=XDSTable&SPECS='.$table);
    $xd=clone($x->desc[$d]);
    $xf=clone($x->desc[$f]);
    $xf->compulsory=false;
    $xd->multivalued=$xf->multivalued=false;
    $xd->dependency=$xf->dependency=NULL;

    $txt=array();
    $opt=array('fieldname'=>str_replace('[xxx]','[dval]',$name),'simple'=>true);
    $o=&$xd->edit($valued,$opt);
    $txt[0]='<select name="'.str_replace('[xxx]','[op]',$name).'">'.
      '<option value="="'.($op=='='?' selected':'').'>=</option>'.
      '<option value="!="'.($op=='!='?' selected':'').'>!=</option>'.
      '</select> '.$o->html;

    $txt[1]=$this->_getDependencyStyleUI($name,$style);

    $valname=str_replace('[xxx]','[val]',$name);
    $opt=array('fieldname'=>$valname,'simple'=>true);
    $o=&$xf->edit($value,$opt);
    $txt[2]='<input type="hidden" name="'.str_replace('[xxx]','[nochange_HID]',$name).'" value="0">'.
      '<input type="checkbox" value="1" name="'.str_replace('[xxx]','[nochange]',$name).'" '.($nochange?'checked ':'').
      'onclick="if(this.checked) document.getElementById(\''.$valname.'\').style.display=\'none\'; else '.
      'document.getElementById(\''.$valname.'\').style.display=\'inline\';">no change';
    $txt[2].='<br><span id="'.$valname.'"'.($nochange?'style="display:none" ':'').
      '>'.$o->html.'</span>';

    unset($xd);
    unset($xf);
    return $txt;
  }
  private function _getDependencyStyleUI($name,$style){
    $txt='<select name="'.str_replace('[xxx]','[style]',$name).'">';
    $txt.='<option value="">----</option>';
    $txt.='<option value="hidden"'.($style=='hidden'?' selected':'').'>Cacher</option>';
    $txt.='<option value="invalid"'.($style=='invalid'?' selected':'').'>Invalider</option>';
    $txt.='</select>';
    return $txt;
  }
  private function _getDateUI($name, $value, $opts=NULL) {
    if(!empty($opts['free'])){
      $t='<input type="text" name="'.$name.'_free" value="'.$value.'">';
    }else{
      list($year,$month,$day)=explode('-',$value);
      $xdate=new XDateDef();
      $t=$xdate->getJSCode($value,$name,$name,uniqid(),2);
    }
    return $t;
  }
  private function _getLinkUI($name,$value,$options,$table) {
    $r['FIELD']=$name;
    $r['FTYPE']='XLinkDef';
    $r['DTAB']='';
    $r['COMPULSORY']=false;
    $r['MULTIVALUED']=false;
    $r['TARGET']=$table;
    $o=(object)$r;
    $n=XFieldDef::objectFactory($o);
    $cond='';
    if(!empty($options['cond'])) $cond='and '.$options['cond'];
    if(!empty($options['moid'])) $cond="(modid='".$options['moid']."') $cond";
    $n->filter=$cond;
    $n->checkbox=false;
    $n->multivalued=$options['multivalued'];
    if($n->multivalued) $value=implode('||',unserialize($value));
    if(is_array($value)) $value=implode('',$value);
    $opts1=array('intable'=>true);
    $e=$n->edit($value,$opts);
    $boid=XDataSource::getBoidFromSpecs('XDSTable',$table);
    $url=$GLOBALS['TZR_SESSION_MANAGER']::complete_self().'&moid='.XModule::getMoid(XMODDATASOURCE_TOID).'&function=XDSContentInput&tplentry=br&boid='.$boid.
      '&template=xmoddatasource/XDSContentInput.html&modid='.$options['moid'];
    $e->html.=' <a class="cv8-ajaxlink" href="'.$url.'">'.XLabels::getSysLabel('general','new').'</a>';
    return $e->html;
  }
  private function _getLinkView($name, $value,$table) {
    $r['FIELD']=$name;
    $r['FTYPE']='XLinkDef';
    $r['DTAB']='';
    $r['COMPULSORY']=false;
    $r['MULTIVALUED']=false;
    $r['TARGET']=$table;
    $o=(object)$r;
    $n = XFieldDef::objectFactory($o);
    $e = $n->display($value);
    return $e->html;
  }
  private function _getListUI($name, $value, $labels, $values,$onchange) {
    $t = "<select name=\"$name\" $onchange >";
    foreach($labels as $i => $l) {
      if($value==$values[$i])
	$t.="<option value=\"".$values[$i]."\" selected>".$labels[$i]."</option>";
      else
	$t.="<option value=\"".$values[$i]."\">".$labels[$i].'</option>';
    }
    return $t;
  }
  private function _getMultipleListUI($name, $value, $labels, $values, $onchange) {
    $t = "<select name=\"{$name}[]\" multiple $onchange >";
    foreach($labels as $i => $l) {
      if(in_array($values[$i],$value))
	$t.="<option value=\"".$values[$i]."\" selected>".$labels[$i]."</option>";
      else
	$t.="<option value=\"".$values[$i]."\">".$labels[$i].'</option>';
    }
    return $t;
  }

  /// Retourne le tableau des options avec leur valeur complète
  public function getAllValues(&$o,$ar=NULL,$block='options') {
    $options=array();
    foreach($this->_p as $n=>$v) {
      switch($v['type']){
      case 'ttext':
        $txts=XLabels::getAMsg($this->id.':'.$n);
        foreach($GLOBALS['TZR_LANGUAGES'] as $lang=>$iso) {
          if(!empty($txts[$lang])) $options[$block.'['.$n.']['.$lang.']']=str_replace("\r\n","\n",$txts[$lang]);
        }
        break;
      case 'module':
        $v=$this->get($o,$n);
        if(!empty($v)) $options[$block.'['.$n.']']=$v;
        break;
      case 'boolean':
        $v=$this->get($o,$n);
        if($v) $options[$block.'['.$n.']']=1;
        else $options[$block.'['.$n.']']=0;
        break;
      case 'table':
        $v=$this->get($o,$n);
        if($v && $v!='%') $options[$block.'['.$n.']']=$v;
        break;
      case 'dependency':
        $v=$this->get($o,$n);
        if($v['f']){
          $options[$block.'[dependency][f]']=$v['f'];
          foreach($v['dval'] as $i=>$dv){
            if ($ar['for_export']) // protéger l'operateur pour excel
              $options[$block.'[dependency][op]['.$i.']'] = '"' . $v['op'][$i] . '"';
            else
              $options[$block.'[dependency][op]['.$i.']']=$v['op'][$i];
	    $options[$block.'[dependency][dval]['.$i.']']=$v['dval'][$i];
	    $options[$block.'[dependency][style]['.$i.']']=$v['style'][$i];
	    $options[$block.'[dependency][nochange]['.$i.']']=$v['nochange'][$i];
	    $options[$block.'[dependency][val]['.$i.']']=$v['val'][$i];
	  }
	}
	break;
      default:
        $v=$this->get($o,$n);
        if(!empty($v)) $options[$block.'['.$n.']']=$v;
        break;
      }
    }
    return $options;
  }

  /****m* XOptions/toXML
   * NAME
   *   XOptions::toXML - serialisation vers XML
   * DESCRIPTION
   *   Serialisation d'un ensemble d'options vers XML
   * INPUTS
   * $s - bloc contenant les valeurs des options
   * RETURN
   * chaine representant les options en XML
   ****/
  public function toXML(&$s) {
    $txt="<?xml version=\"1.0\" encoding=\"utf-8\" ?".">";
    $txt.='<properties>';
    foreach($this->_p as $n =>$v) {
      $typ=$v['type'];
      $txt.= "<field name=\"$n\" type=\"$typ\">";
      $val=$this->get($s, $n);
      if($typ=='module' && is_object($val)){
	$val=$val->_moid;
      }elseif(is_array($val)){
	if($v['type']=='dependency' && $val['f']){
	  // Fait un post_edit sur les valeurs et tri les regles par opérateurs
	  $x=XDataSource::objectFactoryHelper8('BCLASS=XDSTable&SPECS='.$this->get($s,'table'));
	  $defined=$orgval=array();
	  foreach($val['dval'] as $i=>$foo){
	    $tmp=$x->desc[$val['f']]->post_edit($val['dval'][$i]);
	    if(in_array($val['op'][$i].'-'.$tmp->raw,$defined)) continue;
	    $tmp2=$s->post_edit($val['val'][$i]);
	    $orgval[$val['op'][$i]]['op'][]=$val['op'][$i];
	    $orgval[$val['op'][$i]]['val'][]=$tmp2->raw;
	    $orgval[$val['op'][$i]]['dval'][]=$tmp->raw;
	    $orgval[$val['op'][$i]]['style'][]=$val['style'][$i];
	    $orgval[$val['op'][$i]]['nochange'][]=$val['nochange'][$i];
	    $defined[]=$val['op'][$i].'-'.$tmp->raw;
	  }
	  $val['op']=array_merge((array)$orgval['=']['op'],(array)$orgval['!=']['op']);
	  $val['val']=array_merge((array)$orgval['=']['val'],(array)$orgval['!=']['val']);
	  $val['dval']=array_merge((array)$orgval['=']['dval'],(array)$orgval['!=']['dval']);
	  $val['style']=array_merge((array)$orgval['=']['style'],(array)$orgval['!=']['style']);
	  $val['nochange']=array_merge((array)$orgval['=']['nochange'],(array)$orgval['!=']['nochange']);
	  $val=serialize($val);
	}else{
	  foreach($val as &$v) convert_charset($v,TZR_INTERNAL_CHARSET,"UTF-8");
	  $val=serialize($val);
	}
      }else{
	convert_charset($val, TZR_INTERNAL_CHARSET,"UTF-8");
      }
      if(isset($val)) $txt.='<value><![CDATA['.$val.']]></value>';
      else $txt.='<value />';
      $txt.='</field>';
    }
    $txt.='</properties>';
    return $txt;
  }
  /***** XOptions/rawToXML
   * NAME
   *   XOptions::rawToXML - transformation d'un tableau en bloc XML
   * DESCRIPTION
   *   création d'un bloc xml depuis un tableau
   * INPUTS
   * $ar - le tableau
   * $ch - encodage des valeurs du tableau
   * RETURN
   * chaine bloc XML
   ****/

  static public function rawToXML($ar,$ch = TZR_INTERNAL_CHARSET){
    $txt="<?xml version=\"1.0\" encoding=\"utf-8\" ?".">";
    $txt.='<properties>';
    if(is_array($ar)) {
      foreach($ar as $n =>$v) {
	$txt.= "<field name=\"$n\" type=\"\">";
	$val=$v;

	if($ch != "UTF-8"){
	  if(is_array($val)){
	    foreach($val as &$v) convert_charset($v,$ch,"UTF-8");
	    $val=serialize($val);
	  }else{
	    convert_charset($val, $ch,"UTF-8");
	  }
	}
	if(isset($val))
	  $txt.='<value><![CDATA['.$val.']]></value>';
	else
	  $txt.='<value />';
	$txt.='</field>';
      }
    }
    $txt.='</properties>';
    return $txt;
  }

  /**
   * Liste d'options construite par XML
   * @param $values object Objet qui récupère les valeurs par défaut des options
   * @param $xml string chaine XML contenant les options
   */
  function setOptsFromXML(&$values, &$xml) {
    $oxml = simplexml_load_string($xml);
    if (empty($oxml->field)) return;
    foreach ($oxml->field as $field) {
      $name = (string) $field['name'];
      $type = (string) $field['type'];
      $label   = (string) $field->label;
      $default = (string) $field->default;
      $comment = (string) $field->comment;
      $options = (array) $field->options;
      $group = (string) $field->group;
      $level = (string) $field->level;
      $this->setOpt($label, $name, $type, $options, $default, empty($group) ? 'Specific' : $group, empty($level) ? 'none' : $level);
      if (!empty($comment)) $this->setComment($comment, $name);
      if (!empty($default)) $this->set($values, $name, $default);
    }
  }

  /****m* XOptions/fromXML
   * NAME
   *   XOptions::fromXML - recupération des donnees depuis un bloc xml
   * DESCRIPTION
   *   Recuperation des données des options depuis un bloc xml
   * INPUTS
   * $s - objet de stockage des données
   * $xml - chaine contenant la representation xml
   * OUTPUTS
   * $s - les valeurs sont mises à jour
   ****/
  public function fromXML(&$s, &$xml) {
    $r=self::rawFromXML($xml);
    $this->setValues($s,$r);
  }
  /****m* XOptions/rawFromXML
   * NAME
   *   XOptions::rawFromXML - recupération des donnees depuis un bloc xml
   * DESCRIPTION
   *   Recuperation des données des options depuis un bloc xml
   * INPUTS
   * $xml - chaine contenant la representation xml
   * OUTPUTS
   * return - les valeurs sont mises à jour
   ****/
  static public function rawFromXML(&$xml) {
    $oxml=simplexml_load_string($xml);
    $r=array();
    if(empty($oxml->field)) return $r;
    foreach($oxml->field as $o) {
      $name=(string)$o['name'];
      $type=(string)$o['type'];
      $value=(string)$o->value;
      if($type=='multiplelist' || $type=='dependency') $value=unserialize($value);
      if(TZR_INTERNAL_CHARSET != "UTF-8"){
	if(is_array($value)){
	  foreach($value as &$v) convert_charset($v,"UTF-8",TZR_INTERNAL_CHARSET);
	}else{
	  convert_charset($value,"UTF-8",TZR_INTERNAL_CHARSET);
	}
      }
      $r[$name]=$value;
    }
    return $r;
  }
}
?>
Back to Top