diff -u b/includes/mail.inc b/includes/mail.inc --- b/includes/mail.inc +++ b/includes/mail.inc @@ -249,100 +249,68 @@ } } - // Retrieve list of users being globally subscribed to all issues. - $accounts_all_issues = array(); - $result = db_query("SELECT pisu.uid, u.name, u.mail FROM {project_issue_subscriptions_user} pisu INNER JOIN {users} u ON pisu.uid = u.uid WHERE u.status = 1 AND pisu.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_ALL); - while ($account = db_fetch_object($result)) { - $accounts_all_issues[$account->uid] = $account; - } - // Check whether Flag module integration is enabled for e-mail notifications. $flag_integration = (module_exists('flag') && variable_get('project_issue_subscription_flag', 0)); - // Retrieve list of users being globally subscribed to "own" issues (without - // Flag integration). - $accounts_flagged_issues = array(); - if (!$flag_integration) { - $result = db_query("SELECT pisu.uid, u.name, u.mail FROM {project_issue_subscriptions_user} pisu INNER JOIN {users} u ON pisu.uid = u.uid WHERE u.status = 1 AND pisu.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_FLAGGED); - while ($account = db_fetch_object($result)) { - $accounts_flagged_issues[$account->uid] = $account; - } + // If flag integration is present, the list of users for 'own' issues will + // be those that flagged the issue. + if ($flag_integration) { + // Retrieve all users who flagged the issue. + $flag_contents = flag_get_content_flags('node', $node->nid, variable_get('project_issue_subscription_flag', 0)); + $own_issues_uids = array_keys($flag_contents); + } + // Without flag integration, 'own issues' are all users that posted to the + // issue. + else { + $own_issues_uids = array_keys($uids); } - // Check per-project subscriptions of involved users. - if (!empty($uids)) { - $placeholders = implode(',', array_fill(0, count($uids), '%d')); - $args = $uids; - array_unshift($args, $node->project_issue['pid']); - - // Check which involved users are subscribed to all issues of the project. - $result = db_query("SELECT pisp.uid, u.name, u.mail - FROM {project_issue_subscriptions_project} pisp - INNER JOIN {users} u ON pisp.uid = u.uid - WHERE u.status = 1 AND pisp.nid = %d AND pisp.uid IN ($placeholders) AND pisp.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_ALL, $args); - while ($account = db_fetch_object($result)) { - $accounts_all_issues[$account->uid] = $account; - } - // Check which involved users are subscribed to "own" issues of the project - // (without Flag integration). - if (!$flag_integration) { - $result = db_query("SELECT pisp.uid, u.name, u.mail - FROM {project_issue_subscriptions_project} pisp - INNER JOIN {users} u ON pisp.uid = u.uid - WHERE u.status = 1 AND pisp.nid = %d AND pisp.uid IN ($placeholders) AND pisp.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_FLAGGED, $args); - while ($account = db_fetch_object($result)) { - $accounts_flagged_issues[$account->uid] = $account; - } - } + // Initialize the recipient list. After compilation, the list will include + // users that are: + // - globally subscribed to all issues + // - globally subscribed to own issues (without Flag integration) + // - globally subscribed to flagged issues + // - subscribed to all issues of the project + // - subscribed to own issues of the project (without Flag integration) + // - subscribed to flagged issues of the project + $recipients = array(); + + $args = array(); + + // Build up filters for different issue subscription levels. + $filter_array = array(); + // 'All' level. + $filter_array[] = "(pi.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_ALL . ")"; + // 'Own' level using the list of users attached to the issue. + if (!empty($own_issues_uids)) { + $placeholders = implode(',', array_fill(0, count($own_issues_uids), '%d')); + $filter_array[] = "(pi.uid IN ($placeholders) AND pi.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_FLAGGED . ")"; + $args = array_merge($args, $own_issues_uids); } + $filter = implode(" OR ", $filter_array); - // Check which users subscribed to the issue via Flag. - if ($flag_integration) { - // Retrieve all users who flagged the issue. - $flag_contents = flag_get_content_flags('node', $node->nid, variable_get('project_issue_subscription_flag', 0)); - - // Now use the list of users that flagged the issue to retrieve the list of - // users who are subscribed to flagged issues of the project. For example, a - // user who globally subscribed to no issues should not get an e-mail - // notification after flagging the issue. That should only happen when the - // user subscribed to flagged issues for the project. - if (!empty($flag_contents)) { - $flags_uids = array_keys($flag_contents); - $placeholders = implode(',', array_fill(0, count($flags_uids), '%d')); - $args = $flags_uids; - - // Check which users flagged the issue and globally subscribed to flagged - // issues (across projects). - $result = db_query("SELECT pisu.uid, u.name, u.mail - FROM {project_issue_subscriptions_user} pisu - INNER JOIN {users} u ON pisu.uid = u.uid - WHERE u.status = 1 AND pisu.uid IN ($placeholders) AND pisu.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_FLAGGED, $args); - while ($account = db_fetch_object($result)) { - $accounts_flagged_issues[$account->uid] = $account; - } - - // Check which users flagged the issue and subscribed to flagged issues of - // the project. - array_unshift($args, $node->project_issue['pid']); - $result = db_query("SELECT pisp.uid, u.name, u.mail - FROM {project_issue_subscriptions_project} pisp - INNER JOIN {users} u ON pisp.uid = u.uid - WHERE u.status = 1 AND pisp.nid = %d AND pisp.uid IN ($placeholders) AND pisp.level = " . PROJECT_ISSUE_SUBSCRIPTIONS_FLAGGED, $args); - while ($account = db_fetch_object($result)) { - $accounts_flagged_issues[$account->uid] = $account; - } - } + // Pull subscribers at the user level. + $user_levels_query = "SELECT pi.uid, u.name, u.mail + FROM {project_issue_subscriptions_user} pi + INNER JOIN {users} u ON pi.uid = u.uid + WHERE u.status = 1 AND (" . $filter . ")"; + $result = db_query($user_levels_query, $args); + while ($account = db_fetch_object($result)) { + $recipients[$account->uid] = $account; } - // Lastly, join the lists of users being - // - globally subscribed to all issues - // - globally subscribed to own issues (without Flag integration) - // - globally subscribed to flagged issues - // - subscribed to all issues of the project - // - subscribed to own issues of the project (without Flag integration) - // - subscribed to flagged issues of the project - $recipients = $accounts_all_issues + $accounts_flagged_issues; + // Pull subscribers at the project level. + $project_levels_query = "SELECT pi.uid, u.name, u.mail + FROM {project_issue_subscriptions_project} pi + INNER JOIN {users} u ON pi.uid = u.uid + WHERE u.status = 1 AND pi.nid = %d AND (" . $filter . ")"; + $project_levels_args = $args; + array_unshift($project_levels_args, $node->project_issue['pid']); + $result = db_query($project_levels_query, $project_levels_args); + while ($account = db_fetch_object($result)) { + $recipients[$account->uid] = $account; + } // To save workload, check here if either the anonymous role or the // authenticated role has the 'view uploaded files' permission, since