Index: trunk/extensions/CentralNotice/SpecialCentralNotice.php |
— | — | @@ -563,26 +563,37 @@ |
564 | 564 | } |
565 | 565 | |
566 | 566 | // Handle locking/unlocking campaign |
567 | | - if ( $wgRequest->getArray( 'locked' ) ) { |
| 567 | + if ( $wgRequest->getCheck( 'locked' ) ) { |
568 | 568 | $this->updateLock( $notice, '1' ); |
569 | 569 | } else { |
570 | 570 | $this->updateLock( $notice, 0 ); |
571 | 571 | } |
572 | 572 | |
573 | 573 | // Handle enabling/disabling campaign |
574 | | - if ( $wgRequest->getArray( 'enabled' ) ) { |
| 574 | + if ( $wgRequest->getCheck( 'enabled' ) ) { |
575 | 575 | $this->updateEnabled( $notice, '1' ); |
576 | 576 | } else { |
577 | 577 | $this->updateEnabled( $notice, 0 ); |
578 | 578 | } |
579 | 579 | |
580 | 580 | // Handle setting campaign to preferred/not preferred |
581 | | - if ( $wgRequest->getArray( 'preferred' ) ) { |
| 581 | + if ( $wgRequest->getCheck( 'preferred' ) ) { |
582 | 582 | $this->updatePreferred( $notice, '1' ); |
583 | 583 | } else { |
584 | 584 | $this->updatePreferred( $notice, 0 ); |
585 | 585 | } |
586 | 586 | |
| 587 | + // Handle updating geotargeting |
| 588 | + if ( $wgRequest->getCheck( 'geotargeted' ) ) { |
| 589 | + $this->updateGeotargeted( $notice, '1' ); |
| 590 | + $countries = $wgRequest->getArray( 'geo_countries' ); |
| 591 | + if ( $countries ) { |
| 592 | + $this->updateCountries( $notice, $countries ); |
| 593 | + } |
| 594 | + } else { |
| 595 | + $this->updateGeotargeted( $notice, 0 ); |
| 596 | + } |
| 597 | + |
587 | 598 | // Handle updating the start and end settings |
588 | 599 | $start = $wgRequest->getArray( 'start' ); |
589 | 600 | $end = $wgRequest->getArray( 'end' ); |
— | — | @@ -736,7 +747,8 @@ |
737 | 748 | 'not_enabled', |
738 | 749 | 'not_preferred', |
739 | 750 | 'not_project', |
740 | | - 'not_locked' |
| 751 | + 'not_locked', |
| 752 | + 'not_geo' |
741 | 753 | ), |
742 | 754 | array( 'not_name' => $notice ), |
743 | 755 | __METHOD__ |
— | — | @@ -765,6 +777,8 @@ |
766 | 778 | $isLocked = $wgRequest->getCheck( 'locked' ); |
767 | 779 | $projectSelected = $wgRequest->getVal( 'project_name' ); |
768 | 780 | $noticeLanguages = $wgRequest->getArray( 'project_languages', array() ); |
| 781 | + $isGeotargeted = $wgRequest->getCheck( 'geotargeted' ); |
| 782 | + $countries = $wgRequest->getArray( 'geo_countries', array() ); |
769 | 783 | } else { // Defaults |
770 | 784 | $startTimestamp = $row->not_start; |
771 | 785 | $endTimestamp = $row->not_end; |
— | — | @@ -773,6 +787,8 @@ |
774 | 788 | $isLocked = ( $row->not_locked == '1' ); |
775 | 789 | $projectSelected = $row->not_project; |
776 | 790 | $noticeLanguages = $this->getNoticeLanguages( $notice ); |
| 791 | + $isGeotargeted = ( $row->not_geo == '1' ); |
| 792 | + $countries = $this->getNoticeCountries( $notice ); |
777 | 793 | } |
778 | 794 | |
779 | 795 | // Build Html |
— | — | @@ -814,11 +830,11 @@ |
815 | 831 | // Countries |
816 | 832 | $htmlOut .= Xml::openElement( 'tr' ); |
817 | 833 | $htmlOut .= Xml::tags( 'td', array(), Xml::label( wfMsgHtml( 'centralnotice-geotargeted' ), 'geotargeted' ) ); |
818 | | - $htmlOut .= Xml::tags( 'td', array(), Xml::check( 'geotargeted', false, wfArrayMerge( $readonly, array( 'value' => $row->not_name, 'id' => 'geotargeted' ) ) ) ); |
| 834 | + $htmlOut .= Xml::tags( 'td', array(), Xml::check( 'geotargeted', $isGeotargeted, wfArrayMerge( $readonly, array( 'value' => $row->not_name, 'id' => 'geotargeted' ) ) ) ); |
819 | 835 | $htmlOut .= Xml::closeElement( 'tr' ); |
820 | 836 | $htmlOut .= Xml::openElement( 'tr', array( 'id'=>'geoMultiSelector' ) ); |
821 | 837 | $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' ), wfMsgHtml( 'centralnotice-countries' ) ); |
822 | | - $htmlOut .= Xml::tags( 'td', array(), $this->geoMultiSelector() ); |
| 838 | + $htmlOut .= Xml::tags( 'td', array(), $this->geoMultiSelector( $countries ) ); |
823 | 839 | $htmlOut .= Xml::closeElement( 'tr' ); |
824 | 840 | // Enabled |
825 | 841 | $htmlOut .= Xml::openElement( 'tr' ); |
— | — | @@ -1173,6 +1189,20 @@ |
1174 | 1190 | } |
1175 | 1191 | return $languages; |
1176 | 1192 | } |
| 1193 | + |
| 1194 | + function getNoticeCountries( $noticeName ) { |
| 1195 | + $dbr = wfGetDB( DB_SLAVE ); |
| 1196 | + $eNoticeName = htmlspecialchars( $noticeName ); |
| 1197 | + $row = $dbr->selectRow( 'cn_notices', 'not_id', array( 'not_name' => $eNoticeName ) ); |
| 1198 | + $countries = array(); |
| 1199 | + if ( $row ) { |
| 1200 | + $res = $dbr->select( 'cn_notice_countries', 'nc_country', array( 'nc_notice_id' => $row->not_id ) ); |
| 1201 | + foreach ( $res as $countryRow ) { |
| 1202 | + $countries[] = $countryRow->nc_country; |
| 1203 | + } |
| 1204 | + } |
| 1205 | + return $countries; |
| 1206 | + } |
1177 | 1207 | |
1178 | 1208 | function getNoticeProjectName( $noticeName ) { |
1179 | 1209 | $dbr = wfGetDB( DB_SLAVE ); |
— | — | @@ -1246,7 +1276,7 @@ |
1247 | 1277 | ); |
1248 | 1278 | } |
1249 | 1279 | } |
1250 | | - |
| 1280 | + |
1251 | 1281 | /** |
1252 | 1282 | * Update the preferred/not preferred state of a campaign |
1253 | 1283 | */ |
— | — | @@ -1265,6 +1295,23 @@ |
1266 | 1296 | } |
1267 | 1297 | |
1268 | 1298 | /** |
| 1299 | + * Update the geotargeted/not geotargeted state of a campaign |
| 1300 | + */ |
| 1301 | + function updateGeotargeted( $noticeName, $isGeotargeted ) { |
| 1302 | + global $wgOut; |
| 1303 | + |
| 1304 | + if ( !$this->noticeExists( $noticeName ) ) { |
| 1305 | + $this->showError( 'centralnotice-doesnt-exist' ); |
| 1306 | + } else { |
| 1307 | + $dbw = wfGetDB( DB_MASTER ); |
| 1308 | + $res = $dbw->update( 'cn_notices', |
| 1309 | + array( 'not_geo' => $isGeotargeted ), |
| 1310 | + array( 'not_name' => $noticeName ) |
| 1311 | + ); |
| 1312 | + } |
| 1313 | + } |
| 1314 | + |
| 1315 | + /** |
1269 | 1316 | * Update the locked/unlocked state of a campaign |
1270 | 1317 | */ |
1271 | 1318 | function updateLock( $noticeName, $isLocked ) { |
— | — | @@ -1404,6 +1451,36 @@ |
1405 | 1452 | $dbw->commit(); |
1406 | 1453 | } |
1407 | 1454 | |
| 1455 | + function updateCountries( $notice, $newCountries ) { |
| 1456 | + $dbw = wfGetDB( DB_MASTER ); |
| 1457 | + $dbw->begin(); |
| 1458 | + |
| 1459 | + // Get the previously assigned languages |
| 1460 | + $oldCountries = array(); |
| 1461 | + $oldCountries = $this->getNoticeCountries( $notice ); |
| 1462 | + |
| 1463 | + // Get the notice id |
| 1464 | + $row = $dbw->selectRow( 'cn_notices', 'not_id', array( 'not_name' => $notice ) ); |
| 1465 | + |
| 1466 | + // Add newly assigned countries |
| 1467 | + $addCountries = array_diff( $newCountries, $oldCountries ); |
| 1468 | + $insertArray = array(); |
| 1469 | + foreach( $addCountries as $code ) { |
| 1470 | + $insertArray[] = array( 'nc_notice_id' => $row->not_id, 'nc_country' => $code ); |
| 1471 | + } |
| 1472 | + $res = $dbw->insert( 'cn_notice_countries', $insertArray, __METHOD__, array( 'IGNORE' ) ); |
| 1473 | + |
| 1474 | + // Remove disassociated countries |
| 1475 | + $removeCountries = array_diff( $oldCountries, $newCountries ); |
| 1476 | + if ( $removeCountries ) { |
| 1477 | + $res = $dbw->delete( 'cn_notice_countries', |
| 1478 | + array( 'nc_notice_id' => $row->not_id, 'nc_country' => $removeCountries ) |
| 1479 | + ); |
| 1480 | + } |
| 1481 | + |
| 1482 | + $dbw->commit(); |
| 1483 | + } |
| 1484 | + |
1408 | 1485 | public static function noticeExists( $noticeName ) { |
1409 | 1486 | $dbr = wfGetDB( DB_SLAVE ); |
1410 | 1487 | $eNoticeName = htmlspecialchars( $noticeName ); |