PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/Logs.class.php

https://github.com/bgerp/bgerp
PHP | 430 lines | 212 code | 104 blank | 114 comment | 31 complexity | 6bfb34918f454819ca3e60d85ffeb7d9 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * @category bgerp
  6. * @package tags
  7. *
  8. * @author Yusein Yuseinov <yyuseinov@gmail.com>
  9. * @copyright 2006 - 2020 Experta OOD
  10. * @license GPL 3
  11. *
  12. * @since v 0.1
  13. */
  14. class tags_Logs extends core_Manager
  15. {
  16. /**
  17. * Заглавие на модела
  18. */
  19. public $title = 'Логове';
  20. /**
  21. * Кой има право да чете?
  22. */
  23. public $canRead = 'ceo, admin';
  24. /**
  25. * Кой има право да променя?
  26. */
  27. public $canEdit = 'no_one';
  28. /**
  29. * Кой има право да добавя?
  30. */
  31. public $canAdd = 'no_one';
  32. /**
  33. * Кой има право да го види?
  34. */
  35. public $canView = 'ceo, admin';
  36. /**
  37. * Кой може да го разглежда?
  38. */
  39. public $canList = 'ceo, admin';
  40. /**
  41. * Кой има право да го изтрие?
  42. */
  43. public $canDelete = 'no_one';
  44. /**
  45. * Кой има право да го изтрие?
  46. */
  47. public $canTag = 'powerUser';
  48. /**
  49. * Плъгини за зареждане
  50. */
  51. public $loadList = 'tags_Wrapper, plg_Created, plg_Search, plg_Sorting';
  52. /**
  53. * @var string
  54. */
  55. public $listFields = 'id, docLink=Документ, tagId, userId';
  56. /**
  57. * Описание на модела
  58. */
  59. public function description()
  60. {
  61. $this->FLD('docClassId', 'class(interface=doc_DocumentIntf,select=title,allowEmpty)', 'caption=Документ->Клас');
  62. $this->FLD('docId', 'int', 'caption=Документ->Обект');
  63. $this->FLD('containerId', 'key(mvc=doc_Containers)', 'caption = Контейнер');
  64. $this->FLD('tagId', 'key(mvc=tags_Tags, select=name, allowEmpty)', 'caption=Таг, refreshForm');
  65. $this->FLD('userId', 'user', 'caption=Потребител');
  66. $this->setDbUnique('containerId, tagId, userId');
  67. $this->setDbIndex('docId, docClassId');
  68. }
  69. /**
  70. * Добавя ограничение за типа
  71. *
  72. * @param $query
  73. * @param $userId
  74. */
  75. protected static function restrictQueryByType(&$query, $userId = null)
  76. {
  77. if (!isset($userId)) {
  78. $userId = core_Users::getCurrent();
  79. }
  80. $query->EXT('type', 'tags_Tags', 'externalKey=tagId');
  81. if ($userId) {
  82. $query->where("#type != 'personal'");
  83. $query->orWhere(array("#type = 'personal' AND #createdBy = '[#1#]'", $userId));
  84. }
  85. }
  86. /**
  87. * Помощна функция за вземана на таговете към документи
  88. *
  89. * @param mixed $docClassId
  90. * @param integer $docId
  91. * @param null|integer $userId
  92. * @param bool $order
  93. *
  94. * @return array
  95. */
  96. public static function getTagsFor($docClassId, $docId, $userId = null, $order = true)
  97. {
  98. if (!isset($userId)) {
  99. $userId = core_Users::getCurrent();
  100. }
  101. $query = self::getQuery();
  102. $query->where(array("#docId = '[#1#]'", $docId));
  103. $query->where(array("#docClassId = '[#1#]'", $docClassId));
  104. self::restrictQueryByType($query, $userId);
  105. if ($order) {
  106. $query->orderBy('type', 'DESC');
  107. $query->EXT('name', 'tags_Tags', 'externalKey=tagId');
  108. $query->orderBy('name', 'ASC');
  109. }
  110. $resArr = array();
  111. while ($rec = $query->fetch()) {
  112. $tArr = tags_Tags::getTagNameArr($rec->tagId);
  113. $resArr[$rec->id]['name'] = $tArr['name'];
  114. $resArr[$rec->id]['span'] = $tArr['span'];
  115. $resArr[$rec->id]['spanNoName'] = $tArr['spanNoName'];
  116. $resArr[$rec->id]['color'] = $tArr['color'];
  117. }
  118. return $resArr;
  119. }
  120. /**
  121. * След преобразуване на записа в четим за хора вид.
  122. *
  123. * @param core_Manager $mvc
  124. * @param stdClass $row Това ще се покаже
  125. * @param stdClass $rec Това е записа в машинно представяне
  126. */
  127. public static function on_AfterRecToVerbal($mvc, $row, $rec, $fields = array())
  128. {
  129. if (cls::load($rec->docClassId)) {
  130. $inst = cls::get($rec->docClassId);
  131. if ($inst->haveRightFor('single')) {
  132. $row->docLink = $inst->getLink($rec->docId, 0);
  133. }
  134. }
  135. if (!$row->docLink) {
  136. $row->docLink = tr('Липсващ документ');
  137. }
  138. if ($fields['-list']) {
  139. if ($rec->tagId) {
  140. $tArr = tags_Tags::getTagNameArr($rec->tagId);
  141. $row->tagId = "<span class='documentTags'>" . $tArr['spanNoName'] . $row->tagId . '</span>';
  142. }
  143. }
  144. }
  145. /**
  146. * Изпълнява се след подготовката на ролите, които могат да изпълняват това действие.
  147. *
  148. * Забранява изтриването на вече използвани сметки
  149. *
  150. * @param core_Manager $mvc
  151. * @param string $requiredRoles
  152. * @param string $action
  153. * @param stdClass|NULL $rec
  154. * @param int|NULL $userId
  155. */
  156. public static function on_AfterGetRequiredRoles($mvc, &$requiredRoles, $action, $rec = null, $userId = null)
  157. {
  158. if ($action == 'tag') {
  159. if ($rec && ($requiredRoles != 'no_one')) {
  160. if ($rec->containerId) {
  161. try{
  162. $document = doc_Containers::getDocument($rec->containerId);
  163. if (!$document->haveRightFor('single')) {
  164. $requiredRoles = 'no_one';
  165. }
  166. } catch(core_exception_Expect $e){
  167. wp($e, $rec);
  168. $requiredRoles = 'no_one';
  169. }
  170. }
  171. }
  172. }
  173. }
  174. /**
  175. *
  176. * @param $form
  177. * @param $cid
  178. */
  179. public static function prepareFormForTag(&$form, $cid)
  180. {
  181. $document = doc_Containers::getDocument($cid);
  182. $docClassId = $document->getClassId();
  183. $docId = $document->that;
  184. $userId = core_Users::getCurrent();
  185. $form->FNC('personalTags', 'keylist(mvc=tags_Tags, select=name, select2MinItems=28, columns=2)', 'caption=Тагове->Персонални, class=keylist-wide twoCols, input=input, silent');
  186. $form->FNC('commonTags', 'keylist(mvc=tags_Tags, select=name, select2MinItems=28, columns=2)', 'caption=Тагове->Общи, class=keylist-wide twoCols, input=input, silent');
  187. $query = self::getQuery();
  188. $query->where(array("#docId = '[#1#]'", $docId));
  189. $query->where(array("#docClassId = '[#1#]'", $docClassId));
  190. $form->_cQuery = clone $query;
  191. $query->show('tagId');
  192. self::restrictQueryByType($query, core_Users::getCurrent());
  193. $oldTagArr = array();
  194. while ($oRec = $query->fetch()) {
  195. $oldTagArr[$oRec->tagId] = $oRec->tagId;
  196. }
  197. $form->_oldTagArr = $oldTagArr;
  198. $tagsArr = tags_Tags::getTagsOptions($oldTagArr, $docClassId);
  199. $form->setSuggestions('personalTags', $tagsArr['personal']);
  200. $form->setSuggestions('commonTags', $tagsArr['common']);
  201. if (!empty($oldTagArr)) {
  202. $form->setDefault('personalTags', $oldTagArr);
  203. $form->setDefault('commonTags', $oldTagArr);
  204. }
  205. }
  206. /**
  207. * @param $form
  208. * @param $cid
  209. */
  210. public static function onSubmitFormForTag($form, $cid)
  211. {
  212. $cQuery = $form->_cQuery;
  213. $oldTagArr = $form->_oldTagArr;
  214. $document = doc_Containers::getDocument($cid);
  215. $docClassId = $document->getClassId();
  216. $docId = $document->that;
  217. $rec = $form->rec;
  218. $tArr = type_Keylist::toArray($rec->personalTags) + type_Keylist::toArray($rec->commonTags);
  219. foreach ($tArr as $tId) {
  220. if (!$oldTagArr[$tId]) {
  221. $rec = new stdClass();
  222. $rec->docClassId = $docClassId;
  223. $rec->docId = $docId;
  224. $rec->tagId = $tId;
  225. $rec->userId = core_Users::getCurrent();
  226. $rec->containerId = $cid;
  227. self::save($rec, null, 'IGNORE');
  228. } else {
  229. unset($oldTagArr[$tId]);
  230. }
  231. }
  232. // Изтрива старите премахнати записи
  233. if (!empty($oldTagArr)) {
  234. $cQuery->in('tagId', $oldTagArr);
  235. $cu = core_Users::getCurrent();
  236. while ($oRec = $cQuery->fetch()) {
  237. $tagType = tags_Tags::fetchField($oRec->tagId, 'type');
  238. if ($tagType == 'personal') {
  239. if ($oRec->createdBy != $cu) {
  240. continue;
  241. }
  242. }
  243. $containerId = $oRec->containerId;
  244. self::delete($oRec->id);
  245. self::clearCache($containerId);
  246. }
  247. }
  248. }
  249. /**
  250. * Екшън за редакция на таговете
  251. *
  252. * @return Redirect
  253. */
  254. function act_Tag()
  255. {
  256. $this->requireRightFor('tag');
  257. $cid = Request::get('id', 'int');
  258. $document = doc_Containers::getDocument($cid);
  259. $dRec = $document->fetch();
  260. $this->requireRightFor('tag', $dRec);
  261. $form = cls::get('core_Form');
  262. $form->title = 'Промяна на таговете на документ';
  263. $this->prepareFormForTag($form, $cid);
  264. $rec = $form->input();
  265. $retUrl = getRetUrl();
  266. if (empty($retUrl)) {
  267. $retUrl = array($document->instance, 'single', $document->that);
  268. }
  269. if ($form->isSubmitted()) {
  270. $this->onSubmitFormForTag($form, $cid);
  271. doc_Containers::logWrite('Промяна на таг', $cid);
  272. return new Redirect($retUrl);
  273. }
  274. $form->toolbar->addSbBtn('Запис', 'save', 'ef_icon = img/16/disk.png');
  275. $form->toolbar->addBtn('Отказ', $retUrl, 'ef_icon = img/16/close-red.png');
  276. return $form->renderHtml();
  277. }
  278. /**
  279. * Изпълнява се след запис на документ
  280. *
  281. * @param accda_Da $mvc
  282. * @param integer $id
  283. * @param stdClass $rec
  284. * @param null|string|array $fields
  285. */
  286. public static function on_AfterSave($mvc, &$id, $rec, $fields = null)
  287. {
  288. if ($rec->containerId) {
  289. $mvc->clearCache($rec->containerId);
  290. }
  291. }
  292. /**
  293. * Изчистване на кеша
  294. *
  295. * @param $containerId
  296. */
  297. public static function clearCache($containerId)
  298. {
  299. bgerp_Portal::invalidateCache(null, 'doc_drivers_FolderPortal');
  300. bgerp_Portal::invalidateCache(null, 'doc_drivers_LatestDocPortal');
  301. bgerp_Portal::invalidateCache(null, 'bgerp_drivers_Recently');
  302. bgerp_Portal::invalidateCache(null, 'bgerp_drivers_Tasks');
  303. bgerp_Portal::invalidateCache(null, 'bgerp_drivers_Calendar');
  304. doc_DocumentCache::cacheInvalidation($containerId);
  305. }
  306. /**
  307. * Филтър на on_AfterPrepareListFilter()
  308. * Малко манипулации след подготвянето на формата за филтриране
  309. *
  310. * @param core_Mvc $mvc
  311. * @param stdClass $data
  312. */
  313. protected static function on_AfterPrepareListFilter($mvc, $data)
  314. {
  315. $data->listFilter->view = 'horizontal';
  316. $data->listFilter->showFields = 'tagId';
  317. $data->listFilter->toolbar->addSbBtn('Филтрирай', array($mvc, 'list', 'show' => Request::get('show')), 'id=filter', 'ef_icon = img/16/funnel.png');
  318. $tagsArr = tags_Tags::getTagsOptions();
  319. $data->listFilter->setOptions('tagId', $tagsArr['all']);
  320. $data->listFilter->input(null, 'silent');
  321. if ($data->listFilter->rec->tagId) {
  322. $data->query->where(array("#tagId = '[#1#]'", $data->listFilter->rec->tagId));
  323. }
  324. $data->query->orderBy('createdOn', 'DESC');
  325. }
  326. }