/**
  * @param string $campaignName
  * @param bool $anonymous
  * @throws NoAdsMatchingCriteriaException
  * @throws NoFallbackCampaign
  * @throws FallbackCampaignDisabled
  */
 function __construct($campaignName, $anonymous = false)
 {
     global $wgPromoterFallbackCampaign;
     $this->anonymous = $anonymous;
     $campaign = new AdCampaign($campaignName);
     if (!AdCampaign::campaignExists($campaignName) || !$campaign->isEnabled()) {
         /* If the selected campaign doesn't exist or is disabled, fallback: */
         $campaign = new AdCampaign($wgPromoterFallbackCampaign);
         if (!AdCampaign::campaignExists($wgPromoterFallbackCampaign)) {
             throw new NoFallbackCampaign();
         } elseif (!$campaign->isEnabled()) {
             throw new FallbackCampaignDisabled();
         }
     }
     $this->campaignName = $campaign->getName();
     $this->ads = $campaign->getAds();
     $this->filterAds();
     if (count($this->ads) < 1) {
         throw new NoAdsMatchingCriteriaException($this->campaignName);
     }
     /*
     echo '<pre dir="ltr">';
     print_r( $this->ads );
     echo '</pre>';
     */
     $this->allocate();
     //$chosenAd = $this->chooseAd();
 }
 function execute($par)
 {
     $this->getParams();
     $html = '';
     $error = false;
     $renderedAds = array();
     try {
         $campaign = new AdCampaign($this->campaignName);
         $ads = $campaign->getAds();
         foreach ($ads as $ad) {
             $renderedAds[] = Ad::fromName($ad['name'])->renderHtml();
         }
     } catch (MWException $e) {
         wfDebugLog('Promoter', $e->getMessage());
         $error = $e->getMessage();
     }
     if ($this->isPreview) {
         $this->setHeaders();
         if ($error) {
             $html = "Exception {$error}.";
         } else {
             $html = '<div id="adPreview clearfix">';
             foreach ($renderedAds as $ad) {
                 $html .= '<div class="col-sm-4">' . $ad . '</div>';
             }
             $html .= '</div>';
         }
         $this->getOutput()->addHTML($html);
     } else {
         $this->getOutput()->disable();
         $this->sendHeaders();
         print_r($renderedAds);
     }
 }
 /**
  * Return settings for a campaign
  *
  * @param $campaignName string: The name of the campaign
  *
  * @return array|bool an array of settings or false if the campaign does not exist
  */
 static function getCampaignSettings($campaignName)
 {
     $dbr = PRDatabase::getDb();
     // Get campaign info from database
     $row = $dbr->selectRow(array('campaigns' => 'pr_campaigns'), array('cmp_id', 'cmp_enabled', 'cmp_archived'), array('cmp_name' => $campaignName), __METHOD__);
     if ($row) {
         $campaign = array('enabled' => $row->cmp_enabled, 'archived' => $row->cmp_archived);
     } else {
         return false;
     }
     $campaignObj = new AdCampaign($campaignName);
     $adsIn = $campaignObj->getAds();
     $adsOut = array();
     // All we want are the ad names and weights
     foreach ($adsIn as $key => $row) {
         $outKey = $adsIn[$key]['name'];
         $adsOut[$outKey]['weight'] = $adsIn[$key]['weight'];
     }
     // Encode into a JSON string for storage
     $campaign['ads'] = FormatJson::encode($adsOut);
     return $campaign;
 }