Sindbad~EG File Manager

Current Path : /var/www/ispania.gr/components/com_rsdirectory/models/
Upload File :
Current File : /var/www/ispania.gr/components/com_rsdirectory/models/radius.php

<?php
/**
 * @package	RSDirectory!
 * @copyright	(c) 2013 - 2021 RSJoomla!
 * @link		https://www.rsjoomla.com
 * @license	GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html
 */

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

/**
 * Radius Search model.
 */
class RSDirectoryModelRadius extends JModelList
{
	/**
     * Method to auto-populate the model state.
     *
     * @param string $ordering An optional ordering field.
	 * @param string $direction An optional direction (asc|desc).
     * 
     * @access protected
     */
    protected function populateState($ordering = null, $direction = null)
    {
		// Get mainframe.
		$app = JFactory::getApplication();
			
		$params = $app->getParams();
		$this->setState('params', $params);
			
		parent::populateState($ordering, $direction);
    }
		
	/**
	 * Method to get an array of data items.
	 *
	 * @return mixed An array of data items on success, false on failure.
	 */
	public function getItems()
	{
		$app = JFactory::getApplication();
		$jinput = $app->input;
		$params = $app->getParams();
		$user = JFactory::getUser();
		$db = JFactory::getDbo();

		// get the Item Id
		$itemId = $params->get('itemid', 0);
			
		// Get the map fields.
		$map_fields_ids = array();
			
		foreach ( $params->get('fields', array()) as $map_field_id )
		{
			// Reset the map fields array if the field is 0 or '' ("All map categories" is selected ), because we will fetch them all later if the array is empty.
			if (!$map_field_id)
			{
				$map_fields_ids = array();
				break;
			}
				
			$map_fields_ids[] = $map_field_id;
		}
			
		$map_fields_ids = RSDirectoryHelper::arrayInt($map_fields_ids);
		$map_fields = RSDirectoryHelper::getFormFields(null, 1, 1, 'map');
		$map_fields_ids_aux = array();
			
		if ($map_fields)
		{
			if ( empty($map_fields_ids) || in_array(0, $map_fields_ids) )
			{
				foreach ($map_fields as $map_field)
				{
					$map_fields_ids_aux[] = $map_field->id;
				}
			}
			else
			{
				foreach ($map_fields as $map_field)
				{
					if ( in_array($map_field->id, $map_fields_ids) )
					{
						$map_fields_ids_aux[] = $map_field->id;
					}
				}
			}
		}
			
		$map_fields_ids = $map_fields_ids_aux;
			
		// Exit the function if there are no map fields.
		if ( empty($map_fields_ids) )
			return;
			
		// Get the featured categories.
		$featured_categories = array();
			
		if ( is_array( $params->get('featured_categories') ) )
		{
			foreach ( $params->get('featured_categories') as $category_id )
			{
				// Reset the featured categories array if the category is 0 or '' ("All categories" is selected ), because we will fetch them all later if the array is empty.
				if (!$category_id)
				{
					$featured_categories = array();
					break;
				}
					
				$featured_categories[] = $category_id;
			}
		}
			
		$featured_categories = RSDirectoryHelper::arrayInt($featured_categories);
			
		if (!empty($featured_categories) && !in_array(0, $featured_categories)) {
			// Let's see if the featured categories we've selected are accessible
			if ($categories_filtered_ids = array_intersect($featured_categories, $this->getCategories())) {
				$categories_ids = array();
				foreach ($categories_filtered_ids as $id) {
					$categories_ids[] = $id; 
					$categories_ids = array_merge($categories_ids, $this->getCategories($id));
				}
			}
		} else {
			$categories_ids = $this->getCategories();
		}
			
		if ( empty($categories_ids) )
		{
			$categories_ids = array(0);
		}
			
		// Get entry author display info (name, username, email..).
		$author = RSDirectoryConfig::getInstance()->get('entry_author', 'name');
			
		$select = array(
			$db->qn('e') . '.*',
			$db->qn('ec') . '.*',
			$db->qn('c.title', 'category_title'),
			$db->qn('c.path', 'category_path'),
			$db->qn('c.params', 'category_params'),
			$db->qn('f.entry_id', 'faved'),
			$db->qn("u.$author", 'author'),
		);
		
		// selecting only the near entries...not all
		if (!is_null($jinput->getString('startpoint'))) {
			
			$coords = explode(',', $jinput->getString('startpoint'));
			$radius_start = array(
				'lat' => $coords[0],
				'lng' => $coords[1]
			);
			
			$unit = $jinput->getString('unit', 'km');
			if ($unit == 'km') {
				$unit_value = '6371';
			}
			else {
				$unit_value = '3959';
			}
			
			foreach ($map_fields_ids as $i=>$map_field_id)
			{
				$select[] = "( {$unit_value} * acos( cos( radians({$radius_start['lat']}) ) * cos( radians( ".$db->qn("f_{$map_field_id}_lat")." ) ) * cos( radians( ".$db->qn("f_{$map_field_id}_lng")." ) - radians({$radius_start['lng']}) ) + sin( radians({$radius_start['lat']}) ) * sin( radians( ".$db->qn("f_{$map_field_id}_lat")." ) ) ) ) AS rs_rad_distance_".$i;
				
			}
		}
			
		$query = $db->getQuery(true)
			   ->select($select)
			   ->from( $db->qn('#__rsdirectory_entries', 'e') )
			   ->innerJoin( $db->qn('#__rsdirectory_entries_custom', 'ec') . ' ON ' . $db->qn('e.id') . ' = ' . $db->qn('ec.entry_id') )
			   ->innerJoin( $db->qn('#__categories', 'c') . ' ON ' . $db->qn('e.category_id') . ' = ' . $db->qn('c.id') )
			   ->innerJoin( $db->qn('#__users', 'u') . ' ON ' . $db->qn('e.user_id') .  ' = ' . $db->qn('u.id') )
			   ->leftJoin( $db->qn('#__rsdirectory_favorites', 'f') . ' ON ' . $db->qn('e.id') . ' = ' . $db->qn('f.entry_id') . ' AND ' . $db->qn('f.user_id') .  ' = ' . $db->q($user->id) )
			   ->where( $db->qn('e.published') . ' = ' . $db->q(1) )
			   ->where( $db->qn('e.published_time') . ' <= ' . $db->q( JFactory::getDate()->toSql() ) )
			   ->where( '(' . $db->qn('e.expiry_time') . ' = ' . $db->q('0000-00-00 00:00:00') . ' OR ' . $db->qn('e.expiry_time') . ' >= ' . $db->q( JFactory::getDate()->toSql() ) . ')' )
			   ->where( $db->qn('u.block') . ' = ' . $db->q(0) )
			   ->where( $db->qn('e.category_id') . ' IN (' . implode( ',', array_unique($categories_ids) ) . ')' )
			   ->group( $db->qn('e.id') );
		
		$where_map_coords = array();
		
		foreach ($map_fields_ids as $map_field_id)
		{
			$where_map_coords[] = '(' . $db->qn("f_{$map_field_id}_lat") . ' != ' . $db->q(0) . ' AND ' . $db->qn("f_{$map_field_id}_lng") . ' != ' . $db->q(0) . ')';
		}
		
		if ($where_map_coords) {
			$query->where('(' . implode(' OR ', $where_map_coords) . ')');
		}
		
		// adding the conditions for radius search
		if (isset($radius_start)) {
			$radius = $jinput->getInt('radius', 0);
			if ($radius > 0) {
				for( $i = 0; $i < count($map_fields_ids); $i++) {
					$query->having( $db->qn('rs_rad_distance_'.$i) .' < '.$radius, 'OR');
				}
			}
		}
			
		$filters = $jinput->get( 'filters', array(), 'array' );
			
		// Get the filter fields for the selected categories.
		$fields = RSDirectoryHelper::getFilterFields($featured_categories);
			
		if ($fields)
		{
			foreach ($fields as $field)
			{
				// Convert the range values to a format understood by the RSDirectoryHelper::buildEntriesFilteringQuery function.
				if ( $field->properties->get('searchable_advanced') == 'range' && !empty($filters[$field->form_field_name]) )
				{
					$values = $filters[$field->form_field_name];
					$new_values = array();

					if (isset($values['sliding-range'])) {
						$values['ranges'] = $values['sliding-range'];
					}
						
					if ( !empty($values['ranges']) )
					{
						$ranges = $values['ranges'];
							
						if ( !is_array($ranges) )
						{
							$ranges = (array)$ranges;
						}
							
						foreach ($ranges as &$range)
						{
							$range = (string) $range;
							$range = trim($range);
								
							// Do a few validations.
							if ( strpos($range, 'lt-') !== false )
							{
								$aux_value = trim( str_replace('lt-', '', $range) );
									
								if ( !is_numeric($aux_value) )
									continue;
									
								$new_values[] = "lt-$aux_value";
							}
							else if ( strpos($range, 'gt-') !== false )
							{
								$aux_value = trim( str_replace('gt-', '', $range) );
									
								if ( !is_numeric($aux_value) )
									continue;
									
								$new_values[] = "gt-$aux_value";
							}
							else
							{
								$arr = explode('-', $range);
									
								foreach ($arr as &$v)
								{
									$v = trim($v);
								}
									
								if ( isset($arr[2]) || !is_numeric($arr[0]) || !is_numeric($arr[1]) )
									continue;
									
								$new_values[] = $arr[0] < $arr[1] ? $arr[0] . '-' . $arr[1] : $arr[1] . '-' . $arr[0];
							}
						}
					}
						
					// Process custom range.
					if ( !empty($values['custom']) && ( isset($values['from']) || isset($values['to']) ) )
					{
						unset($from);
						unset($to);
							
						if ( isset($values['from']) && is_numeric($values['from']) )
						{
							$from = $values['from'];
						}
							
						if ( isset($values['to']) && is_numeric($values['to']) )
						{
							$to = $values['to'];
						}
							
						if ( isset($from, $to) && $from > $to )
						{
							$aux = $from;
							$from = $to;
							$to = $aux;
						}
							
						if ( isset($from) )
						{
							$new_values[] = "from-$from";
						}
							
						if ( isset($to) )
						{
							$new_values[] = "to-$to";
						}
					}
						
					$filters[$field->form_field_name] = $new_values;
				}
			}
		}

		// Initialize the order array.
		$order = array(
			$db->qn('promoted') . ' DESC'
		);
		
		if ($orderBy = $params->get('order_by')) {
			list($column, $direction) = explode('-', $orderBy, 2);
			if ($column == 'random')
			{
				$order[] = 'RAND(' . RSDirectoryHelper::getRandomSeed('radius_') . ')';
			}
			else
			{
				$order[] = $db->qn($column).' '.$direction;
			}
		}
		
		$query->order($order);

		RSDirectoryHelper::buildEntriesFilteringQuery($fields, $filters, $query);

		// Get the entries.
		$db->setQuery($query);
		$entries = $db->loadObjectList();
			
		// Initialize the results array.
		$results = array();
			
		if ($entries)
		{
			$entries = RSDirectoryHelper::getEntriesData($entries);
				
			foreach ($entries as $entry)
			{				
				$coords = array();
					
				foreach ($map_fields_ids as $map_field_id)
				{
					$lat_column_name  = "f_{$map_field_id}_lat";
					$lng_column_name  = "f_{$map_field_id}_lng";
					$addr_column_name = "f_{$map_field_id}_address";
					
					if ((float) $entry->{$lat_column_name} && (float) $entry->{$lng_column_name}) {
						$coords = array(
							'lat' => $entry->{$lat_column_name},
							'lng' => $entry->{$lng_column_name},
						);
						
						$url = RSDirectoryRoute::getEntryURL($entry->id, $entry->title, '', $itemId);
						
						$image = '';
						
						if ( $params->get('show_thumbnails') ) {
							$images_field 		= RSDirectoryHelper::findFormField('images', $entry->form->fields);
							$thumbnails_width 	= $params->get('thumbnails_width');
							$thumbnails_height 	= $params->get('thumbnails_height');
							
							$image .= '<a class="thumbnail" href="' . $url . '">';
							if ( empty($images_field) || empty($images_field->files) )
							{
								$image .= '<i class="rsdir-no-image" style="width: ' . $thumbnails_width . 'px; height: ' . $thumbnails_height . 'px;"></i>';
							}
							else
							{
								$src = RSDirectoryHelper::getImageURL($images_field->files[0]->hash, 'small');
									
								$image .= '<img class="media-object'.(RSDirectoryHelper::isJ4() ? ' img-thumbnail' : '').'" src="' . $src . '" alt="" width="' . $thumbnails_width . '" height="' . $thumbnails_height . '" />';
							}
								
							$image .= '</a>';
						}
						
						$link = '<p><a href="'.$url.'">'.RSDirectoryHelper::escapeHTML($entry->title).'</a></p>';

						$map_icon = array();
						if ($entry->category_params) {
							$cat_params = json_decode($entry->category_params);
							if (!empty($cat_params->map_icon)) {
								$icon = RSDirectoryHelper::getCategoryMapIconObject($cat_params->map_icon);
								if ($icon) {
									$map_icon['icon'] 		= RSDirectoryHelper::getImageURL($icon->hash, 'mapicon');
									$map_icon['w'] 			= RSDirectoryConfig::getInstance()->get('mapicon_thumbnail_width');
									$map_icon['h'] 			= RSDirectoryConfig::getInstance()->get('mapicon_thumbnail_height');
									$map_icon['shadow'] 	= JUri::root() . "media/com_rsdirectory/images/leaflet/marker-shadow.png";
									$map_icon['icon_anchor_x'] 	= ((int) $map_icon['w']) / 2;
									$map_icon['icon_anchor_y'] 	= $map_icon['h'];
									$map_icon['popup_anchor_y'] = -$map_icon['h'];
								}
							}
						}
						
						$results[] = array(
							'id' 		=> $entry->id,
							'image'		=> $image,
							'link' 		=> $link,
							'address'	=> $entry->{$addr_column_name},
							'coords' 	=> $coords,
							'promoted'	=> $entry->promoted,
							'map_icon'	=> empty($map_icon) ? '' : $map_icon
						);
					}
				}
			}
		}
			
		return $results;
	}
	
	public function getEntry() {
		$db 	= JFactory::getDbo();
		$input 	= JFactory::getApplication()->input;
		$user 	= JFactory::getUser();
			
		// Get entry author display info (name, username, email..).
		$author = RSDirectoryConfig::getInstance()->get('entry_author', 'name');
			
		$select = array(
			$db->qn('e') . '.*',
			$db->qn('ec') . '.*',
			$db->qn('c.title', 'category_title'),
			$db->qn('c.path', 'category_path'),
			$db->qn('f.entry_id', 'faved'),
			$db->qn("u.$author", 'author'),
		);
			
		$query = $db->getQuery(true)
			   ->select($select)
			   ->from( $db->qn('#__rsdirectory_entries', 'e') )
			   ->innerJoin( $db->qn('#__rsdirectory_entries_custom', 'ec') . ' ON ' . $db->qn('e.id') . ' = ' . $db->qn('ec.entry_id') )
			   ->innerJoin( $db->qn('#__categories', 'c') . ' ON ' . $db->qn('e.category_id') . ' = ' . $db->qn('c.id') )
			   ->innerJoin( $db->qn('#__users', 'u') . ' ON ' . $db->qn('e.user_id') .  ' = ' . $db->qn('u.id') )
			   ->leftJoin( $db->qn('#__rsdirectory_favorites', 'f') . ' ON ' . $db->qn('e.id') . ' = ' . $db->qn('f.entry_id') . ' AND ' . $db->qn('f.user_id') .  ' = ' . $db->q($user->id) )
			   ->where( $db->qn('e.published') . ' = ' . $db->q(1) )
			   ->where( $db->qn('e.published_time') . ' <= ' . $db->q( JFactory::getDate()->toSql() ) )
			   ->where( '(' . $db->qn('e.expiry_time') . ' = ' . $db->q('0000-00-00 00:00:00') . ' OR ' . $db->qn('e.expiry_time') . ' >= ' . $db->q( JFactory::getDate()->toSql() ) . ')' )
			   ->where( $db->qn('u.block') . ' = ' . $db->q(0) )
			   ->where( $db->qn('e.id') . ' = ' . $db->q( $input->getInt('id') ) );
			   
		$entry = $db->setQuery($query)->loadObject();
		
		$entry = RSDirectoryHelper::getEntryData($entry);
		
		return $entry;
	}
	
	public function getParams() {
		return JFactory::getApplication()->getParams();
	}
	
	protected function getCategories($id = 'root') {
		$options = array(
			'extension' 	=> 'com_rsdirectory',
			'table' 		=> '#__rsdirectory_entries',
			'field' 		=> 'category_id',
			'statefield' 	=> 'published',
			'countItems' 	=> false
		);
			
		// Create a new JCategories object.
		$categories = new JCategories($options);
		
		if ($root = $categories->get($id)) {
			if ($children = $root->getChildren()) {				
				return $this->getIds($children);
			}
		}
		
		return array();
	}
	
	protected function getIds($categories) {
		$ids = array();

		foreach ($categories as $category) {
			$ids[] = $category->id;
			
			if ($category->hasChildren()) {
				$ids = array_merge($ids, $this->getIds($category->getChildren()));
			}
		}
		
		return $ids;
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists