<?php
ob_start();
$show_business = false;
$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";
}*/


$q = "select * from yp_businesses where friendly_name='$yp_business';";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
	$row = mysql_fetch_assoc($r);
	
	$uid = $row['uid'];

    // Get category info
    $categ_title = '';
    $qc = "SELECT c.* FROM yp_categories c, yp_businesses_categories b WHERE c.uid=b.category_uid AND b.business_uid='$uid'";
    $rc = @mysql_query($qc);
    if($rc && mysql_num_rows($rc) > 0) {
        $rowc = mysql_fetch_assoc($rc);

        $categ_title = stripslashes($rowc['cy_admin_title_en']);
        $categ_link = $protocol . $microdata_host . "/directory/" . trim(stripslashes($rowc['cy_friendly_name_en']));

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

        $breadcrumbs [] = [
            "title" => $categ_title,
            "link" => $categ_link
        ];
    }


	$name = stripslashes($row['name']);
	$name = mb_convert_case($name, MB_CASE_TITLE, 'UTF-8');
	$description = stripslashes(trim($row['description']));
	$website = trim($row['website']);
	if($website != ""){
		$website = fix_url($website);
	}
	
	$webpage_title_en = $name . " | Directory Entry";
	$meta_advert = $description;
	$meta_params = array(
		"company"	=> stripslashes($name),
		"location"	=> "",
		"category"	=> $categ_title != "" ? $categ_title : stripslashes(trim($domain_content_title))
	);
	
	$meta['keywords'] = stripslashes($name) . ', ' . $meta['keywords'];
	
	$meta_replacements = array(
		'[COMPANY]',
		'[LOCATION]',
		'[CATEGORY]'
	);
	
	$meta_loc = "";
	$meta_phrase = "[COMPANY] is a Cyprus business located in [LOCATION]. It can be found in our [CATEGORY] directory";
		
	?>
	<article  class="article cyprus" <?php echo ($have_microdata) ? 'itemscope itemtype="' . $microdata_types[$microdata_domain]['adverts']['type'] . '"' : '';?>>
		<header>
            <h1 <?php echo ($have_microdata) ? 'itemprop="name"': '';?>><?php echo $name;?></h1>
            <!--[/SocialShareButtons/]-->
        </header>
        <div class="article-body cyprus">

            <?php
            if($website != ""){
                ?>
                <a href="javascript:;" rel="<?php echo $website;?>"  class="yp_js_link" target="_blank">visit website</a>
                <?php
            }
            ?>
            <div class="bl-description">
                <p <?php echo ($have_microdata) ? 'itemprop="description"': '';?>>
                    <?php
                    echo $description;
                    ?>
                </p>
                <?php
                if($website != "") {
                    ?>
                    <div class="pull-right">
                        <a href="<?php echo $website; ?>" rel="nofollow" target="_blank" class="yp_js_link">
                            <img src="https://open.thumbshots.org/image.aspx?url=<?php echo urlencode(fix_url($website)); ?>" alt="<?php echo $name;?>" class="img-responsive img-thumbnail"/>
                        </a>
                        <br/>
                        <span class="label label-primary"><?php echo fix_url($website);?></span>
                    </div>

                    <?php
                }
                ?>
            </div>

            <br/>
            <div>
            <?php

                $locs = array();
                $q = "select * from yp_businesses_locations where business_uid='$uid' order by district asc;";
                $loc_cnt = 1;
                $r = @mysql_query($q);
                if($r && mysql_num_rows($r) > 0) {
                    $show_business = true;
                    while ($row = mysql_fetch_assoc($r)) {

                        $l = array(
                            "address"	=> "&nbsp;",
                            "tel"		=> "&nbsp;",
                            "fax"		=> "&nbsp;",
                            "email"		=> "&nbsp;",
                            "district"	=> "&nbsp;"
                        );

                        $d = array_map('stripslashes', $row);
                        $b_trade_name = $d['trade_name'];
                        $b_trade_name_label = $d['trade_name_label'];
                        $b_description = $d['description'];
                        $b_address = $d['address'];
                        $b_pobox = $d['pobox'];
                        $b_pobox_postal_code = $d['pobox_postal_code'];
                        $b_postal_code = $d['postal_code'];
                        $b_location = $d['location'];
                        $b_district = isset($districts_map[$d['district']]) ? $districts_map[$d['district']] : "";
                        $b_district_id = $d['district_id'];
                        $b_tel1 = $d['tel1'];
                        $b_tel2 = $d['tel2'];
                        $b_fax1 = $d['fax1'];
                        $b_fax2 = $d['fax2'];
                        $b_mobile1 = $d['mobile1'];
                        $b_mobile2 = $d['mobile2'];
                        $b_email1 = $d['emal1'];
                        $b_email2 = $d['email2'];
                        $b_website = $d['website'];

                        if($b_district != "") {
                            $l['district'] = $b_district;
                        }

                        if($b_location != "") {
                            $keys = array_keys($districts_map);
                            $b_location = str_ireplace($keys, $districts_map, $b_location);
                        }


                        $address = array();
                        if($b_address != "") {
                            if($have_microdata) {
                                $address[] = '<span itemprop="streetAddress">' . $b_address . '</span>';
                            }
                            else {
                                $address[] = $b_address;
                            }
                        }

                        if($b_pobox != "") {
                            $address[] = "<strong>P.O. Box:</strong> " .  $b_pobox;
                        }

                        if($b_pobox_postal_code != "") {
                            $address[] = "<strong>P.O. Box Postal Code:</strong> " . $b_pobox_postal_code;
                        }


                        $dist = "";

                        if($b_postal_code != "") {
                            if($have_microdata) {
                                $dist .= '<span itemprop="PostalCode">' . $b_postal_code. '</span>';
                            }
                            else {
                                $dist .= $b_postal_code . ', ';
                            }
                        }

                        if($b_location != "") {
                            if($have_microdata) {
                                $dist .= '<span itemprop="addressLocality">' . $b_location. '</span>, ';
                            }
                            else {
                                $dist .= $b_location . ', ';
                            }

                            if($loc_cnt == 1) {
                                $meta_loc = trim(strip_tags(str_replace(array('0','1','2','3','4','5','6','7','8','9'), '', stripslashes($b_location))));
                            }

                        }

                        if($b_district != "") {
                            if($have_microdata) {
                                $dist .= '<span itemprop="addressRegion">' . $b_district. '</span><br/>';
                            }
                            else {
                                $dist .= "<strong>$b_district</strong><br/>";
                            }

                            if($loc_cnt == 1) {
                                if($meta_loc == "") {
                                    $meta_loc = trim(stripslashes(strip_tags($b_district)));
                                }
                                elseif($meta_loc != trim(stripslashes(strip_tags($b_district)))) {
                                    $meta_loc .= ", " . trim(stripslashes(strip_tags($b_district)));
                                }
                            }
                        }


                        if($meta_loc == "") $meta_loc = "Cyprus";

                        if($have_microdata) {
                            $dist .= '<meta itemprop="addressCountry" content="CY">';
                        }

                        if($dist != "") {
                            $address[] = $dist;
                        }

                        if(count($address) > 0) {
                            $l['address'] = implode("<br/>", $address);
                        }

                        $tels = array();
                        if($b_tel1 != "") $tels [] = $b_tel1;
                        if($b_tel2 != "") $tels [] = $b_tel2;

                        $faxes = array();
                        if($b_fax1 != "") $faxes [] = $b_fax1;
                        if($b_fax2 != "") $faxes [] = $b_fax2;

                        if(count($tels) > 0) {
                            $l['tel']  = implode(", ", $tels);
                        }

                        if(count($faxes) > 0) {
                            $l['fax'] = implode(", ", $faxes);
                        }

                        $emails = array();
                        if($b_email1 != "") $emails [] = $b_email1;
                        if($b_email2 != "") $emails [] = $b_email2;

                        if(count($emails) > 0) {
                            $l['email'] = implode(", ", $emails);
                        }

                        $have_loc_data = false;
                        foreach ($l as $type=>$val) {
                            if($val != "&nbsp;") {
                                $have_loc_data = true;
                                break;
                            }
                        }

                        if($have_loc_data) {
                            $locs[] = $l;
                        }

                        $loc_cnt ++;

                    }
                }

                if(count($locs) > 0) {

                    foreach ($locs as $i=>$loc) {
                        ?>
                        <table class="table" <?php echo ($have_microdata) ? 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"': '';?>>
                            <tr>
                                <th colspan="2">
                                    <?php echo ucfirst($loc['district']) . ' Address Details'; ?>
                                </th>
                            </tr>
                            <?php
                            if($loc['address'] != "&nbsp;") {
                                ?>
                                <tr>
                                    <td class="th">Address:</td>
                                    <td><?php echo $loc['address'];?></td>
                                </tr>
                                <?php
                            }
                            ?>
                            <?php
                            if($loc['tel'] != "&nbsp;") {
                                ?>
                                <tr>
                                    <td class="th">Telephone:</td>
                                    <td><span  <?php echo ($have_microdata) ? 'itemprop="telephone"': '';?>><?php echo $loc['tel'];?></span></td>
                                </tr>
                                <?php
                            }
                            ?>
                            <?php
                            if($loc['fax'] != "&nbsp;") {
                                ?>
                                <tr>
                                    <td class="th">Fax:</td>
                                    <td><span <?php echo ($have_microdata) ? 'itemprop="faxNumber"': '';?>><?php echo $loc['fax'];?></span></td>
                                </tr>
                                <?php
                            }
                            ?>
                            <?php
                            if($loc['email'] != "&nbsp;") {
                                ?>
                                <tr>
                                    <td class="th">Email:</td>
                                    <td><span <?php echo ($have_microdata) ? 'itemprop="email"': '';?>><?php echo $loc['email'];?></span></td>
                                </tr>
                                <?php
                            }
                            ?>

                        </table>
                        <?php
                    }
                }
                if($meta_loc != "") {
                    $meta_params['location'] = $meta_loc;
                    $meta_description_temp = str_replace($meta_replacements, $meta_params, $meta_phrase);
                    $meta_description_count = mb_strlen(trim($meta_description_temp), 'UTF-8');
                    if($meta_description_count < 10) {
                        $meta['description'] = $meta_description_temp . '. ' . mb_substr(trim(strip_tags($meta_advert)), 0, (150 - $meta_description_count));
                    }
                    else {
                        $meta['description'] = $meta_description_temp;
                    }
                }
                else {
                    $meta['description'] = '';
                }
                ?>

            </div>
		</div>
	</article>

	<?php
}
$business_data = ob_get_contents();
ob_end_clean();

if($show_business) {
	echo $business_data;
}
else {
	$send404 = true;
	require_once(dirname(__FILE__) . "/../404/404.php");
}
?>