Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / view / control / PhabricatorObjectSelectorDialog.php
blob3455cbc8bd2ac16405c20a87f81c6e241c8b3200
1 <?php
3 final class PhabricatorObjectSelectorDialog extends Phobject {
5 private $user;
6 private $filters = array();
7 private $handles = array();
8 private $cancelURI;
9 private $submitURI;
10 private $searchURI;
11 private $selectedFilter;
12 private $excluded;
13 private $initialPHIDs;
14 private $maximumSelectionSize;
16 private $title;
17 private $header;
18 private $buttonText;
19 private $instructions;
21 public function setUser($user) {
22 $this->user = $user;
23 return $this;
26 public function setFilters(array $filters) {
27 $this->filters = $filters;
28 return $this;
31 public function setSelectedFilter($selected_filter) {
32 $this->selectedFilter = $selected_filter;
33 return $this;
36 public function setExcluded($excluded_phid) {
37 $this->excluded = $excluded_phid;
38 return $this;
41 public function setHandles(array $handles) {
42 assert_instances_of($handles, 'PhabricatorObjectHandle');
43 $this->handles = $handles;
44 return $this;
47 public function setCancelURI($cancel_uri) {
48 $this->cancelURI = $cancel_uri;
49 return $this;
52 public function setSubmitURI($submit_uri) {
53 $this->submitURI = $submit_uri;
54 return $this;
57 public function setSearchURI($search_uri) {
58 $this->searchURI = $search_uri;
59 return $this;
62 public function setTitle($title) {
63 $this->title = $title;
64 return $this;
67 public function setHeader($header) {
68 $this->header = $header;
69 return $this;
72 public function setButtonText($button_text) {
73 $this->buttonText = $button_text;
74 return $this;
77 public function setInstructions($instructions) {
78 $this->instructions = $instructions;
79 return $this;
82 public function setInitialPHIDs(array $initial_phids) {
83 $this->initialPHIDs = $initial_phids;
84 return $this;
87 public function getInitialPHIDs() {
88 return $this->initialPHIDs;
91 public function setMaximumSelectionSize($maximum_selection_size) {
92 $this->maximumSelectionSize = $maximum_selection_size;
93 return $this;
96 public function getMaximumSelectionSize() {
97 return $this->maximumSelectionSize;
100 public function buildDialog() {
101 $user = $this->user;
103 $filter_id = celerity_generate_unique_node_id();
104 $query_id = celerity_generate_unique_node_id();
105 $results_id = celerity_generate_unique_node_id();
106 $current_id = celerity_generate_unique_node_id();
107 $search_id = celerity_generate_unique_node_id();
108 $form_id = celerity_generate_unique_node_id();
110 require_celerity_resource('phabricator-object-selector-css');
112 $options = array();
113 foreach ($this->filters as $key => $label) {
114 $options[] = phutil_tag(
115 'option',
116 array(
117 'value' => $key,
118 'selected' => ($key == $this->selectedFilter)
119 ? 'selected'
120 : null,
122 $label);
125 $instructions = null;
126 if ($this->instructions) {
127 $instructions = phutil_tag(
128 'p',
129 array('class' => 'phabricator-object-selector-instructions'),
130 $this->instructions);
133 $search_box = phabricator_form(
134 $user,
135 array(
136 'method' => 'POST',
137 'action' => $this->submitURI,
138 'id' => $search_id,
140 phutil_tag(
141 'table',
142 array('class' => 'phabricator-object-selector-search'),
143 phutil_tag('tr', array(), array(
144 phutil_tag(
145 'td',
146 array('class' => 'phabricator-object-selector-search-filter'),
147 phutil_tag('select', array('id' => $filter_id), $options)),
148 phutil_tag(
149 'td',
150 array('class' => 'phabricator-object-selector-search-text'),
151 phutil_tag('input', array('id' => $query_id, 'type' => 'text'))),
152 ))));
154 $result_box = phutil_tag(
155 'div',
156 array(
157 'class' => 'phabricator-object-selector-results',
158 'id' => $results_id,
160 '');
162 $attached_box = phutil_tag_div(
163 'phabricator-object-selector-current',
164 phutil_tag_div(
165 'phabricator-object-selector-currently-attached',
166 array(
167 phutil_tag_div('phabricator-object-selector-header', $this->header),
168 phutil_tag('div', array('id' => $current_id)),
169 $instructions,
170 )));
172 $dialog = new AphrontDialogView();
173 $dialog
174 ->setUser($this->user)
175 ->setTitle($this->title)
176 ->setClass('phabricator-object-selector-dialog')
177 ->appendChild($search_box)
178 ->appendChild($result_box)
179 ->appendChild($attached_box)
180 ->setRenderDialogAsDiv()
181 ->setFormID($form_id)
182 ->addSubmitButton($this->buttonText);
184 if ($this->cancelURI) {
185 $dialog->addCancelButton($this->cancelURI);
188 $handle_views = array();
189 foreach ($this->handles as $handle) {
190 $phid = $handle->getPHID();
191 $view = new PhabricatorHandleObjectSelectorDataView($handle);
192 $handle_views[$phid] = $view->renderData();
195 $dialog->addHiddenInput('phids', implode(';', array_keys($this->handles)));
197 $initial_phids = $this->getInitialPHIDs();
198 if ($initial_phids) {
199 $initial_phids = implode(', ', $initial_phids);
200 $dialog->addHiddenInput('initialPHIDs', $initial_phids);
203 $maximum = $this->getMaximumSelectionSize();
205 Javelin::initBehavior(
206 'phabricator-object-selector',
207 array(
208 'filter' => $filter_id,
209 'query' => $query_id,
210 'search' => $search_id,
211 'results' => $results_id,
212 'current' => $current_id,
213 'form' => $form_id,
214 'exclude' => $this->excluded,
215 'uri' => $this->searchURI,
216 'handles' => $handle_views,
217 'maximum' => $maximum,
220 $dialog->setResizeX(true);
221 $dialog->setResizeY($results_id);
223 return $dialog;