diff --git a/src/applications/macro/query/PhabricatorMacroQuery.php b/src/applications/macro/query/PhabricatorMacroQuery.php index d4ffe46107..3ba30502d5 100644 --- a/src/applications/macro/query/PhabricatorMacroQuery.php +++ b/src/applications/macro/query/PhabricatorMacroQuery.php @@ -1,278 +1,269 @@ pht('Active Macros'), self::STATUS_DISABLED => pht('Disabled Macros'), self::STATUS_ANY => pht('Active and Disabled Macros'), ); } public static function getFlagColorsOptions() { $options = array( '-1' => pht('(No Filtering)'), '-2' => pht('(Marked With Any Flag)'), ); foreach (PhabricatorFlagColor::getColorNameMap() as $color => $name) { $options[$color] = $name; } return $options; } public function withIDs(array $ids) { $this->ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } - public function withAuthorPHIDs(array $authors) { - $this->authors = $authors; + public function withAuthorPHIDs(array $author_phids) { + $this->authorPHIDs = $author_phids; return $this; } public function withNameLike($name) { $this->nameLike = $name; return $this; } public function withNames(array $names) { $this->names = $names; return $this; } public function withNamePrefix($prefix) { $this->namePrefix = $prefix; return $this; } public function withStatus($status) { $this->status = $status; return $this; } public function withDateCreatedBefore($date_created_before) { $this->dateCreatedBefore = $date_created_before; return $this; } public function withDateCreatedAfter($date_created_after) { $this->dateCreatedAfter = $date_created_after; return $this; } public function withFlagColor($flag_color) { $this->flagColor = $flag_color; return $this; } public function needFiles($need_files) { $this->needFiles = $need_files; return $this; } + public function newResultObject() { + return new PhabricatorFileImageMacro(); + } + protected function loadPage() { - $macro_table = new PhabricatorFileImageMacro(); - $conn = $macro_table->establishConnection('r'); - - $rows = queryfx_all( - $conn, - 'SELECT m.* FROM %T m %Q %Q %Q', - $macro_table->getTableName(), - $this->buildWhereClause($conn), - $this->buildOrderClause($conn), - $this->buildLimitClause($conn)); - - return $macro_table->loadAllFromArray($rows); + return $this->loadStandardPage(new PhabricatorFileImageMacro()); } - protected function buildWhereClause(AphrontDatabaseConnection $conn) { - $where = array(); + protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { + $where = parent::buildWhereClauseParts($conn); - if ($this->ids) { + if ($this->ids !== null) { $where[] = qsprintf( $conn, 'm.id IN (%Ld)', $this->ids); } - if ($this->phids) { + if ($this->phids !== null) { $where[] = qsprintf( $conn, 'm.phid IN (%Ls)', $this->phids); } - if ($this->authors) { + if ($this->authorPHIDs !== null) { $where[] = qsprintf( $conn, 'm.authorPHID IN (%Ls)', - $this->authors); + $this->authorPHIDs); } - if ($this->nameLike) { + if (strlen($this->nameLike)) { $where[] = qsprintf( $conn, 'm.name LIKE %~', $this->nameLike); } - if ($this->names) { + if ($this->names !== null) { $where[] = qsprintf( $conn, 'm.name IN (%Ls)', $this->names); } if (strlen($this->namePrefix)) { $where[] = qsprintf( $conn, 'm.name LIKE %>', $this->namePrefix); } switch ($this->status) { case self::STATUS_ACTIVE: $where[] = qsprintf( $conn, 'm.isDisabled = 0'); break; case self::STATUS_DISABLED: $where[] = qsprintf( $conn, 'm.isDisabled = 1'); break; case self::STATUS_ANY: break; default: throw new Exception(pht("Unknown status '%s'!", $this->status)); } if ($this->dateCreatedAfter) { $where[] = qsprintf( $conn, 'm.dateCreated >= %d', $this->dateCreatedAfter); } if ($this->dateCreatedBefore) { $where[] = qsprintf( $conn, 'm.dateCreated <= %d', $this->dateCreatedBefore); } if ($this->flagColor != '-1' && $this->flagColor !== null) { if ($this->flagColor == '-2') { $flag_colors = array_keys(PhabricatorFlagColor::getColorNameMap()); } else { $flag_colors = array($this->flagColor); } $flags = id(new PhabricatorFlagQuery()) ->withOwnerPHIDs(array($this->getViewer()->getPHID())) ->withTypes(array(PhabricatorMacroMacroPHIDType::TYPECONST)) ->withColors($flag_colors) ->setViewer($this->getViewer()) ->execute(); if (empty($flags)) { throw new PhabricatorEmptyQueryException(pht('No matching flags.')); } else { $where[] = qsprintf( $conn, 'm.phid IN (%Ls)', mpull($flags, 'getObjectPHID')); } } - $where[] = $this->buildPagingClause($conn); - - return $this->formatWhereClause($where); + return $where; } protected function didFilterPage(array $macros) { if ($this->needFiles) { $file_phids = mpull($macros, 'getFilePHID'); $files = id(new PhabricatorFileQuery()) ->setViewer($this->getViewer()) ->setParentQuery($this) ->withPHIDs($file_phids) ->execute(); $files = mpull($files, null, 'getPHID'); foreach ($macros as $key => $macro) { $file = idx($files, $macro->getFilePHID()); if (!$file) { unset($macros[$key]); continue; } $macro->attachFile($file); } } return $macros; } protected function getPrimaryTableAlias() { return 'm'; } public function getQueryApplicationClass() { return 'PhabricatorMacroApplication'; } public function getOrderableColumns() { return parent::getOrderableColumns() + array( 'name' => array( 'table' => 'm', 'column' => 'name', 'type' => 'string', 'reverse' => true, 'unique' => true, ), ); } protected function getPagingValueMap($cursor, array $keys) { $macro = $this->loadCursorObject($cursor); return array( 'id' => $macro->getID(), 'name' => $macro->getName(), ); } public function getBuiltinOrders() { return array( 'name' => array( 'vector' => array('name'), 'name' => pht('Name'), ), ) + parent::getBuiltinOrders(); } } diff --git a/src/applications/macro/query/PhabricatorMacroSearchEngine.php b/src/applications/macro/query/PhabricatorMacroSearchEngine.php index 8d632e818f..6bf41e35bb 100644 --- a/src/applications/macro/query/PhabricatorMacroSearchEngine.php +++ b/src/applications/macro/query/PhabricatorMacroSearchEngine.php @@ -1,229 +1,184 @@ setParameter( - 'authorPHIDs', - $this->readUsersFromRequest($request, 'authors')); - - $saved->setParameter('status', $request->getStr('status')); - $saved->setParameter('names', $request->getStrList('names')); - $saved->setParameter('nameLike', $request->getStr('nameLike')); - $saved->setParameter('createdStart', $request->getStr('createdStart')); - $saved->setParameter('createdEnd', $request->getStr('createdEnd')); - $saved->setParameter('flagColor', $request->getStr('flagColor', '-1')); - - $this->saveQueryOrder($saved, $request); - - return $saved; + public function newQuery() { + return id(new PhabricatorMacroQuery()) + ->needFiles(true); } - public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { - $query = id(new PhabricatorMacroQuery()) - ->needFiles(true) - ->withIDs($saved->getParameter('ids', array())) - ->withPHIDs($saved->getParameter('phids', array())) - ->withAuthorPHIDs($saved->getParameter('authorPHIDs', array())); + protected function buildCustomSearchFields() { + return array( + id(new PhabricatorSearchSelectField()) + ->setLabel(pht('Status')) + ->setKey('status') + ->setOptions(PhabricatorMacroQuery::getStatusOptions()), + id(new PhabricatorSearchUsersField()) + ->setLabel(pht('Authors')) + ->setKey('authorPHIDs') + ->setAliases(array('author', 'authors')), + id(new PhabricatorSearchTextField()) + ->setLabel(pht('Name Contains')) + ->setKey('nameLike'), + id(new PhabricatorSearchStringListField()) + ->setLabel(pht('Exact Names')) + ->setKey('names'), + id(new PhabricatorSearchSelectField()) + ->setLabel(pht('Marked with Flag')) + ->setKey('flagColor') + ->setDefault('-1') + ->setOptions(PhabricatorMacroQuery::getFlagColorsOptions()), + id(new PhabricatorSearchDateField()) + ->setLabel(pht('Created After')) + ->setKey('createdStart'), + id(new PhabricatorSearchDateField()) + ->setLabel(pht('Created Before')) + ->setKey('createdEnd'), + ); + } + + protected function getDefaultFieldOrder() { + return array( + '...', + 'createdStart', + 'createdEnd', + ); + } - $this->setQueryOrder($query, $saved); + public function buildQueryFromParameters(array $map) { + $query = $this->newQuery(); - $status = $saved->getParameter('status'); - $options = PhabricatorMacroQuery::getStatusOptions(); - if (empty($options[$status])) { - $status = head_key($options); + if ($map['authorPHIDs']) { + $query->withAuthorPHIDs($map['authorPHIDs']); } - $query->withStatus($status); - $names = $saved->getParameter('names', array()); - if ($names) { - $query->withNames($names); + if ($map['status']) { + $query->withStatus($map['status']); } - $like = $saved->getParameter('nameLike'); - if (strlen($like)) { - $query->withNameLike($like); + if ($map['names']) { + $query->withNames($map['names']); } - $start = $this->parseDateTime($saved->getParameter('createdStart')); - $end = $this->parseDateTime($saved->getParameter('createdEnd')); + if (strlen($map['nameLike'])) { + $query->withNameLike($map['nameLike']); + } - if ($start) { - $query->withDateCreatedAfter($start); + if ($map['createdStart']) { + $query->withDateCreatedAfter($map['createdStart']); } - if ($end) { - $query->withDateCreatedBefore($end); + if ($map['createdEnd']) { + $query->withDateCreatedBefore($map['createdEnd']); } - $color = $saved->getParameter('flagColor'); - if (strlen($color)) { - $query->withFlagColor($color); + if ($map['flagColor'] !== null) { + $query->withFlagColor($map['flagColor']); } return $query; } - public function buildSearchForm( - AphrontFormView $form, - PhabricatorSavedQuery $saved) { - - $author_phids = $saved->getParameter('authorPHIDs', array()); - $status = $saved->getParameter('status'); - $names = implode(', ', $saved->getParameter('names', array())); - $like = $saved->getParameter('nameLike'); - $color = $saved->getParameter('flagColor', '-1'); - - $form - ->appendChild( - id(new AphrontFormSelectControl()) - ->setName('status') - ->setLabel(pht('Status')) - ->setOptions(PhabricatorMacroQuery::getStatusOptions()) - ->setValue($status)) - ->appendControl( - id(new AphrontFormTokenizerControl()) - ->setDatasource(new PhabricatorPeopleDatasource()) - ->setName('authors') - ->setLabel(pht('Authors')) - ->setValue($author_phids)) - ->appendChild( - id(new AphrontFormTextControl()) - ->setName('nameLike') - ->setLabel(pht('Name Contains')) - ->setValue($like)) - ->appendChild( - id(new AphrontFormTextControl()) - ->setName('names') - ->setLabel(pht('Exact Names')) - ->setValue($names)) - ->appendChild( - id(new AphrontFormSelectControl()) - ->setName('flagColor') - ->setLabel(pht('Marked with Flag')) - ->setOptions(PhabricatorMacroQuery::getFlagColorsOptions()) - ->setValue($color)); - - $this->buildDateRange( - $form, - $saved, - 'createdStart', - pht('Created After'), - 'createdEnd', - pht('Created Before')); - - $this->appendOrderFieldsToForm( - $form, - $saved, - new PhabricatorMacroQuery()); - } - protected function getURI($path) { return '/macro/'.$path; } protected function getBuiltinQueryNames() { $names = array( 'active' => pht('Active'), 'all' => pht('All'), ); if ($this->requireViewer()->isLoggedIn()) { $names['authored'] = pht('Authored'); } return $names; } public function buildSavedQueryFromBuiltin($query_key) { $query = $this->newSavedQuery(); $query->setQueryKey($query_key); switch ($query_key) { case 'active': return $query; case 'all': return $query->setParameter( 'status', PhabricatorMacroQuery::STATUS_ANY); case 'authored': return $query->setParameter( 'authorPHIDs', array($this->requireViewer()->getPHID())); } return parent::buildSavedQueryFromBuiltin($query_key); } - protected function getRequiredHandlePHIDsForResultList( - array $macros, - PhabricatorSavedQuery $query) { - return mpull($macros, 'getAuthorPHID'); - } - protected function renderResultList( array $macros, PhabricatorSavedQuery $query, array $handles) { assert_instances_of($macros, 'PhabricatorFileImageMacro'); $viewer = $this->requireViewer(); + $handles = $viewer->loadHandles(mpull($macros, 'getAuthorPHID')); $xform = PhabricatorFileTransform::getTransformByKey( PhabricatorFileThumbnailTransform::TRANSFORM_PINBOARD); $pinboard = new PHUIPinboardView(); foreach ($macros as $macro) { $file = $macro->getFile(); $item = new PHUIPinboardItemView(); if ($file) { $item->setImageURI($file->getURIForTransform($xform)); list($x, $y) = $xform->getTransformedDimensions($file); $item->setImageSize($x, $y); } if ($macro->getDateCreated()) { $datetime = phabricator_date($macro->getDateCreated(), $viewer); $item->appendChild( phutil_tag( 'div', array(), pht('Created on %s', $datetime))); } else { // Very old macros don't have a creation date. Rendering something // keeps all the pins at the same height and avoids flow issues. $item->appendChild( phutil_tag( 'div', array(), pht('Created in ages long past'))); } if ($macro->getAuthorPHID()) { $author_handle = $handles[$macro->getAuthorPHID()]; $item->appendChild( pht('Created by %s', $author_handle->renderLink())); } $item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/')); $item->setDisabled($macro->getisDisabled()); $item->setHeader($macro->getName()); $pinboard->addItem($item); } return $pinboard; } } diff --git a/src/applications/search/field/PhabricatorSearchSelectField.php b/src/applications/search/field/PhabricatorSearchSelectField.php index ca68415583..0806174220 100644 --- a/src/applications/search/field/PhabricatorSearchSelectField.php +++ b/src/applications/search/field/PhabricatorSearchSelectField.php @@ -1,30 +1,36 @@ options = $options; return $this; } public function getOptions() { return $this->options; } protected function getDefaultValue() { - return null; + return $this->default; + } + + public function setDefault($default) { + $this->default = $default; + return $this; } protected function getValueFromRequest(AphrontRequest $request, $key) { return $request->getStr($key); } protected function newControl() { return id(new AphrontFormSelectControl()) ->setOptions($this->getOptions()); } }