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

require_once SYS."/system/lib/sort.func.php";
require_once SELF."/lib/get_page.func.php";

$nsLang->TplInc("inc/menu");
$MenuSection="funnel";

if (!@$CpId) $nsProduct->Redir('default',false,'admin');

$ProgPath[0]['Name']=$Lang['MFunnels'];
$ProgPath[0]['Url']=getURL("funnels", "", "admin");

$PageTitle=$Lang['Title'];

$Query = "SELECT COUNT(*) FROM ?_site WHERE COMPANY_ID=?";
$SiteCnt = $Db->ReturnValue($Query, $CpId);
if (!$SiteCnt) $nsProduct->Redir('default',false,'admin');

$EditId=V($_REQUEST['EditId']);
$DeleteId=V($_REQUEST['DeleteId']);
$EditArr=V($_REQUEST['EditArr']);
$StepArr=V($_REQUEST['StepArr'],array());
$SiteSelect=false;

$MyVersion=$Db->Version;
$MyVersion=ToFloat($MyVersion)*10;
$DisableReport=false;
if ($MyVersion < 41) {$Logs->Alert($Lang['MysqlVer']); $DisableReport=true; }
if (!$Settings['All']->CRON_ENABLE) { $Logs->Alert($Lang['Cron']); $DisableReport=true; }


if (!$nsUser->DEMO) {
    if ($EditId && $EditArr) SavePath($EditId,$EditArr,$StepArr);
    if ($DeleteId) DeletePath($DeleteId);
}



if (!$EditId) {
    
    $SubMenu[0]['Name']=$Lang['CreateNew'];
    $SubMenu[0]['Link']=getURL("funnels","EditId=new","admin");
    
    $Paths=array();
    $Query = "SELECT * FROM ?_path WHERE COMPANY_ID=? ORDER BY NAME ASC";
    $Sql = new Query($Query, false, $CpId);
    while ($Row=$Sql->Row()) $Paths[]=$Row;
    
    include $nsTemplate->Inc("inc/header");
    include $nsTemplate->Inc("inc/submenu");
    include $nsTemplate->Inc();
    include $nsTemplate->Inc("inc/footer");
}

if ($EditId) {
    
    $SiteArr=array();
    if ($SiteSelect) {
        $Query = "SELECT ID, HOST FROM ?_site WHERE COMPANY_ID=? ORDER BY HOST";
        $Sql = new Query($Query,false,$CompId);
        while($Row=$Sql->Row()) $SiteArr[$Row->ID]=$Row->HOST;
    }
    
    $SubMenu[0]['Name']=$Lang['BackToList'];
    $SubMenu[0]['Link']=getURL("funnels","","admin");
    if (!$DisableReport) {
        $SubMenu[1]['Name']=$Lang['ViewStats'];
        $SubMenu[1]['Link']=getURL("funnel","FId=$EditId","report");
    }    

    if ($EditId!='new') {
        $Query = "SELECT * FROM ?_path WHERE ID=? AND COMPANY_ID=?";
        $EditPath=$Db->Select($Query,false,$EditId,$CpId);

        if (!$EditPath) $nsProduct->Redir('client_page',false,'admin');

        $ProgPath[1]['Name']=$EditPath->NAME;
        $ProgPath[1]['Url']=getURL('funnels',"EditId=$EditId");

        if (empty($StepArr)) $StepArr=array();
        $Query = "SELECT * FROM ?_path_step WHERE PATH_ID=? ORDER BY POSITION";
        $Sql = new Query($Query,false,$EditId);
        while ($Row=$Sql->Row()) $StepArr[$Row->POSITION]['Obj']=$Row;
    }

    $EditArr['Name']=V($EditArr['Name'],V($EditPath->NAME,''));
    $EditArr['Descr']=V($EditArr['Descr'],V($EditPath->DESCRIPTION,''));
    if (empty($StepArr)) $StepArr[1]=array('type'=>'');
    foreach ($StepArr as $i=>$Row) {
        if (!V($Row['type'])) {
            if (V($Row['Obj']->CAMP_ID)||V($Row['Obj']->ANY_CAMPAIGN)) $Row['type']='click';
            if (V($Row['Obj']->SPLIT_ID)||V($Row['Obj']->ANY_SPLIT_TEST)) $Row['type']='split';
            if (V($Row['Obj']->ACTION_ID)||V($Row['Obj']->ACTION_ITEM_ID)||V($Row['Obj']->ANY_ACTION)) $Row['type']='action';
            if (V($Row['Obj']->PRODUCT_ID)||V($Row['Obj']->ANY_SALE)) $Row['type']='sale';
            if (V($Row['Obj']->REF_GRP_ID)||V($Row['Obj']->REF_HOST_ID)||V($Row['Obj']->ANY_REFERER)) $Row['type']='ref';
            if (V($Row['Obj']->PQ_ID)) $Row['type']='page';
            if (V($Row['Obj']->ANY_ENTRY)) $Row['type']='entry';
        }
        
        $Row['CampId']=V($Row['CampId'],V($Row['Obj']->CAMP_ID,0));
        $Row['SplitId']=V($Row['SplitId'],V($Row['Obj']->SPLIT_ID,0));
        $Row['ActionId']=V($Row['ActionId'],V($Row['Obj']->ACTION_ID,0));
        $Row['ActionItemId']=V($Row['ActionItemId'],V($Row['Obj']->ACTION_ITEM_ID,0));
        $Row['ProductId']=V($Row['ProductId'],V($Row['Obj']->PRODUCT_ID,0));
        $Row['PageId']=V($Row['PageId'],V($Row['Obj']->PQ_ID,0));
        $Row['Page']=V($Row['Page'],'');
        $Row['SiteId']=V($Row['SiteId'],'');
        $Row['KeyId']=V($Row['KeyId'],V($Row['Obj']->KEYWORD_ID,0));
        $Row['RefGrpId']=V($Row['RefGrpId'],V($Row['Obj']->REF_GRP_ID,0));
        $Row['RefHostId']=V($Row['RefHostId'],V($Row['Obj']->REF_HOST_ID,0));
        $Row['Entry']=V($Row['Entry'],V($Row['Obj']->ANY_ENTRY,0));
        $Row['AnyQuery']=V($Row['AnyQuery'],V($Row['Obj']->ANY_QUERY_STRING,0));

        $Row['FirstSession']=V($Row['FirstSession'],V($Row['Obj']->FIRST_SESSION,0));
        $Row['FirstStep']=V($Row['FirstStep'],V($Row['Obj']->FIRST_STEP,0));
        $Row['NoOuts']=V($Row['NoOuts'],V($Row['Obj']->NO_OUTS,0));

        $StepArr[$i]=$Row;
    }

    include $nsTemplate->Inc("inc/header");
    include $nsTemplate->Inc("inc/submenu");
    include $nsTemplate->Inc("admin.funnel_edit");
    include $nsTemplate->Inc("inc/footer");       
}




function SavePath($Id,$EditArr=array(),$StepArr=array())
{
    global $Db, $nsProduct,$Logs,$Lang,$SiteSelect,$CpId, $EditId;
    
    extract($EditArr);
    if (!V($Name)) return $Logs->Err($Lang['MustFillName']);
    $Descr=V($Descr,'');
    $Msg='';
    
    if ($Id=='new') {
        $Query = "INSERT INTO ?_path (NAME, DESCRIPTION, COMPANY_ID) VALUES (?,?,?)";
        $Db->Query($Query,$Name,$Descr,$CpId);
        $Id=$Db->LastInsertId;
        $Msg=$Lang['RecordCreated'];
    }
    else {
        $Query = "UPDATE ?_path SET NAME=?, DESCRIPTION=? WHERE ID=?";
        $Db->Query($Query,$Name,$Descr,$Id);
        $Msg=$Lang['RecordUpdated'];
    }
    
    $EditId = $Id;
    
    $Positions=array();
    foreach ($StepArr as $Position=>$Arr) {
        $P=SaveStep($Id,$Position,$Arr);
        if (in_array($P,array("host","nohost"))) {$SiteSelect=$Position; return $Logs->Alert('Choose site');}
        $Positions[]=$P;
    }
    if (!empty($Positions)) {
        $Query = "DELETE FROM ?_path_step WHERE PATH_ID=? AND POSITION NOT IN (?)";
        $Db->Query($Query,$Id,$Positions);
    }
    else $Db->Query("DELETE FROM ?_path_step WHERE PATH_ID=?",$Id);
    ResortTable("?_path_step","POSITION","PATH_ID=$Id");
    
    if ($Msg) $Logs->Msg($Msg);
    $nsProduct->Redir("funnels","EditId=$Id");
}


function SaveStep($PathId, $Position, $Arr)
{
    global $Db,$Logs,$Lang;

    if (!V($Arr['type'])) return -1;

    $Query = "SELECT ID FROM ?_path_step WHERE PATH_ID=? AND POSITION=?";
    $StepId=$Db->ReturnValue($Query,$PathId,$Position);
    if ($StepId) {
        $Query = "UPDATE ?_path_step SET CAMP_ID=0, SPLIT_ID=0, ACTION_ID=0,
                                        ACTION_ITEM_ID=0, PRODUCT_ID=0, PQ_ID=0, ANY_ACTION=0,
                                        ANY_SALE=0,ANY_CAMPAIGN=0,ANY_SPLIT_TEST=0,FIRST_STEP=0,
                                        FIRST_SESSION=0,NO_OUTS=0,ANY_ENTRY=0,REF_GRP_ID=0,REF_HOST_ID=0,
                                        ANY_REFERER=0, ANY_QUERY_STRING=0
                        WHERE ID=?";
        $Db->Query($Query,$StepId);
    }
    else {
        $Query = "INSERT INTO ?_path_step (PATH_ID,POSITION) VALUES (?,?)";
        $Db->Query($Query,$PathId,$Position);
        $StepId=$Db->LastInsertId;
    }

    $UpdateArr=array();
    if ($Arr['type']=='click') {
        if ($Position>1) {$Logs->Err($Lang['EntryPosition']); return -1;}
        if (V($Arr['CampId'])>0) $UpdateArr['CAMP_ID']=$Arr['CampId'];
        else $UpdateArr['ANY_CAMPAIGN']=1;
        $UpdateArr['KEYWORD_ID']=(V($Arr['Key']))?GetKeywordId($Arr['Key']):0;
    }
    if ($Arr['type']=='action') {
        if (V($Arr['ActionId'])>0) $UpdateArr['ACTION_ID']=$Arr['ActionId'];
        else $UpdateArr['ANY_ACTION']=1;
        if (V($Arr['ActionItemId'])>0) $UpdateArr['ACTION_ITEM_ID']=$Arr['ActionItemId'];
    }
    if ($Arr['type']=='split') {
        if ($Position>1) {$Logs->Err($Lang['EntryPosition']); return -1;}
        if (V($Arr['SplitId'])>0) $UpdateArr['SPLIT_ID']=$Arr['SplitId'];
        else $UpdateArr['ANY_SPLIT_TEST']=1;
        if (V($Arr['Key'])) $UpdateArr['KEYWORD_ID']=GetKeywordId($Arr['Key']);
    }
    if ($Arr['type']=='ref') {
        if ($Position>1) {$Logs->Err($Lang['EntryPosition']); return -1;}
        $UpdateArr['REF_GRP_ID']=(V($Arr['RefGrpId'])>0)?$Arr['RefGrpId']:0;
        $UpdateArr['REF_HOST_ID']=(V($Arr['Host']))?GetHostId($Arr['Host']):0;
        if (!$UpdateArr['REF_GRP_ID']&&!$UpdateArr['REF_HOST_ID']) $UpdateArr['ANY_REFERER']=1;
        $UpdateArr['KEYWORD_ID']=(V($Arr['Key']))?GetKeywordId($Arr['Key']):0;
    }
    if ($Arr['type']=='sale') {
        if (V($Arr['ProductId'])>0) $UpdateArr['PRODUCT_ID']=$Arr['ProductId'];
        else $UpdateArr['ANY_SALE']=1;
    }
    if ($Arr['type']=='page') {
        $UpdateArr['ANY_QUERY_STRING']=V($Arr['AnyQuery'])?1:0;
        if (V($Arr['PageId'])>0) {
            $UpdateArr['PQ_ID']=$Arr['PageId'];
        }
        elseif (V($Arr['Page'])) {
            if (substr($Arr['Page'], 0, 4) != 'http') $Arr['Page'] = 'http://'.$Arr['Page'];
            $PageId=GetPqId($Arr['Page'],V($Arr['SiteId']), (V($Arr['SiteId']) ? false : true));
            //if ($PageId!=strval(intval($PageId))&&trim($Arr['Page'])) return $PageId;
            if (trim($Arr['Page'])) $UpdateArr['PQ_ID']=$PageId;
        }
    }
    if ($Arr['type']=='entry') {
        if ($Position>1) {$Logs->Err($Lang['EntryPosition']); return -1;}
        $UpdateArr['ANY_ENTRY']=(V($Arr['Entry'])==2)?2:1;
    }

    if (V($Arr['FirstStep'])>0) $UpdateArr['FIRST_STEP']=1;
    if (V($Arr['FirstSession'])>0) $UpdateArr['FIRST_SESSION']=1;
    if (V($Arr['NoOuts'])>0) $UpdateArr['NO_OUTS']=1;
    
    if ($Position>1) $UpdateArr['FIRST_STEP']=$UpdateArr['FIRST_SESSION']=0;
    if ($Position==1) $UpdateArr['NO_OUTS']=0;

    if (!empty($UpdateArr)) {
        $Query = "UPDATE ?_path_step SET ?% WHERE ID=?";
        $Db->Query($Query,$UpdateArr,$StepId);
    }
    else return -1;
    return $Position;
}

function GetKeywordId($Key=false)
{
    if (!$Key) return 0;
    global $Db;
    
    $LowerKey=ToLower($Key);
    $LowerKey=md5($LowerKey);

    $Query = "SELECT ID FROM ?_keyword WHERE MD5_SEARCH=?";
    $CheckId=$Db->ReturnValue($Query,$LowerKey);
    if (ValidId($CheckId)) return $CheckId;
    $Query ="INSERT INTO ?_keyword (KEYWORD, MD5_SEARCH) VALUES (?, ?)";
    $Db->Query($Query, $Key, $LowerKey);
    return $Db->LastInsertId;
}

function GetHostId($Host=false)
{
    if (!$Host) return 0;
    global $Db;
    $Host=ToLower($Host);

    $Query = "SELECT ID FROM ?_host WHERE HOST=?";
    $CheckId=$Db->ReturnValue($Query,$Host);
    if (ValidId($CheckId)) return $CheckId;
    $Query ="INSERT INTO ?_host (HOST) VALUES (?)";
    $Db->Query($Query, $Host);
    return $Db->LastInsertId;    
    
}



function DeletePath($Id)
{
    global $Db, $nsProduct;
    
    $Query = "DELETE FROM ?_path_step WHERE PATH_ID=?";
    $Db->Query($Query,$Id);
    $Query = "DELETE FROM ?_path WHERE ID=?";
    $Db->Query($Query,$Id);
    
    $nsProduct->Redir('funnels','RDlt=1');
}

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