class_dialogAttributes.inc 17.3 KB
Newer Older
1
2
3
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4
  Copyright (C) 2012-2013  FusionDirectory
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

  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 of the License, 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 should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

21
class GenericDialog
22
{
23
24
25
26
27
28
  protected $dialogClass = "";
  protected $dialog;
  protected $attribute;

  protected $post_cancel = 'add_cancel';
  protected $post_finish = 'add_finish';
29

30
  function __construct($simplePlugin, &$attribute)
31
  {
32
33
    $this->attribute  = $attribute;
    $this->dialog     = new $this->dialogClass($simplePlugin->config, get_userinfo());
34
35
36
37
  }

  function execute ()
  {
38
    if (isset($_POST[$this->post_cancel])) {
39
      return $this->handle_cancel();
40
    }
Côme Bernigaud's avatar
Côme Bernigaud committed
41
    if (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) {
42
      return $this->handle_finish();
43
    }
44
45
46
47
48
49
50
51
52
53
54
55
    return $this->dialog_execute();
  }

  function dialog_execute ()
  {
    return $this->dialog->execute();
  }

  function handle_finish ()
  {
    trigger_error('empty function');
    return FALSE;
56
  }
57
58
59
60
61

  function handle_cancel ()
  {
    return FALSE;
  }
62
63
}

64
class GenericSelectDialog extends GenericDialog
65
{
66
  function dialog_execute ()
67
  {
68
69
    session::set('filterBlacklist', $this->attribute->getFilterBlackList());
    return parent::dialog_execute();
70
71
  }

72
  function handle_finish ()
73
  {
74
75
76
    $result = $this->dialog->detectPostActions();
    if (isset($result['targets'])) {
      $headpage = $this->dialog->getHeadpage();
77
      foreach ($result['targets'] as $dn) {
78
79
80
        $attrs = $headpage->getEntry($dn);

        $this->attribute->addValue($dn, $attrs);
81
82
      }
    }
83
    return FALSE;
84
85
86
  }
}

87
class UserSelectDialog extends GenericSelectDialog
88
{
89
90
  protected $dialogClass = 'userSelect';
}
91

92
93
class SingleUserSelectDialog extends GenericDialog
{
Côme Bernigaud's avatar
Côme Bernigaud committed
94
  protected $dialogClass = 'singleUserSelect';
95
96
97
98
99
100
101
102
103
104
105

  function handle_finish ()
  {
    $result = $this->dialog->detectPostActions();
    if (isset($result['targets']) && count($result['targets'])) {
      $headpage = $this->dialog->getHeadpage();
      $dn       = $result['targets'][0];
      $attrs    = $headpage->getEntry($dn);
      $this->attribute->handleDialogResult($dn, $attrs);
    }
    return FALSE;
106
  }
107
}
108

109
110
111
112
113
class SystemSelectDialog extends GenericSelectDialog
{
  protected $dialogClass = 'systemSelect';
}

114
115
116
class UserGroupSelectDialog extends GenericSelectDialog
{
  protected $dialogClass = 'userGroupSelect';
117
118
}

119
class GroupSelectDialog extends GenericSelectDialog
120
{
121
122
  protected $dialogClass = 'groupSelect';
}
123

124
125
126
class MailSelectDialog extends GenericSelectDialog
{
  protected $dialogClass = 'mailAddressSelect';
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
}

/*! \brief This class allows to handle an attribute with a popup for selection
 *
 * It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more items.
 * It stores their dn as values, but displays the cn.
 *
 */
class DialogAttribute extends SetAttribute
{
  protected $dialogClass = NULL;

  /*! \brief The constructor of DialogAttribute
   *
   *  \param string $label The label to show for this attribute
   *  \param string $description A more detailed description for the attribute
   *  \param string $ldapName The name of the attribute in the LDAP (If it's not in the ldap, still provide a unique name)
   *  \param boolean $required Is this attribute mandatory or not
   *  \param array $defaultValue The default value for this attribute
   *  \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
   */
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
  {
    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
151
152
153
154
155
156
157
158
159
160
161
    $this->attribute = FALSE;
  }

  function addPostValue ($value)
  {
    $this->addValue($value, NULL);
  }

  function delPostValue ($key)
  {
    $this->removeValue($key);
162
163
164
165
166
167
168
  }

  function addValue ($value, $attrs)
  {
    trigger_error("abstract method");
  }

169
  protected function removeValue ($row)
170
  {
171
    unset($this->value[$row]);
172
173
  }

174
  function getFilterBlackList ()
175
  {
176
    trigger_error("abstract method");
177
178
179
180
  }

  function loadPostValue ()
  {
181
    parent::loadPostValue();
182
    if ($this->isVisible()) {
183
      if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
184
185
186
187
188
        $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
      }
    }
  }

189
  function applyPostValue ()
190
191
192
  {
  }

193
  function renderButtons ()
194
  {
195
196
197
198
199
200
201
202
203
204
    $id = $this->getHtmlId();
    $buttons  = $this->renderInputField(
      'submit', 'add'.$id.'_dialog',
      array('value' => '{msgPool type=addButton}')
    );
    $buttons  .= $this->renderInputField(
      'submit', 'del'.$id,
      array('value' => '{msgPool type=delButton}')
    );
    return $buttons;
205
206
  }

207
  protected function htmlIds()
208
  {
209
210
    $id = $this->getHtmlId();
    return array('add'.$id.'_dialog','del'.$id,'row'.$id);
211
  }
212
213
214
215
216
}

class DialogOrderedArrayAttribute extends OrderedArrayAttribute
{
  protected $dialogClass;
217

218
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
219
  {
220
221
222
    Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
    $this->edit_enabled = TRUE;
    $this->attribute    = FALSE;
223
224
  }

225
  function loadPostValue ()
226
  {
227
228
229
230
231
232
233
    if ($this->isVisible()) {
      parent::loadPostValue();
      parent::applyPostValue();
      if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
        $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
      }
    }
234
235
  }

236
  protected function handleEdit($key)
237
  {
238
239
240
    $this->editingValue = $this->value[$key];
    $this->delPostValue($key);
    $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this, $this->editingValue));
241
242
  }

243
  function applyPostValue ()
244
245
246
  {
  }

247
  function addValue($value)
248
  {
249
250
    $this->value[] = $value;
    $this->reIndexValues();
251
252
  }

253
  function renderButtons ()
254
255
  {
    $id = $this->getHtmlId();
256
257
258
259
260
261
262
263
264
265
    return $this->renderInputField(
      'submit', 'add'.$id.'_dialog',
      array('value' => '{msgPool type=addButton}')
    );
  }

  protected function htmlIds()
  {
    $ids = parent::htmlIds();
    $ids[0] .= '_dialog';
266
267
  }

268
  protected function getAttributeArrayValue($value)
269
  {
270
271
    /* Convert text value to displayable array value */
    die('Pure virtual method');
272
273
274
  }
}

275
class GenericDialogAttribute extends DialogAttribute
276
{
277
  protected $displays     = array();
278
279
  protected $store_attr   = 'dn';
  protected $display_attr = 'cn';
280

281
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $store_attr = 'dn', $display_attr = 'cn', $acl = '')
282
  {
283
284
    $this->store_attr   = $store_attr;
    $this->display_attr = $display_attr;
285
286
287
288
289
    parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
  }

  function getFilterBlackList ()
  {
290
    return array($this->store_attr => $this->getValue());
291
292
  }

293
  function addValue ($dn, $attrs)
294
  {
295
296
297
298
299
    if ($this->store_attr == 'dn') {
      $value = $dn;
    } else {
      $value = $attrs[$this->store_attr][0];
    }
300
    if (!in_array($value, $this->value)) {
301
302
303
304
      end($this->value);
      $new_key = key($this->value) + 1;
      $this->value[$new_key] = $value;
      $this->fillDisplayValueFrom($new_key, $attrs);
305
306
307
308
309
310
311
312
313
314
315
    }
  }

  protected function removeValue ($row)
  {
    unset($this->displays[$row]);
    unset($this->value[$row]);
  }

  function getDisplayValues ()
  {
316
    foreach (array_keys($this->value) as $i) {
317
318
319
320
321
322
323
      if (!isset($this->displays[$i])) {
        $this->fillDisplayValue($i);
      }
    }
    return $this->displays;
  }

324
325
326
327
328
  function setDisplayValues ($array)
  {
    $this->displays = $array;
  }

329
330
331
  protected function fillDisplayValue ($i)
  {
    $ldap = $this->plugin->config->get_ldap_link();
332
333
334

    $value = $this->value[$i];
    if ($this->store_attr == 'dn') {
335
      $ldap->cat($value, $this->ldapAttributesToGet());
336
    } else {
337
      $ldap->cd($this->plugin->config->current['BASE']);
338
      $ldap->search('('.$this->store_attr.'='.$value.')', $this->ldapAttributesToGet());
339
    }
340
    $this->fillDisplayValueFrom($i, $ldap->fetch());
Côme Bernigaud's avatar
Côme Bernigaud committed
341
342
  }

343
344
345
346
347
  protected function ldapAttributesToGet ()
  {
    return array($this->display_attr);
  }

Côme Bernigaud's avatar
Côme Bernigaud committed
348
349
350
351
352
353
354
  protected function fillDisplayValueFrom ($i, $attrs)
  {
    if (!isset($attrs[$this->display_attr])) {
      unset($this->value[$i]);
    } else {
      if ($this->display_attr == 'dn') {
        $this->displays[$i] = $attrs['dn'];
355
      } else {
356
        $this->displays[$i] = $attrs[$this->display_attr][0];
357
358
359
360
361
      }
    }
  }
}

362
363
364
365
366
367
368
369
370
371
372
/*! \brief This class allows to handle an attribute for selecting user
 *
 * It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more users.
 * It stores their dn as values, but displays the cn.
 *
 */
class UsersAttribute extends GenericDialogAttribute
{
  protected $dialogClass  = 'UserSelectDialog';
}

373
374
375
376
377
/*! \brief This class allows to handle an attribute for selecting user or groups
 *
 * It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more users or groups.
 * It stores their dn as values, but displays the cn.
 */
378
class UsersGroupsAttribute extends GenericDialogAttribute
379
{
380
  protected $dialogClass = 'UserGroupSelectDialog';
381

382
  protected function ldapAttributesToGet ()
383
  {
384
    return array('cn', 'uid', 'objectClass');
385
386
  }

387
  protected function fillDisplayValueFrom ($i, $attrs)
388
  {
389
390
391
392
393
394
395
396
397
    if (!isset($attrs[$this->display_attr])) {
      unset($this->value[$i]);
    } else {
      if ($this->display_attr == 'dn') {
        $this->displays[$i] = $attrs['dn'];
      } elseif (in_array("posixGroup", $attrs['objectClass'])) {
        $this->displays[$i] = sprintf(_("Group %s"), trim($attrs['cn'][0]));
      } elseif (isset($attrs['uid'][0])) {
        $this->displays[$i] = trim($attrs['uid'][0]);
398
      } else {
399
        $this->displays[$i] = $attrs[$this->display_attr][0];
400
401
402
403
404
405
406
407
408
409
410
      }
    }
  }
}

/*! \brief This class allows to handle an attribute for selecting groups
 *
 * It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more groups.
 * It stores their dn as values, but displays the cn.
 *
 */
411
class GroupsAttribute extends GenericDialogAttribute
412
{
413
414
415
416
417
418
  protected $dialogClass = 'GroupSelectDialog';

  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = '')
  {
    parent::__construct ($label, $description, $ldapName, $required, $defaultValue, 'dn', 'cn', $acl);
  }
419
420
421
422
423

  function addValue ($dn, $entry)
  {
    if (!in_array($dn, $this->value)) {
      $display = $entry['cn'][0];
424
      if (isset($entry['description'][0]) && !empty($entry['description'][0])) {
425
426
427
428
429
430
431
432
433
        $display .= ' ['.preg_replace ('/^Group of user/', _('Group of user'), $entry['description'][0]).']';
      }

      $this->value[]    = $dn;
      $this->displays[] = $display;
    }
  }
}

434
/*! \brief This class allows to handle an attribute for selecting an mail address
435
436
437
438
439
440
 *
 * It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more users.
 *
 */
class MailsAttribute extends DialogAttribute
{
441
  protected $dialogClass = 'MailSelectDialog';
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

  /*! \brief The constructor of MailsAttribute
   *
   *  \param string $label The label to show for this attribute
   *  \param string $description A more detailed description for the attribute
   *  \param string $ldapName The name of the attribute in the LDAP (If it's not in the ldap, still provide a unique name)
   *  \param boolean $required Is this attribute mandatory or not
   *  \param array $defaultValue The default value for this attribute
   *  \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
   */
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
  {
    SetAttribute::__construct(new MailAttribute($label, $description, $ldapName, $required, "", $acl), $defaultValue);
  }

  function getFilterBlackList ()
  {
    return array('mail' => $this->getValue());
  }

  function addValue ($dn, $attrs)
  {
    $value = ($attrs != NULL?$attrs['mail'][0]:$dn);
    if (!in_array($value, $this->value)) {
      $this->value[]    = $value;
    }
  }

  function renderButtons ()
  {
    $id = $this->getHtmlId();
473
474
475
476
477
478
479
480
481
482
483
484
    $buttons  = $this->renderInputField(
      'submit', 'add'.$id,
      array('value' => '{msgPool type=addButton}')
    );
    $buttons  .= $this->renderInputField(
      'submit', 'add'.$id.'_dialog',
      array('value' => '{msgPool type=addButton} (from list)')
    );
    $buttons  .= $this->renderInputField(
      'submit', 'del'.$id,
      array('value' => '{msgPool type=delButton}')
    );
485
486
487
488
489
490
491
492
493
494
    return $buttons;
  }

  protected function htmlIds()
  {
    $id = $this->getHtmlId();
    return array('add'.$id,'add'.$id.'_dialog','del'.$id,'row'.$id);
  }
}

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
class SystemsAttribute extends DialogAttribute
{
  protected $dialogClass = 'SystemSelectDialog';

  function addValue ($dn, $entry)
  {
    $value = $entry['cn'][0];
    if (!in_array($value, $this->value)) {
      $this->value[]    = $value;
    }
  }

  function getFilterBlackList ()
  {
    return array('cn' => $this->getValue());
  }
}

513
514
class ButtonAttribute extends Attribute
{
515
  protected $buttonText = NULL;
516

517
  function __construct ($label, $description, $ldapName, $required, $buttonText, $defaultValue = "", $acl = "")
518
  {
519
    parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
520
521
522
523
524
525
    $this->buttonText   = $buttonText;
  }

  function renderFormInput ()
  {
    $id = $this->getHtmlId();
526
527
528
529
    $display = $this->renderInputField(
      'submit', $id,
      array('value' => '{literal}'.$this->buttonText.'{/literal}')
    );
530
531
532
533
534
535
    return $this->renderAcl($display);
  }

  function applyPostValue ()
  {
  }
536
}
537

538
539
540
541
542
543
544
545
546
547
548
549
class DialogButtonAttribute extends ButtonAttribute
{
  protected $dialogClass  = NULL;

  function __construct ($label, $description, $ldapName, $required, $dialogClass, $buttonText, $defaultValue = "", $acl = "")
  {
    parent::__construct($label, $description, $ldapName, $required, $buttonText, $defaultValue, $acl);
    $this->dialogClass  = $dialogClass;
  }

  function loadPostValue ()
  {
550
551
    if (!$this->disabled && $this->isVisible()) {
      if (isset($_POST[$this->getHtmlId()])) {
552
553
554
555
        $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
      }
    }
  }
556
}
557

558
class UserAttribute extends DialogButtonAttribute
559
560
561
562
563
564
{
  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "")
  {
    parent::__construct($label, $description, $ldapName, $required, 'SingleUserSelectDialog', '', $defaultValue, $acl);
  }

565
566
  function handleDialogResult ($dn, $attrs)
  {
567
568
569
570
571
572
573
    $this->setValue($dn);
    $this->buttonText = $attrs['cn'][0];
  }

  function renderFormInput ()
  {
    $id = $this->getHtmlId();
574
    $display  = '<img src="geticon.php?context=types&icon=user&size=16" alt="user icon" class="center"/>';
575
576
577
578
579
580
581
    if ($this->getValue() == '') {
      $display  .= '<b>'._('None').'</b>';
    } elseif ($this->buttonText === NULL) {
      $display  .= '<b>'._('Unknown').'</b>';
    } else {
      $display  .= '<a>'.$this->buttonText.'</a>';
    }
582
583
584
585
586
587
588
589
590
    $display .= $this->renderInputField(
      'image', $id,
      array(
        'class' => 'center',
        'src'   => 'geticon.php?context=actions&icon=document-edit&size=16',
        'title' => _('Edit'),
        'alt'   => _('Edit')
      )
    );
591
    if (!$this->isRequired()) {
592
593
594
595
596
597
598
599
600
      $display .= $this->renderInputField(
        'image', $id.'_remove',
        array(
          'class' => 'center',
          'src'   => 'geticon.php?context=actions&icon=edit-delete&size=16',
          'title' => _('Remove'),
          'alt'   => _('Remove')
        )
      );
601
    }
602
603
604
    return $this->renderAcl($display);
  }

605
606
607
608
  function loadPostValue ()
  {
    $id = $this->getHtmlId();
    if (!$this->disabled && $this->isVisible()) {
609
      foreach (array_keys($_POST) as $name) {
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
        if (!$this->isRequired()) {
          if (preg_match('/^'.$id.'_remove_/', $name)) {
            $this->setValue('');
            $this->buttonText = NULL;
            break;
          }
        }
        if (preg_match('/^'.$id.'_/', $name)) {
          $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
          break;
        }
      }
    }
  }

625
626
627
628
629
630
631
632
633
634
635
636
637
638
  protected function loadAttrValue ($attrs)
  {
    parent::loadAttrValue($attrs);
    $dn = $this->getValue();
    if ($dn != '') {
      $ldap = $this->plugin->config->get_ldap_link();
      $ldap->cat($dn);
      if ($attrs = $ldap->fetch()) {
        $this->handleDialogResult($dn, $attrs);
      } else {
        $this->buttonText = NULL;
      }
    }
  }
639
640
641
642
643
644
645
646
647

  protected function htmlIds()
  {
    $ids = array($this->getHtmlId());
    if (!$this->isRequired()) {
      $ids[] = $ids[0].'_remove';
    }
    return $ids;
  }
648
}
649
?>