FusionDirectory
 All Data Structures Files Functions Variables
class_dialogAttributes.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
22 {
23  protected $dialogClass = "";
24  protected $dialog;
25  protected $attribute;
26 
27  protected $post_cancel = 'add_cancel';
28  protected $post_finish = 'add_finish';
29 
30  function __construct($simplePlugin, $attribute)
31  {
32  $this->attribute = $attribute;
33  $this->dialog = new $this->dialogClass();
34  }
35 
36  function execute ()
37  {
38  if (isset($_POST[$this->post_cancel])) {
39  return $this->handle_cancel();
40  }
41  if (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) {
42  return $this->handle_finish();
43  }
44  return $this->dialog_execute();
45  }
46 
47  function dialog_execute ()
48  {
49  return $this->dialog->execute();
50  }
51 
52  function handle_finish ()
53  {
54  trigger_error('empty function');
55  return FALSE;
56  }
57 
58  function handle_cancel ()
59  {
60  return FALSE;
61  }
62 }
63 
65 {
66  function dialog_execute ()
67  {
68  session::set('filterBlacklist', $this->attribute->getFilterBlackList());
69  session::set('filterWhitelist', $this->attribute->getFilterWhiteList());
70  return parent::dialog_execute();
71  }
72 
73  function handle_finish ()
74  {
75  $result = $this->dialog->detectPostActions();
76  if (isset($result['targets'])) {
77  $headpage = $this->dialog->getHeadpage();
78  foreach ($result['targets'] as $dn) {
79  $attrs = $headpage->getEntry($dn);
80 
81  $this->attribute->addValue($dn, $attrs);
82  }
83  }
84  return FALSE;
85  }
86 }
87 
89 {
90  protected $dialogClass = 'userSelect';
91 }
92 
94 {
95  protected $dialogClass = 'singleUserSelect';
96 
97  function dialog_execute ()
98  {
99  session::set('filterBlacklist', $this->attribute->getFilterBlackList());
100  session::set('filterWhitelist', $this->attribute->getFilterWhiteList());
101  return parent::dialog_execute();
102  }
103 
104  function handle_finish ()
105  {
106  $result = $this->dialog->detectPostActions();
107  if (isset($result['targets']) && count($result['targets'])) {
108  $headpage = $this->dialog->getHeadpage();
109  $dn = $result['targets'][0];
110  $attrs = $headpage->getEntry($dn);
111  $this->attribute->handleDialogResult($dn, $attrs);
112  }
113  return FALSE;
114  }
115 }
116 
118 {
119  protected $dialogClass = 'systemSelect';
120 }
121 
123 {
124  protected $dialogClass = 'userGroupSelect';
125 }
126 
128 {
129  protected $dialogClass = 'userGroupRoleSelect';
130 }
131 
133 {
134  protected $dialogClass = 'groupSelect';
135 }
136 
138 {
139  protected $dialogClass = 'mailAddressSelect';
140 }
141 
149 {
150  protected $dialogClass = NULL;
151 
161  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
162  {
163  Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
164  $this->attribute = FALSE;
165  }
166 
167  function addPostValue ($value)
168  {
169  $this->addValue($value, NULL);
170  }
171 
172  function delPostValue ($key)
173  {
174  $this->removeValue($key);
175  }
176 
177  function addValue ($value, $attrs)
178  {
179  trigger_error("abstract method");
180  }
181 
182  function searchAndRemove ($value)
183  {
184  $row = array_search($value, $this->value);
185  if ($row !== FALSE) {
186  $this->removeValue($row);
187  }
188  }
189 
190  protected function removeValue ($row)
191  {
192  unset($this->value[$row]);
193  }
194 
195  function getFilterBlackList ()
196  {
197  trigger_error("abstract method");
198  }
199 
200  function getFilterWhiteList ()
201  {
202  return array();
203  }
204 
205  function loadPostValue ()
206  {
207  parent::loadPostValue();
208  if ($this->isVisible()) {
209  if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
210  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
211  }
212  }
213  }
214 
215  function applyPostValue ()
216  {
217  }
218 
219  function renderButtons ()
220  {
221  $id = $this->getHtmlId();
222  $buttons = $this->renderInputField(
223  'submit', 'add'.$id.'_dialog',
224  array('value' => '{msgPool type=addButton}')
225  );
226  $buttons .= $this->renderInputField(
227  'submit', 'del'.$id,
228  array('value' => '{msgPool type=delButton}')
229  );
230  return $buttons;
231  }
232 
233  public function htmlIds()
234  {
235  $id = $this->getHtmlId();
236  return array('add'.$id.'_dialog','del'.$id,'row'.$id);
237  }
238 }
239 
241 {
242  protected $dialogClass;
243 
244  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
245  {
246  Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
247  $this->edit_enabled = TRUE;
248  $this->attribute = FALSE;
249  }
250 
251  function getFilterBlackList ()
252  {
253  trigger_error("abstract method");
254  }
255 
256  function getFilterWhiteList ()
257  {
258  return array();
259  }
260 
261  function loadPostValue ()
262  {
263  if ($this->isVisible()) {
264  parent::loadPostValue();
265  parent::applyPostValue();
266  if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
267  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
268  }
269  }
270  }
271 
272  protected function handleEdit($key)
273  {
274  $this->editingValue = $this->value[$key];
275  $this->delPostValue($key);
276  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this, $this->editingValue));
277  }
278 
279  function applyPostValue ()
280  {
281  }
282 
283  function addValue($value)
284  {
285  $this->value[] = $value;
286  $this->reIndexValues();
287  }
288 
289  function renderButtons ()
290  {
291  $id = $this->getHtmlId();
292  return $this->renderInputField(
293  'submit', 'add'.$id.'_dialog',
294  array('value' => '{msgPool type=addButton}')
295  );
296  }
297 
298  public function htmlIds()
299  {
300  $ids = parent::htmlIds();
301  $ids[0] .= '_dialog';
302  return $ids;
303  }
304 
305  protected function getAttributeArrayValue($key, $value)
306  {
307  /* Convert text value to displayable array value */
308  die('Pure virtual method');
309  }
310 }
311 
313 {
314  protected $displays = array();
315  protected $store_attr = 'dn';
316  protected $display_attr = 'cn';
317 
318  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $store_attr = 'dn', $display_attr = 'cn', $acl = '')
319  {
320  $this->store_attr = $store_attr;
321  $this->display_attr = $display_attr;
322  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
323  }
324 
325  function getFilterBlackList ()
326  {
327  return array($this->store_attr => $this->getValue());
328  }
329 
330  function addValue ($dn, $attrs)
331  {
332  if ($this->store_attr == 'dn') {
333  $value = $dn;
334  } else {
335  $value = $attrs[$this->store_attr][0];
336  }
337  if (!in_array($value, $this->value)) {
338  end($this->value);
339  $new_key = key($this->value) + 1;
340  $this->value[$new_key] = $value;
341  $this->fillDisplayValueFrom($new_key, $attrs);
342  }
343  }
344 
345  protected function removeValue ($row)
346  {
347  unset($this->displays[$row]);
348  unset($this->value[$row]);
349  }
350 
351  function getDisplayValues ()
352  {
353  foreach (array_keys($this->value) as $i) {
354  if (!isset($this->displays[$i])) {
355  $this->fillDisplayValue($i);
356  }
357  }
358  return $this->displays;
359  }
360 
361  function setDisplayValues ($array)
362  {
363  $this->displays = $array;
364  }
365 
366  protected function fillDisplayValue ($i)
367  {
368  global $config;
369  $ldap = $config->get_ldap_link();
370 
371  $value = $this->value[$i];
372  if ($this->store_attr == 'dn') {
373  $ldap->cat($value, $this->ldapAttributesToGet());
374  } else {
375  $ldap->cd($config->current['BASE']);
376  $ldap->search('('.$this->store_attr.'='.ldap_escape_f($value).')', $this->ldapAttributesToGet());
377  }
378  $this->fillDisplayValueFrom($i, $ldap->fetch());
379  }
380 
381  protected function ldapAttributesToGet ()
382  {
383  return array($this->display_attr);
384  }
385 
386  protected function fillDisplayValueFrom ($i, $attrs)
387  {
388  if (!isset($attrs[$this->display_attr])) {
389  unset($this->value[$i]);
390  } else {
391  if ($this->display_attr == 'dn') {
392  $this->displays[$i] = $attrs['dn'];
393  } else {
394  $this->displays[$i] = $attrs[$this->display_attr][0];
395  }
396  }
397  }
398 
399  function foreignKeyUpdate($oldvalue, $newvalue, $source)
400  {
401  foreach ($this->value as $key => &$value) {
402  if ($value == $oldvalue) {
403  if ($newvalue === NULL) {
404  $this->removeValue($key);
405  } elseif ($source['MODE'] == 'copy') {
406  $this->value[] = $newvalue;
407  } elseif ($source['MODE'] == 'move') {
408  $value = $newvalue;
409  /* Update display */
410  $this->fillDisplayValue($key);
411  }
412  }
413  }
414  unset($value);
415  }
416 }
417 
425 {
426  protected $dialogClass = 'UserSelectDialog';
427 }
428 
435 {
436  protected $dialogClass = 'UserGroupSelectDialog';
437 
438  protected function ldapAttributesToGet ()
439  {
440  return array('cn', 'uid', 'objectClass');
441  }
442 
443  protected function fillDisplayValueFrom ($i, $attrs)
444  {
445  if (!isset($attrs[$this->display_attr])) {
446  unset($this->value[$i]);
447  } else {
448  if ($this->display_attr == 'dn') {
449  $this->displays[$i] = $attrs['dn'];
450  } elseif (in_array('posixGroup', $attrs['objectClass'])) {
451  $this->displays[$i] = sprintf(_('POSIX group %s'), trim($attrs['cn'][0]));
452  } elseif (in_array('organizationalRole', $attrs['objectClass'])) {
453  $this->displays[$i] = sprintf(_('Role %s'), trim($attrs['cn'][0]));
454  } elseif (in_array('groupOfNames', $attrs['objectClass'])) {
455  $this->displays[$i] = sprintf(_('Group %s'), trim($attrs['cn'][0]));
456  } elseif (isset($attrs['uid'][0])) {
457  $this->displays[$i] = trim($attrs['uid'][0]);
458  } else {
459  $this->displays[$i] = $attrs[$this->display_attr][0];
460  }
461  }
462  }
463 }
464 
466 {
467  protected $dialogClass = 'UserGroupRoleSelectDialog';
468 }
469 
477 {
478  protected $dialogClass = 'GroupSelectDialog';
479 
480  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = '')
481  {
482  parent::__construct ($label, $description, $ldapName, $required, $defaultValue, 'dn', 'cn', $acl);
483  }
484 
485  function addValue ($dn, $entry)
486  {
487  if (!in_array($dn, $this->value)) {
488  $display = $entry['cn'][0];
489  if (isset($entry['description'][0]) && !empty($entry['description'][0])) {
490  $display .= ' ['.preg_replace ('/^Group of user/', _('Group of user'), $entry['description'][0]).']';
491  }
492 
493  $this->value[] = $dn;
494  $this->displays[] = $display;
495  }
496  }
497 }
498 
505 {
506  protected $dialogClass = 'MailSelectDialog';
507 
517  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
518  {
519  SetAttribute::__construct(new MailAttribute($label, $description, $ldapName, $required, "", $acl), $defaultValue);
520  }
521 
522  function getFilterBlackList ()
523  {
524  return array('mail' => $this->getValue());
525  }
526 
527  function addValue ($dn, $attrs)
528  {
529  $value = ($attrs != NULL?$attrs['mail'][0]:$dn);
530  if (!in_array($value, $this->value)) {
531  $this->value[] = $value;
532  }
533  }
534 
535  function renderButtons ()
536  {
537  $id = $this->getHtmlId();
538  $buttons = $this->renderInputField(
539  'submit', 'add'.$id,
540  array('value' => '{msgPool type=addButton}')
541  );
542  $buttons .= $this->renderInputField(
543  'submit', 'add'.$id.'_dialog',
544  array('value' => '{msgPool type=addButton} (from list)')
545  );
546  $buttons .= $this->renderInputField(
547  'submit', 'del'.$id,
548  array('value' => '{msgPool type=delButton}')
549  );
550  return $buttons;
551  }
552 
553  public function htmlIds()
554  {
555  $id = $this->getHtmlId();
556  return array('add'.$id,'add'.$id.'_dialog','del'.$id,'row'.$id);
557  }
558 }
559 
561 {
562  protected $dialogClass = 'SystemSelectDialog';
563 
564  function addValue ($dn, $entry)
565  {
566  $value = $entry['cn'][0];
567  if (!in_array($value, $this->value)) {
568  $this->value[] = $value;
569  }
570  }
571 
572  function getFilterBlackList ()
573  {
574  return array('cn' => $this->getValue());
575  }
576 }
577 
579 {
580  protected $buttonText = NULL;
581  protected $action;
582 
583  function __construct ($label, $description, $ldapName, $buttonText, $action = NULL, $defaultValue = "", $acl = "")
584  {
585  parent::__construct($label, $description, $ldapName, FALSE, $defaultValue, $acl);
586  $this->buttonText = $buttonText;
587  if ($action === NULL) {
588  $action = 'handle_'.$ldapName;
589  }
590  $this->action = $action;
591  }
592 
593  function renderFormInput ()
594  {
595  $id = $this->getHtmlId();
596  $display = $this->renderInputField(
597  'submit', $id,
598  array('value' => '{literal}'.htmlentities($this->buttonText, ENT_COMPAT, 'UTF-8').'{/literal}')
599  );
600  return $this->renderAcl($display);
601  }
602 
603  function loadPostValue ()
604  {
605  $this->postValue = FALSE;
606  if ($this->isVisible()) {
607  if (isset($_POST[$this->getHtmlId()])) {
608  $this->setPostValue(TRUE);
609  }
610  }
611  }
612 
613  function applyPostValue ()
614  {
615  if (!$this->disabled && $this->isVisible() && $this->postValue) {
616  $func = $this->action;
617  $this->plugin->$func();
618  }
619  }
620 }
621 
623 {
624  protected $dialogClass = NULL;
625 
626  function __construct ($label, $description, $ldapName, $buttonText, $dialogClass, $defaultValue = '', $acl = '')
627  {
628  parent::__construct($label, $description, $ldapName, $buttonText, NULL, $defaultValue, $acl);
629  $this->dialogClass = $dialogClass;
630  }
631 
632  function applyPostValue ()
633  {
634  if (!$this->disabled && $this->isVisible() && $this->postValue) {
635  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
636  }
637  }
638 
639  function getFilterBlackList ()
640  {
641  return array();
642  }
643 
644  function getFilterWhiteList ()
645  {
646  return array();
647  }
648 }
649 
651 {
652  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "")
653  {
654  parent::__construct($label, $description, $ldapName, '', 'SingleUserSelectDialog', $defaultValue, $acl);
655  $this->setRequired($required);
656  }
657 
658  function handleDialogResult ($dn, $attrs)
659  {
660  $this->setValue($dn);
661  $this->buttonText = $attrs['cn'][0];
662  }
663 
664  function renderFormInput ()
665  {
666  $id = $this->getHtmlId();
667  $display = '<img src="geticon.php?context=types&amp;icon=user&amp;size=16" alt="user icon" class="center"/>';
668  if ($this->getValue() == '') {
669  $display .= '<b>'._('None').'</b>';
670  } elseif ($this->buttonText === NULL) {
671  $display .= '<b>'._('Unknown').'</b>';
672  } else {
673  $display .= '<a>'.$this->buttonText.'</a>';
674  }
675  $display .= $this->renderInputField(
676  'image', $id,
677  array(
678  'class' => 'center',
679  'src' => 'geticon.php?context=actions&amp;icon=document-edit&amp;size=16',
680  'title' => _('Edit'),
681  'alt' => _('Edit')
682  )
683  );
684  if (!$this->isRequired()) {
685  $display .= $this->renderInputField(
686  'image', $id.'_remove',
687  array(
688  'class' => 'center',
689  'src' => 'geticon.php?context=actions&amp;icon=remove&amp;size=16',
690  'title' => _('Remove'),
691  'alt' => _('Remove')
692  )
693  );
694  }
695  return $this->renderAcl($display);
696  }
697 
698  function loadPostValue ()
699  {
700  $id = $this->getHtmlId();
701  if (!$this->disabled && $this->isVisible()) {
702  foreach (array_keys($_POST) as $name) {
703  if (!$this->isRequired()) {
704  if (preg_match('/^'.$id.'_remove_/', $name)) {
705  $this->setValue('');
706  $this->buttonText = NULL;
707  break;
708  }
709  }
710  if (preg_match('/^'.$id.'_/', $name)) {
711  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
712  break;
713  }
714  }
715  }
716  }
717 
718  protected function loadAttrValue ($attrs)
719  {
720  global $config;
721  parent::loadAttrValue($attrs);
722  $dn = $this->getValue();
723  if ($dn != '') {
724  $ldap = $config->get_ldap_link();
725  $ldap->cat($dn);
726  if ($attrs = $ldap->fetch()) {
727  $this->handleDialogResult($dn, $attrs);
728  } else {
729  $this->buttonText = NULL;
730  }
731  }
732  }
733 
734  public function htmlIds()
735  {
736  $ids = array($this->getHtmlId());
737  if (!$this->isRequired()) {
738  $ids[] = $ids[0].'_remove';
739  }
740  return $ids;
741  }
742 }
743 
745 {
746  protected $initialDialogValue = NULL;
747 
748  function __construct($simplePlugin, $attribute, $value = array())
749  {
750  $this->attribute = $attribute;
751  $this->dialog = new $this->dialogClass('new');
752 
753  $base = $simplePlugin->dn;
754  if ($base == 'new') {
755  $base = $simplePlugin->base;
756  }
757  $this->dialog->set_acl_base($base);
758  $this->dialog->set_acl_category($simplePlugin->acl_category);
759  if (!empty($value)) {
760  $this->initialDialogValue = $value;
761  foreach ($this->dialog->attributes as $attr) {
762  if (isset($value[$attr])) {
763  $this->dialog->$attr = $value[$attr];
764  }
765  }
766  }
767  }
768 
769  function buttons ()
770  {
771  return '<div style="width:100%; text-align:right; clear:both; float:none;">'.
772  ' <input type="submit" name="'.$this->post_finish.'" value="'.msgPool::saveButton().'"/>&nbsp;'.
773  ' <input type="submit" name="'.$this->post_cancel.'" value="'.msgPool::cancelButton().'"/>'.
774  '</div>';
775  }
776 
777  function dialog_execute ()
778  {
779  $this->dialog->save_object();
780  return $this->dialog->execute().$this->buttons();
781  }
782 
783  function handle_finish ()
784  {
785  $this->dialog->save_object();
786  $msgs = $this->dialog->check();
787  if (count($msgs)) {
788  msg_dialog::displayChecks($msgs);
789  return $this->dialog->execute().$this->buttons();
790  }
791 
792  $value = array();
793  foreach ($this->dialog->attributes as $attr) {
794  $value[$attr] = $this->dialog->$attr;
795  }
796 
797  $this->attribute->addValue($value);
798  return FALSE;
799  }
800 
801  function handle_cancel ()
802  {
803  if ($this->initialDialogValue !== NULL) {
804  $this->attribute->addValue($this->initialDialogValue);
805  }
806  return FALSE;
807  }
808 }
809 ?>
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue=array(), $acl="")
The constructor of DialogAttribute.
renderAcl($display)
Add ACL information around display.
static cancelButton()
Text for a cancel button.
setPostValue($value)
Set the postValue.
This class allow to handle easily any kind of LDAP attribute.
This class allows to handle an attribute for selecting groups.
This class allows to handle an attribute with a popup for selection.
static set($name, $value)
Set a value in a session.
static saveButton()
Text for a save button.
__construct($attribute, $values=array(), $valueUnicity=TRUE)
The constructor of SetAttribute.
This class allow to handle easily a multi-valuated attribute.
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue=array(), $acl="")
The constructor of MailsAttribute.
This class allows to handle an attribute for selecting user.
This class allow to handle easily a String LDAP attribute that contains a mail address.
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue="", $acl="")
The constructor of Attribute.
This class allows to handle an attribute for selecting an mail address.
This class allows to handle an attribute for selecting user or groups.