Booosta Tutorial

Facebook Example

lib/framework.incl.php: <?php $DEBUG_MODE = false; $BACKUPMODE = false; include_once 'db_mysql.incl.php'; include_once 'dbconnect.incl.php'; include_once 'templateparser.incl.php'; include_once 'ajax.incl.php'; include_once 'schedulefinder.incl.php'; include_once 'gui.incl.php'; ############ do not change anything beyond this #################### require 'tableclass.incl.php'; eval($ds_); require 'staticclass.incl.php'; ?> lib/schedulefinder.incl.php: <?php include_once 'facebookapp.incl.php'; class ScheduleFinder extends FacebookApp { protected $USERID; public function __construct() { parent::__construct(); $this->USERID = query_value("select id from useraccount where username='$this->FB_USERID'"); if($this->USERID == ''): $obj = new C_Useraccount(); $obj->set('username', $this->FB_USERID); $obj->set('privileges', array()); $user = $this->fb->api("/$this->FB_USERID"); $obj->set('settings', array('firstname' => $user['first_name'], 'surname' => $user['last_name'])); $this->USERID = $obj->insert(); endif; } } ?> index.php: <?php include_once 'lib/framework.incl.php'; class App extends ScheduleFinder { protected $name = 'event'; public function __construct() { parent::__construct(); $this->hide_fields('id,owner,user_invites,user_add_dates'); $this->set_header(array('name'=>'Name', 'description'=>'Description', 'location'=>'Location')); } protected function before_add_($data, &$obj) { $obj->set('owner', query_value("select id from useraccount where username='$this->FB_USERID'")); if($data['user_add_dates']) $obj->set('user_add_dates', 1); else $obj->set('user_add_dates', 0); if($data['user_invites']) $obj->set('user_invites', 1); else $obj->set('user_invites', 0); $addvars = 'name,description,location,user_add_dates,user_invites'; $this->set_addvars($addvars); } protected function before_edit_($id, $data, &$obj) { if($obj->get('owner') == $this->USERID): $editvars = 'name,description,location,user_add_dates,user_invites,newdate1,newdate2,newdate3,newdate4,newdate5,' . 'newtime1,newtime2,newtime3,newtime4,newtime5'; if($data['user_add_dates']) $obj->set('user_add_dates', 1); else $obj->set('user_add_dates', 0); if($data['user_invites']) $obj->set('user_invites', 1); else $obj->set('user_invites', 0); elseif($obj->get('user_add_dates')): $editvars = 'newdate1,newdate2,newdate3,newdate4,newdate5,newtime1,newtime2,newtime3,newtime4,newtime5'; else: $editvars = ''; endif; $this->set_editvars($editvars); } protected function after_edit_($id, $data) { for($i = 1; $i <=5; $i++): if($data["newdate$i"]): $obj = new C_Dates(); $obj->set('event', $id); $obj->set('dtime', $data["newdate$i"] . ' ' . $data["newhour$i"] . ':' . $data["newminute$i"]); $obj->insert(); endif; endfor; } protected function after_action_edit() { $result = ''; $this->TPL['is_owner'] = (query_value("select owner from event where id='$this->id'") == $this->USERID); $objs = C_Dates::get_objects("event='$this->id'"); foreach($objs as $obj): if($this->TPL['is_owner']) $link = '{LINK <img~src="tpl/delete.png"> index.php?action=deletedate&id=' . $obj->get('id') . '}'; else $link = ''; $result .= '<tr><td>' . $obj->get('dtime') . "</td><td>$link</td></tr>"; endforeach; $this->TPL['current_dates'] = $result; } protected function action_deletedate() { $obj = C_Dates::get_object($this->id); $obj->delete(); $this->backpage = $this->phpself; } protected function default_clause() { $invitations = query_value_set("select event from invitation where invitee='$this->USERID'"); $ownevents = query_value_set("select id from event where owner='$this->USERID'"); $events = array_merge($invitations, $ownevents); if(sizeof($events)) return 'id in (' . implode(',', $events) . ')'; return '0'; // do not display any events } protected function in_default_makelist(&$list) { $list->set_links(array('vote' => "$this->phpself?action=vote&eventid={"."$this->idfield}", 'invite' => "askfriends.php?event={"."$this->idfield}", 'edit' => "$this->phpself?action=edit&id={"."$this->idfield}", 'delete' => "$this->phpself?action=delete&id={"."$this->idfield}")); $list->add_condition('delete', "{owner} == '$this->USERID'"); $list->add_condition('edit', "{owner} == '$this->USERID' || {user_add_dates} == '1'"); $list->add_condition('invite', "{owner} == '$this->USERID' || {user_invites} == '1'"); $extrafields = array('vote' => new GuiTooltip('vote#', 'Vote for a schedule', 'tpl', 'vote.png'), 'invite' => new GuiTooltip('invite#', 'Invite friends to vote', 'tpl', 'invite.png'), 'edit' => new GuiTooltip('edit#', 'Edit this event', 'tpl', 'edit.png'), 'delete' => new GuiTooltip('delete#', 'Delete this event', 'tpl', 'delete.png')); $list->set_extrafields($extrafields); $this->TPL['js'] .= $this->get_invite_js(); $this->TPL['js'] .= Gui::get_html_includes('lib'); } protected function action_vote() { $dates = query_value_set("select id from dates where event='".$this->VAR['eventid']."'"); $voters = query_value_set("select distinct u.id from useraccount u, votelist v where u.id=v.useraccount and v.event='".$this->VAR['eventid']."'"); $result = '<tr><th colspan="2">Voter<th>'; foreach($dates as $date): $ddate = query_value("select dtime from dates where id='$date'"); $ddate = str_replace(' ', '<br>', $ddate); $result .= "<th>$ddate<th>"; endforeach; $result .= "<tr><tr><td><img src='https://graph.facebook.com/$this->FB_USERID/picture'/><td><td>My Vote<td>"; foreach($dates as $date): $has_voted = query_value("select count(*) from vote where useraccount='$this->USERID' and ddate='$date'"); $img = ($has_voted ? 'yes.png' : 'no.png'); $result .= "<td>{IMG tpl/$img onClick::request_vote($date); id::voteimg$date}<td>"; endforeach; $result .= '<tr>'; foreach($voters as $voter): if($voter == $this->USERID) continue; // do not display myself again $obj = C_Useraccount::get_object($voter); $username = $obj->get('username'); $settings = $obj->get('settings'); $result .= "<tr class='vote'><td><img src='https://graph.facebook.com/$username/picture' /></td><td>$settings[firstname] $settings[surname]</td>"; foreach($dates as $date): $has_voted = query_value("select count(*) from vote where useraccount='$voter' and ddate='$date'"); $img = ($has_voted ? 'yes.png' : 'no.png'); $result .= "<td>{IMG tpl/$img}</td>"; endforeach; $result .= '</tr>'; endforeach; $result .= '<tr class="gesamt"><td colspan="2">Summary:</td>'; foreach($dates as $date) $result .= "<td id='sum$date'>" . query_value("select count(*) from vote where ddate='$date'") . '</td>'; $result .= '</tr>'; $ajax = new Ajax('vote', 'result'); $this->TPL['js'] = $ajax->get_javascript(); $this->TPL['votelist'] = $result; $this->maintpl = 'tpl/votes.tpl'; } protected function action_votedo() { $was_voted = query_value("select count(*) from vote where useraccount='$this->USERID' and ddate='{$this->VAR['dateid']}'"); if($was_voted) query("delete from vote where useraccount='$this->USERID' and ddate='{$this->VAR['dateid']}'"); else query("insert into vote (useraccount, ddate) values ('$this->USERID', '{$this->VAR['dateid']}')"); $image = ($was_voted ? 'no.png' : 'yes.png'); Ajax::print_response('result', $image); $this->maintpl = null; } protected function action_imprint() { $this->maintpl = 'tpl/imprint.tpl'; } }; $app = new App(); $app->run(); ?> askfriends.php: <?php include_once 'lib/framework.incl.php'; class App extends ScheduleFinder { protected $name = 'askfriends'; protected function action_default() { $friendlist = $this->get_friendids(); $userlist = query_value_set('select username from useraccount'); $frienduser = array_intersect($friendlist, $userlist); $usernames = $this->get_friendnames(); $invitees = query_value_set("select invitee from invitation where event='{$this->VAR['event']}'"); foreach($frienduser as $user): $userid = query_value("select id from useraccount where username='$user'"); if(in_array($userid, $invitees)) $check = 'checked'; else $check = ''; $list .= "$usernames[$user]{CHECKBOX invite_$userid $check}"; endforeach; $this->TPL['liste'] = $list; $this->TPL['js'] = $this->get_invite_js(); $this->maintpl = 'tpl/askfriends_default.tpl'; } protected function action_invitedo() { $invitee_list = array(); foreach($this->VAR as $var=>$val) if(substr($var, 0, 7) == 'invite_' && $val): list($dummy, $invitee) = explode('_', $var); $invitee_list[] = $invitee; $invited_before = query_value("select count(*) from invitation where event='{$this->VAR['event']}' and invitee='$invitee'"); if(!$invited_before): // do all the stuff to invite user to vote for this event $obj = new C_Invitation(); $obj->set('event', $this->VAR['event']); $obj->set('invitor', $this->USERID); $obj->set('invitee', $invitee); $obj->insert(); // send invitee a facebook message to tell him he is invited to vote $invitee_fbid = query_value("select username from useraccount where id='$invitee'"); $eventname = query_value("select name from event where id='{$this->VAR['event']}'"); $message = 'I have invited you to vote on a schedule on Schedule Finder!'; $data = array('name' => 'Vote on a schedule', 'message' => $message, 'link' => 'https://apps.facebook.com/at_epb_datefinder/?action=vote&eventid=3', 'description' => "You are invited to vote for a schedule on the event '$eventname'."); if($invitee_fbid) $this->post_to_wall($data, $invitee_fbid); endif; endif; // if we have invited the invitee // we can remove him from the list if he has not voted yet $invitations = query_arrays("select id, invitor, invitee from invitation where event='{$this->VAR['event']}'"); foreach($invitations as $invitation) if($invitation['invitor'] == $this->USERID && !in_array($invitation['invitee'], $invitee_list)): if(query_value("select count(*) from votelist where event='$this->VAR[event]' and useraccount='$invitation[invitee]'") == 0): $obj = C_Invitation::get_object($invitation['id']); $obj->delete(); endif; endif; $this->backpage = 'index.php'; } } $app = new App(); $app->run(); ?> tpl/askfriends_default.tpl: {%js} <h1>Ask Friends to vote</h1> {FORMSTART askfriends.php} {HIDDEN event {%event}} {HIDDEN action invitedo} <table> <tr><th>Name</th><th>Invite</th></tr> {%liste} </table> {FORMSUBMIT} {FORMEND} <br><br> Any friends missing? Probably they don't have this app installed! {LINK Invite~them! javascript:invite_friends()} <br> {LINK Home index.php} tpl/event_default.tpl: {%js} <h1>Events</h1> {LINK New~Event ?action=new} {%liste} <br><br> {LINK Invite~Friends javascript:invite_friends()} tpl/event_edit.tpl: <h1>Edit Event</h1> {FORMSTART #} {HIDDEN action editdo} <table> %if({%is_owner}): <tr><td>Name:</td><td>{TEXT name {%~name}}</td></tr> <tr><td>Description:</td><td>{TEXTAREA description 30 5 {%description}}</td></tr> <tr><td>Locaction:</td><td>{TEXT location {%~location}}</td></tr> <tr><td>Others can add dates:</td><td>{CHECKBOX user_add_dates {%user_add_dates}}</td></tr> <tr><td>Others can invite users:</td><td>{CHECKBOX user_invites {%user_invites}}</td></tr> %else: <tr><td>Name:</td><td>{%name}</td></tr> <tr><td>Description:</td><td>{%description}</td></tr> <tr><td>Locaction:</td><td>{%location}</td></tr> <tr><td>Others can add dates:</td><td>{%user_add_dates}</td></tr> <tr><td>Others can invite users:</td><td>{%user_invites}</td></tr> %endif; <tr><td><b>Dates:</b></td><td> </td></tr> {%current_dates} {DATEINIT} <tr><td>New Date:</td><td>{DATE newdate1}{NUMBERSEL newhour1 23 0 0}:{NUMBERSEL newminute1 59 0 0}</td></tr> <tr><td>New Date:</td><td>{DATE newdate2}{NUMBERSEL newhour2 23 0 0}:{NUMBERSEL newminute2 59 0 0}</td></tr> <tr><td>New Date:</td><td>{DATE newdate3}{NUMBERSEL newhour3 23 0 0}:{NUMBERSEL newminute3 59 0 0}</td></tr> <tr><td>New Date:</td><td>{DATE newdate4}{NUMBERSEL newhour4 23 0 0}:{NUMBERSEL newminute4 59 0 0}</td></tr> <tr><td>New Date:</td><td>{DATE newdate5}{NUMBERSEL newhour5 23 0 0}:{NUMBERSEL newminute5 59 0 0}</td></tr> </table> {FORMSUBMIT} {FORMEND} <br><br> {LINK Home index.php} tpl/event_new.tpl: <h1>Create New Event</h1> {FORMSTART #} {HIDDEN action newdo} <table> <tr><td>Name:</td><td>{TEXT name}</td></tr> <tr><td>Description:</td><td>{TEXTAREA description 30 5}</td></tr> <tr><td>Locaction:</td><td>{TEXT location}</td></tr> <tr><td>Others can add dates:</td><td>{CHECKBOX user_add_dates}</td></tr> <tr><td>Others can invite users:</td><td>{CHECKBOX user_invites}</td></tr> </table> {FORMSUBMIT} {FORMEND} tpl/votes.tpl: {%js} <script type='text/javascript'> var lastdateid; function request_vote_(dateid) { url_vote = 'index.php?action=votedo&eventid={%eventid}&dateid=' + dateid; lastdateid = dateid; } function response_vote_() { var image = document.getElementById('voteimg' + lastdateid); image.src = 'tpl/' + xml_result_vote; var sum = document.getElementById('sum' + lastdateid); var count = parseInt(sum.innerHTML); if(xml_result_vote == 'yes.png') count = count + 1; else count = count - 1; sum.innerHTML = count; } </script> <h1>All Votes</h1> <table> {%votelist} </table> <br><br> {LINK Home index.php} Database definition: -- -- Tabellenstruktur für Tabelle 'dates' -- CREATE TABLE dates ( id int(11) NOT NULL auto_increment, event int(11) NOT NULL, dtime datetime NOT NULL, `comment` text collate utf8_unicode_ci NOT NULL, PRIMARY KEY (id), KEY event (event) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -------------------------------------------------------- -- -- Tabellenstruktur für Tabelle 'event' -- CREATE TABLE event ( id int(11) NOT NULL auto_increment, owner int(11) NOT NULL, `name` varchar(255) NOT NULL, description text NOT NULL, location varchar(255) NOT NULL, user_add_dates tinyint(1) NOT NULL, user_invites tinyint(1) NOT NULL, PRIMARY KEY (id), KEY owner (owner) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -------------------------------------------------------- -- -- Tabellenstruktur für Tabelle 'invitation' -- CREATE TABLE invitation ( id int(11) NOT NULL auto_increment, event int(11) NOT NULL, invitor int(11) NOT NULL, invitee int(11) NOT NULL, PRIMARY KEY (id), KEY invitor (invitor), KEY invitee (invitee), KEY event (event) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -------------------------------------------------------- -- -- Tabellenstruktur für Tabelle 'useraccount' -- CREATE TABLE useraccount ( id int(11) NOT NULL auto_increment, username varchar(255) collate utf8_unicode_ci NOT NULL, `password` varchar(255) collate utf8_unicode_ci NOT NULL, `privileges` text collate utf8_unicode_ci NOT NULL, facebook tinyint(1) NOT NULL default '1', settings text collate utf8_unicode_ci NOT NULL, PRIMARY KEY (id), UNIQUE KEY username (username) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- -------------------------------------------------------- -- -- Tabellenstruktur für Tabelle 'vote' -- CREATE TABLE vote ( id int(11) NOT NULL auto_increment, ddate int(11) NOT NULL, useraccount int(11) NOT NULL, `comment` text collate utf8_unicode_ci NOT NULL, PRIMARY KEY (id), KEY vote (ddate), KEY useraccount (useraccount) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; CREATE ALGORITHM=UNDEFINED DEFINER=root@localhost SQL SECURITY DEFINER VIEW votelist AS select v.id AS id,v.ddate AS ddate,v.useraccount AS useraccount,v.`comment` AS `comment`, d.event AS event from (vote v join dates d) where (v.ddate = d.id); -- -- Constraints der exportierten Tabellen -- -- -- Constraints der Tabelle `dates` -- ALTER TABLE `dates` ADD CONSTRAINT dates_ibfk_1 FOREIGN KEY (event) REFERENCES event (id) ON DELETE CASCADE ON UPDATE CASCADE; -- -- Constraints der Tabelle `event` -- ALTER TABLE `event` ADD CONSTRAINT event_ibfk_1 FOREIGN KEY (owner) REFERENCES useraccount (id) ON DELETE CASCADE ON UPDATE CASCADE; -- -- Constraints der Tabelle `invitation` -- ALTER TABLE `invitation` ADD CONSTRAINT invitation_ibfk_3 FOREIGN KEY (invitee) REFERENCES useraccount (id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT invitation_ibfk_1 FOREIGN KEY (event) REFERENCES event (id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT invitation_ibfk_2 FOREIGN KEY (invitor) REFERENCES useraccount (id) ON UPDATE CASCADE; -- -- Constraints der Tabelle `vote` -- ALTER TABLE `vote` ADD CONSTRAINT vote_ibfk_2 FOREIGN KEY (useraccount) REFERENCES useraccount (id) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT vote_ibfk_3 FOREIGN KEY (ddate) REFERENCES dates (id) ON DELETE CASCADE ON UPDATE CASCADE; There are also some static template files like HTML and images which are not of interest for this tutorial and are omitted here.