<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Baraka's FileManager Searcher</id>
    <version>1.0</version>
    <vqmver>1.X</vqmver>
    <author>Baraka</author>

    <file name="admin/controller/common/filemanager.php">
        <operation error="log" info="Add on Ajax Function to Class">
            <search position="before"><![CDATA[
             public function index() {
             ]]></search>
            <add><![CDATA[

            /* Mod By Baraka  */
            public function find_exel($directory, &$directory_array)
                {
                $this->load->model('tool/image');

                $count = 0;
                    // Create array of current directory
                    $files = scandir($directory);
                    if(is_array($files))
                    {
                        foreach($files as $value)
                        {
                            if($value == '.' || $value == '..')
                                continue;
                           $count++;

                         if (is_file($value)) {
                              $ext = strrchr($value, '.');
                         } else {
                              $ext = '';
                         }

                        //$value image name
                        //path $directory;
                            if(is_dir($directory.'/'.$value))
                            {
                                // Add value to current array, dir or file
                                $directory_array["path"][$value] = array($value => "file", "size" => $size);
                                $this->find_exel($directory.'/'.$value, $directory_array);
                            }
                            else
                            {
                               $re = $this->model_tool_image->resize(substr($directory, strlen(DIR_IMAGE)).'/'.$value, 100, 100);
                               $directory_array["path"][$value] = array('url'=> $re,'id' => $count, 'filename' => basename($value), "file" => substr($directory, strlen(DIR_IMAGE)).'/'.$value);
                            }
                        }
                    }
                    ksort($directory_array);
                }

                public function preg_grep_keys( $pattern, $input, $flags = 0 )
                {
                    $keys = preg_grep( $pattern, array_keys( $input ), $flags );
                    $vals = array();
                    foreach ( $keys as $key )
                    {
                        $vals[$key] = $input[$key];
                    }
                    return $vals;
                }


            public function exel() {
			 header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
             header('Cache-Control: no-cache, must-revalidate');
             header('Pragma: no-cache');
             header('Content-type: application/json; charset=UTF-8');
            $root = DIR_IMAGE . 'data/';
               /* Only use this for debugging
                $json = array();

                if (isset($this->request->get['directory'])) {
                    $directories = glob(rtrim(DIR_IMAGE . 'data/' . str_replace('../', '', $this->request->post['directory']), '/') . '/*', GLOB_ONLYDIR);


                    if ($directories) {
                        $i = 0;

                        foreach ($directories as $directory) {
                            $json[basename($directory)] = basename($directory);
                            $i++;
                          $final = array();
                            foreach($json as $exel => $k) {
                             $id = "{$k}";
                             $name = "data";
                             $final[$exel] = '';
                             $out = $final;
                            }
                        }
                    }
                }
                if (array_key_exists($this->request->get['name'], $folder_list['path'])) {
                       print_r(json_encode(array('status' => 'success', "data" => $folder_list['path'][$this->request->get['name']])));
                } else {
                        print(json_encode(array('status' => 'invalid', 'errors' => (!empty($this->request->get['name'])) ? "Nothing was found" : 'Nothing submitted')));
                }
                */
           // $paths =  array("'".implode("','", array_keys($out))."'");
            $folder_list = array();
            $this->find_exel($root, $folder_list);


           /* echo "<pre>";
            print_r($folder_list);
            echo "</pre>";*/


            if(!empty($this->request->get['name'])) {
            $search = $this->preg_grep_keys("/{$this->request->get['name']}/i",$folder_list['path']);
               print(json_encode(array('status' => 'success', "data" => $search)));
            } else {
               print(json_encode(array('status' => 'invalid', 'errors' => (!empty($this->request->get['name'])) ? "Nothing was found" : 'Nothing submitted')));
            }

            }
			]]></add>
        </operation>
    </file>
    <file name="admin/view/template/common/filemanager.tpl">
        <operation error="skip" info="Replace Form with add-on">
            <search position="after"><![CDATA[
                   <div id="menu">
                ]]></search>
            <add trim="true"><![CDATA[
                <div id="menu">
                 <div style="float:right;margin-right: 18px;">
                  <input type="text" id="search" value="" placeholder="Search image title.." style="margin-top: 5px;float:left;width: 200px;"/>
                  <a id="btnSearch" class="button" style="background-image: url('view/image/filemanager/search.gif');">Filter This Category</a>
                  <a id="btnSearchGlobal" class="button" style="background-image: url('view/image/filemanager/search.gif');">Filter Globally</a>
                  </div>
                  </div>
                ]]></add>
        </operation>
    </file>
    <file name="admin/view/template/common/filemanager.tpl">
        <operation error="skip" info="Replace Form with add-on">
            <search position="before"><![CDATA[
             $('#refresh').bind('click', function () {

                ]]></search>
            <add trim="true"><![CDATA[

            // New selector
            jQuery.expr[':'].Contains = function(a, i, m) {
             return jQuery(a).text().toUpperCase()
                 .indexOf(m[3].toUpperCase()) >= 0;
            };

            // Overwrites old selecor
            jQuery.expr[':'].contains = function(a, i, m) {
             return jQuery(a).text().toUpperCase()
                 .indexOf(m[3].toUpperCase()) >= 0;
            };

            $("#btnSearch").click(function (){
                var str = $("#search").val();
                if ($("#column_right div:contains('" + str + "')").length > 0)
                $("#column_right div a:contains('" + str + "')").attr("class","selected");

                $('#column_right div a').hide();

                $("#column_right div a.selected").each(function(){
                    $(this).show().removeClass("selected");
                });
            });

        $("#btnSearchGlobal").click(function (){
        var str = $("#search").val();

        $.ajax({
                            url: 'index.php?route=common/filemanager/exel&token=<?php echo $token; ?>',
                            type: 'get',
                            data: 'name=' + encodeURIComponent(str),

                            dataType: 'json',
                            success: function(json) {
                                html = '<div>';
                                if (json.data) {

                               $.each(json.data, function(index, element) {
                                         //console.log(index);
                                         //console.log(element.filename);
                                         id = element.id;
                                         filename = element.filename;
                                         file = element.file;
                                         url = element.url;
                                         html += '<a style="width: 122px;overflow: hidden;-o-text-overflow: ellipsis; -ms-text-overflow: ellipsis;text-overflow: ellipsis;white-space: nowrap" id="'+id+'" data-image="' +url + '" file="' + file + '"><img width="100" height="100" src="' + url + '" title="' + filename + '" /><br />' + filename + '</a>';
                               });
                               }
        
                                html += '</div>';
                                $('#column_right').html(html);
                               //console.log(html);

                            }


                        });


        });
                ]]></add>
        </operation>
    </file>
</modification>