Your IP : 216.73.216.54


Current Path : /var/www/html/components/com_jvld/models/
Upload File :
Current File : /var/www/html/components/com_jvld/models/links.php

<?php
/**
 * @version		$Id$
 * @package		JV-LinkDirectory
 * @subpackage	com_jvld
 * @copyright	Copyright 2008-2013 JV-Extensions. All rights reserved
 * @license		GNU General Public License version 3 or later
 * @author		JV-Extensions
 * @link		http://www.jv-extensions.com
 */

// No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');

class JvldModelLinks extends JModelList
{
	var $input;
	var $filter_group = 'com_jvld.catpage.filters';
	var $srch_fields = array();

	var $isSearch = 0;

	protected function getListQuery($pinned=0)
	{
		$cfg = JvldCfg::getInstance();

		$this->input = JFactory::getApplication()->input;

		$cid = $this->input->getInt('cid', 0);

		$u_lalevels = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
		$langcheckq = (_JVLD_JOOMLA_MLANG) ?  " and (L.language = '".JFactory::getLanguage()->getTag()."' or L.language = '*')" : "";

		// get searchable extra fields
		$this->srch_fields = JvldDb::getRows("select id from #__jvld_extrafields where searchable = 1 order by fieldtype desc");

		$select_sql_1 = '';
		$radius_filters = $this->getRadiusFilters();

		if ($radius_filters['radius'] && ($radius_filters['saddress'] != ''))
		{
			$geocache = new JvldGeocodescache();
			$saddress_coord = $geocache->getCoordinates($radius_filters['saddress']);
			if (($saddress_coord['lat'] != '0.0000') && ($saddress_coord != 0) && ($saddress_coord != ''))
			{
				$ruval = ($cfg->get('distance_metric') == 'mi') ? 69.0 : 111.1111;
				$select_sql_1 = ", " . $ruval . " * DEGREES(ACOS(COS(RADIANS(".$saddress_coord['lat'].")) * COS(RADIANS(coord_lat)) * COS(RADIANS(".$saddress_coord['lng'].") - RADIANS(coord_lng)) + SIN(RADIANS(".$saddress_coord['lat'].")) * SIN(RADIANS(coord_lat)))) as distance";

				$this->isSearch = 1;
			}
		}

		$dsql = "	select distinct L.id ".$select_sql_1."
					from #__jvld_links as L, #__jvld_catlinks as CL, #__categories as JC, #__jvld_linkprof as PROF  
					where 
						PROF.lid = L.id and 
						CL.cid = JC.id and 
						CL.cid = ".(int)$cid." and 
						L.link_status = "._JVLD_LINKSTATUS_ESTABLISHED." and 
						L.access in (".$u_lalevels.") and 
						JC.access in (".$u_lalevels.") {PINNED-SQL} and  
						L.id = CL.lid ".$langcheckq." ";

		$searchq = $this->getSearchString();
		if ($searchq != '')
		{
			// Get the column names of extra fields that can be searched into
			$field_names = array();
			$srch_fields_cnt = count($this->srch_fields);
			for ($i = 0; $i < $srch_fields_cnt; $i++)
			{
				$sfield = new JvldField($this->srch_fields[$i]->id);
				if (($sfield->getTypeSearchMode() == _JVLD_FIELD_SEARCHTYPE_CONTAINED) && (!$sfield->isTypeOptionBased()) && (!$sfield->isTypeMultiValued()))
				{
					$field_names[] = "L.field".$sfield->getID();
				}
			}

			if (count($field_names))
			{
				$search_match = "match(L.partner_title, L.metadesc, L.metakeys, PROF.title, PROF.description, ";
				$search_match .= implode(", ", $field_names);
				$search_match .= ")";
			}
			else
			{
				$search_match = "match(L.partner_title, L.metadesc, L.metakeys, PROF.title, PROF.description)";
			}

			$dsql .= "   and ".$search_match." against ('\"".JvldSecure::defendSQL($searchq, 1)."\"' in boolean mode) ";

			$this->isSearch = 1;
		}

		$efields = $this->getFilters();
		if (count($efields))
		{
			foreach ($efields as $f => $v)
			{
				if ($v != '')
				{
					$fid = (int)\Joomla\String\StringHelper::str_ireplace('catpage-filterby-', '', $f);
					if (is_array($v))
					{
						// multivalue case
						for ($k=0;$k<count($v);$k++)
						{
							$dsql .= " and L.field" . $fid . " like '%|" . JvldSecure::defendSQL($v[$k]) . "|%' ";
						}
					}
					else
					{
						$dsql .= " and L.field".$fid." = '".JvldSecure::defendSQL($v)."' ";
					}

					$this->isSearch = 1;
				}
			}
		}

		if ($this->getCurrentPageNumber())
		{
			if ($pinned == 0)
			{
				$dsql = str_replace("{PINNED-SQL}", " and L.pinned != 1 ", $dsql);
			}
			else if ($pinned == 1)
			{
				$dsql = str_replace("{PINNED-SQL}", " and L.pinned = 1 ", $dsql);
			}
			else
			{
				$dsql = str_replace("{PINNED-SQL}", "", $dsql);
			}
		}
		else
		{
			$dsql = str_replace("{PINNED-SQL}", "", $dsql);
		}

		if ($select_sql_1 != '')
		{
			$dsql .= " having distance <= ".$radius_filters['radius'];
		}

		$dsql .= $this->getOrderBySQL($pinned);
		return $dsql;
	}

	public function getAllData()
	{
		$query = $this->getListQuery(-1);
		return JvldDb::getRows($query);
	}

	public function getPinnedData()
	{
		$query = $this->getListQuery(1);
		return JvldDb::getRows($query);
	}

	public function getCurrentPageNumber()
	{
		if ($this->getLimit() == 0)
			return 0;

		return ($this->getStart()/$this->getLimit()) + 1;
	}

	public function getStart()
	{
		return $this->getState('list.start');
	}

	public function getLimit()
	{
		return $this->getState('list.limit');
	}

	protected function getStoreId($id = '')
	{
		return parent::getStoreId($id);
	}

	public function getTotalListingCountWoFilters()
	{
		$cid = $this->input->getInt('cid', 0);
		$u_lalevels = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
		$langcheckq = (_JVLD_JOOMLA_MLANG) ?  " and (L.language = '".JFactory::getLanguage()->getTag()."' or L.language = '*')" : "";

		return JvldDb::getCount("	select count(L.id) as CNT 
									from #__jvld_links as L, #__jvld_catlinks as CL, #__categories as JC   
									where 										 
										CL.cid = JC.id and 
										CL.cid = ".(int)$cid." and 
										L.link_status = "._JVLD_LINKSTATUS_ESTABLISHED." and 
										L.access in (".$u_lalevels.") and 
										JC.access in (".$u_lalevels.") and  
										L.id = CL.lid ".$langcheckq
                                );
	}

	public function getSortBy()
	{
		$cfg = JvldCfg::getInstance();

		$filter = 'catpage-sortby';

		// Check if there is a filter from request. If not, take it from session, if not use default val
		$sortby = $this->input->getInt($filter, -1);

		$sortby = ($sortby == -1) ? JvldSession::get($filter, $cfg->get('catpage_sortoption_default'), $this->filter_group) : $sortby;
		JvldSession::set($filter, $sortby, $this->filter_group);

		return $sortby;
	}

	public function getSearchString()
	{
		$filter = 'catpage-search-query';

		$searchq = \Joomla\String\StringHelper::trim($this->input->getString($filter, ''));
		return $searchq;
	}

	public function getFilters()
	{
		$efields = array();

		$srch_fields_cnt = count($this->srch_fields);
		for ($i=0;$i<$srch_fields_cnt;$i++)
		{
			$sfield = new JvldField($this->srch_fields[$i]->id);

			if (($sfield->getTypeSearchMode() == _JVLD_FIELD_SEARCHTYPE_EXACT) && ($sfield->isTypeOptionBased()))
			{
				$filter = 'catpage-filterby-'.$sfield->getID();

				if ($this->input->getInt('rallf'))
				{
					$fval = ($sfield->isTypeMultiValued()) ? array() : '';
				}
				else
				{
					$fval = ($sfield->isTypeMultiValued()) ? $this->input->getVar($filter, array()) : $this->input->getString($filter, '');
				}

				$efields[$filter] = $fval;
			}
		}

		return $efields;
	}

	public function getRadiusFilters()
	{
		$radius = $this->input->getFloat('catpage-radius', '0');
		$saddress = \Joomla\String\StringHelper::trim($this->input->getString('catpage-startfrom', ''));

		return array('radius' => $radius, 'saddress' => $saddress);
	}

	protected function populateState($ordering = null, $direction = null)
	{
		$app = JFactory::getApplication();

		$value = $app->input->get('limitstart', 0, 'int');
		$this->setState('list.start', $value);

		$value = ($value == -1) ? 0 : $app->input->get('limit', intval($app->get('list_limit', 0)), 'uint');
		$this->setState('list.limit', $value);
	}

	public function deleteLink()
	{
		try
		{
			$lid = JFactory::getApplication()->input->getInt('lid', 0);
			$cfg = JvldCfg::getInstance();
			$user = JFactory::getUser();

			// Get link info
			$link = new JvldLinkinfo($lid);
			if (!$link->isValid())
				throw new Exception(JText::_("COM_JVLD_ERR_INVALID_REQ"));

			// Am i the owner?
			if ($link->getInfo('uid') != $user->get('id'))
				throw new Exception(JText::_("COM_JVLD_UNAUTHORIZED"));

			// Update - user is deleting the link from frontend. Dont send to recycle bin... just purge, so that user can add the same url next time
			$link->purge();

			// Inform the website admin
			$em = new JvldEmail('NA_LINK_DELETION');
			$em->setTag('{PARTNER-URL}', JvldPostinit::getLinkUrlorTitleForDisplay($link->getInfo('partner_url'), $link->getInfo('partner_title')));
			$em->setTag('{DELETION-REASON}', JText::sprintf("COM_JVLD_SSMGMT_PP_DEL_USER", $user->get('username')));
			$em->send($cfg->get('site_email'));

			JvldMsg::display(JText::_("COM_JVLD_LINK_DELETE_OK"), _JVLD_NEXT_PAGE_GO_TO_URL, JvldPostinit::getRoute('mylinks'));
			return;
		}
		catch (Exception $ex)
		{
			JvldMsg::display($ex->getMessage(), _JVLD_NEXT_PAGE_GO_BACK);
			return;
		}
	}

	public function getAllListingsForMap()
	{
		$cid = $this->input->getInt('cid', 0);

		$u_lalevels = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
		$langcheckq = (_JVLD_JOOMLA_MLANG) ?  " and (L.language = '".JFactory::getLanguage()->getTag()."' or L.language = '*')" : "";

		$sql = "	select distinct L.id 
					from #__jvld_links as L, #__jvld_catlinks as CL, #__categories as JC   
					where 
						CL.cid = JC.id and 
						CL.cid = ".(int)$cid." and 
						L.link_status = "._JVLD_LINKSTATUS_ESTABLISHED." and 
						L.access in (".$u_lalevels.") and 
						JC.access in (".$u_lalevels.") and  
						L.id = CL.lid ".$langcheckq;

		return JvldDb::getRows($sql);
	}

	public function getIsSearch()
	{
		return $this->isSearch;
	}


	private function getOrderBySQL($pinned)
	{
		if ($pinned)
		{
			$cfg = JvldCfg::getInstance();
			switch ($cfg->get('pinlinks_feorder'))
			{
				case 0:
					return " order by RAND()";
				case 1:
				default:
					return " order by L.pinned_till asc";
			}
		}
		else
		{
			$sortby = $this->getSortBy();
			switch ($sortby)
			{
				case 1:
					return " order by L.link_published_on desc";
				case 6:
					return " order by L.link_published_on asc";
				case 13:
					return " order by L.partner_title desc";
				case 14:
					return " order by L.partner_title asc";
				case 2:
					return " order by L.link_rating desc";
				case 7:
					return " order by L.link_rating asc";
				case 3:
					return " order by L.num_votes desc";
				case 8:
					return " order by L.num_votes asc";
				case 11:
					return " order by L.num_views desc";
				case 12:
					return " order by L.num_views asc";
				case 4:
					return " order by L.itr_numvisits desc";
				case 5:
					return " order by L.out_hits desc";
				case 9:
					return " order by L.itr_numvisits asc";
				case 10:
					return " order by L.out_hits asc";
				case 15:
					return " order by L.atr desc";
				case 16:
					return " order by L.atr asc";
				default:
					return " order by L.link_published_on desc";
			}
		}
	}
}