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 handle_finish ()
98  {
99  $result = $this->dialog->detectPostActions();
100  if (isset($result['targets']) && count($result['targets'])) {
101  $headpage = $this->dialog->getHeadpage();
102  $dn = $result['targets'][0];
103  $attrs = $headpage->getEntry($dn);
104  $this->attribute->handleDialogResult($dn, $attrs);
105  }
106  return FALSE;
107  }
108 }
109 
111 {
112  protected $dialogClass = 'systemSelect';
113 }
114 
116 {
117  protected $dialogClass = 'userGroupSelect';
118 }
119 
121 {
122  protected $dialogClass = 'userGroupRoleSelect';
123 }
124 
126 {
127  protected $dialogClass = 'groupSelect';
128 }
129 
131 {
132  protected $dialogClass = 'mailAddressSelect';
133 }
134 
142 {
143  protected $dialogClass = NULL;
144 
154  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
155  {
156  Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
157  $this->attribute = FALSE;
158  }
159 
160  function addPostValue ($value)
161  {
162  $this->addValue($value, NULL);
163  }
164 
165  function delPostValue ($key)
166  {
167  $this->removeValue($key);
168  }
169 
170  function addValue ($value, $attrs)
171  {
172  trigger_error("abstract method");
173  }
174 
175  function searchAndRemove ($value)
176  {
177  $row = array_search($value, $this->value);
178  if ($row !== FALSE) {
179  $this->removeValue($row);
180  }
181  }
182 
183  protected function removeValue ($row)
184  {
185  unset($this->value[$row]);
186  }
187 
188  function getFilterBlackList ()
189  {
190  trigger_error("abstract method");
191  }
192 
193  function getFilterWhiteList ()
194  {
195  return array();
196  }
197 
198  function loadPostValue ()
199  {
200  parent::loadPostValue();
201  if ($this->isVisible()) {
202  if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
203  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
204  }
205  }
206  }
207 
208  function applyPostValue ()
209  {
210  }
211 
212  function renderButtons ()
213  {
214  $id = $this->getHtmlId();
215  $buttons = $this->renderInputField(
216  'submit', 'add'.$id.'_dialog',
217  array('value' => '{msgPool type=addButton}')
218  );
219  $buttons .= $this->renderInputField(
220  'submit', 'del'.$id,
221  array('value' => '{msgPool type=delButton}')
222  );
223  return $buttons;
224  }
225 
226  public function htmlIds()
227  {
228  $id = $this->getHtmlId();
229  return array('add'.$id.'_dialog','del'.$id,'row'.$id);
230  }
231 }
232 
234 {
235  protected $dialogClass;
236 
237  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
238  {
239  Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
240  $this->edit_enabled = TRUE;
241  $this->attribute = FALSE;
242  }
243 
244  function getFilterBlackList ()
245  {
246  trigger_error("abstract method");
247  }
248 
249  function getFilterWhiteList ()
250  {
251  return array();
252  }
253 
254  function loadPostValue ()
255  {
256  if ($this->isVisible()) {
257  parent::loadPostValue();
258  parent::applyPostValue();
259  if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
260  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
261  }
262  }
263  }
264 
265  protected function handleEdit($key)
266  {
267  $this->editingValue = $this->value[$key];
268  $this->delPostValue($key);
269  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this, $this->editingValue));
270  }
271 
272  function applyPostValue ()
273  {
274  }
275 
276  function addValue($value)
277  {
278  $this->value[] = $value;
279  $this->reIndexValues();
280  }
281 
282  function renderButtons ()
283  {
284  $id = $this->getHtmlId();
285  return $this->renderInputField(
286  'submit', 'add'.$id.'_dialog',
287  array('value' => '{msgPool type=addButton}')
288  );
289  }
290 
291  public function htmlIds()
292  {
293  $ids = parent::htmlIds();
294  $ids[0] .= '_dialog';
295  return $ids;
296  }
297 
298  protected function getAttributeArrayValue($key, $value)
299  {
300  /* Convert text value to displayable array value */
301  die('Pure virtual method');
302  }
303 }
304 
306 {
307  protected $displays = array();
308  protected $store_attr = 'dn';
309  protected $display_attr = 'cn';
310 
311  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $store_attr = 'dn', $display_attr = 'cn', $acl = '')
312  {
313  $this->store_attr = $store_attr;
314  $this->display_attr = $display_attr;
315  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
316  }
317 
318  function getFilterBlackList ()
319  {
320  return array($this->store_attr => $this->getValue());
321  }
322 
323  function addValue ($dn, $attrs)
324  {
325  if ($this->store_attr == 'dn') {
326  $value = $dn;
327  } else {
328  $value = $attrs[$this->store_attr][0];
329  }
330  if (!in_array($value, $this->value)) {
331  end($this->value);
332  $new_key = key($this->value) + 1;
333  $this->value[$new_key] = $value;
334  $this->fillDisplayValueFrom($new_key, $attrs);
335  }
336  }
337 
338  protected function removeValue ($row)
339  {
340  unset($this->displays[$row]);
341  unset($this->value[$row]);
342  }
343 
344  function getDisplayValues ()
345  {
346  foreach (array_keys($this->value) as $i) {
347  if (!isset($this->displays[$i])) {
348  $this->fillDisplayValue($i);
349  }
350  }
351  return $this->displays;
352  }
353 
354  function setDisplayValues ($array)
355  {
356  $this->displays = $array;
357  }
358 
359  protected function fillDisplayValue ($i)
360  {
361  global $config;
362  $ldap = $config->get_ldap_link();
363 
364  $value = $this->value[$i];
365  if ($this->store_attr == 'dn') {
366  $ldap->cat($value, $this->ldapAttributesToGet());
367  } else {
368  $ldap->cd($config->current['BASE']);
369  $ldap->search('('.$this->store_attr.'='.ldap_escape_f($value).')', $this->ldapAttributesToGet());
370  }
371  $this->fillDisplayValueFrom($i, $ldap->fetch());
372  }
373 
374  protected function ldapAttributesToGet ()
375  {
376  return array($this->display_attr);
377  }
378 
379  protected function fillDisplayValueFrom ($i, $attrs)
380  {
381  if (!isset($attrs[$this->display_attr])) {
382  unset($this->value[$i]);
383  } else {
384  if ($this->display_attr == 'dn') {
385  $this->displays[$i] = $attrs['dn'];
386  } else {
387  $this->displays[$i] = $attrs[$this->display_attr][0];
388  }
389  }
390  }
391 
392  function foreignKeyUpdate($oldvalue, $newvalue, $source)
393  {
394  foreach ($this->value as $key => &$value) {
395  if ($value == $oldvalue) {
396  if ($newvalue === NULL) {
397  $this->removeValue($key);
398  } elseif ($source['MODE'] == 'copy') {
399  $this->value[] = $newvalue;
400  } elseif ($source['MODE'] == 'move') {
401  $value = $newvalue;
402  /* Update display */
403  $this->fillDisplayValue($key);
404  }
405  }
406  }
407  unset($value);
408  }
409 }
410 
418 {
419  protected $dialogClass = 'UserSelectDialog';
420 }
421 
428 {
429  protected $dialogClass = 'UserGroupSelectDialog';
430 
431  protected function ldapAttributesToGet ()
432  {
433  return array('cn', 'uid', 'objectClass');
434  }
435 
436  protected function fillDisplayValueFrom ($i, $attrs)
437  {
438  if (!isset($attrs[$this->display_attr])) {
439  unset($this->value[$i]);
440  } else {
441  if ($this->display_attr == 'dn') {
442  $this->displays[$i] = $attrs['dn'];
443  } elseif (in_array('posixGroup', $attrs['objectClass'])) {
444  $this->displays[$i] = sprintf(_('POSIX group %s'), trim($attrs['cn'][0]));
445  } elseif (in_array('organizationalRole', $attrs['objectClass'])) {
446  $this->displays[$i] = sprintf(_('Role %s'), trim($attrs['cn'][0]));
447  } elseif (in_array('groupOfNames', $attrs['objectClass'])) {
448  $this->displays[$i] = sprintf(_('Group %s'), trim($attrs['cn'][0]));
449  } elseif (isset($attrs['uid'][0])) {
450  $this->displays[$i] = trim($attrs['uid'][0]);
451  } else {
452  $this->displays[$i] = $attrs[$this->display_attr][0];
453  }
454  }
455  }
456 }
457 
459 {
460  protected $dialogClass = 'UserGroupRoleSelectDialog';
461 }
462 
470 {
471  protected $dialogClass = 'GroupSelectDialog';
472 
473  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = '')
474  {
475  parent::__construct ($label, $description, $ldapName, $required, $defaultValue, 'dn', 'cn', $acl);
476  }
477 
478  function addValue ($dn, $entry)
479  {
480  if (!in_array($dn, $this->value)) {
481  $display = $entry['cn'][0];
482  if (isset($entry['description'][0]) && !empty($entry['description'][0])) {
483  $display .= ' ['.preg_replace ('/^Group of user/', _('Group of user'), $entry['description'][0]).']';
484  }
485 
486  $this->value[] = $dn;
487  $this->displays[] = $display;
488  }
489  }
490 }
491 
498 {
499  protected $dialogClass = 'MailSelectDialog';
500 
510  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = array(), $acl = "")
511  {
512  SetAttribute::__construct(new MailAttribute($label, $description, $ldapName, $required, "", $acl), $defaultValue);
513  }
514 
515  function getFilterBlackList ()
516  {
517  return array('mail' => $this->getValue());
518  }
519 
520  function addValue ($dn, $attrs)
521  {
522  $value = ($attrs != NULL?$attrs['mail'][0]:$dn);
523  if (!in_array($value, $this->value)) {
524  $this->value[] = $value;
525  }
526  }
527 
528  function renderButtons ()
529  {
530  $id = $this->getHtmlId();
531  $buttons = $this->renderInputField(
532  'submit', 'add'.$id,
533  array('value' => '{msgPool type=addButton}')
534  );
535  $buttons .= $this->renderInputField(
536  'submit', 'add'.$id.'_dialog',
537  array('value' => '{msgPool type=addButton} (from list)')
538  );
539  $buttons .= $this->renderInputField(
540  'submit', 'del'.$id,
541  array('value' => '{msgPool type=delButton}')
542  );
543  return $buttons;
544  }
545 
546  public function htmlIds()
547  {
548  $id = $this->getHtmlId();
549  return array('add'.$id,'add'.$id.'_dialog','del'.$id,'row'.$id);
550  }
551 }
552 
554 {
555  protected $dialogClass = 'SystemSelectDialog';
556 
557  function addValue ($dn, $entry)
558  {
559  $value = $entry['cn'][0];
560  if (!in_array($value, $this->value)) {
561  $this->value[] = $value;
562  }
563  }
564 
565  function getFilterBlackList ()
566  {
567  return array('cn' => $this->getValue());
568  }
569 }
570 
572 {
573  protected $buttonText = NULL;
574  protected $action;
575 
576  function __construct ($label, $description, $ldapName, $buttonText, $action = NULL, $defaultValue = "", $acl = "")
577  {
578  parent::__construct($label, $description, $ldapName, FALSE, $defaultValue, $acl);
579  $this->buttonText = $buttonText;
580  if ($action === NULL) {
581  $action = 'handle_'.$ldapName;
582  }
583  $this->action = $action;
584  }
585 
586  function renderFormInput ()
587  {
588  $id = $this->getHtmlId();
589  $display = $this->renderInputField(
590  'submit', $id,
591  array('value' => '{literal}'.htmlentities($this->buttonText, ENT_COMPAT, 'UTF-8').'{/literal}')
592  );
593  return $this->renderAcl($display);
594  }
595 
596  function loadPostValue ()
597  {
598  $this->postValue = FALSE;
599  if ($this->isVisible()) {
600  if (isset($_POST[$this->getHtmlId()])) {
601  $this->setPostValue(TRUE);
602  }
603  }
604  }
605 
606  function applyPostValue ()
607  {
608  if (!$this->disabled && $this->isVisible() && $this->postValue) {
609  $func = $this->action;
610  $this->plugin->$func();
611  }
612  }
613 }
614 
616 {
617  protected $dialogClass = NULL;
618 
619  function __construct ($label, $description, $ldapName, $buttonText, $dialogClass, $defaultValue = '', $acl = '')
620  {
621  parent::__construct($label, $description, $ldapName, $buttonText, NULL, $defaultValue, $acl);
622  $this->dialogClass = $dialogClass;
623  }
624 
625  function applyPostValue ()
626  {
627  if (!$this->disabled && $this->isVisible() && $this->postValue) {
628  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
629  }
630  }
631 }
632 
634 {
635  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "")
636  {
637  parent::__construct($label, $description, $ldapName, '', 'SingleUserSelectDialog', $defaultValue, $acl);
638  $this->setRequired($required);
639  }
640 
641  function handleDialogResult ($dn, $attrs)
642  {
643  $this->setValue($dn);
644  $this->buttonText = $attrs['cn'][0];
645  }
646 
647  function renderFormInput ()
648  {
649  $id = $this->getHtmlId();
650  $display = '<img src="geticon.php?context=types&amp;icon=user&amp;size=16" alt="user icon" class="center"/>';
651  if ($this->getValue() == '') {
652  $display .= '<b>'._('None').'</b>';
653  } elseif ($this->buttonText === NULL) {
654  $display .= '<b>'._('Unknown').'</b>';
655  } else {
656  $display .= '<a>'.$this->buttonText.'</a>';
657  }
658  $display .= $this->renderInputField(
659  'image', $id,
660  array(
661  'class' => 'center',
662  'src' => 'geticon.php?context=actions&amp;icon=document-edit&amp;size=16',
663  'title' => _('Edit'),
664  'alt' => _('Edit')
665  )
666  );
667  if (!$this->isRequired()) {
668  $display .= $this->renderInputField(
669  'image', $id.'_remove',
670  array(
671  'class' => 'center',
672  'src' => 'geticon.php?context=actions&amp;icon=remove&amp;size=16',
673  'title' => _('Remove'),
674  'alt' => _('Remove')
675  )
676  );
677  }
678  return $this->renderAcl($display);
679  }
680 
681  function loadPostValue ()
682  {
683  $id = $this->getHtmlId();
684  if (!$this->disabled && $this->isVisible()) {
685  foreach (array_keys($_POST) as $name) {
686  if (!$this->isRequired()) {
687  if (preg_match('/^'.$id.'_remove_/', $name)) {
688  $this->setValue('');
689  $this->buttonText = NULL;
690  break;
691  }
692  }
693  if (preg_match('/^'.$id.'_/', $name)) {
694  $this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
695  break;
696  }
697  }
698  }
699  }
700 
701  protected function loadAttrValue ($attrs)
702  {
703  global $config;
704  parent::loadAttrValue($attrs);
705  $dn = $this->getValue();
706  if ($dn != '') {
707  $ldap = $config->get_ldap_link();
708  $ldap->cat($dn);
709  if ($attrs = $ldap->fetch()) {
710  $this->handleDialogResult($dn, $attrs);
711  } else {
712  $this->buttonText = NULL;
713  }
714  }
715  }
716 
717  public function htmlIds()
718  {
719  $ids = array($this->getHtmlId());
720  if (!$this->isRequired()) {
721  $ids[] = $ids[0].'_remove';
722  }
723  return $ids;
724  }
725 }
726 
728 {
729  protected $initialDialogValue = NULL;
730 
731  function __construct($simplePlugin, $attribute, $value = array())
732  {
733  $this->attribute = $attribute;
734  $this->dialog = new $this->dialogClass('new');
735 
736  $base = $simplePlugin->dn;
737  if ($base == 'new') {
738  $base = $simplePlugin->base;
739  }
740  $this->dialog->set_acl_base($base);
741  $this->dialog->set_acl_category($simplePlugin->acl_category);
742  if (!empty($value)) {
743  $this->initialDialogValue = $value;
744  foreach ($this->dialog->attributes as $attr) {
745  if (isset($value[$attr])) {
746  $this->dialog->$attr = $value[$attr];
747  }
748  }
749  }
750  }
751 
752  function buttons ()
753  {
754  return '<div style="width:100%; text-align:right; clear:both; float:none;">'.
755  ' <input type="submit" name="'.$this->post_finish.'" value="'.msgPool::saveButton().'"/>&nbsp;'.
756  ' <input type="submit" name="'.$this->post_cancel.'" value="'.msgPool::cancelButton().'"/>'.
757  '</div>';
758  }
759 
760  function dialog_execute ()
761  {
762  $this->dialog->save_object();
763  return $this->dialog->execute().$this->buttons();
764  }
765 
766  function handle_finish ()
767  {
768  $this->dialog->save_object();
769  $msgs = $this->dialog->check();
770  if (count($msgs)) {
771  msg_dialog::displayChecks($msgs);
772  return $this->dialog->execute().$this->buttons();
773  }
774 
775  $value = array();
776  foreach ($this->dialog->attributes as $attr) {
777  $value[$attr] = $this->dialog->$attr;
778  }
779 
780  $this->attribute->addValue($value);
781  return FALSE;
782  }
783 
784  function handle_cancel ()
785  {
786  if ($this->initialDialogValue !== NULL) {
787  $this->attribute->addValue($this->initialDialogValue);
788  }
789  return FALSE;
790  }
791 }
792 ?>
__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.