+ return $joins;
+ }
+
+ /** EMAILS
+ */
+ private $ra = array();
+ /** Allows filtering by redirection.
+ * @param $email If null, enable a left join on the email redirection table
+ * (email_redirect_account); otherwise, perform a left join on users having
+ * that email as a redirection.
+ * @return Suffix to use to access the adequate table.
+ */
+ public function addEmailRedirectFilter($email = null)
+ {
+ $this->requireAccounts();
+ return $this->register_optional($this->ra, $email);
+ }
+
+ const ALIAS_BEST = 'bestalias';
+ const ALIAS_FORLIFE = 'forlife';
+ const ALIAS_AUXILIARY = 'alias_aux';
+ private $sa = array();
+ /** Allows filtering by source email.
+ * @param $email If null, enable a left join on the email source table
+ * (email_source_account); otherwise, perform a left join on users having
+ * that email as a source email.
+ * @return Suffix to use to access the adequate table.
+ */
+ public function addAliasFilter($email = null)
+ {
+ $this->requireAccounts();
+ return $this->register_optional($this->sa, $email);
+ }
+
+ private $with_rf = false;
+ /** Allows filtering by active redirection.
+ * @return Suffix to use to access the adequate table.
+ */
+ public function addActiveEmailRedirectFilter($email = null)
+ {
+ $this->requireAccounts();
+ $this->with_rf = true;
+ }
+
+ protected function emailJoins()
+ {
+ global $globals;
+ $joins = array();
+ foreach ($this->ra as $sub => $redirections) {
+ if (is_null($redirections)) {
+ $joins['ra' . $sub] = PlSqlJoin::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\'');
+ } else {
+ if (!is_array($redirections)) {
+ $key = array($redirections);
+ }
+ $joins['ra' . $sub] = PlSqlJoin::left('email_redirect_account', '$ME.uid = $UID AND $ME.type != \'imap\'
+ AND $ME.redirect IN {?}', $redirections);
+ }
+ }
+ foreach ($this->sa as $sub => $emails) {
+ if (is_null($emails)) {
+ $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID');
+ } else if ($sub == self::ALIAS_BEST) {
+ $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND FIND_IN_SET(\'bestalias\', $ME.flags)');
+ } else if ($sub == self::ALIAS_FORLIFE) {
+ $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'forlife\'');
+ } else if ($sub == self::ALIAS_AUXILIARY) {
+ $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.type = \'alias_aux\'');
+ } else {
+ if (!is_array($emails)) {
+ $key = array($emails);
+ }
+ $joins['sa' . $sub] = PlSqlJoin::left('email_source_account', '$ME.uid = $UID AND $ME.email IN {?}', $emails);