[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/macro/query/ -> PhabricatorMacroQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorMacroQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $authors;
   9    private $names;
  10    private $nameLike;
  11    private $dateCreatedAfter;
  12    private $dateCreatedBefore;
  13    private $flagColor;
  14  
  15    private $needFiles;
  16  
  17    private $status = 'status-any';
  18    const STATUS_ANY = 'status-any';
  19    const STATUS_ACTIVE = 'status-active';
  20    const STATUS_DISABLED = 'status-disabled';
  21  
  22    public static function getStatusOptions() {
  23      return array(
  24        self::STATUS_ACTIVE   => pht('Active Macros'),
  25        self::STATUS_DISABLED => pht('Disabled Macros'),
  26        self::STATUS_ANY      => pht('Active and Disabled Macros'),
  27      );
  28    }
  29  
  30    public static function getFlagColorsOptions() {
  31      $options = array(
  32        '-1' => pht('(No Filtering)'),
  33        '-2' => pht('(Marked With Any Flag)'),
  34      );
  35  
  36      foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) {
  37        $options[$color] = $name;
  38      }
  39  
  40      return $options;
  41    }
  42  
  43    public function withIDs(array $ids) {
  44      $this->ids = $ids;
  45      return $this;
  46    }
  47  
  48    public function withPHIDs(array $phids) {
  49      $this->phids = $phids;
  50      return $this;
  51    }
  52  
  53    public function withAuthorPHIDs(array $authors) {
  54      $this->authors = $authors;
  55      return $this;
  56    }
  57  
  58    public function withNameLike($name) {
  59      $this->nameLike = $name;
  60      return $this;
  61    }
  62  
  63    public function withNames(array $names) {
  64      $this->names = $names;
  65      return $this;
  66    }
  67  
  68    public function withStatus($status) {
  69      $this->status = $status;
  70      return $this;
  71    }
  72  
  73    public function withDateCreatedBefore($date_created_before) {
  74      $this->dateCreatedBefore = $date_created_before;
  75      return $this;
  76    }
  77  
  78    public function withDateCreatedAfter($date_created_after) {
  79      $this->dateCreatedAfter = $date_created_after;
  80      return $this;
  81    }
  82  
  83    public function withFlagColor($flag_color) {
  84      $this->flagColor = $flag_color;
  85      return $this;
  86    }
  87  
  88    public function needFiles($need_files) {
  89      $this->needFiles = $need_files;
  90      return $this;
  91    }
  92  
  93    protected function loadPage() {
  94      $macro_table = new PhabricatorFileImageMacro();
  95      $conn = $macro_table->establishConnection('r');
  96  
  97      $rows = queryfx_all(
  98        $conn,
  99        'SELECT m.* FROM %T m %Q %Q %Q',
 100        $macro_table->getTableName(),
 101        $this->buildWhereClause($conn),
 102        $this->buildOrderClause($conn),
 103        $this->buildLimitClause($conn));
 104  
 105      return $macro_table->loadAllFromArray($rows);
 106    }
 107  
 108    protected function buildWhereClause(AphrontDatabaseConnection $conn) {
 109      $where = array();
 110  
 111      if ($this->ids) {
 112        $where[] = qsprintf(
 113          $conn,
 114          'm.id IN (%Ld)',
 115          $this->ids);
 116      }
 117  
 118      if ($this->phids) {
 119        $where[] = qsprintf(
 120          $conn,
 121          'm.phid IN (%Ls)',
 122          $this->phids);
 123      }
 124  
 125      if ($this->authors) {
 126        $where[] = qsprintf(
 127          $conn,
 128          'm.authorPHID IN (%Ls)',
 129          $this->authors);
 130      }
 131  
 132      if ($this->nameLike) {
 133        $where[] = qsprintf(
 134          $conn,
 135          'm.name LIKE %~',
 136          $this->nameLike);
 137      }
 138  
 139      if ($this->names) {
 140        $where[] = qsprintf(
 141          $conn,
 142          'm.name IN (%Ls)',
 143          $this->names);
 144      }
 145  
 146      switch ($this->status) {
 147        case self::STATUS_ACTIVE:
 148          $where[] = qsprintf(
 149            $conn,
 150            'm.isDisabled = 0');
 151          break;
 152        case self::STATUS_DISABLED:
 153          $where[] = qsprintf(
 154            $conn,
 155            'm.isDisabled = 1');
 156          break;
 157        case self::STATUS_ANY:
 158          break;
 159        default:
 160          throw new Exception("Unknown status '{$this->status}'!");
 161      }
 162  
 163      if ($this->dateCreatedAfter) {
 164        $where[] = qsprintf(
 165          $conn,
 166          'm.dateCreated >= %d',
 167          $this->dateCreatedAfter);
 168      }
 169  
 170      if ($this->dateCreatedBefore) {
 171        $where[] = qsprintf(
 172          $conn,
 173          'm.dateCreated <= %d',
 174          $this->dateCreatedBefore);
 175      }
 176  
 177      if ($this->flagColor != '-1' && $this->flagColor !== null) {
 178        if ($this->flagColor == '-2') {
 179          $flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap());
 180        } else {
 181          $flag_colors = array($this->flagColor);
 182        }
 183        $flags = id(new PhabricatorFlagQuery())
 184          ->withOwnerPHIDs(array($this->getViewer()->getPHID()))
 185          ->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST))
 186          ->withColors($flag_colors)
 187          ->setViewer($this->getViewer())
 188          ->execute();
 189  
 190        if (empty($flags)) {
 191          throw new PhabricatorEmptyQueryException('No matching flags.');
 192        } else {
 193          $where[] = qsprintf(
 194            $conn,
 195            'm.phid IN (%Ls)',
 196            mpull($flags, 'getObjectPHID'));
 197        }
 198      }
 199  
 200      $where[] = $this->buildPagingClause($conn);
 201  
 202      return $this->formatWhereClause($where);
 203    }
 204  
 205    protected function didFilterPage(array $macros) {
 206      if ($this->needFiles) {
 207        $file_phids = mpull($macros, 'getFilePHID');
 208        $files = id(new PhabricatorFileQuery())
 209          ->setViewer($this->getViewer())
 210          ->setParentQuery($this)
 211          ->withPHIDs($file_phids)
 212          ->execute();
 213        $files = mpull($files, null, 'getPHID');
 214  
 215        foreach ($macros as $key => $macro) {
 216          $file = idx($files, $macro->getFilePHID());
 217          if (!$file) {
 218            unset($macros[$key]);
 219            continue;
 220          }
 221          $macro->attachFile($file);
 222        }
 223      }
 224  
 225      return $macros;
 226    }
 227  
 228    protected function getPagingColumn() {
 229      return 'm.id';
 230    }
 231  
 232    public function getQueryApplicationClass() {
 233      return 'PhabricatorMacroApplication';
 234    }
 235  
 236  }


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