public static function matchEventTimesToDates($event, $phpDates) { $dates = array(); foreach ($phpDates as $dateVal) { $dates[] = $dateVal->format('Y-m-d'); } foreach ($event->buildEventTimes('id') as $eventTime) { // For all existing dates $formattedDate = $eventTime->getFormattedDate(); if (!in_array($formattedDate, $dates)) { // If they didn't submit this existing date delete it $eventTime->delete(); } else { if (($key = array_search($formattedDate, $dates)) !== false) { unset($dates[$key]); } } } foreach ($dates as $newDate) { $eventTime = new EventTime(); $eventTime->setModified(time()); $eventTime->setId($event->getId()); $eventTime->setEventdate($newDate); $eventTime->setEventstatus('A'); $eventTime->store(); } // Flourish is suck. I can't figure out the "right" way to do one-to-many cause docs are crap // This clears a cache that causes subsequent operations (buildEventTimes) to return stale data $event->related_records = array(); }
* { * events: [ * { * * }, * ... * ] * } * * If there is a problem the error code will be 400 with a json response of the form: * { * "error": { * "message": "Error message" * } * } */ if (isset($_GET['startdate']) && ($parseddate = strtotime($_GET['startdate']))) { $startdate = $parseddate; } else { $startdate = time(); } if (isset($_GET['enddate']) && ($parseddate = strtotime($_GET['enddate']))) { $enddate = $parseddate; } else { $enddate = time(); } $json = array('events' => array()); foreach (EventTime::getRangeVisible($startdate, $enddate) as $eventTime) { $json['events'][] = $eventTime->toEventSummaryArray(); } fJSON::output($json);
* ... * ] * } * * If there is a problem the error code will be 400 with a json response of the form: * { * "error": { * "message": "Error message" * } * } */ if (isset($_GET['startdate']) && ($parseddate = strtotime($_GET['startdate']))) { $startdate = $parseddate; } else { $startdate = time(); } if (isset($_GET['enddate']) && ($parseddate = strtotime($_GET['enddate']))) { $enddate = $parseddate; } else { $enddate = time(); } $json = array('events' => array()); if (isset($_GET['id'])) { $events = EventTime::getByID($_GET['id']); } else { $events = EventTime::getRangeVisible($startdate, $enddate); } foreach ($events as $eventTime) { $json['events'][] = $eventTime->toEventSummaryArray(); } fJSON::output($json);
function build_json_response() { if (!isset($_POST['json'])) { return array('error' => array('message' => "No JSON found")); } $data = json_decode($_POST['json'], true); if (!$data) { return array('error' => array('message' => "JSON could not be decoded")); } $_POST = $data; // fValidation inspects $_POST for field data $validator = new fValidation(); $validator->addRequiredFields('title', 'details', 'venue', 'address', 'organizer', 'email', 'read_comic'); $validator->addEmailFields('email'); $validator->addRegexReplacement('#^(.*?): (.*)$#', '\\2 for <span class="field-name">\\1</span>'); // If id is specified require secret $validator->addConditionalRule(array('id'), NULL, array('secret')); $messages = $validator->validate(TRUE, TRUE); if (!$data['read_comic']) { $messages['read_comic'] = 'You must have read the Ride Leading Comic'; } if ($messages) { return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages)); } $inputDateStrings = get($data['dates'], array()); $validDates = array(); $invalidDates = array(); foreach ($inputDateStrings as $dateString) { $date = DateTime::createFromFormat('Y-m-d', $dateString); if ($date) { $validDates[] = $date; } else { $invalidDates[] = $dateString; } } if ($invalidDates) { $messages['dates'] = "Invalid dates: " . implode(', ', $invalidDates); } if (count($validDates) === 1) { $data['datestype'] = 'O'; $data['datestring'] = date_format($validDates[0], 'l, F j'); } else { // not dealing with 'consecutive' $data['datestype'] = 'S'; $data['datestring'] = 'Scattered days'; } // Converts data to an event, loading the existing one if id is included in data $event = Event::fromArray($data); // Else if ($event->exists() && !$event->secretValid($data['secret'])) { return array('error' => array('message' => 'Invalid secret, use link from email')); } $messages = $event->validate($return_messages = TRUE, $remove_column_names = TRUE); if (isset($_FILES['file'])) { $uploader = new fUpload(); $uploader->setMIMETypes(array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'), 'The file uploaded is not an image'); $uploader->setMaxSize('2MB'); $uploader->setOptional(); $file_message = $uploader->validate('file', TRUE); if ($file_message != null) { $messages['file'] = $file_message; } global $IMAGEDIR; $file = $uploader->move($IMAGEDIR, 'file'); $event->setImage($file->getName()); } if ($messages) { return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages)); } // if needs secret generate and email if (!$event->exists()) { $includeSecret = true; } else { $includeSecret = false; } // If there are validation errors this starts spewing html, so we validate before $event->store(); // Create/delete EventTimes to match the list of dates included EventTime::matchEventTimesToDates($event, $validDates); // Returns the created object $details = $event->toDetailArray(true); if ($includeSecret) { $details['secret'] = $event->getPassword(); // Wait until after it is stored to ensure it has an id $event->emailSecret(); } return $details; }
<?php /** * Imports some events from the real calendar for testing * * Takes one argument, number of days of events to import. Default is 10. */ include 'init.php'; const URL = 'http://shift2bikes.org/betacal/www/events.php'; if (count($argv) > 1) { $futureDays = intval($argv[1]) - 1; } else { $futureDays = 9; } $endDate = new DateTime("+{$futureDays} days"); $ch = curl_init(URL . "?enddate=" . $endDate->format('Y-m-d')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $eventJson = curl_exec($ch); curl_close($ch); $decoded = json_decode($eventJson, true); $eventArrays = $decoded['events']; foreach ($eventArrays as $eventArray) { $event = Event::fromArray($eventArray); $event->store(); $dates = array(DateTime::createFromFormat('Y-m-d', $eventArray['date'])); EventTime::matchEventTimesToDates($event, $dates); } $num = count($eventArrays); print "Imported {$num} events from shift2bikes.org\n";
<?php require_once '../time.php'; echo 'Test Event Time Class: </br>'; $time = array('year' => '1995', 'month' => 'jan', 'day' => '28', 'startTime' => '0830', 'endTime' => '0930'); $eventTime = new EventTime($time); echo 'Year: ' . $eventTime->year . '</br>'; echo $eventTime->toString(); echo '</br></br>'; echo 'Test Lesson Time Class: </br>'; echo 'Case 1: </br>'; $time = array('startTime' => '0830', 'endTime' => '0930', 'wkDay' => '01'); $lessonTime = new LessonTime($time); echo 'Year: ' . $lessonTime->year . '</br>'; echo 'wkDay: ' . $lessonTime->wkDay . '</br>'; echo $lessonTime->toString(); echo 'Case 2: </br>'; $time = array('startTime' => '0830', 'endTime' => '0930', 'wkDay' => 'mon'); $lessonTime = new LessonTime($time); $lessonTime->setWkRepeatTrueforThisWk(0); echo 'Year: ' . $lessonTime->year . '</br>'; echo 'wkDay: ' . $lessonTime->wkDay . '</br>'; echo $lessonTime->toString(); echo '</br></br>'; echo 'Test semInfo: </br>'; $sem = semInfo('2013', '1'); echo 'Year: ' . $sem['year'] . '</br>'; echo 'Month: ' . $sem['month'] . '</br>'; echo 'Day: ' . $sem['day'] . '</br>'; echo '</br></br>'; echo 'Test fewDaysNextOrBefore: </br>';