/extensions/CentralNotice/special/SpecialBannerAllocation.php

https://github.com/ChuguluGames/mediawiki-svn · PHP · 248 lines · 166 code · 57 blank · 25 comment · 8 complexity · c9e00a12bc3806c812c4faee38e25a30 MD5 · raw file

  1. <?php
  2. class SpecialBannerAllocation extends UnlistedSpecialPage {
  3. public $project = 'wikipedia';
  4. public $language = 'en';
  5. public $location = 'US';
  6. function __construct() {
  7. // Register special page
  8. parent::__construct( 'BannerAllocation' );
  9. }
  10. /**
  11. * Handle different types of page requests
  12. */
  13. function execute( $sub ) {
  14. global $wgOut, $wgRequest, $wgExtensionAssetsPath, $wgNoticeProjects, $wgLanguageCode, $wgNoticeProject;
  15. $locationSubmitted = false;
  16. $this->project = $wgRequest->getText( 'project', $wgNoticeProject );
  17. $this->language = $wgRequest->getText( 'language', $wgLanguageCode );
  18. // If the form has been submitted, the country code should be passed along.
  19. $locationSubmitted = $wgRequest->getVal( 'country' );
  20. $this->location = $locationSubmitted ? $locationSubmitted : $this->location;
  21. // Convert submitted location to boolean value. If it true, showList() will be called.
  22. $locationSubmitted = (boolean) $locationSubmitted;
  23. // Begin output
  24. $this->setHeaders();
  25. // Add style file to the output headers
  26. $wgOut->addExtensionStyle( "$wgExtensionAssetsPath/CentralNotice/centralnotice.css" );
  27. // Add script file to the output headers
  28. $wgOut->addScriptFile( "$wgExtensionAssetsPath/CentralNotice/centralnotice.js" );
  29. // Initialize error variable
  30. $this->centralNoticeError = false;
  31. // Show summary
  32. $wgOut->addWikiMsg( 'centralnotice-summary' );
  33. // Show header
  34. CentralNotice::printHeader();
  35. // Begin Banners tab content
  36. $wgOut->addHTML( Html::openElement( 'div', array( 'id' => 'preferences' ) ) );
  37. $htmlOut = '';
  38. // Begin Allocation selection fieldset
  39. $htmlOut .= Html::openElement( 'fieldset', array( 'class' => 'prefsection' ) );
  40. $htmlOut .= Html::openElement( 'form', array( 'method' => 'get' ) );
  41. $htmlOut .= Html::element( 'h2', null, wfMsg( 'centralnotice-view-allocation' ) );
  42. $htmlOut .= Xml::tags( 'p', null, wfMsg( 'centralnotice-allocation-instructions' ) );
  43. $htmlOut .= Html::openElement( 'table', array ( 'id' => 'envpicker', 'cellpadding' => 7 ) );
  44. $htmlOut .= Html::openElement( 'tr' );
  45. $htmlOut .= Xml::tags( 'td',
  46. array( 'style' => 'width: 20%;' ),
  47. wfMsg( 'centralnotice-project-name' ) );
  48. $htmlOut .= Html::openElement( 'td' );
  49. $htmlOut .= Html::openElement( 'select', array( 'name' => 'project' ) );
  50. foreach ( $wgNoticeProjects as $value ) {
  51. $htmlOut .= Xml::option( $value, $value, $value === $this->project );
  52. }
  53. $htmlOut .= Html::closeElement( 'select' );
  54. $htmlOut .= Html::closeElement( 'td' );
  55. $htmlOut .= Html::closeElement( 'tr' );
  56. $htmlOut .= Html::openElement( 'tr' );
  57. $htmlOut .= Xml::tags( 'td',
  58. array( 'valign' => 'top' ),
  59. wfMsg( 'centralnotice-project-lang' ) );
  60. $htmlOut .= Html::openElement( 'td' );
  61. // Make sure the site language is in the list; a custom language code
  62. // might not have a defined name...
  63. $languages = Language::getLanguageNames( true );
  64. if( !array_key_exists( $wgLanguageCode, $languages ) ) {
  65. $languages[$wgLanguageCode] = $wgLanguageCode;
  66. }
  67. ksort( $languages );
  68. $htmlOut .= Html::openElement( 'select', array( 'name' => 'language' ) );
  69. foreach( $languages as $code => $name ) {
  70. $htmlOut .= Xml::option(
  71. wfMsg( 'centralnotice-language-listing', $code, $name ),
  72. $code, $code === $this->language );
  73. }
  74. $htmlOut .= Html::closeElement( 'select' );
  75. $htmlOut .= Html::closeElement( 'td' );
  76. $htmlOut .= Html::closeElement( 'tr' );
  77. $htmlOut .= Html::openElement( 'tr' );
  78. $htmlOut .= Xml::tags( 'td', array(), wfMsg( 'centralnotice-country' ) );
  79. $htmlOut .= Html::openElement( 'td' );
  80. $countries = CentralNoticeDB::getCountriesList();
  81. $htmlOut .= Html::openElement( 'select', array( 'name' => 'country' ) );
  82. foreach( $countries as $code => $name ) {
  83. $htmlOut .= Xml::option( $name, $code, $code === $this->location );
  84. }
  85. $htmlOut .= Html::closeElement( 'select' );
  86. $htmlOut .= Html::closeElement( 'td' );
  87. $htmlOut .= Html::closeElement( 'tr' );
  88. $htmlOut .= Html::closeElement( 'table' );
  89. $htmlOut .= Xml::tags( 'div',
  90. array( 'class' => 'cn-buttons' ),
  91. Xml::submitButton( wfMsg( 'centralnotice-view' ) )
  92. );
  93. $htmlOut .= Html::closeElement( 'form' );
  94. // End Allocation selection fieldset
  95. $htmlOut .= Html::closeElement( 'fieldset' );
  96. $wgOut->addHTML( $htmlOut );
  97. // Handle form submissions
  98. if ( $locationSubmitted ) {
  99. $this->showList();
  100. }
  101. // End Banners tab content
  102. $wgOut->addHTML( Html::closeElement( 'div' ) );
  103. }
  104. /**
  105. * Show a list of banners with allocation. Newer banners are shown first.
  106. */
  107. function showList() {
  108. global $wgOut, $wgRequest;
  109. // Begin building HTML
  110. $htmlOut = '';
  111. // Begin Allocation list fieldset
  112. $htmlOut .= Html::openElement( 'fieldset', array( 'class' => 'prefsection' ) );
  113. $bannerLister = new SpecialBannerListLoader();
  114. $bannerLister->project = $wgRequest->getVal( 'project' );
  115. $bannerLister->language = $wgRequest->getVal( 'language' );
  116. $bannerLister->location = $wgRequest->getVal( 'country' );
  117. $htmlOut .= Xml::tags( 'p', null,
  118. wfMsg (
  119. 'centralnotice-allocation-description',
  120. htmlspecialchars( $bannerLister->language ),
  121. htmlspecialchars( $bannerLister->project ),
  122. htmlspecialchars( $bannerLister->location )
  123. )
  124. );
  125. $bannerList = $bannerLister->getJsonList();
  126. $banners = FormatJson::decode( $bannerList, true );
  127. $anonBanners = array();
  128. $accountBanners = array();
  129. $anonWeight = 0;
  130. $accountWeight = 0;
  131. if ( $banners ) {
  132. foreach ( $banners as $banner ) {
  133. if ($banner['display_anon']) {
  134. $anonBanners[] = $banner;
  135. $anonWeight += $banner['weight'];
  136. }
  137. if ($banner['display_account']) {
  138. $accountBanners[] = $banner;
  139. $accountWeight += $banner['weight'];
  140. }
  141. }
  142. if ( $anonBanners ) {
  143. $htmlOut .= $this->getTable( wfMsg ( 'centralnotice-banner-anonymous' ), $anonBanners, $anonWeight );
  144. }
  145. if ( $accountBanners ) {
  146. $htmlOut .= $this->getTable( wfMsg ( 'centralnotice-banner-logged-in' ), $accountBanners, $accountWeight );
  147. }
  148. } else {
  149. $htmlOut .= Xml::tags( 'p', null, wfMsg ( 'centralnotice-no-allocation' ) );
  150. }
  151. // End Allocation list fieldset
  152. $htmlOut .= Html::closeElement( 'fieldset' );
  153. $wgOut->addHTML( $htmlOut );
  154. }
  155. function getTable( $type, $banners, $weight ) {
  156. global $wgUser, $wgLang;
  157. $sk = $wgUser->getSkin();
  158. $viewBanner = $this->getTitleFor( 'NoticeTemplate', 'view' );
  159. $viewCampaign = $this->getTitleFor( 'CentralNotice' );
  160. $htmlOut = Html::openElement( 'table',
  161. array ( 'cellpadding' => 9, 'class' => 'wikitable sortable', 'style' => 'margin: 1em;' )
  162. );
  163. $htmlOut .= Html::element( 'caption', array( 'style' => 'font-size: 1.2em;' ), $type );
  164. $htmlOut .= Html::openElement( 'tr' );
  165. $htmlOut .= Html::element( 'th', array( 'width' => '20%' ),
  166. wfMsg ( 'centralnotice-percentage' ) );
  167. $htmlOut .= Html::element( 'th', array( 'width' => '30%' ),
  168. wfMsg ( 'centralnotice-banner' ) );
  169. $htmlOut .= Html::element( 'th', array( 'width' => '30%' ),
  170. wfMsg ( 'centralnotice-notice' ) );
  171. $htmlOut .= Html::closeElement( 'tr' );
  172. foreach ( $banners as $banner ) {
  173. $htmlOut .= Html::openElement( 'tr' );
  174. $htmlOut .= Html::openElement( 'td' );
  175. $percentage = round( ( $banner['weight'] / $weight ) * 100, 2 );
  176. $htmlOut .= wfMsg ( 'percent', $wgLang->formatNum( $percentage ) );
  177. $htmlOut .= Html::closeElement( 'td' );
  178. $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
  179. $sk->makeLinkObj( $viewBanner, htmlspecialchars( $banner['name'] ),
  180. 'template=' . urlencode( $banner['name'] ) )
  181. );
  182. $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ),
  183. $sk->makeLinkObj( $viewCampaign, htmlspecialchars( $banner['campaign'] ),
  184. 'method=listNoticeDetail&notice=' . urlencode( $banner['campaign'] ) )
  185. );
  186. $htmlOut .= Html::closeElement( 'tr' );
  187. }
  188. $htmlOut .= Html::closeElement( 'table' );
  189. return $htmlOut;
  190. }
  191. }