public function save() { Logger::log("Enter: EventAssociation::save"); // check for complete info if (empty($this->event_id)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: event_id is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'event_id is missing.'); } if (empty($this->assoc_target_type)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: assoc_target_type is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'assoc_target_type is missing.'); } if (empty($this->assoc_target_id)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: assoc_target_id is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'assoc_target_id is missing.'); } if (empty($this->assoc_target_name)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: assoc_target_name is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'assoc_target_name is missing.'); } // depending on assoc_target_type check if network|group|user exists switch ($this->assoc_target_type) { case "network": // network of assoc_target_id exists? // this check should maybe be part of the Network class? $res = Dal::query("SELECT COUNT(*) FROM {networks} \n WHERE network_id=? AND is_active=1", array($this->assoc_target_id)); if (!$res->numRows()) { Logger::log(" Throwing exception NETWORK_NOT_FOUND | Message: Network does not exist", LOGGER_ERROR); throw new PAException(NETWORK_NOT_FOUND, 'Network does not exist.'); } break; case "group": // group of assoc_target_id exists? $res = Dal::query("SELECT COUNT(*) FROM {groups} \n WHERE group_id=?", array($this->assoc_target_id)); if (!$res->numRows()) { Logger::log(" Throwing exception GROUP_NAME_NOT_EXIST | Message: Group does not exist", LOGGER_ERROR); throw new PAException(GROUP_NAME_NOT_EXIST, 'Group does not exist.'); } break; case "user": // user of assoc_target_id exists? if (!User::user_exist($this->assoc_target_id)) { Logger::log(" Throwing exception USER_NOT_FOUND | Message: User does not exist", LOGGER_ERROR); throw new PAException(USER_NOT_FOUND, 'User does not exist.'); } break; default: // oh-oh, not a valid assoc_target_type!! Logger::log(" Throwing exception BAD_PARAMETER | Message: " . $this->assoc_target_type . " is not a valid assoc_target_type", LOGGER_ERROR); throw new PAException(BAD_PARAMETER, $this->assoc_target_type . " is not a valid assoc_target_type"); break; } // check to prevent duplicate associations if (EventAssociation::assoc_exists($this->assoc_target_type, $this->assoc_target_id, $this->event_id)) { Logger::log(" Throwing exception BAD_PARAMETER | Message: " . "There already is an EventAsssociation for this network, group or user.", LOGGER_ERROR); throw new PAException(BAD_PARAMETER, "The Event is already associated to this " . $this->assoc_target_type . "."); } if (!Event::exists($this->event_id)) { Logger::log(" Throwing exception EVENT_NOT_EXIST | Message: Event does not exist", LOGGER_ERROR); throw new PAException(EVENT_NOT_EXIST, 'Event does not exist.'); } // load the Event if not already loaded if (!$this->event) { $this->load_event($this->event_id); } // serialize assoc_data for storage $assoc_data = ""; if (!empty($this->assoc_data)) { $assoc_data = serialize($this->assoc_data); } // are we creating a new one? if (!$this->assoc_id) { // do we have a real User set as owner? if (!User::user_exist($this->user_id)) { Logger::log(" Throwing exception USER_NOT_FOUND | Message: User does not exist", LOGGER_ERROR); throw new PAException(USER_NOT_FOUND, 'User does not exist.'); } // do we have an Event? if (!Event::exists($this->event->event_id)) { Logger::log(" Throwing exception EVENT_NOT_EXIST | Message: Event does not exist", LOGGER_ERROR); throw new PAException(EVENT_NOT_EXIST, 'Event does not exist.'); } $sql = "INSERT INTO events_associations \n (event_id, user_id, assoc_target_type, assoc_target_id, assoc_target_name, event_title, start_time, end_time, assoc_data) \n VALUES (?,?,?,?,?,?,?,?,?)"; $data = array($this->event->event_id, $this->user_id, $this->assoc_target_type, $this->assoc_target_id, $this->assoc_target_name, $this->event->event_title, $this->event->start_time, $this->event->end_time, $assoc_data); } else { $sql = "UPDATE {events_associations} SET " . "event_id = ?, user_id = ?, assoc_target_type = ?, assoc_target_id = ?,\n assoc_target_name = ?, event_title = ?, start_time = ?, end_time = ?,\n assoc_data = ?" . "WHERE assoc_id = ?"; $data = array($this->event->event_id, $this->user_id, $this->assoc_target_type, $this->assoc_target_id, $this->assoc_target_name, $this->event->event_title, $this->event->start_time, $this->event->end_time, $assoc_data, $this->assoc_id); } // write to DB try { Dal::query($sql, $data); if (!$this->assoc_id) { $this->assoc_id = Dal::insert_id(); } // Finally - commit our changes to the DB Dal::commit(); } catch (PAException $e) { // roll back database operations and re-throw the exception Dal::rollback(); throw $e; } Logger::log("Exit: EventAssociation::save"); }
* @license http://bit.ly/aVWqRV PayAsYouGo License * @copyright Copyright (c) 2010 Broadband Mechanics * @package Cyberspace-Networks */ if (!empty($ed['event_id'])) { ?> <h2><?php echo __("This event appears on"); ?> </h2> <ul class="event-associations"> <?php // find all EventAssociations for the Event $assoc_ids = EventAssociation::find_for_event($ed['event_id']); $assocs = EventAssociation::load_in_list($assoc_ids); $appears_in = array(); foreach ($assocs as $n => $assoc) { // remember this for add to calendars below $appears_in[] = $assoc->assoc_target_type . ":" . $assoc->assoc_target_id; echo "<li>"; echo __("Calendar for") . " " . $assoc->assoc_target_type . " " . $assoc->assoc_target_name; // add remove link if we are allowed to $may_remove = false; // user is creator/owner if ($assoc->user_id == PA::$login_user->user_id) { $may_remove = true; } // user is target if ($assoc->assoc_target_type == 'user' && $assoc->assoc_target_id == PA::$login_user->user_id) { $may_remove = true;
} } if (count($add_assoc_group_errors)) { $msg .= __("Add group:"); foreach ($add_assoc_group_errors as $n => $err) { $msg .= "<br />{$err}"; } $is_display = false; $is_edit = true; } } // see if we have a remove request for an association foreach ($_POST as $k => $v) { if (preg_match('/^remove_assoc_(\\d+)/', $k, $m)) { $rem_assoc = (int) $m[1]; $assoc = new EventAssociation(); try { $assoc->load($rem_assoc); } catch (CNException $e) { $msg = __("You may not remove the association to this event.") . "<br/>" . $e->getMessage(); } if ($assoc->user_id) { // is this user OWNER and TARGET? if ($assoc->user_id == PA::$login_user->user_id && ($assoc->assoc_target_type == 'user' && $assoc->assoc_target_id == PA::$login_user->user_id)) { $msg = __("You cannot remove yourself from this event, as you created it. Please use 'Delete Event' instead."); } else { if ($assoc->user_id == PA::$login_user->user_id || $assoc->assoc_target_type == 'user' && $assoc->assoc_target_id == PA::$login_user->user_id) { EventAssociation::delete($rem_assoc); } else { $msg = __("You may not remove the association to this event."); }
public function save() { Logger::log("Enter: Event::save"); // check for complete info if (empty($this->event_title)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: event_title is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'Please supply an Event Title.'); } $this->title = $this->event_title; if (empty($this->start_time)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: start_time is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'Please specify the Start Time.'); } if (empty($this->end_time)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: end_time is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'Please specify the End Time.'); } // serialize event_data for storage $event_data = ""; if (!empty($this->event_data)) { $event_data = serialize($this->event_data); } // make end_time sane (can only be same or after start_time) if (strtotime($this->end_time) <= strtotime($this->start_time)) { $this->end_time = $this->start_time; } $this->author_id = $this->user_id; $this->body = $this->event_data['description']; // are we creating a new one? if (!$this->event_id) { /* if (empty($this->content_id)) { Logger::log(" Throwing exception REQUIRED_PARAMETERS_MISSING | Message: content_id is empty", LOGGER_ERROR); throw new PAException(REQUIRED_PARAMETERS_MISSING, 'Content id is missing.'); } */ // do we have a real User set as owner? if (!User::user_exist((int) $this->user_id)) { Logger::log(" Throwing exception USER_NOT_FOUND | Message: User does not exist", LOGGER_ERROR); throw new PAException(USER_NOT_FOUND, 'User does not exist.'); } // save a Content parent::save(); $sql = "INSERT INTO events \n (content_id, user_id, event_title, start_time, end_time, event_data) \n VALUES (?, ?, ?, ?, ?, ?)"; $data = array($this->content_id, $this->user_id, $this->event_title, $this->start_time, $this->end_time, $event_data); } else { // save as Content parent::save(); $sql = "UPDATE {events} SET " . "event_title = ?, start_time = ?, end_time = ?, event_data = ? " . "WHERE event_id = ?"; $data = array($this->event_title, $this->start_time, $this->end_time, $event_data, $this->event_id); } // write to DB try { Dal::query($sql, $data); if (!$this->event_id) { // newly created $this->event_id = Dal::insert_id(); } else { // update any existing EventAssociations EventAssociation::update_assocs_for_event($this); } // Finally - commit our changes to the DB Dal::commit(); } catch (PAException $e) { // roll back database operations and re-throw the exception Dal::rollback(); throw $e; } Logger::log("Exit: Event::save"); }
public function testAddUpdateDeleteEvent() { // Dal::register_query_callback("explain_query"); echo "getting a user\n"; $user = Test::get_test_user(); $testusername = $user->first_name . " " . $user->last_name; echo "test user = {$testusername}\n"; /* setup some times and time strings */ $today = mktime(0, 0, 0, date("m"), date("d"), date("Y")); $tomorrow = mktime(0, 0, 0, date("m"), date("d") + 1, date("Y")); $yesterday = mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")); $lastmonth = mktime(0, 0, 0, date("m") - 1, date("d"), date("Y")); $nextmonth = mktime(0, 0, 0, date("m") + 1, date("d"), date("Y")); $nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y") + 1); $oneday = 60 * 60 * 24; $simple_dateformat = "Y-m-d"; /* use the constants in the format parameter */ // something like: Mon, 15 Aug 2005 15:12:46 UTC $now_rfc822 = date(DATE_RFC822); // something like: 2000-07-01T00:00:00+00:00 // $now_atom = date(DATE_ATOM); // create an Event echo "create and save Event\n"; $e = new Event(); $e->content_id = "http://myevent.info/1"; // anything basically $e->user_id = $user->user_id; $e->event_title = "Test Event for {$testusername}"; $now = time(); $nowplusoneday = $now + $oneday; $e->start_time = date(DATE_ATOM, $now); $e->end_time = date(DATE_ATOM, $now + 60 * 60); // duration 1h $e->event_data = array('description' => "This Event takes place to test the class Event", 'start' => $now, 'end' => $now + 60 * 60); $e->save(); // print_r($e); // see if we got it echo "Retrieving Event {$e->event_id}\n"; $e2 = new Event(); $e2->load($e->event_id); echo "Testing integrity of dates\n"; // print_r($e2); // see if the stored timestamps match $this->assertEquals($now, $e2->event_data['start']); // see if our dates survived the DB conversion roundtrip $this->assertEquals($now, strtotime($e2->start_time)); $this->assertEquals($now + 60 * 60, strtotime($e2->end_time)); // create two EventAssociations $ea1 = new EventAssociation(); $ea2 = new EventAssociation(); $ea1->user_id = $user->user_id; $ea2->user_id = $user->user_id; // user EventAssocoiation $ea1->assoc_target_type = 'user'; $ea1->assoc_target_id = $user->user_id; // could very well be other user $ea1->assoc_target_name = $testusername; $ea1->event_id = $e->event_id; $ea1->save(); // network EventAssocoiation // find a network the user is member of // $networks = Network::get_user_networks($user->user_id); $network = Network::get_mothership_info(); // use the mothership // print_r($network); $ea2->assoc_target_type = 'network'; $ea2->assoc_target_id = $network->network_id; // could very well be other user $ea2->assoc_target_name = $network->name; $ea2->event_id = $e->event_id; $ea2->save(); echo "Testing EventAssociations for Event {$e->event_id}\n"; $assoc_ids = EventAssociation::find_for_event($e->event_id); // print_r($assoc_ids); $a_cnt = count($assoc_ids); $this->assertEquals($a_cnt, 2, "expected 2 assocs, got {$a_cnt}\n"); echo "Testing EventAssociations::find_for_target_and_delta for Network\n"; $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id); // find_for_target_and_delta($target_type, $target_id, $range_start = NULL, $range_end = NULL) // print_r($assoc_ids); $a_cnt = count($assoc_ids); // we expect at least one (or more, the user might have others too) $this->assertTrue($a_cnt >= 1, "expected 1 or more assocs, got {$a_cnt}\n"); echo "Testing EventAssociations::find_for_target_and_delta for Today\n"; /* echo "yesterday = " . date(DATE_ATOM, $yesterday) . "\n"; echo "today = " . date(DATE_ATOM, $today) . "\n"; echo "event start_time = " . date(DATE_ATOM, strtotime($e2->start_time)) . "\n"; echo "event end_time = " . date(DATE_ATOM, strtotime($e2->end_time)) . "\n"; echo "tomorrow = " . date(DATE_ATOM, $tomorrow) . "\n"; */ $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $today), date(DATE_ATOM, $tomorrow)); print_r($assoc_ids); /* $assocs = EventAssociation::load_in_list($assoc_ids); print_r($assocs); */ $a_cnt = count($assoc_ids); // we expect at least one (or more, the user might have others too) $this->assertTrue($a_cnt >= 1, "expected 1 or more assocs, got {$a_cnt}\n"); echo "Testing if the EventAssociations now show up in Tomorrow's Calendar\n"; $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $tomorrow), date(DATE_ATOM, $tomorrow + $oneday)); print_r($assoc_ids); $a_cnt2 = count($assoc_ids); // we expect one less than before $this->assertTrue($a_cnt2 < $a_cnt, "expected " . $a_cnt - 1 . " assocs, got {$a_cnt2}\n"); echo "Modifying original Event\n"; $e2->title = "changed title"; $e2->end_time = date(DATE_ATOM, $nextmonth); $e2->save(); // see if we got it $e3 = new Event(); $e3->load($e->event_id); echo "Testing integrity of dates again in the Event\n"; // see if our dates survived the DB conversion roundtrip $this->assertEquals($now, strtotime($e3->start_time)); $this->assertEquals(date(DATE_ATOM, strtotime($e3->end_time)), date(DATE_ATOM, $nextmonth)); echo "Testing if modified dates made it to the EventAssociations\n"; $assoc_ids = EventAssociation::find_for_event($e3->event_id); $assocs = EventAssociation::load_in_list($assoc_ids); echo "e3 end_time: " . date(DATE_ATOM, strtotime($assocs[0]->end_time)) . "\nnextmonth: " . date(DATE_ATOM, $nextmonth) . "\n"; $this->assertEquals(date(DATE_ATOM, strtotime($assocs[0]->end_time)), date(DATE_ATOM, $nextmonth)); echo "Testing if the EventAssociations now show up in Tomorrow's Calendar\n"; echo "test range: " . date(DATE_ATOM, $tomorrow) . " - " . date(DATE_ATOM, $tomorrow + $oneday) . "\n"; echo "event duration: " . date(DATE_ATOM, strtotime($e3->start_time)) . " - " . date(DATE_ATOM, strtotime($e3->end_time)) . "\n"; // print_r($e3); $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $tomorrow), date(DATE_ATOM, $tomorrow + $oneday)); print_r($assoc_ids); $a_cnt = count($assoc_ids); // we expect at least one (or more, the user might have others too) $this->assertTrue($a_cnt >= 1, "expected 1 or more assocs, got {$a_cnt}\n"); echo "Deleting Event {$e->event_id}\n"; Event::delete($e->event_id); // try loading $this->assertNull($e3->load($e->event_id)); echo "Testing if all EventAssociations have been removed\n"; $assoc_ids = EventAssociation::find_for_event($e->event_id); $a_cnt = count($assoc_ids); $this->assertEquals($a_cnt, 0, "expected 0 assocs, got {$a_cnt}\n"); }