Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
calendar_event_cancellation.inc
1 <?php
17 require_once dirname(__FILE__).'/../../calendar_event/calendar_event.inc';
18 require_once dirname(__FILE__).'/../calendar_event_recurring/calendar_event_recurring.inc';
19 
33 {
34 
35 
42  function Calendar_Event_Cancellation($assetid=0)
43  {
44  $this->Calendar_Event($assetid);
45 
46  }//end constructor
47 
48 
59  function _preCreateCheck(&$link)
60  {
61  $parent =& $link['asset'];
62  if (!is_a($parent, 'calendar_event_recurring')) {
63  trigger_localised_error('CAL0054', E_USER_WARNING);
64  return FALSE;
65  }
66 
67  if (!$this->synchronizeWithParent($parent)) {
68  return FALSE;
69  }
70 
71  if (!parent::_preCreateCheck($link)) return FALSE;
72 
73  if (!($link['value'] = $this->getStartDate())) {
74  trigger_localised_error('CAL0053', E_USER_WARNING);
75  return FALSE;
76  }
77 
78  return TRUE;
79 
80  }//end _preCreateCheck()
81 
82 
93  protected function _getName($short_name=FALSE, $contextid=NULL)
94  {
95  // No context specified, using the current context
96  if ($contextid === NULL) {
97  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
98  }//end if
99 
100  if ($short_name) {
101  return translate('cal_event_cancelled_date', date('d M Y', iso8601_ts($this->attr('start_date'))));
102  }//end if
103 
104  // Obtain the attribute value for Name from the specified Context
105  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
106  if (empty($values) === TRUE) {
107  return parent::_getName($short_name, $contextid);
108  } else {
109  return $values[$this->id];
110  }
111 
112  }//end _getName()
113 
114 
121  function _getAllowedLinks()
122  {
123  $links = parent::_getAllowedLinks();
124  $links[SQ_LINK_TYPE_2]['calendar_event_single'] = Array('card' => 1, 'exclusive' => FALSE);
125  return $links;
126 
127  }//end _getAllowedLinks()
128 
129 
139  function synchronizeWithParent(&$parent)
140  {
141  // check that it has an occurrence on the date specified
142  if (!$parent->hasOccurrenceOnDate($this->getStartDate())) {
143  trigger_localised_error('CAL0055', E_USER_WARNING, $this->getStartDate());
144  return FALSE;
145  }
146 
147  // update our start time, end date and end time to correspond with the parent
148  $sd = $this->getStartDate();
149  $st = $parent->getStartTime();
150  if ($st) {
151  $this->setAttrValue('start_date', $sd.' '.$st.':00');
152  }
153  if ($duration = $parent->getDuration()) {
154  $this->setAttrValue('end_date', date('Y-m-d', iso8601_ts($this->attr('start_date')) + $duration).substr($parent->attr('end_date'), 10));
155  }
156  // set our name based on the parent
157  $this->setAttrValue('name', translate('cal_event_cancelled_date', date('d M Y', iso8601_ts($this->attr('start_date')))).': '.$parent->attr('name'));
158 
159  return TRUE;
160 
161  }//end synchronizeWithParent()
162 
163 
174  function addReplacement($start_date, $end_date, $description)
175  {
176  if (!$this->writeAccess('')) {
177  trigger_localised_error('CAL0056', E_USER_WARNING);
178  return FALSE;
179  }
180  if ($existing_link = $this->getReplacementLink()) {
181  $replacement_event = $GLOBALS['SQ_SYSTEM']->am->getAsset($existing_link['minorid']);
182  } else {
183  // create a replacement event
184  $parent_link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, NULL, '', TRUE, NULL, 'minor');
185  $parent_info = reset($GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($parent_link['majorid'])));
186  $new_type_code = str_replace('recurring', 'single', $parent_info['type_code']);
187  $GLOBALS['SQ_SYSTEM']->am->includeAsset($new_type_code);
188  $replacement_event = new $new_type_code();
189  }
190  $replacement_event->setAttrValue('start_date', $start_date);
191  $replacement_event->setAttrValue('end_date', $end_date);
192  $replacement_event->setAttrValue('description', $description);
193  if (!$replacement_event->id) {
194  $link = Array('asset' => &$this, 'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
195  $replacement_event->create($link);
196  } else {
197  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($replacement_event->id, 'attributes')) {
198  trigger_localised_error('CAL0057', E_USER_WARNING, $replacement_event->name);
199  return 0;
200  }
201  $replacement_event->saveAttributes();
202  $GLOBALS['SQ_SYSTEM']->am->releaseLock($replacement_event->id, 'attributes');
203  }
204  if ($replacement_event->id) {
205  $replacement_event->processStatusChange($this->status);
206  return $replacement_event;
207  } else {
208  return FALSE;
209  }
210 
211  }//end addReplacement()
212 
213 
227  function getReplacementLink($id=NULL)
228  {
229  if (is_null($id)) $id = $this->id;
230  $child_links = $GLOBALS['SQ_SYSTEM']->am->getLinks($id, SQ_SC_LINK_ALL, 'calendar_event_single', FALSE, 'major');
231  return current($child_links);
232 
233  }//end getReplacementLink()
234 
235 
244  function processEventAction($action_name)
245  {
246  $res = '';
247  switch ($action_name) {
248  case 'addreplacement':
249  $parent_assetids = array_keys($GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'calendar_event_recurring'));
250  $parent_object = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent_assetids[0]);
251 
252  $res = '<form method="POST" action="?SQ_CALENDAR_VIEW=event&amp;SQ_CALENDAR_EVENT_ID='.$this->id.'&amp;SQ_CALENDAR_EVENT_ACTION=addreplacement2&amp;SQ_CALENDAR_DATE='.$_REQUEST['SQ_CALENDAR_DATE'].'"><table cellpadding="4" border="0">';
253  $res .= '<tr><td style="vertical-align: top">'.translate('cal_event_start_and_end').'</td><td>';
254  ob_start();
255  $edit_fns = $this->getEditFns();
256  $edit_fns->paintStartEndChooser($this, 'event');
257  $res .= ob_get_contents();
258  ob_end_clean();
259  $res .= '</td></tr>';
260 
261  $res .= '<tr><td style="vertical-align: top">'.translate('description').': </td><td><textarea name="event_description">';
262  $res .= $parent_object->attr('description');
263  $res .= '</textarea></td></tr>';
264  $res .= '<tr><td colspan="2"><input type="submit" value="'.translate('cal_event_add_replacement').'"/>&nbsp;&nbsp;<input type="button" value="'.translate('cancel').'" onclick="document.location=\''.$_SERVER['PHP_SELF'].'?SQ_CALENDAR_EVENT_ID='.$parent_object->id.'&SQ_CALENDAR_VIEW=event&SQ_CALENDAR_DATE='.$_REQUEST['SQ_CALENDAR_DATE'].'\'" /></td></tr></table></form>';
265  break;
266 
267  case 'addreplacement2':
268  if (!isset($_REQUEST['event_start_time_enabled'])) {
269  $_REQUEST['event_start_hours'] = '--';
270  $_REQUEST['event_start_minutes'] = '--';
271  } else {
272  if ($_REQUEST['event_start_is_pm'] && ($_REQUEST['event_start_hours'] != 12)) {
273  $_REQUEST['event_start_hours'] += 12;
274  }
275  }
276  if (!isset($_REQUEST['event_end_time_enabled'])) {
277  $_REQUEST['event_end_hours'] = '--';
278  $_REQUEST['event_end_minutes'] = '--';
279  } else {
280  if ($_REQUEST['event_end_is_pm'] && ($_REQUEST['event_end_hours'] != 12)) {
281  $_REQUEST['event_end_hours'] += 12;
282  }
283  }
284  if (!isset($_REQUEST['event_end_date_enabled'])) {
285  $_REQUEST['event_end_year'] = '----';
286  $_REQUEST['event_end_month'] = '--';
287  $_REQUEST['event_end_day'] = '--';
288  }
289 
290  $replacement_event = $this->addReplacement(sprintf('%04s-%02s-%02s %02s:%02s:--', $_REQUEST['event_start_year'], $_REQUEST['event_start_month'], $_REQUEST['event_start_day'], $_REQUEST['event_start_hours'], $_REQUEST['event_start_minutes']), sprintf('%04s-%02s-%02s %02s:%02s:--', $_REQUEST['event_end_year'], $_REQUEST['event_end_month'], $_REQUEST['event_end_day'], $_REQUEST['event_end_hours'], $_REQUEST['event_end_minutes']), $_REQUEST['event_description']);
291  if ($replacement_event) {
292  $res = '<p>'.translate('cal_event_replacement_event_added').'</p>';
293  }
294  break;
295 
296  default:
297  $res = parent::processEventAction($action_name);
298  break;
299  }//end switch
300  return $res;
301 
302  }//end processEventAction()
303 
304 
315  function saveAttributes($dont_run_updated=FALSE)
316  {
317  if ($this->id) {
318 
319  if (!$dont_run_updated) {
320 
321  // get the parent recurring event asset
322  $recurring_event_ids = array_keys($GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'calendar_event_recurring', FALSE));
323  if (empty($recurring_event_ids)) {
324  trigger_localised_error('CAL0054', E_USER_ERROR);
325  return FALSE;
326  }
327  $recurring_event = $GLOBALS['SQ_SYSTEM']->am->getAsset($recurring_event_ids[0]);
328 
329  if (!$this->synchronizeWithParent($recurring_event)) {
330  return FALSE;
331  }
332  }
333  }
334  return parent::saveAttributes($dont_run_updated);
335 
336 
337  }//end saveAttributes()
338 
339 
346  function printBody()
347  {
348  $root_url = array_values($GLOBALS['SQ_SYSTEM']->am->getRootURL());
349  if (is_array($root_url)) $root_url = $root_url[0];
350  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
351  ?>
352  <table border="0" cellspacing="2" cellpadding="2">
353  <tr>
354  <td><b><?php echo translate('name') ?></b></td>
355  <td><?php echo $this->attr('name'); ?></td>
356  </tr>
357  <tr>
358  <td><b><?php echo translate('cancels') ?></b></td>
359  <td><?php
360  // get the parent recurring event asset
361  if ($recurring_events = $GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'calendar_event_recurring', FALSE)) {
362  $recurring_events = array_keys($recurring_events);
363  $recurring_event = $GLOBALS['SQ_SYSTEM']->am->getAsset($recurring_events[0]);
364  echo '<a href="'.$root_url.'?a='.$recurring_events[0].'">'.$recurring_event->attr('name').'</a>';
365  }
366  ?>
367  </td>
368  </tr>
369  <tr>
370  <td><b><?php echo translate('cal_event_cancelled_start_date'); ?></b></td>
371  <td><?php echo $this->getFriendlyStart(); ?></td>
372  </tr>
373  <tr>
374  <td><b><?php echo translate('cal_event_cancelled_end_date'); ?></b></td>
375  <td><?php echo $this->getFriendlyEnd(); ?></td>
376  </tr>
377  <tr>
378  <td><b><?php echo translate('cal_event_reason_for_cancellation'); ?></b></td>
379  <td><?php echo $this->attr('description'); ?></td>
380  </tr>
381  </table>
382  <?php
383 
384  }//end printBody()
385 
386 
387 }//end class
388 
389 ?>