Use switches instead of if/else in Module\Search\Acl

This commit is contained in:
Hypolite Petovan 2019-10-04 22:27:13 -04:00
parent 9e29321aaf
commit 908197cc70

View file

@ -116,45 +116,55 @@ class Acl extends BaseModule
$sql_extra2 .= ' ' . Widget::unavailableNetworks(); $sql_extra2 .= ' ' . Widget::unavailableNetworks();
$contact_count = 0; $contact_count = 0;
if ($type == self::TYPE_MENTION_CONTACT_GROUP || $type == self::TYPE_MENTION_CONTACT) { switch ($type) {
// autocomplete for editor mentions case self::TYPE_MENTION_CONTACT_GROUP:
$r = q("SELECT COUNT(*) AS c FROM `contact` case self::TYPE_MENTION_CONTACT:
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` // autocomplete for editor mentions
AND NOT `blocked` AND NOT `pending` AND NOT `archive` $r = q("SELECT COUNT(*) AS c FROM `contact`
AND `notify` != '' $sql_extra2", WHERE `uid` = %d AND NOT `self` AND NOT `deleted`
intval(local_user()) AND NOT `blocked` AND NOT `pending` AND NOT `archive`
); AND `notify` != '' $sql_extra2",
$contact_count = (int) $r[0]['c']; intval(local_user())
} elseif ($type == self::TYPE_MENTION_FORUM) { );
// autocomplete for editor mentions of forums $contact_count = (int) $r[0]['c'];
$r = q("SELECT COUNT(*) AS c FROM `contact` break;
WHERE `uid` = %d AND NOT `self` AND NOT `deleted`
AND NOT `blocked` AND NOT `pending` AND NOT `archive` case self::TYPE_MENTION_FORUM:
AND (`forum` OR `prv`) // autocomplete for editor mentions of forums
AND `notify` != '' $sql_extra2", $r = q("SELECT COUNT(*) AS c FROM `contact`
intval(local_user()) WHERE `uid` = %d AND NOT `self` AND NOT `deleted`
); AND NOT `blocked` AND NOT `pending` AND NOT `archive`
$contact_count = (int) $r[0]['c']; AND (`forum` OR `prv`)
} elseif ($type == self::TYPE_PRIVATE_MESSAGE) { AND `notify` != '' $sql_extra2",
// autocomplete for Private Messages intval(local_user())
$r = q("SELECT COUNT(*) AS c FROM `contact` );
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` $contact_count = (int) $r[0]['c'];
AND NOT `blocked` AND NOT `pending` AND NOT `archive` break;
AND `network` IN ('%s', '%s', '%s') $sql_extra2",
intval(local_user()), case self::TYPE_PRIVATE_MESSAGE:
DBA::escape(Protocol::ACTIVITYPUB), // autocomplete for Private Messages
DBA::escape(Protocol::DFRN), $r = q("SELECT COUNT(*) AS c FROM `contact`
DBA::escape(Protocol::DIASPORA) WHERE `uid` = %d AND NOT `self` AND NOT `deleted`
); AND NOT `blocked` AND NOT `pending` AND NOT `archive`
$contact_count = (int) $r[0]['c']; AND `network` IN ('%s', '%s', '%s') $sql_extra2",
} elseif ($type == self::TYPE_ANY_CONTACT) { intval(local_user()),
// autocomplete for Contacts DBA::escape(Protocol::ACTIVITYPUB),
$r = q("SELECT COUNT(*) AS c FROM `contact` DBA::escape(Protocol::DFRN),
WHERE `uid` = %d AND NOT `self` DBA::escape(Protocol::DIASPORA)
AND NOT `pending` AND NOT `deleted` $sql_extra2", );
intval(local_user()) $contact_count = (int) $r[0]['c'];
); break;
$contact_count = (int) $r[0]['c'];
case self::TYPE_ANY_CONTACT:
default:
// autocomplete for Contacts
$r = q("SELECT COUNT(*) AS c FROM `contact`
WHERE `uid` = %d AND NOT `self`
AND NOT `pending` AND NOT `deleted` $sql_extra2",
intval(local_user())
);
$contact_count = (int) $r[0]['c'];
break;
} }
$tot = $group_count + $contact_count; $tot = $group_count + $contact_count;
@ -195,53 +205,64 @@ class Acl extends BaseModule
} }
$r = []; $r = [];
if ($type == self::TYPE_MENTION_CONTACT_GROUP) { switch ($type) {
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact` case self::TYPE_MENTION_CONTACT_GROUP:
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != '' $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv`, (`prv` OR `forum`) AS `frm` FROM `contact`
AND NOT (`network` IN ('%s', '%s')) WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
$sql_extra2 AND NOT (`network` IN ('%s', '%s'))
ORDER BY `name`", $sql_extra2
intval(local_user()), ORDER BY `name`",
DBA::escape(Protocol::OSTATUS), intval(local_user()),
DBA::escape(Protocol::STATUSNET) DBA::escape(Protocol::OSTATUS),
); DBA::escape(Protocol::STATUSNET)
} elseif ($type == self::TYPE_MENTION_CONTACT) { );
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact` break;
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
AND NOT (`network` IN ('%s')) case self::TYPE_MENTION_CONTACT:
$sql_extra2 $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
ORDER BY `name`", WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
intval(local_user()), AND NOT (`network` IN ('%s'))
DBA::escape(Protocol::STATUSNET) $sql_extra2
); ORDER BY `name`",
} elseif ($type == self::TYPE_MENTION_FORUM) { intval(local_user()),
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact` DBA::escape(Protocol::STATUSNET)
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != '' );
AND NOT (`network` IN ('%s')) break;
AND (`forum` OR `prv`)
$sql_extra2 case self::TYPE_MENTION_FORUM:
ORDER BY `name`", $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
intval(local_user()), WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `notify` != ''
DBA::escape(Protocol::STATUSNET) AND NOT (`network` IN ('%s'))
); AND (`forum` OR `prv`)
} elseif ($type == self::TYPE_PRIVATE_MESSAGE) { $sql_extra2
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact` ORDER BY `name`",
WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive` intval(local_user()),
AND `network` IN ('%s', '%s', '%s') DBA::escape(Protocol::STATUSNET)
$sql_extra2 );
ORDER BY `name`", break;
intval(local_user()),
DBA::escape(Protocol::ACTIVITYPUB), case self::TYPE_PRIVATE_MESSAGE:
DBA::escape(Protocol::DFRN), $r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr` FROM `contact`
DBA::escape(Protocol::DIASPORA) WHERE `uid` = %d AND NOT `self` AND NOT `deleted` AND NOT `blocked` AND NOT `pending` AND NOT `archive`
); AND `network` IN ('%s', '%s', '%s')
} elseif ($type == self::TYPE_ANY_CONTACT) { $sql_extra2
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact` ORDER BY `name`",
WHERE `uid` = %d AND NOT `deleted` AND NOT `pending` AND NOT `archive` intval(local_user()),
$sql_extra2 DBA::escape(Protocol::ACTIVITYPUB),
ORDER BY `name`", DBA::escape(Protocol::DFRN),
intval(local_user()) DBA::escape(Protocol::DIASPORA)
); );
break;
case self::TYPE_ANY_CONTACT:
default:
$r = q("SELECT `id`, `name`, `nick`, `micro`, `network`, `url`, `attag`, `addr`, `forum`, `prv` FROM `contact`
WHERE `uid` = %d AND NOT `deleted` AND NOT `pending` AND NOT `archive`
$sql_extra2
ORDER BY `name`",
intval(local_user())
);
break;
} }
if (DBA::isResult($r)) { if (DBA::isResult($r)) {