PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/applications/phid/PhabricatorObjectHandle.php

https://github.com/navyuginfo/phabricator
PHP | 299 lines | 206 code | 55 blank | 38 comment | 16 complexity | 929784fb3f7ad1a83379a174410d0eab 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 PhabricatorObjectHandle
  3. implements PhabricatorPolicyInterface {
  4. private $uri;
  5. private $phid;
  6. private $type;
  7. private $name;
  8. private $fullName;
  9. private $title;
  10. private $imageURI;
  11. private $icon;
  12. private $timestamp;
  13. private $status = PhabricatorObjectHandleStatus::STATUS_OPEN;
  14. private $complete;
  15. private $disabled;
  16. private $objectName;
  17. private $policyFiltered;
  18. public function setIcon($icon) {
  19. $this->icon = $icon;
  20. return $this;
  21. }
  22. public function getIcon() {
  23. if ($this->icon) {
  24. return $this->icon;
  25. }
  26. return $this->getTypeIcon();
  27. }
  28. public function getTypeIcon() {
  29. if ($this->getPHIDType()) {
  30. return $this->getPHIDType()->getTypeIcon();
  31. }
  32. return null;
  33. }
  34. public function setPolicyFiltered($policy_filered) {
  35. $this->policyFiltered = $policy_filered;
  36. return $this;
  37. }
  38. public function getPolicyFiltered() {
  39. return $this->policyFiltered;
  40. }
  41. public function setObjectName($object_name) {
  42. $this->objectName = $object_name;
  43. return $this;
  44. }
  45. public function getObjectName() {
  46. if (!$this->objectName) {
  47. return $this->getName();
  48. }
  49. return $this->objectName;
  50. }
  51. public function setURI($uri) {
  52. $this->uri = $uri;
  53. return $this;
  54. }
  55. public function getURI() {
  56. return $this->uri;
  57. }
  58. public function setPHID($phid) {
  59. $this->phid = $phid;
  60. return $this;
  61. }
  62. public function getPHID() {
  63. return $this->phid;
  64. }
  65. public function setName($name) {
  66. $this->name = $name;
  67. return $this;
  68. }
  69. public function getName() {
  70. if ($this->name === null) {
  71. if ($this->getPolicyFiltered()) {
  72. return pht('Restricted %s', $this->getTypeName());
  73. } else {
  74. return pht('Unknown Object (%s)', $this->getTypeName());
  75. }
  76. }
  77. return $this->name;
  78. }
  79. public function setStatus($status) {
  80. $this->status = $status;
  81. return $this;
  82. }
  83. public function getStatus() {
  84. return $this->status;
  85. }
  86. public function setFullName($full_name) {
  87. $this->fullName = $full_name;
  88. return $this;
  89. }
  90. public function getFullName() {
  91. if ($this->fullName !== null) {
  92. return $this->fullName;
  93. }
  94. return $this->getName();
  95. }
  96. public function setTitle($title) {
  97. $this->title = $title;
  98. return $this;
  99. }
  100. public function getTitle() {
  101. return $this->title;
  102. }
  103. public function setType($type) {
  104. $this->type = $type;
  105. return $this;
  106. }
  107. public function getType() {
  108. return $this->type;
  109. }
  110. public function setImageURI($uri) {
  111. $this->imageURI = $uri;
  112. return $this;
  113. }
  114. public function getImageURI() {
  115. return $this->imageURI;
  116. }
  117. public function setTimestamp($timestamp) {
  118. $this->timestamp = $timestamp;
  119. return $this;
  120. }
  121. public function getTimestamp() {
  122. return $this->timestamp;
  123. }
  124. public function getTypeName() {
  125. if ($this->getPHIDType()) {
  126. return $this->getPHIDType()->getTypeName();
  127. }
  128. return $this->getType();
  129. }
  130. /**
  131. * Set whether or not the underlying object is complete. See
  132. * @{method:isComplete} for an explanation of what it means to be complete.
  133. *
  134. * @param bool True if the handle represents a complete object.
  135. * @return this
  136. */
  137. public function setComplete($complete) {
  138. $this->complete = $complete;
  139. return $this;
  140. }
  141. /**
  142. * Determine if the handle represents an object which was completely loaded
  143. * (i.e., the underlying object exists) vs an object which could not be
  144. * completely loaded (e.g., the type or data for the PHID could not be
  145. * identified or located).
  146. *
  147. * Basically, @{class:PhabricatorHandleQuery} gives you back a handle for
  148. * any PHID you give it, but it gives you a complete handle only for valid
  149. * PHIDs.
  150. *
  151. * @return bool True if the handle represents a complete object.
  152. */
  153. public function isComplete() {
  154. return $this->complete;
  155. }
  156. /**
  157. * Set whether or not the underlying object is disabled. See
  158. * @{method:isDisabled} for an explanation of what it means to be disabled.
  159. *
  160. * @param bool True if the handle represents a disabled object.
  161. * @return this
  162. */
  163. public function setDisabled($disabled) {
  164. $this->disabled = $disabled;
  165. return $this;
  166. }
  167. /**
  168. * Determine if the handle represents an object which has been disabled --
  169. * for example, disabled users, archived projects, etc. These objects are
  170. * complete and exist, but should be excluded from some system interactions
  171. * (for instance, they usually should not appear in typeaheads, and should
  172. * not have mail/notifications delivered to or about them).
  173. *
  174. * @return bool True if the handle represents a disabled object.
  175. */
  176. public function isDisabled() {
  177. return $this->disabled;
  178. }
  179. public function renderLink($name = null) {
  180. if ($name === null) {
  181. $name = $this->getLinkName();
  182. }
  183. $classes = array();
  184. $classes[] = 'phui-handle';
  185. $title = $this->title;
  186. if ($this->status != PhabricatorObjectHandleStatus::STATUS_OPEN) {
  187. $classes[] = 'handle-status-'.$this->status;
  188. $title = $title ? $title : $this->status;
  189. }
  190. if ($this->disabled) {
  191. $classes[] = 'handle-disabled';
  192. $title = pht('Disabled'); // Overwrite status.
  193. }
  194. if ($this->getType() == PhabricatorPeoplePHIDTypeUser::TYPECONST) {
  195. $classes[] = 'phui-link-person';
  196. }
  197. $uri = $this->getURI();
  198. $icon = null;
  199. if ($this->getPolicyFiltered()) {
  200. $icon = id(new PHUIIconView())
  201. ->setIconFont('fa-lock lightgreytext');
  202. }
  203. return phutil_tag(
  204. $uri ? 'a' : 'span',
  205. array(
  206. 'href' => $uri,
  207. 'class' => implode(' ', $classes),
  208. 'title' => $title,
  209. ),
  210. array($icon, $name));
  211. }
  212. public function getLinkName() {
  213. switch ($this->getType()) {
  214. case PhabricatorPeoplePHIDTypeUser::TYPECONST:
  215. $name = $this->getName();
  216. break;
  217. default:
  218. $name = $this->getFullName();
  219. break;
  220. }
  221. return $name;
  222. }
  223. protected function getPHIDType() {
  224. $types = PhabricatorPHIDType::getAllTypes();
  225. return idx($types, $this->getType());
  226. }
  227. /* -( PhabricatorPolicyInterface )----------------------------------------- */
  228. public function getCapabilities() {
  229. return array(
  230. PhabricatorPolicyCapability::CAN_VIEW,
  231. );
  232. }
  233. public function getPolicy($capability) {
  234. return PhabricatorPolicies::POLICY_PUBLIC;
  235. }
  236. public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
  237. // NOTE: Handles are always visible, they just don't get populated with
  238. // data if the user can't see the underlying object.
  239. return true;
  240. }
  241. public function describeAutomaticCapability($capability) {
  242. return null;
  243. }
  244. }