<!-- Script PHP/MYSQL of management of classifieds ads developed by Script PAG Script PAG all rights reserved. Use under license. http://www.script-pag.com -->
<?php

if($listing_search == 1 || $listing_search >= 3)
{
	foreach($search_array as $row)
	{
		if($listing_search == 4)
		{
			if(preg_match("#Script_PAG#", $row) == true)
			{
				$row = str_replace('Script_PAG', '', $row);
				$row = (int) $row;
			}
			else $row = 0;
				
			if(empty($row)) continue;
			
			$row = get_ad($row, 0);
			if(empty($row)) continue;
		}
		
		$id_ad = (int) $row['id_ad'];
		$id_reg = (int) $row['id_reg'];
		$id_county = (int) $row['id_county'];
		$id_cat = (int) $row['id_cat'];
		$disc = get_disc($id_cat);
		$cat_name = name_cat($id_cat);
		$reg_name = name_region($id_reg);
		$county_name = name_county($id_county);
		$title = stripslashes(htmlspecialchars($row['title']));
		$text = stripslashes(htmlspecialchars($row['text']));
		$postcode = htmlspecialchars($row['postcode'], ENT_QUOTES);
		$city = htmlspecialchars($row['city'], ENT_QUOTES);
		$status = (int) $row['status'];
		$type =(int) $row['type'];
		$date = (int) $row['date'];
		$price = (float) $row['price'];
		$urgent = (int) $row['urgent'];
		$framed = (int) $row['framed'];
		$picture_name = htmlspecialchars($row['picture_name'], ENT_QUOTES);
		$video = htmlspecialchars($row['video'], ENT_QUOTES);
		$nb_pictures = htmlspecialchars($row['pictures_num'], ENT_QUOTES);
		$nb_visit = (int) $row['visit_num'];
		$comment_total = (int) $row['comment_total'];
		$comment_average = (double) $row['comment_average'];	
		$display_comments = get_comments($row['id_cat']);	
		$name_picture = ($nb_pictures > 1) ? $language['picture_ad2'] : $language['picture_ad1'];
		
		//Url rewriting
		
		$url_rewrite = '';
		$url_rewrite .= (!empty($reg_name)) ? $reg_name .' ' : '';
		$url_rewrite .= (!empty($county_name)) ? $county_name .' ' : '';
		$url_rewrite .= (!empty($cat_name)) ? $cat_name .' ' : '';
		
		$url_display = rewriting($url_rewrite . $title);
		
		//Size
		
		$cat_name = resize_text($cat_name, 10);
		$title = resize_text($title, 30);
		$city = resize_text($city, 10);
		$text = resize_text($text, 200);
			
		//Date
			
		$day = get_date($date, $language, $_SESSION['code_lang']);
		$hour = get_hour($date, $_SESSION['code_lang']);
		
		$framed_class = !empty($framed) ? 'background-framed' : '';
		$selected = !empty($ad_selected[$id_ad]) ? 'selected' : '';
		
		?>
			
		<?php if($disc == 1 && !isset($_SESSION['disc']) && $listing_search != 3) : ?>
		<div class="background-ads-listing-disc flex-container">
			<div>
				<p><?php echo $language['text_disc']; ?></p>
			
				<form method="post" action="">
					<input type="hidden" name="disc" value="1" />
					<input type="submit" value="<?php echo $language['bt_confirm_major']; ?>" />
				</form>
			</div>
		</div>
		<?php else : ?>
		<div class="background-ads-listing-container">
			
			<?php if($listing_search == 1 || $listing_search == 5) : ?> 
			<a href="<?php echo URL_DISPLAY; ?>/selection.php" data-heart="1" data-id="<?php echo  $id_ad; ?>" class="icon-heart <?php echo $selected; ?>"></a>
			<?php endif; ?>
			
			<?php if($listing_search == 3) : ?>
			<span class="check-all-ads"><input type="checkbox" id="<?php echo $id_ad; ?>" name="check_all[]" value="<?php echo $id_ad; ?>" /><label for="<?php echo $id_ad; ?>"></label></span>
			<?php endif; ?>
			
			<?php if($listing_search == 4) : ?>
			<a  href="<?php echo URL_DISPLAY; ?>/selection.php" data-id="<?php echo  $id_ad; ?>" class="icon-deleted-ad-selected"></a>
			<?php endif; ?>
			
			<a href="<?php echo URL_DISPLAY; ?>/<?php echo $language['router_slug_ad'] .'/'. $url_display .'-'. $id_ad; ?>" class="background-ads-listing <?php echo $framed_class; ?> flex-container" title="<?php echo $text; ?>">
				<div class="bloc-listing-first">
					<p class="title-listing"><?php echo $title; ?></p>
					
					<?php $localisation_name = !empty($county_name) ? $county_name : $reg_name; ?>
					<div class="flex-container">
						<p class="localisation-listing">
							<?php echo $localisation_name; ?><br />
							<?php echo $postcode .' '. $city; ?>
							
							<?php if($price != 0) : ?>
							<br /><span class="price-listing"><?php pool::execute('displayPrice', $id_ad, $price); ?></span>
							<?php endif; ?>
							
							<?php if($urgent == 1) : ?>
					        <br /><span class="logo-urgent"><?php echo $language['logo_urgent']; ?></span>
					        <?php endif; ?>
						</p>
						
						<div class="category-listing">
							<p>
								<?php echo $language['type_ad'. $type] .' / '. $cat_name; ?>
								
								<?php if($status == 2) : ?>
								<strong><?php echo ' '. $language['pro_ad']; ?></strong>
								<?php endif; ?>
								
								<?php if(!empty($video)) : ?>
								<br /><span class="video-listing"><?php echo ' '. $language['video_ad']; ?></span>
								<?php endif; ?>
							</p>
							
							<?php if($display_comments == 1) : ?>
							<p>
								<span class="rating-listing">
									<span class="comment-total">(<?php echo $comment_total; ?>)</span>
									<span <?php if(round($comment_average) == 5) echo 'class="actif"'; ?>></span>
									<span <?php if(round($comment_average) >= 4) echo 'class="actif"'; ?>></span>
									<span <?php if(round($comment_average) >= 3) echo 'class="actif"'; ?>></span>
									<span <?php if(round($comment_average) >= 2) echo 'class="actif"'; ?>></span>
									<span <?php if(round($comment_average) >= 1) echo 'class="actif"'; ?>></span>			
								</span>
							</p>
							<?php endif; ?>
						</div>
						
					</div>
				</div>
				<div class="bloc-listing-picture">
					<?php
						
					if(!empty($picture_name) && preg_match('#^https?://#', $picture_name) == true)
					{
						echo '<img src="'. $picture_name .'" alt="" />';
					}
					elseif(!empty($picture_name) && file_exists('upload/thumbnails/'. $picture_name))
					{ 
						echo '<img src="'. URL .'/upload/thumbnails/'. $picture_name .'" alt="" />';
					}
					else echo '<img src="'. URL .'/template/images/no_photo2.png" alt="" />';
					
					?>
				</div>
				<div class="bloc-listing-last">
					<p>
					<?php
						
						if($listing_search != 3)
						{
							if($day == $language['today_ad'] || $day == $language['yesterday_ad']) 
							echo $day .'<br />'. $language['hour_ad'] .' '. $hour;
							
							else echo $language['date_ad'] .' '. $day .'<br />'. $language['hour_ad'] .' '. $hour; 
							
							echo '<br /><br /><strong>'. $nb_pictures .'</strong> '. $name_picture;
						}
						else echo '<span class="nb-ads">'. $nb_visit .' '. $language['views'] .'</span>';
					?>
					</p>
				</div>
				
			</a>
			
		</div>
		<?php endif; ?>
		
		<?php
		
		//Advertisement
		
		if($count_adv == $param_gen['nb_ads_adv'] && $listing_search == 1)
		{
			$count_adv = 0;
			$display_adv = 0;
			
			foreach($cache_advertisement as $v)
			{
				if(($v['id_reg'] == 0 || (isset($search_data['reg']) &&  $v['id_reg'] == $search_data['reg'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 7)
				$display_adv = '1';
				
				elseif(($v['id_county'] == 0 || (isset($search_data['county']) && $v['id_county'] == $search_data['county'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 7)
				$display_adv = '1';
				
				elseif(($v['id_cat'] == 0 || (isset($search_data['cat']) && $v['id_cat'] == $search_data['cat'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 7)
				$display_adv = '1';
			}
			 
			if($display_adv == 1)
			{
				$_SESSION['adv'] = 1;
				
				foreach($cache_advertisement as $row)
				{	
					$id_reg = (int) $row['id_reg'];
					$id_county = (int) $row['id_county'];
					$id_cat = (int) $row['id_cat'];
					$code_lang = stripslashes(htmlspecialchars($row['code_lang'], ENT_QUOTES));
					$name = stripslashes(htmlspecialchars($row['name'], ENT_QUOTES));
					$picture = stripslashes(htmlspecialchars($row['picture'], ENT_QUOTES));
					$url = stripslashes(htmlspecialchars($row['url'], ENT_QUOTES));
					$text = stripslashes(htmlspecialchars($row['text'], ENT_QUOTES));
					$script = $row['script'];
					$submit_cookies = (int) $row['submit_cookies'];
					$adv = (int) $row['adv'];
					
					if((($id_reg == 0 || (isset($search_data['reg']) && $id_reg == $search_data['reg'])) && ($id_county == 0 || (isset($search_data['county']) && $id_county == $search_data['county'])) && ($id_cat == 0 || (isset($search_data['cat']) && $id_cat == $search_data['cat'])) && ($code_lang == '' || $code_lang == $_SESSION['code_lang'])) && $adv == 7)
					{
						if(empty($script) && $_SESSION['adv'] == $_SESSION['count_adv'])
						echo '<div class="bloc-advertisement-area listing text-center"><a href="'. $url .'" target="_blank"><img src="'. URL .'/upload/advertisement/'. $picture .'" alt="'. $name .'" /></a></div>';
					 
						if((!empty($script) && $submit_cookies == 0) || (!empty($script) && $submit_cookies == 1 && is_allowed_cookie('publicity')) && $_SESSION['adv'] == $_SESSION['count_adv'])
						echo '<div class="bloc-advertisement-area listing text-center">'. $script .'</div>';
						
						$_SESSION['adv']++;
					}
				}	
				
				$_SESSION['count_adv']++;	
				
				if($_SESSION['count_adv'] >= $_SESSION['adv']) 
				$_SESSION['count_adv'] = 1;
			}

		}

		$count_adv++;
	}
	
	if($listing_search == 3)
	echo '</form>'; 
	
	if($limit_page > 0)
	require_once __DIR__.'/tpl_listing_pagination.php';

}
elseif($listing_search == 2)
{
	foreach($search_array as $row)
	{
		$id_shop = (int) $row['id_shop'];
		$id_reg = (int) $row['id_reg'];
		$id_county = (int) $row['id_county'];
		$id_cat = (int) $row['id_cat'];
		$title = stripslashes(htmlspecialchars($row['title']));
		$postcode = stripslashes(htmlspecialchars($row['postcode'], ENT_QUOTES));
		$city = stripslashes(htmlspecialchars($row['city'], ENT_QUOTES));
		$name = stripslashes(htmlspecialchars($row['name'], ENT_QUOTES));
		$logo_name = stripslashes(htmlspecialchars($row['logo_name'], ENT_QUOTES));
		$id_acc = (int) $row['id_acc'];
		$framed = (int) $row['framed'];
		$nb_ads = (int) $row['ads_num'];
		$text_nb_ads = ($nb_ads <= 1) ? $language['nb_ad_shop'] : $language['nb_ad_shop_more'] ;
		$comment_total = (int) $row['comment_total'];
		$comment_average = (double) $row['comment_average'];
		$cat_name = name_cat($id_cat);
		$reg_name = name_region($id_reg);
		$county_name = name_county($id_county);
	
		//Url rewriting
		
		$url_rewrite = '';
		$url_rewrite .= (!empty($reg_name)) ? $reg_name .' ' : '';
		$url_rewrite .= (!empty($county_name)) ? $county_name .' ' : '';
		$url_rewrite .= (!empty($cat_name)) ? $cat_name .' ' : '';
	
		$url_display = rewriting($url_rewrite . $title);
	
		//Size
		$cat_name = resize_text($cat_name, 20);
		$title = resize_text($title, 42);
		$city = resize_text($city, 20);
		
		$framed_class = !empty($framed) ? 'background-framed' : '';
	
		?>
		
		<a href="<?php echo URL_DISPLAY; ?>/<?php echo $language['router_slug_shop'] .'/'. $url_display .'-'. $id_shop; ?>" class="background-shops-listing <?php echo $framed_class; ?> flex-container" title="<?php echo $text; ?>">
		
			<div class="bloc-listing-picture shops">
				<?php
					
				if(!empty($logo_name) && file_exists('upload/logos/'. $logo_name))
				{ 
					echo '<img src="'. URL .'/upload/logos/'. $logo_name .'" alt="" />';
				}
				else echo '<img src="'. URL .'/template/images/no_photo2.png" alt="" />';
				
				?>
			</div>
			
			<div class="bloc-listing-first shops">
				<p>
					<span class="title-listing"><?php echo $title; ?></span><br /><br />
					<span class="video-listing"><?php echo $nb_ads .' '. $text_nb_ads; ?></span>
				</p>
				
				<?php if($param_gen['active_comment'] == 1) : ?>
				<p>
					<span class="rating-listing">
						<span class="comment-total">(<?php echo $comment_total; ?>)</span>
						<span <?php if(round($comment_average) == 5) echo 'class="actif"'; ?>></span>
						<span <?php if(round($comment_average) >= 4) echo 'class="actif"'; ?>></span>
						<span <?php if(round($comment_average) >= 3) echo 'class="actif"'; ?>></span>
						<span <?php if(round($comment_average) >= 2) echo 'class="actif"'; ?>></span>
						<span <?php if(round($comment_average) >= 1) echo 'class="actif"'; ?>></span>				
					</span>
				</p>
				<?php endif; ?>
			</div>
			
			<div class="bloc-listing-last shops">
				<p>
					<?php if(empty($county_name)) : ?>
					<?php echo $reg_name; ?><br />
					<?php else : ?>
					<?php echo $county_name; ?><br />
					<?php endif; ?>
					
					<?php echo $postcode .' '. $city; ?><br />
					<span class="uppercase"><?php echo $cat_name; ?></span>
				</p>
			</div>
			
		</a>
		
		<?php
		
		//Advertisement
		
		if($count_adv == $param_gen['nb_shops_adv'])
		{
			$count_adv = 0;
			$display_adv = 0;
			
			foreach($cache_advertisement as $v)
			{
				if(($v['id_reg'] == 0 || (isset($search_data['reg']) && $v['id_reg'] == $search_data['reg'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 10)
				$display_adv = '1';
				
				elseif(($v['id_county'] == 0 || (isset($search_data['county']) && $v['id_county'] == $search_data['county'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 10)
				$display_adv = '1';
				
				elseif(($v['id_cat'] == 0 || (isset($search_data['cat']) && $v['id_cat'] == $search_data['cat'])) && ($v['code_lang'] == '' || $v['code_lang'] == $_SESSION['code_lang']) && $v['adv'] == 10)
				$display_adv = '1';
			}
			 
			if($display_adv == 1)
			{
				$_SESSION['adv'] = 1;
				
				foreach($cache_advertisement as $row)
				{	
					$id_reg = (int) $row['id_reg'];
					$id_county = (int) $row['id_county'];
					$id_cat = (int) $row['id_cat'];
					$code_lang = stripslashes(htmlspecialchars($row['code_lang'], ENT_QUOTES));
					$name = stripslashes(htmlspecialchars($row['name'], ENT_QUOTES));
					$picture = stripslashes(htmlspecialchars($row['picture'], ENT_QUOTES));
					$url = stripslashes(htmlspecialchars($row['url'], ENT_QUOTES));
					$text = stripslashes(htmlspecialchars($row['text'], ENT_QUOTES));
					$script = $row['script'];
					$submit_cookies = (int) $row['submit_cookies'];
					$adv = (int) $row['adv'];
					
					if((($id_reg == 0 || (isset($search_data['reg']) && $id_reg == $search_data['reg'])) && ($id_county == 0 || (isset($search_data['county']) && $id_county == $search_data['county'])) && ($id_cat == 0 || (isset($search_data['cat']) && $id_cat == $search_data['cat'])) && ($code_lang == '' || $code_lang == $_SESSION['code_lang'])) && $adv == 10)
					{
						if(empty($script) && $_SESSION['adv'] == $_SESSION['count_adv'])
						echo '<div class="bloc-advertisement-area listing text-center"><a href="'. $url .'" target="_blank"><img src="'. URL .'/upload/advertisement/'. $picture .'" alt="'. $name .'" /></a></div>';
						
						if((!empty($script) && $submit_cookies == 0) || (!empty($script) && $submit_cookies == 1 && is_allowed_cookie('publicity')) && $_SESSION['adv'] == $_SESSION['count_adv'])
						echo '<div class="bloc-advertisement-area listing text-center">'. $script .'</div>';
						
						$_SESSION['adv']++;
					}
				}	
				
				$_SESSION['count_adv']++;	
				
				if($_SESSION['count_adv'] >= $_SESSION['adv']) 
				$_SESSION['count_adv'] = 1;
			}
		}
		$count_adv++;
	}
	
	if($limit_page > 0)
	require_once __DIR__.'/tpl_listing_pagination.php';
}