Ejemplo n.º 1
0
 /**
  * A method to test the getLinkedEmailZoneIds() method.
  *
  * Note that the banner/zone association in this test is
  * performed via the Admin_DA class, as a result of the fact
  * that neither the "zone" or "banner" Entity Service
  * classes support this functionality at the time of writing.
  */
 function testGetLinkedEmailZoneIds()
 {
     $oDataGenerator = new DataGenerator();
     $dalZones = OA_Dal::factoryDAL('zones');
     // Test with invalid input
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds('foo');
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     // Test with invalid campaign IDs
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds(-1);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds(0);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds(2);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     // Generate an owning manager account
     $doAgency = OA_Dal::factoryDO('agency');
     $managerId = $oDataGenerator->generateOne($doAgency);
     // Generate an advertiser account within the manager account
     $doClients = OA_Dal::factoryDO('clients');
     $doClients->agencyid = $managerId;
     $advertiserId = $oDataGenerator->generateOne($doClients);
     // Generate a website account within the manager account
     $doAffiliates = OA_Dal::factoryDO('affiliates');
     $doAffiliates->agencyid = $managerId;
     $websiteId = $oDataGenerator->generateOne($doAffiliates);
     // Generate a campaign, with start and end dates
     $doCampaigns = OA_Dal::factoryDO('campaigns');
     $doCampaigns->clientid = $advertiserId;
     $doCampaigns->activate_time = '2009-02-23 00:00:00';
     $doCampaigns->expire_time = '2009-02-24 23:59:59';
     $campaignId = $oDataGenerator->generateOne($doCampaigns);
     // Generate a banner in the campaign generated above
     $doBanners = OA_Dal::factoryDO('banners');
     $doBanners->campaignid = $campaignId;
     $bannerId = $oDataGenerator->generateOne($doBanners);
     // Test with no linked zones
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds($campaignId);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     // Add two non-email zones, and link the campaign to the zones
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->affiliateid = $websiteId;
     $doZones->delivery = phpAds_ZoneBanner;
     $zoneId1 = $oDataGenerator->generateOne($doZones);
     $result = $dalZones->linkZonesToCampaign(array($zoneId1), $campaignId);
     $this->assertEqual($result, 1);
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->affiliateid = $websiteId;
     $doZones->delivery = phpAds_ZonePopup;
     $zoneId2 = $oDataGenerator->generateOne($doZones);
     $result = $dalZones->linkZonesToCampaign(array($zoneId2), $campaignId);
     $this->assertEqual($result, 1);
     // Test with linked zones that are not email zones
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds($campaignId);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(empty($aResult));
     // Add an email zone, and link the campaign's banner to the zone
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->affiliateid = $websiteId;
     $doZones->delivery = MAX_ZoneEmail;
     $zoneId3 = $oDataGenerator->generateOne($doZones);
     $aParameters = array('zone_id' => $zoneId3, 'ad_id' => $bannerId);
     $result = Admin_DA::addAdZone($aParameters);
     $this->assertTrue($result);
     // Test with linked zones, one of which is an email zone
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds($campaignId);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(!empty($aResult));
     $this->assertEqual(count($aResult), 1);
     $this->assertEqual($aResult[0], $zoneId3);
     // Add another email zone, and link the campaign's banner to the zone
     $doZones = OA_Dal::factoryDO('zones');
     $doZones->affiliateid = $websiteId;
     $doZones->delivery = MAX_ZoneEmail;
     $zoneId4 = $oDataGenerator->generateOne($doZones);
     $aParameters = array('zone_id' => $zoneId4, 'ad_id' => $bannerId);
     $result = Admin_DA::addAdZone($aParameters);
     $this->assertTrue($result);
     // Test with linked zones, some of which are email zones
     $aResult = $this->oDalCampaigns->getLinkedEmailZoneIds($campaignId);
     $this->assertTrue(is_array($aResult));
     $this->assertTrue(!empty($aResult));
     $this->assertEqual(count($aResult), 2);
     $this->assertEqual($aResult[0], $zoneId3);
     $this->assertEqual($aResult[1], $zoneId4);
 }
Ejemplo n.º 2
0
 function testdeleteAdZones()
 {
     $this->_generateStats();
     $ret = Admin_DA::addAdZone(array('zone_id' => $this->zoneId, 'ad_id' => $this->bannerId));
     $this->assertTrue(is_int($ret));
     $this->assertTrue($ret > 0);
     Admin_DA::deleteAdZones(array('zone_id' => $this->zoneId, 'ad_id' => $this->adId));
 }
Ejemplo n.º 3
0
 /**
  * A method to link a banner to a zone
  *
  * @param int $zoneId
  * @param int $bannerId
  * @return bool
  */
 function linkBanner($zoneId, $bannerId)
 {
     if ($this->checkIdExistence('zones', $zoneId)) {
         $doZones = OA_Dal::staticGetDO('zones', $zoneId);
         if (!$this->checkPermissions(null, 'affiliates', $doZones->affiliateid, OA_PERM_ZONE_LINK)) {
             return false;
         }
         if ($this->checkIdExistence('banners', $bannerId)) {
             $aLinkedAds = Admin_DA::getAdZones(array('zone_id' => $zoneId), false, 'ad_id');
             if (!isset($aLinkedAds[$bannerId])) {
                 $result = Admin_DA::addAdZone(array('zone_id' => $zoneId, 'ad_id' => $bannerId));
                 if (PEAR::isError($result)) {
                     $this->raiseError($result->getMessage());
                     return false;
                 }
                 return true;
             } else {
                 // Already linked
                 return true;
             }
         }
     }
     return false;
 }
Ejemplo n.º 4
0
function MAX_duplicateAdZones($fromAdId, $toAdId)
{
    $aAdZones = Admin_DA::getAdZones(array('ad_id' => $fromAdId), true, 'zone_id');
    if (!empty($aAdZones)) {
        foreach ($aAdZones as $zoneId => $adId) {
            Admin_DA::addAdZone(array('ad_id' => $toAdId, 'zone_id' => $zoneId));
        }
    }
}
Ejemplo n.º 5
0
 function insert($autoLinkMatchingZones = true)
 {
     $this->_rebuildCache();
     $id = parent::insert();
     if ($id) {
         // add default zone
         $aVariables = array('ad_id' => $id, 'zone_id' => 0);
         Admin_DA::addAdZone($aVariables);
         if ($autoLinkMatchingZones) {
             MAX_addDefaultPlacementZones($id, $this->campaignid);
         }
     }
     return $id;
 }
Ejemplo n.º 6
0
 /**
  * A method to insert the default data into the database.
  *
  * The default data are:
  *
  *  - Test Advertiser 1
  *    - Placement 11
  *      - High Priority
  *      - Daily target of 120 impressions (5 per
  *        hour assuming even delivery each hour)
  *        - Advertisement 111
  *          - Banner Weight 1
  *    - Placement 12
  *      - High Priority
  *      - Runs from 2005-01-01 to 2005-12-31
  *      - Total target of 87,600 (10 per hour
  *        assuming even delivery each hour)
  *        - Advertisement 121
  *          - Banner Weight 2
  *        - Advertisement 122
  *          - Banner Weight 1
  *
  *  - Test Advertiser 2
  *    - Placement 21
  *    - Placement 22
  *
  *  - Test Publisher 1
  *    - Zone 11
  *    - Zone 12
  *
  *  - Test Publisher 2
  *    - Zone 21
  *    - Zone 22
  *
  * - Advertisement 111 is linked to Zone 11
  * - Advertisement 121 is linked to Zone 21
  * - Advertisement 122 is linked to Zone 21 AND Zone 22
  *
  * @static
  * @access public
  * @TODO Complete the specification of the default data and the implementation
  *       of the creation thereof.
  */
 function insertDefaultData()
 {
     $oDbh =& OA_DB::singleton();
     // Set now
     $oServiceLocator =& OA_ServiceLocator::instance();
     $oldNow = $oServiceLocator->get('now');
     $oServiceLocator->register('now', new Date('2005-03-01'));
     // Add a default agency
     $agencyID = Admin_DA::addAgency(array('name' => 'Test Agency', 'contact' => 'Contact Name', 'username' => 'agency', 'email' => '*****@*****.**', 'active' => 1));
     // Add two advertisers for the agency
     $advertiserOneID = Admin_DA::addAdvertiser(array('agencyid' => $agencyID, 'clientname' => 'Test Advertiser 1', 'contact' => 'Contact Name 1', 'clientusername' => 'advertiser1', 'email' => '*****@*****.**'));
     $advertiserTwoID = Admin_DA::addAdvertiser(array('agencyid' => $agencyID, 'clientname' => 'Test Advertiser 2', 'contact' => 'Contact Name 2', 'clientusername' => 'advertiser2', 'email' => '*****@*****.**'));
     // Add the advertiser's placements (campaigns) & advertisements
     $campaignOneOneID = Admin_DA::addPlacement(array('campaignname' => 'Campaign 11 - Manual Daily Target of 120', 'clientid' => $advertiserOneID, 'views' => -1, 'clicks' => -1, 'conversions' => -1, 'status' => OA_ENTITY_STATUS_RUNNING, 'priority' => 2, 'target_impression' => 120, 'target_click' => -1, 'target_conversion' => -1));
     $adOneOneOneID = Admin_DA::addAd(array('campaignid' => $campaignOneOneID, 'description' => 'Advertisement 111', 'active' => 't', 'weight' => 1, 'htmltemplate' => '', 'url' => '', 'bannertext' => '', 'compiledlimitation' => '', 'append' => ''));
     $campaignOneTwoID = Admin_DA::addPlacement(array('campaignname' => 'Campaign 22 - Auto Distribution of 87,600 Impressions', 'clientid' => $advertiserOneID, 'views' => 87600, 'clicks' => -1, 'conversions' => -1, 'status' => OA_ENTITY_STATUS_RUNNING, 'priority' => 2, 'target_impression' => -1, 'target_click' => -1, 'target_conversion' => -1, 'activate_time' => '2005-01-01 00:00:00', 'expire_time' => '2005-12-31 23:59:59'));
     $adOneTwoOneID = Admin_DA::addAd(array('campaignid' => $campaignOneTwoID, 'description' => 'Advertisement 121', 'active' => 't', 'weight' => 2, 'htmltemplate' => '', 'url' => '', 'bannertext' => '', 'compiledlimitation' => '', 'append' => ''));
     $adOneTwoTwoID = Admin_DA::addAd(array('campaignid' => $campaignOneTwoID, 'description' => 'Advertisement 122', 'active' => 't', 'weight' => 1, 'htmltemplate' => '', 'url' => '', 'bannertext' => '', 'compiledlimitation' => '', 'append' => ''));
     // Add two publishers for the agency
     $publisherOneID = Admin_DA::addPublisher(array('agencyid' => $agencyID, 'name' => 'Test Publisher 1', 'contact' => 'Contact Name 1', 'username' => 'publisher1', 'email' => '*****@*****.**'));
     $publisherTwoID = Admin_DA::addPublisher(array('agencyid' => $agencyID, 'name' => 'Test Publisher 1', 'contact' => 'Contact Name 1', 'username' => 'publisher1', 'email' => '*****@*****.**'));
     // Add the publisher's zones
     $zoneOneOneID = Admin_DA::addZone(array('affiliateid' => $publisherOneID, 'zonename' => 'Zone 11', 'type' => 0, 'category' => '', 'ad_selection' => '', 'chain' => '', 'prepend' => '', 'append' => '', 'what' => ''));
     $zoneOneTwoID = Admin_DA::addZone(array('affiliateid' => $publisherOneID, 'zonename' => 'Zone 12', 'type' => 0, 'category' => '', 'ad_selection' => '', 'chain' => '', 'prepend' => '', 'append' => '', 'what' => ''));
     $zoneTwoOneID = Admin_DA::addZone(array('affiliateid' => $publisherOneID, 'zonename' => 'Zone 21', 'type' => 0, 'category' => '', 'ad_selection' => '', 'chain' => '', 'prepend' => '', 'append' => '', 'what' => ''));
     $zoneTwoTwoID = Admin_DA::addZone(array('affiliateid' => $publisherOneID, 'zonename' => 'Zone 22', 'type' => 0, 'category' => '', 'ad_selection' => '', 'chain' => '', 'prepend' => '', 'append' => '', 'what' => ''));
     // Link the ads to the zones
     Admin_DA::addAdZone(array('ad_id' => $adOneOneOneID, 'zone_id' => $zoneOneOneID, 'link_type' => 1));
     Admin_DA::addAdZone(array('ad_id' => $adOneTwoOneID, 'zone_id' => $zoneTwoOneID, 'link_type' => 1));
     Admin_DA::addAdZone(array('ad_id' => $adOneTwoTwoID, 'zone_id' => $zoneTwoOneID, 'link_type' => 1));
     Admin_DA::addAdZone(array('ad_id' => $adOneTwoTwoID, 'zone_id' => $zoneTwoTwoID, 'link_type' => 1));
     // Restore "now"
     if ($oldNow) {
         $oServiceLocator->register('now', $oldNow);
     } else {
         $oServiceLocator->remove('now');
     }
 }
Ejemplo n.º 7
0
 function addPlacementZone($aVariables, $autoLinkMatchingBanners = true)
 {
     if (!($pzaId = Admin_DA::_addEntity('placement_zone_assoc', $aVariables))) {
         return false;
     }
     if (!$autoLinkMatchingBanners) {
         return $pzaId;
     }
     // Selects ads which belongs to the campaign (placement) and fit into
     // the zone. Then links all those ads to the zone if they are not linked already.
     $azParams = Admin_DA::getLinkedAdParams($aVariables['zone_id']);
     $azParams['placement_id'] = $aVariables['placement_id'];
     $azParams['market_ads_include'] = true;
     $azAds = Admin_DA::getAds($azParams);
     $azLinkedAds = Admin_DA::getAdZones(array('zone_id' => $aVariables['zone_id']), false, 'ad_id');
     foreach ($azAds as $adId => $azAd) {
         if (!isset($azLinkedAds[$adId])) {
             Admin_DA::addAdZone(array('zone_id' => $aVariables['zone_id'], 'ad_id' => $adId));
         }
     }
     return $pzaId;
 }
Ejemplo n.º 8
0
/**
 * Processes submit values of zone form
 *
 * @param OA_Admin_UI_Component_Form $form form to process
 * @return An array of Pear::Error objects if any
 */
function processForm($form)
{
    $aFields = $form->exportValues();
    switch ($aFields['delivery']) {
        case phpAds_ZoneText:
            $aFields['width'] = $aFields['height'] = 0;
            break;
        case OX_ZoneVideoOverlay:
            $aFields['width'] = $aFields['height'] = -2;
            break;
        case OX_ZoneVideoInstream:
            $aFields['width'] = $aFields['height'] = -3;
            break;
        default:
            if ($aFields['sizetype'] == 'custom') {
                if (isset($aFields['width']) && $aFields['width'] == '*') {
                    $aFields['width'] = -1;
                }
                if (isset($aFields['height']) && $aFields['height'] == '*') {
                    $aFields['height'] = -1;
                }
            } else {
                list($aFields['width'], $aFields['height']) = explode('x', $aFields['size']);
            }
            break;
    }
    if (!is_numeric($aFields['oac_category_id']) || $aFields['oac_category_id'] <= 0) {
        $aFields['oac_category_id'] = 'NULL';
    }
    if (empty($errors)) {
        // Edit
        if (!empty($aFields['zoneid'])) {
            // before we commit any changes to db, store whether the size has changed
            $aZone = Admin_DA::getZone($aFields['zoneid']);
            $size_changed = $aFields['width'] != $aZone['width'] || $aFields['height'] != $aZone['height'] ? true : false;
            $type_changed = $aFields['delivery'] != $aZone['delivery'] ? true : false;
            $doZones = OA_Dal::factoryDO('zones');
            $doZones->zonename = $aFields['zonename'];
            $doZones->description = $aFields['description'];
            $doZones->width = $aFields['width'];
            $doZones->height = $aFields['height'];
            $doZones->comments = $aFields['comments'];
            $doZones->delivery = $aFields['delivery'];
            if ($aFields['delivery'] != phpAds_ZoneText && $aFields['delivery'] != phpAds_ZoneBanner) {
                $doZones->append = '';
            }
            if ($aFields['delivery'] != phpAds_ZoneText) {
                $doZones->prepend = '';
            }
            $doZones->oac_category_id = $aFields['oac_category_id'];
            $doZones->zoneid = $aFields['zoneid'];
            $doZones->update();
            // Ad  Networks
            $doPublisher = OA_Dal::factoryDO('affiliates');
            $doPublisher->get($aFields['affiliateid']);
            $anWebsiteId = $doPublisher->as_website_id;
            if ($anWebsiteId) {
                $oAdNetworks = new OA_Central_AdNetworks();
                $doZones->get($aFields['zoneid']);
                $oAdNetworks->updateZone($doZones, $anWebsiteId);
            }
            // Reset append codes which called this zone
            $doZones = OA_Dal::factoryDO('zones');
            $doZones->appendtype = phpAds_ZoneAppendZone;
            if (OA_Permission::isAccount(OA_ACCOUNT_MANAGER)) {
                $doZones->addReferenceFilter('agency', OA_Permission::getEntityId());
            } elseif (OA_Permission::isAccount(OA_ACCOUNT_TRAFFICKER)) {
                $doZones->addReferenceFilter('affiliates', OA_Permission::getEntityId());
            }
            $doZones->find();
            while ($doZones->fetch() && ($row = $doZones->toArray())) {
                $append = phpAds_ZoneParseAppendCode($row['append']);
                if ($append[0]['zoneid'] == $aFields['zoneid']) {
                    $doZonesClone = clone $doZones;
                    $doZonesClone->appendtype = phpAds_ZoneAppendRaw;
                    $doZonesClone->append = '';
                    $doZonesClone->update();
                }
            }
            if ($type_changed && $aFields['delivery'] == MAX_ZoneEmail) {
                // Unlink all campaigns/banners linked to this zone
                $aPlacementZones = Admin_DA::getPlacementZones(array('zone_id' => $aFields['zoneid']), true, 'placement_id');
                if (!empty($aPlacementZones)) {
                    foreach ($aPlacementZones as $placementId => $aPlacementZone) {
                        Admin_DA::deletePlacementZones(array('zone_id' => $aFields['zoneid'], 'placement_id' => $placementId));
                    }
                }
                $aAdZones = Admin_DA::getAdZones(array('zone_id' => $aFields['zoneid']), false, 'ad_id');
                if (!empty($aAdZones)) {
                    foreach ($aAdZones as $adId => $aAdZone) {
                        Admin_DA::deleteAdZones(array('zone_id' => $aFields['zoneid'], 'ad_id' => $adId));
                    }
                }
            } else {
                if ($size_changed) {
                    $aZone = Admin_DA::getZone($aFields['zoneid']);
                    // Loop through all appended banners and make sure that they still fit...
                    $aAds = Admin_DA::getAdZones(array('zone_id' => $aFields['zoneid']), false, 'ad_id');
                    if (!empty($aAds)) {
                        foreach ($aAds as $adId => $aAd) {
                            $aAd = Admin_DA::getAd($adId);
                            if ($aZone['type'] == phpAds_ZoneText && $aAd['type'] != 'txt' || $aAd['width'] != $aZone['width'] && $aZone['width'] > -1 || $aAd['height'] != $aZone['height'] && $aZone['height'] > -1) {
                                Admin_DA::deleteAdZones(array('zone_id' => $aFields['zoneid'], 'ad_id' => $adId));
                            }
                        }
                    }
                    // Check if any campaigns linked to this zone have ads that now fit.
                    // If so, link them to the zone.
                    $aPlacementZones = Admin_DA::getPlacementZones(array('zone_id' => $aFields['zoneid']), true);
                    if (!empty($aPlacementZones)) {
                        foreach ($aPlacementZones as $aPlacementZone) {
                            // get ads in this campaign
                            $aAds = Admin_DA::getAds(array('placement_id' => $aPlacementZone['placement_id']), true);
                            foreach ($aAds as $adId => $aAd) {
                                Admin_DA::addAdZone(array('zone_id' => $aFields['zoneid'], 'ad_id' => $adId));
                            }
                        }
                    }
                }
            }
            // Queue confirmation message
            $translation = new OX_Translation();
            $translated_message = $translation->translate($GLOBALS['strZoneHasBeenUpdated'], array(MAX::constructURL(MAX_URL_ADMIN, "zone-edit.php?affiliateid=" . $aFields['affiliateid'] . "&zoneid=" . $aFields['zoneid']), htmlspecialchars($aFields['zonename'])));
            OA_Admin_UI::queueMessage($translated_message, 'local', 'confirm', 0);
            OX_Admin_Redirect::redirect("zone-edit.php?affiliateid=" . $aFields['affiliateid'] . "&zoneid=" . $aFields['zoneid']);
        } else {
            $doZones = OA_Dal::factoryDO('zones');
            $doZones->affiliateid = $aFields['affiliateid'];
            $doZones->zonename = $aFields['zonename'];
            $doZones->zonetype = phpAds_ZoneCampaign;
            $doZones->description = $aFields['description'];
            $doZones->comments = $aFields['comments'];
            $doZones->width = $aFields['width'];
            $doZones->height = $aFields['height'];
            $doZones->delivery = $aFields['delivery'];
            $doZones->oac_category_id = $aFields['oac_category_id'];
            // The following fields are NOT NULL but do not get values set in the form.
            // Should these fields be changed to NULL in the schema or should they have a default value?
            $doZones->category = '';
            $doZones->ad_selection = '';
            $doZones->chain = '';
            $doZones->prepend = '';
            $doZones->append = '';
            $aFields['zoneid'] = $doZones->insert();
            // Ad  Networks
            $doPublisher = OA_Dal::factoryDO('affiliates');
            $doPublisher->get($aFields['affiliateid']);
            $anWebsiteId = $doPublisher->as_website_id;
            if ($anWebsiteId) {
                $oAdNetworks = new OA_Central_AdNetworks();
                $oAdNetworks->updateZone($doZones, $anWebsiteId);
            }
            // Queue confirmation message
            $translation = new OX_Translation();
            $translated_message = $translation->translate($GLOBALS['strZoneHasBeenAdded'], array(MAX::constructURL(MAX_URL_ADMIN, 'zone-edit.php?affiliateid=' . $aFields['affiliateid'] . '&zoneid=' . $aFields['zoneid']), htmlspecialchars($aFields['zonename'])));
            OA_Admin_UI::queueMessage($translated_message, 'local', 'confirm', 0);
            OX_Admin_Redirect::redirect("affiliate-zones.php?affiliateid=" . $aFields['affiliateid']);
        }
    }
    return $errors;
}
Ejemplo n.º 9
0
                    Admin_DA::deleteAdZones($aParameters);
                }
            } else {
                // Remove this key, because it is already there and does not need to be added again.
                unset($aCurrent[$id]);
            }
        }
    }
    $addResult = true;
    if (!empty($aCurrent)) {
        foreach ($aCurrent as $id => $value) {
            $aVariables = array('zone_id' => $zoneId, $key => $id);
            if ($view == 'placement') {
                $addResult = Admin_DA::addPlacementZone($aVariables);
            } else {
                $addResult = Admin_DA::addAdZone($aVariables);
            }
        }
    }
    if (!$addResult) {
        Header("Location: zone-include.php?affiliateid={$publisherId}&zoneid={$zoneId}");
        exit;
    }
    // Move on to the next page
    Header("Location: zone-probability.php?affiliateid={$publisherId}&zoneid={$zoneId}");
    exit;
}
// Display initial parameters...
$tabIndex = 1;
$aOtherPublishers = Admin_DA::getPublishers(array('agency_id' => $agencyId));
$aOtherZones = Admin_DA::getZones(array('publisher_id' => $publisherId));
Ejemplo n.º 10
0
 /**
  * A method to subscribe one or more websites to the Ad Networks program
  *
  * @see R-AN-3: Gathering the data of Websites during Installation
  * @see R-AN-4: Creation of the Ad Networks Entities
  * @see R-AN-5: Generation of Campaigns and Banners
  *
  * @todo Implement rollback
  *
  * @param array $aWebsites
  * @return mixed True on success, PEAR_Error otherwise
  */
 function subscribeWebsites(&$aWebsites)
 {
     $aPref = $GLOBALS['_MAX']['PREF'];
     $oDbh = OA_DB::singleton();
     $aSubscriptions = $this->oMapper->subscribeWebsites($aWebsites);
     if (PEAR::isError($aSubscriptions)) {
         return $aSubscriptions;
     }
     if (!$this->oDal->beginTransaction()) {
         return new PEAR_Error('Cannot start transaction');
     }
     // Simulate transactions
     $aCreated = array('publishers' => array(), 'advertisers' => array(), 'campaigns' => array(), 'banners' => array(), 'zones' => array());
     $aAdNetworks = array();
     $ok = true;
     foreach ($aSubscriptions['adnetworks'] as $aAdvertiser) {
         $doAdvertisers = OA_Dal::factoryDO('clients');
         $doAdvertisers->an_adnetwork_id = $aAdvertiser['adnetwork_id'];
         $doAdvertisers->find();
         if ($doAdvertisers->fetch()) {
             // Advertiser for this adnetwork already exists
             $aAdNetworks[$aAdvertiser['adnetwork_id']] = $doAdvertisers->toArray();
         } else {
             // Create advertiser
             $advertiserName = $this->oDal->getUniqueAdvertiserName($aAdvertiser['name']);
             $advertiser = array('clientname' => $advertiserName, 'contact' => $aPref['admin_name'], 'email' => $aPref['admin_email'], 'an_adnetwork_id' => $aAdvertiser['adnetwork_id']);
             $doAdvertisers = OA_Dal::factoryDO('clients');
             $doAdvertisers->setFrom($advertiser);
             $advertiserId = $doAdvertisers->insert();
             if (!empty($advertiserId)) {
                 $aCreated['advertisers'][] = $advertiserId;
                 $aAdNetworks[$aAdvertiser['adnetwork_id']] = $advertiser + array('clientid' => $advertiserId);
             } else {
                 $ok = false;
             }
         }
     }
     $isAlexaDataFailed = false;
     for (reset($aSubscriptions['websites']); $ok && ($aWebsite = current($aSubscriptions['websites'])); next($aSubscriptions['websites'])) {
         $isAlexaDataFailed = $aWebsite['isAlexaDataFailed'] ? $aWebsite['isAlexaDataFailed'] : $isAlexaDataFailed;
         // Create new or use existing publisher
         $websiteIdx = key($aWebsites);
         foreach ($aWebsites as $key => $value) {
             if ($value['url'] == $aWebsite['url']) {
                 $websiteIdx = $key;
             }
         }
         $existingPublisher = !empty($aWebsites[$websiteIdx]['id']);
         $doPublishers = OA_Dal::factoryDO('affiliates');
         if ($existingPublisher) {
             $doPublishers->get($aWebsites[$websiteIdx]['id']);
             $publisher = array();
             $publisherName = $doPublishers->name;
         } else {
             $publisherName = $this->oDal->getUniquePublisherName($aWebsite['url']);
             $publisher = array('name' => $publisherName, 'website' => 'http://' . $aWebsite['url'], 'mnemonic' => '', 'contact' => $aPref['admin_name'], 'email' => $aPref['admin_email'], 'oac_country_code' => $aWebsites[$websiteIdx]['country'], 'oac_language_id' => $aWebsites[$websiteIdx]['language'], 'oac_category_id' => $aWebsites[$websiteIdx]['category']);
         }
         // an_website_id equal as_website_id
         //            if ($aWebsites[$websiteIdx]['adnetworks']) {
         //                $publisher += array(
         //                    'an_website_id'   => $aWebsite['website_id'],
         //                );
         //            }
         if ($aWebsites[$websiteIdx]['advsignup']) {
             $publisher += array('as_website_id' => $aWebsite['website_id']);
         }
         $doPublishers->setFrom($publisher);
         if ($existingPublisher) {
             $publisherId = $doPublishers->update() ? $aWebsites[$websiteIdx]['id'] : '';
         } else {
             $publisherId = $doPublishers->insert();
             $aWebsites[$websiteIdx]['id'] = $publisherId;
         }
         if (!empty($publisherId)) {
             if (!$existingPublisher) {
                 $aCreated['publishers'][] = $publisherId;
             }
             // Lookup the existing zone sizes for this publisher
             $aZones = array();
             $doZones = OA_Dal::factoryDO('zones');
             $doZones->affiliateid = $publisherId;
             $doZones->find();
             while ($doZones->fetch()) {
                 $zoneSize = $doZones->width . 'x' . $doZones->height;
                 $aZones[$zoneSize][] = $doZones->zoneid;
             }
         } else {
             $ok = false;
         }
         for (reset($aWebsite['campaigns']); $ok && ($aCampaign = current($aWebsite['campaigns'])); next($aWebsite['campaigns'])) {
             // Create campaign
             if (!isset($aAdNetworks[$aCampaign['adnetwork_id']])) {
                 $ok = false;
                 break;
             }
             $advertiserId = $aAdNetworks[$aCampaign['adnetwork_id']]['clientid'];
             $advertiserName = $aAdNetworks[$aCampaign['adnetwork_id']]['clientname'];
             $campaignName = $this->oDal->getUniqueCampaignName("{$advertiserName} - {$aCampaign['name']} - {$publisherName}");
             $campaignStatus = !empty($aCampaign['status']) ? OA_ENTITY_STATUS_PENDING : OA_ENTITY_STATUS_RUNNING;
             $campaign = array('campaignname' => $campaignName, 'clientid' => $advertiserId, 'weight' => $aCampaign['weight'], 'block' => $aCampaign['block'], 'capping' => $aCampaign['capping'], 'session_capping' => $aCampaign['session_capping'], 'an_campaign_id' => $aCampaign['campaign_id'], 'status' => $campaignStatus, 'an_status' => empty($aCampaign['status']) ? OA_ENTITY_ADNETWORKS_STATUS_RUNNING : $aCampaign['status']);
             $doCampaigns = OA_Dal::factoryDO('campaigns');
             $doCampaigns->setFrom($campaign);
             $campaignId = $doCampaigns->insert();
             if (!empty($campaignId)) {
                 $aCreated['campaigns'][] = $campaignId;
             } else {
                 $ok = false;
             }
             for (reset($aCampaign['banners']); $ok && ($aBanner = current($aCampaign['banners'])); next($aCampaign['banners'])) {
                 // Create banner
                 $bannerName = $this->oDal->getUniqueBannerName("{$advertiserName} - {$aBanner['name']}");
                 $banner = array('description' => $bannerName, 'campaignid' => $campaignId, 'width' => $aBanner['width'], 'height' => $aBanner['height'], 'block' => $aBanner['block'], 'capping' => $aBanner['capping'], 'session_capping' => $aBanner['session_capping'], 'storagetype' => 'html', 'contenttype' => 'html', 'htmltemplate' => $aBanner['html'], 'adserver' => $aBanner['adserver'], 'an_banner_id' => $aBanner['banner_id']);
                 $doBanners = OA_Dal::factoryDO('banners');
                 $doBanners->setFrom($banner);
                 $bannerId = $doBanners->insert();
                 if (!empty($bannerId)) {
                     $aCreated['banners'][] = $bannerId;
                     $zoneSize = "{$aBanner['width']}x{$aBanner['height']}";
                     if (!empty($aZones[$zoneSize])) {
                         $zoneIds = $aZones[$zoneSize];
                     } else {
                         // Create zone
                         $zoneName = $this->oDal->getUniqueZoneName("{$publisherName} - {$zoneSize}");
                         $zone = array('zonename' => $zoneName, 'affiliateid' => $publisherId, 'width' => $aBanner['width'], 'height' => $aBanner['height']);
                         $doZones = OA_Dal::factoryDO('zones');
                         $doZones->setFrom($zone);
                         $zoneId = $doZones->insert();
                         $aZones[$zoneSize][] = $zoneId;
                     }
                     foreach ($aZones[$zoneSize] as $idx => $zoneId) {
                         // Link banner to zone
                         $aVariables = array('ad_id' => $bannerId, 'zone_id' => $zoneId);
                         $result = Admin_DA::addAdZone($aVariables);
                         if (PEAR::isError($result)) {
                             $ok = false;
                         }
                     }
                 } else {
                     $ok = false;
                 }
             }
         }
         // Add zones to central.
         $this->updateZones($publisherId);
     }
     if (!$ok) {
         if (!$this->oDal->rollback()) {
             $this->oDal->undoEntities($aCreated);
         }
         return new PEAR_Error('There was an error storing the data on the database');
     }
     $return = array('commit' => $this->oDal->commit(), 'isAlexaDataFailed' => $isAlexaDataFailed);
     return $return;
 }