<?php
/**
 * Created by PhpStorm.
 * User: IOAN
 * Date: 12/3/2015
 * Time: 10:55 AM
 */
if(isset($_REQUEST['debug_xpc_buttons'])) {
    echo '<pre>$module_params = ', var_export($module_params, true), '</pre>';
}
$xpc_buttons = array();
$xpc_pages = isset($module_params['xpc_pages']) ? $module_params['xpc_pages'] : array();
$xpc_pages_link = isset($module_params['xpc_pages_link']) ? $module_params['xpc_pages_link'] : array();
$xpc_module_title = isset($module_params['xpc_module_title']) ? $module_params['xpc_module_title'] : '';
if(is_array($xpc_pages) && count($xpc_pages) > 0 ) {
    $xpc_catdoms = implode(", ", $xpc_pages);
    $q = "SELECT * FROM domain_pages WHERE catdom_id='$domain_id' AND page_id IN ($xpc_catdoms) 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)) {
            $xpc_buttons[$row['page_id']] = array_map('stripslashes', $row);
        }
    }
}
//echo '<pre>', var_export($xpc_buttons, true), '</pre>';
if(count($xpc_buttons) > 0) {
    if (is_array($xpc_buttons) && count($xpc_buttons) > 0) {
        $optres['css'][] = $module_css_path . 'amodules/xpc_buttons/css/xpc_buttons.css';
        ?>
        <section class="xpc-buttons" role="navigation">
            <?php
            if($xpc_module_title != "") {
                ?>
                <header>
                    <h4><?= $xpc_module_title; ?></h4>
                </header>
                <?php
            }
            ?>
            <div class="list-group">
                <?php
                $xpc_cnt = 1;
                foreach ($xpc_buttons as $xpc_id => $xpc_data) {
                    $selected = '';
                    $xpc_url = $xpc_data['friendly_url'] . '.html';
                    $xpc_title = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $xpc_data['title_el']));

                    if(isset($xpc_pages_link[$xpc_id]) && trim($xpc_pages_link[$xpc_id]) != "") {
                        $xpc_title = preg_replace('/[ ]{2,}/', ' ', str_replace('|', '', $xpc_pages_link[$xpc_id]));
                    }

                    $xpc_title_alt = '';

                    if($xpc_title_alt == "") $xpc_title_alt = $xpc_title;

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