Location: PHPKode > scripts > SeeMeLikeMe > untitled folder/includes/lightwork_mini_smlm.php
<?php
/*
    (c) copyright 2011 nadlabs.co.uk. All rights reserved.
    
    Please read the full text of the nl-DFLA-mini licence for this software at
    
    http://www.nadlabs.co.uk/licence.php

*/


class smlm{
    
    
    
    public function get_votes($id,$type,$days){
        
        global $conn;
        
        $day_sql='';
        if($days!=0){
            $day_sql = " AND date_vote > DATE_SUB(NOW(),INTERVAL $days DAY) ";
        }
        
        $sql_getvote = "
                            SELECT
                            FORMAT(IFNULL(SUM(IF(vote_val=1,1,0)),0),0) AS likes,
                            FORMAT(IFNULL(SUM(IF(vote_val=-1,1,0)),0),0) AS dislikes,
                            FORMAT(IFNULL(SUM(vote_val),0),0) AS rating
                            
                            FROM mini_smlm_vote
                            
                            WHERE valid=1
                            AND elementid='$id'
                            AND type = '$type'
                            $day_sql
        
                        ";
                        
       $return = globalQuery($sql_getvote,$conn,2);
       if($return[1]>0){
              
                
                return array('Ack'=>'success','Msg'=>'success',"likes"=>$return[0]['likes'],"dislikes"=>$return[0]['dislikes'],"rating"=>$return[0]['rating']);
                
                
                
                      
       }
       else{
        
                return array('Ack'=>'fail','Msg'=>'Oops!');
        
       }
       
    }
    
    public function vote($vote,$id,$type){
        global $conn;
        
        $ipad = globalMagic($_SERVER['REMOTE_ADDR']);
        
        if(IP_OR_USERID=='USERID' && !isset($_SESSION['userid']) ){
             return array('Ack'=>'fail','Msg'=>'Sign in to vote.');
        }
    
            $userid = (isset($_SESSION['userid']))?globalMagic($_SESSION['userid']):-9;
                       
            $iporuserid = (IP_OR_USERID=='IP')?" ipad = '$ipad'":"userid='$userid'";
       
            
            $sql_vote_check = "
                         SELECT voteid
                         FROM mini_smlm_vote
                         WHERE $iporuserid
                         AND elementid='$id'
                         AND type='$type'
                         ";
                       
            $sql_vote_insert = "
                                INSERT INTO mini_smlm_vote VALUES
                                (NULL,'$userid','$ipad','$vote','$id','$type',now(),1);
                            "; 
                         
            $return = globalQuery($sql_vote_check,$conn,2);
            
            //if already voted
            if($return[1]>0){
                //just tell them they voted (or you can set to fail and give a different message)
                //return array('Ack'=>'success','Msg'=>'Thanks for voting.','change'=>0);
                
                return array('Ack'=>'fail','Msg'=>'You have already voted.','change'=>0);
            }
            else{
                //echo $sql_vote_insert;
                $return = globalQuery($sql_vote_insert,$conn,1);
                
                if($return[1]==1){
                    return array('Ack'=>'success','Msg'=>'Thanks for voting.','change'=>$vote);
                }
                else{
                    return array('Ack'=>'fail','Msg'=>'Oops. Try again.');
                }
                
            }
    }
    
    public function trackstat($elementid,$type){
        global $conn;
        if(VISIT_TRACK){
            
            $userid = (isset($_SESSION['userid']))?$_SESSION['userid']:'';
            $reg = ($userid!='')?1:0;
            $ipad = globalMagic($_SERVER['REMOTE_ADDR']);
            $refurl =(isset($_SERVER['HTTP_REFERER']))?globalMagic($_SERVER['HTTP_REFERER']):'type-in-traffic';
            $refdomain = getDomain($refurl);
            $refdomain = (substr($refdomain,0,4)=='www.')?str_replace('www.','',$refdomain):$refdomain;
            
            if (!isset($_SESSION['visit_track'])){
                
                
               
                $vflag = 1;
                
                
                
                
             
                
                $sql_insert = "
                                    INSERT INTO mini_smlm_page_stat VALUES
                                    
                                    (NULL,'$userid','$reg','$ipad','$vflag','$refurl','$refdomain','$elementid','$type',now(),1);
                                ";
                
                $return = globalQuery($sql_insert,$conn,1);
                
                if($return[1]==1){
                    $_SESSION['visit_track']=1;
                    return true;
                }
                else{
                    return false;
                }
                
                
                
                
                
            }
            else{
                
                if(VIEW_TRACK){
                    
                  
                    $vflag = 0;
                    
                  
                    $sql_insert = "
                                    INSERT INTO mini_smlm_page_stat VALUES
                                    
                                    (NULL,'$userid','$reg','$ipad','$vflag','$refurl','$refdomain','$elementid','$type',now(),1);
                                ";
                
                    $return = globalQuery($sql_insert,$conn,1);
                    
                    if($return[1]==1){
                        return true;
                    }
                    else{
                        return false;
                    }
                
                
                
                
                
                }
            }
            
           
        }
    }
    
  
    
    
    public function get_stats($elementid,$type,$days){
        global $conn;
        
        /*
            to get views, visitors, rolling stats + URL defined 
        */
        $day_sql='';
        $in_domain='';
        if($days!=0){
            $day_sql = " AND visitdate > DATE_SUB(NOW(),INTERVAL $days DAY) ";
        }
        
       
        
        $sql_getstats = "
                            SELECT
                            FORMAT(IFNULL(COUNT(statid),0),0) AS statcounter,
                          
                            DATE_FORMAT(visitdate,'%d/%m/%Y') AS trend_date
                            
                            FROM mini_smlm_page_stat
                            WHERE valid=1
                            AND elementid='$elementid'
                            AND type = '$type' 
                            $day_sql 
                            GROUP BY DATE_FORMAT(visitdate,'%d/%m/%Y') 
                            ORDER BY visitdate ASC
                            
                        ";
                        
        if(IGNORE_INTERNAL_REF){
            $in_domain = " AND refdomain != '".INT_DOM."' ";
            
        }
        
        
        
        
                     
        $sql_get_refurl = "
                                SELECT FORMAT(IFNULL(COUNT(statid),0),0) AS statcounter,refurl, DATE_FORMAT(MIN(visitdate),'%d/%m/%Y')  AS trend_date_url
                                FROM mini_smlm_page_stat
                                WHERE valid =1
                                AND elementid = '$elementid'
                                AND TYPE = '$type'
                                $day_sql
                                $in_domain
                                GROUP BY refurl
                                ORDER BY visitdate ASC
        
                            ";
                            
                            
        $sql_get_views = "
                        SELECT FORMAT(IFNULL(COUNT(statid),0),0) AS views
                                FROM mini_smlm_page_stat
                                WHERE valid =1
                                AND elementid = '$elementid'
                                AND TYPE = '$type'
                            $in_domain
                        ";
        
        
 //echo $sql_get_refurl;
         $return_views = globalQuery($sql_get_views,$conn,2);
         $return = globalQueryPlus($sql_getstats,$conn,2);
         $return_url = globalQueryPlus($sql_get_refurl,$conn,2);
                    
             if($return_views[1]>0){
                $views = $return_views[0]['views'];
             }
             else{
                $views='failed to load ';
             }
            
        
        if($return[1]>0){
            $set    = array();
            
            
            if($return_url[1]>0){
                $refset = array();
                while($rowref = mysql_fetch_array($return_url[0],MYSQL_ASSOC)) {
                
                
                
                    $refset[] = $rowref;
              
                }
            }
            else{
                $refset = 'false';
            }
            
            while($row = mysql_fetch_array($return[0],MYSQL_ASSOC)) {
                
                
                
                $set[] = $row;
              
            }
            
            
            return array('Ack'=>'success','Msg'=>"success", "data"=>$set,"refdata"=>$refset,"views"=>$views);
        }
        else{
            return  array('Ack'=>'fail','Msg'=>"Oops. That wasn't supposed to happen.");
        }
    }


}

?>
Return current item: SeeMeLikeMe