Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
calendar_event_edit_fns.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
18 require_once SQ_FUDGE_PATH.'/datetime_field/datetime_field.inc';
19 
32 {
33 
34 
40  {
41  $this->Asset_Edit_Fns();
42  $this->static_screens['details']['force_unlock'] = FALSE;
43 
44  }//end constructor
45 
46 
57  function paintStartEndChooser(&$asset, $prefix, $attributes=Array())
58  {
59  $occurrence_end_ts = (isset($_REQUEST['SQ_CALENDAR_DATE']) && (!$asset->getEndDate())) ? strtotime($_REQUEST['SQ_CALENDAR_DATE'].' '.($asset->getEndTime() ? $asset->getEndTime() : '12:00:00 GMT')) : FALSE;
60  $end_disabled_code = (($asset->getEndTime() || $asset->getEndDate()) || (isset($_REQUEST['SQ_CALENDAR_DURATION']))) ? '' : 'disabled="disabled"';
61  $end_disabled_array = $end_disabled_code ? Array('disabled'=>'disabled') : Array();
62  $duration = '';
63  $duration_units = 'i';
64  if ($d = ($asset->getDuration() / 60)) $duration = $d;
65  if (isset($_REQUEST['SQ_CALENDAR_DURATION'])) {
66  list($duration, $duration_units) = explode('_', $_REQUEST['SQ_CALENDAR_DURATION']);
67  }
68  if ($duration > 60) {
69  $duration = $duration / 60;
70  $duration_units = 'h';
71  if ($duration > 24) {
72  $duration = $duration / 24;
73  $duration_units = 'd';
74  }
75  }
76 
77  ?>
78  <script type="text/javascript" src="<?php echo sq_web_path('data').'/asset_types/calendar_event/js/date_chooser.js'?>"></script>
79 
80  <!-- start -->
81  <table border="0" cellpadding="0" cellspacing="0" style="margin-bottom: 1ex">
82  <tr>
83  <td class="sq-backend-data" style="vertical-align: bottom; white-space: nowrap">&nbsp;<?php echo translate('cal_event_occurs_on') ?>&nbsp;
84  <?php $this->printDateChooser($prefix.'_start', (isset($_REQUEST['SQ_CALENDAR_DATE']) && !$asset->id) ? strtotime($_REQUEST['SQ_CALENDAR_DATE']) : ($asset->getStartDate() ? iso8601_ts($asset->attr('start_date')) : 0), $attributes+Array('onkeypress'=>'processKeyEvent(this)', 'onfocus'=>'currentField=this', 'onblur'=>'currentField=null; oldField=this; setTimeout(\'processStartDateBlur(oldField, \\\''.$prefix.'\\\')\', 10);')); ?>
85  </td>
86  <td class="sq-backend-data" style="white-space: nowrap">
87  &nbsp;&nbsp;<?php $this->printTimeChooser($prefix.'_start', isset($_REQUEST['SQ_CALENDAR_TIME']) ? strtotime($_REQUEST['SQ_CALENDAR_TIME']) : ($asset->getStartTime() ? iso8601_ts($asset->attr('start_date')) : FALSE), $attributes+Array('onkeypress'=>'processKeyEvent(this)', 'onfocus'=>'currentField=this', 'onblur'=>'currentField=null; oldField=this; setTimeout(\'processStartDateBlur(oldField, \\\''.$prefix.'\\\')\', 10);'), Array('onclick'=>'if (isChecked(\''.$prefix.'_end_enabled\')) { if (this.checked) enableTimeField(\''.$prefix.'_end\'); else disableTimeField(\''.$prefix.'_end\'); } else { if (this.checked) { checkBox(\''.$prefix.'_end_time_enabled\'); } else { uncheckBox(\''.$prefix.'_end_time_enabled\'); } } ')); ?>
88  </td>
89  </tr>
90  </table>
91 
92  <!-- end -->
93  <table border="0" cellpadding="0" cellspacing="0">
94  <tr>
95  <td class="sq-backend-data" style="white-space: nowrap">
96  <?php $checked = $asset->getEndDate() || isset($_REQUEST['SQ_CALENDAR_DURATION']); ?>
97  <input name="<?php echo $prefix ?>_end_enabled" id="<?php echo $prefix ?>_end_enabled" type="checkbox" <?php if ($checked) echo ' checked="checked" '; ?> onclick="processEndClick(this, '<?php echo $prefix; ?>');"/>&nbsp;runs&nbsp;
98  </td>
99  <!-- duration -->
100  <td class="sq-backend-data" style="white-space: nowrap">
101  <input type="radio" value="duration" id="<?php echo $prefix; ?>_duration_enabled" name="<?php echo $prefix; ?>_end_type" <?php if (!$asset->id) echo 'checked="checked"'; ?> <?php echo $end_disabled_code; ?> /> <?php echo strtolower(translate('for')); ?>
102  </td>
103  <td colspan="2" class="sq-backend-data" style="white-space: nowrap">
104  <?php text_box($prefix.'_duration', $duration, 2, 0, 0, 'onclick="checkBox(\''.$prefix.'_duration_enabled\'); checkBox(\''.$prefix.'_start_time_enabled\'); enableTimeField(\''.$prefix.'_start\');" onblur="updateDurationValuesByPrefix(\''.$prefix.'\')" '.$end_disabled_code);
105  combo_box($prefix.'_duration_units', Array('i' => translate('minutes'), 'h' => translate('hours'), 'd' => translate('days')), FALSE, $duration_units, 0, 'onclick="checkBox(\''.$prefix.'_duration_enabled\'); checkBox(\''.$prefix.'_start_time_enabled\'); enableTimeField(\''.$prefix.'_start\');" onblur="updateDurationValuesByPrefix(\''.$prefix.'\')" '.$end_disabled_code); ?>
106  </td>
107  </tr>
108  <tr>
109  <td>&nbsp;</td>
110  <!-- end date -->
111  <td class="sq-backend-data" style="white-space: nowrap">
112  <input type="radio" <?php if ($asset->id) echo 'checked="checked"'; ?> value="date" id="<?php echo $prefix; ?>_end_date_enabled" name="<?php echo $prefix; ?>_end_type" <?php echo $end_disabled_code; ?> /> <?php echo strtolower(translate('until')); ?> &nbsp;
113  </td>
114  <td class="sq-backend-data" style="white-space: nowrap">
115  <?php $this->printDateChooser($prefix.'_end', $occurrence_end_ts ? $occurrence_end_ts : ($asset->getEndDate() ? iso8601_ts($asset->attr('end_date')) : FALSE), $attributes+Array('onclick'=>'checkBox(\''.$prefix.'_end_date_enabled\');', 'onkeypress'=>'processKeyEvent(this)', 'onfocus'=>'currentField=this', 'onblur'=>'currentField=null; oldField=this; setTimeout(\'processEndDateBlur(oldField, \\\''.$prefix.'\\\')\', 10);')+$end_disabled_array); ?>
116  </td>
117  <td class="sq-backend-data" style="white-space: nowrap">
118  &nbsp;&nbsp;<?php $this->printTimeChooser($prefix.'_end', ($asset->getEndTime() ? iso8601_ts($asset->attr('end_date')) : ($asset->getStartTime() ? 0 : FALSE)), $attributes+Array('onclick'=>'checkBox(\''.$prefix.'_end_date_enabled\');', 'onkeypress'=>'processKeyEvent(this)', 'onfocus'=>'currentField=this', 'onblur'=>'currentField=null; oldField=this; setTimeout(\'processEndDateBlur(oldField, \\\''.$prefix.'\\\')\', 10);')+$end_disabled_array, Array('onclick'=>'if (this.checked) { checkBox(\''.$prefix.'_end_date_enabled\'); enableTimeField(\''.$prefix.'_start\'); } else disableTimeField(\''.$prefix.'_start\');')+$end_disabled_array); ?>
119  </td>
120  </tr>
121  </table>
122 
123  <?php
124  hidden_field($prefix.'_start_end_chooser_printed', 1);
125  return TRUE;
126 
127  }//end paintStartEndChooser()
128 
129 
139  function _validateDateTimeFields($prefix, $component)
140  {
141  if (isset($_REQUEST[$prefix.$component.'_time_enabled'])) {
142  // interpret AM/PM values and covert to 24hr time
143  if ($_REQUEST[$prefix.$component.'_is_pm']) {
144  if ($_REQUEST[$prefix.$component.'_hours'] != 12) {
145  $_REQUEST[$prefix.$component.'_hours'] += 12;
146  }
147  } else {
148  if ($_REQUEST[$prefix.$component.'_hours'] == 12) {
149  $_REQUEST[$prefix.$component.'_hours'] = 0;
150  }
151  }
152  $_REQUEST[$prefix.$component.'_hours'] = (int)$_REQUEST[$prefix.$component.'_hours'];
153  if (($_REQUEST[$prefix.$component.'_hours'] < 0) || ($_REQUEST[$prefix.$component.'_hours'] > 23)) {
154  trigger_localised_error('CAL0001', E_USER_WARNING, $_REQUEST[$prefix.$component.'_hours']);
155  return FALSE;
156  }
157  $_REQUEST[$prefix.$component.'_minutes'] = (int)$_REQUEST[$prefix.$component.'_minutes'];
158  if (($_REQUEST[$prefix.$component.'_minutes'] < 0) || ($_REQUEST[$prefix.$component.'_minutes'] > 59)) {
159  trigger_localised_error('CAL0002', E_USER_WARNING, $_REQUEST[$prefix.$component.'_minutes']);
160  return FALSE;
161  }
162  }
163  if (isset($_REQUEST[$prefix.$component.'_year'])) {
164  $_REQUEST[$prefix.$component.'_year'] = (int)$_REQUEST[$prefix.$component.'_year'];
165  if (($_REQUEST[$prefix.$component.'_year'] < 1900) || ($_REQUEST[$prefix.$component.'_year'] > 2100)) {
166  trigger_localised_error('CAL0003', E_USER_WARNING, $_REQUEST[$prefix.$component.'_year']);
167  return FALSE;
168  }
169  $_REQUEST[$prefix.$component.'_month'] = (int)$_REQUEST[$prefix.$component.'_month'];
170  if (($_REQUEST[$prefix.$component.'_month'] < 1) || ($_REQUEST[$prefix.$component.'_month'] > 12)) {
171  trigger_localised_error('CAL0004', E_USER_WARNING, $_REQUEST[$prefix.$component.'_month']);
172  return FALSE;
173  }
174  $_REQUEST[$prefix.$component.'_day'] = (int)$_REQUEST[$prefix.$component.'_day'];
175  if (($_REQUEST[$prefix.$component.'_day'] < 1) || ($_REQUEST[$prefix.$component.'_day'] > 31)) {
176  trigger_localised_error('CAL0005', E_USER_WARNING, $_REQUEST[$prefix.$component.'_day']);
177  return FALSE;
178  }
179  }
180  return TRUE;
181 
182  }//end _validateDateTimeFields()
183 
184 
197  function processStartEndChooser(&$asset, &$o, $prefix)
198  {
199  if (isset($_REQUEST[$prefix.'_start_end_chooser_printed'])) {
200  $values = $this->getStartEndChooserResult($prefix);
201  if (FALSE === $values) return FALSE;
202  $res = TRUE;
203  $res &= $asset->setAttrValue('start_date', $values['start']);
204  $res &= $asset->setAttrValue('end_date', $values['end']);
205  } else {
206  $res = FALSE;
207  }
208 
209  return $res;
210 
211  }//end processStartEndChooser()
212 
213 
224  function getStartEndChooserResult($prefix)
225  {
226  // begin with the start time
227  if (!$this->_validateDateTimeFields($prefix, '_start')) {
228  return FALSE;
229  }
230  $start_date_value = $_REQUEST[$prefix.'_start_year'].'-'.sprintf("%02d", $_REQUEST[$prefix.'_start_month']).'-'.sprintf("%02d", $_REQUEST[$prefix.'_start_day']).' ';
231  if (isset($_REQUEST[$prefix.'_start_time_enabled'])) {
232  $start_date_value .= sprintf("%02d", $_REQUEST[$prefix.'_start_hours']).':'
233  .sprintf("%02d", $_REQUEST[$prefix.'_start_minutes']).':00';
234  } else {
235  $start_date_value .= '--:--:--';
236  }
237 
238  // the end is more complex...
239  if (isset($_REQUEST[$prefix.'_end_enabled'])) {
240  if (!isset($_REQUEST[$prefix.'_end_type'])) {
241  trigger_localised_error('CAL0006', E_USER_WARNING);
242  return FALSE;
243  }
244  if ($_REQUEST[$prefix.'_end_type'] == 'duration') {
245  if (!isset($_REQUEST[$prefix.'_duration'])) {
246  trigger_localised_error('CAL0007', E_USER_WARNING);
247  return FALSE;
248  }
249  $start_ts = iso8601_ts($start_date_value);
250  switch ($_REQUEST[$prefix.'_duration_units']) {
251  case 'i':
252  $end_ts = $start_ts + ((int)$_REQUEST[$prefix.'_duration'] * 60);
253  break;
254  case 'h':
255  $end_ts = $start_ts + ((int)$_REQUEST[$prefix.'_duration'] * 60 * 60);
256  break;
257  case 'd':
258  if (isset($_REQUEST[$prefix.'_start_time_enabled'])) {
259  $end_ts = $start_ts + ((int)$_REQUEST[$prefix.'_duration'] * 60 * 60 * 24);
260  } else {
261  // if no start time, the duration is the number of days covered
262  $end_ts = $start_ts + (((int)$_REQUEST[$prefix.'_duration'] - 1) * 60 * 60 * 24);
263  }
264  break;
265  }
266  $end_date_value = ts_iso8601($end_ts);
267  if (!isset($_REQUEST[$prefix.'_start_time_enabled'])) {
268  $end_date_value = substr($end_date_value, 0, 10).' --:--:--';
269  }
270  } else {
271  if (!$this->_validateDateTimeFields($prefix, '_end')) {
272  return FALSE;
273  }
274  if (isset($_REQUEST[$prefix.'_end_time_enabled']) && isset($_REQUEST[$prefix.'_start_time_enabled'])) {
275  $end_date_value = $_REQUEST[$prefix.'_end_year'].'-'
276  .sprintf("%02d", $_REQUEST[$prefix.'_end_month']).'-'
277  .sprintf("%02d", $_REQUEST[$prefix.'_end_day']).' '
278  .sprintf("%02d", $_REQUEST[$prefix.'_end_hours']).':'
279  .sprintf("%02d", $_REQUEST[$prefix.'_end_minutes']).':00';
280  } else {
281  $end_date_value = $_REQUEST[$prefix.'_end_year'].'-'
282  .sprintf("%02d", $_REQUEST[$prefix.'_end_month']).'-'
283  .sprintf("%02d", $_REQUEST[$prefix.'_end_day']).' '
284  .'--:--:--';
285  }
286  }
287  } else {
288  $end_date_value = '---------- --:--:--';
289  }
290  return Array('start' => $start_date_value, 'end' => $end_date_value);
291 
292  }//end getStartEndChooserResult()
293 
294 
305  function printDateChooser($prefix, $timestamp=0, $attributes=Array())
306  {
307  $select_attributes = $attributes;
308  unset($select_attributes['onkeypress']);
309  $attrs_code = '';
310  foreach ($attributes as $name => $value) {
311  $attrs_code .= ' '.$name.'="'.$value.'"';
312  }
313  if ($timestamp === FALSE) {
314  if(!isset($attributes['disabled'])){
315  $attrs_code .= ' disabled="disabled"';
316  }
317  }
318  if (!$timestamp) $timestamp = time();
319  ?>
320  <input class="sq-form-field" style="width: 5ex" type="text" name="<?php echo $prefix; ?>_day" id="<?php echo $prefix; ?>_day" size="2" value="<?php echo date('j', $timestamp); ?>" <?php echo $attrs_code ?> />
321 
322  <select class="sq-form-field" name="<?php echo $prefix; ?>_month" id="<?php echo $prefix; ?>_month" <?php echo $attrs_code; ?>>
323  <?php
324  $currentMonth = date('n', $timestamp);
325  foreach (range(1, 12) as $monthNumber) {
326  echo ' <option '.(($monthNumber == $currentMonth) ? ' selected="selected" ' : '').'value="'.$monthNumber.'">'.DateTime_Field::shortMonth($monthNumber)."</option>\n";
327  }
328  ?>
329  </select>
330 
331  <input class="sq-form-field" type="text" name="<?php echo $prefix; ?>_year" id="<?php echo $prefix; ?>_year" size="5" value="<?php echo date('Y', $timestamp); ?>" <?php echo $attrs_code; ?> />
332  <?php
333  return TRUE;
334 
335  }//end printDateChooser()
336 
337 
349  function printTimeChooser($prefix, $timestamp, $attributes=Array(), $checkbox_attributes=Array())
350  {
351  $attrs_code = '';
352  foreach ($attributes as $name => $value) {
353  $attrs_code .= ' '.$name.'="'.$value.'"';
354  }
355  if ($timestamp === FALSE) {
356  if(!isset($attributes['disabled'])){
357  $attrs_code .= ' disabled="disabled"';
358  }
359  }
360  $checked_text = ($timestamp !== FALSE) ? 'checked="checked"' : '';
361  if (!$timestamp) $timestamp = time();
362 
363  if (!isset($checkbox_attributes['onclick'])) {
364  $checkbox_attributes['onclick'] = '';
365  }
366  $checkbox_attributes['onclick'] = 'if (this.checked) { enableTimeField(\''.$prefix.'\'); } else { disableTimeField(\''.$prefix.'\'); } '.$checkbox_attributes['onclick'];
367  $checkbox_attrs_code = '';
368  foreach ($checkbox_attributes as $name => $value) {
369  $checkbox_attrs_code .= ' '.$name.'="'.$value.'"';
370  }
371  ?>
372  <input class="sq-form-field" type="checkbox" <?php echo $checked_text ?> value="1" name="<?php echo $prefix; ?>_time_enabled" id="<?php echo $prefix; ?>_time_enabled" <?php echo $checkbox_attrs_code; ?> />at
373 
374  <input class="sq-form-field" style="width: 5ex" type="text" name="<?php echo $prefix; ?>_hours" id="<?php echo $prefix; ?>_hours" size="2" maxlength="2" value="<?php echo date('g', $timestamp); ?>" <?php echo $attrs_code; ?> /> :
375 
376  <input class="sq-form-field" style="width: 5ex" type="text" name="<?php echo $prefix; ?>_minutes" id="<?php echo $prefix; ?>_minutes" size="2" maxlength="2" value="<?php echo date('i', $timestamp); ?>" <?php echo $attrs_code; ?> />
377 
378  <select class="sq-form-field" name="<?php echo $prefix; ?>_is_pm" id="<?php echo $prefix; ?>_is_pm" <?php echo $attrs_code; ?> >
379  <option value="0" <?php echo (date('a', $timestamp)=='am') ? ' selected="selected" ' : ''; ?>>am</option>
380  <option value="1" <?php echo (date('a', $timestamp)=='pm') ? ' selected="selected" ' : ''; ?>>pm</option>
381  </select>
382  <?php
383  return TRUE;
384 
385  }//end printTimeChooser()
386 
387 
388 }//end class
389 
390 ?>