PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/view/phui/PHUIObjectBoxView.php

https://github.com/navyuginfo/phabricator
PHP | 271 lines | 228 code | 42 blank | 1 comment | 31 complexity | 84f95efbceaa32d85792a9585c408314 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.0, LGPL-3.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. final class PHUIObjectBoxView extends AphrontView {
  3. private $headerText;
  4. private $formErrors = null;
  5. private $formSaved = false;
  6. private $errorView;
  7. private $form;
  8. private $validationException;
  9. private $header;
  10. private $flush;
  11. private $id;
  12. private $sigils = array();
  13. private $metadata;
  14. private $tabs = array();
  15. private $propertyLists = array();
  16. public function addSigil($sigil) {
  17. $this->sigils[] = $sigil;
  18. return $this;
  19. }
  20. public function setMetadata(array $metadata) {
  21. $this->metadata = $metadata;
  22. return $this;
  23. }
  24. public function addPropertyList(
  25. PHUIPropertyListView $property_list,
  26. $tab = null) {
  27. if (!($tab instanceof PHUIListItemView) &&
  28. ($tab !== null)) {
  29. assert_stringlike($tab);
  30. $tab = id(new PHUIListItemView())->setName($tab);
  31. }
  32. if ($tab) {
  33. if ($tab->getKey()) {
  34. $key = $tab->getKey();
  35. } else {
  36. $key = 'tab.default.'.spl_object_hash($tab);
  37. $tab->setKey($key);
  38. }
  39. } else {
  40. $key = 'tab.default';
  41. }
  42. if ($tab) {
  43. if (empty($this->tabs[$key])) {
  44. $tab->addSigil('phui-object-box-tab');
  45. $tab->setMetadata(
  46. array(
  47. 'tabKey' => $key,
  48. ));
  49. if (!$tab->getHref()) {
  50. $tab->setHref('#');
  51. }
  52. if (!$tab->getType()) {
  53. $tab->setType(PHUIListItemView::TYPE_LINK);
  54. }
  55. $this->tabs[$key] = $tab;
  56. }
  57. }
  58. $this->propertyLists[$key][] = $property_list;
  59. return $this;
  60. }
  61. public function setHeaderText($text) {
  62. $this->headerText = $text;
  63. return $this;
  64. }
  65. public function setFormErrors(array $errors, $title = null) {
  66. if (nonempty($errors)) {
  67. $this->formErrors = id(new AphrontErrorView())
  68. ->setTitle($title)
  69. ->setErrors($errors);
  70. }
  71. return $this;
  72. }
  73. public function setFormSaved($saved, $text = null) {
  74. if (!$text) {
  75. $text = pht('Changes saved.');
  76. }
  77. if ($saved) {
  78. $save = id(new AphrontErrorView())
  79. ->setSeverity(AphrontErrorView::SEVERITY_NOTICE)
  80. ->appendChild($text);
  81. $this->formSaved = $save;
  82. }
  83. return $this;
  84. }
  85. public function setErrorView(AphrontErrorView $view) {
  86. $this->errorView = $view;
  87. return $this;
  88. }
  89. public function setForm($form) {
  90. $this->form = $form;
  91. return $this;
  92. }
  93. public function setID($id) {
  94. $this->id = $id;
  95. return $this;
  96. }
  97. public function setHeader(PHUIHeaderView $header) {
  98. $this->header = $header;
  99. return $this;
  100. }
  101. public function setFlush($flush) {
  102. $this->flush = $flush;
  103. return $this;
  104. }
  105. public function setValidationException(
  106. PhabricatorApplicationTransactionValidationException $ex = null) {
  107. $this->validationException = $ex;
  108. return $this;
  109. }
  110. public function render() {
  111. require_celerity_resource('phui-object-box-css');
  112. if ($this->header) {
  113. $header = $this->header;
  114. $header->setGradient(PhabricatorActionHeaderView::HEADER_LIGHTBLUE);
  115. } else {
  116. $header = id(new PHUIHeaderView())
  117. ->setHeader($this->headerText)
  118. ->setGradient(PhabricatorActionHeaderView::HEADER_LIGHTBLUE);
  119. }
  120. $ex = $this->validationException;
  121. $exception_errors = null;
  122. if ($ex) {
  123. $messages = array();
  124. foreach ($ex->getErrors() as $error) {
  125. $messages[] = $error->getMessage();
  126. }
  127. if ($messages) {
  128. $exception_errors = id(new AphrontErrorView())
  129. ->setErrors($messages);
  130. }
  131. }
  132. $tab_lists = array();
  133. $property_lists = array();
  134. $tab_map = array();
  135. $default_key = 'tab.default';
  136. // Find the selected tab, or select the first tab if none are selected.
  137. if ($this->tabs) {
  138. $selected_tab = null;
  139. foreach ($this->tabs as $key => $tab) {
  140. if ($tab->getSelected()) {
  141. $selected_tab = $key;
  142. break;
  143. }
  144. }
  145. if ($selected_tab === null) {
  146. head($this->tabs)->setSelected(true);
  147. $selected_tab = head_key($this->tabs);
  148. }
  149. }
  150. foreach ($this->propertyLists as $key => $list) {
  151. $group = new PHUIPropertyGroupView();
  152. $i = 0;
  153. foreach ($list as $item) {
  154. $group->addPropertyList($item);
  155. if ($i > 0) {
  156. $item->addClass('phui-property-list-section-noninitial');
  157. }
  158. $i++;
  159. }
  160. if ($this->tabs && $key != $default_key) {
  161. $tab_id = celerity_generate_unique_node_id();
  162. $tab_map[$key] = $tab_id;
  163. if ($key === $selected_tab) {
  164. $style = null;
  165. } else {
  166. $style = 'display: none';
  167. }
  168. $tab_lists[] = phutil_tag(
  169. 'div',
  170. array(
  171. 'style' => $style,
  172. 'id' => $tab_id,
  173. ),
  174. $group);
  175. } else {
  176. if ($this->tabs) {
  177. $group->addClass('phui-property-group-noninitial');
  178. }
  179. $property_lists[] = $group;
  180. }
  181. }
  182. $tabs = null;
  183. if ($this->tabs) {
  184. $tabs = id(new PHUIListView())
  185. ->setType(PHUIListView::NAVBAR_LIST);
  186. foreach ($this->tabs as $tab) {
  187. $tabs->addMenuItem($tab);
  188. }
  189. Javelin::initBehavior('phui-object-box-tabs');
  190. }
  191. $content = id(new PHUIBoxView())
  192. ->appendChild(
  193. array(
  194. $header,
  195. $this->errorView,
  196. $this->formErrors,
  197. $this->formSaved,
  198. $exception_errors,
  199. $this->form,
  200. $tabs,
  201. $tab_lists,
  202. $property_lists,
  203. $this->renderChildren(),
  204. ))
  205. ->setBorder(true)
  206. ->setID($this->id)
  207. ->addMargin(PHUI::MARGIN_LARGE_TOP)
  208. ->addMargin(PHUI::MARGIN_LARGE_LEFT)
  209. ->addMargin(PHUI::MARGIN_LARGE_RIGHT)
  210. ->addClass('phui-object-box');
  211. if ($this->tabs) {
  212. $content->addSigil('phui-object-box');
  213. $content->setMetadata(
  214. array(
  215. 'tabMap' => $tab_map,
  216. ));
  217. }
  218. if ($this->flush) {
  219. $content->addClass('phui-object-box-flush');
  220. }
  221. foreach ($this->sigils as $sigil) {
  222. $content->addSigil($sigil);
  223. }
  224. if ($this->metadata !== null) {
  225. $content->setMetadata($this->metadata);
  226. }
  227. return $content;
  228. }
  229. }