<?php

/**
* Requires and includes
*/
include_once $CFG->dirroot."/search/lib.php";

$defaultfiletypes = "PDF,TXT,HTML,PPT,XML,DOC,HTM";

if (!isset($CFG->block_search_utf8dir)){
    $CFG->block_search_utf8dir = 0;
}
if (!isset($CFG->block_search_softlock)){
    $CFG->block_search_softlock = 0;
}

?>
<div style="text-align:center;">
<table cellspacing="5" width="90%">
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configsearchtext', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_text" type="text" name="block_search_text" value="<?php 
            if(isset($CFG->block_search_text)) {
                p($CFG->block_search_text);
            } else {
                p(get_string('searchmoodle', 'block_search'));
            } ?>"/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configbuttonlabel', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
          <input id="block_search_button" type="text" name="block_search_button" value="<?php 
            if(isset($CFG->block_search_button)) {
                p($CFG->block_search_button);
            } else {
                p(get_string('go', 'block_search'));
            } ?>"/><br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configutf8transcoding', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
          <input type="radio" name="block_search_utf8dir" value="0" <?php if (!$CFG->block_search_utf8dir) echo "checked=\"checked\"" ?> /> <?php print_string('enabled', 'block_search') ?>
          <input type="radio" name="block_search_utf8dir" value="1" <?php if ($CFG->block_search_utf8dir) echo "checked=\"checked\"" ?> /> <?php print_string('disabled', 'block_search') ?>
          <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configusingsoftlock', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
          <input type="radio" name="block_search_softlock" value="0" <?php if (!$CFG->block_search_softlock) echo "checked=\"checked\"" ?> /> <?php print_string('disabled', 'block_search') ?>
          <input type="radio" name="block_search_softlock" value="1" <?php if ($CFG->block_search_softlock) echo "checked=\"checked\"" ?> /> <?php print_string('enabled', 'block_search') ?>
          <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configenablefileindexing', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_enable_file_indexing" type="radio" name="block_search_enable_file_indexing" value="1" <?php 
            if(isset($CFG->block_search_enable_file_indexing) && $CFG->block_search_enable_file_indexing) {
                echo " checked=\"checked\" ";
            } ?> /> <?php print_string('yes') ?> - 
            <input id="block_search_enable_file_indexing" type="radio" name="block_search_enable_file_indexing" value="0" <?php 
            if(!isset($CFG->block_search_enable_file_indexing) || !$CFG->block_search_enable_file_indexing) {
                echo " checked=\"checked\" ";
            } ?> /> <?php print_string('no') ?>
            <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configfiletypes', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
          <input id="block_search_filetypes" type="text" name="block_search_filetypes" value="<?php 
            if(!isset($CFG->block_search_filetypes)) {
                $CFG->block_search_filetypes = $defaultfiletypes;
            } 
            p($CFG->block_search_filetypes);
            ?>" />
            <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('usemoodleroot', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
        <?php 
            $usemoodleroot = (isset($CFG->block_search_usemoodleroot)) ? 'checked="checked"' : '' ; 
            $notusemoodleroot = (!isset($CFG->block_search_usemoodleroot)) ? 'checked="checked"' : '' ; 
        ?>
          <input id="block_search_usemoodleroot" type="radio" name="block_search_usemoodleroot" <?php echo $usemoodleroot ?> value="1"/> <?php print_string('yes') ?> - 
          <input id="block_search_usemoodleroot" type="radio" name="block_search_usemoodleroot" <?php echo $notusemoodleroot ?> value="0"/> <?php print_string('no') ?> 
          <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configlimitindexbody', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_limit_index_body" type="text" size="8" name="block_search_limit_index_body" value="<?php 
            if(isset($CFG->block_search_limit_index_body)) {
                p($CFG->block_search_limit_index_body);
            } else {
                p('');
            } ?>"/> <?php print_string('bytes', 'block_search') ?><br/><br/>
        </td>
    </tr>
</table>

<fieldset class="">
<legend align="top"><?php print_string('pdfhandling', 'block_search') ?></legend> 
<table cellspacing="5" width="90%">
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configpdftotextcmd', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_pdf_to_text_cmd" type="text" size="60" name="block_search_pdf_to_text_cmd" value="<?php 
            if(isset($CFG->block_search_pdf_to_text_cmd)) {
                p($CFG->block_search_pdf_to_text_cmd);
            } else {
                $encoding = ($CFG->unicodedb) ? "-enc UTF-8" : "-enc ISO-8859-1" ;
                if ($CFG->ostype == 'WINDOWS'){
                    p("lib/xpdf/win32/pdftotext.exe -eol dos $encoding -q");
                }
                else{
                    p("lib/xpdf/linux/pdftotext $encoding -eol unix -q");
                }
            } ?>"/><br/><br/>
        </td>
    </tr>
</table>
</fieldset>

<fieldset class="">
<legend align="top"><?php print_string('wordhandling', 'block_search') ?></legend> 
<table cellspacing="5" width="90%">
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configwordtotextcmd', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_word_to_text_cmd" type="text" size="60" name="block_search_word_to_text_cmd" value="<?php 
            if(isset($CFG->block_search_word_to_text_cmd)) {
                p($CFG->block_search_word_to_text_cmd);
            } else {
                if ($CFG->ostype == 'WINDOWS'){
                    p("lib/antiword/win32/antiword/antiword.exe ");
                }
                else{
                    p("lib/antiword/linux/usr/bin/antiword");
                }
            } ?>"/><br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configwordtotextenv', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_word_to_text_env" type="text" size="60" name="block_search_word_to_text_env" value="<?php 
            if(isset($CFG->block_search_word_to_text_env)) {
                echo stripslashes($CFG->block_search_word_to_text_env);
            } else {
                if ($CFG->ostype == 'WINDOWS'){
                    p("HOME={$CFG->dirroot}\\lib\\antiword\\win32");
                }
                else{
                    p("ANTIWORDHOME={$CFG->dirroot}/lib/antiword/linux/usr/share/antiword");
                }
            } ?>"/><br/><br/>
        </td>
    </tr>
</table>
</fieldset>

<?php 
$types = split(',', $CFG->block_search_filetypes);
if (!empty($types)){
    foreach($types as $type) {
        $utype = strtoupper($type);
        $type = strtolower($type);
        $type = trim($type);
        if (preg_match("/\\b$type\\b/i", $defaultfiletypes)) continue;
?>
<fieldset class="">
<legend align="top"><?php echo get_string('handlingfor', 'block_search').' '.$utype ?></legend> 
<table cellspacing="5" width="90%">
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configtypetotxtcmd', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_<?php p($type) ?>_to_text_cmd" type="text" size="60" name="block_search_<?php p($type) ?>_to_text_cmd" value="<?php 
            $propname = "block_search_{$type}_to_text_cmd";
            if(isset($CFG->$propname)) {
                p($CFG->$propname);
            } 
            ?>"/>
            <br/><br/>
        </td>
    </tr>
    <tr>
        <td valign="top" align="right">
            <b><?php print_string('configtypetotxtenv', 'block_search') ?>:</b>
        </td>
        <td valign="top" align="left">
            <input id="block_search_<?php p($type) ?>_to_text_env" type="text" size="60" name="block_search_<?php p($type) ?>_to_text_env" value="<?php 
            $propname = "block_search_{$type}_to_text_env";
            if(isset($CFG->$propname)) {
                echo stripslashes($CFG->$propname);
            } ?>"/><br/><br/>
        </td>
    </tr>
</table>
</fieldset>
<?php
    }
}
?>

<fieldset class="">
<legend align="top"><?php echo get_string('searchdiscovery', 'block_search') ?></legend> 
<table>
    <tr>
        <td>
            <pre>
            <?php
            $searchnames = search_collect_searchables(true);
            $searchable_list = implode("','", $searchnames);
            ?>
            </pre>
        </td>
    </tr>
</table>
</fieldset>

<fieldset class="">
<legend align="top"><?php echo get_string('modulessearchswitches', 'block_search') ?></legend> 
<table cellspacing="5" width="90%">
    <tr valign="top">
        <td align="left">
<?php
$i = 0;
$found_searchable_modules = 0;
if ($modules = get_records_select('modules', " name IN ('{$searchable_list}') ", 'name', 'id,name')){
    foreach($modules as $module){
        $i++;
        $keyname = "search_in_{$module->name}";
        $checkedup = (!isset($CFG->$keyname) || $CFG->$keyname) ? 'checked="checked"' : '' ;
        $checkeddown = !(!isset($CFG->$keyname) || $CFG->$keyname) ? 'checked="checked"' : '' ;
        echo "<input type=\"radio\" name=\"search_in_{$module->name}\" value=\"0\" {$checkeddown} /> " . get_string('no');
        echo " - <input type=\"radio\" name=\"search_in_{$module->name}\" value=\"1\" {$checkedup} /> " . get_string('yes');
        echo " - ".get_string('modulename', $module->name).'<br/>';
        if ($i % 20 == 0){
            echo "</td><td align=\"left\">";
        }
        $found_searchable_modules = 1;
    }
} 
if (!$found_searchable_modules) {
    print_string('nosearchablemodules', 'block_search');
}  
?>

        </td>
    </tr>
</table>
</fieldset>

<fieldset class="">
<legend align="top"><?php echo get_string('blockssearchswitches', 'block_search') ?></legend> 
<table cellspacing="5" width="90%">
    <tr valign="top">
        <td align="left">
<?php
$i = 0;
$found_searchable_blocks = 0;
if ($blocks = get_records_select('block', " name IN ('{$searchable_list}') ", 'name', 'id,name')){
    foreach($blocks as $block){
        $i++;
        $keyname = "search_in_{$block->name}";
        $checked = (!isset($CFG->$keyname) || $CFG->$keyname) ? 'checked="checked"' : '' ;
        echo "<input type=\"checkbox\" name=\"search_in_{$block->name}\" value=\"1\" {$checked} />";
        
        // multiple fallback strategy to get the name of the block
        $blocklabel = get_string('blockname', 'block_'.$block->name);
        if ($blocklabel == '[[blockname]]') $blocklabel = get_string($block->name, 'block_'.$block->name);
        if ($blocklabel == "[[{$block->name}]]") $blocklabel = "<span class=\"dimmed_text\">$block->name</span>";
        echo " - ".$blocklabel.'<br/>';
        if ($i % 20 == 0){
            echo "</td><td align=\"left\">";
        }
        $found_searchable_blocks = 1;
    }
}    
if (!$found_searchable_blocks) {
    print_string('nosearchableblocks', 'block_search');
}  
?>

        </td>
    </tr>
</table>
</fieldset>

<table cellspacing="5" width="90%">
    <tr>
        <td valign="top" align="right" colspan="2">
            <input type="submit" value="<?php print_string('savechanges'); ?>" />
        </td>
    </tr>
</table>
</div>
