Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
calendar_event_multi_date.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 
33 {
34 
35 
42  function Calendar_Event_Multi_Date($assetid=0)
43  {
44  $this->Asset($assetid);
45 
46  }//end constructor
47 
48 
58  function _preCreateCheck(&$link)
59  {
60  if (!parent::_preCreateCheck($link)) return FALSE;
61 
62  $name = trim($this->attr('name'));
63  if ($name == '') {
64  trigger_localised_error('CORE0083', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
65  return FALSE;
66  }
67 
68  return TRUE;
69 
70  }//end _preCreateCheck()
71 
72 
81  function _createAdditional(&$link)
82  {
83  if (!parent::_createAdditional($link)) return FALSE;
84 
85  // Add web path
86  $initial_path = strtolower($this->attr('name'));
87  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
88  $valid_paths = make_valid_web_paths(Array($initial_path));
89  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($link['asset'], $valid_paths, $this->id, TRUE);
90  if (!$this->saveWebPaths($good_paths)) return FALSE;
91 
92  // Create bodycopy
93  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bodycopy');
94  $bodycopy = new Bodycopy();
95  $copy_link = Array('asset' => &$this, 'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 0);
96  $bodycopy->setAttrValue('name', 'Page Contents');
97  if (!$bodycopy->create($copy_link)) return FALSE;
98 
99  // Process pending children
100  if (!empty($this->_tmp['pending_children'])) {
101  foreach ($this->_tmp['pending_children'] as $entry) {
102  if (!$entry['asset']->create($entry['link'])) {
103  return FALSE;
104  }
105  }
106  }
107 
108  $this->name = $this->_getName();
109  $this->short_name = $this->_getName();
110 
111  return TRUE;
112 
113  }//end _createAdditional()
114 
115 
122  public function cloneDependantAdditional(Asset $clone)
123  {
124  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
125  $return_value = TRUE;
126  $value = TRUE;
127 
128  // Load the web paths for the design's components
129  $components = $GLOBALS['SQ_SYSTEM']->am->getChildren($clone->id);
130  foreach ($components as $componentid => $component) {
131  // set web path for components
132  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($componentid);
133 
134  $initial_path = strtolower($asset->name);
135  $valid_paths = make_valid_web_paths(Array($initial_path));
136  $good_paths = $GLOBALS['SQ_SYSTEM']->am->webPathsInUse($clone, $valid_paths, $asset->id, TRUE);
137  $GLOBALS['SQ_SYSTEM']->acquireLock($asset->id, 'all');
138  $return_value = $asset->saveWebPaths($good_paths);
139  $value = (!$return_value) ? FALSE : TRUE;
140 
141  $return_value = $asset->updateLookups(FALSE);
142  $value = (!$return_value) ? FALSE : TRUE;
143  $GLOBALS['SQ_SYSTEM']->releaseLock($asset->id, 'all');
144  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
145  }//end foreach
146 
147  return $value;
148 
149  }//end cloneDependantAdditional()
150 
151 
159  function _getAllowedLinks()
160  {
161  $allowed = parent::_getAllowedLinks();
162  $allowed[SQ_LINK_TYPE_1]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
163  $allowed[SQ_LINK_TYPE_2]['folder'] = Array('card' => 'M', 'exclusive' => FALSE);
164  $allowed[SQ_LINK_TYPE_3]['calendar_event'] = Array('card' => 'M', 'exclusive' => FALSE);
165  $allowed[SQ_LINK_TYPE_2]['calendar_event'] = Array('card' => 'M', 'exclusive' => FALSE);
166  $allowed[SQ_LINK_TYPE_2]['bodycopy'] = Array('card' => 1, 'exclusive' => FALSE);
167 
168  return $allowed;
169 
170  }//end _getAllowedLinks()
171 
172 
179  function printBody()
180  {
181  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
182  if ($contextid === 0) {
183  $cache_key = 'default';
184  } else {
185  $cache_key = 'default.'.$contextid;
186  }
187 
188  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
189  $cached_contents = $cm->loadFromCache($this->id, $cache_key);
190 
191  // if the cache is empty, go ahead and regenrate a new version
192  if ($cached_contents === FALSE) {
193  // get the bodycopy here to avoid caching of any possible error messages
194  $bodycopy = $this->getBodycopy();
195  ob_start();
196  // we only want to cache if we actually have a bodycopy
197  if (!is_null($bodycopy)) {
198  $bodycopy->printBody();
199  $cm->saveToCache($this->id, $this->type(), $cache_key, ob_get_contents());
200  }
201  ob_end_flush();
202  } else {
203  echo $cached_contents;
204  }
205 
206  }//end printBody()
207 
208 
216  {
217  return $this->attr('short_date');
218 
219  }//end getEventShortDateKeywordReplacement()
220 
221 
232  protected function _getName($short_name=FALSE, $contextid=NULL)
233  {
234  // No context specified, using the current context
235  if ($contextid === NULL) {
236  $contextid = $GLOBALS['SQ_SYSTEM']->getContextId();
237  }//end if
238 
239  // Obtain the attribute value for Name from the specified Context
240  $values = $GLOBALS['SQ_SYSTEM']->am->getAttributeValuesByName('name', $this->type(), Array($this->id), $contextid);
241  if (empty($values) === TRUE) {
242  return parent::_getName($short_name, $contextid);
243  } else {
244  return $values[$this->id];
245  }
246 
247  }//end _getName()
248 
249 
263  function setAttrValue($name, $value)
264  {
265  if ($name == 'name') {
266  $this->_tmp['name_updated'] = TRUE;
267  }
268  if ($name == 'short_date') {
269  $this->_tmp['short_date_updated'] = TRUE;
270  }
271  return parent::setAttrValue($name, $value);
272 
273  }//end setAttrValue()
274 
275 
282  function &getBodycopy()
283  {
284  $null = NULL;
285  $link = $GLOBALS['SQ_SYSTEM']->am->getLink($this->id, SQ_LINK_TYPE_2, 'bodycopy', TRUE, '', 'major');
286  if (empty($link)) return $null;
287  $bodycopy = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
288  return $bodycopy;
289 
290  }//end getBodycopy()
291 
292 
302  function addPendingChild(&$asset, $link)
303  {
304  if ($this->id) {
305  trigger_error('Cannot add pending child - parent is already created');
306  return;
307  }
308  if (empty($this->_tmp['pending_children'])) {
309  $this->_tmp['pending_children'] = Array();
310  }
311  $this->_tmp['pending_children'][] = Array('asset' => $asset, 'link' => $link);
312 
313  }//end addPendingChild()
314 
315 
325  function getCustomKeywordReplacements($keywords, $invoke_backend = FALSE)
326  {
327  if (!is_array($keywords) || empty($keywords)) {
328  return Array();
329  }
330  $print_commit_button = FALSE;
331 
332  $replacements = Array();
333  foreach ($keywords as $keyword) {
334  switch ($keyword) {
335  case 'contents' :
336  $bodycopy = $this->getBodycopy();
337  if (is_null($bodycopy)) continue;
338  $edit = $bodycopy->getEditFns();
339  if (!$invoke_backend) {
340  $o = $GLOBALS['SQ_SYSTEM']->backend->out ;
341  } else {
342  require_once SQ_INCLUDE_PATH.'/backend_outputter.inc';
343  $o = new Backend_Outputter();
344  }
345  ob_start();
346  if ($edit->paintBodycopy($bodycopy, $o, $bodycopy->getPrefix())) {
347  $print_commit_button = TRUE;
348  }
349  $replacements[$keyword] = ob_get_contents();
350  ob_end_clean();
351  break;
352 
353  default:
354  $replacements[$keyword] = '';
355  break;
356  }//end switch
357  }//end foreach
358 
359  return Array('replacements' => $replacements, 'print_commit_button' => $print_commit_button);
360 
361  }//end getCustomKeywordReplacements()
362 
363 
373  function processCustomKeywords($keywords)
374  {
375  if (!is_array($keywords) || empty($keywords)) {
376  return Array();
377  }
378 
379  foreach ($keywords as $keyword) {
380  switch ($keyword) {
381  case 'contents' :
382  $bodycopy = $this->getBodycopy();
383  if (is_null($bodycopy)) continue;
384  $edit = $bodycopy->getEditFns();
385  $edit->processBodycopy($bodycopy, $GLOBALS['SQ_SYSTEM']->backend->out, $bodycopy->getPrefix());
386  break;
387  }
388  }
389 
390  return TRUE;
391 
392  }//end processCustomKeywords()
393 
394 
404  function processStatusChange($new_status, $update_parents)
405  {
406  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
407 
408  //make sure our data directory exist
409  if (!create_directory($this->data_path)) {
410  trigger_localised_error('CORE0049', E_USER_WARNING, $this->name);
411  return FALSE;
412  }
413 
414  switch ($new_status) {
415  case SQ_STATUS_EDITING:
416  // Going to safe edit
417  if ($this->status == SQ_STATUS_LIVE) {
418  $child_events = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_3, Array('calendar_event_single', 'calendar_event_recurring'), FALSE);
419  file_put_contents($this->data_path.'/.safe_edit_before', serialize($child_events));
420  file_put_contents($this->data_path.'/.safe_edit_after', serialize($child_events));
421  }//end if
422  break;
423  case SQ_STATUS_LIVE:
424  if ($this->status == SQ_STATUS_EDITING) {
425  //we are cancelling safe edit
426  $before_safe_edit = Array();
427  $after_safe_edit = Array();
428  $before_safe_edit_string = file_to_string($this->data_path.'/.safe_edit_before');
429  $after_safe_edit_string = file_to_string($this->data_path.'/.safe_edit_after');
430 
431  if (!empty($before_safe_edit_string)) {
432  $before_safe_edit = @unserialize($before_safe_edit_string);
433  }//end if
434  if (!empty($after_safe_edit_string)) {
435  $after_safe_edit = @unserialize($after_safe_edit_string);
436  }//end if
437 
438  if (!empty($after_safe_edit)) {
439  foreach ($after_safe_edit as $active_link) {
440  if (!$this->_compareLink($active_link, $before_safe_edit)) {
441  // this link did not exist , move to thrash
442  if (isset($active_link['linkid'])) {
443  // Drop to a run level to allow the links to be shuffled out without a fuss
444  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
445  $this->deleteLink($active_link['linkid']);
446  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
447  }// end if
448  }// end if
449  }// end foreach
450  }// end if
451  // Coming out of safe edit remove safe edit files
452  $this->_cleanupSafeEdit();
453 
454  }// end $this->status == SQ_SAFE_EDITING
455 
456  if ($this->status == SQ_STATUS_EDITING_APPROVED) {
457  // Changes approved
458  $this->_cleanupSafeEdit();
459  }// end $this->status == SQ_STATUS_EDITING_APPROVED
460  break;
461 
462  }// end switch
463 
464  return parent::processStatusChange($new_status, $update_parents);
465 
466  }//end processStatusChange()
467 
468 
478  function _compareLink($current_link, $before_links)
479  {
480  if (!isset($current_link['minorid'])) {
481  // Not enough information to proceed
482  return FALSE;
483  }//end if
484 
485  //if before_link is empty, that means there were no dependat events before, just return true
486  if (empty($before_links)){
487  return FALSE;
488  }
489 
490  foreach ($before_links as $before_link) {
491  if (isset($before_link['minorid'])) {
492  if ($before_link['minorid'] == $current_link['minorid']) {
493  // It exists!
494  return TRUE;
495  }//end if
496  }//end if
497  }//end foreach
498 
499  // Didn't find one, report that...
500  return FALSE;
501 
502  }//end _compareLink()
503 
504 
511  function _cleanupSafeEdit()
512  {
513  if (file_exists($this->data_path.'/.safe_edit_after')) {
514  unlink($this->data_path.'/.safe_edit_after');
515  }//end if
516  if (file_exists($this->data_path.'/.safe_edit_before')) {
517  unlink($this->data_path.'/.safe_edit_before');
518  }//end if
519 
520  }//end _cleanupSafeEdit()
521 
522 
523 }//end class
524 
525 ?>