<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 12/3/2015
 * Time: 10:55 AM
 */
if(isset($_REQUEST['debug_xp_buttons'])) {
    echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';
}
$xp_buttons = array();
$xp_pages = isset($module_params['xp_pages']) ? $module_params['xp_pages'] : array();
$xp_pages_link = isset($module_params['xp_pages_link']) ? $module_params['xp_pages_link'] : array();
$xp_module_title = isset($module_params['xp_module_title']) ? $module_params['xp_module_title'] : '';
if(is_array($xp_pages) && count($xp_pages) > 0 ) {
    $xp_catdoms = implode(", ", $xp_pages);
    $q = "SELECT * FROM domaincategories WHERE parent_id='$domain_id' AND catdom_id IN ($xp_catdoms) AND is_top_menu='0' AND active='yes' ORDER BY order_seq ASC;";
    //echo $q;
    $r = @mysql_query($q);
    if ($r && mysql_num_rows($r) > 0) {
        while ($row = mysql_fetch_assoc($r)) {
            $xp_buttons[$row['catdom_id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre>', var_export($xp_buttons, true), '</pre>';
if(count($xp_buttons) > 0) {
    if (is_array($xp_buttons) && count($xp_buttons) > 0) {
        $optres['css'][] = $module_css_path . 'amodules/xp_buttons/css/xp_buttons.css';
        ?>
        <section class="xp-buttons" role="navigation">
            <?php
            if($xp_module_title != "") {
                ?>
                <header>
                    <i class="fa fa-bookmark text-danger pull-left"></i>
                    <h4><?= $xp_module_title; ?></h4>
                </header>
                <?php
            }
            ?>
            <div class="list-group">
                <?php
                $xp_cnt = 1;
                foreach ($xp_buttons as $xp_id => $xp_data) {
                    $selected = '';
                    $xp_url = "$base_url/" . $xp_data['friendly_url'] . '.html';
                    $xp_title = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $xp_data['title_el']));

                    if(isset($xp_pages_link[$xp_id]) && trim($xp_pages_link[$xp_id]) != "") {
                        $xp_title = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $xp_pages_link[$xp_id]));
                    }

                    $xp_title_alt = trim(preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $xp_data['title_el_alt'])));

                    if($xp_title_alt == "") $xp_title_alt = $xp_title;

                    if (strpos($_SERVER['REQUEST_URI'], $xp_url) !== false) {
                        $selected = 'active';
                    }
                    ?>
                    <div class="list-group-item xp<?= $xp_cnt; ?> text-left <?= $selected; ?>">
                        <a href="<?= $xp_url; ?>" class="" title="<?= $xp_title; ?>"><?= $xp_title; ?></a>
                    </div>
                    <?php
                    $xp_cnt++;
                    if ($xp_cnt > 9) $xp_cnt = 1;
                }
                ?>
            </div>
        </section>
        <?php
    }
}