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);
     }
 }
 /**
  * @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();
 }
 /**
  * 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)));
     }
 }
 public function updateSettingsFields(FieldList $fields)
 {
     $fields->addFieldToTab('Root.Advertisements', new CheckboxField('InheritSettings', _t('Advertisements.INHERIT', 'Inherit parent settings')));
     //		$fields->addFieldToTab('Root.Advertisements', new CheckboxField('UseRandom', _t('Advertisements.USE_RANDOM', 'Use random selection')));
     $fields->addFieldToTab('Root.Advertisements', new NumericField('NumberOfAds', _t('Advertisements.NUM_ADS', 'How many Ads should be returned?')));
     $gf = GridField::create('Advertisements', 'Advertisements', $this->owner->Advertisements(), GridFieldConfig_RelationEditor::create());
     $fields->addFieldToTab('Root.Advertisements', $gf);
     //		$fields->addFieldToTab('Root.Advertisements', new ManyManyPickerField($this->owner, 'Advertisements'));
     $fields->addFieldToTab('Root.Advertisements', $df = new DropdownField('UseCampaignID', 'Use campaign', AdCampaign::get()->map()));
     $df->setEmptyString('-- OR Select campaign --');
 }
 public function getCMSFields()
 {
     $fields = new FieldList();
     $fields->push(new TabSet('Root', new Tab('Main', new TextField('Title', 'Title'), new TextField('TargetURL', 'Target URL'))));
     if ($this->ID) {
         $impressions = $this->getImpressions();
         $clicks = $this->getClicks();
         $fields->addFieldToTab('Root.Main', new ReadonlyField('Impressions', 'Impressions', $impressions), 'Title');
         $fields->addFieldToTab('Root.Main', new ReadonlyField('Clicks', 'Clicks', $clicks), 'Title');
         $fields->addFieldsToTab('Root.Main', array(new UploadField('Image'), new Treedropdownfield('InternalPageID', 'Internal Page Link', 'Page'), new DropdownField('CampaignID', 'Ad Campaign', AdCampaign::get())));
     }
     return $fields;
 }
 function getLinkedCampaignNames()
 {
     $campaignNames = array();
     $dbr = wfGetDB(DB_SLAVE);
     $res = $dbr->select('pr_adlinks', 'cmp_id', array('ad_id' => $this->getId()));
     foreach ($res as $row) {
         $campaignNames[] = AdCampaign::getCampaignName($row->cmp_id);
     }
     return $campaignNames;
 }
 /**
  * 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));
 }
 /**
  * Create form for managing campaign settings (start date, end date, languages, etc.)
  */
 function campaignDetailForm($campaignNameOrId)
 {
     if ($this->editable) {
         $readonly = array();
     } else {
         $readonly = array('disabled' => 'disabled');
     }
     $campaign = AdCampaign::getCampaignSettings($campaignNameOrId);
     if ($campaign) {
         // If there was an error, we'll need to restore the state of the form
         $request = $this->getRequest();
         if ($request->wasPosted()) {
             $isEnabled = $request->getCheck('enabled');
             $isArchived = $request->getCheck('archived');
             //$campaignNameOrId = $request->getText( 'campaign' );
             //$catPageId = $request->getInt( 'catPageId' );
         } else {
             // Defaults
             $isEnabled = $campaign['enabled'] == '1';
             $isArchived = $campaign['archived'] == '1';
             //$catPageId = (int)$campaign[ 'catPageId' ];
         }
         // Build Html
         $htmlOut = '';
         $htmlOut .= Xml::tags('h2', null, $this->msg('promoter-campaign-heading', $campaignNameOrId)->text());
         $htmlOut .= Xml::openElement('table', array('cellpadding' => 9));
         // Rows
         // Allow changing campaign name
         $htmlOut .= Xml::openElement('tr');
         $htmlOut .= Xml::tags('td', array(), Xml::label($this->msg('promoter-campaign-name')->text(), 'campaign'));
         $htmlOut .= Xml::tags('td', array(), Xml::input('campaign', 30, $campaignNameOrId, array_replace($readonly, array('id' => 'campaign'))));
         $htmlOut .= Xml::closeElement('tr');
         /*
         // Linked to Category / Page
         $catTitle = Title::newFromID( $catPageId );
         $catName = $catTitle ? $catTitle->getText() : $this->msg( 'promoter-no-assigned-cat' )->text();
         $htmlOut .= Xml::openElement( 'tr' );
         $htmlOut .= Xml::tags( 'td', array(),
         	Xml::label( $this->msg( 'promoter-campaign-linked-to' )->text(), 'catPageId' ) );
         $htmlOut .= Xml::tags( 'td', array(),
         	Xml::input( 'catPageId', 30, $catName, array_replace( $readonly,
         			array( 'id' => 'catPageId' ) )
         	)
         );
         $htmlOut .= Xml::closeElement( 'tr' );
         */
         // Enabled
         $htmlOut .= Xml::openElement('tr');
         $htmlOut .= Xml::tags('td', array(), Xml::label($this->msg('promoter-enabled')->text(), 'enabled'));
         $htmlOut .= Xml::tags('td', array(), Xml::check('enabled', $isEnabled, array_replace($readonly, array('value' => $campaignNameOrId, 'id' => 'enabled'))));
         $htmlOut .= Xml::closeElement('tr');
         if ($this->editable) {
             // Locked
             $htmlOut .= Xml::openElement('tr');
             $htmlOut .= Xml::tags('td', array(), Xml::label($this->msg('promoter-archive-campaign')->text(), 'archive'));
             $htmlOut .= Xml::tags('td', array(), Xml::check('archive', $isArchived, array('value' => $campaignNameOrId, 'id' => 'archive')));
             $htmlOut .= Xml::closeElement('tr');
         }
         $htmlOut .= Xml::closeElement('table');
         return $htmlOut;
     } else {
         return '';
     }
 }