Current File : /home/pacjaorg/public_html/kmm/components/com_djclassifieds/model.php |
<?php
/**
* @package DJ-Classifieds
* @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://dj-extensions.com
* @author email contact@dj-extensions.com
*/
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djcategory.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djregion.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djtheme.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djtype.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djaccess.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djfield.php');
class DJClassifiedsModel extends JModelLegacy
{
static $_items = array();
function getItemById($id, $force_load = false)
{
if(empty(self::$_items[$id]) || $force_load){
$db = JFactory::getDBO();
$par = JComponentHelper::getParams('com_djclassifieds');
$query = "SELECT i.*, c.id as c_id, c.name as c_name, c.alias as c_alias, c.autopublish as c_autopublish, c.price as c_price, c.points as c_points, c.access_view as c_access_view, c.access_item_view as c_access_item_view, iu.name as unit_name, "
."r.id as r_id, r.name as r_name, r.alias as r_alias, "
."u.".$par->get('authorname','name')." as authorname, u.name as u_name, u.username, u.username as u_username, u.email as u_email, p.group_id, "
."t.name as t_name, t.id as t_id, t.params as t_params "
."FROM #__djcf_items i "
."LEFT JOIN #__djcf_categories c ON c.id=i.cat_id "
."LEFT JOIN #__djcf_regions r ON r.id=i.region_id "
."LEFT JOIN #__djcf_types t ON t.id=i.type_id "
."LEFT JOIN #__users u ON u.id=i.user_id "
."LEFT JOIN #__djcf_profiles p ON i.user_id=p.user_id "
."LEFT JOIN #__djcf_items_units iu ON iu.id = i.unit_id "
."WHERE i.id=".$id." LIMIT 1";
$db->setQuery($query);
$item = $db->loadObject();
if($item){
$item->item_uri = DJClassifiedsSEO::getItemRoute($item->id.':'.$item->alias,$item->cat_id.':'.$item->c_alias,$item->region_id.':'.$item->r_alias);
$item->profile_uri = $item->user_id ? DJClassifiedsSEO::getViewUri('profile', array('group_id' => $item->group_id)).'&uid='.DJClassifiedsSEO::getUserSlug($item->user_id, $item->authorname) : null;
}
self::$_items[$id] = $item;
}
return self::$_items[$id];
}
function getItemByToken($token)
{
$db = JFactory::getDBO();
$query = "SELECT id FROM #__djcf_items WHERE user_id=0 AND token=".$db->q($db->escape($token));
$db->setQuery($query);
$id = $db->loadResult();
return $this->getItemById($id);
}
function getProfileRow($uid)
{
$row = JTable::getInstance('Profiles', 'DJClassifiedsTable');
if($uid){
$row->load($uid);
if(!$row->user_id){
$row->user_id = $uid;
}
}
return $row;
}
function getProfile($uid, $in_item = false, $par = null)
{
$par = $par ? $par : JComponentHelper::getParams('com_djclassifieds');
$db = JFactory::getDBO();
$user = JFactory::getUser();
$ug = implode(',', $user->getAuthorisedViewLevels());
$profile = array();
$profile['id'] = $uid;
$profile['juser'] = JFactory::getUser($uid);
$profile['name'] = $this->getAuthorName($uid, $par);
$query = "SELECT * FROM #__djcf_images WHERE item_id = ".$uid." AND type='profile' LIMIT 1";
$db->setQuery($query);
$profile['img'] = $db->loadObject();
if($par->get('fav_profiles','0')){
$query = "SELECT COUNT(id) FROM #__djcf_profiles_fav WHERE profile_id=".$uid." AND user_id=".$user->id;
$db->setQuery($query);
$profile['fav'] = $db->loadResult();
}
$query = $db->getQuery(true);
$query->select(array('fv.*', 'f.*'))
->from('#__djcf_fields f')
->join('LEFT', '#__djcf_fields_values_profile fv ON f.id = fv.field_id AND fv.user_id = '.$uid)
->where('f.source = 2')
->where('f.published = 1')
->where('f.access IN ('.$ug.')')
->order('f.ordering, f.label');
if($in_item){
$query->where('f.in_item = 1');
}
if(JPluginHelper::isEnabled('djclassifieds', 'conditionalfields')){
$query->join('LEFT', '#__djcf_conditionalfields cf ON f.id = cf.field_id');
$query->where('(cf.id IS NULL OR fv.id IS NOT NULL)');
}
$db->setQuery($query);
$profile['data'] = $db->loadObjectList('id');
DJClassifiedsAccess::filterFieldsByGroupAccess($profile['data']);
DJClassifiedsField::convertFieldsValues($profile['data'], $par);
$query = "SELECT * FROM #__djcf_profiles WHERE user_id=".$uid." LIMIT 1";
$db->setQuery($query);
$profile['details'] = $db->loadObject();
$profile['uri'] = DJClassifiedsSEO::getViewUri('profile', array('group_id' => !empty($profile['details']) ? $profile['details']->group_id : null)).'&uid='.DJClassifiedsSEO::getUserSlug($profile['id'], $profile['name']);
return $profile;
}
function getUserFieldGroup($uid)
{
$db = JFactory::getDBO();
$query = "SELECT group_id FROM #__djcf_profiles WHERE user_id=".$uid;
$db->setQuery($query);
$group_id = $db->loadResult();
return $group_id;
}
function getAuthorName($uid, $par)
{
$user = JFactory::getUser((int)$uid);
return $user->{$par->get('authorname','name')};
}
function getDays($cat_id = '0', $user_id = null)
{
$db = JFactory::getDBO();
$ug_arr = DJClassifiedsAccess::getUserGroups($user_id);
$query = "SELECT * FROM #__djcf_days WHERE published=1 ORDER BY days, price DESC";
$db->setQuery($query);
$days_all = $db->loadObjectList();
$days = array();
foreach($days_all as $day){
$cat_access = false;
$query = "SELECT cat_id FROM #__djcf_days_xref WHERE day_id=".$day->id;
$db->setQuery($query);
$day_cat_arr = $db->loadColumn();
$cat_match = $day_cat_arr ? count(array_intersect(explode(',', $cat_id), $day_cat_arr)) : null;
if(!$day_cat_arr || (!$day->cat_access_disallow && $cat_match) || ($day->cat_access_disallow && !$cat_match)){
$cat_access = true;
}
$ug_access = false;
$query = "SELECT group_id FROM #__djcf_days_groups WHERE day_id=".$day->id;
$db->setQuery($query);
$day_ug_arr = $db->loadColumn();
$ug_match = $day_ug_arr ? count(array_intersect($ug_arr, $day_ug_arr)) : null;
if(!$day_ug_arr || (!$day->ug_access_disallow && $ug_match) || ($day->ug_access_disallow && !$ug_match)){
$ug_access = true;
}
if($cat_access && $ug_access && !isset($days[$day->days])){
$days[$day->days] = $day;
}
}
if(isset($days[0])){
$day_0 = $days[0];
unset($days[0]);
$days[0] = $day_0;
}
return $days;
}
function getDuration($exp_days, $cat_id = '0', $user_id = null)
{
$days = $this->getDays($cat_id, $user_id);
return isset($days[$exp_days]) ? $days[$exp_days] : null;
}
function getDurations()
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_days WHERE published=1";
$db->setQuery($query);
$durations = $db->loadObjectList('id');
return $durations;
}
function getPromotions()
{
$db = JFactory::getDBO();
$query = "SELECT * "
."FROM #__djcf_promotions "
."WHERE published=1 "
."ORDER BY ordering, id";
$db->setQuery($query);
$promotions = $db->loadObjectList('id');
return $promotions;
}
function getPromotionsPrices($item = null, $post_data = null)
{
$db = JFactory::getDBO();
$user = JFactory::getUser();
$ug_arr = DJClassifiedsAccess::getUserGroups($user->id);
$promotions = $this->getPromotions();
$query = "SELECT DISTINCT pp.* "
."FROM #__djcf_promotions_prices pp "
."LEFT JOIN #__djcf_promotions_prices_usergroups ppug ON pp.id=ppug.prom_price_id "
."WHERE (ppug.usergroup_id IN (".implode(',', $ug_arr).") OR ppug.usergroup_id IS NULL) "
."ORDER BY pp.prom_id, pp.days";
$db->setQuery($query);
$prom_prices = $db->loadObjectList();
if($item && $item->pay_type){
$item_pay_types = array_filter(explode(',', $item->pay_type));
}
foreach($prom_prices as $pp){
if(isset($promotions[$pp->prom_id])){
if(!isset($promotions[$pp->prom_id]->prices)){
$promotions[$pp->prom_id]->prices = array();
}
if(!empty($item_pay_types) && in_array($promotions[$pp->prom_id]->name.'_'.$promotions[$pp->prom_id]->id.'_'.$pp->days, $item_pay_types)){
$pp->selected = true;
}
$promotions[$pp->prom_id]->prices[$pp->days] = $pp;
}
}
foreach($promotions as $key => $prom){
if(!empty($post_data[$prom->name]) && !empty($prom->prices[$post_data[$prom->name]])){
$promotions[$key]->prices[$post_data[$prom->name]]->selected = true;
}
if(empty($prom->prices)){
unset($promotions[$key]);
}
}
return $promotions;
}
function getItemPromotions($id)
{
if(!$id){
return;
}
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_items_promotions WHERE item_id=".$id;
$db->setQuery($query);
$promotions = $db->loadObjectList('prom_id');
return $promotions;
}
function getCategory($cat_id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_categories WHERE id=".$cat_id." AND published=1 LIMIT 1";
$db->setQuery($query);
$cat = $db->loadObject();
return $cat;
}
function getCategories($ord = 'ordering', $parent_id = null, $form_access = true)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('c.*')
->from('#__djcf_categories c')
->where('c.published != 0')
->order('c.parent_id, c.'.$ord);
if($parent_id !== null){
$query->where('c.parent_id='.$parent_id);
}
if($form_access){
$user = JFactory::getUser();
$ug_arr = DJClassifiedsAccess::getUserGroups($user->id);
$query->select('g.g_active');
$query->join('LEFT', '(SELECT COUNT(id) as g_active, cat_id FROM #__djcf_categories_groups WHERE group_id IN ('.implode(',', $ug_arr).') GROUP BY cat_id) g ON g.cat_id=c.id');
$query->where('(c.access=0 OR (c.access=1 AND g.g_active>0))');
}
$db->setQuery($query);
$cats = $db->loadObjectList('id');
return $cats;
}
function getItemCategories($item_id)
{
$db = JFactory::getDBO();
$query = "SELECT c.* "
."FROM #__djcf_items_categories ic "
."INNER JOIN #__djcf_categories c ON ic.cat_id=c.id "
."WHERE c.published=1 AND ic.item_id=".$item_id;
$db->setQuery($query);
$mcats = $db->loadObjectList('id');
return $mcats;
}
function getRegion($reg_id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_regions WHERE id=".$reg_id." AND published=1 LIMIT 1";
$db->setQuery($query);
$reg = $db->loadObject();
return $reg;
}
function getRegions($ord = 'ordering,name', $parent_id = null)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__djcf_regions')
->where('published != 0')
->order('parent_id, '.$ord);
if($parent_id !== null){
$query->where('parent_id='.$parent_id);
}
$db->setQuery($query);
$regions = $db->loadObjectList('id');
return $regions;
}
function getUserItemsCount($uid)
{
$items_count = $this->getItemsCountBy('user_id');
return !empty($items_count[$uid]) ? $items_count[$uid]->items_count : 0;
}
function getItemsCountBy($col)
{
$par = JComponentHelper::getParams('com_djclassifieds');
$date_now = JFactory::getDate()->format('Y-m-d H:00:00');
$where = '';
$reglist = DJClassifiedsRegion::getDefaultRegionsIds();
if($reglist){
$where .= 'AND region_id IN ('.$reglist.') ';
}
if($par->get('show_archived',0)==2){
$where .= "AND ((published=1 AND date_exp > '".$date_now."') OR published=2) ";
}else{
$where .= "AND published=1 AND date_exp > '".$date_now."' ";
}
$query = "SELECT ".$col.", count(*) as items_count "
."FROM #__djcf_items "
."WHERE ".$col." != 0 AND blocked=0 ".$where." "
."GROUP BY ".$col;
if($par->get('cache_lib_cats','0')=='1' || $par->get('cache_lib_regs','0')=='1'){
$cache = JFactory::getCache();
$cache->setCaching(1);
$items_count = $cache->get(array('DJClassifiedsModel', 'getDboObjectList'), array($query, $col));
}else{
$items_count = self::getDboObjectList($query, $col);
}
return $items_count;
}
static function getDboObjectList($query, $index = null)
{
$db = JFactory::getDBO();
$db->setQuery($query);
return $db->loadObjectList($index);
}
function getTreePathArray($cat_id, $cats, $legacy = false)
{
$cat_path = array();
if($cat_id && $cats){
$id = Array();
$name = Array();
$cid = $cat_id;
if($cid){
while($cid){
foreach($cats as $li){
if($li->id == $cid){
$cid = $li->parent_id;
$id[] = $li->id;
$name[] = $li->name;
$cat_path[] = $li->parent_id.','.$li->id;
break;
}
}
if($cid == $cat_id){
break;
}
}
if(!$legacy && count($cat_path) > 1){
$cat_path = array_reverse($cat_path);
array_shift($cat_path);
}
}
}
return $cat_path;
}
function getTypes() // backward compatibility
{
return DJClassifiedsType::getTypes();
}
function getSearchFieldsQuery($type = 'item', $cat_id = 0)
{
$app = JFactory::getApplication();
$db = JFactory::getDBO();
if($type == 'item'){
$fv_table = '#__djcf_fields_values';
$fv_item_id = 'item_id';
$items_join_col = 'i.id';
if($cat_id){
$cat_tree = DJClassifiedsCategory::getSEOParentPath($cat_id);
$parent_cid = (int)end($cat_tree);
}else{
$parent_cid = 0;
}
$app->setUserState('last_parent_cid', $parent_cid);
}else{
$fv_table = '#__djcf_fields_values_profile';
$fv_item_id = 'user_id';
$items_join_col = 'u.id';
}
$query = $db->getQuery(true);
$query->select('f.*')
->from('#__djcf_fields f')
->where('f.published = 1');
if($type == 'item'){
$query->where('f.source < 2');
if($cat_id){
$query->join('INNER', '#__djcf_fields_xref fx ON fx.field_id=f.id AND fx.cat_id='.$cat_id);
}
}else{
$query->where('f.source = 2');
}
$db->setQuery($query);
$fields = $db->loadObjectList();
$search_fields = "";
$sf_count = 0;
foreach($fields as $f){
if($f->type=='date'){
if($f->search_type=='inputbox_min_max'){
if($app->input->get->exists('se_'.$f->id.'_min') || $app->input->get->exists('se_'.$f->id.'_max')){
$f_v1 = $app->input->get->exists('se_'.$f->id.'_min') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_min')) : '';
$f_v2 = $app->input->get->exists('se_'.$f->id.'_max') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_max')) : '';
if($f_v1!='' || $f_v2!=''){
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." ";
foreach(array('>=' => $f_v1, '<=' => $f_v2) as $operator => $f_v){
if($f_v!=''){
if($f->date_format == 'AGE'){
$search_fields .= " AND (SELECT YEAR('".JFactory::getDate()->toSQL()."') - YEAR(f.value_date) - (DATE_FORMAT('".JFactory::getDate()->toSQL()."', '%m%d') < DATE_FORMAT(f.value_date, '%m%d'))) ".$operator." '".$f_v."'";
}else{
if($f_v!='0000-00-00 00:00:00' && $f->date_format){
$f_v = DJClassifiedsTheme::dateDBConvert($f_v, $f->date_format);
}
$search_fields .= " AND f.value_date ".$operator." '".$f_v."'";
}
}
}
$search_fields .= " ) UNION ";
$app->setUserState('se_'.$f->id.'_min',$f_v1);
$app->setUserState('se_'.$f->id.'_max',$f_v2);
}
}
if(!$app->input->get->exists('se_'.$f->id.'_min')){
$app->setUserState('se_'.$f->id.'_min','');
}
if(!$app->input->get->exists('se_'.$f->id.'_max')){
$app->setUserState('se_'.$f->id.'_max','');
}
}else{
if($app->input->get->exists('se_'.$f->id)){
$f_v1 = $db->escape($app->input->get->getVar('se_'.$f->id));
if($f_v1!=''){
if($f_v1!='0000-00-00 00:00:00' && $f->date_format){
$f_v1 = DJClassifiedsTheme::dateDBConvert($f_v1, $f->date_format);
}
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." AND f.value_date = '".$f_v1."' ) UNION ";
$app->setUserState('se_'.$f->id,$f_v1);
}else{
$app->setUserState('se_'.$f->id,'');
}
}else{
$app->setUserState('se_'.$f->id,'');
}
}
}else if($f->type=='date_from_to'){
if($f->search_type=='inputbox_min_max'){
if($app->input->get->exists('se_'.$f->id.'_min') || $app->input->get->exists('se_'.$f->id.'_max')){
$f_v1 = $app->input->get->exists('se_'.$f->id.'_min') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_min')) : '';
$f_v2 = $app->input->get->exists('se_'.$f->id.'_max') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_max')) : '';
if($f_v1!='' || $f_v2!=''){
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." ";
if($f_v1!=''){
if($f_v1!='0000-00-00 00:00:00' && $f->date_format){
$f_v1 = DJClassifiedsTheme::dateDBConvert($f_v1, $f->date_format);
}
$search_fields .= " AND f.value_date_to >= '".$f_v1."'";
}
if($f_v2!=''){
if($f_v2!='0000-00-00 00:00:00' && $f->date_format){
$f_v2 = DJClassifiedsTheme::dateDBConvert($f_v2, $f->date_format);
}
$search_fields .= " AND f.value_date <= '".$f_v2."' ";
}
$search_fields .= " ) UNION ";
$app->setUserState('se_'.$f->id.'_min',$f_v1);
$app->setUserState('se_'.$f->id.'_max',$f_v2);
}
}
if(!$app->input->get->exists('se_'.$f->id.'_min')){
$app->setUserState('se_'.$f->id.'_min','');
}
if(!$app->input->get->exists('se_'.$f->id.'_max')){
$app->setUserState('se_'.$f->id.'_max','');
}
}else{
if($app->input->get->exists('se_'.$f->id)){
$f_v1 = $db->escape($app->input->get->getVar('se_'.$f->id));
if($f_v1!=''){
if($f_v1!='0000-00-00 00:00:00' && $f->date_format){
$f_v1 = DJClassifiedsTheme::dateDBConvert($f_v1, $f->date_format);
}
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." AND f.value_date = '".$f_v1."' ) UNION ";
$app->setUserState('se_'.$f->id,$f_v1);
}else{
$app->setUserState('se_'.$f->id,'');
}
}else{
$app->setUserState('se_'.$f->id,'');
}
}
}else{
if($f->search_type=='select_min_max' || $f->search_type=='inputbox_min_max'){
if($app->input->get->exists('se_'.$f->id.'_min') || $app->input->get->exists('se_'.$f->id.'_max')){
$f_v1 = $app->input->get->exists('se_'.$f->id.'_min') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_min')) : '';
$f_v2 = $app->input->get->exists('se_'.$f->id.'_max') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_max')) : '';
if($f_v1!='' || $f_v2!=''){
$sf_count ++;
if(is_numeric($f_v1) || is_numeric($f_v2)){
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." ";
if(is_numeric($f_v1)){
$search_fields .= " AND f.value >= ".$f_v1." ";
}
if(is_numeric($f_v2)){
$search_fields .= " AND f.value <= ".$f_v2." ";
}
$search_fields .= " ) UNION ";
}else{
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." ";
if($f_v1){
$search_fields .= " AND f.value >= '".$f_v1."'";
}
if($f_v2){
$search_fields .= " AND f.value <= '".$f_v2."' ";
}
$search_fields .= " ) UNION ";
}
$app->setUserState('se_'.$f->id.'_min',$f_v1);
$app->setUserState('se_'.$f->id.'_max',$f_v2);
}
}
if(!$app->input->get->exists('se_'.$f->id.'_min')){
$app->setUserState('se_'.$f->id.'_min','');
}
if(!$app->input->get->exists('se_'.$f->id.'_max')){
$app->setUserState('se_'.$f->id.'_max','');
}
}elseif($f->search_type=='checkbox' || $f->search_type=='checkbox_accordion_o' || $f->search_type=='checkbox_accordion_c'){
if($app->input->get->exists('se_'.$f->id)){
$v_chec = array();
$v_chec = explode(',', $app->input->get->getVar('se_'.$f->id));
$f_v1 =';';
if($v_chec){
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." AND ( ";
$v_c_arr = array();
foreach($v_chec as $v_c){
if($f->type=='checkbox'){
$v_c_arr[] = "f.value LIKE '%;".$db->escape($v_c).";%'";
}elseif($f->type=='inputbox' || $f->type=='textarea'){
$v_c_arr[] = "f.value LIKE '%".$db->escape($v_c)."%'";
}else{
$v_c_arr[] = "f.value = '".$db->escape($v_c)."'";
}
$f_v1 .= $v_c.';';
}
if($f->chx_filter_logic==1){
$search_fields .= implode(' AND ', $v_c_arr);
}else{
$search_fields .= implode(' OR ', $v_c_arr);
}
$search_fields .= ") ";
$search_fields .= " ) UNION ";
$app->setUserState('se_'.$f->id,$f_v1);
}
}else{
$app->setUserState('se_'.$f->id,'');
}
}elseif($f->search_type=='date_min_max'){
$f_v1 = $app->input->get->exists('se_'.$f->id.'_min') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_min')) : '';
$f_v2 = $app->input->get->exists('se_'.$f->id.'_max') ? $db->escape($app->input->get->getVar('se_'.$f->id.'_max')) : '';
if($f_v1!='' || $f_v2!=''){
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." ";
if($f_v1){
$search_fields .= " AND (CASE WHEN f.value_date_end='0000-00-00 00:00:00' THEN DATE_FORMAT(f.value_date_start,'%Y-%m-%d') ELSE DATE_FORMAT(f.value_date_end,'%Y-%m-%d') END) >= '".$f_v1."'";
}
if($f_v2){
$search_fields .= " AND DATE_FORMAT(f.value_date_start,'%Y-%m-%d') <= '".$f_v2."' ";
}
$search_fields .= " ) UNION ";
$app->setUserState('se_'.$f->id.'_min',$f_v1);
$app->setUserState('se_'.$f->id.'_max',$f_v2);
}
if(!$app->input->get->exists('se_'.$f->id.'_min')){
$app->setUserState('se_'.$f->id.'_min','');
}
if(!$app->input->get->exists('se_'.$f->id.'_max')){
$app->setUserState('se_'.$f->id.'_max','');
}
}elseif($f->search_type=='select'){
if($app->input->get->exists('se_'.$f->id)){
$f_v1 = $db->escape($app->input->get->getVar('se_'.$f->id));
if($f_v1!=''){
$sf_count ++;
if($f->type=='checkbox'){
$val_collate = "f.value LIKE '%;".$db->escape($f_v1).";%'";
}elseif($f->type=='inputbox' || $f->type=='textarea'){
$val_collate = "f.value LIKE '%".$db->escape($f_v1)."%'";
}else{
$val_collate = "f.value = '".$db->escape($f_v1)."'";
}
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." AND ".$val_collate.") UNION ";
$app->setUserState('se_'.$f->id,$f_v1);
}else{
$app->setUserState('se_'.$f->id,'');
}
}else{
$app->setUserState('se_'.$f->id,'');
}
}else{
if($app->input->get->exists('se_'.$f->id)){
$f_v1 = $db->escape($app->input->get->getVar('se_'.$f->id));
if($f_v1!=''){
$sf_count ++;
$search_fields .= " (SELECT * FROM ".$fv_table." f WHERE f.field_id=".$f->id." AND f.value LIKE '%".$f_v1."%' ) UNION ";
$app->setUserState('se_'.$f->id,$f_v1);
}else{
$app->setUserState('se_'.$f->id,'');
}
}else{
$app->setUserState('se_'.$f->id,'');
}
}
}
}
if($sf_count){
$search_fields = "INNER JOIN (SELECT * FROM (SELECT COUNT(*) AS c, ".$fv_item_id." FROM (".substr($search_fields, 0, -6).") AS f GROUP BY f.".$fv_item_id." ) f WHERE f.c = ".$sf_count." ) sf ON ".$items_join_col."=sf.".$fv_item_id." ";
}
// 'use in word search' custom fields support
$search_fields .= "LEFT JOIN (SELECT GROUP_CONCAT(REPLACE(fv.value,';',' ') SEPARATOR ' ') vals, fv.".$fv_item_id." FROM #__djcf_fields f INNER JOIN ".$fv_table." fv ON f.id=fv.field_id WHERE f.published=1 AND f.use_in_word_search=1 GROUP BY fv.".$fv_item_id.") ws ON ".$items_join_col."=ws.".$fv_item_id." ";
return $search_fields;
}
function getTermsLink($id, $for_modal = false)
{
$content_helper_route = JPATH_ROOT.'/components/com_content/helpers/route.php';
if(file_exists($content_helper_route)){
include_once $content_helper_route;
}
$db = JFactory::getDBO();
$query = "SELECT a.id, a.alias, a.catid, c.alias as c_alias "
."FROM #__content a "
."LEFT JOIN #__categories c ON c.id=a.catid "
."WHERE a.state=1 AND a.id=".$id;
$db->setQuery($query);
$terms_article = $db->loadObject();
if($terms_article){
$slug = $terms_article->id.':'.$terms_article->alias;
$cslug = $terms_article->catid.':'.$terms_article->c_alias;
$article_link = ContentHelperRoute::getArticleRoute($slug, $cslug);
if($for_modal){
$article_link .='&tmpl=component';
}
$terms_link = JRoute::_($article_link, false);
return $terms_link;
}
}
public function getPurchaseDetails($user_id)
{
if(!$user_id){
return;
}
$db = JFactory::getDBO();
$query = "SELECT fv.value "
."FROM #__djcf_fields_values_profile fv "
."INNER JOIN #__djcf_fields f ON fv.field_id=f.id AND f.published=1 AND f.source=2 AND f.name='purchase_details' "
."WHERE fv.user_id=".$user_id;
$db->setQuery($query);
$purchase_details = $db->loadResult();
return $purchase_details;
}
function getUserPoints()
{
$db = JFactory::getDBO();
$user = JFactory::getUser();
$query = "SELECT SUM(points) FROM #__djcf_users_points WHERE user_id=".$user->id;
$db->setQuery($query);
$points_count = $db->loadResult();
return $points_count ? $points_count : 0;
}
function getBids($id, $limit=0)
{
if(!$id){
return;
}
$db = JFactory::getDBO();
$query = "SELECT a.*, u.name as u_name "
."FROM #__djcf_auctions a "
."INNER JOIN #__users u ON a.user_id=u.id "
."INNER JOIN #__djcf_items i ON a.item_id=i.id "
."WHERE a.item_id=".$id." AND (CASE WHEN i.date_renew IS NOT NULL AND i.date_renew!='0000-00-00 00:00:00' THEN a.date>i.date_renew ELSE 1=1 END) "
."ORDER BY a.date DESC, a.id DESC";
$db->setQuery($query);
$bids = $db->loadObjectList();
$prev_user_id = 0;
$ret_bids = array();
foreach($bids as $key => $bid){
if($prev_user_id != $bid->user_id){
$ret_bids[] = $bid;
}
$prev_user_id = $bid->user_id;
if($limit && $key+1 == $limit){
break;
}
}
return $ret_bids;
}
function getHighestBid($item_id)
{
$db = JFactory::getDBO();
$query = "SELECT a.price "
."FROM #__djcf_auctions a "
."INNER JOIN #__users u ON a.user_id=u.id "
."INNER JOIN #__djcf_items i ON a.item_id=i.id "
."WHERE a.item_id=".$item_id." AND (CASE WHEN i.date_renew IS NOT NULL AND i.date_renew!='0000-00-00 00:00:00' THEN a.date>i.date_renew ELSE 1=1 END) "
."ORDER BY a.price DESC LIMIT 1";
$db->setQuery($query);
$max_bid = $db->loadResult();
return $max_bid;
}
function getUserMaxBid($item_id, $user_id)
{
$db = JFactory::getDBO();
$query = "SELECT max_bid FROM #__djcf_auctions_assist WHERE item_id=".$item_id." AND user_id=".$user_id." ORDER BY max_bid DESC LIMIT 1";
$db->setQuery($query);
$user_max_bid = $db->loadResult();
return $user_max_bid;
}
function getMinBid($item, $bids)
{
$bid_min = $item->bid_min > 0 ? $item->bid_min : 1;
$price_start = $item->price_start > 0 ? $item->price_start : $bid_min;
$min_bid = $bids ? $bids[0]->price + $bid_min : $price_start;
return $min_bid;
}
function getField($id)
{
return DJClassifiedsField::getField($id);
}
function getFieldGroup($group_id)
{
return DJClassifiedsField::getFieldGroup($group_id);
}
function getPayment($id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_payments WHERE id=".$id;
$db->setQuery($query);
$payment = $db->loadObject();
return $payment;
}
function getPointsPackage($id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_points WHERE published=1 AND id=".$id;
$db->setQuery($query);
$points = $db->loadObject();
return $points;
}
function getOrder($id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_orders WHERE id=".$id;
$db->setQuery($query);
$order = $db->loadObject();
return $order;
}
function getOffer($id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_offers WHERE id=".$id;
$db->setQuery($query);
$offer = $db->loadObject();
return $offer;
}
function getPlan($id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_plans WHERE published=1 AND id=".$id;
$db->setQuery($query);
$plan = $db->loadObject();
return $plan;
}
function getUserPlans($user_id)
{
$db = JFactory::getDBO();
$query = "SELECT p.* FROM #__djcf_plans_subscr ps INNER JOIN #__djcf_plans p ON p.id=ps.plan_id WHERE ps.user_id=".$user_id;
$db->setQuery($query);
$user_plans = $db->loadObjectList('id');
return $user_plans;
}
function getUserActivePlans($user_id)
{
$db = JFactory::getDBO();
$query = "SELECT p.*, ps.adverts_available, ps.id subscr_id "
."FROM #__djcf_plans_subscr ps "
."INNER JOIN #__djcf_plans p ON p.id=ps.plan_id "
."WHERE ps.user_id=".$user_id." "
."AND (".$this->getPlanActiveWhere().")";
$db->setQuery($query);
$user_plans = $db->loadObjectList('id');
return $user_plans;
}
function getItemPlan($item_id)
{
$db = JFactory::getDBO();
$query = "SELECT p.* "
."FROM #__djcf_plans_subscr_items psi "
."INNER JOIN #__djcf_plans_subscr ps ON psi.subscr_id=ps.id "
."INNER JOIN #__djcf_plans p ON ps.plan_id=p.id WHERE psi.item_id=".$item_id;
$db->setQuery($query);
return $db->loadObject();
}
function getItemPlansRenew($item_id)
{
$db = JFactory::getDBO();
$query = "SELECT p.name, psir.date "
."FROM #__djcf_plans_subscr_items_renew psir "
."INNER JOIN #__djcf_plans_subscr ps ON psir.subscr_id=ps.id "
."INNER JOIN #__djcf_plans p ON ps.plan_id=p.id "
."WHERE psir.item_id=".$item_id." "
."ORDER BY psir.date DESC";
$db->setQuery($query);
return $db->loadObjectList();
}
function getPlanActiveWhere()
{
return '!('.$this->getPlanExpiredWhere().')';
}
function getPlanExpiredWhere()
{
$db = JFactory::getDBO();
$date_now = JFactory::getDate()->toSQL();
$plans_plugin = JPluginHelper::getPlugin('djclassifieds', 'plans');
$plugin_params = new JRegistry($plans_plugin->params);
$free_expiration = $plugin_params->get('ps_free_plan_expiration','0'); // 0 - expiration date or 0 adverts limit reached; 1 - only when expiration date reached
$q_free = DJClassifiedsPayment::getFreeWhere();
$q_notfree = DJClassifiedsPayment::getFreeWhere(false);
$q_expired = "(date_exp <= ".$db->q($date_now)." AND date_exp != '0000-00-00 00:00:00')";
$q_noadverts = "adverts_available = 0";
$default_exp_where = "(".$q_notfree." AND ".$q_expired.") OR (".$q_free." AND (".$q_expired.($free_expiration == '0' ? " OR ".$q_noadverts : "")."))";
$exp_where = "(CASE exp_type WHEN '0' THEN (".$q_expired." OR ".$q_noadverts.") WHEN '1' THEN (".$q_expired.") ELSE ".$default_exp_where." END)";
return $exp_where;
}
}