PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/extensions/SocialProfile/UserGifts/SpecialGiveGift.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 382 lines | 318 code | 35 blank | 29 comment | 40 complexity | 312ad1395a6e60ed20d634ec5f7c6071 MD5 | raw file
  1. <?php
  2. /**
  3. * Special:GiveGift -- a special page for sending out user-to-user gifts
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. */
  8. class GiveGift extends SpecialPage {
  9. /**
  10. * Constructor
  11. */
  12. public function __construct() {
  13. parent::__construct( 'GiveGift' );
  14. }
  15. /**
  16. * Show the special page
  17. *
  18. * @param $par Mixed: parameter passed to the page or null
  19. */
  20. public function execute( $par ) {
  21. global $wgUser, $wgOut, $wgRequest, $wgMemc, $wgUploadPath, $wgUserGiftsScripts;
  22. $output = ''; // Prevent E_NOTICE
  23. $wgOut->addScriptFile( $wgUserGiftsScripts . '/UserGifts.js' );
  24. $wgOut->addExtensionStyle( $wgUserGiftsScripts . '/UserGifts.css' );
  25. $userTitle = Title::newFromDBkey( $wgRequest->getVal( 'user' ) );
  26. if ( !$userTitle ) {
  27. $wgOut->addHTML( $this->displayFormNoUser() );
  28. return false;
  29. }
  30. $user_title = Title::makeTitle( NS_USER, $wgRequest->getVal( 'user' ) );
  31. $this->user_name_to = $userTitle->getText();
  32. $this->user_id_to = User::idFromName( $this->user_name_to );
  33. $giftId = $wgRequest->getInt( 'gift_id' );
  34. $out = '';
  35. if ( $wgUser->getID() === $this->user_id_to ) {
  36. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  37. $out .= wfMsg( 'g-error-message-to-yourself' );
  38. $wgOut->addHTML( $out );
  39. } elseif ( $wgUser->isBlocked() ) {
  40. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  41. $out .= wfMsg( 'g-error-message-blocked' );
  42. $wgOut->addHTML( $out );
  43. } elseif ( $this->user_id_to == 0 ) {
  44. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  45. $wgOut->addHTML( wfMsg( 'g-error-message-no-user' ) );
  46. } elseif ( $wgUser->getID() == 0 ) {
  47. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  48. $out .= wfMsg( 'g-error-message-login' );
  49. $wgOut->addHTML( $out );
  50. } else {
  51. $gift = new UserGifts( $wgUser->getName() );
  52. if ( $wgRequest->wasPosted() && $_SESSION['alreadysubmitted'] == false ) {
  53. $_SESSION['alreadysubmitted'] = true;
  54. $ug_gift_id = $gift->sendGift(
  55. $this->user_name_to,
  56. $wgRequest->getInt( 'gift_id' ),
  57. 0,
  58. $wgRequest->getVal( 'message' )
  59. );
  60. // clear the cache for the user profile gifts for this user
  61. $wgMemc->delete( wfMemcKey( 'user', 'profile', 'gifts', $this->user_id_to ) );
  62. $key = wfMemcKey( 'gifts', 'unique', 4 );
  63. $data = $wgMemc->get( $key );
  64. // check to see if this type of gift is in the unique list
  65. $lastUniqueGifts = $data;
  66. $found = 1;
  67. if ( is_array( $lastUniqueGifts ) ) {
  68. foreach ( $lastUniqueGifts as $lastUniqueGift ) {
  69. if ( $wgRequest->getInt( 'gift_id' ) == $lastUniqueGift['gift_id'] ) {
  70. $found = 0;
  71. }
  72. }
  73. }
  74. if ( $found ) {
  75. // add new unique to array
  76. $lastUniqueGifts[] = array(
  77. 'id' => $ug_gift_id,
  78. 'gift_id' => $wgRequest->getInt( 'gift_id' )
  79. );
  80. // remove oldest value
  81. if ( count( $lastUniqueGifts ) > 4 ) {
  82. array_shift( $lastUniqueGifts );
  83. }
  84. // reset the cache
  85. $wgMemc->set( $key, $lastUniqueGifts );
  86. }
  87. $sent_gift = UserGifts::getUserGift( $ug_gift_id );
  88. $gift_image = '<img src="' . $wgUploadPath . '/awards/' .
  89. Gifts::getGiftImage( $sent_gift['gift_id'], 'l' ) .
  90. '" border="0" alt="" />';
  91. $output .= $wgOut->setPageTitle( wfMsg( 'g-sent-title', $this->user_name_to ) );
  92. $output .= '<div class="back-links">
  93. <a href="' . $user_title->escapeFullURL() . '">' .
  94. wfMsg( 'g-back-link', $this->user_name_to ) .
  95. '</a>
  96. </div>
  97. <div class="g-message">' .
  98. wfMsg( 'g-sent-message', $this->user_name_to ) .
  99. '</div>
  100. <div class="g-container">' .
  101. $gift_image .
  102. '<div class="g-title">' . $sent_gift['name'] . '</div>';
  103. if ( $sent_gift['message'] ) {
  104. $output .= '<div class="g-user-message">' .
  105. $sent_gift['message'] .
  106. '</div>';
  107. }
  108. $output .= '</div>
  109. <div class="cleared"></div>
  110. <div class="g-buttons">
  111. <input type="button" class="site-button" value="' . wfMsg( 'g-main-page' ) . '" size="20" onclick="window.location=\'index.php?title=' . wfMsgForContent( 'mainpage' ) . '\'" />
  112. <input type="button" class="site-button" value="' . wfMsg( 'g-your-profile' ) . '" size="20" onclick="window.location=\'' . $wgUser->getUserPage()->escapeFullURL() . '\'" />
  113. </div>';
  114. $wgOut->addHTML( $output );
  115. } else {
  116. $_SESSION['alreadysubmitted'] = false;
  117. if ( $giftId ) {
  118. $wgOut->addHTML( $this->displayFormSingle() );
  119. } else {
  120. $wgOut->addHTML( $this->displayFormAll() );
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * Display the form for sending out a single gift.
  127. * Relies on the gift_id URL parameter and bails out if it's not there.
  128. *
  129. * @return String: HTML
  130. */
  131. function displayFormSingle() {
  132. global $wgUser, $wgOut, $wgRequest, $wgUploadPath;
  133. $giftId = $wgRequest->getInt( 'gift_id' );
  134. if ( !$giftId || !is_numeric( $giftId ) ) {
  135. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  136. $wgOut->addHTML( wfMsg( 'g-error-message-invalid-link' ) );
  137. return false;
  138. }
  139. $gift = Gifts::getGift( $giftId );
  140. if ( empty( $gift ) ) {
  141. return false;
  142. }
  143. if ( $gift['access'] == 1 && $wgUser->getID() != $gift['creator_user_id'] ) {
  144. return $this->displayFormAll();
  145. }
  146. // Safe titles
  147. $user = Title::makeTitle( NS_USER, $this->user_name_to );
  148. $giveGiftLink = SpecialPage::getTitleFor( 'GiveGift' );
  149. $wgOut->setPageTitle( wfMsg( 'g-give-to-user-title', $gift['gift_name'], $this->user_name_to ) );
  150. $gift_image = "<img id=\"gift_image_{$gift['gift_id']}\" src=\"{$wgUploadPath}/awards/" .
  151. Gifts::getGiftImage( $gift['gift_id'], 'l' ) .
  152. '" border="0" alt="" />';
  153. $output = '<form action="" method="post" enctype="multipart/form-data" name="gift">
  154. <div class="g-message">' .
  155. wfMsg(
  156. 'g-give-to-user-message',
  157. $this->user_name_to,
  158. $giveGiftLink->escapeFullURL( 'user=' . $this->user_name_to )
  159. ) . "</div>
  160. <div id=\"give_gift_{$gift['gift_id']}\" class=\"g-container\">
  161. {$gift_image}
  162. <div class=\"g-title\">{$gift['gift_name']}</div>";
  163. if ( $gift['gift_description'] ) {
  164. $output .= '<div class="g-describe">' .
  165. $gift['gift_description'] .
  166. '</div>';
  167. }
  168. $output .= '</div>
  169. <div class="cleared"></div>
  170. <div class="g-add-message">' . wfMsg( 'g-add-message' ) . '</div>
  171. <textarea name="message" id="message" rows="4" cols="50"></textarea>
  172. <div class="g-buttons">
  173. <input type="hidden" name="gift_id" value="' . $giftId . '" />
  174. <input type="hidden" name="user_name" value="' . addslashes( $this->user_name_to ) . '" />
  175. <input type="button" class="site-button" value="' . wfMsg( 'g-send-gift' ) . '" size="20" onclick="document.gift.submit()" />
  176. <input type="button" class="site-button" value="' . wfMsg( 'g-cancel' ) . '" size="20" onclick="history.go(-1)" />
  177. </div>
  178. </form>';
  179. return $output;
  180. }
  181. /**
  182. * Display the form for giving out a gift to a user when there was no user
  183. * parameter in the URL.
  184. *
  185. * @return String: HTML
  186. */
  187. function displayFormNoUser() {
  188. global $wgUser, $wgOut, $wgFriendingEnabled;
  189. $wgOut->setPageTitle( wfMsg( 'g-give-no-user-title' ) );
  190. $output = '<form action="" method="get" enctype="multipart/form-data" name="gift">' .
  191. Html::hidden( 'title', $this->getTitle() ) .
  192. '<div class="g-message">' .
  193. wfMsg( 'g-give-no-user-message' ) .
  194. '</div>
  195. <div class="g-give-container">';
  196. // If friending is enabled, build a dropdown menu of the user's
  197. // friends
  198. if ( $wgFriendingEnabled ) {
  199. $rel = new UserRelationship( $wgUser->getName() );
  200. $friends = $rel->getRelationshipList( 1 );
  201. if ( $friends ) {
  202. $output .= '<div class="g-give-title">' .
  203. wfMsg( 'g-give-list-friends-title' ) .
  204. '</div>
  205. <div class="g-gift-select">
  206. <select onchange="javascript:chooseFriend(this.value)">
  207. <option value="#" selected="selected">' .
  208. wfMsg( 'g-select-a-friend' ) .
  209. '</option>';
  210. foreach ( $friends as $friend ) {
  211. $output .= '<option value="' . urlencode( $friend['user_name'] ) . '">' .
  212. $friend['user_name'] .
  213. '</option>' . "\n";
  214. }
  215. $output .= '</select>
  216. </div>
  217. <div class="g-give-separator">' .
  218. wfMsg( 'g-give-separator' ) .
  219. '</div>';
  220. }
  221. }
  222. $output .= '<div class="g-give-title">' .
  223. wfMsg( 'g-give-enter-friend-title' ) .
  224. '</div>
  225. <div class="g-give-textbox">
  226. <input type="text" width="85" name="user" value="" />
  227. <input class="site-button" type="button" value="' . wfMsg( 'g-give-gift' ) . '" onclick="document.gift.submit()" />
  228. </div>
  229. </div>
  230. </form>';
  231. return $output;
  232. }
  233. function displayFormAll() {
  234. global $wgUser, $wgOut, $wgRequest, $wgGiveGiftPerRow, $wgUploadPath;
  235. $user = Title::makeTitle( NS_USER, $this->user_name_to );
  236. $page = $wgRequest->getInt( 'page' );
  237. if ( !$page || !is_numeric( $page ) ) {
  238. $page = 1;
  239. }
  240. $per_page = 24;
  241. $per_row = $wgGiveGiftPerRow;
  242. if ( !$per_row ) {
  243. $per_row = 3;
  244. }
  245. $total = Gifts::getGiftCount();
  246. $gifts = Gifts::getGiftList( $per_page, $page, 'gift_name' );
  247. $output = '';
  248. if ( $gifts ) {
  249. $wgOut->setPageTitle( wfMsg( 'g-give-all-title', $this->user_name_to ) );
  250. $output .= '<div class="back-links">
  251. <a href="' . $user->escapeFullURL() . '">' .
  252. wfMsg( 'g-back-link', $this->user_name_to ) .
  253. '</a>
  254. </div>
  255. <div class="g-message">' .
  256. wfMsg( 'g-give-all', $this->user_name_to ) .
  257. '</div>
  258. <form action="" method="post" enctype="multipart/form-data" name="gift">';
  259. $x = 1;
  260. foreach ( $gifts as $gift ) {
  261. $gift_image = "<img id=\"gift_image_{$gift['id']}\" src=\"{$wgUploadPath}/awards/" .
  262. Gifts::getGiftImage( $gift['id'], 'l' ) .
  263. '" border="0" alt="" />';
  264. $output .= "<div onclick=\"selectGift({$gift['id']})\" onmouseover=\"highlightGift({$gift['id']})\" onmouseout=\"unHighlightGift({$gift['id']})\" id=\"give_gift_{$gift['id']}\" class=\"g-give-all\">
  265. {$gift_image}
  266. <div class=\"g-title g-blue\">{$gift['gift_name']}</div>";
  267. if ( $gift['gift_description'] ) {
  268. $output .= "<div class=\"g-describe\">{$gift['gift_description']}</div>";
  269. }
  270. $output .= '<div class="cleared"></div>
  271. </div>';
  272. if ( $x == count( $gifts ) || $x != 1 && $x % $per_row == 0 ) {
  273. $output .= '<div class="cleared"></div>';
  274. }
  275. $x++;
  276. }
  277. /**
  278. * Build next/prev nav
  279. */
  280. $giveGiftLink = SpecialPage::getTitleFor( 'GiveGift' );
  281. $numofpages = $total / $per_page;
  282. $user_safe = urlencode( $user->getText() );
  283. if ( $numofpages > 1 ) {
  284. $output .= '<div class="page-nav">';
  285. if ( $page > 1 ) {
  286. $output .= '<a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page - 1 ) ) . '">' .
  287. wfMsg( 'g-previous' ) . '</a> ';
  288. }
  289. if ( ( $total % $per_page ) != 0 ) {
  290. $numofpages++;
  291. }
  292. if ( $numofpages >= 9 ) {
  293. $numofpages = 9 + $page;
  294. }
  295. for ( $i = 1; $i <= $numofpages; $i++ ) {
  296. if ( $i == $page ) {
  297. $output .= ( $i . ' ' );
  298. } else {
  299. $output .= '<a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . $i ) . "\">$i</a> ";
  300. }
  301. }
  302. if ( ( $total - ( $per_page * $page ) ) > 0 ) {
  303. $output .= ' <a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page + 1 ) ) . '">' .
  304. wfMsg( 'g-next' ) . '</a>';
  305. }
  306. $output .= '</div>';
  307. }
  308. /**
  309. * Build next/prev nav
  310. */
  311. $output .= '<div class="g-give-all-message-title">' .
  312. wfMsg( 'g-give-all-message-title' ) .
  313. '</div>
  314. <textarea name="message" id="message" rows="4" cols="50"></textarea>
  315. <div class="g-buttons">
  316. <input type="hidden" name="gift_id" value="0" />
  317. <input type="hidden" name="user_name" value="' . addslashes( $this->user_name_to ) . '" />
  318. <input type="button" class="site-button" value="' . wfMsg( 'g-send-gift' ) . '" size="20" onclick="sendGift()" />
  319. <input type="button" class="site-button" value="' . wfMsg( 'g-cancel' ) . '" size="20" onclick="history.go(-1)" />
  320. </div>
  321. </form>';
  322. } else {
  323. $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
  324. $wgOut->addHTML( wfMsg( 'g-error-message-invalid-link' ) );
  325. }
  326. return $output;
  327. }
  328. }