<?php
$optres['css'][] = $module_css_path . "amodules/bl_middle/css/bl_middle.css";
$bl_lang = "en";
$in_catdom_id = $domain_id;
if(isset($catdom_id) && intval($catdom_id) > 0) {
    $in_catdom_id = $catdom_id;
}

$show_bl_business = true;
$display_bl_business = true;
$q = "select c.* from left_content c, module m where c.catdom_id='$in_catdom_id' and c.module_id=m.module_id
	and m.url='bl_middle' and c.`type`='module' and c.location='middle' and c.module_active='yes' and c.module_params !=''
";
//ob_end_clean();
//echo $q;
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    $row = mysql_fetch_assoc($r);
    $bl_mp = unserialize(base64_decode($row['module_params']));
    $bl_categ_uids = array();
    if(isset($bl_mp['module_bl_categs']) && is_array($bl_mp['module_bl_categs'])) {
        $bl_categ_uids = array_keys($bl_mp['module_bl_categs']);
        $bl_categ_uids_str = "";
        if(count($bl_categ_uids) > 0) {
            $bl_temp = array();
            foreach ($bl_categ_uids as $i=>$t_uid) {
                $bl_temp [] = "'" . $t_uid . "'";
            }
            $bl_categ_uids_str = implode(",", $bl_temp);
        }
        else {
            $send404 = true;
            $show_bl_business = false;
        }
    }
    else {
        $send404 = true;
        $show_bl_business = false;
    }
}
else {
    $send404 = true;
    $show_bl_business = false;
}

if($show_bl_business) {

    $ref_url = null;
    $server_referer_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;

    if($server_referer_url != null) {
        $server_referer = parse_url($server_referer_url);
        if($server_referer['host'] == $_SERVER['HTTP_HOST']) {
            $ref_url = $server_referer_url;
        }
    }
    else {
        if($_SESSION['previous_url']) {
            $ref_url = $_SESSION['previous_url'];
        }
    }


    //echo '<br>$referrer_url = ' . $referrer_url;
    //echo '<br>$_SESSION[\'previous_url\'] = ' . $_SESSION['previous_url'];

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

    if($ref_url) {
        $referer_url = $ref_url;
        $referer = parse_url($referer_url);
        $referer_path = $referer['path'];
        if(strpos($referer_path, 'listings') !== false ) {
            $refs = explode("/", ltrim(rtrim($referer_path, '/'), '/'));
            if(count($refs) > 1) {
                $category_fr = $refs[count($refs) - 1];
                $category_lang = null;
                if($refs[count($refs) - 2] != "listings") $category_lang = $refs[count($refs) - 2];

                if($category_lang) {
                    // Get category details
                    $qc = "SELECT * FROM bl_categories_links_tr WHERE friendly_name='".mysql_real_escape_string($category_fr)."' and lang='$category_lang';";
                    $rc = @mysql_query($qc);
                    if($rc && mysql_num_rows($rc) > 0) {
                        $rowc = mysql_fetch_assoc($rc);
                        $category_uid = $rowc['uid'];

                        $breadcrumbs[] = [
                            "title" => stripslashes($rowc['title']),
                            "link" => $protocol . $server_host . "/listings/$bl_lang/" . stripslashes($rowc['friendly_name'])
                        ];

                        $category_breadcrumbs = true;
                    }
                }
                else {
                    // Get category details
                    $qc = "SELECT * FROM bl_categories WHERE cy_friendly_name_en='".mysql_real_escape_string($category_fr)."'";
                    $rc = @mysql_query($qc);
                    if($rc && mysql_num_rows($rc) > 0) {
                        $rowc = mysql_fetch_assoc($rc);
                        $category_uid = $rowc['uid'];
                        $breadcrumbs[] = [
                            "title" => stripslashes($rowc['cy_admin_title_en']),
                            "link" => $protocol . $server_host . "/listings/" . stripslashes($rowc['cy_friendly_name_en'])
                        ];

                        $category_breadcrumbs = true;
                    }
                }

                //echo $qc;

            }
        }
    }

    if(isset($bl_alias_lang)) {
        $q = "select b.*, c.category_uid from bl_businesses_tr b, bl_businesses_categories c where b.friendly_name='$bl_business' and lang='$bl_alias_lang' and b.uid=c.business_uid and c.category_uid in ($bl_categ_uids_str) GROUP BY b.uid;";
        //echo $q;
        $r = @mysql_query($q);
        if($r && mysql_num_rows($r) > 0) {
            $row = mysql_fetch_assoc($r);
            $parent_uid = $row['uid'];
            $category_uid = $row['category_uid'];

            $q = "select b.* from bl_businesses b where b.uid='$parent_uid'";

        }
        else {
            $show_bl_business = false;
            $send404 = true;
        }
    }
    else {
        $q = "select b.*, c.category_uid from bl_businesses b, bl_businesses_categories c where b.friendly_name='$bl_business' and b.uid=c.business_uid and c.category_uid in ($bl_categ_uids_str) GROUP BY b.uid;";
    }
    //echo $q;
    $r = @mysql_query($q);
    if($r && mysql_num_rows($r) > 0) {
        $row = mysql_fetch_assoc($r);
        if($category_uid == null) $category_uid = $row['category_uid'];

        if(isset($_REQUEST['debug'])) {
            echo '<pre>', var_export($row, true), '</pre>';
        }

        $uid = $row['uid'];
        $name = stripslashes($row['name']);
        //$name = mb_convert_case($name, MB_CASE_TITLE, 'UTF-8');
        $description = stripslashes(trim($row['description']));
        $website = trim($row['website']);
        $website_error = (array_key_exists("website_error", $row) && !is_null($row['website_error'])) ? true : false;
        if($website != "" && !$website_error){
            $website = fix_url($website);
        }
        else {
            $website = "";
        }

        if(isset($bl_alias_lang)) {
            $bl_lang = $bl_alias_lang;

            $qt = "select * from bl_businesses_tr where uid='$uid' and lang='$bl_lang';";
            $rt = @mysql_query($qt);
            if($rt && mysql_num_rows($rt) == 1) {
                $rowt = mysql_fetch_assoc($rt);

                $name = stripslashes($rowt['name']);
                //$name = mb_convert_case($name, MB_CASE_TITLE, 'UTF-8');
                $description = stripslashes(trim($rowt['description']));
            }

            if(!$category_breadcrumbs) {
                // Get category details
                $qc = "SELECT * FROM bl_categories_links_tr WHERE uid='$category_uid'' and lang='$bl_lang';";
                $rc = @mysql_query($qc);
                if ($rc && mysql_num_rows($rc) > 0) {
                    $rowc = mysql_fetch_assoc($rc);

                    $breadcrumbs[] = [
                        "title" => stripslashes($rowc['title']),
                        "link" => $protocol . $server_host . "/listings/$bl_lang/" . stripslashes($rowc['friendly_name'])
                    ];
                }
            }

        }
        else {
            if(!$category_breadcrumbs) {
                // Get category details
                $qc = "SELECT * FROM bl_categories WHERE uid='$category_uid'";
                $rc = @mysql_query($qc);
                if ($rc && mysql_num_rows($rc) > 0) {
                    $rowc = mysql_fetch_assoc($rc);

                    $breadcrumbs[] = [
                        "title" => stripslashes($rowc['cy_admin_title_en']),
                        "link" => $protocol . $server_host . "/listings/" . stripslashes($rowc['cy_friendly_name_en'])
                    ];
                }
            }
        }

        //echo $qc;





        $webpage_title_en = $name . " | Free Listing";

        if(isset($bl_alias_lang)) {
            $webpage_title_en .= " | " . strtoupper($bl_alias_lang);
        }

        $meta_advert = $description;
        $meta_params = array(
            "company"	=> stripslashes($name),
            "location"	=> "",
            "category"	=> stripslashes(trim($domain_content_title)),
            "lang"		=> (isset($bl_lang) && $bl_lang != "en") ? $dlc[$bl_lang] : ''
        );

        $meta['keywords'] = stripslashes($name) . ', ' . $meta['keywords'];

        $meta_replacements = array(
            '[COMPANY]',
            '[LOCATION]',
            '[CATEGORY]',
            '[LANG]'
        );

        $meta_loc = "";
        $meta_phrase = "[COMPANY] is a Cyprus business located in [LOCATION]. It can be found in our [CATEGORY] [LANG] listing";

        ob_start();



        ?>
        <article class="article cyprus">
            <header>
                <h1 lang="<?php echo $bl_lang; ?>" xml:lang="<?php echo $bl_lang; ?>"><?php echo $name;?></h1>
                <!--[/SocialShareButtons/]-->
            </header>

            <div class="article-body cyprus">
                <?php
                if($website != ""){
                    ?>
                    <a href="<?php echo $website;?>" rel="nofollow"  class="yp_js_link" target="_blank">visit website</a>
                    <?php
                }

                ?>

                <div class="bl-description">
                    <p lang="<?php echo $bl_lang; ?>" xml:lang="<?php echo $bl_lang; ?>">
                        <?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="<?=$protocol;?>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 bl_businesses_locations where business_uid='$uid' order by district asc;";
                    $loc_cnt = 1;
                    $r = @mysql_query($q);
                    if($r && mysql_num_rows($r) > 0) {
                        while ($row = mysql_fetch_assoc($r)) {

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

                            $d = array_map('stripslashes', $row);
                            $location_id = $d['id'];
                            $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 = $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(isset($bl_alias_lang)) {
                                $qt = "select * from bl_businesses_locations_tr where business_uid='$uid' and location_id='$location_id' and lang='$bl_lang' order by district asc;";
                                $rt = @mysql_query($qt);
                                if($rt && mysql_num_rows($rt) == 1) {
                                    $rowt = mysql_fetch_assoc($rt);

                                    $b_trade_name = stripslashes($rowt['trade_name']);
                                    $b_trade_name_label = stripslashes($rowt['trade_name_label']);
                                    $b_address = stripslashes($rowt['address']);
                                    $b_location = stripslashes($rowt['location']);
                                    $b_district = stripslashes($rowt['district']);
                                }
                            }

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

                            if($b_location != "") {
                                //
                            }


                            $address = array();
                            if($b_address != "") {
                                $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 != "") {
                                $dist .= $b_postal_code . ', ';
                            }

                            if($b_location != "") {
                                $dist .= $b_location . ', ';
                                if($loc_cnt == 1) {
                                    $meta_loc = trim(stripslashes($b_location));
                                }
                            }

                            if($b_district != "") {
                                $dist .= "<strong>$b_district</strong><br/>";
                                if($loc_cnt == 1) {
                                    if($meta_loc == "") {
                                        $meta_loc = trim(stripslashes($b_district));
                                    }
                                    elseif($meta_loc != trim(stripslashes($b_district))) {
                                        $meta_loc .= ", " . trim(stripslashes($b_district));
                                    }
                                }
                            }

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

                            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">
                                <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><?php echo $loc['tel'];?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                                <?php
                                if($loc['fax'] != "&nbsp;") {
                                    ?>
                                    <tr>
                                        <td class="th">Fax:</td>
                                        <td><?php echo $loc['fax'];?></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                                <?php
                                if($loc['email'] != "&nbsp;") {
                                    ?>
                                    <tr>
                                        <td class="th">Email:</td>
                                        <td><?php echo $loc['email'];?></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

        if(!isset($_REQUEST['page']) && $show_bl_adverts) {
            echo '<br/>';
            $hide_adverts_title = 1;
            include_once(dirname(__FILE__) . "/../adverts/adverts.php");
            echo '<br/>';
        }

        $bl_content = ob_get_contents();
        ob_end_clean();
    }
    else {
        $display_bl_business = false;
        $send404 = true;
    }
}

if($show_bl_business && $display_bl_business) {
    echo $bl_content;
}
else {
	require_once(dirname(__FILE__) . "/../404/404.php");
}
