PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/core/Tracker/Definition.php

https://gitlab.com/ElvisAns/tiki
PHP | 396 lines | 324 code | 57 blank | 15 comment | 75 complexity | 41a22f4dc255abe4fa3ed9a986536914 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. class Tracker_Definition
  8. {
  9. public static $definitions = [];
  10. private $trackerInfo;
  11. private $factory;
  12. private $fields;
  13. public static function get($trackerId, $useCache = true)
  14. {
  15. $trackerId = (int) $trackerId;
  16. if ($useCache && isset(self::$definitions[$trackerId])) {
  17. return self::$definitions[$trackerId];
  18. }
  19. $trklib = TikiLib::lib('trk');
  20. $tracker_info = $trklib->get_tracker($trackerId);
  21. $definition = false;
  22. if ($tracker_info) {
  23. if ($t = $trklib->get_tracker_options($trackerId)) {
  24. $tracker_info = array_merge($t, $tracker_info);
  25. }
  26. $definition = new self($tracker_info);
  27. }
  28. return self::$definitions[$trackerId] = $definition;
  29. }
  30. public static function createFake(array $trackerInfo, array $fields)
  31. {
  32. $def = new self($trackerInfo);
  33. $def->fields = $fields;
  34. return $def;
  35. }
  36. public static function getDefault()
  37. {
  38. $def = new self([]);
  39. $def->fields = [];
  40. return $def;
  41. }
  42. private function __construct($trackerInfo)
  43. {
  44. $this->trackerInfo = $trackerInfo;
  45. }
  46. public function getInformation()
  47. {
  48. return $this->trackerInfo;
  49. }
  50. public function getFieldFactory()
  51. {
  52. if ($this->factory) {
  53. return $this->factory;
  54. }
  55. return $this->factory = new Tracker_Field_Factory($this);
  56. }
  57. public function getConfiguration($key, $default = false)
  58. {
  59. return isset($this->trackerInfo[$key]) ? $this->trackerInfo[$key] : $default;
  60. }
  61. public function isEnabled($key)
  62. {
  63. return $this->getConfiguration($key) === 'y';
  64. }
  65. public function getFieldsIdKeys()
  66. {
  67. $fields = [];
  68. foreach ($this->getFields() as $key => $field) {
  69. $fields[$field['fieldId']] = $field;
  70. }
  71. return $fields;
  72. }
  73. public function getFields()
  74. {
  75. if ($this->fields) {
  76. return $this->fields;
  77. }
  78. $trklib = TikiLib::lib('trk');
  79. $trackerId = $this->trackerInfo['trackerId'];
  80. if ($trackerId) {
  81. $fields = $trklib->list_tracker_fields($trackerId, 0, -1, 'position_asc', '', false /* Translation must be done from the views to avoid translating the sources on edit. */);
  82. return $this->fields = $fields['data'];
  83. } else {
  84. return $this->fields = [];
  85. }
  86. }
  87. public function setFields($fields)
  88. {
  89. $this->fields = $fields;
  90. }
  91. public function getField($id)
  92. {
  93. if (is_numeric($id)) {
  94. foreach ($this->getFields() as $f) {
  95. if ($f['fieldId'] == $id) {
  96. return $f;
  97. }
  98. }
  99. } else {
  100. return $this->getFieldFromPermName($id);
  101. }
  102. }
  103. public function getFieldFromName($name)
  104. {
  105. foreach ($this->getFields() as $f) {
  106. if ($f['name'] == $name) {
  107. return $f;
  108. }
  109. }
  110. }
  111. public function getFieldFromPermName($name)
  112. {
  113. if (empty($name)) {
  114. return null;
  115. }
  116. foreach ($this->getFields() as $f) {
  117. if ($f['permName'] == $name) {
  118. return $f;
  119. }
  120. }
  121. }
  122. public function getPopupFields()
  123. {
  124. if (! empty($this->trackerInfo['showPopup'])) {
  125. return explode(',', $this->trackerInfo['showPopup']);
  126. } else {
  127. return [];
  128. }
  129. }
  130. public function getAuthorField()
  131. {
  132. foreach ($this->getFields() as $field) {
  133. if (
  134. $field['type'] == 'u'
  135. && $field['options_map']['autoassign'] == 1
  136. && ($this->isEnabled('userCanSeeOwn') or $this->isEnabled('writerCanModify'))
  137. ) {
  138. return $field['fieldId'];
  139. }
  140. }
  141. }
  142. public function getAuthorIpField()
  143. {
  144. foreach ($this->getFields() as $field) {
  145. if (
  146. $field['type'] == 'I'
  147. && $field['options_map']['autoassign'] == 1
  148. ) {
  149. return $field['fieldId'];
  150. }
  151. }
  152. }
  153. public function getWriterField()
  154. {
  155. foreach ($this->getFields() as $field) {
  156. if (
  157. in_array($field['type'], ['u', 'I'])
  158. && $field['options_map']['autoassign'] == 1
  159. ) {
  160. return $field['fieldId'];
  161. }
  162. }
  163. }
  164. public function getUserField()
  165. {
  166. foreach ($this->getFields() as $field) {
  167. if (
  168. $field['type'] == 'u'
  169. && $field['options_map']['autoassign'] == 1
  170. ) {
  171. return $field['fieldId'];
  172. }
  173. }
  174. }
  175. public function getItemOwnerFields()
  176. {
  177. $ownerFields = [];
  178. foreach ($this->getFields() as $field) {
  179. if (
  180. $field['type'] == 'u'
  181. && $field['options_map']['owner'] == 1
  182. ) {
  183. $ownerFields[] = $field['fieldId'];
  184. }
  185. }
  186. if (! $ownerFields) {
  187. $ownerFields = [$this->getUserField()];
  188. }
  189. return array_filter($ownerFields);
  190. }
  191. public function getItemGroupOwnerFields()
  192. {
  193. $ownerFields = [];
  194. foreach ($this->getFields() as $field) {
  195. if ($field['type'] == 'g' && ! empty($field['options_map']['owner'])) {
  196. $ownerFields[] = $field['fieldId'];
  197. }
  198. }
  199. if (! $ownerFields) {
  200. $ownerFields = [$this->getWriterGroupField()];
  201. }
  202. return array_filter($ownerFields);
  203. }
  204. public function getArticleField()
  205. {
  206. foreach ($this->getFields() as $field) {
  207. if ($field['type'] == 'articles') {
  208. return $field['fieldId'];
  209. }
  210. }
  211. }
  212. public function getGeolocationField()
  213. {
  214. foreach ($this->getFields() as $field) {
  215. if ($field['type'] == 'G' && in_array($field['options_map']['use_as_item_location'], [1, 'y'])) {
  216. return $field['fieldId'];
  217. }
  218. }
  219. }
  220. public function getWikiFields()
  221. {
  222. $fields = [];
  223. foreach ($this->getFields() as $field) {
  224. if ($field['type'] == 'wiki') {
  225. $fields[] = $field['fieldId'];
  226. }
  227. }
  228. return $fields;
  229. }
  230. public function getIconField()
  231. {
  232. foreach ($this->getFields() as $field) {
  233. if ($field['type'] == 'icon') {
  234. return $field['fieldId'];
  235. }
  236. }
  237. }
  238. public function getWriterGroupField()
  239. {
  240. foreach ($this->getFields() as $field) {
  241. if (
  242. $field['type'] == 'g'
  243. && $field['options_map']['autoassign'] == 1
  244. ) {
  245. return $field['fieldId'];
  246. }
  247. }
  248. }
  249. public function getRateField()
  250. {
  251. // This is here to support some legacy code for the deprecated 's' type rating field. It is not meant to be generically apply to the newer stars rating field
  252. foreach ($this->getFields() as $field) {
  253. // if ($field['type'] == 's' && $field['name'] == 'Rating') { // Do not force the name to be exactly the non-l10n string "Rating" to allow fetching the fieldID !!!
  254. if ($field['type'] == 's') {
  255. return $field['fieldId'];
  256. }
  257. }
  258. }
  259. public function getFreetagField()
  260. {
  261. foreach ($this->getFields() as $field) {
  262. if ($field['type'] == 'F') {
  263. return $field['fieldId'];
  264. }
  265. }
  266. }
  267. public function getLanguageField()
  268. {
  269. foreach ($this->getFields() as $field) {
  270. if (
  271. $field['type'] == 'LANG'
  272. && $field['options_map']['autoassign'] == 1
  273. ) {
  274. return $field['fieldId'];
  275. }
  276. }
  277. }
  278. public function getCategorizedFields()
  279. {
  280. $out = [];
  281. foreach ($this->getFields() as $field) {
  282. if ($field['type'] == 'e') {
  283. $out[] = $field['fieldId'];
  284. }
  285. }
  286. return $out;
  287. }
  288. public function getRelationField($relation)
  289. {
  290. foreach ($this->getFields() as $field) {
  291. if (
  292. $field['type'] == 'REL'
  293. && $field['options_map']['relation'] == $relation
  294. ) {
  295. return $field['fieldId'];
  296. }
  297. }
  298. }
  299. /**
  300. * Get the names of the item user(s) if any.
  301. * An item user is defined if a 'user selector' field
  302. * exist for this tracker and it has at least one user selected.
  303. *
  304. * @param int $itemId
  305. * @return array|mixed item user name
  306. */
  307. public function getItemUsers($itemId)
  308. {
  309. $trklib = TikiLib::lib('trk');
  310. return $trklib->get_item_creators($this->trackerInfo['trackerId'], $itemId);
  311. }
  312. public function getSyncInformation()
  313. {
  314. global $prefs;
  315. if ($prefs['tracker_remote_sync'] != 'y') {
  316. return false;
  317. }
  318. $attributelib = TikiLib::lib('attribute');
  319. $attributes = $attributelib->get_attributes('tracker', $this->getConfiguration('trackerId'));
  320. if (! isset($attributes['tiki.sync.provider'])) {
  321. return false;
  322. }
  323. return [
  324. 'provider' => $attributes['tiki.sync.provider'],
  325. 'source' => $attributes['tiki.sync.source'],
  326. 'last' => $attributes['tiki.sync.last'],
  327. 'modified' => $this->getConfiguration('lastModif') > $attributes['tiki.sync.last'],
  328. ];
  329. }
  330. public function canInsert(array $keyList)
  331. {
  332. foreach ($keyList as $key) {
  333. if (! $this->getFieldFromPermName($key)) {
  334. return false;
  335. }
  336. }
  337. return true;
  338. }
  339. }