2011-07-08 20:13:11 +02:00
|
|
|
<?php
|
|
|
|
|
2011-09-14 17:02:31 +02:00
|
|
|
/**
|
|
|
|
* @group slowvote
|
|
|
|
*/
|
2013-07-15 19:50:38 +02:00
|
|
|
final class PhabricatorSlowvoteEditController
|
2011-07-08 20:13:11 +02:00
|
|
|
extends PhabricatorSlowvoteController {
|
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
2011-07-08 20:13:11 +02:00
|
|
|
public function processRequest() {
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($this->id) {
|
|
|
|
$poll = id(new PhabricatorSlowvoteQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->executeOne();
|
|
|
|
if (!$poll) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
$is_new = false;
|
|
|
|
} else {
|
|
|
|
$poll = id(new PhabricatorSlowvotePoll())
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setViewPolicy(PhabricatorPolicies::POLICY_USER);
|
|
|
|
$is_new = true;
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
|
|
|
|
$e_question = true;
|
|
|
|
$e_response = true;
|
|
|
|
$errors = array();
|
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
$v_question = $poll->getQuestion();
|
|
|
|
$v_description = $poll->getDescription();
|
|
|
|
$v_responses = $poll->getResponseVisibility();
|
|
|
|
$v_shuffle = $poll->getShuffle();
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
$responses = $request->getArr('response');
|
2011-07-08 20:13:11 +02:00
|
|
|
if ($request->isFormPost()) {
|
2013-07-15 19:50:38 +02:00
|
|
|
$v_question = $request->getStr('question');
|
|
|
|
$v_description = $request->getStr('description');
|
2013-07-15 22:18:50 +02:00
|
|
|
$v_responses = (int)$request->getInt('responses');
|
2013-07-15 19:50:38 +02:00
|
|
|
$v_shuffle = (int)$request->getBool('shuffle');
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
if ($is_new) {
|
|
|
|
$poll->setMethod($request->getInt('method'));
|
|
|
|
}
|
2013-07-15 01:02:04 +02:00
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
if (!strlen($v_question)) {
|
2013-02-14 01:47:24 +01:00
|
|
|
$e_question = pht('Required');
|
|
|
|
$errors[] = pht('You must ask a poll question.');
|
2011-07-08 20:13:11 +02:00
|
|
|
} else {
|
|
|
|
$e_question = null;
|
|
|
|
}
|
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($is_new) {
|
|
|
|
$responses = array_filter($responses);
|
|
|
|
if (empty($responses)) {
|
|
|
|
$errors[] = pht('You must offer at least one response.');
|
|
|
|
$e_response = pht('Required');
|
|
|
|
} else {
|
|
|
|
$e_response = null;
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
2013-07-15 19:50:38 +02:00
|
|
|
$xactions = array();
|
|
|
|
$template = id(new PhabricatorSlowvoteTransaction());
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_QUESTION)
|
|
|
|
->setNewValue($v_question);
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_DESCRIPTION)
|
|
|
|
->setNewValue($v_description);
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_RESPONSES)
|
|
|
|
->setNewValue($v_responses);
|
|
|
|
|
|
|
|
$xactions[] = id(clone $template)
|
|
|
|
->setTransactionType(PhabricatorSlowvoteTransaction::TYPE_SHUFFLE)
|
|
|
|
->setNewValue($v_shuffle);
|
|
|
|
|
2011-07-08 20:13:11 +02:00
|
|
|
if (empty($errors)) {
|
2013-07-15 19:50:38 +02:00
|
|
|
$editor = id(new PhabricatorSlowvoteEditor())
|
|
|
|
->setActor($user)
|
|
|
|
->setContinueOnNoEffect(true)
|
2013-07-15 20:07:37 +02:00
|
|
|
->setContentSourceFromRequest($request);
|
|
|
|
|
|
|
|
$xactions = $editor->applyTransactions($poll, $xactions);
|
2013-07-15 19:50:38 +02:00
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($is_new) {
|
|
|
|
$poll->save();
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
foreach ($responses as $response) {
|
|
|
|
$option = new PhabricatorSlowvoteOption();
|
|
|
|
$option->setName($response);
|
|
|
|
$option->setPollID($poll->getID());
|
|
|
|
$option->save();
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/V'.$poll->getID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$error_view = null;
|
|
|
|
if ($errors) {
|
|
|
|
$error_view = new AphrontErrorView();
|
2013-02-14 01:47:24 +01:00
|
|
|
$error_view->setTitle(pht('Form Errors'));
|
2011-07-08 20:13:11 +02:00
|
|
|
$error_view->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
2013-02-14 01:47:24 +01:00
|
|
|
$instructions =
|
|
|
|
phutil_tag(
|
|
|
|
'p',
|
|
|
|
array(
|
|
|
|
'class' => 'aphront-form-instructions',
|
|
|
|
),
|
|
|
|
pht('Resolve issues and build consensus through '.
|
2013-02-19 22:33:10 +01:00
|
|
|
'protracted deliberation.'));
|
2013-02-14 01:47:24 +01:00
|
|
|
|
2011-07-08 20:13:11 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2013-02-14 01:47:24 +01:00
|
|
|
->appendChild($instructions)
|
2011-07-08 20:13:11 +02:00
|
|
|
->appendChild(
|
2013-07-13 19:42:05 +02:00
|
|
|
id(new AphrontFormTextAreaControl())
|
|
|
|
->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_SHORT)
|
2013-02-14 01:47:24 +01:00
|
|
|
->setLabel(pht('Question'))
|
2011-07-08 20:13:11 +02:00
|
|
|
->setName('question')
|
2013-07-15 19:50:38 +02:00
|
|
|
->setValue($v_question)
|
|
|
|
->setError($e_question))
|
|
|
|
->appendChild(
|
|
|
|
id(new PhabricatorRemarkupControl())
|
|
|
|
->setLabel(pht('Description'))
|
|
|
|
->setName('description')
|
|
|
|
->setValue($v_description));
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($is_new) {
|
|
|
|
for ($ii = 0; $ii < 10; $ii++) {
|
|
|
|
$n = ($ii + 1);
|
|
|
|
$response = id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht("Response %d", $n))
|
|
|
|
->setName('response[]')
|
|
|
|
->setValue(idx($responses, $ii, ''));
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($ii == 0) {
|
|
|
|
$response->setError($e_response);
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
$form->appendChild($response);
|
|
|
|
}
|
2011-07-08 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$poll_type_options = array(
|
2013-02-14 01:47:24 +01:00
|
|
|
PhabricatorSlowvotePoll::METHOD_PLURALITY =>
|
|
|
|
pht('Plurality (Single Choice)'),
|
|
|
|
PhabricatorSlowvotePoll::METHOD_APPROVAL =>
|
|
|
|
pht('Approval (Multiple Choice)'),
|
2011-07-08 20:13:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$response_type_options = array(
|
|
|
|
PhabricatorSlowvotePoll::RESPONSES_VISIBLE
|
2013-02-14 01:47:24 +01:00
|
|
|
=> pht('Allow anyone to see the responses'),
|
2011-07-08 20:13:11 +02:00
|
|
|
PhabricatorSlowvotePoll::RESPONSES_VOTERS
|
2013-02-14 01:47:24 +01:00
|
|
|
=> pht('Require a vote to see the responses'),
|
2011-07-08 20:13:11 +02:00
|
|
|
PhabricatorSlowvotePoll::RESPONSES_OWNER
|
2013-02-14 01:47:24 +01:00
|
|
|
=> pht('Only I can see the responses'),
|
2011-07-08 20:13:11 +02:00
|
|
|
);
|
|
|
|
|
2013-07-15 20:07:37 +02:00
|
|
|
if ($is_new) {
|
|
|
|
$form->appendChild(
|
2011-07-08 20:13:11 +02:00
|
|
|
id(new AphrontFormSelectControl())
|
2013-02-14 01:47:24 +01:00
|
|
|
->setLabel(pht('Vote Type'))
|
2011-07-08 20:13:11 +02:00
|
|
|
->setName('method')
|
|
|
|
->setValue($poll->getMethod())
|
2013-07-15 20:07:37 +02:00
|
|
|
->setOptions($poll_type_options));
|
|
|
|
} else {
|
|
|
|
$form->appendChild(
|
|
|
|
id(new AphrontFormStaticControl())
|
|
|
|
->setLabel(pht('Vote Type'))
|
|
|
|
->setValue(idx($poll_type_options, $poll->getMethod())));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_new) {
|
|
|
|
$title = pht('Create Slowvote');
|
|
|
|
$button = pht('Create');
|
|
|
|
$cancel_uri = $this->getApplicationURI();
|
|
|
|
} else {
|
|
|
|
$title = pht('Edit %s', 'V'.$poll->getID());
|
|
|
|
$button = pht('Save Changes');
|
|
|
|
$cancel_uri = '/V'.$poll->getID();
|
|
|
|
}
|
|
|
|
|
|
|
|
$form
|
2011-07-08 20:13:11 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
2013-02-14 01:47:24 +01:00
|
|
|
->setLabel(pht('Responses'))
|
2013-07-15 19:50:38 +02:00
|
|
|
->setName('responses')
|
|
|
|
->setValue($v_responses)
|
2011-07-08 20:13:11 +02:00
|
|
|
->setOptions($response_type_options))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormCheckboxControl())
|
2013-02-14 01:47:24 +01:00
|
|
|
->setLabel(pht('Shuffle'))
|
2011-07-08 20:13:11 +02:00
|
|
|
->addCheckbox(
|
|
|
|
'shuffle',
|
|
|
|
1,
|
2013-07-13 19:42:05 +02:00
|
|
|
pht('Show choices in random order.'),
|
2013-07-15 19:50:38 +02:00
|
|
|
$v_shuffle))
|
2011-07-08 20:13:11 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
2013-07-15 20:07:37 +02:00
|
|
|
->setValue($button)
|
|
|
|
->addCancelButton($cancel_uri));
|
2011-07-08 20:13:11 +02:00
|
|
|
|
2013-02-14 01:47:24 +01:00
|
|
|
$crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
2013-07-15 20:07:37 +02:00
|
|
|
->setName($title));
|
2013-02-14 01:47:24 +01:00
|
|
|
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box = id(new PHUIFormBoxView())
|
|
|
|
->setHeaderText($title)
|
|
|
|
->setFormError($error_view)
|
|
|
|
->setForm($form);
|
|
|
|
|
2013-02-14 01:47:24 +01:00
|
|
|
return $this->buildApplicationPage(
|
2011-07-08 20:13:11 +02:00
|
|
|
array(
|
2013-02-14 01:47:24 +01:00
|
|
|
$crumbs,
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box,
|
2011-07-08 20:13:11 +02:00
|
|
|
),
|
|
|
|
array(
|
2013-07-15 20:07:37 +02:00
|
|
|
'title' => $title,
|
2013-02-14 01:47:24 +01:00
|
|
|
'device' => true,
|
2011-07-08 20:13:11 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|