Location: PHPKode > projects > Stuffed Tracker - Web Traffic Monitoring & Analysis > stuffed-tracker-web-traffic-monitoring-analysis/actions/admin.my_tracker.php
<?
if (!defined("NS_TRACKER_INDEX")) exit;
/////////////////////////////////////////////
///////// permission check here
if (!$nsUser->Logged()) $nsProduct->Redir("login", "", "admin");

/////////////////////////////////////////////
///////// require libraries here

require_once SELF."/class/pagenums2.class.php";

$nsLang->TplInc("my_tracker");
$nsLang->TplInc("inc/reports");
/////////////////////////////////////////////
///////// prepare any variables

$EditId=(ValidId($_GP['EditId']))?$_GP['EditId']:false;
$DeleteId=(ValidId($_GP['DeleteId']))?$_GP['DeleteId']:false;
$MyDeleteId=(ValidId($_GP['MyDeleteId']))?$_GP['MyDeleteId']:false;
$EditArr=(ValidArr($_GP['EditArr']))?$_GP['EditArr']:false;
$Mode = V($_REQUEST['Mode'],'list');
$AddToMy=(ValidArr($_GP['AddToMy']))?$_GP['AddToMy']:false;
$Filter = (ValidVar($_GP['Filter'])) ?$_GP['Filter']:false;
$SelectCpId=(ValidId($_GP['SelectCpId']))?$_GP['SelectCpId']:false;

$PageTitle=$Lang['Title'];
$AllClients=($nsUser->ADMIN)?1:false;
if (!$Mode) $Mode="list";
$DATE_DIFF=false;

$ProgPath[0]['Name']=$Lang['Title'];
$ProgPath[0]['Url']=$nsProduct->SelfAction();

$MenuSection="my_tracker";

    $SubMenu[0]['Name']=$Lang['ReportsLog'];
    $SubMenu[0]['Link']=getURL("my_tracker", "Mode=reports", "admin");
    $SubMenu[1]['Name']=$Lang['ScheduledEmails'];
    $SubMenu[1]['Link']=getURL("my_tracker", "Mode=email", "admin");
	$SubMenu[2]['Name']=$Lang['Visitors'];
	$SubMenu[2]['Link']=getURL("my_tracker", "Mode=visitors", "admin");
	$SubMenu[3]['Name']=$Lang['Actions'];
	$SubMenu[3]['Link']=getURL("my_tracker", "Mode=actions", "admin");
	$SubMenu[4]['Name']=$Lang['ActionItems'];
	$SubMenu[4]['Link']=getURL("my_tracker", "Mode=action_items", "admin");
    if ($nsProduct->LICENSE!=4) {
	$SubMenu[5]['Name']=$Lang['SaleItems'];
	$SubMenu[5]['Link']=getURL("my_tracker", "Mode=sale_items", "admin");
    }
    $SubMenu[6]['Name']=$Lang['Tags'];
    $SubMenu[6]['Link']=getURL("my_tracker", "Mode=tags", "admin");

/////////////////////////////////////////////
///////// call any process functions
if (!$nsUser->DEMO) {
	if ($Mode=="visitors"&&ValidId($DeleteId)) DeleteClientVisitor($DeleteId);
    if ($Mode=='tags' && $DeleteId && $SelectCpId) DeleteTag($SelectCpId,$DeleteId);

	if (ValidArr($AddToMy)&&$Mode=="reports") AddToMy($AddToMy, "REPORT_ID");
	if (ValidId($MyDeleteId)&&$Mode=="reports") DeleteFromMy($MyDeleteId, "REPORT_ID");
	if (ValidArr($AddToMy)&&$Mode=="visitors") AddToMy($AddToMy, "VISITOR_ID");
	if (ValidId($MyDeleteId)&&$Mode=="visitors") DeleteFromMy($MyDeleteId, "VISITOR_ID");
	if (ValidArr($AddToMy)&&$Mode=="actions") AddToMy($AddToMy, "ACTION_ID");
	if (ValidId($MyDeleteId)&&$Mode=="actions") DeleteFromMy($MyDeleteId, "ACTION_ID");
	if (ValidArr($AddToMy)&&$Mode=="action_items") AddToMy($AddToMy, "ACTION_ITEM_ID");
	if (ValidId($MyDeleteId)&&$Mode=="action_items") DeleteFromMy($MyDeleteId, "ACTION_ITEM_ID");
	if (ValidArr($AddToMy)&&$Mode=="sale_items") AddToMy($AddToMy, "SALE_ITEM_ID");
	if (ValidId($MyDeleteId)&&$Mode=="sale_items") DeleteFromMy($MyDeleteId, "SALE_ITEM_ID");

}

/////////////////////////////////////////////
///////// display section here


///////////////////////
// Ñïèñîê âñåãî, çà ÷åì ÿ ñëåæó

if ($Mode=="reports") include_once SELF."/actions/admin.user_cache.php";
if ($Mode=="email") include_once SELF."/actions/admin.email.php";


if ($Mode=="list") {



	//// ÌÎÈ ÎÒ×ÅÒÛ
	$Query = "
		SELECT SR.*, C.NAME AS COMP_NAME
		FROM ?_watch W
		INNER JOIN  ?_static_report SR
			ON SR.ID=W.REPORT_ID
		LEFT JOIN ?_client C
			ON C.ID=SR.COMPANY_ID
		WHERE W.USER_ID=? 
		ORDER BY C.NAME, SR.NAME
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	$UserReports=array();
	while ($Row=$Sql->Row()) {
        if (isset($Lang[$Row->NAME])) $Row->NAME=$Lang[$Row->NAME];
		$Row->NAME=stripslashes($Row->NAME);
		if ($Row->MODE=="NATURAL") $Row->Addr="natural_constructor";
		if ($Row->MODE=="PAID") $Row->Addr="paid_constructor";
		$Row->CP_ID=($Row->COMPANY_ID==0)?$CurrentCompany->ID:$Row->COMPANY_ID;
		$UserReports[]=$Row;
	}

	$Where="";
	$IdsStr="";
	if (!$nsUser->ADMIN) {
		$SiteIds=array();
		$Query = "SELECT ID FROM ?_site WHERE COMPANY_ID=?";
		$Sql= new Query($Query, false, $nsUser->COMPANY_ID);
		while ($Row=$Sql->Row()) $SiteIds[]=$Row->ID;
		$IdsStr=implode(",",$SiteIds);
		$Where="AND S_LOG.SITE_ID IN ($IdsStr)";
	}

	/////////// ÎÒÑËÅÆÈÂÀÅÌÛÅ ÏÎÑÅÒÈÒÅËÈ
	$WatchVis=array();
	$UsersWhere="";
	if (!$nsUser->ADMIN) $UsersWhere="AND CV.COMPANY_ID=".$CurrentCompany->ID;
	$Query = "
		SELECT 
		V.*, CV.NAME, CV.DESCRIPTION, CV.COMPANY_ID,
			I.IP AS LAST_IP
		FROM ?_watch WV
		INNER JOIN ?_visitor V
			ON V.ID=WV.VISITOR_ID
		INNER JOIN ?_client_visitor CV	
			ON CV.VISITOR_ID=V.ID $UsersWhere
		LEFT JOIN ?_ip I
				ON I.ID=V.LAST_IP_ID
		WHERE WV.USER_ID=?
		ORDER BY CV.NAME ASC, V.ID ASC
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	while ($Row=$Sql->Row()) {
		$Query = "SELECT UNIX_TIMESTAMP(DATE_ADD(STAMP, INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE)) FROM ?_{$Row->COMPANY_ID}_stat_log S_LOG WHERE S_LOG.VISITOR_ID=$Row->ID AND S_LOG.PAGE_ID>0 $Where ORDER BY STAMP DESC LIMIT 1";
		$Row->LAST_STAMP=$Db->ReturnValue($Query);
		if ($Row->LAST_STAMP)  $Row->DATE_DIFF_NAME=LastStamp($Row->LAST_STAMP);
		$Row->DATE_DIFF=$DATE_DIFF;
		$WatchVis[]=$Row;
	}

	/////////// ÎÒÑËÅÆÈÂÀÅÌÛÅ ÄÅÉÑÒÂÈß
	$WatchActions=array();
	$ActionsWhere="";
	if (!$nsUser->ADMIN) $ActionsWhere=" AND C.ID=".$CurrentCompany->ID;
	$Query = "
		SELECT 
			VA.ID, VA.NAME, C.ID AS COMPANY_ID, C.NAME AS COMP_NAME, S.HOST, S.ID AS SITE_ID
			FROM ?_watch W
			INNER JOIN ?_visitor_action VA
				ON VA.ID=W.ACTION_ID
			INNER JOIN ?_site S
				ON S.ID=VA.SITE_ID
			INNER JOIN ?_client C
				ON C.ID=S.COMPANY_ID
		WHERE W.USER_ID=? $ActionsWhere
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	while ($Row=$Sql->Row()) {
		$Query = "
			SELECT
			UNIX_TIMESTAMP(DATE_ADD(S_LOG.STAMP, INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE))
			FROM ?_{$Row->COMPANY_ID}_stat_log S_LOG
			INNER JOIN ?_{$Row->COMPANY_ID}_stat_action S_ACTION
				 ON S_ACTION.LOG_ID=S_LOG.ID
			WHERE S_ACTION.ACTION_ID=".$Row->ID." $Where
			ORDER BY S_LOG.STAMP DESC
			LIMIT 1
		";
		$Row->LAST_STAMP=$Db->ReturnValue($Query);
		if ($Row->LAST_STAMP)  $Row->DATE_DIFF_NAME=LastStamp($Row->LAST_STAMP);
		$Row->DATE_DIFF=$DATE_DIFF;
		$WatchActions[]=$Row;
	}

	/////////// ÎÒÑËÅÆÈÂÀÅÌÛÅ ÏÐÅÄÌÅÒÛ ÄÅÉÑÒÂÈÉ

	$WatchActionItems=array();
	$ActionItemsWhere="";
	if (!$nsUser->ADMIN) $ActionItemsWhere=" AND AI.COMPANY_ID=".$CurrentCompany->ID;

	$Query = "
		SELECT
			AI.*
			FROM ?_watch W
			INNER JOIN ?_action_item AI
				ON AI.ID=W.ACTION_ITEM_ID
		WHERE W.USER_ID=? $ActionItemsWhere
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	while ($Row=$Sql->Row()) {
		$Query = "
			SELECT
			UNIX_TIMESTAMP(DATE_ADD(S_LOG.STAMP, INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE))
			FROM ?_{$Row->COMPANY_ID}_stat_log S_LOG
			INNER JOIN ?_{$Row->COMPANY_ID}_stat_action S_ACTION
				 ON S_ACTION.LOG_ID=S_LOG.ID
			INNER JOIN ?_action_set SAS
				ON SAS.STAT_ACTION_ID=S_ACTION.ID
			INNER JOIN ?_action_item AI
				ON AI.ID=SAS.ACTION_ITEM_ID
			WHERE AI.ID=$Row->ID $ActionItemsWhere
			ORDER BY S_LOG.STAMP DESC
			LIMIT 1
		";
		$Row->LAST_STAMP=$Db->ReturnValue($Query);
		if ($Row->LAST_STAMP)  $Row->DATE_DIFF_NAME=LastStamp($Row->LAST_STAMP);
		$Row->DATE_DIFF=$DATE_DIFF;
		$WatchActionItems[]=$Row;
	}


	/////////// ÎÒÑËÅÆÈÂÀÅÌÛÅ ÒÎÂÀÐÛ
    if ($nsProduct->LICENSE!=4) {
	$WatchSaleItems=array();
	$SaleItemsWhere="";
	if (!$nsUser->ADMIN) $SaleItemsWhere=" AND SI.COMPANY_ID=".$CurrentCompany->ID;

	$Query = "
		SELECT
			SI.*
			FROM ?_watch W
			INNER JOIN ?_sale_item SI
				ON SI.ID=W.SALE_ITEM_ID
		WHERE W.USER_ID=? $SaleItemsWhere
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	while ($Row=$Sql->Row()) {
		$Query = "
			SELECT
			UNIX_TIMESTAMP(DATE_ADD(S_LOG.STAMP, INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE))
			FROM ?_{$Row->COMPANY_ID}_stat_log S_LOG
			INNER JOIN ?_{$Row->COMPANY_ID}_stat_sale S_SALE
				 ON S_SALE.LOG_ID=S_LOG.ID
			INNER JOIN ?_sale_set SS
				ON SS.SALE_ID=S_SALE.ID
			INNER JOIN ?_sale_item SI
				ON SI.ID=SS.ITEM_ID
			WHERE SI.ID=$Row->ID $SaleItemsWhere
			ORDER BY S_LOG.STAMP DESC
			LIMIT 1
		";
		$Row->LAST_STAMP=$Db->ReturnValue($Query);
		if ($Row->LAST_STAMP)  $Row->DATE_DIFF_NAME=LastStamp($Row->LAST_STAMP);
		$Row->DATE_DIFF=$DATE_DIFF;
		$WatchSaleItems[]=$Row;
	}
    }

	include $nsTemplate->Inc("admin.my_tracker");
}





$NoAdd=true;



/////////////////////////////////////
///// ÑÏÈÑÎÊ ÏÎÑÅÒÈÒÅËÅÉ

if ($Mode=="visitors") {

	$ProgPath[1]['Name']=$Lang['Visitors'];
	$ProgPath[1]['Url']=$nsProduct->SelfAction("Mode=$Mode");

    $VisitorsList=array();
    if (!$SelectCpId) $SelectCpId=V($CurrentCompany->ID);
    if ($nsUser->ADMIN) {
        $Query = "SELECT * FROM ?_client WHERE HIDDEN != '1' ORDER BY NAME ASC";
        $Sql = new Query($Query);
        $CompArr=array();
        while ($Row = $Sql->Row()) $CompArr[]=$Row;
        if (count($CompArr)==1) $SelectCpId=$CompArr[0]->ID;
    }
    if ($SelectCpId) {

    $Args=array();
    $Args['cp']=$SelectCpId;
    $Args['uid']=$nsUser->UserId();

	$VisitorsWhere=""; $WhereArr=array();
	$WhereArr[]="CV.COMPANY_ID=?cp";

    if (V($Filter)) {
        $WhereArr[]="CV.NAME LIKE ?flt";
        $Args['flt']="%$Filter%";
        $Filter=stripslashes($Filter);
    }    

    $VisitorsWhere=(!empty($WhereArr))?"WHERE ".implode(" AND ",$WhereArr):"";
    
    $Query = "SELECT COUNT(*) FROM ?_client_visitor CV $VisitorsWhere";
    $Cnt=$Db->ReturnValue($Query,$Args);
    $Pages = new PageNums($Cnt,50);
    $Pages->Calculate();
    $Pages->Args="&Mode=$Mode&SelectCpId=$SelectCpId";
    
	$Query = "
		SELECT 
		V.*, CV.NAME, CV.DESCRIPTION, CV.COMPANY_ID, C.NAME AS COMP_NAME, W.ID AS WATCH_ID
		FROM ?_visitor V
		INNER JOIN ?_client_visitor CV	
			ON CV.VISITOR_ID=V.ID 
		INNER JOIN ?_client C
			ON C.ID=CV.COMPANY_ID
		LEFT JOIN ?_watch W
			ON W.VISITOR_ID=V.ID AND W.USER_ID=?uid
		$VisitorsWhere
		ORDER BY C.NAME ASC, V.ID ASC
        LIMIT {$Pages->PageStart}, {$Pages->Limit}
	";
	$Sql = new Query($Query, false, $Args);
	$VisitorsList=array();
	$PrevCp=0;
	while ($Row=$Sql->Row()) {
		if (!$Row->WATCH_ID) $NoAdd=false;
		$Row->NewComp=false;
		$Row->NAME=stripslashes($Row->NAME);
		$Row->DESCRIPTION=stripslashes($Row->DESCRIPTION);
		$Row->CP_ID=$Row->COMPANY_ID;
		if ($PrevCp!=$Row->COMPANY_ID) $Row->NewComp=true;
		$VisitorsList[]=$Row;
		$PrevCp=$Row->COMPANY_ID;
	}
    
    }
	include $nsTemplate->Inc("my_tracker/visitors.inc");
}


/////////////////////////////////////
///// ÑÏÈÑÎÊ ÄÅÉÑÒÂÈÉ

if ($Mode=="actions") {

	$ProgPath[1]['Name']=$Lang['Actions'];
	$ProgPath[1]['Url']=$nsProduct->SelfAction("Mode=$Mode");

	$ActionsWhere="";
	if (!$nsUser->ADMIN) $ActionsWhere="WHERE S.COMPANY_ID=".$CurrentCompany->ID;
	$Query = "
		SELECT 
			C.ID AS COMPANY_ID, C.NAME AS COMP_NAME,
			S.HOST, S.ID AS SITE_ID,
			VA.ID, VA.NAME, VA.CODE_ACTION,
			W.ID AS WATCH_ID
			FROM ?_visitor_action VA
			LEFT JOIN ?_site S
				ON S.ID=VA.SITE_ID
			INNER JOIN ?_client C
				ON C.ID=S.COMPANY_ID
			LEFT JOIN ?_watch W
				ON W.ACTION_ID=VA.ID AND W.USER_ID=?
		$ActionsWhere
		ORDER BY S.COMPANY_ID, S.HOST, VA.NAME
	";
	$Sql = new Query($Query, false, $nsUser->UserId());
	$ActionsList=array();
	$PrevCp=0;
	while ($Row=$Sql->Row()) {
		if (!$Row->WATCH_ID) $NoAdd=false;
		$Row->NewComp=false;
		$Row->NAME=stripslashes($Row->NAME);
		$Row->CP_ID=$Row->COMPANY_ID;
		if ($PrevCp!=$Row->COMPANY_ID) $Row->NewComp=true;
		$ActionsList[]=$Row;
		$PrevCp=$Row->COMPANY_ID;
	}
	include $nsTemplate->Inc("my_tracker/actions.inc");

}

/////////////////////////////////////
///// ÑÏÈÑÎÊ ÏÐÅÄÌÅÒÎÂ ÄÅÉÑÒÂÈÉ

if ($Mode=="action_items") {

	$ProgPath[1]['Name']=$Lang['ActionItems'];
	$ProgPath[1]['Url']=$nsProduct->SelfAction("Mode=$Mode");

	$ActionItemsList=array();
	if (!$SelectCpId) $SelectCpId=V($CurrentCompany->ID);
	if ($nsUser->ADMIN) {
		$Query = "SELECT * FROM ?_client WHERE HIDDEN != '1' ORDER BY NAME ASC";
		$Sql = new Query($Query);
		$CompArr=array();
		while ($Row = $Sql->Row()) $CompArr[]=$Row;
		if (count($CompArr)==1) $SelectCpId=$CompArr[0]->ID;
	}

	if ($SelectCpId) {

		$ItemsWhere=""; $WhereArr=array(); $Args=array();
        $WhereArr[]="AI.COMPANY_ID=?cp";
        $Args['cp']=$SelectCpId;
        $Args['uid']=$nsUser->UserId();
		if (V($Filter)) {
			$Args['flt']="%$Filter%";
			$WhereArr[]="AND AI.NAME LIKE ?flt";
			$Filter=stripslashes($Filter);
		}

        $ItemsWhere=(!empty($WhereArr))?"WHERE ".implode(" AND ",$WhereArr):"";

        $Query = "SELECT COUNT(*) FROM ?_action_item AI $ItemsWhere";
        $Cnt=$Db->ReturnValue($Query,$Args);
        $Pages = new PageNums($Cnt,50);
        $Pages->Calculate();
        $Pages->Args="&Mode=$Mode&SelectCpId=$SelectCpId";

		$Query = "
			SELECT 
			AI.*, C.NAME AS COMP_NAME, W.ID AS WATCH_ID
			FROM ?_action_item AI
			INNER JOIN ?_client C
				ON C.ID=AI.COMPANY_ID
			LEFT JOIN ?_watch W
				ON W.ACTION_ITEM_ID=AI.ID AND W.USER_ID=?uid
			$ItemsWhere 
			ORDER BY AI.COMPANY_ID, AI.NAME
            LIMIT {$Pages->PageStart}, {$Pages->Limit}
		";
		$Sql = new Query($Query, false,$Args);
		$ActionsList=array();
		$PrevCp=0;
		while ($Row=$Sql->Row()) {
			if (!$Row->WATCH_ID) $NoAdd=false;
			$Row->NewComp=false;
			$Row->NAME=htmlspecialchars(stripslashes($Row->NAME));
			$Row->CP_ID=$Row->COMPANY_ID;
			if ($PrevCp!=$Row->COMPANY_ID) $Row->NewComp=true;
			$ActionItemsList[]=$Row;
			$PrevCp=$Row->COMPANY_ID;
		}

	}
	include $nsTemplate->Inc("my_tracker/action_items.inc");
}


/////////////////////////////////////
///// ÑÏÈÑÎÊ ÒÎÂÀÐÎÂ

if ($Mode=="sale_items") {
	$SaleItemsList=array();
	$ProgPath[1]['Name']=$Lang['SaleItems'];
	$ProgPath[1]['Url']=$nsProduct->SelfAction("Mode=$Mode");
	if (!$SelectCpId) $SelectCpId=V($CurrentCompany->ID);
	if ($nsUser->ADMIN) {
		$Query = "SELECT * FROM ?_client WHERE HIDDEN != '1' ORDER BY NAME ASC";
		$Sql = new Query($Query);
		$CompArr=array();
		while ($Row = $Sql->Row()) $CompArr[]=$Row;
		if (count($CompArr)==1) $SelectCpId=$CompArr[0]->ID;
	}
	if ($SelectCpId) {

		$ItemsWhere="";
		if (ValidVar($Filter)) {
			$Filter=addslashes($Filter);
			$ItemsWhere="AND SI.NAME LIKE '%$Filter%'";
			$Filter=stripslashes($Filter);
		}

		$Query = "
			SELECT 
			SI.*, C.NAME AS COMP_NAME, W.ID AS WATCH_ID
			FROM ?_sale_item SI
			INNER JOIN ?_client C
				ON C.ID=SI.COMPANY_ID
			LEFT JOIN ?_watch W
				ON W.SALE_ITEM_ID=SI.ID AND W.USER_ID=?
			WHERE SI.COMPANY_ID=?
				$ItemsWhere
			ORDER BY SI.COMPANY_ID, SI.NAME
		";
		$Sql = new Query($Query, false, $nsUser->UserId(),$SelectCpId);
		$PrevCp=0;
		while ($Row=$Sql->Row()) {
			if (!$Row->WATCH_ID) $NoAdd=false;
			$Row->NewComp=false;
			$Row->NAME=htmlspecialchars(stripslashes($Row->NAME));
			$Row->CP_ID=$Row->COMPANY_ID;
			if ($PrevCp!=$Row->COMPANY_ID) $Row->NewComp=true;
			$SaleItemsList[]=$Row;
			$PrevCp=$Row->COMPANY_ID;
		}

	}
	else {
		//$Logs->Msg($Lang['SIChooseClient']);
	}
	include $nsTemplate->Inc("my_tracker/sale_items.inc");

}

/////////////////////////////////////
///// ÒÀÃÈ

if ($Mode=="tags") {
    $TagsArr=array();
    $ProgPath[1]['Name']=$Lang['Tags'];
    $ProgPath[1]['Url']=$nsProduct->SelfAction("Mode=$Mode");
    if (!$SelectCpId) $SelectCpId=V($CurrentCompany->ID);
    if ($nsUser->ADMIN) {
        $Query = "SELECT * FROM ?_client WHERE HIDDEN != '1' ORDER BY NAME ASC";
        $Sql = new Query($Query);
        $CompArr=array();
        while ($Row = $Sql->Row()) $CompArr[]=$Row;
        if (count($CompArr)==1) $SelectCpId=$CompArr[0]->ID;
    }
    if ($nsUser->COMPANY_ID) $SelectCpId = $nsUser->COMPANY_ID;
    if ($SelectCpId) {

        $TagsWhere = "C.ID=?cp";
        if (V($Filter)) $TagsWhere=" AND T.TAG LIKE ?flt";

        $Query = "
            SELECT 
            DISTINCT T.ID, T.TAG AS NAME, C.NAME AS COMP_NAME, VT.COMPANY_ID
            FROM ?_tag T
            INNER JOIN ?_visitor_tag_rel VT ON VT.TAG_ID=T.ID
            INNER JOIN ?_client C ON C.ID=VT.COMPANY_ID
            WHERE $TagsWhere
            ORDER BY VT.COMPANY_ID, T.TAG
        ";
        $Sql = new Query($Query, false, array('cp'=>$SelectCpId,'flt'=>"%$Filter%"));
        $PrevCp=0;
        while ($Row=$Sql->Row()) {
            $Row->NewComp=false;
            $Row->NAME=htmlspecialchars(stripslashes($Row->NAME));
            $Row->CP_ID=$Row->COMPANY_ID;
            if ($PrevCp!=$Row->COMPANY_ID) $Row->NewComp=true;
            $TagsArr[]=$Row;
            $PrevCp=$Row->COMPANY_ID;
        }

    }

    include $nsTemplate->Inc("my_tracker/tags.inc");

}

/////////////////////////////////////////////
///////// process functions here

function DeleteClientVisitor($Id) 
{
	global $Db, $nsUser, $nsProduct;
	$Check=true;
	if (!$nsUser->ADMIN) {
		$Query = "SELECT ID FROM ?_client_visitor WHERE VISITOR_ID=? AND COMPANY_ID=?";
		$Check=$Db->ReturnValue($Query, $Id, $nsUser->COMPANY_ID);
	}
	if (!$Check) return false;
	$Query = "DELETE FROM ?_client_visitor WHERE VISITOR_ID=?";
	$Db->Query($Query, $Id);
	$nsProduct->Redir("my_tracker", "Mode=visitors&RDlt=1");
}

function DeleteClientVisitorGrp($Id) 
{
	global $Db, $nsUser, $nsProduct;
	$Check=true;
	if (!$nsUser->ADMIN) {
		$Query = "SELECT ID FROM ?_client_visitor_grp WHERE ID=? AND COMPANY_ID=?";
		$Check=$Db->ReturnValue($Query, $Id, $nsUser->COMPANY_ID);
	}
	if (!$Check) return false;
	$Query = "DELETE FROM ?_client_visitor_grp WHERE ID=?";
	$Db->Query($Query, $Id);
	$nsProduct->Redir("my_tracker", "Mode=visitor_grps&RDlt=1");
}

function AddToMy($Arr, $Field)
{
	global $Db, $nsUser, $nsProduct;
	$UserId=$nsUser->UserId();
	foreach ($Arr as $SomeId=>$Value) {
		if ($Value!=1) continue;
		$Query = "INSERT INTO ?_watch (USER_ID, $Field) VALUES (?, ?)";
		$Db->Query($Query, $UserId, $SomeId);
	}
	$nsProduct->Redir("my_tracker", "RUpd=1", "admin");
}

function DeleteFromMy($Id, $Field)
{
	global $Db, $nsUser, $nsProduct;
	$Query = "DELETE FROM ?_watch WHERE USER_ID=? AND $Field=?";
	$Db->Query($Query, $nsUser->UserId(), $Id);
	$nsProduct->Redir("my_tracker", "RUpd=1", "admin");
}

function DeleteTag($CpId,$Id)
{
    global $Db,$nsProduct;
    $Query = "DELETE FROM ?_visitor_tag_rel WHERE COMPANY_ID=? AND TAG_ID=?";
    $Db->Query($Query,$CpId,$Id);
    $Query = "SELECT ID FROM ?_visitor_tag_rel WHERE TAG_ID=?";
    $CheckId=$Db->ReturnValue($Query,$Id);
    if (!$CheckId) {
        $Query = "DELETE FROM ?_tag WHERE ID=?";
        $Db->Query($Query,$Id);
    }
    $nsProduct->Redir('my_tracker',"Mode=tags&SelectCpId=$CpId");
}

/////////////////////////////////////////////
///////// library section

function LastStamp($Stamp) 
{
	global $Db, $Lang, $DATE_DIFF, $nsUser;
    $UserDate=UserDate();
	$Query= "SELECT TO_DAYS(DATE_ADD('$UserDate', INTERVAL '$nsUser->SQL_TZ' HOUR_MINUTE))-TO_DAYS(?)";
	$DATE_DIFF=$Db->ReturnValue($Query, date("Y-m-d", $Stamp));
	if ($DATE_DIFF==0) $DATE_DIFF_NAME=$Lang['Today'];
	if ($DATE_DIFF==1) $DATE_DIFF_NAME=$Lang['Yesterday'];
	if ($DATE_DIFF>1) {
		$DATE_DIFF_NAME=$DATE_DIFF." ";
		$DATE_DIFF_NAME.=ParseNumberNow($DATE_DIFF, $Lang['DayN'], $Lang['Day1'], $Lang['Day2']);
		$DATE_DIFF_NAME.=' '.$Lang['DaysBefore'];
	}
	return $DATE_DIFF_NAME;
}

?>
Return current item: Stuffed Tracker - Web Traffic Monitoring & Analysis