Tuesday, March 21, 2006

EventController.php



<?php
require_once 'config/config.php';
require_once 'helpers/SecurityHelper.php';
require_once 'helpers/ModelHelper.php';
require_once 'helpers/HTMLHelper.php';
require_once 'helpers/ApplicationHelper.php';
require_once 'models/Event.php';
require_once 'XNC/Form.php';

class EventController {

public static function process($action, $errors = null) {

switch($action) {

case 'new':
if (SecurityHelper::reportFailure(SecurityHelper::checkCurrentUserIsSignedIn())) { return; }
$form = new XNC_Form(array('month' => date('m', time()), 'day' => date('d', time()), 'year' => date('Y', time())));
include 'views/event/new.php';
break;

case 'create':
if (SecurityHelper::reportFailure(SecurityHelper::checkCurrentUserIsSignedIn())) { return; }
if ($errors = self::validate()) {
self::process('new', $errors);
return;
}
$event = XN_Content::create('Event');
self::copyPostVariablesToContentObject($event);
$event->isPrivate = TRUE;
$event->save();
self::addTags($_POST['event:tags'],$event);
header('Location:/index.php?controller=event&action=view&id=' . $event->id);
break;

case 'view':
$event = XN_Content::load(intval($_GET['id']));
$event->focus();
require_once 'models/Comment.php';
$comment = new Comment($event);
include 'views/event/view.php';
break;

case 'delete':
$event = XN_Content::load(intval($_GET['id']));
if (SecurityHelper::reportFailure(SecurityHelper::checkCurrentUserContributedOrIsAppOwner($event))) { return; }
XN_Content::delete($event);
header('Location:/index.php?controller=event&action=list');
break;

case 'edit':
$event = XN_Content::load(intval($_GET['id']));
if (SecurityHelper::reportFailure(SecurityHelper::checkCurrentUserContributedOrIsAppOwner($event))) { return; }
$form = new XNC_Form(self::formDefaults($event));
$event->focus();
$tags = HTMLHelper::popularTagsPlain($event);
include 'views/event/edit.php';
break;

case 'widgetInstructions':
include 'views/event/widgetInstructions.php';
break;

case 'update':
$event = XN_Content::load(intval($_GET['id']));
if (SecurityHelper::reportFailure(SecurityHelper::checkCurrentUserContributedOrIsAppOwner($event))) { return; }
if ($errors = self::validate()) {
self::process('edit', $errors);
return;
}
self::copyPostVariablesToContentObject($event);
$event->isPrivate = TRUE;
$event->save();
self::addTags($_POST['event:tags'],$event);
header('Location:/index.php?controller=event&action=view&id=' . $event->id);
break;

case 'list':
require_once 'XNC/Calendar.php';
$month = $_GET['month'] ? $_GET['month'] : date('m', time());
$year = $_GET['year'] ? $_GET['year'] : date('Y', time());
$previousMonth = ($month == 1) ? 12 : ($month - 1);
$previousYear = ($month == 1) ? ($year - 1) : $year;
$nextMonth = ($month == 12) ? 1 : ($month + 1);
$nextYear = ($month == 12) ? ($year + 1) : $year;
$query = XN_Query::create('Content')
->filter('owner')
->filter('type', 'eic', "Event")
->filter('my->date', '>=', ApplicationHelper::ISO8601Date($year, $month, 1), XN_Attribute::DATE)
->filter('my->date', '<', ApplicationHelper::ISO8601Date($nextYear, $nextMonth, 1), XN_Attribute::DATE);
$events = $query->;execute();
$birthdayQuery = XN_Query::create('Content')
->filter('owner')
->filter('type', 'eic', 'Person')
// Use intval to strip the leading 0 [Jon Aquino 2006-02-24]
->filter('my->parsedBirthMonth', '=', ''.intval($month));
foreach ($birthdayQuery->execute() as $person) {
$person->my->set('date', ApplicationHelper::ISO8601Date($year, $month, $person->my->parsedBirthDay), XN_Attribute::DATE);
$events []= $person;
}
$calendar = XNC_Calendar::create($month, $year, $events, 'my->date');
$monthEventCount = 0;
for ($i = 1; $i < $calendar->daysInMonth(); $i++) { $monthEventCount += count($calendar->getDayEvents($i)); }
include 'views/event/list.php';
break;

default:
throw new Exception('Unknown action: ' . $action);

}
}
private static function validate() {
$errors = ModelHelper::validate('Event');
if (ApplicationHelper::ISO8601Date($_POST['year'], $_POST['month'], $_POST['day']) === NULL) {
$errors[] = 'Date must be a valid mm/dd/yyyy date';
}
return $errors;
}
private static function copyPostVariablesToContentObject($event) {
ModelHelper::processContent(new Event(), $event);
$event->my->set('date', ApplicationHelper::ISO8601Date($_POST['year'], $_POST['month'], $_POST['day']), XN_Attribute::DATE);
}
private static function formDefaults($event) {
$defaults = ModelHelper::getDefaultsFromContent(new Event(), $event);
$defaults['year'] = gmdate('Y', strtotime($event->my->date));
$defaults['month'] = gmdate('m', strtotime($event->my->date));
// j not d -- see EXA-1569 [Jon Aquino 2006-03-13]
$defaults['day'] = gmdate('j', strtotime($event->my->date));
return $defaults;
}
private static function weekend($dayNumber) { return $dayNumber == 1 || $dayNumber == 7; }
private static function addTags($tags,$event) {
if (strlen($tags) > 0) {
try {
XN_Tag::addTags($event, $tags);
} catch (Exception $e) {
// ignore since the tag already exists
}
}
}
}
?>




Event.php


<?php

// Specify the absolute path, as this is included by scripts/api.php [Jon Aquino 2006-03-06]
require_once '/' . XN_Application::load()->relativeUrl . '/helpers/ApplicationHelper.php';

class Event {

// The variables are simply used to tell ModelHelper the names of the
// attributes in the content object (its "shape"). They are not actually
// populated. [Jon Aquino 2005-09-30]
public $title;
public $description;
public $location;
public $day;
public $month;
public $year;

public function __construct() {
$this->_rules['title'] = array('required', array('maxlength', 100));
$this->_rules['description'] = array(array('maxlength', 4000));
$this->_rules['location'] = array(array('maxlength', 100));
$this->_rules['day'] = array(array('maxlength', 2));
$this->_rules['month'] = array(array('maxlength', 2));
$this->_rules['year'] = array(array('maxlength', 4));
}

public static function currentEvents() {
$today = ApplicationHelper::ISO8601Date(date('Y', time()), date('m', time()), date('d', time()));
if ($_GET['currentEventsTestDate']) {
$currentEventsTestDate = split(',', $_GET['currentEventsTestDate']);
$today = ApplicationHelper::ISO8601Date($currentEventsTestDate[0], $currentEventsTestDate[1], $currentEventsTestDate[2]);
}
$n = 4;
$currentEvents = XN_Query::create('Content')
->filter('type','=','Event')
->filter('owner')
->filter('my->date', '>=', $today, XN_Attribute::DATE)
->end($n)
->order('my->date', 'asc', XN_Attribute::DATE)
->execute();
if (count($currentEvents) < $n) {
$recentEvents = XN_Query::create('Content')
->filter('type','=','Event')
->filter('owner')
->filter('my->date', '<', $today, XN_Attribute::DATE)
->end($n - count($currentEvents))
->order('my->date', 'desc', XN_Attribute::DATE)
->execute();
$currentEvents = array_merge(array_reverse($recentEvents), $currentEvents);
}
return $currentEvents;
}

}



event/view.php


<?php
require_once 'helpers/HTMLHelper.php';
HTMLHelper::displayErrors($errors); ?>
<h2><?php echo XN_Application::load()->name ?> Events</h2>
<form id="form" name="form" method="POST" action="/index.php?controller=event&amp;action=create&amp;">
<h3>Add your event!</h3>
<fieldset>
<dl>
<?php
include '_form.php' ?>
</dl>
</fieldset>
<p class="align-right"><input src="/images/but/addevent.gif" alt="Add Event" type="image"></p>
</form>

4 Comments:

At 4/30/2006 10:39 a.m., Anonymous Anonymous said...

Мapping classnames to filenames and using __autoload() helps to avoid manually listing multiple require_once()

 
At 4/30/2006 12:13 p.m., Blogger Jonathan said...

Anonymous - great tip! thx!

 
At 7/03/2006 12:17 p.m., Anonymous Anonymous said...

Hi Jon, I'm having some trouble seeing your code with Firefox...

 
At 7/03/2006 12:22 p.m., Blogger Jonathan said...

Hi Greg--yeah it's probably being obscured by the dark background on either side.

You can see the code in its entirety if you click View > Page Style > No Style

 

Post a Comment

<< Home