1<?php
2/*----------------------------------------------------------------------------*/
3//  a simple search function for IssueTracker
4/*----------------------------------------------------------------------------*/
5//function it_simple_search ($project, &$renderer) {
6    global $ID;
7    global $conf;
8    $Generated_Header  = '';
9    $Generated_Scripts = '';
10    $Generated_Report  = '';
11    $Generated_Message = '';
12
13    $ref_findings = array();
14    $cnt_i_findings = -1;     // count issue findings
15    $cnt_c_findings = -1;     // count comment findings
16
17    $project = $this->project;
18    $search_words = explode(' ', $this->parameter);
19
20//---------------------------------------------------------------------------------------
21//  0. handle multi_projects
22    // detect the IssueTracker data store (path)
23    if($this->getConf('it_data')==false) $it_datastore = DOKU_CONF."../data/meta/";
24    else $it_datastore = DOKU_CONF."../". $this->getConf('it_data');
25
26    // check if last sign is a slash
27    $i = strrchr ($it_datastore, chr(47));     // chr(47) = "/"
28    $j = strrchr ($it_datastore, chr(92));     // chr(92) = "\"
29    if(($i !== strlen($it_datastore)) && ($i !== strlen($it_datastore))) { $it_datastore .= chr(47); }
30
31    if($this->getConf('multi_projects')!==false) {
32        // loop through it_datastore and list all .issues files
33        $xprojects = $this->__find_projects($it_datastore);
34
35        $x_projects = explode(',',$xprojects);
36        $issues = array();
37        $tmp    = array();
38
39        foreach ($x_projects as $project)
40        {   $project = trim($project);
41            if(is_file($it_datastore.$project.'.issues') == true) {
42                $tmp = unserialize(@file_get_contents($it_datastore.$project.'.issues'));
43
44                //  1. get issue file content
45                // loop through the field and add project to each row
46                foreach($tmp as &$tmps)
47                {   $tmps['project'] = $project; }
48
49                $issues = array_merge($issues, $tmp);
50                $tmp = array();
51            }
52        }
53    }
54    else {
55    //  1. get issue file content
56        if($conf['plugin']['issuetracker']['it_data']==false) $pfile = DOKU_CONF."../data/meta/".$project.'.issues';
57        else $pfile = DOKU_CONF."../". $conf['plugin']['issuetracker']['it_data'].$project.'.issues';
58        if (@file_exists($pfile))
59            {  $issues  = unserialize(@file_get_contents($pfile));
60               foreach($issues as &$issue) {
61                  $issue['project'] = $project;
62               }
63            }
64        elseif(strlen($project)>1)
65        	  {// promt error message that project file does not exist
66               msg(sprintf($this->getLang('msg_pfilemissing'), $pfile),-1); }
67    }
68
69//    echo var_dump($issues)."<br />";
70
71//---------------------------------------------------------------------------------------
72//  2. loop through single issues
73    foreach($issues as &$issue) {
74       $issue_string = implode($issue);
75
76       if($conf['plugin']['issuetracker']['it_data']==false) $comments_file = DOKU_CONF."../data/meta/".$issue['project']."_".$issue['id']. '.cmnts';
77       else $comments_file = DOKU_CONF."../". $conf['plugin']['issuetracker']['it_data'].$issue['project']."_".$issue['id']. '.cmnts';
78       $comments ='';
79       if (@file_exists($comments_file))  {  $comments  = @file_get_contents($comments_file);  }
80
81//     2.0 loop through search words and search issue and comments per word
82       foreach($search_words as $needle) {
83
84//        2.0.1 store issue id to reference_Array
85          if(stripos($issue_string,$needle)!==false)
86          {   $cnt_i_findings++;
87/*              $issue[$issue['project']."_".$issue['id']] = $issue['project']."_".$issue['id'];
88              $ref_findings['issues'][$cnt_i_findings]['project'] = $issue['project'];
89              $ref_findings['issues'][$cnt_i_findings]['id'] = $issue['id'];
90              $ref_findings['issues'][$cnt_i_findings]['match'] = $needle;
91*/
92              $ref_findings['issues'][$cnt_i_findings] = $issue;
93              $ref_findings['issues'][$cnt_i_findings]['match'] = $needle;
94          }
95
96//        2.0.2 remember comment reference_Array
97          if(stripos($comments,$needle)!==false)
98          {   $cnt_c_findings++;
99/*              $issue[$issue['project']."_".$issue['id']] = $issue['project']."_".$issue['id'];
100              $ref_findings['comment'][$cnt_c_findings]['project'] = $issue['project'];
101              $ref_findings['comment'][$cnt_c_findings]['id'] = $issue['id'];
102              $ref_findings['comment'][$cnt_c_findings]['match'] = $needle;
103*/
104              $ref_findings['comment'][$cnt_c_findings] = $issue;
105              $ref_findings['comment'][$cnt_c_findings]['match'] = $needle;
106          }
107       }
108    }
109
110//---------------------------------------------------------------------------------------
111//    3. Output
112        $found_tbl = '
113          <table class="inline it_search_result">
114            <tr><th>'.$this->getLang('search_Type').'</th><th>'.$this->getLang('th_project').'</th><th>'.$this->getLang('search_ID').'</th><th>'.$this->getLang('search_Subject').'</th></tr>'.NL;
115//     3.1 loop through reference_Array
116        if($cnt_i_findings > -1) {
117          foreach($ref_findings['issues'] as $item) {
118              $link      = 'doku.php?id='.$ID.'&do=showcaselink&showid='.$item['id'].'&project='.$item['project'];
119              $text_snip = $this->xs_format($issues[$item['project']."_".$item['id']]['description']);
120              $h_txt     = '<span class="search_hit">'.$item['match'].'</span>';
121              $text_snip = str_ireplace($item['match'], $h_txt, $text_snip);
122
123              $found_issues .= '<tr><td>'.$this->getLang('search_Issue').'</td>
124                                    <td>'.$item['project'].'</td>
125                                    <td><a href="'.$link.'" title="'.$this->getLang('search_Issue')." ".$item['id'].'">'.$item['id'].'</a></td>
126                                    <td><b><a href="'.$link.'" title="'.$this->getLang('search_Issue')." ".$item['id'].'">'.$item['title'].'</a></b><br />'.$text_snip.'</td>
127                                </tr>'.NL;
128          }
129        }
130        if($cnt_c_findings > -1) {
131          foreach($ref_findings['comment'] as $item) {
132              if($conf['plugin']['issuetracker']['it_data']==false) $comments_file = DOKU_CONF."../data/meta/".$item['project']."_".$item['id']. '.cmnts';
133              else $comments_file = DOKU_CONF."../". $conf['plugin']['issuetracker']['it_data'].$item['project']."_".$item['id']. '.cmnts';
134              $comments      = unserialize(@file_get_contents($comments_file));
135              $link          = 'doku.php?id='.$ID.'&do=showcaselink&showid='.$item['id'].'&project='.$item['project'];
136              $is_txt        = '<b>&raquo;</b> '.$this->getLang('search_Issue');
137              $found_issues .= '<tr style="background: lightgrey;" ><td>'.$is_txt.'</td><td>'.$item['project'].'</td><td><a href="'.$link.'" title="'.$this->getLang('search_Issue')." ".$item['id'].'">'.$item['id'].'</a></td><td>'.$item['title'].'</td></tr>'.NL;
138              foreach($comments as $comment) {
139                  $text_snip = $this->xs_format($comment['comment']);
140                  if(stripos($comment['comment'],$needle)!==false) {
141                      $h_txt         = '<span class="search_hit">'.$item['match'].'</span>';
142                      $text_snip     = str_ireplace($item['match'],$h_txt,$text_snip);
143
144                      $found_issues .= '<tr><td>'.$this->getLang('search_Comment').'</td>
145                                            <td>'.$item['project'].'</td>
146                                            <td><a href="'.$link.'#a'.$comment['id'].'" title="'.$this->getLang('search_Comment')." ".$comment['id'].'">'.$comment['id'].'</a></td>
147                                            <td><a href="'.$link.'#a'.$comment['id'].'" title="'.$this->getLang('search_Comment')." ".$comment['id'].'">'.$text_snip.'</a></td>
148                                        </tr>'.NL;
149                  }
150              }
151          }
152        }
153        // Render
154        if(strlen($found_issues) === 0) {
155            msg('found nothing',-1);
156            html_show();
157             }
158        else {
159            $Generated_Table = $this->getLang('search_hl1').NL.
160                               $this->getLang('search_txt1').NL.'<br /><br />'.NL.
161                               '<table class="it_searchbox"><tr><td>'.NL.
162                               '    <form  method="post" action="doku.php?id=' . $ID . '&do=it_search">'.NL.
163                               '       <label class="it__searchlabel">'.$this->getLang('lbl_search').'</label>'.NL.
164                               '       <input class="itl__sev_filter" name="it_str_search" id="it_str_search" type="text" value="'.$this->parameter.'"/>'.NL.
165                               '       <input type="hidden" name="project" id="project" value="'.$item['project'].'"/>'.NL.
166                               '       <input class="itl__search_button" id="searchcase" type="submit" name="searchcase" value="'.$this->getLang('btn_search').'" title="'.$this->getLang('btn_search_title').'"/>'.NL.
167                               '    </form>'.NL.
168                               '</td></tr></table>'.NL.
169                               $this->getLang('search_hl2').NL.'<div class="level2">'.NL.
170                               $found_tbl . $found_issues . '</table></div>'.NL;
171        }
172//}
173//---------------------------------------------------------------------------------------
174?>
175
176
177