1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class IMP_Basic_Search extends IMP_Basic_Base
27: {
28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: protected function _init()
49: {
50: global $injector, $notification, $page_output, $prefs, $registry;
51:
52:
53: $criteria = array(
54: 'from' => array(
55: 'label' => _("From"),
56: 'type' => 'header'
57: ),
58: 'recip' => array(
59: 'label' => _("Recipients (To/Cc/Bcc)"),
60: 'type' => 'header'
61: ),
62: 'to' => array(
63: 'label' => _("To"),
64: 'type' => 'header'
65: ),
66: 'cc' => array(
67: 'label' => _("Cc"),
68: 'type' => 'header'
69: ),
70: 'bcc' => array(
71: 'label' => _("Bcc"),
72: 'type' => 'header'
73: ),
74: 'subject' => array(
75: 'label' => _("Subject"),
76: 'type' => 'header'
77: ),
78: 'customhdr' => array(
79: 'label' => _("Custom Header"),
80: 'type' => 'customhdr'
81: ),
82: 'body' => array(
83: 'label' => _("Body"),
84: 'type' => 'text'
85: ),
86: 'text' => array(
87: 'label' => _("Entire Message"),
88: 'type' => 'text'
89: ),
90: 'date_range' => array(
91: 'label' => _("Date"),
92: 'type' => 'date'
93: ),
94: 'older' => array(
95: 'label' => _("Older Than"),
96: 'type' => 'within'
97: ),
98: 'younger' => array(
99: 'label' => _("Younger Than"),
100: 'type' => 'within'
101: ),
102:
103: 'size_smaller' => array(
104: 'label' => _("Size (KB) <"),
105: 'type' => 'size'
106: ),
107:
108: 'size_larger' => array(
109: 'label' => _("Size (KB) >"),
110: 'type' => 'size'
111: ),
112: );
113:
114: $filters = array(
115: 'bulk' => array(
116: 'label' => _("Bulk Messages"),
117: 'type' => 'filter'
118: ),
119: 'attach' => array(
120: 'label' => _("Contains Attachment(s)"),
121: 'type' => 'filter'
122: ),
123: 'mailinglist' => array(
124: 'label' => _("Mailing List Messages"),
125: 'type' => 'filter'
126: ),
127: 'personal' => array(
128: 'label' => _("Personal Messages"),
129: 'type' => 'filter'
130: ),
131: );
132:
133:
134: $constants = array(
135: 'within' => array(
136: 'd' => IMP_Search_Element_Within::INTERVAL_DAYS,
137: 'm' => IMP_Search_Element_Within::INTERVAL_MONTHS,
138: 'y' => IMP_Search_Element_Within::INTERVAL_YEARS
139: )
140: );
141:
142: if (isset($this->vars->mailbox_list)) {
143: if (is_array($this->vars->mailbox_list)) {
144: $default_mailbox = IMP_Mailbox::get('INBOX');
145: $search_mailbox = IMP_Mailbox::formFrom($this->vars->mailbox_list);
146: } else {
147: $default_mailbox = IMP_Mailbox::formFrom($this->vars->mailbox_list);
148: $search_mailbox = array($default_mailbox);
149: }
150: } elseif (isset($this->vars->mailbox)) {
151: $default_mailbox = IMP_Mailbox::formFrom($this->vars->mailbox);
152: $search_mailbox = array($default_mailbox);
153: } else {
154: $default_mailbox = IMP_Mailbox::get('INBOX');
155: $search_mailbox = array($default_mailbox);
156: }
157:
158:
159: if (!$injector->getInstance('IMP_Factory_Imap')->create()->access(IMP_Imap::ACCESS_SEARCH)) {
160: $default_mailbox->url('mailbox')->redirect();
161: }
162:
163: $imp_flags = $injector->getInstance('IMP_Flags');
164: $imp_search = $injector->getInstance('IMP_Search');
165:
166: $dynamic_view = ($registry->getView() == Horde_Registry::VIEW_DYNAMIC);
167: $js_vars = array();
168:
169: 170:
171: if ($this->vars->criteria_form) {
172: $c_list = array();
173:
174: foreach (json_decode($this->vars->criteria_form) as $val) {
175: switch ($val->t) {
176: case 'from':
177: case 'to':
178: case 'cc':
179: case 'bcc':
180: case 'subject':
181: $c_list[] = new IMP_Search_Element_Header(
182: $val->v,
183: $val->t,
184: $val->n
185: );
186: break;
187:
188: case 'recip':
189: $c_list[] = new IMP_Search_Element_Recipient(
190: $val->v,
191: $val->n
192: );
193: break;
194:
195: case 'customhdr':
196: $c_list[] = new IMP_Search_Element_Header(
197: $val->v->s,
198: $val->v->h,
199: $val->n
200: );
201: break;
202:
203: case 'body':
204: case 'text':
205: $c_list[] = new IMP_Search_Element_Text(
206: $val->v,
207: ($val->t == 'body'),
208: $val->n
209: );
210: break;
211:
212: case 'date_range':
213: $c_list[] = new IMP_Search_Element_Daterange(
214: $val->b ? new DateTime($val->b) : 0,
215: $val->e ? new DateTime($val->e) : 0,
216: $val->n
217: );
218: break;
219:
220: case 'older':
221: case 'younger':
222: $c_list[] = new IMP_Search_Element_Within(
223: $val->v->v,
224: $constants['within'][$val->v->l],
225: ($val->t == 'older')
226: );
227: break;
228:
229: case 'size_smaller':
230: case 'size_larger':
231: $c_list[] = new IMP_Search_Element_Size(
232: $val->v,
233: ($val->t == 'size_larger')
234: );
235: break;
236:
237: case 'or':
238: $c_list[] = new IMP_Search_Element_Or();
239: break;
240:
241: case 'bulk':
242: $c_list[] = new IMP_Search_Element_Bulk(
243: $val->n
244: );
245: break;
246:
247: case 'attach':
248: $c_list[] = new IMP_Search_Element_Attachment(
249: $val->n
250: );
251: break;
252:
253: case 'mailinglist':
254: $c_list[] = new IMP_Search_Element_Mailinglist(
255: $val->n
256: );
257: break;
258:
259: case 'personal':
260: $c_list[] = new IMP_Search_Element_Personal(
261: $val->n
262: );
263: break;
264:
265: case 'flag':
266:
267: $formdata = $imp_flags->parseFormId(rawurldecode($val->v));
268: $c_list[] = new IMP_Search_Element_Flag(
269: $formdata['flag'],
270: ($formdata['set'] && !$val->n)
271: );
272: break;
273: }
274: }
275:
276: $redirect_dynamic = true;
277: $redirect_target = false;
278:
279: switch ($this->vars->search_type) {
280: case 'filter':
281: $q_ob = $imp_search->createQuery($c_list, array(
282: 'id' => IMP_Mailbox::formFrom($this->vars->edit_query_filter),
283: 'label' => $this->vars->search_label,
284: 'type' => IMP_Search::CREATE_FILTER
285: ));
286:
287: if ($this->vars->edit_query_filter) {
288: $notification->push(sprintf(_("Filter \"%s\" edited successfully."), $this->vars->search_label), 'horde.success');
289: $redirect_dynamic = false;
290: $redirect_target = 'prefs';
291: } else {
292: $notification->push(sprintf(_("Filter \"%s\" created succesfully."), $this->vars->search_label), 'horde.success');
293: }
294: break;
295:
296: case 'vfolder':
297: $form = json_decode($this->vars->mboxes_form);
298: $q_ob = $imp_search->createQuery($c_list, array(
299: 'id' => IMP_Mailbox::formFrom($this->vars->edit_query_vfolder),
300: 'label' => $this->vars->search_label,
301: 'mboxes' => IMP_Mailbox::formFrom($form->mbox),
302: 'subfolders' => IMP_Mailbox::formFrom($form->subfolder),
303: 'type' => IMP_Search::CREATE_VFOLDER
304: ));
305:
306: if ($this->vars->edit_query_vfolder) {
307: $notification->push(sprintf(_("Virtual Folder \"%s\" edited successfully."), $this->vars->search_label), 'horde.success');
308: $redirect_target = 'prefs';
309: } else {
310: $notification->push(sprintf(_("Virtual Folder \"%s\" created succesfully."), $this->vars->search_label), 'horde.success');
311: $redirect_target = 'mailbox';
312: }
313: break;
314:
315: default:
316: $form = json_decode($this->vars->mboxes_form);
317: $q_ob = $imp_search->createQuery($c_list, array(
318: 'mboxes' => IMP_Mailbox::formFrom($form->mbox),
319: 'subfolders' => IMP_Mailbox::formFrom($form->subfolder)
320: ));
321: $redirect_target = 'mailbox';
322: break;
323: }
324:
325:
326: if ($redirect_target) {
327: if ($dynamic_view && $redirect_dynamic) {
328: echo '<html><head>' .
329: Horde::wrapInlineScript(array('window.parent.DimpBase.go("mbox", "' . $q_ob->mbox_ob->form_to . '")')) .
330: '</head></html>';
331: exit;
332: }
333:
334: switch ($redirect_target) {
335: case 'mailbox':
336: $q_ob->mbox_ob->url('mailbox')->redirect();
337: break;
338:
339: case 'prefs':
340: $registry->getServiceLink('prefs', 'imp')->add('group', 'searches')->redirect();
341: break;
342: }
343: exit;
344: }
345: }
346:
347:
348: $view = new Horde_View(array(
349: 'templatePath' => IMP_TEMPLATES . '/search'
350: ));
351: $view->addHelper('Text');
352: $view->addHelper('Tag');
353: $view->addHelper('FormTag');
354:
355: $view->action = self::url();
356:
357:
358: $q_ob = $default_mailbox->getSearchOb();
359: if ($this->vars->edit_query && $default_mailbox->search) {
360: if ($default_mailbox->vfolder) {
361: if (!$default_mailbox->editvfolder) {
362: $notification->push(_("Built-in Virtual Folders cannot be edited."), 'horde.error');
363: $registry->getServiceLink('prefs', 'imp')->add('group', 'searches')->redirect();
364: }
365: $view->edit_query = true;
366: $view->edit_query_vfolder = $default_mailbox->form_to;
367: } elseif ($imp_search->isFilter($q_ob)) {
368: if (!$imp_search->isFilter($q_ob, true)) {
369: $notification->push(_("Built-in Filters cannot be edited."), 'horde.error');
370: $registry->getServiceLink('prefs', 'imp')->add('group', 'searches')->redirect();
371: }
372: $view->edit_query = true;
373: $view->edit_query_filter = $default_mailbox->form_to;
374: }
375:
376: if ($view->edit_query) {
377: $view->search_label = $q_ob->label;
378: $js_vars['ImpSearch.prefsurl'] = strval($registry->getServiceLink('prefs', 'imp')->add('group', 'searches')->setRaw(true));
379: }
380: } else {
381:
382: $iterator = IMP_Search_IteratorFilter::create(
383: IMP_Search_IteratorFilter::QUERY
384: );
385: $rs = array();
386:
387: foreach ($iterator as $val) {
388: $rs[$val->formid] = array(
389: 'c' => $val->criteria,
390: 'f' => array(
391: 'm' => IMP_Mailbox::formTo($val->all ? array(IMP_Search_Query::ALLSEARCH) : array_map('strval', $val->mbox_list)),
392: 's' => IMP_Mailbox::formTo(array_map('strval', $val->subfolder_list))
393: ),
394: 'l' => Horde_String::truncate($val->querytext)
395: );
396: }
397:
398: if (!empty($rs)) {
399: $js_vars['ImpSearch.i_recent'] = $rs;
400: }
401:
402: $s_mboxes = IMP_Mailbox::formTo($search_mailbox);
403: $js_vars['ImpSearch.i_mboxes'] = array(
404: 'm' => $this->vars->subfolder ? array() : $s_mboxes,
405: 's' => $this->vars->subfolder ? $s_mboxes : array()
406: );
407: }
408:
409: if ($default_mailbox->search) {
410: $js_vars['ImpSearch.i_criteria'] = $q_ob->criteria;
411: $js_vars['ImpSearch.i_mboxes'] = array(
412: 'm' => IMP_Mailbox::formTo($q_ob->all ? array(IMP_Search_Query::ALLSEARCH) : $q_ob->mbox_list),
413: 's' => IMP_Mailbox::formTo($q_ob->subfolder_list)
414: );
415: }
416:
417:
418: $c_list = $types = array();
419: foreach ($criteria as $key => $val) {
420: $c_list[] = array(
421: 'l' => $val['label'],
422: 'v' => $key
423: );
424: $types[$key] = $val['type'];
425: }
426: $view->clist = $c_list;
427:
428:
429: $f_list = array();
430: foreach ($filters as $key => $val) {
431: $f_list[] = array(
432: 'l' => $val['label'],
433: 'v' => $key
434: );
435: $types[$key] = 'filter';
436: }
437: $view->filterlist = $f_list;
438:
439:
440: $flist = $imp_flags->getList(array(
441: 'imap' => true,
442: 'mailbox' => $default_mailbox
443: ));
444: $flag_set = array();
445:
446: foreach ($flist as $val) {
447: $flag_set[] = array(
448: 'l' => $val->label,
449: 'v' => rawurlencode($val->form_set)
450: );
451: $types[rawurlencode($val->form_set)] = 'flag';
452: }
453: $view->flist = $flag_set;
454:
455:
456: $mbox_list = array();
457: if (!$view->edit_query_filter) {
458: $js_vars['ImpSearch.allsearch'] = IMP_Mailbox::formTo(IMP_Search_Query::ALLSEARCH);
459: $ob = $injector->getInstance('IMP_Search_Ui')->getSearchMboxList();
460: $mbox_list = $ob->mbox_list;
461: $view->subscribe = $prefs->getValue('subscribe');
462: $view->tree = $ob->tree->getTree();
463: }
464:
465:
466: if (!$dynamic_view) {
467: $injector->getInstance('Horde_View_Topbar')->subinfo =
468: $injector->getInstance('IMP_View_Subinfo')->render();
469: }
470:
471: Horde_Core_Ui_JsCalendar::init();
472: $page_output->addScriptFile('hordecore.js', 'horde');
473: $page_output->addScriptFile('horde.js', 'horde');
474: $page_output->addScriptFile('search.js');
475:
476: $page_output->addInlineJsVars(array_merge($js_vars, array(
477:
478: 'ImpSearch.data' => array(
479: 'constants' => $constants,
480: 'dynamic_view' => $dynamic_view,
481: 'inbox' => IMP_Mailbox::get('INBOX')->form_to,
482: 'mbox_list' => $mbox_list,
483: 'months' => Horde_Core_Ui_JsCalendar::months(),
484: 'searchmbox' => $default_mailbox->form_to,
485: 'types' => $types
486: ),
487:
488: 'ImpSearch.text' => array(
489: 'and' => _("and"),
490: 'customhdr' => _("Custom Header:"),
491: 'datereset' => _("Date Reset"),
492: 'dateselection' => _("Date Selection"),
493: 'flag' => _("Flag:"),
494: 'loading' => _("Loading..."),
495: 'need_criteria' => _("Please select at least one search criteria."),
496: 'need_date' => _("Need at least one date in the date range search."),
497: 'need_mbox' => _("Please select at least one mailbox to search."),
498: 'need_label' => _("Saved searches require a label."),
499: 'not_match' => _("Do NOT Match"),
500: 'or' => _("OR"),
501: 'search_all' => _("Search All Mailboxes"),
502: 'search_term' => _("Search Term:"),
503: 'subfolder_search' => _("Search all subfolders?"),
504: 'to' => _("to")
505: )
506: )), array('onload' => true));
507:
508: if ($dynamic_view) {
509: if (!$this->vars->edit_query) {
510: $view->return_mailbox_val = sprintf(_("Return to %s"), $default_mailbox->display_html);
511: }
512: $page_output->topbar = $page_output->sidebar = false;
513: $this->header_params = array(
514: 'html_id' => 'htmlAllowScroll'
515: );
516: }
517:
518: $this->title = _("Search");
519: $this->output = $view->render('search');
520: }
521:
522: 523:
524: public static function url(array $opts = array())
525: {
526: return Horde::url('basic.php')
527: ->add('page', 'search')
528: ->setRaw(!empty($opts['full']));
529: }
530:
531: }
532: