// Get ID of campaign
 if ($campaignid == "null") {
     $campaignid = phpAds_dbInsertID();
 }
 // Auto-target campaign if adviews purchased and expiration set
 if ($active == 't' && $expire != '0000-00-00' && $views > 0) {
     include phpAds_path . '/libraries/lib-autotargeting.inc.php';
     // Get served adviews for today
     if ($phpAds_config['compact_stats']) {
         $res = phpAds_dbQuery("SELECT SUM(s.views) FROM " . $phpAds_config['tbl_adstats'] . " s INNER JOIN " . $phpAds_config['tbl_banners'] . " b ON (b.bannerid = s.bannerid) WHERE day = '" . date('Y-m-d') . "' AND b.clientid = '" . $campaignid . "'") or phpAds_sqlDie();
         $already_served = phpAds_dbResult($res, 0, 0);
     } else {
         $res = phpAds_dbQuery("SELECT COUNT(*) FROM " . $phpAds_config['tbl_adviews'] . " s INNER JOIN " . $phpAds_config['tbl_banners'] . " b ON (b.bannerid = s.bannerid) WHERE t_stamp >= " . date('Ymd') . "000000 AND b.clientid = '" . $campaignid . "'") or phpAds_sqlDie();
         $already_served = phpAds_dbResult($res, 0, 0);
     }
     $targetviews = phpAds_AutoTargetingGetTarget(phpAds_AutoTargetingPrepareProfile(), $views, mktime(0, 0, 0, $expireMonth, $expireDay, $expireYear), isset($phpAds_config['autotarget_factor']) ? $phpAds_config['autotarget_factor'] : -1, $already_served);
     if (is_array($targetviews)) {
         list($targetviews, ) = $targetviews;
     }
     phpAds_dbQuery("\n\t\t\tUPDATE " . $phpAds_config['tbl_clients'] . "\n\t\t\tSET\n\t\t\t\ttarget = " . $targetviews . "\n\t\t\tWHERE\n\t\t\t\tclientid = " . $campaignid);
 }
 if (isset($move) && $move == 't') {
     // We are moving a client to a campaign
     // Update banners
     $res = phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_banners'] . "\n\t\t\tSET\n\t\t\t\tclientid='" . $campaignid . "'\n\t\t\tWHERE\n\t\t\t\tclientid='" . $clientid . "'\n\t\t\t") or phpAds_sqlDie();
     // Force priority recalculation
     $new_campaing = false;
 }
 // Update targetstats
 if ($targetviews != $target_old) {
     $res = phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_targetstats'] . "\n\t\t\tSET\n\t\t\t\ttarget = '" . $targetviews . "',\n\t\t\t\tmodified = 1\n\t\t\tWHERE\n\t\t\t\tclientid = '" . $campaignid . "' AND\n\t\t\t\tday = " . date('Ymd') . "\n\t\t\t");
require phpAds_path . "/libraries/lib-autotargeting.inc.php";
// Set defaults
$report = '';
// Save views into targetstats table
phpAds_TargetStatsSaveViews();
// Get campaigns that need autotargeting
$res = phpAds_dbQuery("\n\tSELECT\n\t\tclientid,\n\t\tclientname,\n\t\tviews,\n\t\tUNIX_TIMESTAMP(expire) AS expire\n\tFROM\n\t\t" . $phpAds_config['tbl_clients'] . "\n\tWHERE\n\t\tparent > 0 AND\n\t\tactive AND\n\t\texpire > NOW() AND\n\t\tviews > 0 AND\n\t\tweight = 0\n\tORDER BY\n\t\tclientid\n\t");
if (phpAds_dbNumRows($res)) {
    // Autotargeting needed
    $report .= "==================================================\n";
    $report .= "AUTOTARGETING STARTED\n";
    $report .= "==================================================\n\n";
    $report .= "--------------------------------------------------\n";
    $report .= "Smoothing factor calculation ";
    // Prepare the average view profile
    $profile = phpAds_AutoTargetingPrepareProfile();
    // Calculate the factor to use on sunday or if it's disabled
    if (!date('w') || !isset($phpAds_config['autotarget_factor']) || $phpAds_config['autotarget_factor'] == -1) {
        $report .= "started\n";
        $report .= "--------------------------------------------------\n";
        $phpAds_config['autotarget_factor'] = phpAds_AutoTargetingCaclulateFactor($profile, $report);
        // Save factor into db
        phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_config'] . "\n\t\t\tSET\n\t\t\t\tautotarget_factor = " . $phpAds_config['autotarget_factor'] . "\n\t\t\tWHERE\n\t\t\t\tconfigid = 0\n\t\t\t");
    } elseif (!phpAds_AutoTargetingProfileSum($profile)) {
        // Disable if a null profile was supplied
        $phpAds_config['autotarget_factor'] = -1;
        $report .= "skipped: supplied profile is null\n\n";
    } else {
        $report .= "skipped: already set\n\n";
    }
    $report .= "--------------------------------------------------\n";