diff --git a/resources/sql/autopatches/20161210.dashboards.01.author.php b/resources/sql/autopatches/20161210.dashboards.01.author.php new file mode 100644 index 0000000000..84f6fbf8d7 --- /dev/null +++ b/resources/sql/autopatches/20161210.dashboards.01.author.php @@ -0,0 +1,39 @@ +establishConnection('w'); + +$txn_table = new PhabricatorDashboardTransaction(); +$txn_conn = $table->establishConnection('r'); + +echo pht("Building Dashboard authorPHIDs...\n"); + +foreach (new LiskMigrationIterator($table) as $dashboard) { + + if ($dashboard->getAuthorPHID()) { + continue; + } + + $author_row = queryfx_one( + $txn_conn, + 'SELECT authorPHID FROM %T WHERE objectPHID = %s ORDER BY id ASC LIMIT 1', + $txn_table->getTableName(), + $dashboard->getPHID()); + + if (!$author_row) { + $author_phid = id(new PhabricatorDashboardApplication())->getPHID(); + } else { + $author_phid = $author_row['authorPHID']; + } + + queryfx( + $conn_w, + 'UPDATE %T SET authorPHID = %s WHERE id = %d', + $table->getTableName(), + $author_phid, + $dashboard->getID()); +} + +echo pht("Done\n"); diff --git a/resources/sql/autopatches/20161210.dashboards.01.author.sql b/resources/sql/autopatches/20161210.dashboards.01.author.sql new file mode 100644 index 0000000000..405ed5727c --- /dev/null +++ b/resources/sql/autopatches/20161210.dashboards.01.author.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_dashboard.dashboard + ADD authorPHID VARBINARY(64) NOT NULL; diff --git a/src/applications/dashboard/query/PhabricatorDashboardQuery.php b/src/applications/dashboard/query/PhabricatorDashboardQuery.php index 55cd447c7a..a250d646d0 100644 --- a/src/applications/dashboard/query/PhabricatorDashboardQuery.php +++ b/src/applications/dashboard/query/PhabricatorDashboardQuery.php @@ -1,131 +1,144 @@ ids = $ids; return $this; } public function withPHIDs(array $phids) { $this->phids = $phids; return $this; } public function withStatuses(array $statuses) { $this->statuses = $statuses; return $this; } + public function withAuthorPHIDs(array $authors) { + $this->authorPHIDs = $authors; + return $this; + } + public function needPanels($need_panels) { $this->needPanels = $need_panels; return $this; } public function needProjects($need_projects) { $this->needProjects = $need_projects; return $this; } protected function loadPage() { return $this->loadStandardPage($this->newResultObject()); } public function newResultObject() { return new PhabricatorDashboard(); } protected function didFilterPage(array $dashboards) { $phids = mpull($dashboards, 'getPHID'); if ($this->needPanels) { $edge_query = id(new PhabricatorEdgeQuery()) ->withSourcePHIDs($phids) ->withEdgeTypes( array( PhabricatorDashboardDashboardHasPanelEdgeType::EDGECONST, )); $edge_query->execute(); $panel_phids = $edge_query->getDestinationPHIDs(); if ($panel_phids) { $panels = id(new PhabricatorDashboardPanelQuery()) ->setParentQuery($this) ->setViewer($this->getViewer()) ->withPHIDs($panel_phids) ->execute(); $panels = mpull($panels, null, 'getPHID'); } else { $panels = array(); } foreach ($dashboards as $dashboard) { $dashboard_phids = $edge_query->getDestinationPHIDs( array($dashboard->getPHID())); $dashboard_panels = array_select_keys($panels, $dashboard_phids); $dashboard->attachPanelPHIDs($dashboard_phids); $dashboard->attachPanels($dashboard_panels); } } if ($this->needProjects) { $edge_query = id(new PhabricatorEdgeQuery()) ->withSourcePHIDs($phids) ->withEdgeTypes( array( PhabricatorProjectObjectHasProjectEdgeType::EDGECONST, )); $edge_query->execute(); foreach ($dashboards as $dashboard) { $project_phids = $edge_query->getDestinationPHIDs( array($dashboard->getPHID())); $dashboard->attachProjectPHIDs($project_phids); } } return $dashboards; } protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { $where = parent::buildWhereClauseParts($conn); if ($this->ids !== null) { $where[] = qsprintf( $conn, 'id IN (%Ld)', $this->ids); } if ($this->phids !== null) { $where[] = qsprintf( $conn, 'phid IN (%Ls)', $this->phids); } if ($this->statuses !== null) { $where[] = qsprintf( $conn, 'status IN (%Ls)', $this->statuses); } + if ($this->authorPHIDs !== null) { + $where[] = qsprintf( + $conn, + 'authorPHID IN (%Ls)', + $this->authorPHIDs); + } + return $where; } public function getQueryApplicationClass() { return 'PhabricatorDashboardApplication'; } } diff --git a/src/applications/dashboard/query/PhabricatorDashboardSearchEngine.php b/src/applications/dashboard/query/PhabricatorDashboardSearchEngine.php index 26c71c1bab..70325de3bb 100644 --- a/src/applications/dashboard/query/PhabricatorDashboardSearchEngine.php +++ b/src/applications/dashboard/query/PhabricatorDashboardSearchEngine.php @@ -1,197 +1,218 @@ needProjects(true); } protected function buildCustomSearchFields() { return array( + id(new PhabricatorSearchDatasourceField()) + ->setLabel(pht('Authored By')) + ->setKey('authorPHIDs') + ->setDatasource(new PhabricatorPeopleUserFunctionDatasource()), id(new PhabricatorSearchCheckboxesField()) ->setKey('statuses') ->setLabel(pht('Status')) ->setOptions(PhabricatorDashboard::getStatusNameMap()), ); } protected function getURI($path) { return '/dashboard/'.$path; } protected function getBuiltinQueryNames() { - return array( - 'open' => pht('Active Dashboards'), - 'all' => pht('All Dashboards'), - ); + $names = array(); + + if ($this->requireViewer()->isLoggedIn()) { + $names['authored'] = pht('Authored'); + } + + $names['open'] = pht('Active Dashboards'); + $names['all'] = pht('All Dashboards'); + + return $names; } public function buildSavedQueryFromBuiltin($query_key) { $query = $this->newSavedQuery(); $query->setQueryKey($query_key); + $viewer = $this->requireViewer(); switch ($query_key) { case 'all': return $query; + case 'authored': + return $query->setParameter( + 'authored', + array( + $viewer->getPHID(), + )); case 'open': return $query->setParameter( 'statuses', array( PhabricatorDashboard::STATUS_ACTIVE, )); } return parent::buildSavedQueryFromBuiltin($query_key); } protected function buildQueryFromParameters(array $map) { $query = $this->newQuery(); if ($map['statuses']) { $query->withStatuses($map['statuses']); } + if ($map['authorPHIDs']) { + $query->withAuthorPHIDs($map['authorPHIDs']); + } + return $query; } protected function renderResultList( array $dashboards, PhabricatorSavedQuery $query, array $handles) { $dashboards = mpull($dashboards, null, 'getPHID'); $viewer = $this->requireViewer(); if ($dashboards) { $installs = id(new PhabricatorDashboardInstall()) ->loadAllWhere( 'objectPHID IN (%Ls) AND dashboardPHID IN (%Ls)', array( PhabricatorHomeApplication::DASHBOARD_DEFAULT, $viewer->getPHID(), ), array_keys($dashboards)); $installs = mpull($installs, null, 'getDashboardPHID'); } else { $installs = array(); } $proj_phids = array(); foreach ($dashboards as $dashboard) { foreach ($dashboard->getProjectPHIDs() as $project_phid) { $proj_phids[] = $project_phid; } } $proj_handles = id(new PhabricatorHandleQuery()) ->setViewer($viewer) ->withPHIDs($proj_phids) ->execute(); $list = new PHUIObjectItemListView(); $list->setUser($viewer); $list->initBehavior('phabricator-tooltips', array()); $list->requireResource('aphront-tooltip-css'); foreach ($dashboards as $dashboard_phid => $dashboard) { $id = $dashboard->getID(); $item = id(new PHUIObjectItemView()) ->setObjectName(pht('Dashboard %d', $id)) ->setHeader($dashboard->getName()) ->setHref($this->getApplicationURI("view/{$id}/")) ->setObject($dashboard); if (isset($installs[$dashboard_phid])) { $install = $installs[$dashboard_phid]; if ($install->getObjectPHID() == $viewer->getPHID()) { $attrs = array( 'tip' => pht( 'This dashboard is installed to your personal homepage.'), ); $item->addIcon('fa-user', pht('Installed'), $attrs); } else { $attrs = array( 'tip' => pht( 'This dashboard is the default homepage for all users.'), ); $item->addIcon('fa-globe', pht('Installed'), $attrs); } } $project_handles = array_select_keys( $proj_handles, $dashboard->getProjectPHIDs()); $item->addAttribute( id(new PHUIHandleTagListView()) ->setLimit(4) ->setNoDataString(pht('No Projects')) ->setSlim(true) ->setHandles($project_handles)); if ($dashboard->isArchived()) { $item->setDisabled(true); } $can_edit = PhabricatorPolicyFilter::hasCapability( $viewer, $dashboard, PhabricatorPolicyCapability::CAN_EDIT); $href_view = $this->getApplicationURI("manage/{$id}/"); $item->addAction( id(new PHUIListItemView()) ->setName(pht('Manage')) ->setIcon('fa-th') ->setHref($href_view)); $href_edit = $this->getApplicationURI("edit/{$id}/"); $item->addAction( id(new PHUIListItemView()) ->setName(pht('Edit')) ->setIcon('fa-pencil') ->setHref($href_edit) ->setDisabled(!$can_edit)); $list->addItem($item); } $result = new PhabricatorApplicationSearchResultView(); $result->setObjectList($list); $result->setNoDataString(pht('No dashboards found.')); return $result; } protected function getNewUserBody() { $create_button = id(new PHUIButtonView()) ->setTag('a') ->setText(pht('Create a Dashboard')) ->setHref('/dashboard/create/') ->setColor(PHUIButtonView::GREEN); $icon = $this->getApplication()->getIcon(); $app_name = $this->getApplication()->getName(); $view = id(new PHUIBigInfoView()) ->setIcon($icon) ->setTitle(pht('Welcome to %s', $app_name)) ->setDescription( pht('Customize your homepage with different panels and '. 'search queries.')) ->addAction($create_button); return $view; } } diff --git a/src/applications/dashboard/storage/PhabricatorDashboard.php b/src/applications/dashboard/storage/PhabricatorDashboard.php index 2930ca7674..fab431764a 100644 --- a/src/applications/dashboard/storage/PhabricatorDashboard.php +++ b/src/applications/dashboard/storage/PhabricatorDashboard.php @@ -1,183 +1,186 @@ setName('') ->setViewPolicy(PhabricatorPolicies::POLICY_USER) ->setEditPolicy($actor->getPHID()) ->setStatus(self::STATUS_ACTIVE) + ->setAuthorPHID($actor->getPHID()) ->attachPanels(array()) ->attachPanelPHIDs(array()); } public static function getStatusNameMap() { return array( self::STATUS_ACTIVE => pht('Active'), self::STATUS_ARCHIVED => pht('Archived'), ); } public static function copyDashboard( PhabricatorDashboard $dst, PhabricatorDashboard $src) { $dst->name = $src->name; $dst->layoutConfig = $src->layoutConfig; return $dst; } protected function getConfiguration() { return array( self::CONFIG_AUX_PHID => true, self::CONFIG_SERIALIZATION => array( 'layoutConfig' => self::SERIALIZATION_JSON, ), self::CONFIG_COLUMN_SCHEMA => array( 'name' => 'text255', 'status' => 'text32', + 'authorPHID' => 'phid', ), ) + parent::getConfiguration(); } public function generatePHID() { return PhabricatorPHID::generateNewPHID( PhabricatorDashboardDashboardPHIDType::TYPECONST); } public function getLayoutConfigObject() { return PhabricatorDashboardLayoutConfig::newFromDictionary( $this->getLayoutConfig()); } public function setLayoutConfigFromObject( PhabricatorDashboardLayoutConfig $object) { $this->setLayoutConfig($object->toDictionary()); return $this; } public function getProjectPHIDs() { return $this->assertAttached($this->edgeProjectPHIDs); } public function attachProjectPHIDs(array $phids) { $this->edgeProjectPHIDs = $phids; return $this; } public function attachPanelPHIDs(array $phids) { $this->panelPHIDs = $phids; return $this; } public function getPanelPHIDs() { return $this->assertAttached($this->panelPHIDs); } public function attachPanels(array $panels) { assert_instances_of($panels, 'PhabricatorDashboardPanel'); $this->panels = $panels; return $this; } public function getPanels() { return $this->assertAttached($this->panels); } public function isArchived() { return ($this->getStatus() == self::STATUS_ARCHIVED); } /* -( PhabricatorApplicationTransactionInterface )------------------------- */ public function getApplicationTransactionEditor() { return new PhabricatorDashboardTransactionEditor(); } public function getApplicationTransactionObject() { return $this; } public function getApplicationTransactionTemplate() { return new PhabricatorDashboardTransaction(); } public function willRenderTimeline( PhabricatorApplicationTransactionView $timeline, AphrontRequest $request) { return $timeline; } /* -( PhabricatorPolicyInterface )----------------------------------------- */ public function getCapabilities() { return array( PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT, ); } public function getPolicy($capability) { switch ($capability) { case PhabricatorPolicyCapability::CAN_VIEW: return $this->getViewPolicy(); case PhabricatorPolicyCapability::CAN_EDIT: return $this->getEditPolicy(); } } public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { return false; } /* -( PhabricatorDestructibleInterface )----------------------------------- */ public function destroyObjectPermanently( PhabricatorDestructionEngine $engine) { $this->openTransaction(); $installs = id(new PhabricatorDashboardInstall())->loadAllWhere( 'dashboardPHID = %s', $this->getPHID()); foreach ($installs as $install) { $install->delete(); } $this->delete(); $this->saveTransaction(); } }