FusionDirectory
 All Data Structures Files Functions Variables
class_DateAttribute.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 
25 class DateAttribute extends Attribute
26 {
27  protected $format;
28 
39  function __construct ($label, $description, $ldapName, $required, $format, $defaultValue = 'now', $acl = "")
40  {
41  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
42  $this->format = $format;
43  }
44 
45  function inputValue ($value)
46  {
47  if ($value === "" && !$this->isRequired()) {
48  return $value;
49  } elseif (is_object($this->plugin) && $this->plugin->is_template && preg_match('/%/', $value)) {
50  return $value;
51  } else {
52  return $this->ldapToDate($value);
53  }
54  }
55 
56  function getValue ()
57  {
58  if ($this->value === "" && !$this->isRequired()) {
59  return $this->value;
60  } else {
61  try {
62  return $this->getDateValue()->format('d.m.Y');
63  } catch (Exception $e) {
64  if (is_object($this->plugin) && $this->plugin->is_template && preg_match('/%/', $this->value)) {
65  return $this->value;
66  } else {
67  return '';
68  }
69  }
70  }
71  }
72 
73  protected function ldapToDate($ldapValue)
74  {
75  $date = DateTime::createFromFormat($this->format, $ldapValue, new DateTimeZone('UTC'));
76  if ($date !== FALSE) {
77  return $date;
78  } else {
79  trigger_error('LDAP value for '.$this->getLdapName().' was not in the right date format.');
80  return new DateTime($ldapValue, new DateTimeZone('UTC'));
81  }
82  }
83 
84  protected function dateToLdap($dateValue)
85  {
86  return $dateValue->format($this->format);
87  }
88 
89  function getDateValue()
90  {
91  $value = $this->value;
92  if (!($value instanceof DateTime)) {
93  $value = new DateTime($value, new DateTimeZone('UTC'));
94  }
95  return $value;
96  }
97 
98  function computeLdapValue ()
99  {
100  if ($this->value === "" && !$this->isRequired()) {
101  return $this->value;
102  } elseif (!($this->value instanceof DateTime)) {
103  try {
104  $this->setValue($this->getDateValue());
105  } catch (Exception $e) {
106  if (is_object($this->plugin) && $this->plugin->is_template && preg_match('/%/', $this->value)) {
107  return $this->value;
108  } else {
109  throw $e;
110  }
111  }
112  }
113  return $this->dateToLdap($this->value);
114  }
115 
116  function check ()
117  {
118  $error = parent::check();
119  if (!empty($error)) {
120  return $error;
121  } else {
122  if ($this->value instanceof DateTime) {
123  return;
124  } else {
125  try {
126  $this->getDateValue();
127  } catch (Exception $e) {
128  if (is_object($this->plugin) && $this->plugin->is_template && preg_match('/%/', $this->value)) {
129  return;
130  } else {
131  return sprintf(_('Error, incorrect date: %s'), $e->getMessage());
132  }
133  }
134  }
135  }
136  }
137 
138  function renderFormInput ()
139  {
140  $smarty = get_smarty();
141  $smarty->assign('usePrototype', 'true');
142  $id = $this->getHtmlId();
143  $display = $this->renderInputField(
144  'text', $id,
145  array(
146  'value' => '{literal}'.$this->getValue().'{/literal}',
147  'class' => 'date'
148  )
149  );
150  $display .= '{if $'.$this->getAcl().'ACL|regex_replace:"/[cdmr]/":"" == "w"}'.
151  '<script type="text/javascript">
152  {literal}
153  var datepicker = new DatePicker({ relative : \''.$id.'\', language : \'{/literal}{$lang}{literal}\', keepFieldEmpty : true, enableCloseEffect : false, enableShowEffect : false });
154  {/literal}
155  </script>
156  {/if}';
157  return $this->renderAcl($display);
158  }
159 }
160 
162 {
163  function __construct ($label, $description, $ldapName, $required, $defaultValue = 'now', $acl = "")
164  {
165  parent::__construct($label, $description, $ldapName, $required, '', $defaultValue, $acl);
166  }
167 
168  protected function ldapToDate($ldapValue)
169  {
170  try {
171  return LdapGeneralizedTime::fromString($ldapValue);
173  trigger_error('LDAP value "'.$ldapValue.'" for '.$this->getLdapName().' is not in the right date format.');
174  return new DateTime($ldapValue, timezone::utc());
175  }
176  }
177 
178  protected function dateToLdap($dateValue)
179  {
180  return LdapGeneralizedTime::toString($dateValue);
181  }
182 }
renderAcl($display)
Add ACL information around display.
This class allow to handle easily any kind of LDAP attribute.
__construct($label, $description, $ldapName, $required, $format, $defaultValue= 'now', $acl="")
The constructor of DateAttribute.
& get_smarty()
Get global smarty object.
Definition: functions.inc:934
Exception class which can be thrown by LdapGeneralizedTime if the format does not match...
static fromString($string, $useException=TRUE)
Convert from LDAP GeneralizedTime formatted string to DateTime object.
This class allow to handle easily an Date LDAP attribute.
static toString($date, $setToUTC=TRUE)
Convert from DateTime object to LDAP GeneralizedTime formatted string.