~ubuntu-branches/ubuntu/wily/phabricator/wily

« back to all changes in this revision

Viewing changes to phabricator/src/applications/owners/controller/PhabricatorOwnersEditController.php

  • Committer: Package Import Robot
  • Author(s): Richard Sellam
  • Date: 2015-06-13 10:52:10 UTC
  • mfrom: (0.30.1) (0.29.1) (0.17.4) (2.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20150613105210-5uirr7tvnk0n6e6y
Tags: 0~git20150613-1
* New snapshot release (closes: #787805)
* fixed typo in logrotate script (closes: #787645)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
final class PhabricatorOwnersEditController
4
4
  extends PhabricatorOwnersController {
5
5
 
6
 
  private $id;
7
 
 
8
 
  public function willProcessRequest(array $data) {
9
 
    $this->id = idx($data, 'id');
10
 
  }
11
 
 
12
 
  public function processRequest() {
13
 
    $request = $this->getRequest();
14
 
    $user = $request->getUser();
15
 
 
16
 
    if ($this->id) {
17
 
      $package = id(new PhabricatorOwnersPackage())->load($this->id);
 
6
  public function handleRequest(AphrontRequest $request) {
 
7
    $viewer = $request->getUser();
 
8
 
 
9
    $id = $request->getURIData('id');
 
10
    if ($id) {
 
11
      $package = id(new PhabricatorOwnersPackageQuery())
 
12
        ->setViewer($viewer)
 
13
        ->withIDs(array($id))
 
14
        ->requireCapabilities(
 
15
          array(
 
16
            PhabricatorPolicyCapability::CAN_VIEW,
 
17
            // TODO: Support this capability.
 
18
            // PhabricatorPolicyCapability::CAN_EDIT,
 
19
          ))
 
20
        ->executeOne();
18
21
      if (!$package) {
19
22
        return new Aphront404Response();
20
23
      }
 
24
      $is_new = false;
21
25
    } else {
22
 
      $package = new PhabricatorOwnersPackage();
23
 
      $package->setPrimaryOwnerPHID($user->getPHID());
 
26
      $package = PhabricatorOwnersPackage::initializeNewPackage($viewer);
 
27
      $is_new = true;
24
28
    }
25
29
 
26
30
    $e_name = true;
27
31
    $e_primary = true;
28
32
 
 
33
    $v_name = $package->getName();
 
34
    $v_primary = $package->getPrimaryOwnerPHID();
 
35
    // TODO: Pull these off needOwners() on the Query.
 
36
    $v_owners = mpull($package->loadOwners(), 'getUserPHID');
 
37
    $v_auditing = $package->getAuditingEnabled();
 
38
    $v_description = $package->getDescription();
 
39
 
 
40
 
29
41
    $errors = array();
30
 
 
31
42
    if ($request->isFormPost()) {
32
 
      $package->setName($request->getStr('name'));
33
 
      $package->setDescription($request->getStr('description'));
34
 
      $old_auditing_enabled = $package->getAuditingEnabled();
35
 
      $package->setAuditingEnabled(
36
 
        ($request->getStr('auditing') === 'enabled')
37
 
          ? 1
38
 
          : 0);
39
 
 
40
 
      $primary = $request->getArr('primary');
41
 
      $primary = reset($primary);
42
 
      $old_primary = $package->getPrimaryOwnerPHID();
43
 
      $package->setPrimaryOwnerPHID($primary);
44
 
 
45
 
      $owners = $request->getArr('owners');
46
 
      if ($primary) {
47
 
        array_unshift($owners, $primary);
48
 
      }
49
 
      $owners = array_unique($owners);
50
 
 
51
 
      $paths = $request->getArr('path');
52
 
      $repos = $request->getArr('repo');
53
 
      $excludes = $request->getArr('exclude');
54
 
 
55
 
      $path_refs = array();
56
 
      for ($ii = 0; $ii < count($paths); $ii++) {
57
 
        if (empty($paths[$ii]) || empty($repos[$ii])) {
58
 
          continue;
59
 
        }
60
 
        $path_refs[] = array(
61
 
          'repositoryPHID'  => $repos[$ii],
62
 
          'path'            => $paths[$ii],
63
 
          'excluded'        => $excludes[$ii],
64
 
        );
65
 
      }
66
 
 
67
 
      if (!strlen($package->getName())) {
68
 
        $e_name = pht('Required');
69
 
        $errors[] = pht('Package name is required.');
70
 
      } else {
71
 
        $e_name = null;
72
 
      }
73
 
 
74
 
      if (!$package->getPrimaryOwnerPHID()) {
75
 
        $e_primary = pht('Required');
76
 
        $errors[] = pht('Package must have a primary owner.');
77
 
      } else {
78
 
        $e_primary = null;
79
 
      }
80
 
 
81
 
      if (!$path_refs) {
82
 
        $errors[] = pht('Package must include at least one path.');
83
 
      }
84
 
 
85
 
      if (!$errors) {
86
 
        $package->attachUnsavedOwners($owners);
87
 
        $package->attachUnsavedPaths($path_refs);
88
 
        $package->attachOldAuditingEnabled($old_auditing_enabled);
89
 
        $package->attachOldPrimaryOwnerPHID($old_primary);
90
 
        try {
91
 
          id(new PhabricatorOwnersPackageEditor())
92
 
            ->setActor($user)
93
 
            ->setPackage($package)
94
 
            ->save();
95
 
          return id(new AphrontRedirectResponse())
96
 
            ->setURI('/owners/package/'.$package->getID().'/');
97
 
        } catch (AphrontDuplicateKeyQueryException $ex) {
98
 
          $e_name = pht('Duplicate');
99
 
          $errors[] = pht('Package name must be unique.');
100
 
        }
101
 
      }
102
 
    } else {
103
 
      $owners = $package->loadOwners();
104
 
      $owners = mpull($owners, 'getUserPHID');
105
 
 
106
 
      $paths = $package->loadPaths();
107
 
      $path_refs = array();
108
 
      foreach ($paths as $path) {
109
 
        $path_refs[] = array(
110
 
          'repositoryPHID' => $path->getRepositoryPHID(),
111
 
          'path' => $path->getPath(),
112
 
          'excluded' => $path->getExcluded(),
113
 
        );
 
43
      $xactions = array();
 
44
 
 
45
      $v_name = $request->getStr('name');
 
46
      $v_primary = head($request->getArr('primary'));
 
47
      $v_owners = $request->getArr('owners');
 
48
      $v_auditing = ($request->getStr('auditing') == 'enabled');
 
49
      $v_description = $request->getStr('description');
 
50
 
 
51
      if ($v_primary) {
 
52
        $v_owners[] = $v_primary;
 
53
        $v_owners = array_unique($v_owners);
 
54
      }
 
55
 
 
56
      $type_name = PhabricatorOwnersPackageTransaction::TYPE_NAME;
 
57
      $type_primary = PhabricatorOwnersPackageTransaction::TYPE_PRIMARY;
 
58
      $type_owners = PhabricatorOwnersPackageTransaction::TYPE_OWNERS;
 
59
      $type_auditing = PhabricatorOwnersPackageTransaction::TYPE_AUDITING;
 
60
      $type_description = PhabricatorOwnersPackageTransaction::TYPE_DESCRIPTION;
 
61
 
 
62
      $xactions[] = id(new PhabricatorOwnersPackageTransaction())
 
63
        ->setTransactionType($type_name)
 
64
        ->setNewValue($v_name);
 
65
 
 
66
      $xactions[] = id(new PhabricatorOwnersPackageTransaction())
 
67
        ->setTransactionType($type_primary)
 
68
        ->setNewValue($v_primary);
 
69
 
 
70
      $xactions[] = id(new PhabricatorOwnersPackageTransaction())
 
71
        ->setTransactionType($type_owners)
 
72
        ->setNewValue($v_owners);
 
73
 
 
74
      $xactions[] = id(new PhabricatorOwnersPackageTransaction())
 
75
        ->setTransactionType($type_auditing)
 
76
        ->setNewValue($v_auditing);
 
77
 
 
78
      $xactions[] = id(new PhabricatorOwnersPackageTransaction())
 
79
        ->setTransactionType($type_description)
 
80
        ->setNewValue($v_description);
 
81
 
 
82
      $editor = id(new PhabricatorOwnersPackageTransactionEditor())
 
83
        ->setActor($viewer)
 
84
        ->setContentSourceFromRequest($request)
 
85
        ->setContinueOnNoEffect(true);
 
86
 
 
87
      try {
 
88
        $editor->applyTransactions($package, $xactions);
 
89
 
 
90
        $id = $package->getID();
 
91
        if ($is_new) {
 
92
          $next_uri = '/owners/paths/'.$id.'/';
 
93
        } else {
 
94
          $next_uri = '/owners/package/'.$id.'/';
 
95
        }
 
96
 
 
97
        return id(new AphrontRedirectResponse())->setURI($next_uri);
 
98
      } catch (AphrontDuplicateKeyQueryException $ex) {
 
99
        $e_name = pht('Duplicate');
 
100
        $errors[] = pht('Package name must be unique.');
 
101
      } catch (PhabricatorApplicationTransactionValidationException $ex) {
 
102
        $validation_exception = $ex;
 
103
 
 
104
        $e_name = $ex->getShortMessage($type_name);
 
105
        $e_primary = $ex->getShortMessage($type_primary);
114
106
      }
115
107
    }
116
108
 
117
 
    $primary = $package->getPrimaryOwnerPHID();
118
 
    if ($primary) {
119
 
      $value_primary_owner = array($primary);
 
109
    if ($v_primary) {
 
110
      $value_primary_owner = array($v_primary);
120
111
    } else {
121
112
      $value_primary_owner = array();
122
113
    }
123
114
 
124
 
    if ($package->getID()) {
 
115
    if ($is_new) {
 
116
      $cancel_uri = '/owners/';
 
117
      $title = pht('New Package');
 
118
      $button_text = pht('Continue');
 
119
    } else {
 
120
      $cancel_uri = '/owners/package/'.$package->getID().'/';
125
121
      $title = pht('Edit Package');
126
 
      $side_nav_filter = 'edit/'.$this->id;
127
 
    } else {
128
 
      $title = pht('New Package');
129
 
      $side_nav_filter = 'new';
130
 
    }
131
 
    $this->setSideNavFilter($side_nav_filter);
132
 
 
133
 
    $repos = id(new PhabricatorRepositoryQuery())
134
 
      ->setViewer($user)
135
 
      ->execute();
136
 
 
137
 
    $default_paths = array();
138
 
    foreach ($repos as $repo) {
139
 
      $default_path = $repo->getDetail('default-owners-path');
140
 
      if ($default_path) {
141
 
        $default_paths[$repo->getPHID()] = $default_path;
142
 
      }
143
 
    }
144
 
 
145
 
    $repos = mpull($repos, 'getCallsign', 'getPHID');
146
 
    asort($repos);
147
 
 
148
 
    $template = new AphrontTypeaheadTemplateView();
149
 
    $template = $template->render();
150
 
 
151
 
    Javelin::initBehavior(
152
 
      'owners-path-editor',
153
 
      array(
154
 
        'root'                => 'path-editor',
155
 
        'table'               => 'paths',
156
 
        'add_button'          => 'addpath',
157
 
        'repositories'        => $repos,
158
 
        'input_template'      => $template,
159
 
        'pathRefs'            => $path_refs,
160
 
 
161
 
        'completeURI'         => '/diffusion/services/path/complete/',
162
 
        'validateURI'         => '/diffusion/services/path/validate/',
163
 
 
164
 
        'repositoryDefaultPaths' => $default_paths,
165
 
      ));
166
 
 
167
 
    require_celerity_resource('owners-path-editor-css');
168
 
 
169
 
    $cancel_uri = $package->getID()
170
 
      ? '/owners/package/'.$package->getID().'/'
171
 
      : '/owners/';
 
122
      $button_text = pht('Save Package');
 
123
    }
172
124
 
173
125
    $form = id(new AphrontFormView())
174
 
      ->setUser($user)
 
126
      ->setUser($viewer)
175
127
      ->appendChild(
176
128
        id(new AphrontFormTextControl())
177
129
          ->setLabel(pht('Name'))
178
130
          ->setName('name')
179
 
          ->setValue($package->getName())
 
131
          ->setValue($v_name)
180
132
          ->setError($e_name))
181
133
      ->appendControl(
182
134
        id(new AphrontFormTokenizerControl())
191
143
          ->setDatasource(new PhabricatorProjectOrUserDatasource())
192
144
          ->setLabel(pht('Owners'))
193
145
          ->setName('owners')
194
 
          ->setValue($owners))
 
146
          ->setValue($v_owners))
195
147
      ->appendChild(
196
148
        id(new AphrontFormSelectControl())
197
149
          ->setName('auditing')
202
154
              'this package will be reviewed to make sure an owner '.
203
155
              'of the package is involved and the commit message has '.
204
156
              'a valid revision, reviewed by, and author.'))
205
 
          ->setOptions(array(
206
 
            'disabled'  => pht('Disabled'),
207
 
            'enabled'   => pht('Enabled'),
208
 
          ))
209
 
          ->setValue(
210
 
            $package->getAuditingEnabled()
211
 
              ? 'enabled'
212
 
              : 'disabled'))
213
 
      ->appendChild(
214
 
        id(new PHUIFormInsetView())
215
 
          ->setTitle(pht('Paths'))
216
 
          ->addDivAttributes(array('id' => 'path-editor'))
217
 
          ->setRightButton(javelin_tag(
218
 
              'a',
219
 
              array(
220
 
                'href' => '#',
221
 
                'class' => 'button green',
222
 
                'sigil' => 'addpath',
223
 
                'mustcapture' => true,
224
 
              ),
225
 
              pht('Add New Path')))
226
 
          ->setDescription(
227
 
            pht(
228
 
              'Specify the files and directories which comprise '.
229
 
              'this package.'))
230
 
          ->setContent(javelin_tag(
231
 
              'table',
232
 
              array(
233
 
                'class' => 'owners-path-editor-table',
234
 
                'sigil' => 'paths',
235
 
              ),
236
 
              '')))
237
 
      ->appendChild(
238
 
        id(new AphrontFormTextAreaControl())
 
157
          ->setOptions(
 
158
            array(
 
159
              'disabled'  => pht('Disabled'),
 
160
              'enabled'   => pht('Enabled'),
 
161
            ))
 
162
          ->setValue(($v_auditing ? 'enabled' : 'disabled')))
 
163
      ->appendChild(
 
164
        id(new PhabricatorRemarkupControl())
 
165
          ->setUser($viewer)
239
166
          ->setLabel(pht('Description'))
240
167
          ->setName('description')
241
 
          ->setValue($package->getDescription()))
 
168
          ->setValue($v_description))
242
169
      ->appendChild(
243
170
        id(new AphrontFormSubmitControl())
244
171
          ->addCancelButton($cancel_uri)
245
 
          ->setValue(pht('Save Package')));
 
172
          ->setValue($button_text));
246
173
 
247
174
    $form_box = id(new PHUIObjectBoxView())
248
175
      ->setHeaderText($title)
251
178
 
252
179
    $crumbs = $this->buildApplicationCrumbs();
253
180
    if ($package->getID()) {
254
 
      $crumbs->addTextCrumb(pht('Edit %s', $package->getName()));
 
181
      $crumbs->addTextCrumb(
 
182
        $package->getName(),
 
183
        $this->getApplicationURI('package/'.$package->getID().'/'));
 
184
      $crumbs->addTextCrumb(pht('Edit'));
255
185
    } else {
256
186
      $crumbs->addTextCrumb(pht('New Package'));
257
187
    }
258
188
 
259
 
    $nav = $this->buildSideNavView();
260
 
    $nav->appendChild($crumbs);
261
 
    $nav->appendChild($form_box);
262
 
 
263
189
    return $this->buildApplicationPage(
264
190
      array(
265
 
        $nav,
 
191
        $crumbs,
 
192
        $form_box,
266
193
      ),
267
194
      array(
268
195
        'title' => $title,
269
196
      ));
270
197
  }
271
198
 
272
 
  protected function getExtraPackageViews(AphrontSideNavFilterView $view) {
273
 
    if ($this->id) {
274
 
      $view->addFilter('edit/'.$this->id, pht('Edit'));
275
 
    } else {
276
 
      $view->addFilter('new', pht('New'));
277
 
    }
278
 
  }
279
199
}