[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/flag/query/ -> PhabricatorFlagQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorFlagQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    const GROUP_COLOR = 'color';
   7    const GROUP_NONE  = 'none';
   8  
   9    private $ownerPHIDs;
  10    private $types;
  11    private $objectPHIDs;
  12    private $colors;
  13    private $groupBy = self::GROUP_NONE;
  14  
  15    private $needHandles;
  16    private $needObjects;
  17  
  18    public function withOwnerPHIDs(array $owner_phids) {
  19      $this->ownerPHIDs = $owner_phids;
  20      return $this;
  21    }
  22  
  23    public function withTypes(array $types) {
  24      $this->types = $types;
  25      return $this;
  26    }
  27  
  28    public function withObjectPHIDs(array $object_phids) {
  29      $this->objectPHIDs = $object_phids;
  30      return $this;
  31    }
  32  
  33    public function withColors(array $colors) {
  34      $this->colors = $colors;
  35      return $this;
  36    }
  37  
  38    /**
  39     * NOTE: this is done in PHP and not in MySQL, which means its inappropriate
  40     * for large datasets. Pragmatically, this is fine for user flags which are
  41     * typically well under 100 flags per user.
  42     */
  43    public function setGroupBy($group) {
  44      $this->groupBy = $group;
  45      return $this;
  46    }
  47  
  48    public function needHandles($need) {
  49      $this->needHandles = $need;
  50      return $this;
  51    }
  52  
  53    public function needObjects($need) {
  54      $this->needObjects = $need;
  55      return $this;
  56    }
  57  
  58    public static function loadUserFlag(PhabricatorUser $user, $object_phid) {
  59      // Specifying the type in the query allows us to use a key.
  60      return id(new PhabricatorFlagQuery())
  61        ->setViewer($user)
  62        ->withOwnerPHIDs(array($user->getPHID()))
  63        ->withTypes(array(phid_get_type($object_phid)))
  64        ->withObjectPHIDs(array($object_phid))
  65        ->executeOne();
  66    }
  67  
  68    public function loadPage() {
  69      $table = new PhabricatorFlag();
  70      $conn_r = $table->establishConnection('r');
  71  
  72      $data = queryfx_all(
  73        $conn_r,
  74        'SELECT * FROM %T flag %Q %Q %Q',
  75        $table->getTableName(),
  76        $this->buildWhereClause($conn_r),
  77        $this->buildOrderClause($conn_r),
  78        $this->buildLimitClause($conn_r));
  79  
  80      return $table->loadAllFromArray($data);
  81    }
  82  
  83    public function willFilterPage(array $flags) {
  84      if ($this->needObjects) {
  85        $objects = id(new PhabricatorObjectQuery())
  86          ->setViewer($this->getViewer())
  87          ->withPHIDs(mpull($flags, 'getObjectPHID'))
  88          ->execute();
  89        $objects = mpull($objects, null, 'getPHID');
  90        foreach ($flags as $key => $flag) {
  91          $object = idx($objects, $flag->getObjectPHID());
  92          if ($object) {
  93            $flags[$key]->attachObject($object);
  94          } else {
  95            unset($flags[$key]);
  96          }
  97        }
  98      }
  99  
 100      if ($this->needHandles) {
 101        $handles = id(new PhabricatorHandleQuery())
 102          ->setViewer($this->getViewer())
 103          ->withPHIDs(mpull($flags, 'getObjectPHID'))
 104          ->execute();
 105  
 106        foreach ($flags as $flag) {
 107          $flag->attachHandle($handles[$flag->getObjectPHID()]);
 108        }
 109      }
 110  
 111      switch ($this->groupBy) {
 112        case self::GROUP_COLOR:
 113          $flags = msort($flags, 'getColor');
 114          break;
 115        case self::GROUP_NONE:
 116          break;
 117        default:
 118          throw new Exception("Unknown groupBy parameter: $this->groupBy");
 119          break;
 120      }
 121  
 122      return $flags;
 123    }
 124  
 125    private function buildWhereClause($conn_r) {
 126      $where = array();
 127  
 128      if ($this->ownerPHIDs) {
 129        $where[] = qsprintf(
 130          $conn_r,
 131          'flag.ownerPHID IN (%Ls)',
 132          $this->ownerPHIDs);
 133      }
 134  
 135      if ($this->types) {
 136        $where[] = qsprintf(
 137          $conn_r,
 138          'flag.type IN (%Ls)',
 139          $this->types);
 140      }
 141  
 142      if ($this->objectPHIDs) {
 143        $where[] = qsprintf(
 144          $conn_r,
 145          'flag.objectPHID IN (%Ls)',
 146          $this->objectPHIDs);
 147      }
 148  
 149      if ($this->colors) {
 150        $where[] = qsprintf(
 151          $conn_r,
 152          'flag.color IN (%Ld)',
 153          $this->colors);
 154      }
 155  
 156      $where[] = $this->buildPagingClause($conn_r);
 157  
 158      return $this->formatWhereClause($where);
 159    }
 160  
 161    public function getQueryApplicationClass() {
 162      return 'PhabricatorFlagsApplication';
 163    }
 164  
 165  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1