Skip to content

Commit

Permalink
feat(frontend/backend): allow searching for messages in all subfolder…
Browse files Browse the repository at this point in the history
…s of a given folder
  • Loading branch information
mercihabam committed Nov 30, 2024
1 parent 80873a9 commit 9064e7f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
1 change: 0 additions & 1 deletion modules/advanced_search/js_modules/route_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ function applyAdvancedSearchPageHandlers() {
$('.adv_controls').show();
$('.core_msg_control').off('click');
$('.core_msg_control').on("click", function() { return Hm_Message_List.message_action($(this).data('action')); });
Hm_Message_List.set_checkbox_callback();
if (typeof check_select_for_imap !== 'undefined') {
check_select_for_imap();
}
Expand Down
13 changes: 9 additions & 4 deletions modules/advanced_search/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ public function process() {
}
}

if ($this->request->post['all_folders']) {
$searchInAllFolders = $this->request->post['all_folders'] ?? false;
$searchInSpecialFolders = $this->request->post['all_special_folders'] ?? false;
$includeSubfolders = $this->request->post['include_subfolders'] ?? false;
if ($searchInAllFolders) {
$msg_list = $this->all_folders_search($imap, $flags, $params, $limit);
} elseif ($this->request->post['all_special_folders']) {
} elseif ($searchInSpecialFolders) {
$msg_list = $this->special_folders_search($imap, $flags, $params, $limit);
} else if ($includeSubfolders) {
$msg_list = $this->all_folders_search($imap, $flags, $params, $limit, $this->folder);
} else if (!$imap->select_mailbox($this->folder)) {
return;
} else {
Expand All @@ -105,8 +110,8 @@ public function process() {
$this->out('imap_server_ids', array($this->imap_id));
}

private function all_folders_search($imap, $flags, $params, $limit) {
$folders = $imap->get_mailbox_list();
private function all_folders_search($imap, $flags, $params, $limit, $parent = '') {
$folders = $imap->get_mailbox_list(mailbox:$parent);
$msg_list = array();
foreach ($folders as $folder) {
$this->folder = $folder['name'];
Expand Down
1 change: 1 addition & 0 deletions modules/advanced_search/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
'adv_targets' => array('filter' => FILTER_DEFAULT, 'flags' => FILTER_REQUIRE_ARRAY),
'all_folders' => FILTER_VALIDATE_BOOLEAN,
'all_special_folders' => FILTER_VALIDATE_BOOLEAN,
'include_subfolders' => FILTER_VALIDATE_BOOLEAN,
)
);
35 changes: 29 additions & 6 deletions modules/advanced_search/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var expand_adv_folder = function(res) {
$('.adv_folder_link', list_container).on("click", function() { return expand_adv_folder_list($(this).data('target')); });
$('a', list_container).not('.adv_folder_link').off('click');
$('a', list_container).not('.adv_folder_link').on("click", function() { adv_folder_select($(this).data('id')); return false; });
modifyInnerLists();
}
};

Expand Down Expand Up @@ -131,7 +132,9 @@ var adv_select_imap_folder = function(el) {
checkboxesWrapper.append(allSpecialFoldersCheckbox);
checkboxesWrapper.append(allFoldersCheckbox);
$(this).find('.wrapper').append(checkboxesWrapper);
})
});

modifyInnerLists();

$('.imap_folder_link', folders).addClass('adv_folder_link').removeClass('imap_folder_link');
$('.adv_folder_list').html(folders.html());
Expand All @@ -147,6 +150,20 @@ var adv_select_imap_folder = function(el) {
});
};

function modifyInnerLists() {
$('.adv_folder_list').find('.inner_list li').each(function(index) {
const subFoldersCheckbox = `
<span class="form-check form-text">
<label class="form-check-label" for="include_subfolders-${index}">Include subfolders</label>
<input class="form-check-input" type="checkbox" name="include_subfolders" id="include_subfolders-${index}">
</span>
`;
$(this).wrapInner('<div class="d-flex wrapper"></div>');
$(this).find('.wrapper').append(subFoldersCheckbox);
$(this).find('#main-link').css('flex-grow', 0)
});
}

var adv_folder_select = function(id) {
if ($('.'+id, $('.adv_source_list')).length > 0) {
$('.adv_folder_list').html('');
Expand All @@ -164,17 +181,19 @@ var adv_folder_select = function(id) {
var parent_class = '.'+parts[0]+'_'+parts[1]+'_';
var account = $('a', $(parent_class, container)).first().text();
var label = account+' &gt; '+folder;
add_source_to_list(id, label);
const includeSubfolders = $(`.${id}`).closest('li').find('input[name="include_subfolders"]').is(':checked');

add_source_to_list(id, label, includeSubfolders);
$('.adv_folder_list').html('');
$('.close_adv_folders').remove();
$('.adv_folder_list').hide();
};

var add_source_to_list = function(id, label) {
var add_source_to_list = function(id, label, includeSubfolders) {
var close = $(globals.close_html);
close.addClass('adv_remove_source');
close.attr('data-target', id);
var row = '<div class="'+id+'">'+close.prop('outerHTML')+label;
var row = '<div class="'+id+'" data-subfolders="'+includeSubfolders+'">'+close.prop('outerHTML')+label;
row += '<input type="hidden" value="'+id+'" /></div>';
$('.adv_source_list').append(row);
$('.adv_remove_source').off('click');
Expand Down Expand Up @@ -247,7 +266,7 @@ var get_adv_sources = function() {
const source = this.className;
const mailboxSource = source.split('_').slice(0, 2).join('_');
if (!sources.find(s => s.source.indexOf(mailboxSource) > -1)) {
sources.push({'source': source, 'label': $('a', $(this)).text()});
sources.push({'source': source, 'label': $('a', $(this)).text(), subFolders: $(this).data('subfolders')});
}
});
return sources;
Expand Down Expand Up @@ -426,6 +445,8 @@ var send_requests = function(requests) {
params.push({name: 'all_folders', value: true});
} else if (request['all_special_folders']) {
params.push({name: 'all_special_folders', value: true});
} else if (request['sub_folders']) {
params.push({name: 'include_subfolders', value: true});
}

for (var i=0, len=request['terms'].length; i < len; i++) {
Expand Down Expand Up @@ -490,7 +511,9 @@ var build_adv_search_requests = function(terms, sources, targets, times, other)
config['all_folders'] = true;
} else if (source.specialFolders) {
config['all_special_folders'] = true;
}
} else if (source.subFolders) {
config['sub_folders'] = true;
}
requests.push(config);
}
}
Expand Down

0 comments on commit 9064e7f

Please sign in to comment.