<?php
ob_start();
mb_internal_encoding("UTF-8");
$debug_bl = isset($_REQUEST['debug_bl']);
$show_bl_contents = true;
if(isset($module_params['module_bl_show']) && intval($module_params['module_bl_show']) == 0) {
	$show_bl_contents = false;
}

if($debug_bl) {
    echo '$show_bl_contents = ' . var_dump($show_bl_contents);
}
?>

<?php
if($show_bl_contents) {
	$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));
	
	// Get Domain title
    $catdom_title = "Business";
	$more_text = "";
    if(isset($module_params['module_bl_more_text']) && trim($module_params['module_bl_more_text']) != "") {
        $more_text = trim($module_params['module_bl_more_text']);
    }
	$q = "select * from domaincategories where catdom_id='$domain_id';";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)>0) {
		$row = mysql_fetch_assoc($r);
        $catdom_title = stripslashes($row['title_en']);
        if($more_text == "") {
            $more_text = 'See more ' . stripslashes(strtolower($row['title_en'])) . ' listings';
        }
	}
	
	$more_text .= '  <i class="fa fa-chevron-circle-right"></i>';

	$optres['css'][] = $module_css_path . "amodules/bl_middle/css/bl_middle.css";
	//$optres['js'][] = "$path" . "js/jquery-ui-1.8.23.custom.min.js";
	if(isset($module_params['module_bl_categs']) && is_array($module_params['module_bl_categs']) && count($module_params['module_bl_categs']) > 0) {
		
		require_once(dirname(__FILE__) . "/functions/bl_list_entries.php");
		$yp_limit = 10;
		$yp_page = 1;
		$yp_from = 0;
		/* Commented out as we do not want pagination in homepage */
		/*
		if(!isset($_REQUEST['list']) || intval($_REQUEST['list'])<1) {
			$yp_page = 1;
		}
		else {
			$yp_page = intval($_REQUEST['list']);
		}
		$yp_from = 0;
		if($yp_page > 1) {
			$yp_from = $yp_page * $yp_limit;
		}
		*/
		
		if(str_replace('/listings', '', $_SERVER['REQUEST_URI']) != $_SERVER['REQUEST_URI']) {
			//$canonical_link = $protocol . $ultra_domainname . "/listings";
		}
		
		
		
		$module_bl_categs = $module_params['module_bl_categs'];
		
		$categs_uids = array();
		foreach (array_keys($module_bl_categs) as $categ_uid) {
			$categs_uids [] = "'" . $categ_uid . "'";
		}
		$categs_uids_str = implode(",", $categs_uids);

        $yp_data_search = $pfcache->get('yp_data_search');
        if($yp_data_search == null) {
            $yp_data_search = bl_list_all_entries($categs_uids_str, '', $yp_page, $yp_limit, true);
            $pfcache->set('yp_data_search', $yp_data_search, 1200);
        }
			
		if(isset($_REQUEST['debugbl'])) {	
			echo '<pre>', print_r($yp_data_search), '</pre>';
		}
		
		$yp_total_records = $yp_data_search['total_records'];
		$yp_data = $yp_data_search['data'];
		
		if($yp_total_records > 0) {

            $module_bl_title = $catdom_title . ' Listings';
            if(isset($module_params['module_bl_title']) && trim($module_params['module_bl_title']) != "") {
                $module_bl_title = trim(stripslashes($module_params['module_bl_title']));
            }


			?>
            <section class="box box-primary">
                <header class="box-header with-border">
                     <h2 class="h2"><?=$module_bl_title;?></h2>
                </header>
                <div class="box-body no-padding">
                    <?php
                    if(isset($_REQUEST['debugbl'])) {
                        //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'][] = $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
                                        $b_data['name'] = str_replace(".", ". ", $b_data['name']);
                                        echo '<a href="'. $protocol . $server_host.'/listings/name/'.$b_data['friendly_name'].'" class="bl-list-link" '. (($have_microdata)? 'itemprop="url"':'' ) .' target="_blank">';
                                        echo ($have_microdata) ? '<span itemprop="name">':'';
                                        echo $b_data['name'];
                                        echo ($have_microdata) ? '</span>':'';
                                        echo '</a>';
                                        ?>
                                    </div>
                                    <div class="col-md-6 col-sm-7">
                                            <?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 = "&nbsp;";
                                                        $col_class = "col-sm-3 col-xs-3";

                                                        if($td_type == "district") {
                                                            $col_class = "col-sm-4 col-xs-4";
                                                            if($have_microdata) {
                                                                $yp_tmp = '<meta itemprop="addressCountry" content="CY">';
                                                                $yp_tmp .= '<span itemprop="addressRegion">';
                                                            }
                                                            //$yp_val = $yp_val;
                                                            $yp_tmp .= '<strong><i class="fa fa-map-marker" title="Telephone"></i> ' . $yp_val . '</strong>';
                                                            if($have_microdata) {
                                                                $yp_tmp .= '</span>';
                                                            }
                                                        }

                                                        if($td_type == "tel" && $yp_val != "&nbsp;") {
                                                            $col_class = "col-sm-3 col-xs-3";
                                                            $yp_tmp .= '<strong><i class="fa fa-phone" title="Telephone"></i> </strong>';
                                                            if($have_microdata) {
                                                                $yp_tmp = '<span itemprop="telephone">' . $yp_val;
                                                            }
                                                            else {
                                                                $yp_tmp .= $yp_val;
                                                            }
                                                            if($have_microdata) {
                                                                $yp_tmp .= '</span>';
                                                            }
                                                        }

                                                        if($td_type == "fax" && $yp_val !="&nbsp;") {
                                                            $col_class = "col-sm-3 col-xs-3";
                                                            $yp_tmp = '<strong><i class="fa fa-fax" title="Fax"></i> </strong>';
                                                            if($have_microdata) {
                                                                $yp_tmp = '<span itemprop="faxNumber">' . $yp_val;
                                                            }
                                                            else {
                                                                $yp_tmp .= $yp_val;
                                                            }
                                                            if($have_microdata) {
                                                                $yp_tmp .= '</span>';
                                                            }
                                                        }
                                                        ?>
                                                        <div class="<?=$col_class;?> no-padding"><?php echo $yp_tmp;?></div>
                                                        <?php
                                                    }
                                                    ?>
                                                </div>
                                                <?php
                                            }
                                            ?>
                                    </div>

                                </div>
                            <?php

                        }
                        ?>
                        <div class="box-footer">
                            <p><a href="listings"><?php echo $more_text;?></a></p>
                        </div>
                        <?php
                    }
                    ?>

                </div>
            </section>
			<?php
			
		}
	}
}
$bl_contents = ob_get_contents();
ob_end_clean();

if($show_bl_contents) echo $bl_contents;
?>