00001 <?
00011 class BaseEvent extends MyObject
00012 {
00013 public $guests = null;
00014 public $guestTable = null;
00015
00021 public function __construct($data = null, $table = 'events', $guestTable = 'event_guests')
00022 {
00023 $this->useLatLng = true;
00024 $this->commentsEnabled = true;
00025
00026 $this->guestTable = $guestTable;
00027
00028 parent::__construct($data, $table);
00029
00030 $this->fullTextFields[] = 'name';
00031 $this->fullTextFields[] = 'description';
00032 $this->fullTextFields[] = 'location';
00033 }
00034
00040 public function getPagesXml()
00041 {
00042 $xml = parent::getPagesXml();
00043 $xml .= <<<XML
00044 <page name="invite">
00045 <param name="id" required="1" type="int"/>
00046 </page>
00047 <page name="uninvite">
00048 <param name="id" required="1" type="int"/>
00049 <param name="guest_id" required="1" type="int"/>
00050 </page>
00051 <page name="rsvp">
00052 <param name="id" required="1" type="int"/>
00053 </page>
00054 <page name="attend">
00055 <param name="id" required="1" type="int"/>
00056 </page>
00057 <page name="leave">
00058 <param name="id" required="1" type="int"/>
00059 </page>
00060 XML;
00061 return $xml;
00062 }
00063
00067 public function getEditPageParamXml()
00068 {
00069 $xml = '<param name="date" regex="/^\d{4}-\d{1,2}-\d{1,2}$/"/>';
00070
00071 return $xml;
00072 }
00073
00077 public function delete()
00078 {
00079 dbExecute("
00080 DELETE FROM {$this->guestTable}
00081 WHERE event_id = '$this->id'
00082 ");
00083
00084 parent::delete();
00085 }
00086
00092 public function initAttendPage()
00093 {
00094 $this->assertLogin();
00095
00096 $this->id = $this->params('id');
00097
00098 if (!$this->canAttend())
00099 throw new PageError('You do not have access to attend this event.');
00100
00101 $this->pageTitle = "I'm there!";
00102 }
00103
00107 public function drawAttendPage()
00108 {
00109 global $me;
00110
00111 $form = $this->createAttendForm();
00112 if ($form->isSubmitted())
00113 {
00114 $form->validate();
00115
00116 if (!$form->hasError())
00117 {
00118 $this->addGuest($me->id);
00119 $this->rsvp($me->id, 'yes', $form->getData('guests'));
00120 Util::redirect($this->getUrl(".view?id=$this->id"));
00121 }
00122 }
00123 else
00124 $form->setData(array('guests' => 0));
00125
00126 if ($form->needsDrawn())
00127 $form->drawAll();
00128 }
00129
00135 public function createAttendForm()
00136 {
00137 $form = new Form();
00138 $form->action = $this->getUrl(".attend?id=$this->id");
00139
00140 $form->add('TextField', 'guests', array(
00141 'title' => 'How many guests are you bringing?',
00142 'required' => true,
00143 'size' => 10
00144 ));
00145 $form->addSubmit("I'm There");
00146
00147 return $form;
00148 }
00149
00155 public function initLeavePage()
00156 {
00157 $this->assertLogin();
00158
00159 $this->id = $this->params('id');
00160
00161 if (!$this->canLeave())
00162 throw new PageError('You do not have access to leave this event.');
00163
00164 $this->pageTitle = 'Remove Me From Guests';
00165 }
00166
00170 public function drawLeavePage()
00171 {
00172 global $me;
00173
00174 $form = $this->createLeaveForm();
00175 if ($form->isSubmitted())
00176 {
00177 $this->removeGuest($me->id);
00178 Util::redirect($this->getUrl(".view?id=$this->id"));
00179 }
00180 else
00181 $form->drawAll();
00182 }
00183
00189 public function createLeaveForm()
00190 {
00191 $form = new Form();
00192 $form->action = $this->getUrl(".leave?id=$this->id");
00193
00194 $form->add('StaticField', 'info', array(
00195 'text' => 'Are you sure you want to remove yourself from the list of guests?'
00196 ));
00197 $form->addSubmit("Remove me from list");
00198
00199 return $form;
00200 }
00201
00207 public function initRSVPPage()
00208 {
00209 global $me;
00210
00211 $this->assertLogin();
00212
00213 $this->id = $this->params('id');
00214
00215 if (!$this->isGuest($me->id))
00216 throw new PageError('You must be a guest to RSVP.');
00217
00218 $this->pageTitle = 'RSVP to ' . $this->getName();
00219 }
00220
00224 public function drawRSVPPage()
00225 {
00226 global $me;
00227
00228 $form = $this->createRSVPForm();
00229
00230 if ($form->isSubmitted())
00231 {
00232 $form->validate();
00233
00234 if (!$form->hasError())
00235 {
00236 $this->rsvp($me->id, $form->getData('commitment'), $form->getData('guests'));
00237
00238 Util::redirect($this->getUrl(".view?id=$this->id"));
00239 }
00240 }
00241 else
00242 $form->setData($this->getGuest($me->id));
00243
00244 if ($form->needsDrawn())
00245 $form->drawAll();
00246 }
00247
00253 public function createRSVPForm()
00254 {
00255 $form = new Form();
00256 $form->action = $this->getUrl(".rsvp?id=$this->id");
00257
00258 $form->add('SelectField', 'commitment', array(
00259 'title' => 'Are you attending this event?',
00260 'options' => array(
00261 'yes' => 'Yes',
00262 'no' => 'No',
00263 'maybe' => 'Maybe'
00264 ),
00265 'required' => true
00266 ));
00267 $form->add('TextField', 'guests', array(
00268 'title' => 'How many guests are you bringing?',
00269 'size' => 10
00270 ));
00271 $form->addSubmit("RSVP");
00272
00273 return $form;
00274 }
00275
00279 public function initUnInvitePage()
00280 {
00281 $this->assertLogin();
00282
00283 $this->id = $this->params('id');
00284
00285 $this->pageTitle = 'Uninvite Guests';
00286
00287 if (!$this->canUnInvite())
00288 throw new PageError('You do not have access to uninvite people to this event.');
00289 }
00290
00294 public function drawUnInvitePage()
00295 {
00296
00297 $user = new User($this->params('guest_id'));
00298
00299
00300 $form = $this->createUnInviteForm($user);
00301
00302
00303 if ($form->isSubmitted())
00304 {
00305 $form->validate();
00306
00307
00308 if (!$form->hasError())
00309 {
00310 $this->removeGuest($user->id);
00311 Util::redirect($this->getUrl(".view?id=$this->id"));
00312 }
00313 }
00314
00315
00316 if ($form->needsDrawn())
00317 $form->drawAll();
00318 }
00319
00323 public function createUnInviteForm($user)
00324 {
00325 $form = new Form();
00326 $form->action = $this->getUrl(".uninvite?id=$this->id&guest_id=$user->id");
00327
00328 $form->add("StaticField", "guest", array(
00329 'text' => 'Are you sure you want to uninvite ' . $user->getName(true) . '?'
00330 ));
00331 $form->addSubmit('Uninvite Guest');
00332
00333 return $form;
00334 }
00335
00339 public function canInvite()
00340 {
00341 return $this->canEdit();
00342 }
00343
00347 public function canUnInvite()
00348 {
00349 return $this->canDelete();
00350 }
00351
00359 public function canAttend()
00360 {
00361 global $me;
00362
00363 return ((bool)$this->allow_attendees && !$this->invite_only && $this->canView() && !$this->isGuest($me->id));
00364 }
00365
00373 public function canLeave()
00374 {
00375 global $me;
00376
00377 return (bool)$this->isGuest($me->id);
00378 }
00379
00383 public function initInvitePage()
00384 {
00385 $this->assertLogin();
00386
00387 $this->id = $this->params('id');
00388
00389 $this->pageTitle = 'Invite Guests';
00390
00391 if (!$this->canInvite())
00392 throw new PageError('You do not have access to invite people to this event.');
00393 }
00394
00398 public function drawInvitePage()
00399 {
00400 $form = $this->createInviteForm();
00401
00402
00403 if ($form->isSubmitted())
00404 {
00405 $form->validate();
00406
00407 if (!$form->hasError())
00408 {
00409 $guests = explode(',', $form->getData('guests'));
00410 if (count($guests))
00411 foreach ($guests AS $id)
00412 $this->inviteGuest($id);
00413
00414 Util::redirect($this->getUrl(".view?id=$this->id"));
00415 }
00416 }
00417
00418 if ($form->needsDrawn())
00419 $form->drawAll();
00420 }
00421
00425 public function createInviteForm()
00426 {
00427 global $me;
00428
00429 $form = new Form();
00430 $form->action = $this->getUrl(".invite?id=$this->id");
00431
00432
00433 $form->add('MultiSelectField', 'guests', array(
00434 'multiple' => true,
00435 'size' => 10,
00436 'options' => $this->getPotentialGuestList(),
00437 'title' => 'Select the people you want to invite.<br/>
00438 We will email the people you select <br/>and let them know they
00439 have been invited.',
00440 ));
00441
00442 $form->addSubmit('Invite Guests');
00443
00444 return $form;
00445 }
00446
00454 protected function getPotentialGuestList()
00455 {
00456 global $me;
00457
00458
00459 $items = array();
00460 $friends = $me->getFriends();
00461 foreach ($friends AS $member)
00462 if (!$this->isGuest($member->id))
00463 $items[$member->id] = $member->getName();
00464
00465 return $items;
00466 }
00467
00474 public function isGuest($userId)
00475 {
00476 $guests = $this->getGuests();
00477
00478 return is_array($guests[$userId]);
00479 }
00480
00487 public function getGuest($userId)
00488 {
00489 $guests = $this->getGuests();
00490 return $guests[$userId];
00491 }
00492
00500 public function getGuests($all = true, $force = false)
00501 {
00502 if ($this->guests === null || $force)
00503 {
00504 $ret = array();
00505
00506 if ($this->id)
00507 {
00508 $where = '';
00509 if ($all)
00510 $where = "AND commitment = '$yes'";
00511
00512
00513 $guestRs = dbQuery("
00514 SELECT *
00515 FROM {$this->guestTable}
00516 WHERE event_id = '$this->id'
00517 ");
00518
00519 while ($guestAr = dbFetchAssoc($guestRs))
00520 $ret[$guestAr['user_id']] = $guestAr;
00521 }
00522 $this->guests = $ret;
00523 }
00524
00525 return $this->guests;
00526 }
00527
00533 public function getGuestCount($all = true)
00534 {
00535 return count($this->getGuests($all));
00536 }
00537
00541 public function initEditPage()
00542 {
00543 parent::initEditPage();
00544
00545 if (!$this->id)
00546 $this->pageTitle = 'Create New Event';
00547 else
00548 $this->pageTitle = 'Update Event';
00549
00550 $this->needsJs('lib/js/event.js');
00551 }
00552
00558 public function editFormAddFields($form)
00559 {
00560 global $me;
00561
00562 $this->addContentFields($form);
00563 $this->addLocationFields($form);
00564 $this->addPrivacyFields($form);
00565
00566 if (!$this->id)
00567 $form->addSubmit('Add Event');
00568 else
00569 $form->addSubmit('Save Event');
00570 }
00571
00577 public function addContentFields($form)
00578 {
00579 $form->add('TextField', 'name', array(
00580 "required" => true,
00581 "width" => "100%"
00582 ));
00583
00584 $form->add('DateField', 'start_date', array(
00585 'required' => true,
00586 'format' => 12
00587 ));
00588
00589 $form->add('DateField', 'end_date', array(
00590 "definition" => "Leave blank for 1 day events.",
00591 'format' => 12
00592 ));
00593
00594 $form->add('EditorField', 'description', array(
00595 "width" => '100%',
00596 "height" => '300px',
00597 ));
00598
00599 return $form;
00600 }
00601
00607 public function addLocationFields($form)
00608 {
00609 $form->add('TextField', 'location', array(
00610 "title" => "Location<br/>",
00611 "definition" => "My House, Bob's Place, Library, etc.",
00612 "size" => 50
00613 ));
00614
00615 $form->add('TextField', 'street', array(
00616 'size' => 45,
00617 'maxlength' => 255,
00618 'title' => 'Street Address'
00619 ));
00620 $form->add('CityStateZipField', 'citystzip', array(
00621 'title' => 'City / State / Zip'
00622 ));
00623
00624 return $form;
00625 }
00626
00632 public function addPrivacyFields($form)
00633 {
00634 $form->add('CheckboxField', 'allow_attendees', array(
00635 'value' => 1,
00636 'title' => 'Attendees',
00637 'label' => 'Allow people to add themselves to the list of attendees.'
00638 ));
00639
00640 $form->add('CheckboxField', 'invite_only', array(
00641 'title' => '',
00642 'label' => 'Only allow people who are on the list of guests to view the event.<br/>You can add guests once you create the event. (They still have to be family or friend.)',
00643 'id' => 'inviteOnly',
00644 'onclick' => 'doPrivacyCheck(this.checked)'
00645 ));
00646
00647 return $form;
00648 }
00649
00655 public function editFormLoad($form)
00656 {
00657 parent::editFormLoad($form);
00658
00659 if (!$this->id)
00660 {
00661 if ($this->params('date'))
00662 $form->setData(array(
00663 'start_date' => $this->params('date'),
00664 'end_date' => $this->params('date')
00665 ));
00666 else
00667 $form->setData(array(
00668 'start_date' => date('Y-m-d H:i:s'),
00669 'end_date' => date('Y-m-d H:i:s')
00670 ));
00671 }
00672 }
00673
00679 protected function editPagePostSuccess($form)
00680 {
00681 global $me;
00682
00683 if (!$this->isGuest($me->id))
00684 $this->addGuest($me->id, 'yes');
00685
00686 parent::editPagePostSuccess($form);
00687 }
00688
00695 public function addGuest($id, $commitment = 'yes')
00696 {
00697 dbExecute("
00698 INSERT INTO {$this->guestTable}
00699 (user_id, event_id, commitment)
00700 VALUES
00701 ('$id', '$this->id', '$commitment')
00702 ");
00703
00704 $this->guests[$id] = array();
00705 }
00706
00710 public function inviteGuest($id)
00711 {
00712
00713 if ($id)
00714 {
00715 $this->addGuest($id, 'maybe');
00716
00717 $user = new User($id);
00718 $this->emailInvite($user);
00719 }
00720 }
00721
00727 public function removeGuest($id)
00728 {
00729 dbExecute("
00730 DELETE FROM {$this->guestTable}
00731 WHERE user_id = '$id'
00732 AND event_id = '$this->id'
00733 ");
00734
00735 unset($this->guests[$id]);
00736 }
00737
00745 public function rsvp($id, $commitment, $guests)
00746 {
00747
00748 $guests = (int)$guests;
00749
00750
00751 if ($commitment == 'no')
00752 $guests = 0;
00753
00754 dbExecute("
00755 UPDATE {$this->guestTable} SET
00756 commitment = '$commitment',
00757 guests = '$guests'
00758 WHERE user_id = '$id'
00759 AND event_id = '$this->id'
00760 ");
00761 }
00762
00766 protected function emailInvite($user)
00767 {
00768 global $me;
00769
00770 if ($user->email_invites)
00771 {
00772 $subject = $me->getName() . " has invited you to an event.";
00773 $body = $me->getName() . " has recently invited you to an event on " . Config::get('site_name') . ".\n\n";
00774 $body .= "You can view the event, and RSVP with the link below:\n\n";
00775 $html .= $user->getTicket(strtolower(get_class($this)) . ".rsvp?id=$this->id") . "\n\n";
00776 $body .= "Thank you,\nThe Happy " . Config::get('site_name') . " Event Invitation Robot";
00777 $body .= "\n\nPS. You can turn these emails off at: " . $user->getUrl('.emailprefs', null, true);
00778
00779
00780 $html = $me->getName() . " has recently invited you to an event on " . Config::get('site_name') . ".<br/><br/>";
00781 $html .= "You can view the event, and RSVP ";
00782 $html .= $user->getTicket(strtolower(get_class($this)) . ".rsvp?id=$this->id", 'here.') . "<br/><br/>";
00783 $html .= "Thank you,<br/>The Happy " . Config::get('site_name') . " Event Invitation Robot";
00784 $html .= "<br/><br/>PS. You can turn these emails off " . $user->getLink(array('page' => 'emailprefs'), 'here.', null, true);
00785 $user->mail($subject, $body, $html);
00786 }
00787 }
00788
00792 public function initViewPage()
00793 {
00794 global $me;
00795
00796
00797 $this->id = $this->params('id');
00798
00799
00800 parent::initViewPage();
00801
00802
00803 $this->pageTitle = $this->getName();
00804
00805
00806 if ($this->invite_only && !$this->isGuest($me->id))
00807 throw new PageError('Only guests are allowed to view this event.');
00808 }
00809
00813 public function addInfoBox()
00814 {
00815 global $me;
00816
00817
00818 $s = "<p><b>Date:</b> " . $me->formatDate($this->start_date) . "</p>";
00819 if ($this->lat || $this->lon)
00820 {
00821 $s .= "<p><b>Location:</b>";
00822
00823
00824 if ($this->lat || $this->lon)
00825 {
00826 $s .= " (<a href=\"http://maps.google.com/maps?oi=map&q=";
00827 $s .= urlencode("$this->street, $this->city, $this->state $this->zip") . "\">map</a>)";
00828 }
00829
00830 $s .= "<br/>";
00831 if ($this->location)
00832 $s .= "$this->location<br/>";
00833 if ($this->street)
00834 $s .= "$this->street<br/>";
00835 if ($this->city)
00836 $s .= "$this->city, $this->state, $this->zip";
00837 $s .= "</p>";
00838 }
00839 $this->addBox(new BaseBox('Info', $s));
00840 }
00841
00845 public function addGuestBox()
00846 {
00847 $s = '';
00848 $yesCount = 0;
00849 $maybeCount = 0;
00850 if (count($this->guests))
00851 {
00852 $s .= "<table width=\"100%\">";
00853 $s .= "<tr><th>Name</th><th>RSVP</th><th>Guests</th></tr>";
00854 foreach ($this->guests AS $key => $val)
00855 {
00856 $guest = new User($key);
00857 $s .= "<tr><td>";
00858 $s .= $guest->getName(true);
00859
00860 if ($this->canUnInvite())
00861 $s .= " " . $this->getLink(".uninvite?id=$this->id&guest_id=$guest->id", "x");
00862
00863 $s .= "</td>";
00864
00865 if ($val['commitment'] == '')
00866 $commit = '???';
00867 else
00868 $commit = $val['commitment'];
00869
00870 if ($commit == 'yes')
00871 {
00872 $yesCount += $val['guests'];
00873 $yesCount++;
00874 }
00875 else if ($commit == 'maybe')
00876 {
00877 $maybeCount += $val['guests'];
00878 $maybeCount++;
00879 }
00880
00881 $s .= "<td>$commit</td>";
00882
00883 $s .= "<td align=\"center\">$val[guests]</td>";
00884 $s .= "</tr>";
00885 }
00886
00887 $totalCount = $yesCount + $maybeCount;
00888 $s .= "</table>";
00889
00890 $s .= "<p><b>$totalCount</b> total guests.<br/><b>$yesCount</b> yes and <b>$maybeCount</b> maybe.</p>";
00891 }
00892 else
00893 $s .= "No guests yet.<br/>";
00894
00895 $s .= "<p>";
00896 if ($this->canInvite())
00897 $nav[] = $this->getLink(".invite?id=$this->id", "Invite more guests.");
00898 if ($this->canAttend())
00899 $nav[] = $this->getLink(".attend?id=$this->id", "Add me to the guests.");
00900 if ($this->isGuest($me->id))
00901 $nav[] = $this->getLink(".rsvp?id=$this->id", "Update my RSVP.");
00902 if ($this->canLeave())
00903 $nav[] = $this->getLink(".leave?id=$this->id", "Remove me from the guests.");
00904 $s .= implode("<br/>", $nav);
00905 $s .= "</p>";
00906 $this->addBox(new BaseBox('Guests', $s));
00907 }
00908
00912 public function drawLine()
00913 {
00914 global $me;
00915
00916 echo $this->getLink(".view?id=$this->id", $this->name) . " on " . $me->formatDate($this->start_date) . ". ";
00917 echo "Added by " . $this->creator->getName(true) . ".";
00918 }
00919
00926 public function drawLocation($map = false)
00927 {
00928 if ($map)
00929 echo 'map';
00930 else
00931 echo $this->location;
00932 }
00933
00937 public function getRssItem()
00938 {
00939 global $me;
00940
00941 $rss = parent::getRssItem();
00942
00943 $rss->title = $this->name . " (" . $me->formatDate($this->start_date) . ")";
00944 $rss->description = $this->description;
00945
00946 return $rss;
00947 }
00948
00954 public function getPublicData()
00955 {
00956 global $me;
00957
00958 $data = parent::getPublicData();
00959
00960 $data['name'] = $this->getName();
00961 $data['description'] = $this->description;
00962 $data['start_date'] = $me->formatDate($this->start_date);
00963 $data['end_date'] = $me->formatDate($this->end_date);
00964 $data['location'] = $this->location;
00965 $data['street'] = $this->street;
00966 $data['city'] = $this->city;
00967 $data['state'] = $this->state;
00968 $data['zip'] = $this->zip;
00969 $data['guests'] = $this->getGuestCount();
00970
00971 return $data;
00972 }
00973
00977 public function getName($link = false)
00978 {
00979 if ($link)
00980 return $this->getLink(".view?id=$this->id", $this->name);
00981 return $this->name;
00982 }
00983
00989 public function getCreateFieldsArray()
00990 {
00991 $fields = parent::getCreateFieldsArray();
00992
00993 $fields['name'] = "name varchar(255) default '' not null";
00994 $fields['description'] = "description text default '' not null";
00995 $fields['location'] = "location varchar(255) default '' not null";
00996 $fields['start_date'] = "start_date date default '' not null";
00997 $fields['end_date'] = "end_date date default '' not null";
00998 $fields['allow_attendees'] = "allow_attendees tinyint(2) default '' not null";
00999
01000 return $fields;
01001 }
01002
01008 public function getCreateIndexesArray()
01009 {
01010 $fields = parent::getCreateIndexesArray();
01011
01012 $fields['start_date'] = "key(start_date)";
01013 $fields['end_date'] = "key(end_date)";
01014
01015 return $fields;
01016 }
01017 }
01018 ?>