PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/facebook/pages/pageInvite.class.php

http://github.com/newscloud/open-social-media-toolkit
PHP | 162 lines | 102 code | 35 blank | 25 comment | 19 complexity | 6cd695767a4077c312cda8f1992c62f2 MD5 | raw file
  1. <?php
  2. class pageInvite {
  3. var $page;
  4. var $db;
  5. var $session;
  6. var $facebook;
  7. var $templateObj;
  8. function __construct(&$page) {
  9. $this->page=&$page;
  10. $this->db=&$page->db;
  11. $this->facebook=&$page->facebook;
  12. $this->session=&$page->session;
  13. $this->setupLibraries();
  14. }
  15. function setupLibraries() {
  16. require_once(PATH_CORE.'/classes/template.class.php');
  17. $this->templateObj=new template($this->db);
  18. $this->templateObj->registerTemplates(MODULE_ACTIVE,'invite');
  19. }
  20. function fetch() {
  21. // this page requires a session, so a user must be logged in. die if not logged in
  22. // note: these are here for back up, pages.class.php should take care of this
  23. if ($this->page->isAjax) {
  24. $this->facebook=&$this->page->app->loadFacebookLibrary();
  25. }
  26. $inside=$this->buildInvitePage();
  27. if ($this->page->isAjax) return $inside;
  28. $code=$this->page->constructPage('invite',$inside);
  29. // Daniel - not sure if this should be here
  30. //$code.= $this->checkAddNewStreetTeamUser();
  31. return $code;
  32. }
  33. function buildInvitePage() {
  34. // not tested since dropped into invite.class.php
  35. // REQUIRES Facebook session
  36. // some code taken from http://wiki.developers.facebook.com/index.php/Fb:request-forms
  37. /*echo "<pre>";
  38. echo '$this->facebook:'; print_r($this->facebook);
  39. echo '$_GET:'; print_r( $_GET);
  40. echo "</pre>";
  41. */
  42. require_once(PATH_CORE .'/classes/user.class.php');
  43. $userid = $this->session->userid;
  44. $fbId = $this->session->fbId;
  45. if (isset($_GET['submit'])) {
  46. // Process posted invitation ids
  47. if (count($_POST['ids'])>0) {
  48. $result=$this->templateObj->buildFacebookUserList('',$_POST['ids']);
  49. $result.='<h1>Nice work!</h1><h5>What do you want to do now?</h5><div class="bullet_list"><ul>'.
  50. '<li><a href="?p=home" onclick="switchPage(\'home\');return false;">Go to the home page</a>?</li>'.
  51. '<li><a href="?p=team" onclick="switchPage(\'team\');return false;">Visit the '.SITE_TEAM_TITLE.'</a>?</li>'.
  52. '</ul></div><!--end "bullet_list"-->';
  53. $code.=$this->page->buildMessage('success','Your invitations have been sent',$result);
  54. // Insert Invitations into table
  55. $inviteTable = new UserInviteTable($this->db);
  56. $invite = $inviteTable->getRowObject();
  57. $invite->userid = $userid;
  58. $invite->dateInvited = date('Y-m-d H:i:s', time());
  59. foreach ($_POST['ids'] as $id)
  60. {
  61. $invite->friendFbId = $id;
  62. $inviteid=UserInviteTable::checkExists($userid,$id);
  63. if ($inviteid===false)
  64. $invite->insert();
  65. else {
  66. $invite->id=$inviteid;
  67. // date will be updated
  68. $invite->update();
  69. }
  70. //$this->session->ui->cachedFriendsInvited++; // now done through log
  71. $log = $this->page->app->getActivityLog();
  72. $log->update($log->serialize(0, $this->session->userid, 'invite', $invite->friendFbId, 0)); // using itemid since userid2 implies the type is a userid, which it isnt
  73. }
  74. //$this->session->ui->update();
  75. } else {
  76. $code.=$this->page->buildMessage('error','Problem sending invitations','We encountered a problem sending your invitations. <a href="?p=invite" onclick="switchPage(\'invite\');return false;">Please try again</a>.');
  77. }
  78. } else {
  79. // Exclude users who have added the application already
  80. // TODO: do i use the fbApp action db for invitations?
  81. // Exclude users invited in the last 14 days by this user
  82. $userid = $this->session->userid;
  83. $inviteInterval = 3600*24*14; // 2 weeks in seconds
  84. $debug = false;
  85. $userInfoTable = new UserInfoTable($this->db);
  86. require_once(PATH_CORE .'/classes/user.class.php');
  87. if ($debug) echo 'session userinfo: <pre>'.print_r($this->session->ui, true). '</pre>';
  88. $allFriends = explode(',',$this->session->ui->friends);
  89. if ($debug) echo 'session memberFriends: <pre>'.print_r($this->session->ui->memberFriends, true). '</pre>';
  90. $memberFriends = explode(',',$this->session->ui->memberFriends); // now cached
  91. if ($debug) echo 'memberFriends uids: <pre>'.print_r($memberFriends, true). '</pre>';
  92. $memberFriends = $userInfoTable->getFbIdsForUsers($memberFriends);
  93. if ($debug) echo 'memberFriends: <pre>'.print_r($memberFriends, true). '</pre>';
  94. //$invitedFriends = array();
  95. $invitedFriends = UserInviteTable::getRecentlyInvitedFriends($this->db, $userid, $inviteInterval);
  96. if ($debug) echo 'invitedFriends: <pre>'.print_r($invitedFriends, true). '</pre>';
  97. if (is_null($invitedFriends)) $invitedFriends = array();
  98. if (is_null($memberFriends)) $memberFriends = array();
  99. $excludedFriends = array_merge($invitedFriends,$memberFriends);
  100. $excludedFriends = array_unique($excludedFriends);
  101. if ($debug) echo 'excludedFriends: <pre>'.print_r($excludedFriends, true). '</pre>';
  102. $cntExcludedFriends =count($encludedFriends);
  103. //$allFriends=$this->facebook->api_client->friends_get();
  104. if (count($allFriends)<=$cntExcludedFriends)
  105. {
  106. //all friends are signed up or invited already
  107. $code.='<h2>All your friends currently have the '.SITE_TITLE.' application added or have been invited within the past two weeks.</h2><p>Thank you for supporting the '.SITE_TITLE.'.</p>';
  108. $code.='<p><a href="?p=home">Continue to home page</a></p>';
  109. } else {
  110. // Construct a next url for referrals
  111. $nextUrl=$this->facebook->get_add_url("referfbid=".$fbId."&referid=".$userid."&viaInvite");
  112. //$this->db->log($nextUrl);
  113. $inviteText=$this->templateObj->templates['inviteText'] .
  114. "<fb:req-choice url=\"".$nextUrl."\" label=\"Add ".SITE_TITLE."!\" />"; // need to have this local jeff!
  115. $actionStr=$this->templateObj->templates['actionStr'];
  116. $code.='<fb:request-form action="?p=invite&c=skipped&submit" method="POST" invite="true" type="'.SITE_TITLE.'" content="'.htmlentities($inviteText).'">';
  117. if (is_numeric($this->page->app->notifications_per_day))
  118. $maxRequests=$this->page->app->notifications_per_day;
  119. else
  120. $maxRequests=20;
  121. $code.='<fb:multi-friend-selector rows="5" max="'.$maxRequests.'" exclude_ids="'.join(',',$excludedFriends).'" showborder="false" actiontext="'.$actionStr.'"> </fb:request-form>';
  122. }
  123. }
  124. return $code;
  125. }
  126. }
  127. ?>