Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

base-invitation.inc.php

Go to the documentation of this file.
00001 <?
00009 class BaseInvitation extends MyObject
00010 {
00014     public function __construct($id = null, $table = 'invites')
00015     {
00016         $this->hasAuthor = false;
00017 
00018         parent::__construct($id, $table);
00019     }
00020 
00026     public function getPagesXml()
00027     {
00028         $xml = parent::getPagesXml();
00029         $xml .= $this->getInvitePageXml();
00030         $xml .= $this->getApprovePageXml();
00031         $xml .= $this->getDenyPageXml();
00032 
00033         return $xml;
00034     }
00035 
00039     protected function getInvitePageXml()
00040     {
00041         return <<<XML
00042             <page name="invite">
00043             <param name="id" type="int" desc="id of the user you want to invite." required="1"/>
00044             </page>
00045 XML;
00046     }
00047 
00051     protected function getApprovePageXml()
00052     {
00053         return <<<XML
00054             <page name="approve">
00055             <param name="id" type="int" desc="id of the invite you're approving" required="1"/>
00056             </page>
00057 XML;
00058     }
00059 
00063     protected function getDenyPageXml()
00064     {
00065         return <<<XML
00066             <page name="deny">
00067             <param name="id" type="int" desc="id of the invite you're denying"/>
00068             </page>
00069 XML;
00070     }
00071 
00075     protected function getEditPageXml()
00076     {
00077         return '';
00078     }
00079 
00083     public function initMainPage()
00084     {
00085         $this->assertLogin();
00086         $this->pageTitle = 'Invite Friends';
00087     }
00088 
00092     public function drawMainPage()
00093     {
00094         echo "<p>You should override " . get_class($this) . "::drawMainPage() and show a form for searching / inviting new users.</p>";
00095     }
00096 
00100     public function drawRow()
00101     {
00102         echo "<div class=\"" . get_class($this) . "Row\">";
00103         $this->drawLine();
00104         echo "</div>";
00105     }
00106 
00110     public function drawLine()
00111     {
00112         $inviter = new User($this->inviter_id);
00113         $invitee = new User($this->invitee_id);
00114         
00115         //who invited?
00116         if ($inviter->isMe())
00117             echo 'You have ';
00118         else
00119             echo $inviter->getName(true) . " has ";
00120             
00121         echo "invited ";
00122         
00123         //who was invited?
00124         if ($invitee->isMe())
00125             echo " you to join $inviter->their " . $this->getGroup() . ". ";
00126         else
00127             echo $invitee->getName(true) . " to join your " . $this->getGroup() . ". ";
00128 
00129         //can they do anything?
00130         if ($invitee->isMe())
00131         {
00132             echo "You may " . $this->getApproveLink() . " or " . $this->getDenyLink();
00133             echo " $inviter->their request.";
00134         }
00135     }
00136 
00144     public function getApproveLink($text = 'approve')
00145     {
00146         return $this->getLink(".approve?id=$this->id", $text);
00147     }
00148 
00156     public function getDenyLink($text = 'deny')
00157     {
00158         return $this->getLink(".deny?id=$this->id", $text);
00159     }
00160 
00166     public function canDelete()
00167     {
00168         global $me;
00169 
00170         return ($me->isAdmin() || $this->inviter_id == $me->id);
00171     }
00172 
00178     public function canView()
00179     {
00180         global $me;
00181 
00182         return ($me->isAdmin() || $this->inviter_id == $me->id || $this->invitee_id == $me->id);
00183     }
00184 
00188     public function initViewPage()
00189     {
00190         parent::initViewPage();
00191 
00192         $this->pageTitle = 'Respond to Invitation';
00193 
00194         $this->assertLogin();
00195     }
00196 
00200     public function drawViewPage()
00201     {
00202         if ($this->canRespond())
00203             $this->drawLine();
00204         else
00205             echo "<p>There's nothing to see/do here, sorry.</p>";
00206     }
00207 
00211     public function initInvitePage()
00212     {
00213         $this->pageTitle = 'Invite Friend';
00214 
00215         $this->assertLogin();
00216     }
00217 
00221     public function drawInvitePage()
00222     {
00223         global $me;
00224 
00225         $user = new User($this->params('id'));
00226 
00227         $form = $this->createInviteForm($user);
00228         if ($form->isSubmitted())
00229         {
00230             $form->validate();
00231             $this->validateInvite($form);
00232 
00233             if (!$form->hasError())
00234                 $this->invitePost($form);
00235         }
00236         else
00237             $form->setData($this->params());
00238 
00239         //finally draw the form if needed
00240         if ($form->needsDrawn())
00241             $form->drawAll();
00242     }
00243 
00251     public function createInviteForm($user)
00252     {
00253         $form = new Form(new OneColumnTableLayout());
00254         $form->action = $this->getUrl(".invite?id=$user->id");
00255 
00256         $this->addInviteFormFields($form, $user);
00257 
00258         return $form;
00259     }
00260 
00267     protected function addInviteFormFields($form, $object)
00268     {
00269         $form->add('TextAreaField', 'note', array(
00270                     'title' => 'Note to ' . $object->getName(),
00271                     'width' => '100%',
00272                     'height' => '100px'
00273                     ));
00274         $form->addSubmit('Invite ' . $object->getName());
00275     }
00276 
00282     protected function validateInvite($form)
00283     {
00284         return true;
00285     }
00286 
00293     public function invite($inviteeId, $note = null)
00294     {
00295         global $me;
00296 
00297         //set our ids.
00298         $this->saveInvite($inviteeId);
00299 
00300         //email them
00301         $this->emailInvite($note);
00302     }
00303     
00310     protected function invitePost($form)
00311     {
00312         $this->invite($this->params('id'), $form->getData('note'));
00313     }
00314 
00320     protected function saveInvite($inviteeId)
00321     {
00322         global $me;
00323 
00324         //set our ids.
00325         $this->inviter_id = $me->id;
00326         $this->invitee_id = $inviteeId;
00327         $this->add_date = date("Y-m-d H:i:s");
00328 
00329         //save it!
00330         $this->save();
00331     }
00332 
00338     protected function emailInvite($note = null)
00339     {
00340         global $me;
00341 
00342         //get our data.
00343         $user = new User($this->invitee_id);
00344 
00345         //create our email.
00346         if ($user->email_invites)
00347         {
00348             $subject = "New Friend Invitation";
00349             $body = '';
00350             $html = '';
00351 
00352             //do our text email!
00353             if ($note)
00354                 $body .= $note . "\n\n" . "************************************************\n\n";
00355             $body .= $me->getName() . " ($me->email) has invited you to join $me->their " . $this->getGroup() . " on " . Config::get('site_name') . ".\n";
00356             $body .= "For your safety and privacy, only approve invitations from people you know.\n\n";
00357             $body .= "Visit the link below to process $me->their request.\n\n";
00358             $body .= $user->getTicket(strtolower(get_class($this)) . ".view?id=$this->id") . "\n\n";
00359             $body .= "Please note, you will not be connected until you approve the request.\n\n";
00360             $body .= "Thank you,\nThe Happy " . Config::get('site_name') . " Invitation Robot\n\n";
00361             $body .= "PS. You can turn these emails off at: " . $user->getUrl(".emailprefs", null, true);
00362 
00363             //do our html email!
00364             if ($note)
00365                 $html .= $note . "<br/><hr/><br/>";
00366             $html .= $me->getName(true) . " ($me->email) has invited you to join $me->their " . $this->getGroup() . " on " . Config::get('site_name') . ".<br/>";
00367             $html .= "For your safety and privacy, only approve invitations from people you know.<br/><br/>";
00368             $html .= $user->getTicket(strtolower(get_class($this)) . ".view?id=$this->id", "Click here") . " to process $me->their request.<br/><br/>";
00369             $html .= "Please note, you will <b>not</b> be connected until you approve the request.<br/><br/>";
00370 
00371             $html .= "Thank you,<br/>The Happy " . Config::get('site_name') . " Invitation Robot<br/><br/>";
00372             $html .= "PS. You can turn these emails off " . $user->getLink(".emailprefs", 'here.', null, true);
00373 
00374             //send it!
00375             $user->mail($subject, $body, $html);
00376         }
00377     }
00378 
00384     protected function getGroup()
00385     {
00386         return 'friends';
00387     }
00388 
00394     public function canRespond()
00395     {
00396         global $me;
00397 
00398         return ($me->id == $this->invitee_id);
00399     }
00400 
00404     public function initApprovePage()
00405     {
00406         global $me;
00407 
00408         //basic init stuff.
00409         $this->assertLogin();
00410         $this->id = $this->params('id');
00411 
00412         $this->pageTitle = 'Approve Invitation';
00413 
00414         //can they respond?
00415         if (!$this->canRespond())
00416             throw new PageError('You can only respond to your own invitations.');
00417     }
00418 
00422     public function drawApprovePage()
00423     {
00424         $this->doApprovePayload();
00425 
00426         $inviter = new User($this->inviter_id);
00427         echo "<p>You have approved " . $inviter->getName(true) . "'s request.</p>";
00428     }
00429 
00434     protected function doApprovePayload()
00435     {
00436         global $me;
00437 
00438         //delete our invite... we're done with it.
00439         $this->delete();
00440 
00441         //regenerate my tree...
00442         $me->createSession();
00443     }
00444 
00448     public function initDenyPage()
00449     {
00450         global $me;
00451 
00452         //basic init stuff.
00453         $this->assertLogin();
00454         $this->id = $this->params('id');
00455 
00456         $this->pageTitle = 'Deny Invitation';
00457 
00458         //can they respond?
00459         if (!$this->canRespond())
00460             throw new PageError('You can only respond to your own invitations.');
00461     }
00462 
00466     public function drawDenyPage()
00467     {
00468         $this->doDenyPayload();
00469 
00470         $inviter = new User($this->inviter_id);
00471         echo "<p>You have denied " . $inviter->getName(true) . "'s request.</p>";
00472     }
00473 
00478     protected function doDenyPayload()
00479     {
00480         global $me;
00481 
00482         //delete our invite... we're done with it.
00483         $this->delete();
00484 
00485         //regenerate my tree...
00486         $me->createSession();
00487     }
00488 
00494     public function getCreateFieldsArray()
00495     {
00496         //get daddy's fields.
00497         $fields = parent::getCreateFieldsArray();
00498 
00499         //no edit date
00500         unset($fields['edit_date']);
00501 
00502         //and user fields
00503         $fields["inviter_id"] = "inviter_id int(11) default 0 not null";
00504         $fields["invitee_id"] = "invitee_id int(11) default 0 not null";
00505 
00506         //send it back.
00507         return $fields;
00508     }
00509 
00515     public function getCreateIndexesArray()
00516     {
00517         //get daddy's keys.
00518         $keys = parent::getCreateIndexesArray();
00519 
00520         //no edit date
00521         unset($keys['edit_date']);
00522 
00523         //and user keys
00524         $keys["inviter_id"] = "KEY (inviter_id)";
00525         $keys["invitee_id"] = "KEY (invitee_id)";
00526 
00527         //send it back.
00528         return $keys;
00529     }
00530 }
00531 ?>

Generated on Fri Oct 27 12:26:40 2006 for BaseJumper by doxygen 1.3.9.1