Location: PHPKode > projects > Esvon Personals > esvon-personals/modules/Favorites/hw_user.php
<?php

// --------------------------------------------------------------------------
//
// Esvon Classifieds v.4.0
// Copyright(C), Esvon LTD, 2001-2010, All Rights Reserved.
// E-mail: hide@address.com
//
// All forms of reproduction, including, but not limited to, internet posting, 
// printing, e-mailing, faxing and recording are strictly prohibited.
// One license required per site running Esvon Classifieds. 
// To obtain a license for using Esvon Classifieds, please register at
// http://www.esvon.com/pg/products/p_classifieds/
//
// --------------------------------------------------------------------------

if(!defined('SITE_PATH')) die('Access denied');

define('TBL_FAV_AD', hwModTable(HW_MOD,'link'));

define('MOD_FAV_COOKIE', 'ModFavIDs');

class Favorites_Guest {
  var $cookie_name = MOD_FAV_COOKIE;
  var $A_ID = array();
  var $_total = 0;
  function Favorites_Guest(){
    if($_COOKIE[$this->cookie_name]){
      $this->A_ID = explode(',', $_COOKIE[$this->cookie_name]);
      if($this->A_ID){ 
        $this->A_ID = array_flip($this->A_ID);
        $this->_total = count($this->A_ID);
      }
    }
    //register_shutdown_function(array(&$this, 'saveInCookies'));
  }
  function saveInCookies(){
    if($this->_total == count($this->A_ID)) return; // no change
    if(empty($this->A_ID)) setcookie($this->cookie_name, '', 1);
    else setcookie($this->cookie_name, implode(',',array_keys($this->A_ID)), time() + 3600 * 24 * 365 );
  }
  function remove($id){ 
    unset($this->A_ID[$id]);
    $this->saveInCookies();
  }
  function clean(){ 
    $this->A_ID = array(); 
    $this->saveInCookies();
  }
  function add($id){ 
    if(ctype_digit((string)$id)){ 
      $this->A_ID[$id] = 1;
      $this->saveInCookies();
    }
  }
  function getCount(){ return count($this->A_ID); }
  function getItemsSQLWhere(){
    $a_lid = array();
    if($this->A_ID){
      $a_lid = array_keys($this->A_ID);
      $a_lid = array_map('intval', $a_lid);
    }
    return ' WHERE '.( empty($a_lid) ? '0' : 'link_id IN('.implode(',',$a_lid).')' );
  }
}

class Favorites_Member {
  var $db;
  var $uid;
  function Favorites_Member(){
    $this->db = &$GLOBALS['db'];
    $this->uid = (int)hwSessionGetVar('userid');
    if($_COOKIE[MOD_FAV_COOKIE]){ // autoimport guest favorites
      $o_Fav = new Favorites_Guest;
      $a_id = $this->db->one_col_array('SELECT link_id FROM '.TBL_AD.$o_Fav->getItemsSQLWhere());
      if($a_id){ foreach($a_id as $id) $this->add($id); }
      $o_Fav->clean();
    }
  }
  function remove($id){
    $this->db->query('DELETE FROM '.TBL_FAV_AD.' WHERE link_id='.(int)$id.' AND user_id='.$this->uid);
  }
  function clean(){
    $this->db->query('DELETE FROM '.TBL_FAV_AD.' WHERE user_id='.$this->uid);
  }
  function add($id){
    $this->db->query('INSERT IGNORE INTO '.TBL_FAV_AD.' (user_id,link_id) VALUES ('.$this->uid.','.(int)$id.')');
  }
  function getCount(){
    return $this->db->one_data('SELECT COUNT(*) FROM '.TBL_FAV_AD.' WHERE user_id='.$this->uid);
  }
  function getItemsSQLWhere(){
    return ', '.TBL_FAV_AD.' fl WHERE fl.user_id='.$this->uid.' AND fl.link_id=l.link_id';
  }
}

// main function

function Favorites_main(){
global $db,$TITLE_FIELD;

$ses_uid = (int)hwSessionGetVar('userid');
$class_id = $ses_uid ? 'Favorites_Member' : 'Favorites_Guest';
$o_Fav = new $class_id;

if($_GET['delid'] && ctype_digit($_GET['delid'])){
  $o_Fav->remove($_GET['delid']);
  Fav_Redirect();
  exit;
}

$id = (int)$_REQUEST['id'];

if($_GET['pg']=='fav_add' && $id>0){
  $id = $db->one_data('SELECT link_id FROM '.TBL_AD.' WHERE link_id='.$id);
  if($id){
    $o_Fav->add($id);
    Fav_Redirect();
    exit;
  }
}

  $DISP_ROWS = 20;

  $off = (ctype_digit($_GET['off']) && $_SERVER['REQUEST_METHOD']=='GET') ? $_GET['off'] : 0;
  $num = $off*$DISP_ROWS;

  $numrecs = $o_Fav->getCount();

  $tpl = new HawkTpl;
  $tpl->InitArray('row');
  
  $catid = -1;
  $z = array();

  $res = $db->query('SELECT l.'.str_replace(',',',l.',hwSQLFieldsGet()).',l.catid FROM '.TBL_AD.' l'.
         $o_Fav->getItemsSQLWhere().
         ' ORDER BY l.catid,l.'.$TITLE_FIELD." LIMIT $num,".$DISP_ROWS);

  if(mysql_num_rows($res)>0){
    while($v = mysql_fetch_assoc($res)){
      if($catid!=$v['catid']){
        $catid = $v['catid'];
        $z['cname'] = GetCatPathCached($catid);
        $tpl->AddCell('row',$z,'cat');
      }
      $_lid = $v['link_id'];
      $v = hwSQLRowGet($v);
      $v['link_id'] = $_lid;
      $v['num'] = ++$num;
      $tpl->AddCell('row',$v,'link');
    }
  }
  else{
    $tpl->AddCell('row','','empty');
  }

  $tpl_v = array();
  $tpl_v['nav'] = GetNavigation($_SERVER['PHP_SELF'],$DISP_ROWS,10,$numrecs,$off,'&mod='.HW_MOD);

  $tpl->Parse(HW_MOD_TPL.'fav_list.htm', $tpl_v, 1);

}

function Fav_Redirect(){
    echo '<html><head>
    <META HTTP-EQUIV=Refresh CONTENT="0; URL='.HW_MOD_URL.'">
    </head></html>';
}

?>
Return current item: Esvon Personals