[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/slowvote/controller/ -> PhabricatorSlowvoteEditController.php (source)

   1  <?php
   2  
   3  final class PhabricatorSlowvoteEditController
   4    extends PhabricatorSlowvoteController {
   5  
   6    private $id;
   7  
   8    public function willProcessRequest(array $data) {
   9      $this->id = idx($data, 'id');
  10    }
  11  
  12    public function processRequest() {
  13  
  14      $request = $this->getRequest();
  15      $user = $request->getUser();
  16  
  17      if ($this->id) {
  18        $poll = id(new PhabricatorSlowvoteQuery())
  19          ->setViewer($user)
  20          ->withIDs(array($this->id))
  21          ->requireCapabilities(
  22            array(
  23              PhabricatorPolicyCapability::CAN_VIEW,
  24              PhabricatorPolicyCapability::CAN_EDIT,
  25            ))
  26          ->executeOne();
  27        if (!$poll) {
  28          return new Aphront404Response();
  29        }
  30        $is_new = false;
  31      } else {
  32        $poll = PhabricatorSlowvotePoll::initializeNewPoll($user);
  33        $is_new = true;
  34      }
  35  
  36      if ($is_new) {
  37        $v_projects = array();
  38      } else {
  39        $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
  40          $poll->getPHID(),
  41          PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
  42        $v_projects = array_reverse($v_projects);
  43      }
  44  
  45      $e_question = true;
  46      $e_response = true;
  47      $errors = array();
  48  
  49      $v_question = $poll->getQuestion();
  50      $v_description = $poll->getDescription();
  51      $v_responses = $poll->getResponseVisibility();
  52      $v_shuffle = $poll->getShuffle();
  53  
  54      $responses = $request->getArr('response');
  55      if ($request->isFormPost()) {
  56        $v_question = $request->getStr('question');
  57        $v_description = $request->getStr('description');
  58        $v_responses = (int)$request->getInt('responses');
  59        $v_shuffle = (int)$request->getBool('shuffle');
  60        $v_view_policy = $request->getStr('viewPolicy');
  61        $v_projects = $request->getArr('projects');
  62  
  63        if ($is_new) {
  64          $poll->setMethod($request->getInt('method'));
  65        }
  66  
  67        if (!strlen($v_question)) {
  68          $e_question = pht('Required');
  69          $errors[] = pht('You must ask a poll question.');
  70        } else {
  71          $e_question = null;
  72        }
  73  
  74        if ($is_new) {
  75          $responses = array_filter($responses);
  76          if (empty($responses)) {
  77            $errors[] = pht('You must offer at least one response.');
  78            $e_response = pht('Required');
  79          } else {
  80            $e_response = null;
  81          }
  82        }
  83  
  84        $xactions = array();
  85        $template = id(new PhabricatorSlowvoteTransaction());
  86  
  87        $xactions[] = id(clone $template)
  88          ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_QUESTION)
  89          ->setNewValue($v_question);
  90  
  91        $xactions[] = id(clone $template)
  92          ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION)
  93          ->setNewValue($v_description);
  94  
  95        $xactions[] = id(clone $template)
  96          ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_RESPONSES)
  97          ->setNewValue($v_responses);
  98  
  99        $xactions[] = id(clone $template)
 100          ->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_SHUFFLE)
 101          ->setNewValue($v_shuffle);
 102  
 103        $xactions[] = id(clone $template)
 104          ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
 105          ->setNewValue($v_view_policy);
 106  
 107        if (empty($errors)) {
 108          $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
 109          $xactions[] = id(new PhabricatorSlowvoteTransaction())
 110            ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
 111            ->setMetadataValue('edge:type', $proj_edge_type)
 112            ->setNewValue(array('=' => array_fuse($v_projects)));
 113  
 114          $editor = id(new PhabricatorSlowvoteEditor())
 115            ->setActor($user)
 116            ->setContinueOnNoEffect(true)
 117            ->setContentSourceFromRequest($request);
 118  
 119          $xactions = $editor->applyTransactions($poll, $xactions);
 120  
 121          if ($is_new) {
 122            $poll->save();
 123  
 124            foreach ($responses as $response) {
 125              $option = new PhabricatorSlowvoteOption();
 126              $option->setName($response);
 127              $option->setPollID($poll->getID());
 128              $option->save();
 129            }
 130          }
 131  
 132          return id(new AphrontRedirectResponse())
 133            ->setURI('/V'.$poll->getID());
 134        } else {
 135          $poll->setViewPolicy($v_view_policy);
 136        }
 137      }
 138  
 139      $instructions =
 140        phutil_tag(
 141          'p',
 142          array(
 143            'class' => 'aphront-form-instructions',
 144          ),
 145          pht('Resolve issues and build consensus through '.
 146            'protracted deliberation.'));
 147  
 148      if ($v_projects) {
 149        $project_handles = $this->loadViewerHandles($v_projects);
 150      } else {
 151        $project_handles = array();
 152      }
 153  
 154      $form = id(new AphrontFormView())
 155        ->setUser($user)
 156        ->appendChild($instructions)
 157        ->appendChild(
 158          id(new AphrontFormTextAreaControl())
 159            ->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
 160            ->setLabel(pht('Question'))
 161            ->setName('question')
 162            ->setValue($v_question)
 163            ->setError($e_question))
 164        ->appendChild(
 165          id(new PhabricatorRemarkupControl())
 166            ->setUser($user)
 167            ->setLabel(pht('Description'))
 168            ->setName('description')
 169            ->setValue($v_description))
 170        ->appendChild(
 171          id(new AphrontFormTokenizerControl())
 172            ->setLabel(pht('Projects'))
 173            ->setName('projects')
 174            ->setValue($project_handles)
 175            ->setDatasource(new PhabricatorProjectDatasource()));
 176  
 177      if ($is_new) {
 178        for ($ii = 0; $ii < 10; $ii++) {
 179          $n = ($ii + 1);
 180          $response = id(new AphrontFormTextControl())
 181            ->setLabel(pht('Response %d', $n))
 182            ->setName('response[]')
 183            ->setValue(idx($responses, $ii, ''));
 184  
 185          if ($ii == 0) {
 186            $response->setError($e_response);
 187          }
 188  
 189          $form->appendChild($response);
 190        }
 191      }
 192  
 193      $poll_type_options = array(
 194        PhabricatorSlowvotePoll::METHOD_PLURALITY =>
 195          pht('Plurality (Single Choice)'),
 196        PhabricatorSlowvotePoll::METHOD_APPROVAL  =>
 197          pht('Approval (Multiple Choice)'),
 198      );
 199  
 200      $response_type_options = array(
 201        PhabricatorSlowvotePoll::RESPONSES_VISIBLE
 202          => pht('Allow anyone to see the responses'),
 203        PhabricatorSlowvotePoll::RESPONSES_VOTERS
 204          => pht('Require a vote to see the responses'),
 205        PhabricatorSlowvotePoll::RESPONSES_OWNER
 206          => pht('Only I can see the responses'),
 207      );
 208  
 209      if ($is_new) {
 210        $form->appendChild(
 211          id(new AphrontFormSelectControl())
 212            ->setLabel(pht('Vote Type'))
 213            ->setName('method')
 214            ->setValue($poll->getMethod())
 215            ->setOptions($poll_type_options));
 216      } else {
 217        $form->appendChild(
 218          id(new AphrontFormStaticControl())
 219            ->setLabel(pht('Vote Type'))
 220            ->setValue(idx($poll_type_options, $poll->getMethod())));
 221      }
 222  
 223      if ($is_new) {
 224        $title = pht('Create Slowvote');
 225        $button = pht('Create');
 226        $cancel_uri = $this->getApplicationURI();
 227      } else {
 228        $title = pht('Edit %s', 'V'.$poll->getID());
 229        $button = pht('Save Changes');
 230        $cancel_uri = '/V'.$poll->getID();
 231      }
 232  
 233      $policies = id(new PhabricatorPolicyQuery())
 234        ->setViewer($user)
 235        ->setObject($poll)
 236        ->execute();
 237  
 238      $form
 239        ->appendChild(
 240          id(new AphrontFormSelectControl())
 241            ->setLabel(pht('Responses'))
 242            ->setName('responses')
 243            ->setValue($v_responses)
 244            ->setOptions($response_type_options))
 245        ->appendChild(
 246          id(new AphrontFormCheckboxControl())
 247            ->setLabel(pht('Shuffle'))
 248            ->addCheckbox(
 249              'shuffle',
 250              1,
 251              pht('Show choices in random order.'),
 252              $v_shuffle))
 253        ->appendChild(
 254          id(new AphrontFormPolicyControl())
 255            ->setUser($user)
 256            ->setName('viewPolicy')
 257            ->setPolicyObject($poll)
 258            ->setPolicies($policies)
 259            ->setCapability(PhabricatorPolicyCapability::CAN_VIEW))
 260        ->appendChild(
 261          id(new AphrontFormSubmitControl())
 262            ->setValue($button)
 263            ->addCancelButton($cancel_uri));
 264  
 265      $crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
 266      $crumbs->addTextCrumb($title);
 267  
 268      $form_box = id(new PHUIObjectBoxView())
 269        ->setHeaderText($title)
 270        ->setFormErrors($errors)
 271        ->setForm($form);
 272  
 273      return $this->buildApplicationPage(
 274        array(
 275          $crumbs,
 276          $form_box,
 277        ),
 278        array(
 279          'title' => $title,
 280        ));
 281    }
 282  
 283  }


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