/**
  * Handle different types of page requests
  */
 function execute($sub)
 {
     // Begin output
     $this->setHeaders();
     $this->outputHeader();
     $out = $this->getOutput();
     $request = $this->getRequest();
     // Output ResourceLoader module for styling and javascript functions
     $out->addModules('ext.promoter.adminUi.campaignManager');
     // Check permissions
     $this->editable = $this->getUser()->isAllowed('promoter-admin');
     // Initialize error variable
     $this->promoterError = false;
     // Begin Campaigns tab content
     $out->addHTML(Xml::openElement('div', array('id' => 'pr-preferences')));
     $method = $request->getVal('method');
     // Switch to campaign detail interface if requested
     if ($method == 'listCampaignDetail') {
         $campaign = $request->getVal('campaign');
         $this->listCampaignDetail($campaign);
         $out->addHTML(Xml::closeElement('div'));
         return;
     }
     // Handle form submissions from "Manage campaigns" or "Add a campaign" interface
     if ($this->editable && $request->wasPosted()) {
         // Check authentication token
         if ($this->getUser()->matchEditToken($request->getVal('authtoken'))) {
             // Handle adding a campaign
             if ($method == 'addCampaign') {
                 $campaignName = $request->getVal('campaignName');
                 if ($campaignName == '') {
                     $this->showError('promoter-null-string');
                 } else {
                     //$result = AdCampaign::addCampaign( $campaignName, '0', '0', '0', $this->getUser() );
                     $result = AdCampaign::addCampaign($campaignName, '0', $this->getUser());
                     if (is_string($result)) {
                         $this->showError($result);
                     }
                 }
                 // Handle changing settings to existing campaigns
             } else {
                 // Handle archiving campaigns
                 $toArchive = $request->getArray('archiveCampaigns');
                 if ($toArchive) {
                     // Archive campaigns in list
                     foreach ($toArchive as $campaign) {
                         AdCampaign::setBooleanCampaignSetting($campaign, 'archived', 1);
                     }
                 }
                 // Get all the initial campaign settings for logging
                 $allCampaignNames = AdCampaign::getAllCampaignNames();
                 $allInitialCampaignSettings = array();
                 foreach ($allCampaignNames as $campaignName) {
                     $settings = AdCampaign::getCampaignSettings($campaignName);
                     $allInitialCampaignSettings[$campaignName] = $settings;
                 }
                 /*
                 // Handle locking/unlocking campaigns
                 
                 $lockedCampaigns = $request->getArray( 'locked' );
                 if ( $lockedCampaigns ) {
                 	// Build list of campaigns to lock
                 	$unlockedCampaigns = array_diff( AdCampaign::getAllCampaignNames(), $lockedCampaigns );
                 
                 	// Set locked/unlocked flag accordingly
                 	foreach ( $lockedCampaigns as $campaign ) {
                 		AdCampaign::setBooleanCampaignSetting( $campaign, 'locked', 1 );
                 	}
                 	foreach ( $unlockedCampaigns as $campaign ) {
                 		AdCampaign::setBooleanCampaignSetting( $campaign, 'locked', 0 );
                 	}
                 // Handle updates if no post content came through (all checkboxes unchecked)
                 } else {
                 	$allCampaigns = AdCampaign::getAllCampaignNames();
                 	foreach ( $allCampaigns as $campaign ) {
                 		AdCampaign::setBooleanCampaignSetting( $campaign, 'locked', 0 );
                 	}
                 
                 }
                 */
                 // Handle enabling/disabling campaigns
                 $enabledCampaigns = $request->getArray('enabled');
                 if ($enabledCampaigns) {
                     // Build list of campaigns to disable
                     $disabledCampaigns = array_diff(AdCampaign::getAllCampaignNames(), $enabledCampaigns);
                     // Set enabled/disabled flag accordingly
                     foreach ($enabledCampaigns as $campaign) {
                         AdCampaign::setBooleanCampaignSetting($campaign, 'enabled', 1);
                     }
                     foreach ($disabledCampaigns as $campaign) {
                         AdCampaign::setBooleanCampaignSetting($campaign, 'enabled', 0);
                     }
                     // Handle updates if no post content came through (all checkboxes unchecked)
                 } else {
                     $allCampaigns = AdCampaign::getAllCampaignNames();
                     foreach ($allCampaigns as $campaign) {
                         AdCampaign::setBooleanCampaignSetting($campaign, 'enabled', 0);
                     }
                 }
                 // Handle setting priority on campaigns
                 /*
                 $preferredCampaigns = $request->getArray( 'priority' );
                 if ( $preferredCampaigns ) {
                 	foreach ( $preferredCampaigns as $campaign => $value ) {
                 		AdCampaign::setNumericCampaignSetting(
                 			$campaign,
                 			'preferred',
                 			$value,
                 			Promoter::EMERGENCY_PRIORITY,
                 			Promoter::LOW_PRIORITY
                 		);
                 	}
                 }
                 */
                 // Get all the final campaign settings for potential logging
                 foreach ($allCampaignNames as $campaignName) {
                     $finalCampaignSettings = AdCampaign::getCampaignSettings($campaignName);
                     if (!$allInitialCampaignSettings[$campaignName] || !$finalCampaignSettings) {
                         // Condition where the campaign has apparently disappeared mid operations
                         // -- possibly a delete call
                         $diffs = false;
                     } else {
                         $diffs = array_diff_assoc($allInitialCampaignSettings[$campaignName], $finalCampaignSettings);
                     }
                     // If there are changes, log them
                     if ($diffs) {
                         /*
                         $campaignId = AdCampaign::getCampaignId( $campaignName );
                         AdCampaign::logCampaignChange(
                         	'modified',
                         	$campaignId,
                         	$this->getUser(),
                         	$allInitialCampaignSettings[ $campaignName ],
                         	$finalCampaignSettings
                         );
                         */
                     }
                 }
             }
             // If there were no errors, reload the page to prevent duplicate form submission
             if (!$this->promoterError) {
                 $out->redirect($this->getPageTitle()->getLocalURL());
                 return;
             }
         } else {
             $this->showError('sessionfailure');
         }
     }
     // Show list of campaigns
     $this->listCampaigns();
     // End Campaigns tab content
     $out->addHTML(Xml::closeElement('div'));
 }