Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phortune/query/PhortuneMerchantQuery.php
Show First 20 Lines • Show All 80 Lines • ▼ Show 20 Lines | final class PhortuneMerchantQuery | ||||
} | } | ||||
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = parent::buildWhereClauseParts($conn); | $where = parent::buildWhereClauseParts($conn); | ||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'id IN (%Ld)', | 'merchant.id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
if ($this->phids !== null) { | if ($this->phids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'phid IN (%Ls)', | 'merchant.phid IN (%Ls)', | ||||
$this->phids); | $this->phids); | ||||
} | } | ||||
if ($this->memberPHIDs !== null) { | if ($this->memberPHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn, | $conn, | ||||
'e.dst IN (%Ls)', | 'e.dst IN (%Ls)', | ||||
$this->memberPHIDs); | $this->memberPHIDs); | ||||
} | } | ||||
return $where; | return $where; | ||||
} | } | ||||
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | ||||
$joins = parent::buildJoinClauseParts($conn); | $joins = parent::buildJoinClauseParts($conn); | ||||
if ($this->memberPHIDs !== null) { | if ($this->memberPHIDs !== null) { | ||||
$joins[] = qsprintf( | $joins[] = qsprintf( | ||||
$conn, | $conn, | ||||
'LEFT JOIN %T e ON m.phid = e.src AND e.type = %d', | 'LEFT JOIN %T e ON merchant.phid = e.src AND e.type = %d', | ||||
PhabricatorEdgeConfig::TABLE_NAME_EDGE, | PhabricatorEdgeConfig::TABLE_NAME_EDGE, | ||||
PhortuneMerchantHasMemberEdgeType::EDGECONST); | PhortuneMerchantHasMemberEdgeType::EDGECONST); | ||||
} | } | ||||
return $joins; | return $joins; | ||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorPhortuneApplication'; | return 'PhabricatorPhortuneApplication'; | ||||
} | } | ||||
protected function getPrimaryTableAlias() { | protected function getPrimaryTableAlias() { | ||||
return 'm'; | return 'merchant'; | ||||
} | |||||
public static function canViewersEditMerchants( | |||||
array $viewer_phids, | |||||
array $merchant_phids) { | |||||
// See T13366 for some discussion. This is an unusual caching construct to | |||||
// make policy filtering of Accounts easier. | |||||
foreach ($viewer_phids as $key => $viewer_phid) { | |||||
if (!$viewer_phid) { | |||||
unset($viewer_phids[$key]); | |||||
} | |||||
} | |||||
if (!$viewer_phids) { | |||||
return array(); | |||||
} | |||||
$cache_key = 'phortune.merchant.can-edit'; | |||||
$cache = PhabricatorCaches::getRequestCache(); | |||||
$cache_data = $cache->getKey($cache_key); | |||||
if (!$cache_data) { | |||||
$cache_data = array(); | |||||
} | |||||
$load_phids = array(); | |||||
foreach ($viewer_phids as $viewer_phid) { | |||||
if (!isset($cache_data[$viewer_phid])) { | |||||
$load_phids[] = $viewer_phid; | |||||
} | |||||
} | |||||
$did_write = false; | |||||
foreach ($load_phids as $load_phid) { | |||||
$merchants = id(new self()) | |||||
->setViewer(PhabricatorUser::getOmnipotentUser()) | |||||
->withMemberPHIDs(array($load_phid)) | |||||
->execute(); | |||||
foreach ($merchants as $merchant) { | |||||
$cache_data[$load_phid][$merchant->getPHID()] = true; | |||||
$did_write = true; | |||||
} | |||||
} | |||||
if ($did_write) { | |||||
$cache->setKey($cache_key, $cache_data); | |||||
} | |||||
$results = array(); | |||||
foreach ($viewer_phids as $viewer_phid) { | |||||
foreach ($merchant_phids as $merchant_phid) { | |||||
if (!isset($cache_data[$viewer_phid][$merchant_phid])) { | |||||
continue; | |||||
} | |||||
$results[$viewer_phid][$merchant_phid] = true; | |||||
} | |||||
} | |||||
return $results; | |||||
} | } | ||||
} | } |