/**
  * Computes all info relative to referer
  * Detects for each different referer URL (or each entry page, when no referer URLs), the name (and keyword),
  * the domain, the precise page, the partner, the newsletter
  * 
  * Saves in var toRecord all precomputed values. Isn't that great ?
  */
 function computeReferer()
 {
     // Init
     $hits = 0;
     $vis_listing = array('vis_search_engine', 'vis_keyword', 'vis_newsletter', 'vis_partner', 'vis_site');
     foreach ($vis_listing as $value) {
         $this->toRecord[$value] = array();
     }
     $resDirect = 0;
     $int_sum = array();
     $int_sum['search_engine'] = array();
     $int_sum['keyword'] = array();
     $int_sum['site'] = array();
     $int_sum['partner_name'] = array();
     $int_sum['newsletter'] = array();
     $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = array();
     $int_sum['type'][REF_TYPE_NEWSLETTER] = array();
     $int_sum['type'][REF_TYPE_PARTNER] = array();
     $int_sum['type'][REF_TYPE_SEARCH_ENGINE] = array();
     $int_sum['type'][REF_TYPE_SITE] = array();
     // query that selects all distinct referer URLs
     $r = query("SELECT count(*) as s, referer as r" . $GLOBALS['test'] . " FROM " . T_VISIT . " WHERE server_date= '" . $this->date->get() . "' " . " AND idsite = " . $this->site->getId() . " " . " AND referer IS NOT NULL" . " GROUP BY r\n\t\t\t\t\t");
     while ($l = mysql_fetch_assoc($r)) {
         // different types :
         // - partner identified as it is
         // - search engines that give keywords
         // - direct access, url belongs to the site
         // - other misc websites
         $hits = $l['s'];
         $refererUrl = $l['r'];
         // now... lets go in identifying !
         // and don't go back it's so important !
         $refererUrlParse = parse_url($refererUrl);
         // if referer exists (and not 'blockedReferer' or other)
         if (isset($refererUrlParse['host'])) {
             $refererHost = $refererUrlParse['host'];
             $refererSH = $refererUrlParse['scheme'] . '://' . $refererUrlParse['host'];
             //printDebug("<b>".$refererHost." </b> ");
             /*
              * search engine
              */
             if (array_key_exists($refererHost, $GLOBALS['searchEngines'])) {
                 // which search engine ?
                 $sname = $GLOBALS['searchEngines'][$refererHost][0];
                 $vname = $GLOBALS['searchEngines'][$refererHost][1];
                 // id base logs to id base archives
                 $ids = $this->objects['search_engine']->getId($sname);
                 // init
                 if (!isset($this->toRecord['vis_search_engine'][$ids])) {
                     $this->toRecord['vis_search_engine'][$ids]['pmv_sum'] = 0;
                     $int_sum['search_engine'][$ids] = array();
                 }
                 $this->toRecord['vis_search_engine'][$ids]['pmv_sum'] += $hits;
                 // interest for current engine
                 $int_sum['search_engine'][$ids] = $this->getInterestValuesSum($int_sum['search_engine'][$ids], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 // interest for the type search_engine
                 $int_sum['type'][REF_TYPE_SEARCH_ENGINE] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_SEARCH_ENGINE], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 // if there is a query, there may be a keyword...
                 if (isset($refererUrlParse['query'])) {
                     /*
                      *  keyword ?
                      */
                     $query = $refererUrlParse['query'];
                     // for google image take the prev variable
                     // and take the q var in this variable once urldecoded
                     // but sometimes we have images.google.fr/search which is exactly like www.google.fr/search....
                     if (ereg("^images.google.", $refererHost)) {
                         //print("befo = ".$query);
                         $query = getUrlParamValue($query, "prev");
                         // case images.google.com/search we replace in the host images by www
                         if ($query === false) {
                             $refererHost = str_replace("images", "www", $refererHost);
                             $vname = $GLOBALS['searchEngines'][$refererHost][1];
                         } else {
                             $query = urldecode($query);
                             // we want the query from "/images?q=+logo+designs&start=360&ndsp=20&svnum=10&hl=en&lr=&sa=N"
                             $query = substr($query, strpos($query, "?") + 1);
                             // getUrlParam need htmlentitied
                             $query = htmlentities($query);
                             //print("<br> after = ".$query);
                         }
                     }
                     // search for keywords now &vname=keyword
                     $key = strtolower(getUrlParamValue($query, $vname));
                     // for search engines that don't use utf-8
                     if (function_exists('iconv') && isset($GLOBALS['searchEngines'][$refererHost][2])) {
                         $charset = trim($GLOBALS['searchEngines'][$refererHost][2]);
                         if (!empty($charset)) {
                             $key = urlencode(@iconv($charset, 'utf-8//TRANSLIT', urldecode($key)));
                         }
                     }
                     //print($refererUrlParse['query'].",". $vname." : ".$key."<br>");
                     // base logs => base archives
                     $idk = $this->objects['keyword']->getId($key);
                     /*
                      * init
                      */
                     if (!isset($this->toRecord['vis_search_engine'][$ids][$idk])) {
                         $this->toRecord['vis_search_engine'][$ids][$idk] = 0;
                     }
                     if (!isset($this->toRecord['vis_keyword'][$idk][$ids])) {
                         $this->toRecord['vis_keyword'][$idk][$ids] = 0;
                         $int_sum['keyword'][$idk] = array();
                     }
                     if (!isset($this->toRecord['vis_keyword'][$idk]['pmv_sum'])) {
                         $this->toRecord['vis_keyword'][$idk]['pmv_sum'] = 0;
                     }
                     $this->toRecord['vis_search_engine'][$ids][$idk] += $hits;
                     $this->toRecord['vis_keyword'][$idk][$ids] += $hits;
                     $this->toRecord['vis_keyword'][$idk]['pmv_sum'] += $hits;
                     $int_sum['keyword'][$idk] = $this->getInterestValuesSum($int_sum['keyword'][$idk], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 }
             } else {
                 if ($this->site->isUrlIn($refererUrl)) {
                     //printDebug("<br>directentry<br>");
                     $resDirect += $hits;
                     // interest for the type 'direct_entry'
                     $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 } else {
                     if ($this->site->isPartner($refererUrl)) {
                         //printDebug("<br>partner<br>");
                         // comments are not necessary here, code is so clear :-D
                         $url = array_search($refererHost, $this->site->getPartnerUrlsFlat());
                         $pname = $this->site->getPartnerName();
                         $idp = $this->objects['partner_name']->getId($pname);
                         $idpu = $this->objects['partner_url']->getId($refererUrl);
                         // init
                         if (!isset($this->toRecord['vis_partner'][$idp]['pmv_sum'])) {
                             $this->toRecord['vis_partner'][$idp]['pmv_sum'] = 0;
                             $int_sum['partner_name'][$idp] = array();
                         }
                         $this->toRecord['vis_partner'][$idp][$idpu] = $hits;
                         $this->toRecord['vis_partner'][$idp]['pmv_sum'] += $hits;
                         //printDebug($this->toRecord['vis_partner']);
                         $int_sum['partner_name'][$idp] = $this->getInterestValuesSum($int_sum['partner_name'][$idp], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                         $int_sum['type'][REF_TYPE_PARTNER] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_PARTNER], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                     } else {
                         //printDebug(" misc site | ");
                         $idmsh = $this->objects['site']->getId($refererHost);
                         $idms = $this->objects['site']->getId($refererUrl);
                         // init
                         if (!isset($this->toRecord['vis_site'][$idmsh]['pmv_sum'])) {
                             $this->toRecord['vis_site'][$idmsh]['pmv_sum'] = 0;
                             $int_sum['site'][$idmsh] = array();
                         }
                         $this->toRecord['vis_site'][$idmsh]['pmv_sum'] += $hits;
                         $this->toRecord['vis_site'][$idmsh][$idms] = $hits;
                         $int_sum['site'][$idmsh] = $this->getInterestValuesSum($int_sum['site'][$idmsh], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                         $int_sum['type'][REF_TYPE_SITE] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_SITE], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                     }
                 }
             }
         } else {
             // direct entry
             $resDirect += $hits;
             //printDebug("<br>directentry<br>");
             $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
         }
     }
     // query that selects entry pages, when there are no referer URls
     // it allows detection of partners, newsletters and direct entries
     $r = query("SELECT count(distinct idvisit) as sp, pu.idpage_url as p, \n\t\t\t\t\t\t\tpu.url as u" . $GLOBALS['test'] . " FROM " . T_VISIT . " as v, " . T_PAGE_URL . " as pu \n\t\t\t\t  WHERE v.entry_idpageurl = pu.idpage_url \n\t\t\t\t  AND v.server_date= '" . $this->date->get() . "' \n\t\t\t\t AND v.idsite = " . $this->site->getId() . " \n\t\t\t\t AND v.referer IS NULL\n\t\t\t\t GROUP by entry_idpageurl\n\t\t\t\t\t");
     while ($l = mysql_fetch_assoc($r)) {
         // different types :
         // - partner identified by ID in URL
         // - newsletter identified by ID in URL
         // - direct entries
         $hits = $l['sp'];
         $idPageEntry = $l['p'];
         // useless
         // url parsing can fail when for example its value is "/translate... etc."
         // we count it as a direct entry
         $urlParse = @parse_url($l['u']);
         // if referer exists (and not blockedReferer or other)
         if (isset($urlParse['query'])) {
             //printDebug($l['p']." ");
             $urlQuery = $urlParse['query'];
             //				print($urlQuery); exit;
             /*
              * partner
              */
             if ($pid = getUrlParamValue($urlQuery, PARAM_URL_PARTNER)) {
                 //printDebug("<br><b>PID $pid detected !</b>");
                 //printDebug("<br>partner</br>");
                 $pname = $this->site->getPartnerNameFromId($pid);
                 $idp = $this->objects['partner_name']->getId($pname);
                 // init
                 if (!isset($this->toRecord['vis_partner'][$idp]['pmv_sum'])) {
                     $this->toRecord['vis_partner'][$idp]['pmv_sum'] = 0;
                     $int_sum['partner_name'][$idp] = array();
                 }
                 $this->toRecord['vis_partner'][$idp]['pmv_sum'] += $hits;
                 //printDebug($this->toRecord['vis_partner']);
                 $int_sum['partner_name'][$idp] = $this->getInterestValuesSum($int_sum['partner_name'][$idp], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 $int_sum['type'][REF_TYPE_PARTNER] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_PARTNER], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
             } else {
                 if ($nid = getUrlParamValue($urlQuery, PARAM_URL_NEWSLETTER)) {
                     //print("NID to searc = $nid");
                     if (defined("NEWSLETTER_ID_MAX_AUTO") && defined("NEWSLETTER_FORCE_CREATE") && (NEWSLETTER_ID_MAX_AUTO == -1 || $nid <= NEWSLETTER_ID_MAX_AUTO)) {
                         $nname = $this->site->getNewsletterName($nid, NEWSLETTER_FORCE_CREATE);
                     } else {
                         $nname = $this->site->getNewsletterName($nid);
                     }
                     // if newsletter exists
                     if (!$nname) {
                         print "Newsletter name not found! Maybe it's an empty name in phpmv_newsletter ? (for " . PARAM_URL_NEWSLETTER . " = {$nid})";
                         //exit;
                     } else {
                         //			print("<br><b>NID $nid detected !</b>");
                         //			print("<br>newsletter</br>");
                         $idn = $this->objects['newsletter']->getId($nname);
                         // init
                         if (!isset($this->toRecord['vis_newsletter'][$idn])) {
                             $this->toRecord['vis_newsletter'][$idn] = 0;
                             $int_sum['newsletter'][$idn] = array();
                         }
                         $this->toRecord['vis_newsletter'][$idn] += $hits;
                         //printDebug($this->toRecord['vis_newsletter']);
                         $int_sum['newsletter'][$idn] = $this->getInterestValuesSum($int_sum['newsletter'][$idn], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                         $int_sum['type'][REF_TYPE_NEWSLETTER] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_NEWSLETTER], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                     }
                 } else {
                     $resDirect += $hits;
                     $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
                 }
             }
         } else {
             $resDirect += $hits;
             $int_sum['type'][REF_TYPE_DIRECT_ENTRY] = $this->getInterestValuesSum($int_sum['type'][REF_TYPE_DIRECT_ENTRY], $l['s'], $l['sumpage'], $l['onepage'], $l['sumtime']);
         }
     }
     /*
      * developed interest average
      */
     // database-interest-field-name => interestsum-array-keyname
     $int_sum_to_avg = array('int_search_engine' => 'search_engine', 'int_keyword' => 'keyword', 'int_site' => 'site', 'int_partner' => 'partner_name', 'int_newsletter' => 'newsletter', 'int_referer_type' => 'type');
     foreach ($int_sum_to_avg as $int_name => $value) {
         uasort($int_sum[$value], "sortingInterest");
         $this->toRecord[$int_name] = $int_sum[$value];
     }
     /*
      * sum by referer type
      */
     $nbSearchEngine = 0;
     foreach ($this->toRecord['vis_search_engine'] as $key => $value) {
         $nbSearchEngine += $value['pmv_sum'];
     }
     $nbSite = 0;
     foreach ($this->toRecord['vis_site'] as $key => $value) {
         $nbSite += $value['pmv_sum'];
     }
     $nbUniqSite = sizeof($this->toRecord['vis_site']);
     $nbNewsletter = array_sum($this->toRecord['vis_newsletter']);
     $nbPartner = 0;
     foreach ($this->toRecord['vis_partner'] as $key => $value) {
         $nbPartner += $value['pmv_sum'];
     }
     $this->toRecord['nb_direct'] = $resDirect;
     $this->toRecord['nb_search_engine'] = $nbSearchEngine;
     $this->toRecord['nb_site'] = $nbSite;
     $this->toRecord['nb_newsletter'] = $nbNewsletter;
     $this->toRecord['nb_partner'] = $nbPartner;
     /*printDebug("<br><br><b>Access types </b><br> Direct : $nbDirect <br>Search engines : $nbSearchEngine " .
     				"<br> By site : $nbSite <br> Unique sites : $nbUniqSite <br>" .
     				"Partner : $nbPartner <br> Newsletter : $nbNewsletter <br>");
     		*/
     foreach ($vis_listing as $value) {
         if ($value != 'vis_newsletter') {
             if (is_array($this->toRecord[$value])) {
                 foreach ($this->toRecord[$value] as $key2 => $value2) {
                     if (is_array($this->toRecord[$value][$key2])) {
                         arsort($this->toRecord[$value][$key2]);
                     }
                     $this->toRecord[$value][$key2] = getArrayOffsetLimit($this->toRecord[$value][$key2], 0, MAX_DISTINCT_DETAILS_ELEMENTS);
                 }
             } else {
                 arsort($this->toRecord[$value]);
             }
         }
     }
 }
function print_table_header($action_filter_param, $url_params)
{
    global $DEFAULT_SORT_ORDER;
    # print_r($DEFAULT_SORT_ORDER);
    $name_url_params = $action_filter_param . "order_by=name";
    $cmpn_url_params = $action_filter_param . "order_by=cmpn";
    $cat_url_params = $action_filter_param . "order_by=cat";
    $num_prod_url_params = $action_filter_param . "order_by=num_prod";
    $reversed_sort_order = '';
    if (strstr($url_params, 'sort_order=') != false) {
        # get sort order
        $val = getUrlParamValue($url_params, 'sort_order');
        echo '<br/> url_params = ' . $url_params;
        $reversed_sort_order = rev_sort_order($val);
    }
    $order_by = getUrlParamValue($url_params, 'order_by');
    echo 'order_by ' . $order_by . '<br/>';
    switch ($order_by) {
        case 'name':
            $name_url_params .= '&sort_order=' . ($reversed_sort_order == '' ? rev_sort_order($DEFAULT_SORT_ORDER['name']) : $reversed_sort_order);
            break;
        case 'cmpn':
            $cmpn_url_params .= '&sort_order=' . ($reversed_sort_order == '' ? rev_sort_order($DEFAULT_SORT_ORDER['name']) : $reversed_sort_order);
            break;
        case 'cat':
            $cat_url_params .= '&sort_order=' . ($reversed_sort_order == '' ? rev_sort_order($DEFAULT_SORT_ORDER['name']) : $reversed_sort_order);
            break;
        case 'num_prod_url_params':
            $num_prod_url_params .= '&sort_order=' . ($reversed_sort_order == '' ? rev_sort_order($DEFAULT_SORT_ORDER['name']) : $reversed_sort_order);
            break;
    }
    echo '
                <table width="980" border="1" cellspacing="0" cellpadding="4">
                <form action="campaign_manager.php?' . $url_params . '" method="post">
		</tr>
                <tr>
                <td align="left"><a href="campaign_manager.php' . $name_url_params . '"><strong>Name</strong></a></td>
                <td align="left"><a href="campaign_manager.php' . $cmpn_url_params . '"><strong>Campaign</strong></td>
                <td align="left"><a href="campaign_manager.php' . $num_prod_url_params . '"><strong># Prod</strong></td>
                <td align="left"><a href="campaign_manager.php' . $cat_url_params . '"><strong>Cat</strong></td>
                <td align="left" width="25">Keywords</td>
                <td align="left">Action</td>
                <td align="left"><strong>Dates</strong></td>
                <td align="left"><strong>Ev ID</strong></td>
                <td width="33%">URL</td>
                </tr>';
}