Skip to content

Commit ba981f7

Browse files
authored
Merge pull request #1749 from mercihabam/fix-tags-state
fix(frontend/backend): show active tags checked when displaying a message and ensure that unchecking the input, removes the tag from the message
2 parents 6f46258 + bcdbd83 commit ba981f7

File tree

6 files changed

+87
-14
lines changed

6 files changed

+87
-14
lines changed

modules/imap/functions.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,16 +1512,18 @@ function snooze_dropdown($output, $unsnooze = false) {
15121512
}}
15131513

15141514
if (!hm_exists('tags_dropdown')) {
1515-
function tags_dropdown($context, $headers) {
1515+
function tags_dropdown($context) {
1516+
$msgUid = $context->get('msg_text_uid');
1517+
$msgTags = Hm_Tags::getTagIdsWithMessage($msgUid);
1518+
15161519
$folders = $context->get('tags', array());
15171520
$txt = '<div class="dropdown d-inline-block">
1518-
<a class="hlink text-decoration-none btn btn-sm btn-outline-secondary dropdown-toggle" id="dropdownMenuTag" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="true">'.$context->trans('Tags').'</a>
1521+
<a class="hlink text-decoration-none btn btn-sm btn-outline-secondary dropdown-toggle" id="dropdownMenuTag" data-bs-toggle="dropdown" aria-haspopup="true" href="#" aria-expanded="true">'.$context->trans('Tags').'</a>
15191522
<ul class="dropdown-menu" aria-labelledby="dropdownMenuTag">';
15201523

1521-
$tags = !empty($headers['X-Cypht-Tags']) ? explode(',', $headers['X-Cypht-Tags']) : array();
15221524
foreach ($folders as $folder) {
15231525
$tag = $folder['name'];
1524-
$is_checked = in_array($folder['id'], array_map('trim', $tags));
1526+
$is_checked = in_array($folder['id'], $msgTags);
15251527
$txt .= '<li class="d-flex dropdown-item gap-2">';
15261528
$txt .= '<input class="form-check-input me-1 label-checkbox" type="checkbox" value="" aria-label="..." data-id="'.$folder['id'].'" '.($is_checked ? 'checked' : '').'>';
15271529
$txt .= '<span>'.$context->trans($tag).'</span>';

modules/imap/output_modules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ protected function output() {
390390
}
391391

392392
if($this->get('tags')){
393-
$txt .= tags_dropdown($this, $headers);
393+
$txt .= tags_dropdown($this);
394394
}
395395
if (isset($lc_headers['x-schedule'])) {
396396
$txt .= schedule_dropdown($this, true);

modules/imap/site.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,15 +660,16 @@ var get_message_content = function(msg_part, uid, list_path, listParent, detail,
660660
$('.prev, .next').hide();
661661
}
662662
globals.auto_advance_email_enabled = Boolean(res.auto_advance_email_enabled);
663+
664+
if (callback) {
665+
callback(res)
666+
}
663667
};
664668

665669
if (!msg_part) {
666670
var msgContent = get_local_message_content(uid, list_path);
667671
if (msgContent) {
668672
onSuccess(msgContent);
669-
if (callback) {
670-
callback(msgContent)
671-
}
672673
}
673674
}
674675

@@ -702,8 +703,7 @@ var get_message_content = function(msg_part, uid, list_path, listParent, detail,
702703
}
703704
},
704705
[],
705-
false,
706-
callback
706+
false
707707
);
708708
}
709709
}
@@ -1143,7 +1143,20 @@ function imap_setup_tags() {
11431143
Hm_Ajax.request(
11441144
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_tag'},
11451145
{'name': 'tag_id', 'value': folder_id},
1146-
{'name': 'list_path', 'value': ids}]
1146+
{'name': 'list_path', 'value': ids},
1147+
{'name': 'untag', 'value': !$(this).is(':checked')},
1148+
{'name': 'tag', 'value': $(this).is(':checked')}],
1149+
function() {
1150+
if (getPageNameParam() == 'message_list') {
1151+
// remove cached message content so that tag changes are reflected
1152+
ids.forEach((id) => {
1153+
const uid = id.split('_')[1];
1154+
Hm_Utils.remove_from_local_storage(getMessageStorageKey(uid));
1155+
});
1156+
} else if (getPageNameParam() == 'message') {
1157+
set_message_content();
1158+
}
1159+
}
11471160
);
11481161
});
11491162
}

modules/tags/handler_modules.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Hm_Handler_add_tag_to_message extends Hm_Handler_Module {
2323
* Use IMAP to tag the selected message uid
2424
*/
2525
public function process() {
26-
list($success, $form) = $this->process_form(array('tag_id', 'list_path'));
27-
if (!$success) {
26+
list($success, $form) = $this->process_form(array('tag_id', 'list_path', 'tag'));
27+
if (!$success || !isset($form['tag']) || $form['tag'] != true) {
2828
return;
2929
}
3030

@@ -53,6 +53,45 @@ public function process() {
5353
}
5454
}
5555

56+
/**
57+
* Remove tag/label from message
58+
* @subpackage imap/handler
59+
*/
60+
class Hm_Handler_remove_tag_from_message extends Hm_Handler_Module {
61+
/**
62+
* Use IMAP to remove tag from the selected message uid
63+
*/
64+
public function process() {
65+
list($success, $form) = $this->process_form(array('tag_id', 'list_path', 'untag'));
66+
if (!$success || !isset($form['untag']) || $form['untag'] != true) {
67+
return;
68+
}
69+
70+
$untaged_messages = 0;
71+
$ids = explode(',', $form['list_path']);
72+
foreach ($ids as $msg_part) {
73+
list($imap_server_id, $msg_id, $folder) = explode('_', $msg_part);
74+
$folder = hex2bin($folder);
75+
$untagged = Hm_Tags::removeMessage($msg_id, $form['tag_id']);
76+
if ($untagged) {
77+
$untaged_messages++;
78+
}
79+
}
80+
$this->out('untaged_messages', $untaged_messages);
81+
$type = 'success';
82+
if ($untaged_messages == count($ids)) {
83+
$msg = 'Tag removed';
84+
} elseif ($untaged_messages > 0) {
85+
$msg = 'Messages have been untaged';
86+
$type = 'warning';
87+
} else {
88+
$msg = 'ERRFailed to remove tag from selected messages';
89+
$type = 'danger';
90+
}
91+
Hm_Msgs::add($msg, $type);
92+
}
93+
}
94+
5695
/**
5796
* @subpackage tags/handler
5897
*/

modules/tags/hm-tags.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ public static function addMessage($tagId, $serverId, $folder, $messageId) {
3737
return self::edit($tagId, $tag);
3838
}
3939

40+
public static function removeMessage($messageId, $tagId) {
41+
$tag = self::get($tagId);
42+
if (!$tag) {
43+
return false;
44+
}
45+
foreach ($tag['server'] as $serverId => $folders) {
46+
foreach ($folders as $folder => $messages) {
47+
$newMessages = array_filter($messages, function($msgId) use ($messageId) {
48+
return $msgId != $messageId;
49+
});
50+
$tag['server'][$serverId][$folder] = $newMessages;
51+
}
52+
}
53+
return self::edit($tagId, $tag);
54+
}
55+
4056
public static function registerFolder($tag_id, $serverId, $folder) {
4157
$tag = self::get($tag_id);
4258
if (! isset($tag['server'][$serverId][$folder])) {
@@ -53,7 +69,7 @@ public static function getFolders($tag_id, $serverId) {
5369
return [];
5470
}
5571

56-
private static function getTagIdsWithMessage($messageId) {
72+
public static function getTagIdsWithMessage($messageId) {
5773
$tags = self::getAll();
5874
$tagIds = [];
5975
foreach ($tags as $tagId => $tag) {

modules/tags/setup.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
add_handler('ajax_imap_tag', 'close_session_early', true, 'core');
4848
add_handler('ajax_imap_tag', 'tag_data', true, 'tags', 'load_user_data', 'after');
4949
add_handler('ajax_imap_tag', 'add_tag_to_message', true, 'tags', 'save_imap_cache', 'after');
50+
add_handler('ajax_imap_tag', 'remove_tag_from_message', true, 'tags', 'save_imap_cache', 'after');
5051

5152
/* Sync the Tags Repository when moving messages */
5253
add_handler('ajax_imap_move_copy_action', 'tag_data', true, 'tags', 'load_user_data', 'after');
@@ -68,6 +69,8 @@
6869
'tag_delete' => FILTER_UNSAFE_RAW,
6970
'tag_per_source' => FILTER_VALIDATE_INT,
7071
'tag_since' => FILTER_UNSAFE_RAW,
72+
'untag' => FILTER_VALIDATE_BOOLEAN,
73+
'tag' => FILTER_VALIDATE_BOOLEAN,
7174
),
7275
'allowed_get' => array(
7376
'tag_id' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,

0 commit comments

Comments
 (0)