Files
phabricator/src/view/form/control/AphrontFormTokenizerControl.php

155 lines
4.0 KiB
PHP
Raw Normal View History

2011-01-25 13:48:05 -08:00
<?php
final class AphrontFormTokenizerControl extends AphrontFormControl {
2011-01-25 13:48:05 -08:00
private $datasource;
private $disableBehavior;
private $limit;
private $placeholder;
private $handles;
2011-01-25 13:48:05 -08:00
public function setDatasource(PhabricatorTypeaheadDatasource $datasource) {
2011-01-25 13:48:05 -08:00
$this->datasource = $datasource;
return $this;
}
2011-02-04 22:45:42 -08:00
public function setDisableBehavior($disable) {
$this->disableBehavior = $disable;
return $this;
}
2011-01-25 13:48:05 -08:00
protected function getCustomControlClass() {
return 'aphront-form-control-tokenizer';
}
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
public function setPlaceholder($placeholder) {
$this->placeholder = $placeholder;
return $this;
}
public function willRender() {
// Load the handles now so we'll get a bulk load later on when we actually
// render them.
$this->loadHandles();
}
2011-01-25 13:48:05 -08:00
protected function renderInput() {
$name = $this->getName();
$handles = $this->loadHandles();
$handles = iterator_to_array($handles);
2011-02-04 22:45:42 -08:00
if ($this->getID()) {
$id = $this->getID();
} else {
$id = celerity_generate_unique_node_id();
}
2011-01-25 13:48:05 -08:00
$datasource = $this->datasource;
if (!$datasource) {
throw new Exception(
pht('You must set a datasource to use a TokenizerControl.'));
}
$datasource->setViewer($this->getUser());
$placeholder = null;
if (!strlen($this->placeholder)) {
$placeholder = $datasource->getPlaceholderText();
}
$tokens = array();
$values = nonempty($this->getValue(), array());
foreach ($values as $value) {
if (isset($handles[$value])) {
$token = PhabricatorTypeaheadTokenView::newFromHandle($handles[$value]);
} else {
$token = null;
$function = $datasource->parseFunction($value);
if ($function) {
$token_list = $datasource->renderFunctionTokens(
$function['name'],
array($function['argv']));
$token = head($token_list);
}
if (!$token) {
$name = pht('Invalid Function: %s', $value);
$token = $datasource->newInvalidToken($name);
}
$type = $token->getTokenType();
if ($type == PhabricatorTypeaheadTokenView::TYPE_INVALID) {
$token->setKey($value);
}
}
$token->setInputName($this->getName());
$tokens[] = $token;
}
2011-03-22 20:41:02 -07:00
$template = new AphrontTokenizerTemplateView();
$template->setName($name);
$template->setID($id);
$template->setValue($tokens);
2011-03-22 20:41:02 -07:00
$username = null;
if ($this->user) {
$username = $this->user->getUsername();
}
$datasource_uri = $datasource->getDatasourceURI();
$browse_uri = $datasource->getBrowseURI();
if ($browse_uri) {
$template->setBrowseURI($browse_uri);
}
2011-02-04 22:45:42 -08:00
if (!$this->disableBehavior) {
Javelin::initBehavior('aphront-basic-tokenizer', array(
'id' => $id,
'src' => $datasource_uri,
'value' => mpull($tokens, 'getValue', 'getKey'),
'icons' => mpull($tokens, 'getIcon', 'getKey'),
'types' => mpull($tokens, 'getTokenType', 'getKey'),
'colors' => mpull($tokens, 'getColor', 'getKey'),
'limit' => $this->limit,
'username' => $username,
'placeholder' => $placeholder,
'browseURI' => $browse_uri,
2011-02-04 22:45:42 -08:00
));
}
2011-01-25 13:48:05 -08:00
2011-03-22 20:41:02 -07:00
return $template->render();
2011-01-25 13:48:05 -08:00
}
private function loadHandles() {
if ($this->handles === null) {
$viewer = $this->getUser();
if (!$viewer) {
throw new Exception(
pht(
'Call setUser() before rendering tokenizers. Use appendControl() '.
'on AphrontFormView to do this easily.'));
}
$values = nonempty($this->getValue(), array());
$phids = array();
foreach ($values as $value) {
if (!PhabricatorTypeaheadDatasource::isFunctionToken($value)) {
$phids[] = $value;
}
}
$this->handles = $viewer->loadHandles($phids);
}
return $this->handles;
}
2011-01-25 13:48:05 -08:00
}