[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/controller/product/ -> ReleephProductCreateController.php (source)

   1  <?php
   2  
   3  final class ReleephProductCreateController extends ReleephProductController {
   4  
   5    public function processRequest() {
   6      $request = $this->getRequest();
   7      $name = trim($request->getStr('name'));
   8      $trunk_branch = trim($request->getStr('trunkBranch'));
   9      $arc_pr_id = $request->getInt('arcPrID');
  10  
  11      $arc_projects = $this->loadArcProjects();
  12  
  13      $e_name = true;
  14      $e_trunk_branch = true;
  15      $errors = array();
  16  
  17      if ($request->isFormPost()) {
  18        if (!$name) {
  19          $e_name = pht('Required');
  20          $errors[] = pht(
  21            'Your product should have a simple, descriptive name.');
  22        }
  23  
  24        if (!$trunk_branch) {
  25          $e_trunk_branch = pht('Required');
  26          $errors[] = pht(
  27            'You must specify which branch you will be picking from.');
  28        }
  29  
  30        $arc_project = $arc_projects[$arc_pr_id];
  31        $pr_repository = $arc_project->loadRepository();
  32  
  33        if (!$errors) {
  34          $releeph_product = id(new ReleephProject())
  35            ->setName($name)
  36            ->setTrunkBranch($trunk_branch)
  37            ->setRepositoryPHID($pr_repository->getPHID())
  38            ->setArcanistProjectID($arc_project->getID())
  39            ->setCreatedByUserPHID($request->getUser()->getPHID())
  40            ->setIsActive(1);
  41  
  42          try {
  43            $releeph_product->save();
  44  
  45            return id(new AphrontRedirectResponse())
  46              ->setURI($releeph_product->getURI());
  47          } catch (AphrontDuplicateKeyQueryException $ex) {
  48            $e_name = pht('Not Unique');
  49            $errors[] = pht('Another product already uses this name.');
  50          }
  51        }
  52      }
  53  
  54      $arc_project_options = $this->getArcProjectSelectOptions($arc_projects);
  55  
  56      $product_name_input = id(new AphrontFormTextControl())
  57        ->setLabel(pht('Name'))
  58        ->setDisableAutocomplete(true)
  59        ->setName('name')
  60        ->setValue($name)
  61        ->setError($e_name)
  62        ->setCaption(pht('A name like "Thrift" but not "Thrift releases".'));
  63  
  64      $arc_project_input = id(new AphrontFormSelectControl())
  65        ->setLabel(pht('Arc Project'))
  66        ->setName('arcPrID')
  67        ->setValue($arc_pr_id)
  68        ->setCaption(pht(
  69          'If your Arc project isn\'t listed, associate it with a repository %s',
  70          phutil_tag(
  71            'a',
  72            array(
  73              'href' => '/repository/',
  74              'target' => '_blank',
  75            ),
  76            'here')))
  77        ->setOptions($arc_project_options);
  78  
  79      $branch_name_preview = id(new ReleephBranchPreviewView())
  80        ->setLabel(pht('Example Branch'))
  81        ->addControl('projectName', $product_name_input)
  82        ->addControl('arcProjectID', $arc_project_input)
  83        ->addStatic('template', '')
  84        ->addStatic('isSymbolic', false);
  85  
  86      $form = id(new AphrontFormView())
  87        ->setUser($request->getUser())
  88        ->appendChild($product_name_input)
  89        ->appendChild($arc_project_input)
  90        ->appendChild(
  91          id(new AphrontFormTextControl())
  92            ->setLabel(pht('Trunk'))
  93            ->setName('trunkBranch')
  94            ->setValue($trunk_branch)
  95            ->setError($e_trunk_branch)
  96            ->setCaption(pht('The development branch, '.
  97                'from which requests will be picked.')))
  98        ->appendChild($branch_name_preview)
  99        ->appendChild(
 100          id(new AphrontFormSubmitControl())
 101            ->addCancelButton('/releeph/project/')
 102            ->setValue(pht('Create Release Product')));
 103  
 104      $form_box = id(new PHUIObjectBoxView())
 105        ->setHeaderText(pht('Create New Product'))
 106        ->setFormErrors($errors)
 107        ->setForm($form);
 108  
 109      $crumbs = $this->buildApplicationCrumbs();
 110      $crumbs->addTextCrumb(pht('New Product'));
 111  
 112      return $this->buildApplicationPage(
 113        array(
 114          $crumbs,
 115          $form_box,
 116        ),
 117        array(
 118          'title' => pht('Create New Product'),
 119        ));
 120    }
 121  
 122    private function loadArcProjects() {
 123      $viewer = $this->getRequest()->getUser();
 124  
 125      $projects = id(new PhabricatorRepositoryArcanistProjectQuery())
 126        ->setViewer($viewer)
 127        ->needRepositories(true)
 128        ->execute();
 129  
 130      $projects = mfilter($projects, 'getRepository');
 131      $projects = msort($projects, 'getName');
 132  
 133      return $projects;
 134    }
 135  
 136    private function getArcProjectSelectOptions(array $arc_projects) {
 137      assert_instances_of($arc_projects, 'PhabricatorRepositoryArcanistProject');
 138  
 139      $repos = mpull($arc_projects, 'getRepository');
 140      $repos = mpull($repos, null, 'getID');
 141  
 142      $groups = array();
 143      foreach ($arc_projects as $arc_project) {
 144        $id = $arc_project->getID();
 145        $repo_id = $arc_project->getRepository()->getID();
 146        $groups[$repo_id][$id] = $arc_project->getName();
 147      }
 148  
 149      $choices = array();
 150      foreach ($groups as $repo_id => $group) {
 151        $repo_name = $repos[$repo_id]->getName();
 152        $callsign = $repos[$repo_id]->getCallsign();
 153        $name = "r{$callsign} ({$repo_name})";
 154        $choices[$name] = $group;
 155      }
 156  
 157      ksort($choices);
 158  
 159      return $choices;
 160    }
 161  
 162  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1