Path: blob/master/src/applications/macro/query/PhabricatorMacroSearchEngine.php
12242 views
<?php12final class PhabricatorMacroSearchEngine3extends PhabricatorApplicationSearchEngine {45public function getResultTypeDescription() {6return pht('Macros');7}89public function getApplicationClassName() {10return 'PhabricatorMacroApplication';11}1213public function newQuery() {14return id(new PhabricatorMacroQuery())15->needFiles(true);16}1718protected function buildCustomSearchFields() {19return array(20id(new PhabricatorSearchSelectField())21->setLabel(pht('Status'))22->setKey('status')23->setOptions(PhabricatorMacroQuery::getStatusOptions()),24id(new PhabricatorUsersSearchField())25->setLabel(pht('Authors'))26->setKey('authorPHIDs')27->setAliases(array('author', 'authors')),28id(new PhabricatorSearchTextField())29->setLabel(pht('Name Contains'))30->setKey('nameLike'),31id(new PhabricatorSearchStringListField())32->setLabel(pht('Exact Names'))33->setKey('names'),34id(new PhabricatorSearchSelectField())35->setLabel(pht('Marked with Flag'))36->setKey('flagColor')37->setDefault('-1')38->setOptions(PhabricatorMacroQuery::getFlagColorsOptions()),39id(new PhabricatorSearchDateField())40->setLabel(pht('Created After'))41->setKey('createdStart'),42id(new PhabricatorSearchDateField())43->setLabel(pht('Created Before'))44->setKey('createdEnd'),45);46}4748protected function getDefaultFieldOrder() {49return array(50'...',51'createdStart',52'createdEnd',53);54}5556protected function buildQueryFromParameters(array $map) {57$query = $this->newQuery();5859if ($map['authorPHIDs']) {60$query->withAuthorPHIDs($map['authorPHIDs']);61}6263if ($map['status']) {64$query->withStatus($map['status']);65}6667if ($map['names']) {68$query->withNames($map['names']);69}7071if (strlen($map['nameLike'])) {72$query->withNameLike($map['nameLike']);73}7475if ($map['createdStart']) {76$query->withDateCreatedAfter($map['createdStart']);77}7879if ($map['createdEnd']) {80$query->withDateCreatedBefore($map['createdEnd']);81}8283if ($map['flagColor'] !== null) {84$query->withFlagColor($map['flagColor']);85}8687return $query;88}8990protected function getURI($path) {91return '/macro/'.$path;92}9394protected function getBuiltinQueryNames() {95$names = array(96'active' => pht('Active'),97'all' => pht('All'),98);99100if ($this->requireViewer()->isLoggedIn()) {101$names['authored'] = pht('Authored');102}103104return $names;105}106107public function buildSavedQueryFromBuiltin($query_key) {108$query = $this->newSavedQuery();109$query->setQueryKey($query_key);110111switch ($query_key) {112case 'active':113return $query->setParameter(114'status',115PhabricatorMacroQuery::STATUS_ACTIVE);116case 'all':117return $query->setParameter(118'status',119PhabricatorMacroQuery::STATUS_ANY);120case 'authored':121return $query->setParameter(122'authorPHIDs',123array($this->requireViewer()->getPHID()));124}125126return parent::buildSavedQueryFromBuiltin($query_key);127}128129protected function renderResultList(130array $macros,131PhabricatorSavedQuery $query,132array $handles) {133134assert_instances_of($macros, 'PhabricatorFileImageMacro');135$viewer = $this->requireViewer();136$handles = $viewer->loadHandles(mpull($macros, 'getAuthorPHID'));137138$xform = PhabricatorFileTransform::getTransformByKey(139PhabricatorFileThumbnailTransform::TRANSFORM_PINBOARD);140141$pinboard = new PHUIPinboardView();142foreach ($macros as $macro) {143$file = $macro->getFile();144145$item = id(new PHUIPinboardItemView())146->setUser($viewer)147->setObject($macro);148149if ($file) {150$item->setImageURI($file->getURIForTransform($xform));151list($x, $y) = $xform->getTransformedDimensions($file);152$item->setImageSize($x, $y);153}154155if ($macro->getDateCreated()) {156$datetime = phabricator_date($macro->getDateCreated(), $viewer);157$item->appendChild(158phutil_tag(159'div',160array(),161pht('Created on %s', $datetime)));162} else {163// Very old macros don't have a creation date. Rendering something164// keeps all the pins at the same height and avoids flow issues.165$item->appendChild(166phutil_tag(167'div',168array(),169pht('Created in ages long past')));170}171172if ($macro->getAuthorPHID()) {173$author_handle = $handles[$macro->getAuthorPHID()];174$item->appendChild(175pht('Created by %s', $author_handle->renderLink()));176}177178$item->setURI($this->getApplicationURI('/view/'.$macro->getID().'/'));179$item->setDisabled($macro->getisDisabled());180$item->setHeader($macro->getName());181182$pinboard->addItem($item);183}184185$result = new PhabricatorApplicationSearchResultView();186$result->setContent($pinboard);187188return $result;189}190191protected function getNewUserBody() {192$create_button = id(new PHUIButtonView())193->setTag('a')194->setText(pht('Create a Macro'))195->setHref('/macro/create/')196->setColor(PHUIButtonView::GREEN);197198$icon = $this->getApplication()->getIcon();199$app_name = $this->getApplication()->getName();200$view = id(new PHUIBigInfoView())201->setIcon($icon)202->setTitle(pht('Welcome to %s', $app_name))203->setDescription(204pht('Create easy to remember shortcuts to images and memes.'))205->addAction($create_button);206207return $view;208}209210}211212213