Skip to content

Commit

Permalink
add simple keyword search to imap message list views
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmunro committed Nov 27, 2018
1 parent 98f79fe commit c3c0792
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions modules/core/setup.php
Expand Up @@ -212,6 +212,7 @@
'search_fld' => FILTER_SANITIZE_STRING,
'filter' => FILTER_SANITIZE_STRING,
'sort' => FILTER_SANITIZE_STRING,
'keyword' => FILTER_SANITIZE_STRING,
),

'allowed_post' => array(
Expand Down
6 changes: 5 additions & 1 deletion modules/imap/hm-imap.php
Expand Up @@ -2047,10 +2047,11 @@ public function get_first_message_part($uid, $type, $subtype=false, $struct=fals
* @param string $filter type of messages to include (UNSEEN, ANSWERED, ALL, etc)
* @param int $limit max number of messages to return
* @param int $offset offset from the first message in the list
* @param string $keyword optional keyword to filter the results by
* @return array list of headers
*/

public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $limit=0) {
public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $limit=0, $keyword=false) {
$result = array();

/* select the mailbox if need be */
Expand All @@ -2067,6 +2068,9 @@ public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $lim
else {
$uids = $this->sort_by_fetch($sort, $rev, $filter);
}
if ($keyword) {
$uids = $this->search($filter, $uids, array('TEXT' => $keyword));
}
$total = count($uids);

/* reduce to one page */
Expand Down
9 changes: 8 additions & 1 deletion modules/imap/modules.php
Expand Up @@ -460,6 +460,9 @@ public function process() {
}
}
$this->out('custom_list_controls_type', $custom_link);
if (array_key_exists('keyword', $this->request->get)) {
$this->out('list_keyword', $this->request->get['keyword']);
}
if (array_key_exists('filter', $this->request->get)) {
if (in_array($this->request->get['filter'], array('all', 'unseen', 'seen',
'answered', 'unanswered', 'flagged', 'unflagged'), true)) {
Expand Down Expand Up @@ -553,6 +556,7 @@ public function process() {
if ($this->get('list_filter')) {
$filter = strtoupper($this->get('list_filter'));
}
$keyword = $this->get('list_keyword', '');
list($sort, $rev) = process_sort_arg($this->get('list_sort'));
$limit = $this->user_config->get('imap_per_page_setting', DEFAULT_PER_SOURCE);
$offset = 0;
Expand All @@ -575,7 +579,7 @@ public function process() {
$imap = Hm_IMAP_List::connect($form['imap_server_id'], false);
if (imap_authed($imap)) {
$this->out('imap_mailbox_page_path', $path);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit);
list($total, $results) = $imap->get_mailbox_page(hex2bin($form['folder']), $sort, $rev, $filter, $offset, $limit, $keyword);
foreach ($results as $msg) {
$msg['server_id'] = $form['imap_server_id'];
$msg['server_name'] = $details['name'];
Expand Down Expand Up @@ -1521,6 +1525,7 @@ protected function output() {
if ($this->get('custom_list_controls_type')) {
$filter = $this->get('list_filter');
$sort = $this->get('list_sort');
$keyword = $this->get('list_keyword');
$opts = array('all' => $this->trans('All'), 'unseen' => $this->trans('Unread'),
'seen' => $this->trans('Read'), 'flagged' => $this->trans('Flagged'),
'unflagged' => $this->trans('Unflagged'), 'answered' => $this->trans('Answered'),
Expand All @@ -1533,6 +1538,8 @@ protected function output() {
$custom = '<form id="imap_filter_form" method="GET">';
$custom .= '<input type="hidden" name="page" value="message_list" />';
$custom .= '<input type="hidden" name="list_path" value="'.$this->html_safe($this->get('list_path')).'" />';
$custom .= '<input type="search" placeholder="'.$this->trans('search').
'" class="imap_keyword" name="keyword" value="'.$this->html_safe($keyword).'" />';
$custom .= '<select name="sort" class="imap_sort">';
foreach ($sorts as $name => $val) {
$custom .= '<option ';
Expand Down
3 changes: 2 additions & 1 deletion modules/imap/site.css
Expand Up @@ -62,7 +62,8 @@
.msg_move_to .move_to_location { font-size: 100%; left: auto; right: 0px; top: 33px; font-variant: normal; }
.msg_move_to { width: auto !important; position: relative; display: inline-block; }
.move_to_location .expand_link { float: left !important; width: auto !important; }
.imap_sort, .imap_filter { font-size: 75%; float: left; margin-right: 10px; margin-top: 8px; }
.imap_keyword, .imap_sort, .imap_filter { font-size: 75%; float: left; margin-right: 10px; margin-top: 8px; }
.imap_keyword { width: 120px; }
.msg_part_icon { opacity: .4; padding-right: 5px; }
.msg_part_placeholder { visibility: hidden; }

Expand Down
12 changes: 11 additions & 1 deletion modules/imap/site.js
Expand Up @@ -370,9 +370,19 @@ var setup_imap_folder_page = function() {
Hm_Timer.add_job(function() { select_imap_folder(hm_list_path()); }, 60);
$('.remove_source').click(remove_imap_combined_source);
$('.add_source').click(add_imap_combined_source);
$('.refresh_link').click(function() { select_imap_folder(hm_list_path()); });
$('.refresh_link').click(function() {
if ($('.imap_keyword').val()) {
$('#imap_filter_form').submit();
}
else {
select_imap_folder(hm_list_path());
}
});
$('.imap_filter').change(function() { $('#imap_filter_form').submit(); });
$('.imap_sort').change(function() { $('#imap_filter_form').submit(); });
$('.imap_keyword').on('search', function() {
$('#imap_filter_form').submit();
});
Hm_Ajax.add_callback_hook('ajax_message_action', function() { select_imap_folder(hm_list_path()); });
};

Expand Down

0 comments on commit c3c0792

Please sign in to comment.