![]() |
Xataface Calendar Module 0.1
Full Calendar for Xataface
|
00001 <?php 00002 class actions_calendar_event_moved { 00003 function handle($params){ 00004 00005 $app = Dataface_Application::getInstance(); 00006 00007 try { 00008 if ( !@$_POST['-calendar-event-id'] ){ 00009 throw new Exception("No calendar event id provided", 500); 00010 } 00011 00012 $record = df_get_record_by_id($_POST['-calendar-event-id']); 00013 if ( !$record ){ 00014 throw new Exception("The specified event could not be found", 404); 00015 00016 } 00017 00018 if ( !$record->checkPermission('edit') ){ 00019 throw new Exception("Failed to move the event because you don't have sufficient permissions.", 400); 00020 00021 } 00022 00023 import('Dataface/Ontology.php'); 00024 00025 Dataface_Ontology::registerType('CalendarEvent', dirname(__FILE__).'/../ontologies/CalendarEvent.php', 'Dataface_Ontology_CalendarEvent'); 00026 $ontology =& Dataface_Ontology::newOntology('CalendarEvent', $record->_table->tablename); 00027 00028 $startAtt = $ontology->getFieldname('start'); 00029 if ( PEAR::isError($startAtt) ) throw new Exception('Failed to find start time field in this event.', 500); 00030 00031 if ( !$record->checkPermission('edit', array('field'=>$startAtt)) ){ 00032 throw new Exception('Failed to move record because you don\'t have permission to change the start time field.', 400); 00033 00034 } 00035 00036 $endAtt = $ontology->getFieldname('end'); 00037 if ( PEAR::isError($endAtt) ) throw new Exception('Failed to find start time field in this event.', 500); 00038 00039 if ( !$record->checkPermission('edit', array('field'=>$endAtt)) ){ 00040 throw new Exception('Failed to move record because you don\'t have permission to change the end time field.', 400); 00041 00042 } 00043 00044 00045 00046 if ( @$_POST['-calendar-day-delta'] ){ 00047 $dateDelta = intval($_POST['-calendar-day-delta']); 00048 $deltaSign = '+'; 00049 if ( $dateDelta<0 ) $deltaSign='-'; 00050 $dateDelta = abs($dateDelta); 00051 00052 00053 00054 $currStart = $record->strval($startAtt); 00055 $currEnd = $record->strval($endAtt); 00056 $newStart = date('Y-m-d H:i:s', strtotime($deltaSign.$dateDelta.' day', strtotime($currStart))); 00057 $newEnd = date('Y-m-d H:i:s', strtotime($deltaSign.$dateDelta.' day', strtotime($currEnd))); 00058 //echo $deltaSign.$dateDelta.' ('.$newDate.')';exit; 00059 //$record->setValue($dateAtt, $newDate); 00060 $record->setValues(array( 00061 $startAtt=>$newStart, 00062 $endAtt=>$newEnd 00063 )); 00064 00065 00066 } 00067 00068 00069 if ( @$_POST['-calendar-minute-delta'] ){ 00070 $minuteDelta = intval($_POST['-calendar-minute-delta']); 00071 $deltaSign = '+'; 00072 if ( $minuteDelta<0 ) $deltaSign = '-'; 00073 $minuteDelta = $deltaSign.(abs($minuteDelta)); 00074 00075 00076 00077 00078 $currStart = $record->strval($startAtt); 00079 $currEnd = $record->strval($endAtt); 00080 00081 $newStart = date('Y-m-d H:i:s', strtotime($minuteDelta.' minutes', strtotime($currStart))); 00082 $newEnd = date('Y-m-d H:i:s', strtotime($minuteDelta.' minutes', strtotime($currEnd))); 00083 00084 00085 00086 $record->setValues(array( 00087 $startAtt=>$newStart, 00088 $endAtt=>$newEnd 00089 )); 00090 00091 } 00092 00093 if (@$_POST['-calendar-all-day'] == 'true' ){ 00094 // This is set to all day. 00095 $alldayAtt = $ontology->getFieldname('allday'); 00096 00097 if ( !$alldayAtt or PEAR::isError($alldayAtt) ){ 00098 throw new Exception('Could not find allday attribute in the field. Please mark a field with event.allday in the fields.ini file.', 500); 00099 00100 } 00101 00102 $record->setValues(array( 00103 $alldayAtt=>1 00104 )); 00105 00106 } else { 00107 // This is set to all day. 00108 $alldayAtt = $ontology->getFieldname('allday'); 00109 if ( !$alldayAtt or PEAR::isError($alldayAtt) ){ 00110 //throw new Exception('Could not find allday attribute in the field. Please mark a field with event.allday in the fields.ini file.', 500); 00111 00112 } else { 00113 00114 $record->setValues(array( 00115 $alldayAtt=>0 00116 )); 00117 } 00118 } 00119 00120 $res = $record->save(null, true); 00121 if ( PEAR::isError($res) ){ 00122 error_log($res->toString()); 00123 throw new Exception("Failed to update event due to an error while saving. Please see server error log for details.", 500); 00124 } 00125 00126 00127 // At this point, presumeably the event has been updated 00128 $this->out(array( 00129 'code'=>200, 00130 'message'=>'Event updated successfully' 00131 )); 00132 exit; 00133 00134 00135 00136 00137 } catch (Exception $ex){ 00138 00139 $this->out(array( 00140 'message'=>$ex->getMessage(), 00141 'code'=>$ex->getCode() 00142 )); 00143 exit; 00144 } 00145 00146 00147 } 00148 00149 00150 function out($params){ 00151 header('Content-type: text/json; charset="'.Dataface_Application::getInstance()->_conf['oe'].'"'); 00152 echo json_encode($params); 00153 } 00154 }