Xem mẫu

CHAPTER 12 • EXTEND YOUR WEBSITE WITH RSS AND FACEBOOK 281 Figure 12-7. Adding GameNomad`s Video Games Tab to a Facebook Profile Configuring the Facebook Application to Support a Tab In the interests of reducing redundant code, we`re going to reuse the Facebook Controller`s profile view which comprises the tools we gave to the logged-in user. To do this, just set the Profile Tab URL value (Figure 12-8) within the application settings to http://apps.facebook.com/gamenomad/ games. Next, set the Profile Tab Name value to Video Games. Figure 12-8. Configuring the tab URL and name in the application settings Disabling the Authorization Requirement for Tab View Because any Facebook friend could conceivably view the Video Games tab, we need to figure out how to disable authorization when any user is viewing the Facebook controller`s games method. To do this, we`ll need to first modify the Facebook Controller`s init() method to forego authorization of the user when in tabbed mode. Doing so is surprisingly easy, because the Facebook platform will automatically set the $_POST[`fb_sig_in_profile_tab`] variable should a tab page be requested. Therefore all you have to do to forego authorization is check that variable. If not set, perform the authorization: $this->view->onTab = isset($_POST[`fb_sig_in_profile_tab`]); if (! $this->view->onTab) { $this->facebook->require_login(); } Rather than just create a local variable, the $onTab variable was set in the view context in order to later be able to reference it within the Facebook controller`s games view. I use the variable to display a special message introducing GameNomad and inviting users to join, as well as hide the logged-in user`s tool navigation menu. Enabling the Profile Tab Even when configured within the application`s settings interface, it`s not possible to automatically add a tab to the user`s profile. Instead, the user must explicitly enable it through the application settings in-terface. To access this interface, navigate to your applications menu by clicking on the Applications button at the bottom left of your Facebook page. From there, click on Edit, and in application listings page which appears, click on the Edit Settings link next to your application. In the window which pops up, click on the Profile tab (Figure 12-9). Download at Boykma.Com 282 CHAPTER 12 • EXTEND YOUR WEBSITE WITH RSS AND FACEBOOK Figure 12-9. Tab addition authorization takes place in the application settings interface Within this tab the user will first need to first specify the tab should be added to your profile page, and in the Privacy drop-down, he can set an appropriate visibility level, specifying the Video Games tab be visible to on the user, only select users (Custom), only friends, all friends and their friends, or all of the user`s networks and friends. Once added, the tab and its contents will be immediately visible to the allowed parties. Incidentally, when creating the application, it`s useful to set the Privacy setting to Custom, and select from your friends a list of fellow developers and other friends interested in testing the application. This way you can safely test the profile tab and its contents without opening up the application to oth-ers before it`s complete. Step #7. Sending a Facebook User Notification It might be useful to send notifications to a user`s Facebook friends who are also members of GameNomad whenever the user performs some sort of newsworthy action, such as adding a new game to his collection. An example of such a notification is shown in Figure 12-10. Figure 12-10. Notifying a user of a friend`s game collection addition To send these notifications, call the Facebook client`s notifications_send() method. For instance, the following call will send a notification about a new addition to Scott`s game collection to my Face-book account: $this->facebook->api_client->notifications_send(`501632489`, `Scott added Call of Duty: World at War (Xbox 360) to his collection`, `user_to_user`); To incorporate this feature, all you need to do is add the method call at the appropriate location. Download at Boykma.Com CHAPTER 12 • EXTEND YOUR WEBSITE WITH RSS AND FACEBOOK 283 Of course, sending out too many notifications could turn this informative feature into an annoying one, so consider reserving this feature for only the most important of updates. Facebook makes it possible for users to disable notifications altogether for a specific application through the notifications manager, and in cases of perceived abuse of the system, can identify an application as a spammer. Step #8. Adding Facebook Status Updates One of Facebook`s killer features is the status update, used to let friends know what`s currently going on in your life. These days, there seems to be no more effective way to keep tabs on what flavored coffees your network is drinking, or whose children are currently suffering a bout of pinkeye. Face-book`s status update feature can serve somewhat more practical purposes though, such as letting your network know about a new game you`re currently playing. Granting Permissions Because posting an update on a user`s behalf has such visible repercussions, the user must explicitly grant GameNomad permission to perform this task. GameNomad does this via the Facebook control-ler`s privacy method, but what`s so convenient about Facebook permissions is that we don`t have to bother with managing the permissions because Facebook will manage them for us. In fact, using a bit of FBML, we can dynamically include a link prompting the user to grant GameNomad permis-sion to post status updates. What`s particularly nice about using FBML is that if the user has already granted permission, Facebook will automatically override rendering the FBML! What`s more, using a Facebook client call, you can determine whether the user has already granted the permission, and if so display a message explaining how to disable the permission. The following listing demonstrates these concepts: 01 facebook->api_client->users_hasAppPermission(`status_update`)) { 03 ?> 04 05

06 You`ve granted permission to GameNomad to post status updates on your behalf. 07 To remove this permission, navigate to your GameNomad 08 application settings. 09

10 11 12 13 14 Grant permission for status updates 15 A breakdown of the above listing follows: • Line 02 determines whether the user has already granted the application permission to per-form status updates on the user`s behalf. This is just one of several available permissions; Download at Boykma.Com 284 CHAPTER 12 • EXTEND YOUR WEBSITE WITH RSS AND FACEBOOK check the Facebook documentation for more information. If the user has granted permission, lines 05-09 are output. Note this method doesn`t exist in the official client! Later in this sec-tion I`ll show you how to add it to the class. • Lines 13-15 display a link prompting the user to grant permission to GameNomad to perform status updates. If the user has already granted this permission, Facebook will conveniently forego displaying the link altogether! When the user decides to enable updates, he`ll be greeted with a prompt asking him to confirm this request (Figure 12-11). Figure 12-11. Enabling Status Updates If the user does later decide to disable status updates, he`ll have to navigate to http://www.facebook.com/editapps.php and edit GameNomad`s settings. The application`s set-tings window will appear (Figure 12-12). From there, he can click the Additional Permissions tab, and disable status updates. Figure 12-12. Disabling Status Updates Publishing Status Updates Oddly, the Facebook PHP client does not currently support the ability to post status updates. How-ever, modifying the client to support this ability is easy! Open up the facebookapi_php5_restlib. php file residing in the Facebook client directory, and add the following two methods at an appropri-ate location. /** * Sets a user`s Facebook status * * @param string $status The status update to post * @param boolean $clear Clear an existing message matching $status Download at Boykma.Com CHAPTER 12 • EXTEND YOUR WEBSITE WITH RSS AND FACEBOOK 285 * @return boolean */ public function users_setStatus($status,$clear) { return $this->call_method(`facebook.users.setStatus`, array(`status` => $status,`clear` => $clear)); } /** * Determines whether a user has granted a particular Facebook permission * * @param string $permission The permission in question * @return boolean */ public function users_hasAppPermission($permission) { return $this->call_method(`facebook.users.hasAppPermission`, array(`ext_perm` => $permission)); } The code comments should be suffice to explain the purpose of each new method. So how do you subsequently send an update to the user`s Facebook profile? The following example shows how: if (! $this->facebook->api_client->users_hasAppPermission(`status_update`)) { $this->facebook->api_client->users_setStatus(`added Halo 3 to his game collection.`, FALSE); } Step #9. Deploying Your Facebook Application You`ve thoroughly tested your new application, braced your Web server for the onslaught of new users, and told your family they won`t be seeing you for a few days. It must be application launch time! Launching your Facebook application is a very easy process; just navigate to http://www. facebook.com/developers/, choose the application you`d like to launch under the "My Applica-tion" section, and click the submit button! Before the application is added to the Facebook directory though, you`re required to provide five pieces of information if you haven`t already done so, including the application name, contact e-mail address, a short application description, logo, and whether the application uses Facebook`s mobile platform. Furthermore, your application must have at least five users before Facebook will accept it. I`d imagine this provides at least a modicum of proof that you`re serious about the application, and have recruited friends and fellow developers to thoroughly test the project before launching it. Once launched, it`s time to get the word out! Be sure to update your Facebook status to point your Facebook network to the new venture/feature, not to mention let your existing website community know about the new feature. Ask your friends and colleagues to mention the application on their respective Facebook networks. Before you know it, the users will be streaming in! Download at Boykma.Com ... - tailieumienphi.vn