protected function loadPage()
 {
     $table = new PhabricatorOwnersPackage();
     $conn_r = $table->establishConnection('r');
     $data = queryfx_all($conn_r, 'SELECT p.* FROM %T p %Q %Q %Q %Q', $table->getTableName(), $this->buildJoinClause($conn_r), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
     return $table->loadAllFromArray($data);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $package = new PhabricatorOwnersPackage();
     $owner = new PhabricatorOwnersOwner();
     $path = new PhabricatorOwnersPath();
     $repository_phid = '';
     if ($request->getStr('repository') != '') {
         $repository_phid = id(new PhabricatorRepositoryQuery())->setViewer($user)->withCallsigns(array($request->getStr('repository')))->executeOne()->getPHID();
     }
     switch ($this->view) {
         case 'search':
             $packages = array();
             $conn_r = $package->establishConnection('r');
             $where = array('1 = 1');
             $join = array();
             $having = '';
             if ($request->getStr('name')) {
                 $where[] = qsprintf($conn_r, 'p.name LIKE %~', $request->getStr('name'));
             }
             if ($repository_phid || $request->getStr('path')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T path ON path.packageID = p.id', $path->getTableName());
                 if ($repository_phid) {
                     $where[] = qsprintf($conn_r, 'path.repositoryPHID = %s', $repository_phid);
                 }
                 if ($request->getStr('path')) {
                     $where[] = qsprintf($conn_r, '(path.path LIKE %~ AND NOT path.excluded) OR
             %s LIKE CONCAT(REPLACE(path.path, %s, %s), %s)', $request->getStr('path'), $request->getStr('path'), '_', '\\_', '%');
                     $having = 'HAVING MAX(path.excluded) = 0';
                 }
             }
             if ($request->getArr('owner')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T o ON o.packageID = p.id', $owner->getTableName());
                 $where[] = qsprintf($conn_r, 'o.userPHID IN (%Ls)', $request->getArr('owner'));
             }
             $data = queryfx_all($conn_r, 'SELECT p.* FROM %T p %Q WHERE %Q GROUP BY p.id %Q', $package->getTableName(), implode(' ', $join), '(' . implode(') AND (', $where) . ')', $having);
             $packages = $package->loadAllFromArray($data);
             $header = pht('Search Results');
             $nodata = pht('No packages match your query.');
             break;
         case 'owned':
             $data = queryfx_all($package->establishConnection('r'), 'SELECT p.* FROM %T p JOIN %T o ON p.id = o.packageID
         WHERE o.userPHID = %s GROUP BY p.id', $package->getTableName(), $owner->getTableName(), $user->getPHID());
             $packages = $package->loadAllFromArray($data);
             $header = pht('Owned Packages');
             $nodata = pht('No owned packages');
             break;
         case 'projects':
             $projects = id(new PhabricatorProjectQuery())->setViewer($user)->withMemberPHIDs(array($user->getPHID()))->withStatus(PhabricatorProjectQuery::STATUS_ANY)->execute();
             $owner_phids = mpull($projects, 'getPHID');
             if ($owner_phids) {
                 $data = queryfx_all($package->establishConnection('r'), 'SELECT p.* FROM %T p JOIN %T o ON p.id = o.packageID
           WHERE o.userPHID IN (%Ls) GROUP BY p.id', $package->getTableName(), $owner->getTableName(), $owner_phids);
             } else {
                 $data = array();
             }
             $packages = $package->loadAllFromArray($data);
             $header = pht('Owned Packages');
             $nodata = pht('No owned packages');
             break;
         case 'all':
             $packages = $package->loadAll();
             $header = pht('All Packages');
             $nodata = pht('There are no defined packages.');
             break;
     }
     $content = $this->renderPackageTable($packages, $header, $nodata);
     $filter = new AphrontListFilterView();
     $owners_search_value = array();
     if ($request->getArr('owner')) {
         $phids = $request->getArr('owner');
         $phid = reset($phids);
         $handles = $this->loadViewerHandles(array($phid));
         $owners_search_value = array($handles[$phid]);
     }
     $callsigns = array('' => pht('(Any Repository)'));
     $repositories = id(new PhabricatorRepositoryQuery())->setViewer($user)->setOrder(PhabricatorRepositoryQuery::ORDER_CALLSIGN)->execute();
     foreach ($repositories as $repository) {
         $callsigns[$repository->getCallsign()] = $repository->getCallsign() . ': ' . $repository->getName();
     }
     $form = id(new AphrontFormView())->setUser($user)->setAction('/owners/view/search/')->setMethod('GET')->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel(pht('Name'))->setValue($request->getStr('name')))->appendChild(id(new AphrontFormTokenizerControl())->setDatasource(new PhabricatorProjectOrUserDatasource())->setLimit(1)->setName('owner')->setLabel(pht('Owner'))->setValue($owners_search_value))->appendChild(id(new AphrontFormSelectControl())->setName('repository')->setLabel(pht('Repository'))->setOptions($callsigns)->setValue($request->getStr('repository')))->appendChild(id(new AphrontFormTextControl())->setName('path')->setLabel(pht('Path'))->setValue($request->getStr('path')))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Search for Packages')));
     $filter->appendChild($form);
     $nav = $this->buildSideNavView();
     $nav->appendChild($filter);
     $nav->appendChild($content);
     return $this->buildApplicationPage(array($nav), array('title' => pht('Package Index')));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $views = array('owned' => 'Owned Packages', 'all' => 'All Packages', 'search' => 'Search Results');
     if (empty($views[$this->view])) {
         reset($views);
         $this->view = key($views);
     }
     if ($this->view != 'search') {
         unset($views['search']);
     }
     $nav = new AphrontSideNavView();
     foreach ($views as $key => $name) {
         $nav->addNavItem(phutil_render_tag('a', array('href' => '/owners/view/' . $key . '/', 'class' => $this->view == $key ? 'aphront-side-nav-selected' : null), phutil_escape_html($name)));
     }
     $package = new PhabricatorOwnersPackage();
     $owner = new PhabricatorOwnersOwner();
     $path = new PhabricatorOwnersPath();
     switch ($this->view) {
         case 'search':
             $packages = array();
             $conn_r = $package->establishConnection('r');
             $where = array('1 = 1');
             $join = array();
             if ($request->getStr('name')) {
                 $where[] = qsprintf($conn_r, 'p.name LIKE %~', $request->getStr('name'));
             }
             if ($request->getStr('path')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T path ON path.packageID = p.id', $path->getTableName());
                 $where[] = qsprintf($conn_r, 'path.path LIKE %~', $request->getStr('path'));
             }
             if ($request->getArr('owner')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T o ON o.packageID = p.id', $owner->getTableName());
                 $where[] = qsprintf($conn_r, 'o.userPHID IN (%Ls)', $request->getArr('owner'));
             }
             $data = queryfx_all($conn_r, 'SELECT p.* FROM %T p %Q WHERE %Q GROUP BY p.id', $package->getTableName(), implode(' ', $join), '(' . implode(') AND (', $where) . ')');
             $packages = $package->loadAllFromArray($data);
             $header = 'Search Results';
             $nodata = 'No packages match your query.';
             break;
         case 'owned':
             $data = queryfx_all($package->establishConnection('r'), 'SELECT p.* FROM %T p JOIN %T o ON p.id = o.packageID
         WHERE o.userPHID = %s GROUP BY p.id', $package->getTableName(), $owner->getTableName(), $user->getPHID());
             $packages = $package->loadAllFromArray($data);
             $header = 'Owned Packages';
             $nodata = 'No owned packages';
             break;
         case 'all':
             $packages = $package->loadAll();
             $header = 'All Packages';
             $nodata = 'There are no defined packages.';
             break;
     }
     $content = $this->renderPackageTable($packages, $header, $nodata);
     $filter = new AphrontListFilterView();
     $filter->addButton(phutil_render_tag('a', array('href' => '/owners/new/', 'class' => 'green button'), 'Create New Package'));
     $owners_search_value = array();
     if ($request->getArr('owner')) {
         $phids = $request->getArr('owner');
         $phid = reset($phids);
         $handles = id(new PhabricatorObjectHandleData(array($phid)))->loadHandles();
         $owners_search_value = array($phid => $handles[$phid]->getFullName());
     }
     $form = id(new AphrontFormView())->setUser($user)->setAction('/owners/view/search/')->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel('Name')->setValue($request->getStr('name')))->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/users/')->setLimit(1)->setName('owner')->setLabel('Owner')->setValue($owners_search_value))->appendChild(id(new AphrontFormTextControl())->setName('path')->setLabel('Path')->setValue($request->getStr('path')))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search for Packages'));
     $filter->appendChild($form);
     $nav->appendChild($filter);
     $nav->appendChild($content);
     return $this->buildStandardPageResponse($nav, array('title' => 'Package Index', 'tab' => 'index'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $package = new PhabricatorOwnersPackage();
     $owner = new PhabricatorOwnersOwner();
     $path = new PhabricatorOwnersPath();
     $repository_phid = '';
     if ($request->getStr('repository') != '') {
         $repository_phid = id(new PhabricatorRepository())->loadOneWhere('callsign = %s', $request->getStr('repository'))->getPHID();
     }
     switch ($this->view) {
         case 'search':
             $packages = array();
             $conn_r = $package->establishConnection('r');
             $where = array('1 = 1');
             $join = array();
             if ($request->getStr('name')) {
                 $where[] = qsprintf($conn_r, 'p.name LIKE %~', $request->getStr('name'));
             }
             if ($repository_phid || $request->getStr('path')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T path ON path.packageID = p.id', $path->getTableName());
                 if ($repository_phid) {
                     $where[] = qsprintf($conn_r, 'path.repositoryPHID = %s', $repository_phid);
                 }
                 if ($request->getStr('path')) {
                     $where[] = qsprintf($conn_r, 'path.path LIKE %~ OR %s LIKE CONCAT(path.path, %s)', $request->getStr('path'), $request->getStr('path'), '%');
                 }
             }
             if ($request->getArr('owner')) {
                 $join[] = qsprintf($conn_r, 'JOIN %T o ON o.packageID = p.id', $owner->getTableName());
                 $where[] = qsprintf($conn_r, 'o.userPHID IN (%Ls)', $request->getArr('owner'));
             }
             $data = queryfx_all($conn_r, 'SELECT p.* FROM %T p %Q WHERE %Q GROUP BY p.id', $package->getTableName(), implode(' ', $join), '(' . implode(') AND (', $where) . ')');
             $packages = $package->loadAllFromArray($data);
             $header = 'Search Results';
             $nodata = 'No packages match your query.';
             break;
         case 'owned':
             $data = queryfx_all($package->establishConnection('r'), 'SELECT p.* FROM %T p JOIN %T o ON p.id = o.packageID
         WHERE o.userPHID = %s GROUP BY p.id', $package->getTableName(), $owner->getTableName(), $user->getPHID());
             $packages = $package->loadAllFromArray($data);
             $header = 'Owned Packages';
             $nodata = 'No owned packages';
             break;
         case 'all':
             $packages = $package->loadAll();
             $header = 'All Packages';
             $nodata = 'There are no defined packages.';
             break;
     }
     $content = $this->renderPackageTable($packages, $header, $nodata);
     $filter = new AphrontListFilterView();
     $filter->addButton(phutil_render_tag('a', array('href' => '/owners/new/', 'class' => 'green button'), 'Create New Package'));
     $owners_search_value = array();
     if ($request->getArr('owner')) {
         $phids = $request->getArr('owner');
         $phid = reset($phids);
         $handles = id(new PhabricatorObjectHandleData(array($phid)))->loadHandles();
         $owners_search_value = array($phid => $handles[$phid]->getFullName());
     }
     $callsigns = array('' => '(Any Repository)');
     $repositories = id(new PhabricatorRepository())->loadAllWhere('1 = 1 ORDER BY callsign');
     foreach ($repositories as $repository) {
         $callsigns[$repository->getCallsign()] = $repository->getCallsign() . ': ' . $repository->getName();
     }
     $form = id(new AphrontFormView())->setUser($user)->setAction('/owners/view/search/')->setMethod('GET')->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel('Name')->setValue($request->getStr('name')))->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/usersorprojects/')->setLimit(1)->setName('owner')->setLabel('Owner')->setValue($owners_search_value))->appendChild(id(new AphrontFormSelectControl())->setName('repository')->setLabel('Repository')->setOptions($callsigns)->setValue($request->getStr('repository')))->appendChild(id(new AphrontFormTextControl())->setName('path')->setLabel('Path')->setValue($request->getStr('path')))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search for Packages'));
     $filter->appendChild($form);
     return $this->buildStandardPageResponse(array($filter, $content), array('title' => 'Package Index'));
 }