Path: blob/master/src/view/form/control/AphrontFormTokenizerControl.php
12262 views
<?php12final class AphrontFormTokenizerControl extends AphrontFormControl {34private $datasource;5private $disableBehavior;6private $limit;7private $placeholder;8private $handles;9private $initialValue;1011public function setDatasource(PhabricatorTypeaheadDatasource $datasource) {12$this->datasource = $datasource;13return $this;14}1516public function setDisableBehavior($disable) {17$this->disableBehavior = $disable;18return $this;19}2021protected function getCustomControlClass() {22return 'aphront-form-control-tokenizer';23}2425public function setLimit($limit) {26$this->limit = $limit;27return $this;28}2930public function setPlaceholder($placeholder) {31$this->placeholder = $placeholder;32return $this;33}3435public function setInitialValue(array $initial_value) {36$this->initialValue = $initial_value;37return $this;38}3940public function getInitialValue() {41return $this->initialValue;42}4344public function willRender() {45// Load the handles now so we'll get a bulk load later on when we actually46// render them.47$this->loadHandles();48}4950protected function renderInput() {51$name = $this->getName();5253$handles = $this->loadHandles();54$handles = iterator_to_array($handles);5556if ($this->getID()) {57$id = $this->getID();58} else {59$id = celerity_generate_unique_node_id();60}6162$datasource = $this->datasource;63if (!$datasource) {64throw new Exception(65pht('You must set a datasource to use a TokenizerControl.'));66}67$datasource->setViewer($this->getUser());6869$placeholder = $this->placeholder;70if ($placeholder === null || !strlen($placeholder)) {71$placeholder = $datasource->getPlaceholderText();72}7374$values = nonempty($this->getValue(), array());75$tokens = $datasource->renderTokens($values);7677foreach ($tokens as $token) {78$token->setInputName($this->getName());79}8081$template = id(new AphrontTokenizerTemplateView())82->setName($name)83->setID($id)84->setValue($tokens);8586$initial_value = $this->getInitialValue();87if ($initial_value !== null) {88$template->setInitialValue($initial_value);89}9091$username = null;92if ($this->hasViewer()) {93$username = $this->getViewer()->getUsername();94}9596$datasource_uri = $datasource->getDatasourceURI();97$browse_uri = $datasource->getBrowseURI();98if ($browse_uri) {99$template->setBrowseURI($browse_uri);100}101102if (!$this->disableBehavior) {103Javelin::initBehavior('aphront-basic-tokenizer', array(104'id' => $id,105'src' => $datasource_uri,106'value' => mpull($tokens, 'getValue', 'getKey'),107'icons' => mpull($tokens, 'getIcon', 'getKey'),108'types' => mpull($tokens, 'getTokenType', 'getKey'),109'colors' => mpull($tokens, 'getColor', 'getKey'),110'availabilityColors' => mpull(111$tokens,112'getAvailabilityColor',113'getKey'),114'limit' => $this->limit,115'username' => $username,116'placeholder' => $placeholder,117'browseURI' => $browse_uri,118'disabled' => $this->getDisabled(),119));120}121122return $template->render();123}124125private function loadHandles() {126if ($this->handles === null) {127$viewer = $this->getUser();128if (!$viewer) {129throw new Exception(130pht(131'Call %s before rendering tokenizers. '.132'Use %s on %s to do this easily.',133'setUser()',134'appendControl()',135'AphrontFormView'));136}137138$values = nonempty($this->getValue(), array());139140$phids = array();141foreach ($values as $value) {142if (!PhabricatorTypeaheadDatasource::isFunctionToken($value)) {143$phids[] = $value;144}145}146147$this->handles = $viewer->loadHandles($phids);148}149150return $this->handles;151}152153}154155156