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();
|
|
|
|
|
|
|
|
$poll = new PhabricatorSlowvotePoll();
|
|
|
|
$poll->setAuthorPHID($user->getPHID());
|
2013-07-15 19:50:38 +02:00
|
|
|
$poll->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');
|
|
|
|
$v_responses = $request->getInt('responses');
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses = array_filter($responses);
|
|
|
|
if (empty($responses)) {
|
2013-02-14 01:47:24 +01:00
|
|
|
$errors[] = pht('You must offer at least one response.');
|
|
|
|
$e_response = pht('Required');
|
2011-07-08 20:13:11 +02:00
|
|
|
} else {
|
|
|
|
$e_response = null;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->applyTransactions($poll, $xactions);
|
|
|
|
|
2011-07-08 20:13:11 +02:00
|
|
|
$poll->save();
|
|
|
|
|
|
|
|
foreach ($responses as $response) {
|
|
|
|
$option = new PhabricatorSlowvoteOption();
|
|
|
|
$option->setName($response);
|
|
|
|
$option->setPollID($poll->getID());
|
|
|
|
$option->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-13 19:42:05 +02:00
|
|
|
->setFlexible(true)
|
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
|
|
|
|
|
|
|
for ($ii = 0; $ii < 10; $ii++) {
|
|
|
|
$n = ($ii + 1);
|
|
|
|
$response = id(new AphrontFormTextControl())
|
2013-02-14 01:47:24 +01:00
|
|
|
->setLabel(pht("Response %d", $n))
|
2011-07-08 20:13:11 +02:00
|
|
|
->setName('response[]')
|
|
|
|
->setValue(idx($responses, $ii, ''));
|
|
|
|
|
|
|
|
if ($ii == 0) {
|
|
|
|
$response->setError($e_response);
|
|
|
|
}
|
|
|
|
|
|
|
|
$form->appendChild($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
);
|
|
|
|
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
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())
|
|
|
|
->setOptions($poll_type_options))
|
|
|
|
->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-02-14 01:47:24 +01:00
|
|
|
->setValue(pht('Create Slowvote'))
|
2011-07-08 20:13:11 +02:00
|
|
|
->addCancelButton('/vote/'));
|
|
|
|
|
2013-02-14 01:47:24 +01:00
|
|
|
$crumbs = $this->buildApplicationCrumbs($this->buildSideNavView());
|
|
|
|
$crumbs->addCrumb(
|
|
|
|
id(new PhabricatorCrumbView())
|
|
|
|
->setName(pht('Create Slowvote'))
|
2013-02-19 22:33:10 +01:00
|
|
|
->setHref($this->getApplicationURI().'create/'));
|
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,
|
2011-07-08 20:13:11 +02:00
|
|
|
$error_view,
|
2013-07-13 19:42:05 +02:00
|
|
|
$form,
|
2011-07-08 20:13:11 +02:00
|
|
|
),
|
|
|
|
array(
|
2013-02-14 01:47:24 +01:00
|
|
|
'title' => pht('Create Slowvote'),
|
|
|
|
'device' => true,
|
2013-07-13 19:42:05 +02:00
|
|
|
'dust' => true,
|
2011-07-08 20:13:11 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|