Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
calendar_event_modification.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 
32 {
33 
34 
41  function Calendar_Event_Modification($assetid=0)
42  {
43  $this->Calendar_Event($assetid);
44 
45  }//end constructor
46 
47 
58  function _preCreateCheck(&$link)
59  {
60  $parent =& $link['asset'];
61  if ($parent->type() != 'calendar_event_recurring') {
62  trigger_localised_error('CAL0052', E_USER_ERROR);
63  return FALSE;
64  }
65 
66  if (!$this->synchronizeWithParent($parent)) {
67  return FALSE;
68  }
69 
70  if (!parent::_preCreateCheck($link)) return FALSE;
71 
72  if (!($link['value'] = $this->getStartDate())) {
73  trigger_localised_error('CAL0049', E_USER_ERROR);
74  return FALSE;
75  }
76 
77  return TRUE;
78 
79  }//end _preCreateCheck()
80 
81 
92  protected function _getName($short_name=FALSE, $contextid=NULL)
93  {
94  // No context specified, using the current context
95  if ($contextid === NULL) {
96  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
97  }//end if
98 
99  if ($short_name) {
100  return translate('cal_event_modified_date', date('d M Y', iso8601_ts($this->attr('start_date'))));
101  }//end if
102 
103  // Obtain the attribute value for Name from the specified Context
104  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
105  if (empty($values) === TRUE) {
106  return parent::_getName($short_name, $contextid);
107  } else {
108  return $values[$this->id];
109  }
110 
111  }//end _getName()
112 
113 
123  function synchronizeWithParent(&$parent)
124  {
125  // check that it has an occurrence on the date specified
126  if (!$parent->hasOccurrenceOnDate($this->getStartDate())) {
127  trigger_localised_error('CAL0051', E_USER_WARNING, $this->getStartDate());
128  return FALSE;
129  }
130 
131  // update our start time, end date and end time to correspond with the parent
132  $sd = $this->getStartDate();
133  $st = $parent->getStartTime();
134  if ($st) {
135  $this->setAttrValue('start_date', $sd.' '.$st.':00');
136  }
137  if ($duration = $parent->getDuration()) {
138  $this->setAttrValue('end_date', date('Y-m-d', iso8601_ts($this->attr('start_date')) + $duration).substr($parent->attr('end_date'), 10));
139  }
140 
141  // set our name based on the parent
142  if (!$this->attr('name')) {
143  $this->setAttrValue('name', $parent->attr('name').' - Modified '.date('d M Y', iso8601_ts($this->attr('start_date'))));
144  }
145 
146  return TRUE;
147 
148  }//end synchronizeWithParent()
149 
150 
161  function saveAttributes($dont_run_updated=FALSE)
162  {
163  if ($this->id) {
164 
165  if (!$dont_run_updated) {
166 
167  // get the parent recurring event asset
168  $recurring_event_ids = array_keys($GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'calendar_event_recurring', FALSE));
169  if (empty($recurring_event_ids)) {
170  trigger_localised_error('CAL0052', E_USER_ERROR);
171  return FALSE;
172  }
173  $recurring_event =& $GLOBALS['SQ_SYSTEM']->am->getAsset($recurring_event_ids[0]);
174 
175  if (!$this->synchronizeWithParent($recurring_event)) {
176  return FALSE;
177  }
178  }
179  }
180  return parent::saveAttributes($dont_run_updated);
181 
182  }//end saveAttributes()
183 
184 
191  function printBody()
192  {
193  $root_url = array_values($GLOBALS['SQ_SYSTEM']->am->getRootURL());
194  if (is_array($root_url)) $root_url = $root_url[0];
195  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
196  ?>
197  <table border="0" cellspacing="2" cellpadding="2">
198  <tr>
199  <td><b><?php echo translate('cal_event_modification_of'); ?></b></td>
200  <td><?php
201  // get the parent recurring event asset
202  if ($recurring_events = $GLOBALS['SQ_SYSTEM']->am->getParents($this->id, 'calendar_event_recurring', FALSE)) {
203  $recurring_events = array_keys($recurring_events);
204  $recurring_event =& $GLOBALS['SQ_SYSTEM']->am->getAsset($recurring_events[0]);
205  echo '<a href="'.$root_url.'?a='.$recurring_events[0].'">'.$recurring_event->attr('name').'</a>';
206  }
207  ?></td>
208  </tr>
209  <tr>
210  <td><b><?php echo translate('cal_event_modified_start_date'); ?></b></td>
211  <td><?php echo $this->getFriendlyStart(); ?></td>
212  </tr>
213  <tr>
214  <td><b><?php echo translate('cal_event_modified_end_date'); ?></b></td>
215  <td><?php echo $this->getFriendlyEnd(); ?></td>
216  </tr>
217  <tr>
218  <td><b><?php echo translate('cal_event_modified_name'); ?></b></td>
219  <td><?php echo $this->attr('name'); ?></td>
220  </tr>
221  <tr>
222  <td><b><?php echo translate('cal_event_modified_description'); ?></b></td>
223  <td><?php echo $this->attr('description'); ?></td>
224  </tr>
225  </table>
226  <?php
227 
228  }//end printBody()
229 
230 
243  function getInstanceDates($date)
244  {
245  $res = Array();
246  $res['start'] = $date;
247 
248  if ($this->getStartTime()) {
249  $res['start'] .= ' '.$this->getStartTime();
250  }
251 
252  $res['end'] = date('Y-m-d', iso8601_ts($res['start']) + $this->getDuration());
253 
254  if ($this->getEndTime()) {
255  $res['end'] .= ' '.$this->getEndTime();
256  }
257 
258  return $res;
259 
260  }//end getInstanceDates()
261 
262 
277  function getKeywordReplacement($keyword)
278  {
279  $keyword = parse_keyword($keyword, $modifiers);
280 
281  if (in_array($keyword, Array('instance_start_datetime', 'instance_end_datetime'))) {
282  if (isset($_REQUEST['SQ_CALENDAR_DATE'])) {
283  $instance_dates = $this->getInstanceDates($_REQUEST['SQ_CALENDAR_DATE']);
284  switch ($keyword) {
285  case 'instance_start_datetime':
286  $replacement = $instance_dates['start'];
287  case 'instance_end_datetime':
288  $replacement = $instance_dates['end'];
289  }
290  }
291  }
292  $matches = Array();
293  preg_match('/^instance_start_datetime_(.+)/', $keyword, $matches);
294  if ((!empty($matches)) && !empty($_REQUEST['SQ_CALENDAR_DATE'])) {
295  $instance_dates = $this->getInstanceDates($_REQUEST['SQ_CALENDAR_DATE']);
296  $replacement = date($matches[1], iso8601_ts($instance_dates['start']));
297  }
298  $matches = Array();
299  preg_match('/^instance_end_datetime_(.+)/', $keyword, $matches);
300  if ((!empty($matches)) && !empty($_REQUEST['SQ_CALENDAR_DATE'])) {
301  $instance_dates = $this->getInstanceDates($_REQUEST['SQ_CALENDAR_DATE']);
302  $replacement = date($matches[1], iso8601_ts($instance_dates['end']));
303  }
304 
305  if (empty($replacement)) {
306  $replacement = parent::getKeywordReplacement($keyword);
307  }
308  apply_keyword_modifiers($replacement, $modifiers, Array('assetid' => $this->id));
309 
310  return $replacement;
311 
312  }//end getKeywordReplacement()
313 
314 
330  {
331  $res = parent::getAvailableKeywords();
332 
333  $our_keywords[] = 'instance_end_datetime';
334  $our_keywords[] = 'instance_start_datetime';
335  $our_keywords[] = 'instance_start_datetime_';
336  $our_keywords[] = 'instance_end_datetime_';
337 
338  foreach ($our_keywords as $keyword) {
339  $res[$keyword] = translate('cal_evt_recur_keyword_'.$keyword);
340  }
341  ksort($res);
342  return $res;
343 
344  }//end getAvailableKeywords()
345 
346 
347 }//end class
348 
349 ?>