<?php
$server_host = $_SERVER['HTTP_HOST'];
if(str_replace("www.", "", $server_host) != $real_domainname) {
	$server_host .= "/$real_addonname";
}


require_once(dirname(__FILE__) . "/functions/yp_list_entries.php");

$microdata_domain = str_replace("www.", "", $ultra_domainname);
$microdata_host = $_SERVER['HTTP_HOST'];
if(str_replace('www.', '', $microdata_host) != str_replace('www.', '', $ultra_domainname)) $microdata_host .= "/" . $ultra_addonname;
$have_microdata = (isset($microdata_domains) && in_array($microdata_domain, $microdata_domains));


/* furniture - noindex/follow*/
/*if(str_replace('www.', '', $ultra_domainname) == "cyprusfurniture.com") {
	$meta_robots = "noindex, follow";
}*/

if(isset($yp_categ) && $yp_categ != "") {
	$yp_limit = 50;

	if((in_array(str_replace('www.', '', $ultra_domainname), $special_domains))) {
		$yp_limit = 9999999;
	}

	$yp_categ = trim($yp_categ);
	$is_canonycal = true;
	if(strpos($yp_categ, ",") !== false) {
		$tmp = explode(",", $yp_categ);
		$categ_furl = trim($tmp[0]);
		$yp_page = intval($tmp[1]);

		$canonical_link = $protocol . $server_host . "/directory/$categ_furl";
		if($yp_page > 1) {
			$canonical_link = $protocol . $server_host . "/directory/$categ_furl,$yp_page";
		}
		
	}
	else {
		$is_canonycal = true;
		$categ_furl = $yp_categ;
		$yp_page = 1;
		$canonical_link = $protocol . $server_host . "/directory/$categ_furl";
	}


    $breadcrumbs [] = [
        "title" => "Directory",
        "link" => $protocol . $server_host . "/directory"
    ];


	$is_alias = false;
	if(isset($yp_categ_alias)) {
		$is_alias = true;
	}
	
	if(isset($categ_furl)) {
		if(!$is_alias) {
			$q = "select * from yp_categories where cy_friendly_name_en='$categ_furl';";
		}
		else {
			$q = "select l.title as cy_admin_title_en, l.uid, l.district, l.location, c.cy_friendly_name_en as canonical from yp_categories_links l, yp_categories c where l.uid=c.uid and l.friendly_name='$categ_furl';";
		}
		$r = @mysql_query($q);
		if($r && mysql_num_rows($r) > 0) {
			$row = mysql_fetch_assoc($r);
			$categ_uid = $row['uid'];
			$categ_title = stripslashes($row['cy_admin_title_en']);
			$_categ_title = $categ_title;
			if(isset($canonical_link) && $canonical_link != "") {
				$webpage_title_en = $_categ_title . ($yp_page > 1 ? ' | Page ' . $yp_page : '');
			}
			else {
				$webpage_title_en = $_categ_title;
			}
			
			$meta['description'] = 'Directory of ' . $_categ_title;
			
			if(!$is_canonycal && $yp_page > 0) {
				$meta['description'] .= ' - Page ' . $yp_page;
			}
			
			$yp_canonical = '';
			//if(isset($row['canonical'])) {
			//	$yp_canonical = trim($row['canonical']);
			//	$canonical_link = "http://" . $server_host . "/directory/$yp_canonical";
			//}
			
			$yp_district = isset($row['district']) ? trim($row['district']) : '';
			$yp_location = isset($row['location']) ? trim($row['location']) : '';

            $breadcrumbs [] = [
                "title" => $_categ_title,
                "link" => $canonical_link
            ];


			?>
			<article class="article bl-middle-directory bl-middle cyprus">
				<header>
                    <h1><?php echo $_categ_title . (($yp_page > 1) ? ' | Page ' . $yp_page : '');?></h1>
                    <!--[/SocialShareButtons/]-->
                </header>
				<div class="article-body cyprus">

					<nav class="box">
					<?php
					$yp_data_search = list_entries($categ_uid, "$yp_district/$yp_location", $yp_page, $yp_limit);
                    //echo '<pre>', var_export($yp_data_search, true), '</pre>';
					$yp_total_records = $yp_data_search['total_records'];
					$yp_data = $yp_data_search['data'];
					//echo '<pre>',print_r($yp_data), '</pre>';
					if($yp_data && count($yp_data) > 0) {
						foreach ($yp_data as $b_uid=>$b_data) {
							$locations = $b_data['locations'];
							$num_locs = count($locations);
							?>
							<div class="row bl-list-row no-margin" <?php echo ($have_microdata) ? 'itemscope itemtype="' . $microdata_types[$microdata_domain]['adverts']['type'] . '"' : '';?>>
								<?php
								$td_locs = array(
									"district"	=> array(),
									"tel"		=> array(),
									"fax"		=> array()
								);
								$cnt = 0;
								foreach ($locations as $i=>$loc) {
									$have_loc_data = false;
								
									$td_locs_tmp = array(
										"district"	=> $loc['district'],
										"tel"		=> "&nbsp;",
										"fax"		=> "&nbsp;"
									);
									
									$tels = array();
									if($loc['tel1'] != "") $tels [] = $loc['tel1'];
									if($loc['tel2'] != "") $tels [] = $loc['tel2'];
									
									$faxes = array();
									if($loc['fax1'] != "") $faxes [] = $loc['fax1'];
									if($loc['fax2'] != "") $faxes [] = $loc['fax2'];
									
									
									if(count($tels) > 0) {
										$have_loc_data = true;
										$td_locs_tmp['tel'] = implode(", ", $tels) . "";
									}
									
									if(count($faxes) > 0) {
										$have_loc_data = true;
										$td_locs_tmp['fax'] =  implode(", ", $faxes). "";
									}
									
									if($have_loc_data) {
										$td_locs ['district'][] = isset($loc['show_location']) ? $loc['location'] : $td_locs_tmp['district'];
										$td_locs ['tel'][] = $td_locs_tmp['tel'];
										$td_locs ['fax'][] = $td_locs_tmp['fax'];
									}
									
									
								}
								
								?>
								<div class="col-md-4 col-sm-3">
									<?php
									$microdata_url =  ($have_microdata) ? 'itemprop="url"' : '';
									$microdata_name =  ($have_microdata) ? 'itemprop="name"' : '';
									$b_data['name'] = str_replace(".", ". ", $b_data['name']);
									echo '<a href="'. $protocol.$server_host.'/directory/name/'.$b_data['friendly_name'].'" class="yp_link" ',$microdata_url,'><span ',$microdata_name,'>', ucwords(strtolower($b_data['name'])), '</span></a>';
									?>
								</div>
                                <div class="col-md-6 col-sm-7">
								<div class="row no-margin">
										<?php
										$cnt = 1;
										for($i=0; $i<count($td_locs['district']); $i++) {
											?>
											<div class="row no-margin" <?php echo ($have_microdata) ? 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"' : '';?>>
												<?php
												foreach ($td_locs as $td_type=>$td_array) {
													$yp_val = trim($td_array[$i]);
													$yp_tmp = "";
                                                    $td_district = "";
                                                    $td_tel = "";
                                                    $td_fax = "";
                                                    $col_class = "col-sm-3 col-xs-3";

													if($td_type == "district") {
                                                        $col_class = "col-sm-4 col-xs-4";
														if($have_microdata) {
                                                            $td_district .= '<meta itemprop="addressCountry" content="CY">';
                                                            $td_district .= '<span itemprop="addressRegion">';
														}
														//$yp_val = $yp_val;
                                                        $td_district .=  "<b>$yp_val</b>";
														if($have_microdata) {
                                                            $td_district .= '</span>';
														}
                                                        $yp_tmp = $td_district;
													}
													
													if($td_type == "tel" && $yp_val != "&nbsp;") {
                                                        $col_class = "col-sm-3 col-xs-3";
                                                        $td_tel = "<b><i class='fa fa-phone'></i></b> ";
														if($have_microdata) {
                                                            $td_tel .= '<span itemprop="telephone">' . $yp_val . '</span>';
														}
														else {
                                                            $td_tel .= $yp_val;
														}
                                                        $yp_tmp = $td_tel;
													}
													
													if($td_type == "fax" && $yp_val !="&nbsp;") {
                                                        $col_class = "col-sm-3 col-xs-3";
                                                        $td_fax = "<b><i class='fa fa-fax'></i></b> ";
														if($have_microdata) {
                                                            $td_fax .= '<span itemprop="faxNumber">' . $yp_val . '</span>';
														}
														else {
                                                            $td_fax .= $yp_val;
														}
                                                        $yp_tmp = $td_fax;
													}
													?>
                                                    <div class="<?=$col_class;?> no-padding"><?php echo $yp_tmp;?></div>
													<?php
												}
												?>
											</div>
											<?php
										}
										?>
								</div>
                                </div>
									
							</div>
							<?php
							
						}
					}

                    echo '</div>';
					if($yp_total_records > $yp_limit) {
						$nrpages = intval(ceil($yp_total_records/$yp_limit));
						
						if($nrpages == 1) {
							$webpage_title_en = $_categ_title;
						}
						
						$yp_from = ($yp_page == 1) ? 0 : $yp_page * $yp_limit;
						$page = $yp_limit;
						
						if($yp_page == 1 && $nrpages > 1) {
							$rel_next_page = $protocol . $server_host . '/directory/' . $categ_furl . ',2';
						}
						if($yp_page > 1 && $yp_page <= $nrpages) {
							if($yp_page == 2) {
								$rel_prev_page = $protocol . $server_host . '/directory/' . $categ_furl;
							}
							else {
								$rel_prev_page = $protocol . $server_host . '/directory/' . $categ_furl . ',' . ($yp_page - 1);
							}
							
							if($yp_page <= $nrpages - 1) {
								$rel_next_page = $protocol . $server_host . '/directory/' . $categ_furl . ',' . ($yp_page + 1);
							}
							
						}
						
						if($yp_page > $nrpages || $yp_page <= 0) {
							$send404 = true;
							$display_bl_content = false;
						}
						
						?>
                        <div class="search-results-pagination text-left">
                            <?php
                            $pages_to_show = 5;
                            $prev_text = "&laquo;";
                            $next_text = "&raquo;";

                            $P = $yp_page;

                            $plinks = array(
                                "prev" => array(),
                                "left" => array(),
                                "middle" => array(),
                                "right"	=> array(),
                                "next"	=> array()
                            );
                            //echo '<pre>', print_r($plinks), '</pre>';
                            $uri = $_SERVER['REQUEST_URI'];
                            $comma_pos = strpos($uri, ",");
                            if($comma_pos !== false) {
                                $uri = mb_substr($uri, 0, $comma_pos, 'UTF-8');
                            }

                            $link_url = $protocol . $_SERVER['HTTP_HOST'] . $uri;

                            if($P + 2 > $pages_to_show) {
                                for($i=2; $i>=1; $i--) {
                                    $cur_p = $P - $i;
                                    $cur_from = ($cur_p-1) * $page;
                                    $plink['left'][] = array(
                                        "href" 	=> $link_url . ",$cur_p",
                                        "rel"	=> $cur_from,
                                        "text"	=> $cur_p,
                                        "page"	=> $cur_p
                                    );
                                }

                                $cur_p = $P;
                                $cur_from = ($cur_p-1) * $page;
                                $plink['middle'][] = array(
                                        "href" 	=> $link_url . ",$cur_p",
                                        "rel"	=> $cur_from,
                                        "text"	=> $cur_p,
                                        "page"	=> $cur_p
                                    );
                                if($nrpages > $P) {

                                    for($i=1; $i<=($P+2 <= $nrpages ? 2 : $nrpages-$P); $i++) {
                                        $cur_p = $P + $i;
                                        $cur_from = ($cur_p-1) * $page;
                                        $plink['right'][] = array(
                                            "href" 	=> $link_url . ",$cur_p",
                                            "rel"	=> $cur_from,
                                            "text"	=> $cur_p,
                                            "page"	=> $cur_p
                                        );
                                    }
                                }
                            }
                            else {
                                for($i=1; $i<=($nrpages < 5 ? $nrpages : 5); $i++) {
                                    $cur_p = $i;
                                    $cur_from = 0;
                                    $plink['left'][] = array(
                                        "href" 	=> $link_url . ",$cur_p",
                                        "rel"	=> $cur_from,
                                        "text"	=> $cur_p,
                                        "page"	=> $cur_p
                                    );
                                }
                            }

                            if($P>1) {
                                $cur_p = $P - 1;
                                $cur_from = ($cur_p-1) * $page;
                                $plink['prev'][] = array(
                                    "href" 	=> $link_url . ",$cur_p",
                                    "rel"	=> $cur_from,
                                    "text"	=> '&laquo; ' . 'Prev',
                                    "page"	=> $cur_p
                                );
                            }

                            if($nrpages > $P) {
                                $cur_p = $P + 1;
                                $cur_from = ($cur_p-1) * $page;
                                $plink['next'][] = array(
                                    "href" 	=> $link_url . ",$cur_p",
                                    "rel"	=> $cur_from,
                                    "text"	=> 'Next' . ' &raquo;',
                                    "page"	=> $cur_p
                                );
                            }

                            ob_start();
                            ?>
                            <nav>
                                <ul class="pagination">
								<?php
									//echo '<textarea cols=80 rows=10><pre>', print_r($plink),'</pre></textarea>';
									//echo '<div id="ui-paginator" style="padding: 0px 3px;">';
									if(isset($plink['prev']) && count($plink['prev']) > 0) {
										$p_class = "pag_num lnext";
										$ld = $plink['prev'][0];
										echo '<li><a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a></li>';
									}
									else {
										//echo '<li class="disabled"><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo; Prev</span></li>';
									}
									
									$margins = array(
										"left" => array(),
										"right" => array(),
									);
									if($P>=5) {
										
										for($i=1; $i<=1; $i++) {
											$cur_p = $i;
											$cur_from = ($cur_p-1) * $page;
											$margins['left'][] = array(
												"href" 	=> $link_url . "," . $cur_p,
												"rel"	=> $cur_from,
												"text"	=> $cur_p,
												"page"	=> $cur_p
											);
										}
										
										foreach ($margins['left'] as $i=>$ld) {
											$p_class = "pag_num";
											echo '<li><a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a></li>';
										}
										echo '<li class="disabled"><span> ... </span> </li>';
									}
									
									foreach ($plink as $pos_label => $pos) {
										foreach ($pos as $i=>$ld) {
											$p_class = "pag_num";
											if($pos_label != "prev" && $pos_label != "next") {
												if($ld['page'] == $P) {
													echo '<li class="active"><span>' . $ld['text'] . '</span></li>';
												}
												else {
													echo '<li><a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a></li>';
												}
											}
										}
									}
									
									if($P+3 < $nrpages) {
										
										for($i=1; $i<=1; $i++) {
											$cur_p = $nrpages;
											$cur_from = ($cur_p-1) * $page;
											$margins['right'][] = array(
												"href" 	=> $link_url . "," . $cur_p,
												"rel"	=> $cur_from,
												"text"	=> $cur_p,
												"page"	=> $cur_p
											);
										}
										echo '<li class="disabled"><span> ... </span> </li>';
										foreach ($margins['right'] as $i=>$ld) {
											$p_class = "pag_num";
											echo '<li><a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a></li>';
										}
										
									}
									
									if(isset($plink['next']) && count($plink['next']) > 0) {
										$p_class = "pag_num lnext";
										$ld = $plink['next'][0];
										echo '<li><a href="' . $ld['href'] . '" rel="'.$ld['rel'].'" class="'.$p_class.'">' . $ld['text'] . '</a></li>';
									}
									else {
										//echo '<li class="disabled"><a href="#"  aria-label="Next"><span aria-hidden="true">Next &raquo;</span></li>';
									}
									?>
                                </ul>
                            </nav>
                            <?php
								
								$pagination_code = ob_get_contents();
								ob_end_clean();
								echo $pagination_code;
					
								?>
							</div>
						<?php
					}
					
					?>


			</article>
			<?php
			//echo '<pre>', print_r($_SERVER), '</pre>';
		}
	}
	
}
else {
    $canonical_link = $protocol . $server_host . "/directory";
	$dir_module_id = $req_modules['yp_middle']['module_id'];
	$q = "select * from left_content where catdom_id='$domain_id' and module_id='$dir_module_id' and type='module' and location='middle';";
	if(isset($_REQUEST['debug'])) echo "<br/>$q";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r) > 0) {
		$row = mysql_fetch_assoc($r);
		$dir_params = trim($row['module_params']) != "" ? unserialize(base64_decode($row['module_params'])) : false;
		//print_r($dir_params);
        //echo '<pre>', var_export($dir_params, true), '</pre>';
		if($dir_params && isset($dir_params['module_yp_categs'])) {
			$module_yp_categs =  $dir_params['module_yp_categs'];
			$uids = array();
			foreach (array_keys($module_yp_categs) as $categ_uid) {
				$uids [] = "'" . $categ_uid . "'";
			}
			$uids_str = implode(",", $uids);
			
			$locations_links = array();

			$yp_title = $webpage_title_en . " Directory";
			if(isset($dir_params['module_yp_title']) && trim($dir_params['module_yp_title']) != "") {
				$yp_title = trim(stripslashes($dir_params['module_yp_title']));
			}

			$yp_meta_description = "Directory of companies and businesses listed under $webpage_title_en";
			if(isset($dir_params['module_yp_description']) && trim($dir_params['module_yp_description']) != "") {
				$yp_meta_description = trim(stripslashes($dir_params['module_yp_description']));
			}


            $breadcrumbs [] = [
                "title" => "Directory",
                "link" => $canonical_link
            ];

			$q = "select * from yp_categories where uid in ($uids_str) order by cy_admin_title_en";
			if(isset($_REQUEST['debug'])) echo "<br/>$q";
			$r = @mysql_query($q);
			if($r && mysql_num_rows($r) > 0) {
				?>
				<article class="article bl-middle-directory bl-middle  cyprus">
					<?php
					//$meta['description'] = "$domain_content_title is a directory listing the ellite ci "
					$webpage_title_en = $yp_title;
					$meta['description'] = $yp_meta_description;
					?>
					<header>
                        <h1><?php echo $yp_title;?></h1>
                        <!--[/SocialShareButtons/]-->
                    </header>
                    <div class="article-body">
                        <div class="list-group">
                            <?php
                            while ($row = mysql_fetch_assoc($r)) {
                                $c_friendly_name = $row['cy_friendly_name_en'];
                                $c_title = stripslashes($row['cy_admin_title_en']);
                                $c_uid = $row['uid'];


                                ?>
                                <div class="list-group-item"><strong>&raquo; </strong><a href="<?php echo $protocol.$server_host;?>/directory/<?php echo $c_friendly_name;?>"><?php echo $c_title;?></a>
                                    <?php

                                    $qa = "select * from yp_categories_links where uid='$c_uid' order by title asc, lang asc;";
                                    if(isset($_REQUEST['debug'])) echo "<br/>$qa";
                                    $ra = @mysql_query($qa);
                                    if($ra && mysql_num_rows($ra)>0) {
                                        echo '<ul>';
                                        while ($rowa = mysql_fetch_assoc($ra)) {
                                            $al_title = stripslashes($rowa['title']);
                                            $al_friendly_name = stripslashes($rowa['friendly_name']);
                                            $al_lang = $rowa['lang'];
                                            if(trim($al_friendly_name) != "") {
                                                echo '<li>';
                                                ?>
                                                <strong>&raquo; </strong><a href="<?php echo $protocol.$server_host;?>/directory/<?php echo $al_lang;?>/<?php echo $al_friendly_name;?>"><?php echo $al_title;?></a>
                                                <?php
                                                echo '</li>';
                                            }
                                        }
                                        echo '</ul>';
                                    }
                                    ?>
                                </div>
                                <?php
                            }
                            ?>
                        </div>
                    </div>
				</article>
				<?php
			}
			
		}
		
	}
}
?>