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/mapsearch.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');

/**
 * Map Search model.
 */
class RSDirectoryModelMapSearch extends JModelList {
	/**
	 * 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;
		$user = JFactory::getUser();
		$db = JFactory::getDbo();
		
		// Get mod_rsdirectory_map_search parameters
		$params = $this->getParams();
		
		// Get the map fields.
		$map_fields_ids = array();
		$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 categories.
		$categories = array();
		
		// Get a single category.
		$category = $jinput->getInt('category');
		if ($category)
		{
			$categories[] = $category;
		}
		
		if (!empty($categories)) {
			// Let's see if the categories we've selected are accessible
			if ($categories_filtered_ids = array_intersect($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'));
		
		// Conditions for input search
		if ($q = $jinput->getString('q')) {
			// eg. if we search for 100% we need to escape the %
			$q = $db->escape($q, true);
			// Now, replace spaces with % so we can match
			$q = str_replace(' ', '%', $q);
			// Quote everything
			$input = $db->q('%' . $q . '%');
			
			$query->where('('.$db->qn('e.title') . ' LIKE ' . $input . ' OR ' . $db->qn('e.big_subtitle') . ' LIKE ' . $input . ' OR ' . $db->qn('e.small_subtitle') . ' LIKE ' . $input . ' OR ' . $db->qn('e.description') . ' LIKE ' . $input.')');
		}
		
		// Conditions for price range search
		if ($jinput->getFloat('min_price')) {
			$query->having($db->qn('e.price') . ' >= ' . $jinput->getFloat('min_price'));
		}
		if ($jinput->getFloat('max_price')) {
			$query->having($db->qn('e.price') . ' <= ' . $jinput->getFloat('max_price'));
		}
		
		$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) . ')');
		}
		
		// 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');
				}
			}
		}
		
		// 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);

		// 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);
						
						$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 class="rsdir-map-search-item" 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() {
		$module		= JModuleHelper::getModule('mod_rsdirectory_map_search');
		// Get module parameters
		$params = new JRegistry;
		$params->loadString($module->params);
		
		return $params;
	}
	
	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