Skip to content

Commit

Permalink
feat: allow searching in all special folders
Browse files Browse the repository at this point in the history
  • Loading branch information
mercihabam committed Nov 25, 2024
1 parent ed4e297 commit 5be6143
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
16 changes: 16 additions & 0 deletions modules/advanced_search/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public function process() {

if ($this->request->post['all_folders']) {
$msg_list = $this->all_folders_search($imap, $flags, $params, $limit);
} elseif ($this->request->post['all_special_folders']) {
$msg_list = $this->special_folders_search($imap, $flags, $params, $limit);
} else if (!$imap->select_mailbox($this->folder)) {
return;
} else {
Expand All @@ -115,6 +117,20 @@ private function all_folders_search($imap, $flags, $params, $limit) {
return $msg_list;
}

private function special_folders_search($imap, $flags, $params, $limit) {
$specials = $this->user_config->get('special_imap_folders', array());
$folders = $specials[$this->imap_id] ?? [];

$msg_list = array();
foreach ($folders as $folder) {
$this->folder = $folder;
$imap->select_mailbox($this->folder);
$msgs = $this->imap_search($flags, $imap, $params, $limit);
$msg_list = array_merge($msg_list, $msgs);
}
return $msg_list;
}

private function imap_search($flags, $imap, $params, $limit) {
$msg_list = array();
$exclude_deleted = true;
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 @@ -40,5 +40,6 @@
'adv_terms' => array('filter' => FILTER_DEFAULT, 'flags' => FILTER_REQUIRE_ARRAY),
'adv_targets' => array('filter' => FILTER_DEFAULT, 'flags' => FILTER_REQUIRE_ARRAY),
'all_folders' => FILTER_VALIDATE_BOOLEAN,
'all_special_folders' => FILTER_VALIDATE_BOOLEAN,
)
);
70 changes: 51 additions & 19 deletions modules/advanced_search/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,18 @@ var expand_adv_folder = function(res) {
}
};

$(document).on("change", "input#all_folders", function() {
$(document).on("change", "input[name='all_folders'],input[name='all_special_folders']", function() {
const folderLi = $(this).closest('li');
const divergentCheckboxName = this.name === 'all_folders' ? 'all_special_folders' : 'all_folders';
const divergentCheckbox = $(this).closest('div').find(`input[name='${divergentCheckboxName}']`)

if ($(this).is(':checked')) {
folderLi.find('a').attr('disabled', 'disabled');
divergentCheckbox.prop('checked', false);
divergentCheckbox.attr('disabled', 'disabled');
} else {
folderLi.find('a').removeAttr('disabled');
divergentCheckbox.removeAttr('disabled');
}
});

Expand All @@ -106,16 +112,25 @@ var adv_select_imap_folder = function(el) {
list_container.show();
folders.show();

folders.find('li').each(function() {
folders.find('li').each(function(index) {
const wrapper = $('<div class="d-flex justify-content-between"></div>');
$(this).wrapInner(wrapper);
const allSpecialFoldersCheckbox = `
<span class="form-check">
<label class="form-check-label" for="all_special_folders-${index}">All special folders</label>
<input class="form-check-input" type="checkbox" name="all_special_folders" id="all_special_folders-${index}">
</span>
`;
const allFoldersCheckbox = `
<span class="form-check">
<label class="form-check-label" for="all_folders">All Folders</label>
<input class="form-check-input" type="checkbox" id="all_folders">
<label class="form-check-label" for="all_folders-${index}">All folders</label>
<input class="form-check-input" type="checkbox" name="all_folders" id="all_folders-${index}">
</span>
`;
wrapper.append(allFoldersCheckbox);
const checkboxesWrapper = $('<div class="d-flex gap-3"></div>');
checkboxesWrapper.append(allSpecialFoldersCheckbox);
checkboxesWrapper.append(allFoldersCheckbox);
wrapper.append(checkboxesWrapper);
})

$('.imap_folder_link', folders).addClass('adv_folder_link').removeClass('imap_folder_link');
Expand Down Expand Up @@ -212,13 +227,18 @@ var adv_expand_sections = function() {
var get_adv_sources = function() {
const sources = [];

const searchInAllFolders = $('.adv_folder_list li input:checked')
const searchInAllFolders = $('.adv_folder_list li input[name="all_folders"]:checked');
searchInAllFolders.each(function() {
const li = $(this).closest('li');
sources.push({'source': li.attr('class'), 'label': li.find('a').text()});
sources.push({'source': li.attr('class'), 'label': li.find('a').text(), allFolders: true});
});


const searchInSpecialFolders = $('.adv_folder_list li input[name="all_special_folders"]:checked');
searchInSpecialFolders.each(function() {
const li = $(this).closest('li');
sources.push({'source': li.attr('class'), 'label': li.find('a').text(), specialFolders: true});
});

const selected_sources = $('div', $('.adv_source_list'));
if (!selected_sources) {
return sources;
Expand All @@ -230,7 +250,7 @@ var get_adv_sources = function() {
sources.push({'source': source, 'label': $('a', $(this)).text()});
}
});
return [sources, searchInAllFolders.length > 0];
return sources;
};

var get_adv_terms = function() {
Expand Down Expand Up @@ -328,7 +348,7 @@ var process_advanced_search = function() {
Hm_Notices.show([err_msg('You must enter at least one search term')]);
return;
}
const [sources, allFolders] = get_adv_sources();
const sources = get_adv_sources();
if (sources.length == 0) {
Hm_Notices.show([err_msg('You must select at least one source')]);
return;
Expand All @@ -349,7 +369,7 @@ var process_advanced_search = function() {
search_summary({ 'terms': terms, 'targets': targets, 'sources': sources,
'times': times, 'other': other });

send_requests(build_adv_search_requests(terms, sources, targets, times, other), allFolders);
send_requests(build_adv_search_requests(terms, sources, targets, times, other));
};

var save_search_details = function(terms, sources, targets, times, other) {
Expand Down Expand Up @@ -384,7 +404,7 @@ var adv_group_vals = function(data, type) {
return groups;
};

var send_requests = function(requests, allFolders) {
var send_requests = function(requests) {
var request;
$('tr', Hm_Utils.tbody()).remove();
Hm_Utils.save_to_local_storage('formatted_advanced_search_data', '');
Expand All @@ -399,10 +419,15 @@ var send_requests = function(requests, allFolders) {
{'name': 'adv_start', 'value': request['time']['from']},
{'name': 'adv_end', 'value': request['time']['to']},
{'name': 'adv_source_limit', 'value': request['other']['limit']},
{'name': 'adv_charset', 'value': request['other']['charset']},
{'name': 'all_folders', 'value': allFolders}
{'name': 'adv_charset', 'value': request['other']['charset']}
];

if (request['all_folders']) {
params.push({name: 'all_folders', value: true});
} else if (request['all_special_folders']) {
params.push({name: 'all_special_folders', value: true});
}

for (var i=0, len=request['terms'].length; i < len; i++) {
params.push({'name': 'adv_terms[]', 'value': request['terms'][i]});
}
Expand Down Expand Up @@ -449,22 +474,29 @@ var build_adv_search_requests = function(terms, sources, targets, times, other)
var target_vals;
var requests = []
var term_groups = adv_group_vals(terms, 'term');
var target_groups = adv_group_vals(targets, 'target');
var target_groups = adv_group_vals(targets, 'target');

for (var tv=0, tvlen=term_groups.length; tv < tvlen; tv++) {
term_vals = term_groups[tv];
for (var tag=0, taglen=target_groups.length; tag < taglen; tag++) {
target_vals = target_groups[tag];
for (var s=0, slen=sources.length; s < slen; s++) {
source = sources[s]['source'];
source = sources[s];
for (var ti=0, tilen=times.length; ti < tilen; ti++) {
time = times[ti];
requests.push({'source': source, 'time': time, 'other': other,
'targets': target_vals, 'terms': term_vals});
const config = {'source': source.source, 'time': time, 'other': other,
'targets': target_vals, 'terms': term_vals};
if (source.allFolders) {
config['all_folders'] = true;
} else if (source.specialFolders) {
config['all_special_folders'] = true;
}
requests.push(config);
}
}
}
}
}

return requests;
};

Expand Down
2 changes: 1 addition & 1 deletion modules/smtp/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ protected function output() {
'<div class="mb-3 position-relative compose_container p-1 w-100 form-control">'.
'<div class="bubbles bubble_dropdown"></div>'.
'<input autocomplete="off" value="'.$this->html_safe($to).'" required name="compose_to" class="compose_to w-75" type="text" placeholder="'.$this->trans('To').'" id="compose_to" />'.
'<a href="#" tabindex="-1" class="toggle_recipients position-absolute top-0 end-0 pe-2"><i class="bi bi-plus-square-fill fs-3"></i></a>'.
'<a href="#" tabindex="-1" class="toggle_recipients position-absolute end-0 pe-2"><i class="bi bi-plus-square-fill fs-3"></i></a>'.
'<div id="to_contacts"></div>'.
'</div>'.
'</div>'.
Expand Down

0 comments on commit 5be6143

Please sign in to comment.