public function renderExample()
 {
     $filter = new AphrontListFilterView();
     $filter->addButton(phutil_render_tag('a', array('href' => '#', 'class' => 'button green'), 'Create New Thing'));
     $form = new AphrontFormView();
     $form->setUser($this->getRequest()->getUser());
     $form->appendChild(id(new AphrontFormTextControl())->setLabel('Query'))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search'));
     $filter->appendChild($form);
     return $filter;
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $macro_table = new PhabricatorFileImageMacro();
     if ($request->getStr('name') !== null) {
         $macros = $macro_table->loadAllWhere('name LIKE %~', $request->getStr('name'));
     } else {
         $pager = new AphrontPagerView();
         $pager->setOffset($request->getInt('page'));
         $macros = $macro_table->loadAllWhere('1 = 1 ORDER BY id DESC LIMIT %d, %d', $pager->getOffset(), $pager->getPageSize());
         // Get an exact count since the size here is reasonably going to be a few
         // thousand at most in any reasonable case.
         $count = queryfx_one($macro_table->establishConnection('r'), 'SELECT COUNT(*) N FROM %T', $macro_table->getTableName());
         $count = $count['N'];
         $pager->setCount($count);
         $pager->setURI($request->getRequestURI(), 'page');
     }
     $file_phids = mpull($macros, 'getFilePHID');
     $files = array();
     if ($file_phids) {
         $files = id(new PhabricatorFile())->loadAllWhere("phid IN (%Ls)", $file_phids);
         $author_phids = mpull($files, 'getAuthorPHID', 'getPHID');
         $handles = id(new PhabricatorObjectHandleData($author_phids))->loadHandles();
     }
     $files_map = mpull($files, null, 'getPHID');
     $rows = array();
     foreach ($macros as $macro) {
         $file_phid = $macro->getFilePHID();
         $file = idx($files_map, $file_phid);
         $author_link = isset($author_phids[$file_phid]) ? $handles[$author_phids[$file_phid]]->renderLink() : null;
         $rows[] = array(phutil_render_tag('a', array('href' => '/file/macro/edit/' . $macro->getID() . '/'), phutil_escape_html($macro->getName())), $author_link, phutil_render_tag('a', array('href' => $file ? $file->getBestURI() : null, 'target' => '_blank'), phutil_render_tag('img', array('src' => $file ? $file->getBestURI() : null))), javelin_render_tag('a', array('href' => '/file/macro/delete/' . $macro->getID() . '/', 'sigil' => 'workflow', 'class' => 'grey small button'), 'Delete'));
     }
     $table = new AphrontTableView($rows);
     $table->setHeaders(array('Name', 'Author', 'Image', ''));
     $table->setColumnClasses(array('pri', '', 'wide thumb', 'action'));
     $filter_form = id(new AphrontFormView())->setMethod('GET')->setAction('/file/macro/')->setUser($request->getUser())->appendChild(id(new AphrontFormTextControl())->setName('name')->setLabel('Name')->setValue($request->getStr('name')))->appendChild(id(new AphrontFormSubmitControl())->setValue('Filter Image Macros'));
     $filter_view = new AphrontListFilterView();
     $filter_view->appendChild($filter_form);
     $filter_view->addButton(phutil_render_tag('a', array('href' => '/file/macro/edit/', 'class' => 'green button'), 'New Image Macro'));
     $panel = new AphrontPanelView();
     $panel->appendChild($table);
     $panel->setHeader('Image Macros');
     if ($request->getStr('name') === null) {
         $panel->appendChild($pager);
     }
     $side_nav = new PhabricatorFileSideNavView();
     $side_nav->setSelectedFilter('all_macros');
     $side_nav->appendChild($filter_view);
     $side_nav->appendChild($panel);
     return $this->buildStandardPageResponse($side_nav, array('title' => 'Image Macros'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($request->isFormPost()) {
         // Redirect to GET so URIs can be copy/pasted.
         $user_phids = $request->getArr('set_users');
         $proj_phids = $request->getArr('set_projects');
         $task_ids = $request->getStr('set_tasks');
         $user_phids = implode(',', $user_phids);
         $proj_phids = implode(',', $proj_phids);
         $user_phids = nonempty($user_phids, null);
         $proj_phids = nonempty($proj_phids, null);
         $task_ids = nonempty($task_ids, null);
         $uri = $request->getRequestURI()->alter('users', $user_phids)->alter('projects', $proj_phids)->alter('tasks', $task_ids);
         return id(new AphrontRedirectResponse())->setURI($uri);
     }
     $nav = new AphrontSideNavFilterView();
     $nav->setBaseURI(new PhutilURI('/maniphest/view/'));
     $nav->addLabel('User Tasks');
     $nav->addFilter('action', 'Assigned');
     $nav->addFilter('created', 'Created');
     $nav->addFilter('subscribed', 'Subscribed');
     $nav->addFilter('triage', 'Need Triage');
     $nav->addSpacer();
     $nav->addLabel('All Tasks');
     $nav->addFilter('alltriage', 'Need Triage');
     $nav->addFilter('all', 'All Tasks');
     $nav->addSpacer();
     $nav->addFilter('custom', 'Custom');
     $this->view = $nav->selectFilter($this->view, 'action');
     $has_filter = array('action' => true, 'created' => true, 'subscribed' => true, 'triage' => true);
     list($status_map, $status_links) = $this->renderStatusLinks();
     list($grouping, $group_links) = $this->renderGroupLinks();
     list($order, $order_links) = $this->renderOrderLinks();
     $user_phids = $request->getStr('users');
     if (strlen($user_phids)) {
         $user_phids = explode(',', $user_phids);
     } else {
         $user_phids = array($user->getPHID());
     }
     $project_phids = $request->getStr('projects');
     if (strlen($project_phids)) {
         $project_phids = explode(',', $project_phids);
     } else {
         $project_phids = array();
     }
     $task_ids = $request->getStrList('tasks');
     $page = $request->getInt('page');
     $page_size = self::DEFAULT_PAGE_SIZE;
     list($tasks, $handles, $total_count) = $this->loadTasks($user_phids, $project_phids, $task_ids, array('status' => $status_map, 'group' => $grouping, 'order' => $order, 'offset' => $page, 'limit' => $page_size));
     $form = id(new AphrontFormView())->setUser($user)->setAction($request->getRequestURI());
     if (isset($has_filter[$this->view])) {
         $tokens = array();
         foreach ($user_phids as $phid) {
             $tokens[$phid] = $handles[$phid]->getFullName();
         }
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/searchowner/')->setName('set_users')->setLabel('Users')->setValue($tokens));
     }
     if ($this->view == 'custom') {
         $form->appendChild(id(new AphrontFormTextControl())->setName('set_tasks')->setLabel('Task IDs')->setValue(join(',', $task_ids)));
     }
     $tokens = array();
     foreach ($project_phids as $phid) {
         $tokens[$phid] = $handles[$phid]->getFullName();
     }
     $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/projects/')->setName('set_projects')->setLabel('Projects')->setValue($tokens));
     $form->appendChild(id(new AphrontFormToggleButtonsControl())->setLabel('Status')->setValue($status_links))->appendChild(id(new AphrontFormToggleButtonsControl())->setLabel('Group')->setValue($group_links))->appendChild(id(new AphrontFormToggleButtonsControl())->setLabel('Order')->setValue($order_links));
     $form->appendChild(id(new AphrontFormSubmitControl())->setValue('Filter Tasks'));
     $create_uri = new PhutilURI('/maniphest/task/create/');
     if ($project_phids) {
         // If we have project filters selected, use them as defaults for task
         // creation.
         $create_uri->setQueryParam('projects', implode(';', $project_phids));
     }
     $filter = new AphrontListFilterView();
     $filter->addButton(phutil_render_tag('a', array('href' => (string) $create_uri, 'class' => 'green button'), 'Create New Task'));
     $filter->appendChild($form);
     $nav->appendChild($filter);
     $have_tasks = false;
     foreach ($tasks as $group => $list) {
         if (count($list)) {
             $have_tasks = true;
             break;
         }
     }
     require_celerity_resource('maniphest-task-summary-css');
     if (!$have_tasks) {
         $nav->appendChild('<h1 class="maniphest-task-group-header">' . 'No matching tasks.' . '</h1>');
     } else {
         $pager = new AphrontPagerView();
         $pager->setURI($request->getRequestURI(), 'page');
         $pager->setPageSize($page_size);
         $pager->setOffset($page);
         $pager->setCount($total_count);
         $cur = $pager->getOffset() + 1;
         $max = min($pager->getOffset() + $page_size, $total_count);
         $tot = $total_count;
         $cur = number_format($cur);
         $max = number_format($max);
         $tot = number_format($tot);
         $nav->appendChild('<div class="maniphest-total-result-count">' . "Displaying tasks {$cur} - {$max} of {$tot}." . '</div>');
         foreach ($tasks as $group => $list) {
             $task_list = new ManiphestTaskListView();
             $task_list->setUser($user);
             $task_list->setTasks($list);
             $task_list->setHandles($handles);
             $count = number_format(count($list));
             $nav->appendChild('<h1 class="maniphest-task-group-header">' . phutil_escape_html($group) . ' (' . $count . ')' . '</h1>');
             $nav->appendChild($task_list);
         }
         $nav->appendChild($pager);
     }
     return $this->buildStandardPageResponse($nav, array('title' => 'Task List'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($request->isFormPost()) {
         // Redirect to GET so URIs can be copy/pasted.
         $task_ids = $request->getStr('set_tasks');
         $task_ids = nonempty($task_ids, null);
         $search_text = $request->getStr('set_search');
         $search_text = nonempty($search_text, null);
         $min_priority = $request->getInt('set_lpriority');
         $min_priority = nonempty($min_priority, null);
         $max_priority = $request->getInt('set_hpriority');
         $max_priority = nonempty($max_priority, null);
         $uri = $request->getRequestURI()->alter('users', $this->getArrToStrList('set_users'))->alter('projects', $this->getArrToStrList('set_projects'))->alter('xprojects', $this->getArrToStrList('set_xprojects'))->alter('owners', $this->getArrToStrList('set_owners'))->alter('authors', $this->getArrToStrList('set_authors'))->alter('lpriority', $min_priority)->alter('hpriority', $max_priority)->alter('tasks', $task_ids)->alter('search', $search_text);
         return id(new AphrontRedirectResponse())->setURI($uri);
     }
     $nav = $this->buildBaseSideNav();
     $has_filter = array('action' => true, 'created' => true, 'subscribed' => true, 'triage' => true, 'projecttriage' => true, 'projectall' => true);
     $query = null;
     $key = $request->getStr('key');
     if (!$key && !$this->view) {
         if ($this->getDefaultQuery()) {
             $key = $this->getDefaultQuery()->getQueryKey();
         }
     }
     if ($key) {
         $query = id(new PhabricatorSearchQuery())->loadOneWhere('queryKey = %s', $key);
     }
     // If the user is running a saved query, load query parameters from that
     // query. Otherwise, build a new query object from the HTTP request.
     if ($query) {
         $nav->selectFilter('Q:' . $query->getQueryKey(), 'custom');
         $this->view = 'custom';
     } else {
         $this->view = $nav->selectFilter($this->view, 'action');
         $query = $this->buildQueryFromRequest();
     }
     // Execute the query.
     list($tasks, $handles, $total_count) = self::loadTasks($query);
     // Extract information we need to render the filters from the query.
     $search_text = $query->getParameter('fullTextSearch');
     $user_phids = $query->getParameter('userPHIDs', array());
     $task_ids = $query->getParameter('taskIDs', array());
     $owner_phids = $query->getParameter('ownerPHIDs', array());
     $author_phids = $query->getParameter('authorPHIDs', array());
     $project_phids = $query->getParameter('projectPHIDs', array());
     $exclude_project_phids = $query->getParameter('excludeProjectPHIDs', array());
     $low_priority = $query->getParameter('lowPriority');
     $high_priority = $query->getParameter('highPriority');
     $page_size = $query->getParameter('limit');
     $page = $query->getParameter('offset');
     $q_status = $query->getParameter('status');
     $q_group = $query->getParameter('group');
     $q_order = $query->getParameter('order');
     $form = id(new AphrontFormView())->setUser($user)->setAction($request->getRequestURI()->alter('key', null)->alter($this->getStatusRequestKey(), $this->getStatusRequestValue($q_status))->alter($this->getOrderRequestKey(), $this->getOrderRequestValue($q_order))->alter($this->getGroupRequestKey(), $this->getGroupRequestValue($q_group)));
     if (isset($has_filter[$this->view])) {
         $tokens = array();
         foreach ($user_phids as $phid) {
             $tokens[$phid] = $handles[$phid]->getFullName();
         }
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/searchowner/')->setName('set_users')->setLabel('Users')->setValue($tokens));
     }
     if ($this->view == 'custom') {
         $form->appendChild(id(new AphrontFormTextControl())->setName('set_search')->setLabel('Search')->setValue($search_text));
         $form->appendChild(id(new AphrontFormTextControl())->setName('set_tasks')->setLabel('Task IDs')->setValue(join(',', $task_ids)));
         $tokens = array();
         foreach ($owner_phids as $phid) {
             $tokens[$phid] = $handles[$phid]->getFullName();
         }
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/searchowner/')->setName('set_owners')->setLabel('Owners')->setValue($tokens));
         $tokens = array();
         foreach ($author_phids as $phid) {
             $tokens[$phid] = $handles[$phid]->getFullName();
         }
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/users/')->setName('set_authors')->setLabel('Authors')->setValue($tokens));
     }
     $tokens = array();
     foreach ($project_phids as $phid) {
         $tokens[$phid] = $handles[$phid]->getFullName();
     }
     if ($this->view != 'projectall' && $this->view != 'projecttriage') {
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/searchproject/')->setName('set_projects')->setLabel('Projects')->setValue($tokens));
     }
     if ($this->view == 'custom') {
         $tokens = array();
         foreach ($exclude_project_phids as $phid) {
             $tokens[$phid] = $handles[$phid]->getFullName();
         }
         $form->appendChild(id(new AphrontFormTokenizerControl())->setDatasource('/typeahead/common/projects/')->setName('set_xprojects')->setLabel('Exclude Projects')->setValue($tokens));
         $priority = ManiphestTaskPriority::getLowestPriority();
         if ($low_priority) {
             $priority = $low_priority;
         }
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Min Priority')->setName('set_lpriority')->setValue($priority)->setOptions(array_reverse(ManiphestTaskPriority::getTaskPriorityMap(), true)));
         $priority = ManiphestTaskPriority::getHighestPriority();
         if ($high_priority) {
             $priority = $high_priority;
         }
         $form->appendChild(id(new AphrontFormSelectControl())->setLabel('Max Priority')->setName('set_hpriority')->setValue($priority)->setOptions(ManiphestTaskPriority::getTaskPriorityMap()));
     }
     $form->appendChild($this->renderStatusControl($q_status))->appendChild($this->renderGroupControl($q_group))->appendChild($this->renderOrderControl($q_order));
     $submit = id(new AphrontFormSubmitControl())->setValue('Filter Tasks');
     // Only show "Save..." for novel queries which have some kind of query
     // parameters set.
     if ($this->view === 'custom' && empty($key) && $request->getRequestURI()->getQueryParams()) {
         $submit->addCancelButton('/maniphest/custom/edit/?key=' . $query->getQueryKey(), 'Save Custom Query...');
     }
     $form->appendChild($submit);
     $create_uri = new PhutilURI('/maniphest/task/create/');
     if ($project_phids) {
         // If we have project filters selected, use them as defaults for task
         // creation.
         $create_uri->setQueryParam('projects', implode(';', $project_phids));
     }
     $filter = new AphrontListFilterView();
     $filter->addButton(phutil_render_tag('a', array('href' => (string) $create_uri, 'class' => 'green button'), 'Create New Task'));
     if (empty($key)) {
         $filter->appendChild($form);
     }
     $nav->appendChild($filter);
     $have_tasks = false;
     foreach ($tasks as $group => $list) {
         if (count($list)) {
             $have_tasks = true;
             break;
         }
     }
     require_celerity_resource('maniphest-task-summary-css');
     $list_container = new AphrontNullView();
     $list_container->appendChild('<div class="maniphest-list-container">');
     if (!$have_tasks) {
         $list_container->appendChild('<h1 class="maniphest-task-group-header">' . 'No matching tasks.' . '</h1>');
     } else {
         $pager = new AphrontPagerView();
         $pager->setURI($request->getRequestURI(), 'offset');
         $pager->setPageSize($page_size);
         $pager->setOffset($page);
         $pager->setCount($total_count);
         $cur = $pager->getOffset() + 1;
         $max = min($pager->getOffset() + $page_size, $total_count);
         $tot = $total_count;
         $cur = number_format($cur);
         $max = number_format($max);
         $tot = number_format($tot);
         $list_container->appendChild('<div class="maniphest-total-result-count">' . "Displaying tasks {$cur} - {$max} of {$tot}." . '</div>');
         $selector = new AphrontNullView();
         $group = $query->getParameter('group');
         $order = $query->getParameter('order');
         $is_draggable = $group == 'priority' || $group == 'none' && $order == 'priority';
         $lists = new AphrontNullView();
         $lists->appendChild('<div class="maniphest-group-container">');
         foreach ($tasks as $group => $list) {
             $task_list = new ManiphestTaskListView();
             $task_list->setShowBatchControls(true);
             if ($is_draggable) {
                 $task_list->setShowSubpriorityControls(true);
             }
             $task_list->setUser($user);
             $task_list->setTasks($list);
             $task_list->setHandles($handles);
             $count = number_format(count($list));
             $lists->appendChild(javelin_render_tag('h1', array('class' => 'maniphest-task-group-header', 'sigil' => 'task-group', 'meta' => array('priority' => head($list)->getPriority())), phutil_escape_html($group) . ' (' . $count . ')'));
             $lists->appendChild($task_list);
         }
         $lists->appendChild('</div>');
         $selector->appendChild($lists);
         $selector->appendChild($this->renderBatchEditor($query));
         $form_id = celerity_generate_unique_node_id();
         $selector = phabricator_render_form($user, array('method' => 'POST', 'action' => '/maniphest/batch/', 'id' => $form_id), $selector->render());
         $list_container->appendChild($selector);
         $list_container->appendChild($pager);
         Javelin::initBehavior('maniphest-subpriority-editor', array('root' => $form_id, 'uri' => '/maniphest/subpriority/'));
     }
     $list_container->appendChild('</div>');
     $nav->appendChild($list_container);
     return $this->buildStandardPageResponse($nav, array('title' => 'Task List'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $viewer_is_anonymous = !$user->isLoggedIn();
     $params = array_filter(array('status' => $request->getStr('status'), 'order' => $request->getStr('order')));
     $default_filter = $viewer_is_anonymous ? 'all' : 'active';
     $filters = $this->getFilters();
     $this->filter = $this->selectFilter($filters, $this->filter, $default_filter);
     // Redirect from search to canonical URL.
     $phid_arr = $request->getArr('view_user');
     if ($phid_arr) {
         $view_user = id(new PhabricatorUser())->loadOneWhere('phid = %s', head($phid_arr));
         $base_uri = '/differential/filter/' . $this->filter . '/';
         if ($view_user) {
             // This is a user, so generate a pretty URI.
             $uri = $base_uri . phutil_escape_uri($view_user->getUserName()) . '/';
         } else {
             // We're assuming this is a mailing list, generate an ugly URI.
             $uri = $base_uri;
             $params['phid'] = head($phid_arr);
         }
         $uri = new PhutilURI($uri);
         $uri->setQueryParams($params);
         return id(new AphrontRedirectResponse())->setURI($uri);
     }
     $uri = new PhutilURI('/differential/filter/' . $this->filter . '/');
     $uri->setQueryParams($params);
     $username = '';
     if ($this->username) {
         $view_user = id(new PhabricatorUser())->loadOneWhere('userName = %s', $this->username);
         if (!$view_user) {
             return new Aphront404Response();
         }
         $username = phutil_escape_uri($this->username) . '/';
         $uri->setPath('/differential/filter/' . $this->filter . '/' . $username);
         $params['phid'] = $view_user->getPHID();
     } else {
         $phid = $request->getStr('phid');
         if (strlen($phid)) {
             $params['phid'] = $phid;
         }
     }
     // Fill in the defaults we'll actually use for calculations if any
     // parameters are missing.
     $params += array('phid' => $user->getPHID(), 'status' => 'all', 'order' => 'modified');
     $side_nav = new AphrontSideNavView();
     foreach ($filters as $filter) {
         list($filter_name, $display_name) = $filter;
         if ($filter_name) {
             $href = clone $uri;
             $href->setPath('/differential/filter/' . $filter_name . '/' . $username);
             if ($filter_name == $this->filter) {
                 $class = 'aphront-side-nav-selected';
             } else {
                 $class = null;
             }
             $item = phutil_render_tag('a', array('href' => (string) $href, 'class' => $class), phutil_escape_html($display_name));
         } else {
             $item = phutil_render_tag('span', array(), phutil_escape_html($display_name));
         }
         $side_nav->addNavItem($item);
     }
     $panels = array();
     $handles = array();
     $controls = $this->getFilterControls($this->filter);
     if ($this->getFilterRequiresUser($this->filter) && !$params['phid']) {
         // In the anonymous case, we still want to let you see some user's
         // list, but we don't have a default PHID to provide (normally, we use
         // the viewing user's). Show a warning instead.
         $warning = new AphrontErrorView();
         $warning->setSeverity(AphrontErrorView::SEVERITY_WARNING);
         $warning->setTitle('User Required');
         $warning->appendChild('This filter requires that a user be specified above.');
         $panels[] = $warning;
     } else {
         $query = $this->buildQuery($this->filter, $params['phid']);
         $pager = null;
         if ($this->getFilterAllowsPaging($this->filter)) {
             $pager = new AphrontPagerView();
             $pager->setOffset($request->getInt('page'));
             $pager->setPageSize(1000);
             $pager->setURI($uri, 'page');
             $query->setOffset($pager->getOffset());
             $query->setLimit($pager->getPageSize() + 1);
         }
         foreach ($controls as $control) {
             $this->applyControlToQuery($control, $query, $params);
         }
         $revisions = $query->execute();
         if ($pager) {
             $revisions = $pager->sliceResults($revisions);
         }
         $views = $this->buildViews($this->filter, $params['phid'], $revisions);
         $view_objects = array();
         foreach ($views as $view) {
             if (empty($view['special'])) {
                 $view_objects[] = $view['view'];
             }
         }
         $phids = array_mergev(mpull($view_objects, 'getRequiredHandlePHIDs'));
         $phids[] = $params['phid'];
         $handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
         foreach ($views as $view) {
             if (empty($view['special'])) {
                 $view['view']->setHandles($handles);
             }
             $panel = new AphrontPanelView();
             $panel->setHeader($view['title']);
             $panel->appendChild($view['view']);
             if ($pager) {
                 $panel->appendChild($pager);
             }
             $panels[] = $panel;
         }
     }
     $filter_form = id(new AphrontFormView())->setMethod('GET')->setAction('/differential/filter/' . $this->filter . '/')->setUser($user);
     foreach ($controls as $control) {
         $control_view = $this->renderControl($control, $handles, $uri, $params);
         $filter_form->appendChild($control_view);
     }
     $filter_form->addHiddenInput('status', $params['status'])->addHiddenInput('order', $params['order'])->appendChild(id(new AphrontFormSubmitControl())->setValue('Filter Revisions'));
     $filter_view = new AphrontListFilterView();
     $filter_view->appendChild($filter_form);
     if (!$viewer_is_anonymous) {
         $create_uri = new PhutilURI('/differential/diff/create/');
         $filter_view->addButton(phutil_render_tag('a', array('href' => (string) $create_uri, 'class' => 'green button'), 'Create Revision'));
     }
     $side_nav->appendChild($filter_view);
     foreach ($panels as $panel) {
         $side_nav->appendChild($panel);
     }
     return $this->buildStandardPageResponse($side_nav, array('title' => 'Differential Home'));
 }
 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'));
 }