Index: trunk/extensions/CentralNotice/special/SpecialBannerAllocation.php |
— | — | @@ -1,20 +1,15 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -if ( !defined( 'MEDIAWIKI' ) ) { |
5 | | - echo "CentralNotice extension\n"; |
6 | | - exit( 1 ); |
7 | | -} |
8 | | - |
9 | 4 | class SpecialBannerAllocation extends UnlistedSpecialPage { |
10 | 5 | public $project = 'wikipedia'; |
11 | 6 | public $language = 'en'; |
12 | 7 | public $location = 'US'; |
13 | | - |
| 8 | + |
14 | 9 | function __construct() { |
15 | 10 | // Register special page |
16 | | - parent::__construct( "BannerAllocation" ); |
| 11 | + parent::__construct( 'BannerAllocation' ); |
17 | 12 | } |
18 | | - |
| 13 | + |
19 | 14 | /** |
20 | 15 | * Handle different types of page requests |
21 | 16 | */ |
— | — | @@ -22,23 +17,23 @@ |
23 | 18 | global $wgOut, $wgRequest, $wgExtensionAssetsPath, $wgNoticeProjects, $wgLanguageCode, $wgNoticeProject; |
24 | 19 | |
25 | 20 | $locationSubmitted = false; |
26 | | - |
| 21 | + |
27 | 22 | $this->project = $wgRequest->getText( 'project', $wgNoticeProject ); |
28 | 23 | $this->language = $wgRequest->getText( 'language', $wgLanguageCode ); |
29 | | - |
| 24 | + |
30 | 25 | // If the form has been submitted, the country code should be passed along. |
31 | 26 | $locationSubmitted = $wgRequest->getVal( 'country' ); |
32 | 27 | $this->location = $locationSubmitted ? $locationSubmitted : $this->location; |
33 | | - |
| 28 | + |
34 | 29 | // Convert submitted location to boolean value. If it true, showList() will be called. |
35 | 30 | $locationSubmitted = (boolean) $locationSubmitted; |
36 | 31 | |
37 | 32 | // Begin output |
38 | 33 | $this->setHeaders(); |
39 | | - |
| 34 | + |
40 | 35 | // Add style file to the output headers |
41 | 36 | $wgOut->addExtensionStyle( "$wgExtensionAssetsPath/CentralNotice/centralnotice.css" ); |
42 | | - |
| 37 | + |
43 | 38 | // Add script file to the output headers |
44 | 39 | $wgOut->addScriptFile( "$wgExtensionAssetsPath/CentralNotice/centralnotice.js" ); |
45 | 40 | |
— | — | @@ -53,74 +48,84 @@ |
54 | 49 | |
55 | 50 | // Begin Banners tab content |
56 | 51 | $wgOut->addHTML( Xml::openElement( 'div', array( 'id' => 'preferences' ) ) ); |
57 | | - |
| 52 | + |
58 | 53 | $htmlOut = ''; |
59 | | - |
| 54 | + |
60 | 55 | // Begin Allocation selection fieldset |
61 | 56 | $htmlOut .= Xml::openElement( 'fieldset', array( 'class' => 'prefsection' ) ); |
62 | | - |
| 57 | + |
63 | 58 | $htmlOut .= Xml::openElement( 'form', array( 'method' => 'get' ) ); |
64 | 59 | $htmlOut .= Xml::element( 'h2', null, wfMsg( 'centralnotice-view-allocation' ) ); |
65 | 60 | $htmlOut .= Xml::tags( 'p', null, wfMsg( 'centralnotice-allocation-instructions' ) ); |
66 | | - |
| 61 | + |
67 | 62 | $htmlOut .= Xml::openElement( 'table', array ( 'id' => 'envpicker', 'cellpadding' => 7 ) ); |
68 | 63 | $htmlOut .= Xml::openElement( 'tr' ); |
69 | | - $htmlOut .= Xml::tags( 'td', |
70 | | - array( 'style' => 'width: 20%;' ), |
| 64 | + $htmlOut .= Xml::tags( 'td', |
| 65 | + array( 'style' => 'width: 20%;' ), |
71 | 66 | wfMsg( 'centralnotice-project-name' ) ); |
72 | 67 | $htmlOut .= Xml::openElement( 'td' ); |
73 | 68 | $htmlOut .= Xml::openElement( 'select', array( 'name' => 'project' ) ); |
| 69 | + |
74 | 70 | foreach ( $wgNoticeProjects as $value ) { |
75 | 71 | $htmlOut .= Xml::option( $value, $value, $value === $this->project ); |
76 | 72 | } |
| 73 | + |
77 | 74 | $htmlOut .= Xml::closeElement( 'select' ); |
78 | 75 | $htmlOut .= Xml::closeElement( 'td' ); |
79 | 76 | $htmlOut .= Xml::closeElement( 'tr' ); |
80 | 77 | $htmlOut .= Xml::openElement( 'tr' ); |
81 | | - $htmlOut .= Xml::tags( 'td', |
82 | | - array( 'valign' => 'top' ), |
| 78 | + $htmlOut .= Xml::tags( 'td', |
| 79 | + array( 'valign' => 'top' ), |
83 | 80 | wfMsg( 'centralnotice-project-lang' ) ); |
84 | 81 | $htmlOut .= Xml::openElement( 'td' ); |
85 | | - // Make sure the site language is in the list; a custom language code |
| 82 | + |
| 83 | + // Make sure the site language is in the list; a custom language code |
86 | 84 | // might not have a defined name... |
87 | 85 | $languages = Language::getLanguageNames( true ); |
88 | 86 | if( !array_key_exists( $wgLanguageCode, $languages ) ) { |
89 | 87 | $languages[$wgLanguageCode] = $wgLanguageCode; |
90 | 88 | } |
| 89 | + |
91 | 90 | ksort( $languages ); |
92 | 91 | $htmlOut .= Xml::openElement( 'select', array( 'name' => 'language' ) ); |
| 92 | + |
93 | 93 | foreach( $languages as $code => $name ) { |
94 | | - $htmlOut .= Xml::option( |
95 | | - wfMsg( 'centralnotice-language-listing', $code, $name ), |
| 94 | + $htmlOut .= Xml::option( |
| 95 | + wfMsg( 'centralnotice-language-listing', $code, $name ), |
96 | 96 | $code, $code === $this->language ); |
97 | 97 | } |
| 98 | + |
98 | 99 | $htmlOut .= Xml::closeElement( 'select' ); |
99 | 100 | $htmlOut .= Xml::closeElement( 'td' ); |
100 | 101 | $htmlOut .= Xml::closeElement( 'tr' ); |
101 | 102 | $htmlOut .= Xml::openElement( 'tr' ); |
102 | 103 | $htmlOut .= Xml::tags( 'td', array(), wfMsg( 'centralnotice-country' ) ); |
103 | 104 | $htmlOut .= Xml::openElement( 'td' ); |
| 105 | + |
104 | 106 | $countries = CentralNoticeDB::getCountriesList(); |
| 107 | + |
105 | 108 | $htmlOut .= Xml::openElement( 'select', array( 'name' => 'country' ) ); |
| 109 | + |
106 | 110 | foreach( $countries as $code => $name ) { |
107 | 111 | $htmlOut .= Xml::option( $name, $code, $code === $this->location ); |
108 | 112 | } |
| 113 | + |
109 | 114 | $htmlOut .= Xml::closeElement( 'select' ); |
110 | 115 | $htmlOut .= Xml::closeElement( 'td' ); |
111 | 116 | $htmlOut .= Xml::closeElement( 'tr' ); |
112 | 117 | $htmlOut .= Xml::closeElement( 'table' ); |
113 | | - |
114 | | - $htmlOut .= Xml::tags( 'div', |
115 | | - array( 'class' => 'cn-buttons' ), |
116 | | - Xml::submitButton( wfMsg( 'centralnotice-modify' ) ) |
| 118 | + |
| 119 | + $htmlOut .= Xml::tags( 'div', |
| 120 | + array( 'class' => 'cn-buttons' ), |
| 121 | + Xml::submitButton( wfMsg( 'centralnotice-modify' ) ) |
117 | 122 | ); |
118 | 123 | $htmlOut .= Xml::closeElement( 'form' ); |
119 | | - |
| 124 | + |
120 | 125 | // End Allocation selection fieldset |
121 | 126 | $htmlOut .= Xml::closeElement( 'fieldset' ); |
122 | 127 | |
123 | 128 | $wgOut->addHTML( $htmlOut ); |
124 | | - |
| 129 | + |
125 | 130 | // Handle form submissions |
126 | 131 | if ( $locationSubmitted ) { |
127 | 132 | $this->showList(); |
— | — | @@ -129,19 +134,19 @@ |
130 | 135 | // End Banners tab content |
131 | 136 | $wgOut->addHTML( Xml::closeElement( 'div' ) ); |
132 | 137 | } |
133 | | - |
| 138 | + |
134 | 139 | /** |
135 | 140 | * Show a list of banners with allocation. Newer banners are shown first. |
136 | 141 | */ |
137 | 142 | function showList() { |
138 | 143 | global $wgOut, $wgRequest; |
139 | | - |
| 144 | + |
140 | 145 | // Begin building HTML |
141 | 146 | $htmlOut = ''; |
142 | | - |
| 147 | + |
143 | 148 | // Begin Allocation list fieldset |
144 | 149 | $htmlOut .= Xml::openElement( 'fieldset', array( 'class' => 'prefsection' ) ); |
145 | | - |
| 150 | + |
146 | 151 | $bannerLister = new SpecialBannerListLoader(); |
147 | 152 | $bannerLister->project = $wgRequest->getVal( 'project' ); |
148 | 153 | $bannerLister->language = $wgRequest->getVal( 'language' ); |
— | — | @@ -162,70 +167,82 @@ |
163 | 168 | $accountBanners = array(); |
164 | 169 | $anonWeight = 0; |
165 | 170 | $accountWeight = 0; |
| 171 | + |
166 | 172 | if ( $banners ) { |
167 | 173 | foreach ( $banners as $banner ) { |
168 | 174 | if ($banner['display_anon']) { |
169 | 175 | $anonBanners[] = $banner; |
170 | 176 | $anonWeight += $banner['weight']; |
171 | 177 | } |
| 178 | + |
172 | 179 | if ($banner['display_account']) { |
173 | 180 | $accountBanners[] = $banner; |
174 | 181 | $accountWeight += $banner['weight']; |
175 | 182 | } |
176 | 183 | } |
| 184 | + |
177 | 185 | if ( $anonBanners ) { |
178 | 186 | $htmlOut .= $this->getTable( wfMsg ( 'centralnotice-banner-anonymous' ), $anonBanners, $anonWeight ); |
179 | 187 | } |
| 188 | + |
180 | 189 | if ( $accountBanners ) { |
181 | 190 | $htmlOut .= $this->getTable( wfMsg ( 'centralnotice-banner-logged-in' ), $accountBanners, $accountWeight ); |
182 | 191 | } |
183 | 192 | } else { |
184 | 193 | $htmlOut .= Xml::tags( 'p', null, wfMsg ( 'centralnotice-no-allocation' ) ); |
185 | 194 | } |
186 | | - |
| 195 | + |
187 | 196 | // End Allocation list fieldset |
188 | 197 | $htmlOut .= Xml::closeElement( 'fieldset' ); |
189 | 198 | |
190 | 199 | $wgOut->addHTML( $htmlOut ); |
191 | 200 | } |
192 | | - |
| 201 | + |
193 | 202 | function getTable( $type, $banners, $weight ) { |
194 | 203 | global $wgUser, $wgLang; |
195 | | - |
| 204 | + |
196 | 205 | $sk = $wgUser->getSkin(); |
197 | 206 | $viewBanner = $this->getTitleFor( 'NoticeTemplate', 'view' ); |
198 | 207 | $viewCampaign = $this->getTitleFor( 'CentralNotice' ); |
199 | | - |
200 | | - $htmlOut = Xml::openElement( 'table', |
| 208 | + |
| 209 | + $htmlOut = Xml::openElement( 'table', |
201 | 210 | array ( 'cellpadding' => 9, 'class' => 'wikitable sortable', 'style' => 'margin: 1em;' ) |
202 | 211 | ); |
203 | 212 | $htmlOut .= Xml::element( 'caption', array( 'style' => 'font-size: 1.2em;' ), $type ); |
204 | 213 | $htmlOut .= Xml::openElement( 'tr' ); |
205 | | - $htmlOut .= Xml::element( 'th', array( 'width' => '20%' ), |
| 214 | + $htmlOut .= Xml::element( 'th', array( 'width' => '20%' ), |
206 | 215 | wfMsg ( 'centralnotice-percentage' ) ); |
207 | | - $htmlOut .= Xml::element( 'th', array( 'width' => '30%' ), |
| 216 | + $htmlOut .= Xml::element( 'th', array( 'width' => '30%' ), |
208 | 217 | wfMsg ( 'centralnotice-banner' ) ); |
209 | | - $htmlOut .= Xml::element( 'th', array( 'width' => '30%' ), |
| 218 | + $htmlOut .= Xml::element( 'th', array( 'width' => '30%' ), |
210 | 219 | wfMsg ( 'centralnotice-notice' ) ); |
211 | 220 | $htmlOut .= Xml::closeElement( 'tr' ); |
| 221 | + |
212 | 222 | foreach ( $banners as $banner ) { |
213 | 223 | $htmlOut .= Xml::openElement( 'tr' ); |
214 | 224 | $htmlOut .= Xml::openElement( 'td' ); |
| 225 | + |
215 | 226 | $percentage = round( ( $banner['weight'] / $weight ) * 100, 2 ); |
| 227 | + |
216 | 228 | $htmlOut .= wfMsg ( 'percent', $wgLang->formatNum( $percentage ) ); |
217 | 229 | $htmlOut .= Xml::closeElement( 'td' ); |
218 | 230 | $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), |
219 | | - $sk->makeLinkObj( $viewBanner, htmlspecialchars( $banner['name'] ), |
| 231 | + |
| 232 | + $sk->makeLinkObj( $viewBanner, htmlspecialchars( $banner['name'] ), |
220 | 233 | 'template=' . urlencode( $banner['name'] ) ) |
221 | 234 | ); |
| 235 | + |
222 | 236 | $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), |
223 | | - $sk->makeLinkObj( $viewCampaign, htmlspecialchars( $banner['campaign'] ), |
| 237 | + |
| 238 | + $sk->makeLinkObj( $viewCampaign, htmlspecialchars( $banner['campaign'] ), |
224 | 239 | 'method=listNoticeDetail¬ice=' . urlencode( $banner['campaign'] ) ) |
225 | 240 | ); |
| 241 | + |
226 | 242 | $htmlOut .= Xml::closeElement( 'tr' ); |
227 | 243 | } |
| 244 | + |
228 | 245 | $htmlOut .= Xml::closeElement( 'table' ); |
| 246 | + |
229 | 247 | return $htmlOut; |
230 | 248 | } |
231 | | - |
232 | 249 | } |