1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-29 02:02:41 +01:00
phorge-phorge/src/applications/slowvote/view/SlowvoteEmbedView.php

129 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* @group slowvote
*/
final class SlowvoteEmbedView extends AphrontView {
private $poll;
private $options;
private $viewerChoices;
public function setPoll(PhabricatorSlowvotePoll $poll) {
$this->poll = $poll;
return $this;
}
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
public function setViewerChoices(array $viewer_choices) {
$this->viewerChoices = $viewer_choices;
return $this;
}
public function render() {
if (!$this->poll) {
throw new Exception("Call setPoll() before render()!");
}
if (!$this->options) {
throw new Exception("Call setOptions() before render()!");
}
if ($this->poll->getShuffle()) {
shuffle($this->options);
}
require_celerity_resource('phabricator-slowvote-css');
require_celerity_resource('javelin-behavior-slowvote-embed');
$config = array(
'pollID' => $this->poll->getID());
Javelin::initBehavior('slowvote-embed', $config);
$user_choices = array();
if (!empty($this->viewerChoices)) {
$user_choices = mpull($this->viewerChoices, null, 'getOptionID');
}
$options = array();
$ribbon_colors = array('#DF0101', '#DF7401', '#D7DF01', '#74DF00',
'#01DF01', '#01DF74', '#01DFD7', '#0174DF', '#0101DF', '#5F04B4',
'#B404AE');
shuffle($ribbon_colors);
foreach ($this->options as $option) {
$classes = 'phabricator-slowvote-embed-option-text';
$selected = '';
if (idx($user_choices, $option->getID(), false)) {
$classes .= ' phabricator-slowvote-embed-option-selected';
$selected = '@';
}
$is_selected = javelin_tag(
'div',
array(
'class' => 'phabricator-slowvote-embed-option-vote',
'sigil' => 'slowvote-embed-vote'
),
$selected);
$option_text = javelin_tag(
'div',
array(
'class' => $classes,
'sigil' => 'slowvote-option',
'meta' => array(
'optionID' => $option->getID()
)
),
array($is_selected, $option->getName()));
$options[] = phutil_tag(
'div',
array(
'class' => 'phabricator-slowvote-embed-option',
'style' =>
sprintf('border-left: 7px solid %s;', array_shift($ribbon_colors))
),
array($option_text));
}
$link_to_slowvote = phutil_tag(
'a',
array(
'href' => '/V'.$this->poll->getID()
),
$this->poll->getQuestion());
$header = phutil_tag(
'div',
array(),
array('V'.$this->poll->getID().': ', $link_to_slowvote));
$body = phutil_tag(
'div',
array(),
$options);
return javelin_tag(
'div',
array(
'class' => 'phabricator-slowvote-embed',
'sigil' => 'slowvote-embed',
'meta' => array(
'pollID' => $this->poll->getID()
)
),
array($header, $body));
}
}