/**
  * Pull ads from the database
  */
 function getQueryInfo()
 {
     $dbr = PRDatabase::getDb();
     // First we must construct the filter before we pull ads
     // When the filter comes in it is space delimited, so break that...
     $likeArray = preg_split('/\\s/', $this->filter);
     // ...and then insert all the wildcards betwean search terms
     if (empty($likeArray)) {
         $likeArray = $dbr->anyString();
     } else {
         $anyStringToken = $dbr->anyString();
         $tempArray = array($anyStringToken);
         foreach ($likeArray as $likePart) {
             $tempArray[] = $likePart;
             $tempArray[] = $anyStringToken;
         }
         $likeArray = $tempArray;
     }
     // Get the current campaign and filter on that as well if required
     $campaign = $this->mRequest->getVal('campaign');
     $campaignId = AdCampaign::getCampaignId($campaign);
     if ($campaignId) {
         // Return all the ads not already assigned to the current campaign
         return array('tables' => array('adlinks' => 'pr_adlinks', 'ads' => 'pr_ads'), 'fields' => array('ads.ad_name', 'ads.ad_id'), 'conds' => array('adlinks.ad_id IS NULL', 'ad_name' . $dbr->buildLike($likeArray)), 'join_conds' => array('adlinks' => array('LEFT JOIN', "adlinks.ad_id = ads.ad_id " . "AND adlinks.cmp_id = {$campaignId}")));
     } else {
         // Return all the ads in the database
         return array('tables' => array('ads' => 'pr_ads'), 'fields' => array('ads.ad_name', 'ads.ad_id'), 'conds' => array('ads.ad_name' . $dbr->buildLike($likeArray)));
     }
 }
 /**
  * Updates the weight of a ad in a campaign.
  *
  * @param $campaignName String Name of the campaign to update
  * @param $adId   		Int ID of the ad in the campaign
  * @param $weight       Int New ad weight
  */
 static function updateWeight($campaignName, $adId, $weight)
 {
     $dbw = PRDatabase::getDb();
     $campaignId = AdCampaign::getCampaignId($campaignName);
     $dbw->update('pr_adlinks', array('adl_weight' => $weight), array('ad_id' => $adId, 'cmp_id' => $campaignId));
 }
 /**
  * Show the interface for viewing/editing an individual campaign
  *
  * @param $campaign string The name of the campaign to view
  * @throws ErrorPageError
  */
 function listCampaignDetail($campaign)
 {
     $c = new AdCampaign($campaign);
     // Todo: Convert the rest of this page to use this object
     try {
         if ($c->isArchived()) {
             $this->getOutput()->setSubtitle($this->msg('promoter-archive-edit-prevented'));
             $this->editable = false;
             // Todo: Fix this gross hack to prevent editing
         }
     } catch (AdCampaignExistenceException $ex) {
         throw new ErrorPageError('promoter', 'promoter-campaign-doesnt-exist');
     }
     // Handle form submissions from campaign detail interface
     $request = $this->getRequest();
     if ($this->editable && $request->wasPosted()) {
         // If what we're doing is actually serious (ie: not updating the ad
         // filter); process the request. Recall that if the serious request
         // succeeds, the page will be reloaded again.
         if ($request->getCheck('ad-search') == false) {
             // Check authentication token
             if ($this->getUser()->matchEditToken($request->getVal('authtoken'))) {
                 // Handle removing campaign
                 if ($request->getVal('archive')) {
                     AdCampaign::setBooleanCampaignSetting($campaign, 'archived', 1);
                 }
                 $initialCampaignSettings = AdCampaign::getCampaignSettings($campaign);
                 // Handle enabling/disabling campaign
                 if ($request->getCheck('enabled')) {
                     AdCampaign::setBooleanCampaignSetting($campaign, 'enabled', 1);
                 } else {
                     AdCampaign::setBooleanCampaignSetting($campaign, 'enabled', 0);
                 }
                 // Handle adding of ads to the campaign
                 $adsToAdd = $request->getArray('addAds');
                 if ($adsToAdd) {
                     $weight = $request->getArray('weight');
                     foreach ($adsToAdd as $adName) {
                         $adId = Ad::fromName($adName)->getId();
                         $result = AdCampaign::addAdTo($campaign, $adName, $weight[$adId]);
                         if ($result !== true) {
                             $this->showError($result);
                         }
                     }
                 }
                 // Handle removing of ads from the campaign
                 $adToRemove = $request->getArray('removeAds');
                 if ($adToRemove) {
                     foreach ($adToRemove as $ad) {
                         AdCampaign::removeAdFor($campaign, $ad);
                     }
                 }
                 // Handle weight changes
                 $updatedWeights = $request->getArray('weight');
                 $balanced = $request->getCheck('balanced');
                 if ($updatedWeights) {
                     foreach ($updatedWeights as $adId => $weight) {
                         if ($balanced) {
                             $weight = 25;
                         }
                         AdCampaign::updateWeight($campaign, $adId, $weight);
                     }
                 }
                 $finalCampaignSettings = AdCampaign::getCampaignSettings($campaign);
                 $campaignId = AdCampaign::getCampaignId($campaign);
                 /*
                 AdCampaign::logCampaignChange( 'modified', $campaignId, $this->getUser(),
                 	$initialCampaignSettings, $finalCampaignSettings );
                 */
                 // If there were no errors, reload the page to prevent duplicate form submission
                 if (!$this->promoterError) {
                     $this->getOutput()->redirect($this->getPageTitle()->getLocalUrl(array('method' => 'listCampaignDetail', 'campaign' => $campaign)));
                     return;
                 }
             } else {
                 $this->showError('sessionfailure');
             }
         }
     }
     $htmlOut = '';
     // Begin AdCampaign detail form
     $htmlOut .= Xml::openElement('div', array('class' => 'prefsection'));
     if ($this->editable) {
         $htmlOut .= Xml::openElement('form', array('method' => 'post', 'action' => $this->getPageTitle()->getLocalURL(array('method' => 'listCampaignDetail', 'campaign' => $campaign))));
     }
     $output_detail = $this->campaignDetailForm($campaign);
     $output_assigned = $this->assignedAdsForm($campaign);
     $output_ads = $this->addAdsForm($campaign);
     $htmlOut .= $output_detail;
     // Catch for no ads so that we don't double message
     if ($output_assigned == '' && $output_ads == '') {
         $htmlOut .= $this->msg('promoter-no-ads')->escaped();
         $htmlOut .= Xml::element('p');
         $newPage = $this->getTitleFor('CampaignAd', 'add');
         $htmlOut .= Linker::link($newPage, $this->msg('promoter-add-ad')->escaped());
         $htmlOut .= Xml::element('p');
     } elseif ($output_assigned == '') {
         $htmlOut .= Xml::fieldset($this->msg('promoter-assigned-ads')->text());
         $htmlOut .= $this->msg('promoter-no-ads-assigned')->escaped();
         $htmlOut .= Xml::closeElement('fieldset');
         if ($this->editable) {
             $htmlOut .= $output_ads;
         }
     } else {
         $htmlOut .= $output_assigned;
         if ($this->editable) {
             $htmlOut .= $output_ads;
         }
     }
     if ($this->editable) {
         $htmlOut .= Html::hidden('authtoken', $this->getUser()->getEditToken());
         // Submit button
         $htmlOut .= Xml::tags('div', array('class' => 'pr-buttons'), Xml::submitButton($this->msg('promoter-modify')->text()));
     }
     if ($this->editable) {
         $htmlOut .= Xml::closeElement('form');
     }
     $htmlOut .= Xml::closeElement('div');
     $this->getOutput()->addHTML($htmlOut);
 }