Example #1
0
 /**
  * Returns an array of constraints given an entity with params.
  *
  * @param string $entity
  * @param array $aParams
  * @return array
  */
 function _getLimitations($entity, $aParams)
 {
     if (!empty($aParams) && is_array($aParams)) {
         $aParams = MAX_commonSlashArray($aParams);
     }
     $aLimitations = array();
     // size limitation e.g. ((width = -1 AND height = -1) OR (width = 468 AND height = 60));
     $aSizeLimitations = array();
     $aZoneSize = array();
     if (isset($aParams['ad_width'])) {
         SqlBuilder::_addLimitation($aZoneSize, 'ad_width', 'd.width', $aParams['ad_width']);
     }
     if (isset($aParams['ad_height'])) {
         SqlBuilder::_addLimitation($aZoneSize, 'ad_height', 'd.height', $aParams['ad_height']);
     }
     $zoneSize = implode(' AND ', $aZoneSize);
     if (!empty($zoneSize)) {
         if (!empty($aParams['ad_nosize'])) {
             $aNoSize = array();
             SqlBuilder::_addLimitation($aNoSize, 'ad_width', 'd.width', -1);
             SqlBuilder::_addLimitation($aNoSize, 'ad_height', 'd.height', -1);
             $aSizeLimitations[] = '(' . implode(' AND ', $aNoSize) . ')';
         }
         $aSizeLimitations[] = '(' . $zoneSize . ')';
         $sizeLimitation = implode(' OR ', $aSizeLimitations);
         if (!empty($sizeLimitation)) {
             $aLimitations[] = '(' . $sizeLimitation . ')';
         }
     }
     if (!empty($aParams['ad_type'])) {
         if ($aParams['ad_type'] == "!txt") {
             SqlBuilder::_addLimitation($aLimitations, 'ad_type', 'd.storagetype', 'txt', MAX_LIMITATION_NOT_EQUAL);
         } else {
             SqlBuilder::_addLimitation($aLimitations, 'ad_type', 'd.storagetype', $aParams['ad_type']);
         }
     }
     if (!empty($aParams['ad_active'])) {
         SqlBuilder::_addLimitation($aLimitations, 'ad_active', 'd.status', OA_ENTITY_STATUS_RUNNING, $aParams['ad_active'] == 't' ? MAX_LIMITATION_EQUAL : MAX_LIMITATION_NOT_EQUAL);
     }
     if (!empty($aParams['placement_active'])) {
         SqlBuilder::_addLimitation($aLimitations, 'placement_active', 'm.status', OA_ENTITY_STATUS_RUNNING, $aParams['placement_active'] == 't' ? MAX_LIMITATION_EQUAL : MAX_LIMITATION_NOT_EQUAL);
     }
     if (!empty($aParams['placement_anonymous'])) {
         SqlBuilder::_addLimitation($aLimitations, 'placement_anonymous', 'm.anonymous', $aParams['placement_anonymous']);
     }
     if (!empty($aParams['zone_inventory_forecast_type'])) {
         SqlBuilder::_addLimitation($aLimitations, 'zone_inventory_forecast_type', 'z.inventory_forecast_type', $aParams['zone_inventory_forecast_type'], MAX_LIMITATION_BITWISE);
     }
     switch ($entity) {
         case 'ad':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'd.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             // never return the Market ad entity (in stats, UI)
             if (empty($aParams['market_ads_include'])) {
                 $aLimitations[] = "(d.ext_bannertype <> 'market-optin-banner' OR d.ext_bannertype IS NULL)";
             }
             break;
         case 'advertiser':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'a.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'm.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             // always filter by type
             if (!class_exists(DataObjects_Clients)) {
                 OA_Dal::factoryDO('Clients');
             }
             if (empty($aParams['advertiser_type'])) {
                 $aParams['advertiser_type'] = DataObjects_Clients::ADVERTISER_TYPE_DEFAULT;
                 //always add default type
             } else {
                 $aParams['advertiser_type'] .= "," . DataObjects_Clients::ADVERTISER_TYPE_DEFAULT;
                 //always add default type
             }
             SqlBuilder::_addLimitation($aLimitations, 'advertiser_type', 'a.type', $aParams['advertiser_type']);
             // filter by campaign types only if param given
             if (isset($aParams['campaign_type'])) {
                 if (!class_exists(DataObjects_Campaigns)) {
                     OA_Dal::factoryDO('Campaigns');
                 }
                 if (empty($aParams['campaign_type'])) {
                     $aParams['campaign_type'] = DataObjects_Campaigns::CAMPAIGN_TYPE_DEFAULT;
                     //always add default type
                 } else {
                     $aParams['campaign_type'] .= "," . DataObjects_Campaigns::CAMPAIGN_TYPE_DEFAULT;
                     //always add default type
                 }
                 SqlBuilder::_addLimitation($aLimitations, 'campaign_type', 'm.type', $aParams['campaign_type']);
             }
             break;
         case 'ad_category_assoc':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'c.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'd.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'ac.ad_id', $aParams['ad_id']);
             }
             if (!empty($aParams['ad_category_assoc_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_category_assoc_id', 'ac.ad_category_assoc_id', $aParams['ad_category_assoc_id']);
             }
             break;
         case 'ad_zone_assoc':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'p.agencyid', $aParams['agency_id']);
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'z.affiliateid', $aParams['publisher_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 'az.zone_id', $aParams['zone_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'd.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'az.ad_id', $aParams['ad_id']);
             }
             if (!empty($aParams['ad_zone_assoc_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_zone_assoc_id', 'az.ad_zone_assoc_id', $aParams['ad_zone_assoc_id']);
             }
             break;
         case 'agency':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'g.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'a.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'm.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             if (!empty($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'p.affiliateid', $aParams['publisher_id']);
             }
             if (!empty($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 'z.zoneid', $aParams['zone_id']);
             }
             break;
         case 'campaign':
             if (!empty($aParams['client_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'client_id', 'cam.clientid', $aParams['client_id']);
             }
             break;
         case 'category':
             if (!empty($aParams['name'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'name', 'cat.name', $aParams['name']);
             }
             break;
         case 'channel':
             if (isset($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'ch.affiliateid', $aParams['publisher_id']);
             } elseif (isset($aParams['channel_type']) && $aParams['channel_type'] == 'publisher') {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'ch.affiliateid', 0, MAX_LIMITATION_NOT_EQUAL);
             }
             if (!empty($aParams['channel_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'channel_id', 'ch.channelid', $aParams['channel_id']);
             }
             if (isset($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'agencyid', $aParams['agency_id']);
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'ch.affiliateid', 0);
             }
             break;
         case 'channel_limitation':
             if (!empty($aParams['channel_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'channel_id', 'chl.channelid', $aParams['channel_id']);
             }
             break;
         case 'image':
             if (!empty($aParams['file_name'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'file_name', 'i.filename', $aParams['file_name']);
             }
             break;
         case 'limitation':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'c.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'd.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'l.bannerid', $aParams['ad_id']);
             }
             break;
         case 'placement':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'm.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             // always filter by type
             if (!class_exists(DataObjects_Campaigns)) {
                 OA_Dal::factoryDO('Campaigns');
             }
             if (empty($aParams['campaign_type'])) {
                 $aParams['campaign_type'] = DataObjects_Campaigns::CAMPAIGN_TYPE_DEFAULT;
                 //always add default type
             } else {
                 $aParams['campaign_type'] .= "," . DataObjects_Campaigns::CAMPAIGN_TYPE_DEFAULT;
                 //always add default type
             }
             SqlBuilder::_addLimitation($aLimitations, 'campaign_type', 'm.type', $aParams['campaign_type']);
             break;
         case 'placement_tracker':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'c.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 't.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['tracker_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'tracker_id', 'mt.trackerid', $aParams['tracker_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'mt.campaignid', $aParams['placement_id']);
             }
             break;
         case 'placement_zone_assoc':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'p.agencyid', $aParams['agency_id']);
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'c.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'z.affiliateid', $aParams['publisher_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 'pz.zone_id', $aParams['zone_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'pz.placement_id', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             if (!empty($aParams['placement_zone_assoc_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_zone_assoc_id', 'pz.placement_zone_assoc_id', $aParams['placement_zone_assoc_id']);
             }
             break;
         case 'publisher':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'p.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'p.affiliateid', $aParams['publisher_id']);
             }
             if (!empty($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 'z.zoneid', $aParams['zone_id']);
             }
             break;
         case 'history_span':
         case 'history_day_hour':
         case 'history_day':
         case 'history_month':
         case 'history_dow':
         case 'history_hour':
         case 'stats':
             if (!empty($aParams['agency_id'])) {
                 // join to affiliates if stats by zone
                 if (!empty($aParams['publisher_id']) || !empty($aParams['zone_id']) || isset($aParams['zone_type']) || isset($aParams['zone_width']) || isset($aParams['zone_height'])) {
                     SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'p.agencyid', $aParams['agency_id']);
                 }
                 $adIds = SqlBuilder::_getBannerIdsForAgency($aParams['agency_id']);
                 if (!empty($adIds)) {
                     SqlBuilder::_addLimitation($aLimitations, 'ad_id', 's.ad_id', $adIds);
                 } else {
                     SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
                 }
             }
             if (!empty($aParams['publisher_id'])) {
                 if (!empty($aParams['market_stats'])) {
                     SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 's.website_id', $aParams['publisher_id']);
                 } else {
                     SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'z.affiliateid', $aParams['publisher_id']);
                 }
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 'm.clientid', $aParams['advertiser_id']);
             }
             if (isset($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 's.zone_id', $aParams['zone_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'd.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 's.ad_id', $aParams['ad_id']);
             }
             if (empty($aParams['market_stats_get_start_date'])) {
                 $do = OA_Dal::factoryDO('Clients');
                 // Core stats exclude all Market advertiser activity
                 // Market stats only include the Market advertiser activity
                 if (!empty($aParams['market_stats'])) {
                     $aLimitations[] = 'a.type = ' . DataObjects_Clients::ADVERTISER_TYPE_MARKET;
                     // if we are not explicitely selecting zone_id = 0 stats, we exclude all zone_id stats
                     if (empty($aParams['market_stats_including_zone_zero'])) {
                         $aLimitations[] = 's.zone_id <> 0';
                     }
                 } else {
                     $aLimitations[] = 'a.type = ' . DataObjects_Clients::ADVERTISER_TYPE_DEFAULT;
                 }
                 if (!empty($aParams['custom_table']) && $aParams['custom_table'] == 'data_intermediate_ad_connection') {
                     $dateTimeCol = "s.tracker_date_time";
                 } else {
                     $dateTimeCol = "s.date_time";
                 }
                 if (!empty($aParams['day_begin'])) {
                     $aLimitations[] = "{$dateTimeCol}>='" . SqlBuilder::_dayToDateTime($aParams['day_begin'], true) . "'";
                 }
                 if (!empty($aParams['day_end'])) {
                     $aLimitations[] = "{$dateTimeCol}<='" . SqlBuilder::_dayToDateTime($aParams['day_end'], false) . "'";
                 }
             }
             break;
         case 'stats_by_entity':
             $aLimitations += SqlBuilder::_getLimitations('stats', $aParams);
             if (isset($aParams['zone_id']) && $aParams['zone_id'] == 0) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 's.zone_id', 0);
             }
             break;
         case 'tracker':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 't.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['tracker_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'tracker_id', 't.trackerid', $aParams['tracker_id']);
             }
             if (!empty($aParams['placement_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'placement_id', 'mt.campaignid', $aParams['placement_id']);
             }
             if (!empty($aParams['ad_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'ad_id', 'd.bannerid', $aParams['ad_id']);
             }
             break;
         case 'variable':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'a.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['advertiser_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'advertiser_id', 't.clientid', $aParams['advertiser_id']);
             }
             if (!empty($aParams['tracker_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'tracker_id', 'v.trackerid', $aParams['tracker_id']);
             }
             if (!empty($aParams['variable_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'variable_id', 'v.variableid', $aParams['variable_id']);
             }
             break;
         case 'zone':
             if (!empty($aParams['agency_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'agency_id', 'p.agencyid', $aParams['agency_id']);
             }
             if (!empty($aParams['publisher_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'publisher_id', 'z.affiliateid', $aParams['publisher_id']);
             }
             if (isset($aParams['zone_id'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_id', 'z.zoneid', $aParams['zone_id']);
             }
             if (isset($aParams['zone_type'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_type', 'z.delivery', $aParams['zone_type']);
             }
             if (isset($aParams['zone_width'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_width', 'z.width', $aParams['zone_width']);
             }
             if (isset($aParams['zone_height'])) {
                 SqlBuilder::_addLimitation($aLimitations, 'zone_height', 'z.height', $aParams['zone_height']);
             }
             break;
     }
     return $aLimitations;
 }