![]() |
Xataface Calendar Module 0.1
Full Calendar for Xataface
|
00001 <?php 00002 class modules_calendar { 00003 00004 00008 private $baseURL=null; 00009 00010 private $ontologies = array(); 00011 00012 00013 public function __construct(){ 00014 import('Dataface/Ontology.php'); 00015 00016 Dataface_Ontology::registerType('CalendarEvent', dirname(__FILE__).'/ontologies/CalendarEvent.php', 'Dataface_Ontology_CalendarEvent'); 00017 00018 // Now work on our dependencies 00019 $mt = Dataface_ModuleTool::getInstance(); 00020 00021 //$mod = $mt->loadModule('modules_calendar'); 00022 00023 // We require the XataJax module 00024 // The XataJax module activates and embeds the Javascript and CSS tools 00025 $mt->loadModule('modules_XataJax', 'modules/XataJax/XataJax.php'); 00026 00027 $app = Dataface_Application::getInstance(); 00028 $app->registerEventListener('beforeHandleRequest', array($this, 'beforeHandleRequest')); 00029 } 00030 00031 00032 00033 public function loadOntology($table){ 00034 if ( !isset($this->ontologies[$table]) ){ 00035 00036 $this->ontologies[$table] = Dataface_Ontology::newOntology('CalendarEvent', $table); 00037 } 00038 return $this->ontologies[$table]; 00039 } 00040 00041 public function beforeHandleRequest(){ 00042 00043 $app = Dataface_Application::getInstance(); 00044 $query =& $app->getQuery(); 00045 00046 if ( !intval(@$query['--calendar-new-default-all-day']) and @$query['--calendar-new-default-start-date'] ){ 00047 // We have passed the new default start date (intended usually for the 00048 // new record form. 00049 // We need to translate this into the correct value for 00050 // the start date field 00051 $ontology = $this->loadOntology($query['-table']); 00052 00053 $dateAtt = $ontology->getFieldname('start'); 00054 if ( !PEAR::isError($dateAtt) ){ 00055 00056 00057 $_GET[$dateAtt] = $query[$dateAtt] = date('Y-m-d H:i:s', floor(floatval($query['--calendar-new-default-start-date']))); 00058 } 00059 00060 00061 } 00062 00063 if ( @$query['--calendar-new-default-all-day'] ){ 00064 // We have passed the new default start date (intended usually for the 00065 // new record form. 00066 // We need to translate this into the correct value for 00067 // the start date field 00068 00069 $ontology = $this->loadOntology($query['-table']); 00070 $allDayAtt = $ontology->getFieldname('allday'); 00071 if ( !PEAR::isError($allDayAtt) ){ 00072 00073 $_GET[$allDayAtt] = $query[$allDayAtt] = intval($query['--calendar-new-default-all-day']); 00074 } 00075 00076 00077 } 00078 00079 //print_r($query); 00080 } 00081 00082 00083 00084 00092 public function getBaseURL(){ 00093 if ( !isset($this->baseURL) ){ 00094 $this->baseURL = Dataface_ModuleTool::getInstance()->getModuleURL(__FILE__); 00095 } 00096 return $this->baseURL; 00097 } 00098 00099 00100 00101 00102 00114 public function getRepeatSettingsField($tablename){ 00115 $fields = Dataface_Table::loadTable($tablename)->transientFields(true); 00116 foreach ($fields as $name=>$def){ 00117 if ( $def['widget']['type'] == 'calendar_repeat_options' ){ 00118 return $name; 00119 } 00120 } 00121 return null; 00122 00123 } 00124 00125 00126 00127 00128 00135 function beforeSave($params){ 00136 $record = $params[0]; 00137 if ( $record ){ 00138 00139 foreach ($record->transientFields(true) as $fld){ 00140 00141 // Go through all fields in the table to see if any of them 00142 // are tagger widgets.... we collect them all and record 00143 if ( @$fld['widget']['type'] == 'calendar_repeat_options' and $record->valueChanged($fld['name']) ){ 00144 00145 $ontology = $this->loadOntology($fld['tablename']); 00146 $repeatAtt = $ontology->getFieldname('repeat'); 00147 if ( PEAR::isError($repeatAtt) ){ 00148 error_log('Attempt to use calendar_repeat_options widget on table with no repeat field identified. Please mark one of your fields with event.repeat=1 in the fields.ini file.'); 00149 throw new Exception("Failed to update the repeat settings for event because the table has no marked repeat id field."); 00150 00151 } 00152 00153 $repeatId = $record->val($repeatAtt); 00154 $snapshot = $record->getSnapshot(); 00155 00156 $changedFields = array(); 00157 if ( $repeatId ){ 00158 foreach ($record->table()->fields() as $f){ 00159 if ( $record->valueChanged($f['name']) ){ 00160 $changedFields[] = $f['name']; 00161 } 00162 } 00163 } 00164 00165 00166 00167 00168 // Only mark the field for handling if the widget:type=tagger 00169 // and a relationship is specified and the value has changed. 00170 if ( !@$record->pouch['calendar_repeat_options__fields'] ){ 00171 $record->pouch['calendar_repeat_options__fields'] = array(); 00172 } 00173 00174 // We store the fields in the record pouch so that we can access them' 00175 // in the afterSave handler 00176 $record->pouch['calendar_repeat_options__fields'][] = array( 00177 'name'=>$fld['name'], 00178 'snapshot'=> $snapshot[$fld['name']], 00179 'repeatAtt'=>$repeatAtt, 00180 'changedFields' => $changedFields 00181 00182 ); 00183 00184 break; 00185 } 00186 } 00187 } 00188 } 00189 00190 00191 public function parseRepeatField($val){ 00192 parse_str($val, $out); 00193 return $out; 00194 } 00195 00196 public function encodeRepeatField($val){ 00197 return http_build_query($val); 00198 } 00199 00200 00209 function afterSave($params){ 00210 $record = $params[0]; 00211 $io = $params[1]; 00212 00213 // The tagger__fields array was populated in the beforeSave handler 00214 // with the fields that have changed - and use the tagger widget. 00215 if ( @$record->pouch['calendar_repeat_options__fields'] ){ 00216 foreach ($record->pouch['calendar_repeat_options__fields'] as $struct){ 00217 $f = $struct['name']; 00218 $oldval = $this->parseRepeatField($struct['snapshot']); 00219 $repeatAtt = $struct['repeatAtt']; 00220 $changedFields = $struct['changedFields']; 00221 00222 $repeatId = $record->val($repeatAtt); 00223 00224 $ontology = $this->loadOntology(); 00225 $startAtt = $ontology->getFieldname('start'); 00226 $endAtt = $ontology->getFieldname('end'); 00227 00228 00229 $tfield =& $record->_table->getField($f); 00230 00231 $val = $this->parseRepeatField($record->val($f)); 00232 00233 // The format of this field should be able to reflect: 00234 // 1. Whether to make changes to all future items 00235 // 2. The frequency of the repeat 00236 //list($future, $freq) = explode(' ', $val); 00237 $future = intval($val['future']); 00238 $freq = $val['freq']; 00239 //list($oFuture, $oFreq) = explode(' ', $oldval); 00240 $oFuture = intval($oldval['future']); 00241 $oFreq = $oldval['freq']; 00242 00243 if ( $future ){ 00244 if ( $repeatId ){ 00245 // This was already a repeat 00246 // 00247 if ( $oFreq != $freq ){ 00248 // We are changing the frequency of the repeat 00249 $this->updateRepeatFrequency($record, $freq); 00250 } else if ( $changedFields ){ 00251 // The record has changed fields 00252 $changes = $record->vals($changedFields); 00253 00254 00255 $startTime = $record->val($startAtt); 00256 $endTime = $record->val($endAtt); 00257 00258 if ( !isset($changes[$startAtt]) ) $startTime = null; 00259 if ( !isset($changes[$endAtt]) ) $endTime = null; 00260 00261 unset($changes[$startAtt]); 00262 unset($changes[$endAtt]); 00263 00264 00265 00266 $this->updateRepeatValues($record, $changes, $startTime, $endTime, $val['expires']); 00267 00268 00269 } 00270 00271 } else { 00272 00273 $autofield = $record->table()->getAutoIncrementField(); 00274 if ( !$autofield ){ 00275 throw new Exception("Cannot process repeat events in table because it has no autoincrement fields defined."); 00276 00277 } 00278 $this->initRepeat($record, $freq, $record->val($autofield), $val['expires'] ); 00279 00280 00281 00282 00283 } 00284 } else { 00285 // User opted not to make changes to all future items. 00286 } 00287 00288 00289 00290 } 00291 00292 unset($record->pouch['calendar_repeat_options__fields']); 00293 } 00294 } 00295 00296 00297 function newRepeat($sourceObject){ 00298 import(dirname(__FILE__).'/classes/RepeatEvent.class.php'); 00299 return new modules_calendar_RepeatEvent($sourceObject); 00300 } 00301 00302 public static function dropRepeatTable(){ 00303 import(dirname(__FILE__).'/classes/RepeatEvent.class.php'); 00304 modules_calendar_RepeatEvent::dropRepeatTable(); 00305 } 00306 }