Skip to content

Commit

Permalink
Put subscription to folders to separate page
Browse files Browse the repository at this point in the history
  • Loading branch information
josaphatim committed Oct 26, 2023
1 parent 7936b49 commit 4d2ab60
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 84 deletions.
3 changes: 3 additions & 0 deletions modules/imap/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ function format_imap_folder_section($folders, $id, $output_mod) {
else {
$results .= $output_mod->html_safe($folder['basename']);
}
if (isset($folder['subscribed'])) {
$results .= '<input type="checkbox" value="1" class="folder_subscription" id="'.$output_mod->html_safe($folder_name).'" name="'.$folder_name.'" '.($folder['subscribed']? 'checked="checked"': '').($folder['name'] == 'INBOX'? ' disabled="disabled"': '').' />';
}
$results .= '<span class="unread_count unread_imap_'.$id.'_'.$output_mod->html_safe($folder_name).'"></span></li>';
}
if ($manage) {
Expand Down
7 changes: 6 additions & 1 deletion modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,12 @@ public function process() {
$cache = Hm_IMAP_List::get_cache($this->cache, $form['imap_server_id']);
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache);
if (imap_authed($imap)) {
$msgs = $imap->get_folder_list_by_level(hex2bin($folder));
$only_subscribed = $this->user_config->get('only_subscribed_folders_setting', false);
$with_subscription = isset($this->request->post['subscription_state']) && $this->request->post['subscription_state'];
if ($with_subscription) {
$only_subscribed = false;
}
$msgs = $imap->get_folder_list_by_level(hex2bin($folder), $only_subscribed, $with_subscription);
if (isset($msgs[$folder])) {
unset($msgs[$folder]);
}
Expand Down
38 changes: 13 additions & 25 deletions modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,11 @@ public function get_mailbox_list($lsub=false, $mailbox='', $keyword='*') {
$check_for_new = false;

/* full folder name, includes an absolute path of parent folders */
$folder = $this->utf7_decode($vals[(count($vals) - 1)]);
if ($vals[(count($vals) - 1)] == ')' && $lsub) {
$folder = $this->utf7_decode($vals[6]);
} else {
$folder = $this->utf7_decode($vals[(count($vals) - 1)]);
}

/* sometimes LIST responses have dupes */
if (isset($folders[$folder]) || !$folder) {
Expand Down Expand Up @@ -2136,31 +2140,10 @@ public function get_mailbox_page($mailbox, $sort, $rev, $filter, $offset=0, $lim
* @param string $level mailbox name or empty string for the top level
* @return array list of matching folders
*/
public function get_folder_list_by_level($level='') {
public function get_folder_list_by_level($level='', $only_subscribed=false, $with_subscription_state = false) {
$result = array();
$folders = $this->get_mailbox_list(true, $level, '%');
$folders = $this->get_mailbox_list($only_subscribed, $level, '%');
foreach ($folders as $name => $folder) {
$result[$name] = array(
'delim' => $folder['delim'],
'basename' => $folder['basename'],
'children' => $folder['has_kids'],
'noselect' => $folder['noselect'],
'id' => bin2hex($folder['basename']),
'name_parts' => $folder['name_parts'],
);
}
return $result;
}

/**
* return all the folders with subscribed attribute
* @return array list of folders
*/
public function get_mailbox_list_with_subscription() {
$result = array();
$all_folders = $this->get_mailbox_list();
$subscribed_folders = array_column($this->get_mailbox_list(true), 'basename');
foreach ($all_folders as $name => $folder) {
$result[$name] = array(
'name' => $folder['name'],
'delim' => $folder['delim'],
Expand All @@ -2169,9 +2152,14 @@ public function get_mailbox_list_with_subscription() {
'noselect' => $folder['noselect'],
'id' => bin2hex($folder['basename']),
'name_parts' => $folder['name_parts'],
'subscribed' => in_array($folder['basename'], $subscribed_folders)
);
}
if ($with_subscription_state) {
$subscribed_folders = array_column($this->get_mailbox_list(true), 'name');
foreach ($result as $key => $folder) {
$result[$key]['subscribed'] = in_array($folder['name'], $subscribed_folders);
}
}
return $result;
}
}
Expand Down
101 changes: 69 additions & 32 deletions modules/imap_folders/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,27 @@ public function process() {
}
}

/**
* Process input from the folder subscription setting in the settings page
* @subpackage imap/handler
*/
class Hm_Handler_process_only_subscribed_folders_setting extends Hm_Handler_Module {
public function process() {
function only_subscribed_folders_setting_callback($val) {
return $val;
}
process_site_setting('only_subscribed_folders', $this, 'only_subscribed_folders_setting_callback', false, true);
}
}

/**
* @subpackage imap_folders/output
*/
class Hm_Output_folders_server_select extends Hm_Output_Module {
protected function output() {
$server_id = $this->get('folder_server', -1);
$res = '<div class="folders_page"><form method="get">';
$res .= '<input type="hidden" name="page" value="folders" />';
$res .= '<input type="hidden" name="page" value="'.$this->get('router_page_name').'" />';
$res .= '<select id="imap_server_folder" name="imap_server_id">';
$res .= '<option ';
if ($server_id == -1) {
Expand Down Expand Up @@ -518,31 +531,6 @@ protected function output() {
}
}

/**
* @subpackage imap_folders/output
*/
class Hm_Output_folders_folder_subscription extends Hm_Output_Module {
protected function output() {
if (($server = $this->get('folder_server')) === NULL) {
return;
}
$imap = Hm_IMAP_List::connect($server, false);
$folders = $imap->get_mailbox_list_with_subscription();
$results = '<ul class="folders_subscription inner_list">';
foreach ($folders as $folder) {
$padding = substr_count($folder['name'], '/');
$padding = $padding > 0? $padding * 1.3: $padding;
$folder_name = $this->html_safe($folder['id']);
$results .= '<li style="padding-left: '.$padding.'rem" class="imap_'.$server.'_'.$folder_name.'">';
$results .= ' <img class="folder_icon" src="'.Hm_Image_Sources::$folder.'" alt="" width="16" height="16" />';
$results .= $this->html_safe($folder['basename']);
$results .= '<input type="checkbox" value="1" class="folder_subscription" id="'.$folder_name.'" name="'.$folder_name.'" '.($folder['subscribed']? 'checked="checked"': '').' />';
}
$results .= '</ul>';
return $results;
}
}

/**
* @subpackage imap_folders/output
*/
Expand Down Expand Up @@ -600,20 +588,42 @@ protected function output() {
$res .= '</div>';
}
}
class Hm_Output_folders_actions_content_start extends Hm_Output_Module {

class Hm_Output_folders_folder_subscription extends Hm_Output_Module {
protected function output() {
$res = '<div class="folder_actions">';
return $res;
if ($this->get('only_subscribed_folders_setting', 0) && ($server = $this->get('folder_server')) !== NULL) {
$res = '<div class="folder_row"><a href="#" class="subscribe_parent_folder" style="display:none;">';
$res .= $this->trans('Select Folder').'</a><span class="subscribe_parent"></span></div>';
$res .= '<ul class="folders subscribe_parent_folder_select"><li class="suscribe_title"></li></ul>';
$res .= '<input type="hidden" value="" id="subscribe_parent" />';
return $res;
}
}
}

/**
* @subpackage imap_folders/handler
*/
class Hm_Handler_get_only_subscribed_folders_setting extends Hm_Handler_Module {
public function process() {
$this->out('only_subscribed_folders_setting', $this->user_config->get('only_subscribed_folders_setting', 0));
}
}

/**
* @subpackage imap_folders/output
*/
class Hm_Output_folders_actions_content_end extends Hm_Output_Module {
class Hm_Output_folders_folder_subscription_button extends Hm_Output_Module {
protected function output() {
$res = '</div>';
return $res;
if ($this->get('only_subscribed_folders_setting', 0)) {
$server = $this->get('folder_server');
$results = '<div class="folder_subscription_btn"><a href="?page=folders_subscription';
$results .= !is_null($server)? '&imap_server_id='.$server: '';
// $results .= '.
$results .= '" title="'.$this->trans('Folders subscription').'"><img class="account_icon" src="'.$this->html_safe(Hm_Image_Sources::$cog).'" alt="" width="20" height="20" /> ';
$results .= '</a></div>';
return $results;
}
}
}

Expand Down Expand Up @@ -648,6 +658,16 @@ protected function output() {
}
}

/**
* @subpackage imap_folders/output
*/
class Hm_Output_folders_subscription_content_start extends Hm_Output_Module {
protected function output() {
$res = '<div class="content_title">'.$this->trans('Folders subscription').'</div>';
return $res;
}
}

/**
* @subpackage imap_folders/output
*/
Expand All @@ -667,6 +687,23 @@ protected function output() {
}
}

/**
* Option to enable/disable showing only subscribed folders
* @subpackage imap/output
*/
class Hm_Output_imap_only_subscribed_folders_setting extends Hm_Output_Module {
protected function output() {
$checked = '';
$settings = $this->get('user_settings', array());
if (array_key_exists('only_subscribed_folders', $settings) && $settings['only_subscribed_folders']) {
$checked = ' checked="checked"';
}
return '<tr class="general_setting"><td><label for="only_subscribed_folders">'.
$this->trans('Showing subscribed folders only').'</label></td>'.
'<td><input type="checkbox" '.$checked.' id="only_subscribed_folders" name="only_subscribed_folders" value="1" /></td></tr>';
}
}

if (!hm_exists('get_sieve_linked_mailbox')) {
function get_sieve_linked_mailbox ($imap_account, $module) {
if (!$module->module_is_supported('sievefilters') && $module->user_config->get('enable_sieve_filter_setting', true)) {
Expand Down
33 changes: 20 additions & 13 deletions modules/imap_folders/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
add_handler('folders', 'folders_server_id', true, 'imap_folders', 'load_user_data', 'after');
add_handler('folders', 'special_folders', true, 'imap_folders', 'folders_server_id', 'after');
add_output('folders', 'folders_content_start', true, 'imap_folders', 'content_section_start', 'after');
add_output('folders', 'folders_server_select', true, 'imap_folders', 'folders_content_start', 'after');
add_output('folders', 'folders_actions_content_start', true, 'imap_folders', 'folders_folder_subscription', 'after');
add_output('folders', 'folders_server_select', true, 'imap_folders', 'folders_folder_subscription_button', 'after');
add_output('folders', 'folders_create_dialog', true, 'imap_folders', 'folders_server_select', 'after');
add_output('folders', 'folders_rename_dialog', true, 'imap_folders', 'folders_create_dialog', 'after');
add_output('folders', 'folders_delete_dialog', true, 'imap_folders', 'folders_rename_dialog', 'after');
add_output('folders', 'folders_actions_content_end', true, 'imap_folders', 'folders_archive_dialog', 'after');
add_output('folders', 'folders_folder_subscription_button', true, 'imap_folders', 'folders_content_start', 'after');

add_handler('ajax_imap_folder_expand', 'add_folder_manage_link', true, 'imap_folders', 'imap_folder_expand', 'after');
add_handler('folders', 'get_only_subscribed_folders_setting', true, 'imap_folders');

// Commented out during development
add_output('folders', 'folders_trash_dialog', true, 'imap_folders', 'folders_delete_dialog', 'after');
Expand Down Expand Up @@ -54,12 +54,6 @@
add_handler('ajax_imap_folders_create', 'imap_bust_cache', true, 'imap', 'process_folder_create', 'after');
add_handler('ajax_imap_folders_create', 'close_session_early', true, 'core', 'imap_bust_cache', 'after');

setup_base_ajax_page('ajax_imap_folders_create', 'core');
add_handler('ajax_imap_folders_create', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after');
add_handler('ajax_imap_folders_create', 'process_folder_create', true, 'imap_folders', 'load_imap_servers_from_config', 'after');
add_handler('ajax_imap_folders_create', 'imap_bust_cache', true, 'imap', 'process_folder_create', 'after');
add_handler('ajax_imap_folders_create', 'close_session_early', true, 'core', 'imap_bust_cache', 'after');

setup_base_ajax_page('ajax_imap_clear_special_folder', 'core');
add_handler('ajax_imap_clear_special_folder', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after');
add_handler('ajax_imap_clear_special_folder', 'process_clear_special_folder', true, 'imap_folders', 'load_imap_servers_from_config', 'after');
Expand All @@ -75,17 +69,29 @@
add_handler('ajax_imap_accept_special_folders', 'process_accept_special_folders', true, 'imap_folders', 'load_imap_servers_from_config', 'after');
add_handler('ajax_imap_accept_special_folders', 'save_user_data', true, 'core', 'process_special_folders', 'after');

add_handler('ajax_hm_folders', 'imap_folder_check', true, 'imap_folders', 'load_user_data', 'after');
add_output('ajax_hm_folders', 'folders_page_link', true, 'imap_folders', 'settings_menu_end', 'before');

add_handler('settings', 'process_only_subscribed_folders_setting', true, 'imap', 'date', 'after');
add_output('settings', 'imap_only_subscribed_folders_setting', true, 'imap', 'original_folder_setting', 'after');

setup_base_page('folders_subscription', 'core');
add_handler('folders_subscription', 'folders_server_id', true, 'imap_folders', 'load_user_data', 'after');
add_handler('folders_subscription', 'special_folders', true, 'imap_folders', 'folders_server_id', 'after');
add_handler('folders_subscription', 'get_only_subscribed_folders_setting', true, 'imap_folders');
add_output('folders_subscription', 'folders_subscription_content_start', true, 'imap_folders', 'content_section_start', 'after');
add_output('folders_subscription', 'folders_server_select', true, 'imap_folders', 'folders_subscription_content_start', 'after');
add_output('folders_subscription', 'folders_folder_subscription', true, 'imap_folders', 'folders_server_select', 'after');

setup_base_ajax_page('ajax_imap_folder_subscription', 'core');
add_handler('ajax_imap_folder_subscription', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after');
add_handler('ajax_imap_folder_subscription', 'process_imap_folder_subscription', true, 'imap_folders', 'load_imap_servers_from_config', 'after');
add_handler('ajax_imap_folder_subscription', 'save_user_data', true, 'core', 'process_special_folder', 'after');

add_handler('ajax_hm_folders', 'imap_folder_check', true, 'imap_folders', 'load_user_data', 'after');
add_output('ajax_hm_folders', 'folders_page_link', true, 'imap_folders', 'settings_menu_end', 'before');

return array(
'allowed_pages' => array(
'folders',
'folders_subscription',
'ajax_imap_folders_delete',
'ajax_imap_folders_create',
'ajax_imap_folders_rename',
Expand All @@ -106,6 +112,7 @@
'special_folder_type' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'imap_service_name' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
'subscription_state' => FILTER_VALIDATE_BOOLEAN,
'folder' => FILTER_VALIDATE_BOOLEAN
'folder' => FILTER_VALIDATE_BOOLEAN,
'only_subscribed_folders' => FILTER_VALIDATE_BOOLEAN,
)
);
6 changes: 2 additions & 4 deletions modules/imap_folders/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
.manage_folder_icon { vertical-align: -3px; opacity: .3 }
.manage_folder_link { color: #999 !important; }

.folders_subscription { width: 100%; display: inline-block; }
.folder_subscription_btn { position: absolute; right: 20px; top: 20px; }
.folders_subscription { padding-left: 10px; width: 100%; display: inline-block; }
.folders_subscription input { float: right; }
.folder_actions { width: 100%; display: inline-block; vertical-align: top; margin-left: 1%; }

@media (min-width: 687px) {
.folders_subscription { width: 30% }
.folder_actions { width: 65%; }
}

@media (min-width: 1201px) {
.folders_subscription { width: 25% }
.folder_actions { width: 72%; }
}
Loading

0 comments on commit 4d2ab60

Please sign in to comment.