http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72952

Revision: 72952
Author:   kaldari
Date:     2010-09-14 01:44:08 +0000 (Tue, 14 Sep 2010)

Log Message:
-----------
adding some functions for geotargeting, support for updating geo

Modified Paths:
--------------
    trunk/extensions/CentralNotice/SpecialCentralNotice.php

Modified: trunk/extensions/CentralNotice/SpecialCentralNotice.php
===================================================================
--- trunk/extensions/CentralNotice/SpecialCentralNotice.php     2010-09-14 
01:19:37 UTC (rev 72951)
+++ trunk/extensions/CentralNotice/SpecialCentralNotice.php     2010-09-14 
01:44:08 UTC (rev 72952)
@@ -563,26 +563,37 @@
                                        }
                                        
                                        // Handle locking/unlocking campaign
-                                       if ( $wgRequest->getArray( 'locked' ) ) 
{
+                                       if ( $wgRequest->getCheck( 'locked' ) ) 
{
                                                $this->updateLock( $notice, '1' 
);
                                        } else {
                                                $this->updateLock( $notice, 0 );
                                        }
                                        
                                        // Handle enabling/disabling campaign
-                                       if ( $wgRequest->getArray( 'enabled' ) 
) {
+                                       if ( $wgRequest->getCheck( 'enabled' ) 
) {
                                                $this->updateEnabled( $notice, 
'1' );
                                        } else {
                                                $this->updateEnabled( $notice, 
0 );
                                        }
                                        
                                        // Handle setting campaign to 
preferred/not preferred
-                                       if ( $wgRequest->getArray( 'preferred' 
) ) {
+                                       if ( $wgRequest->getCheck( 'preferred' 
) ) {
                                                $this->updatePreferred( 
$notice, '1' );
                                        } else {
                                                $this->updatePreferred( 
$notice, 0 );
                                        }
                                        
+                                       // Handle updating geotargeting
+                                       if ( $wgRequest->getCheck( 
'geotargeted' ) ) {
+                                               $this->updateGeotargeted( 
$notice, '1' );
+                                               $countries = 
$wgRequest->getArray( 'geo_countries' );
+                                               if ( $countries ) {
+                                                       $this->updateCountries( 
$notice, $countries );
+                                               }
+                                       } else {
+                                               $this->updateGeotargeted( 
$notice, 0 );
+                                       }
+                                       
                                        // Handle updating the start and end 
settings
                                        $start = $wgRequest->getArray( 'start' 
);
                                        $end = $wgRequest->getArray( 'end' );
@@ -736,7 +747,8 @@
                                'not_enabled',
                                'not_preferred',
                                'not_project',
-                               'not_locked'
+                               'not_locked',
+                               'not_geo'
                        ),
                        array( 'not_name' => $notice ),
                        __METHOD__
@@ -765,6 +777,8 @@
                                $isLocked = $wgRequest->getCheck( 'locked' );
                                $projectSelected = $wgRequest->getVal( 
'project_name' );
                                $noticeLanguages = $wgRequest->getArray( 
'project_languages', array() );
+                               $isGeotargeted = $wgRequest->getCheck( 
'geotargeted' );
+                               $countries = $wgRequest->getArray( 
'geo_countries', array() );
                        } else { // Defaults
                                $startTimestamp = $row->not_start;
                                $endTimestamp = $row->not_end;
@@ -773,6 +787,8 @@
                                $isLocked = ( $row->not_locked == '1' );
                                $projectSelected = $row->not_project;
                                $noticeLanguages = $this->getNoticeLanguages( 
$notice );
+                               $isGeotargeted = ( $row->not_geo == '1' );
+                               $countries = $this->getNoticeCountries( $notice 
);
                        }
                
                        // Build Html
@@ -814,11 +830,11 @@
                        // Countries
                        $htmlOut .= Xml::openElement( 'tr' );
                        $htmlOut .= Xml::tags( 'td', array(), Xml::label( 
wfMsgHtml( 'centralnotice-geotargeted' ), 'geotargeted' ) );
-                       $htmlOut .= Xml::tags( 'td', array(), Xml::check( 
'geotargeted', false, wfArrayMerge( $readonly, array( 'value' => 
$row->not_name, 'id' => 'geotargeted' ) ) ) );
+                       $htmlOut .= Xml::tags( 'td', array(), Xml::check( 
'geotargeted', $isGeotargeted, wfArrayMerge( $readonly, array( 'value' => 
$row->not_name, 'id' => 'geotargeted' ) ) ) );
                        $htmlOut .= Xml::closeElement( 'tr' );
                        $htmlOut .= Xml::openElement( 'tr', array( 
'id'=>'geoMultiSelector' ) );
                        $htmlOut .= Xml::tags( 'td', array( 'valign' => 'top' 
), wfMsgHtml( 'centralnotice-countries' ) );
-                       $htmlOut .= Xml::tags( 'td', array(), 
$this->geoMultiSelector() );
+                       $htmlOut .= Xml::tags( 'td', array(), 
$this->geoMultiSelector( $countries ) );
                        $htmlOut .= Xml::closeElement( 'tr' );
                        // Enabled
                        $htmlOut .= Xml::openElement( 'tr' );
@@ -1173,6 +1189,20 @@
                }
                return $languages;
        }
+       
+       function getNoticeCountries( $noticeName ) {
+               $dbr = wfGetDB( DB_SLAVE );
+               $eNoticeName = htmlspecialchars( $noticeName );
+               $row = $dbr->selectRow( 'cn_notices', 'not_id', array( 
'not_name' => $eNoticeName ) );
+               $countries = array();
+               if ( $row ) {
+                       $res = $dbr->select( 'cn_notice_countries', 
'nc_country', array( 'nc_notice_id' => $row->not_id ) );
+                       foreach ( $res as $countryRow ) {
+                               $countries[] = $countryRow->nc_country;
+                       }
+               }
+               return $countries;
+       }
 
        function getNoticeProjectName( $noticeName ) {
                 $dbr = wfGetDB( DB_SLAVE );
@@ -1246,7 +1276,7 @@
                        );
                }
        }
-
+       
        /**
         * Update the preferred/not preferred state of a campaign
         */
@@ -1265,6 +1295,23 @@
        }
 
        /**
+        * Update the geotargeted/not geotargeted state of a campaign
+        */
+       function updateGeotargeted( $noticeName, $isGeotargeted ) {
+               global $wgOut;
+               
+               if ( !$this->noticeExists( $noticeName ) ) {
+                       $this->showError( 'centralnotice-doesnt-exist' );
+               } else {
+                       $dbw = wfGetDB( DB_MASTER );
+                       $res = $dbw->update( 'cn_notices',
+                               array( 'not_geo' => $isGeotargeted ),
+                               array( 'not_name' => $noticeName )
+                       );
+               }
+       }
+
+       /**
         * Update the locked/unlocked state of a campaign
         */
        function updateLock( $noticeName, $isLocked ) {
@@ -1404,6 +1451,36 @@
                $dbw->commit();
        }
        
+       function updateCountries( $notice, $newCountries ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
+               
+               // Get the previously assigned languages
+               $oldCountries = array();
+               $oldCountries = $this->getNoticeCountries( $notice );
+               
+               // Get the notice id
+               $row = $dbw->selectRow( 'cn_notices', 'not_id', array( 
'not_name' => $notice ) );
+               
+               // Add newly assigned countries
+               $addCountries = array_diff( $newCountries, $oldCountries );
+               $insertArray = array();
+               foreach( $addCountries as $code ) {
+                       $insertArray[] = array( 'nc_notice_id' => $row->not_id, 
'nc_country' => $code );
+               }
+               $res = $dbw->insert( 'cn_notice_countries', $insertArray, 
__METHOD__, array( 'IGNORE' ) );
+               
+               // Remove disassociated countries
+               $removeCountries = array_diff( $oldCountries, $newCountries );
+               if ( $removeCountries ) {
+                       $res = $dbw->delete( 'cn_notice_countries',
+                               array( 'nc_notice_id' => $row->not_id, 
'nc_country' => $removeCountries )
+                       );
+               }
+               
+               $dbw->commit();
+       }
+       
        public static function noticeExists( $noticeName ) {
                 $dbr = wfGetDB( DB_SLAVE );
                 $eNoticeName = htmlspecialchars( $noticeName );



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to