<?php
// generate item for include/exclude. This function too small,
// and used only here, so don't move it to helper please

if (!function_exists('contextfinder_generate_item')) {
    function contextfinder_generate_item($name, $tabindex, $href = 'widget/allclasses', $value = '', $css_class='') {
        // if something changed here -chnage also in contextfinder.js new_el variable
        $tooltipTitle="Select a Class";

        if(preg_match('/^br*/', $name)===1){
            $tooltipTitle="Select a bundle";
        }

        echo '<div class="input-append flex flex--align_center white-bg item">
            <input name="'.$name.'[]" size="16" type="text" tabindex="'.$tabindex.'" value="'. $value .'" '. ($css_class != '' ? 'class ="'.$css_class.'"' : '').' />
            <a href="'.site_url($href).'" class="class_selector" ><i class="icon-eye-open add-on" rel="tooltip" title="'.$tooltipTitle.'"></i></a>
            <a href="#" class="delete_condition"><i class="icon-remove add-on" rel="tooltip" title="Delete condition"></i></a>
        </div>';
    }
}
?>
<?php

    //show X items if no include/exclude set
    $empty_items_on_page = 1;

 //

?>

<div <?php (isset($html_id) ? 'id="'.$html_id.'"' : '') ?> class="contextfinder_wrapper <?php echo ( (isset($embedded) && $embedded==true) ? 'embedded' : '') ?>">
  <?php
                $left_name = 'include';

                if (isset($fields['left']['name']) && $fields['left']['name'] != '') {
                    $left_name = $fields['left']['name'];
                }

                $right_name = 'exclude';

                if (isset($fields['right']['name']) && $fields['right']['name'] != '') {
                    $right_name = $fields['right']['name'];
                }

?>
        <table celpadding="0" cellspacing="0" class="borderLesstable no-hover">
        <thead>
            <tr>
                <td class="col1 <?php echo (isset($required)&& $required)?'required':'' ?>">
                    <label>
                        Includes (.*+[])
                        <a class="add_condition" destination="includes" fieldname="<?= htmlspecialchars($left_name) ?>" href="#" rel="tooltip" title="Add condition">
                         <i class="icon-plus"></i>
                       </a>
                    </label>
                </td>
                <td class="col2">&nbsp;</td>
                <td class="col1">
                    <label>
                        Excludes (.*+[])
                   <a class="add_condition" destination="excludes" fieldname="<?= htmlspecialchars($right_name) ?>" href="#"  rel="tooltip" title="Add condition">
                       <i class="icon-plus"></i>
                   </a>
                   </label>
                </td>
            </tr>
        </thead>
        <tbody>
        <tr>
            <td class="col1 includes">
<?php $tabindex = 1; ?>
                <?php

if(!isset($css_class))
{
    $css_class = 'input';
}
else
    $css_class = 'input'.' '.$css_class;

                $href= 'widget/allclasses';  //default

                if(isset($fields['left']['href'])) {
                    $href = $fields['left']['href'];
                }

                if (isset($fields['left']['values']) && $fields['left']['values'] != '') {
                $includes = $fields['left']['values'];
                    foreach ($includes as $item => $value) {
                        echo contextfinder_generate_item($left_name, $tabindex, $href, $value, $css_class );
                        $tabindex++;
                    }
                } else {
                    for ($i = 0; $i < $empty_items_on_page; $i++) {
                        echo contextfinder_generate_item($left_name, $tabindex, $href,  '', $css_class);
                        $tabindex++;
                    }
                }
                    ?>
            </td>
            <td class="col2"><a class="invert" href="#" rel="tooltip" title="swap selected context between includes and excludes"><i class="icon-exchange"></i></a></td>
            <td class="col1 excludes">

                <?php

                $href= 'widget/allclasses'; //default

                if(isset($fields['right']['href'])) {
                    $href = $fields['right']['href'];
                }

                if (isset($fields['right']['values']) && $fields['right']['values'] != '') {
                $excludes = $fields['right']['values'];
                    foreach ($excludes as $item => $value) {
                        echo contextfinder_generate_item($right_name, $tabindex, $href, $value, $css_class);
                        $tabindex++;
                    }
                } else {
                    for ($i = 0; $i < $empty_items_on_page; $i++) {
                        echo contextfinder_generate_item($right_name, $tabindex, $href, '', $css_class);
                        $tabindex++;
                    }
                }
            ?>
            </td>
        </tr>
        </tbody>
        </table>
<?php if ( isset($doNotShowButtons) && $doNotShowButtons != true) { ?>
        <a class="btn" id="resetConditions" href="#">Reset</a>
        <a class="btn btn-primary" id="setConditions" href="#"><i class="icon-ok"></i>Set conditions</a>
<?php } ?>

</div>
