PageRenderTime 223ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/SocialProfile/UserRelationship/SpecialViewRelationships.php

https://gitlab.com/link233/bootmw
PHP | 312 lines | 223 code | 40 blank | 49 comment | 41 complexity | 8599edb8e8acdd4d01ffe74008577c5c MD5 | raw file
  1. <?php
  2. /**
  3. * A special page for viewing all relationships by type
  4. * Example URL: index.php?title=Special:ViewRelationships&user=Pean&rel_type=1 (viewing friends)
  5. * Example URL: index.php?title=Special:ViewRelationships&user=Pean&rel_type=2 (viewing foes)
  6. *
  7. * @file
  8. * @ingroup Extensions
  9. * @author David Pean <david.pean@gmail.com>
  10. * @copyright Copyright © 2007, Wikia Inc.
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  12. */
  13. class SpecialViewRelationships extends SpecialPage {
  14. /**
  15. * Constructor -- set up the new special page
  16. */
  17. public function __construct() {
  18. parent::__construct( 'ViewRelationships' );
  19. }
  20. /**
  21. * Group this special page under the correct header in Special:SpecialPages.
  22. *
  23. * @return string
  24. */
  25. function getGroupName() {
  26. return 'users';
  27. }
  28. /**
  29. * Show the special page
  30. *
  31. * @param $params Mixed: parameter(s) passed to the page or null
  32. */
  33. public function execute( $params ) {
  34. $lang = $this->getLanguage();
  35. $out = $this->getOutput();
  36. $request = $this->getRequest();
  37. $user = $this->getUser();
  38. // Set the page title, robot policies, etc.
  39. $this->setHeaders();
  40. // Add CSS
  41. $out->addModuleStyles( array(
  42. 'ext.socialprofile.clearfix',
  43. 'ext.socialprofile.userrelationship.css'
  44. ) );
  45. $output = '';
  46. /**
  47. * Get query string variables
  48. */
  49. $user_name = $request->getVal( 'user' );
  50. $rel_type = $request->getInt( 'rel_type' );
  51. $page = $request->getInt( 'page' );
  52. /**
  53. * Redirect Non-logged in users to Login Page
  54. * It will automatically return them to the ViewRelationships page
  55. */
  56. if ( !$user->isLoggedIn() && $user_name == '' ) {
  57. $out->setPageTitle( $this->msg( 'ur-error-page-title' )->plain() );
  58. $login = SpecialPage::getTitleFor( 'Userlogin' );
  59. $out->redirect( htmlspecialchars( $login->getFullURL( 'returnto=Special:ViewRelationships' ) ) );
  60. return false;
  61. }
  62. /**
  63. * Set up config for page / default values
  64. */
  65. if ( !$page || !is_numeric( $page ) ) {
  66. $page = 1;
  67. }
  68. if ( !$rel_type || !is_numeric( $rel_type ) ) {
  69. $rel_type = 1;
  70. }
  71. $per_page = 50;
  72. $per_row = 2;
  73. /**
  74. * If no user is set in the URL, we assume its the current user
  75. */
  76. if ( !$user_name ) {
  77. $user_name = $user->getName();
  78. }
  79. $user_id = User::idFromName( $user_name );
  80. $userPage = Title::makeTitle( NS_USER, $user_name );
  81. /**
  82. * Error message for username that does not exist (from URL)
  83. */
  84. if ( $user_id == 0 ) {
  85. $out->setPageTitle( $this->msg( 'ur-error-title' )->plain() );
  86. $output = '<div class="relationship-error-message">' .
  87. $this->msg( 'ur-error-message-no-user' )->plain() .
  88. '</div>
  89. <div class="relationship-request-buttons">
  90. <input type="button" class="site-button" value="' . $this->msg( 'ur-main-page' )->plain() . '" onclick=\'window.location="index.php?title=' . $this->msg( 'mainpage' )->inContentLanguage()->escaped() . '"\' />';
  91. if ( $user->isLoggedIn() ) {
  92. $output .= '<input type="button" class="site-button" value="' . $this->msg( 'ur-your-profile' )->plain() . '" onclick=\'window.location="' . htmlspecialchars( $user->getUserPage()->getFullURL() ) . '"\' />';
  93. }
  94. $output .= '</div>';
  95. $out->addHTML( $output );
  96. return false;
  97. }
  98. /**
  99. * Get all relationships
  100. */
  101. $rel = new UserRelationship( $user_name );
  102. $relationships = $rel->getRelationshipList( $rel_type, $per_page, $page );
  103. $stats = new UserStats( $rel->user_id, $rel->user_name );
  104. $stats_data = $stats->getUserStats();
  105. $friend_count = $stats_data['friend_count'];
  106. $foe_count = $stats_data['foe_count'];
  107. $back_link = Title::makeTitle( NS_USER, $rel->user_name );
  108. if ( $rel_type == 1 ) {
  109. $out->setPageTitle( $this->msg( 'ur-title-friend', $rel->user_name )->parse() );
  110. $total = $friend_count;
  111. $rem = $this->msg( 'ur-remove-relationship-friend' )->plain();
  112. $output .= '<div class="back-links">
  113. <a href="' . htmlspecialchars( $back_link->getFullURL() ) . '">' .
  114. $this->msg( 'ur-backlink', $rel->user_name )->parse() .
  115. '</a>
  116. </div>
  117. <div class="relationship-count">' .
  118. $this->msg(
  119. 'ur-relationship-count-friends',
  120. $rel->user_name,
  121. $total
  122. )->text() . '</div>';
  123. } else {
  124. $out->setPageTitle( $this->msg( 'ur-title-foe', $rel->user_name )->parse() );
  125. $total = $foe_count;
  126. $rem = $this->msg( 'ur-remove-relationship-foe' )->plain();
  127. $output .= '<div class="back-links">
  128. <a href="' . htmlspecialchars( $back_link->getFullURL() ) . '">' .
  129. $this->msg( 'ur-backlink', $rel->user_name )->parse() .
  130. '</a>
  131. </div>
  132. <div class="relationship-count">'
  133. . $this->msg(
  134. 'ur-relationship-count-foes',
  135. $rel->user_name,
  136. $total
  137. )->text() . '</div>';
  138. }
  139. if ( $relationships ) {
  140. $x = 1;
  141. foreach ( $relationships as $relationship ) {
  142. $indivRelationship = UserRelationship::getUserRelationshipByID(
  143. $relationship['user_id'],
  144. $user->getID()
  145. );
  146. // Safe titles
  147. $userPage = Title::makeTitle( NS_USER, $relationship['user_name'] );
  148. $addRelationshipLink = SpecialPage::getTitleFor( 'AddRelationship' );
  149. $removeRelationshipLink = SpecialPage::getTitleFor( 'RemoveRelationship' );
  150. $giveGiftLink = SpecialPage::getTitleFor( 'GiveGift' );
  151. $userPageURL = htmlspecialchars( $userPage->getFullURL() );
  152. $avatar = new wAvatar( $relationship['user_id'], 'ml' );
  153. $avatar_img = $avatar->getAvatarURL();
  154. $username_length = strlen( $relationship['user_name'] );
  155. $username_space = stripos( $relationship['user_name'], ' ' );
  156. if ( ( $username_space == false || $username_space >= "30" ) && $username_length > 30 ) {
  157. $user_name_display = substr( $relationship['user_name'], 0, 30 ) .
  158. ' ' . substr( $relationship['user_name'], 30, 50 );
  159. } else {
  160. $user_name_display = $relationship['user_name'];
  161. }
  162. $output .= "<div class=\"relationship-item\">
  163. <a href=\"{$userPageURL}\">{$avatar_img}</a>
  164. <div class=\"relationship-info\">
  165. <div class=\"relationship-name\">
  166. <a href=\"{$userPageURL}\">{$user_name_display}</a>
  167. </div>
  168. <div class=\"relationship-actions\">";
  169. if ( $indivRelationship == false ) {
  170. $output .= $lang->pipeList( array(
  171. Linker::link(
  172. $addRelationshipLink,
  173. $this->msg( 'ur-add-friend' )->plain(),
  174. array(),
  175. array( 'user' => $relationship['user_name'], 'rel_type' => 1 )
  176. ),
  177. Linker::link(
  178. $addRelationshipLink,
  179. $this->msg( 'ur-add-foe' )->plain(),
  180. array(),
  181. array( 'user' => $relationship['user_name'], 'rel_type' => 2 )
  182. ),
  183. ''
  184. ) );
  185. } elseif ( $user_name == $user->getName() ) {
  186. $output .= Linker::link(
  187. $removeRelationshipLink,
  188. $rem,
  189. array(),
  190. array( 'user' => $relationship['user_name'] )
  191. );
  192. $output .= $this->msg( 'pipe-separator' )->escaped();
  193. }
  194. $output .= Linker::link(
  195. $giveGiftLink,
  196. $this->msg( 'ur-give-gift' )->plain(),
  197. array(),
  198. array( 'user' => $relationship['user_name'] )
  199. );
  200. $output .= '</div>
  201. <div class="visualClear"></div>
  202. </div>';
  203. $output .= '</div>';
  204. if ( $x == count( $relationships ) || $x != 1 && $x % $per_row == 0 ) {
  205. $output .= '<div class="visualClear"></div>';
  206. }
  207. $x++;
  208. }
  209. }
  210. /**
  211. * Build next/prev nav
  212. */
  213. $total = intval( $total );
  214. $numofpages = $total / $per_page;
  215. $pageLink = $this->getPageTitle();
  216. if ( $numofpages > 1 ) {
  217. $output .= '<div class="page-nav">';
  218. if ( $page > 1 ) {
  219. $output .= Linker::link(
  220. $pageLink,
  221. $this->msg( 'ur-previous' )->plain(),
  222. array(),
  223. array(
  224. 'user' => $user_name,
  225. 'rel_type' => $rel_type,
  226. 'page' => ( $page - 1 )
  227. )
  228. ) . $this->msg( 'word-separator' )->plain();
  229. }
  230. if ( ( $total % $per_page ) != 0 ) {
  231. $numofpages++;
  232. }
  233. if ( $numofpages >= 9 && $page < $total ) {
  234. $numofpages = 9 + $page;
  235. }
  236. if ( $numofpages >= ( $total / $per_page ) ) {
  237. $numofpages = ( $total / $per_page ) + 1;
  238. }
  239. for ( $i = 1; $i <= $numofpages; $i++ ) {
  240. if ( $i == $page ) {
  241. $output .= ( $i . ' ' );
  242. } else {
  243. $output .= Linker::link(
  244. $pageLink,
  245. $i,
  246. array(),
  247. array(
  248. 'user' => $user_name,
  249. 'rel_type' => $rel_type,
  250. 'page' => $i
  251. )
  252. ) . $this->msg( 'word-separator' )->plain();
  253. }
  254. }
  255. if ( ( $total - ( $per_page * $page ) ) > 0 ) {
  256. $output .= $this->msg( 'word-separator' )->plain() .
  257. Linker::link(
  258. $pageLink,
  259. $this->msg( 'ur-next' )->plain(),
  260. array(),
  261. array(
  262. 'user' => $user_name,
  263. 'rel_type' => $rel_type,
  264. 'page' => ( $page + 1 )
  265. )
  266. );
  267. }
  268. $output .= '</div>';
  269. }
  270. $out->addHTML( $output );
  271. }
  272. }