1<?php
2/**
3 * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (GPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/gpl.
7 *
8 * @category  Horde
9 * @copyright 2009-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/gpl GPL
11 * @package   IMP
12 */
13
14/**
15 * Basic (simple) search script for basic view.
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2009-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/gpl GPL
21 * @package   IMP
22 */
23class IMP_Basic_Searchbasic extends IMP_Basic_Base
24{
25    /**
26     */
27    protected function _init()
28    {
29        global $injector, $notification;
30
31        if (!$this->indices->mailbox->access_search) {
32            $notification->push(_("Searching is not available."), 'horde.error');
33            $this->indices->mailbox->url('mailbox')->redirect();
34        }
35
36        $imp_flags = $injector->getInstance('IMP_Flags');
37        $imp_search = $injector->getInstance('IMP_Search');
38
39        /* If search_basic is set, we are processing the search query. */
40        if ($this->vars->search_basic) {
41            $c_list = array();
42
43            if ($this->vars->search_criteria_text) {
44                switch ($this->vars->search_criteria) {
45                case 'from':
46                case 'subject':
47                    $c_list[] = new IMP_Search_Element_Header(
48                        $this->vars->search_criteria_text,
49                        $this->vars->search_criteria,
50                        $this->vars->search_criteria_not
51                    );
52                    break;
53
54                case 'recip':
55                    $c_list[] = new IMP_Search_Element_Recipient(
56                        $this->vars->search_criteria_text,
57                        $this->vars->search_criteria_not
58                    );
59                    break;
60
61                case 'body':
62                case 'text':
63                    $c_list[] = new IMP_Search_Element_Text(
64                        $this->vars->search_criteria_text,
65                        ($this->vars->search_criteria == 'body'),
66                        $this->vars->search_criteria_not
67                    );
68                    break;
69                }
70            }
71
72            if ($this->vars->search_criteria_flag) {
73                $formdata = $imp_flags->parseFormId($this->vars->search_criteria_flag);
74                $c_list[] = new IMP_Search_Element_Flag(
75                    $formdata['flag'],
76                    ($formdata['set'] && !$this->vars->search_criteria_flag_not)
77                );
78            }
79
80            if (empty($c_list)) {
81                $notification->push(_("No search criteria specified."), 'horde.error');
82            } else {
83                /* Store the search in the session. */
84                $q_ob = $imp_search->createQuery($c_list, array(
85                    'id' => IMP_Search::BASIC_SEARCH,
86                    'mboxes' => array($this->indices->mailbox),
87                    'type' => IMP_Search::CREATE_QUERY
88                ));
89
90                /* Redirect to the mailbox screen. */
91                IMP_Mailbox::get($q_ob)->url('mailbox')->redirect();
92            }
93        }
94
95        $flist = $imp_flags->getList(array(
96            'imap' => true,
97            'mailbox' => $this->indices->mailbox
98        ));
99        $flag_set = array();
100        foreach ($flist as $val) {
101            $flag_set[] = array(
102                'val' => $val->form_set,
103                'label' => $val->label
104            );
105        }
106
107        /* Prepare the search template. */
108        $view = new Horde_View(array(
109            'templatePath' => IMP_TEMPLATES . '/basic/search'
110        ));
111        $view->addHelper('FormTag');
112        $view->addHelper('Tag');
113
114        $view->action = self::url();
115        $view->advsearch = Horde::link($this->indices->mailbox->url(IMP_Basic_Search::url()));
116        $view->mbox = $this->indices->mailbox->form_to;
117        $view->search_title = sprintf(_("Search %s"), $this->indices->mailbox->display_html);
118        $view->flist = $flag_set;
119
120        $this->title = _("Search");
121        $this->output = $view->render('search-basic');
122    }
123
124    /**
125     */
126    static public function url(array $opts = array())
127    {
128        return Horde::url('basic.php')->add('page', 'searchbasic');
129    }
130
131}
132