// 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");
        // 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";
    $report .= "Smoothing factor:               " . sprintf('%.2f', $phpAds_config['autotarget_factor']) . "\n";
    $report .= "Today dow:                      " . phpAds_DowToday . "\n";
    $report .= "Today profile value:            " . $profile[phpAds_DowToday] . "\n";
    if ($phpAds_config['autotarget_factor'] != -1) {
        // Targets should not be fully satisfied if using plain autotargeting
        // Smoothing the view profile for later use
        $profile = phpAds_AutoTargetingSmoothProfile($profile, $phpAds_config['autotarget_factor']);
        $report .= "Today smoothed profile value:   " . $profile[phpAds_DowToday] . "\n";
    }
    $report .= "--------------------------------------------------\n\n";
    while ($row = phpAds_dbFetchArray($res)) {
        $target = phpAds_AutoTargetingGetTarget($profile, $row['views'], $row['expire'], $phpAds_config['autotarget_factor']);
        if (is_array($target)) {
            list($target, $debuglog) = $target;
        } else {
            $debuglog = 'no debug info available';
        }
        phpAds_dbQuery("\n\t\t\tUPDATE\n\t\t\t\t" . $phpAds_config['tbl_clients'] . "\n\t\t\tSET\n\t\t\t\ttarget = " . $target . "\n\t\t\tWHERE\n\t\t\t\tclientid = " . $row['clientid'] . "\n\t\t");
        $report .= "\n<b>{$row['clientname']} [id{$row['clientid']}]:</b> {$target} {$debuglog}\n\n";
    }
}
if ($report != '' && $phpAds_config['userlog_priority']) {
    phpAds_userlogAdd(phpAds_actionPriorityAutoTargeting, 0, $report);
}