api.tagcloud.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /**
  3. * User tags cloud rendering
  4. */
  5. class TagCloud {
  6. /**
  7. * Contains all tagged users
  8. *
  9. * @var array
  10. */
  11. protected $alltags = array();
  12. /**
  13. * Contains user tags data as id=>data login/tagid
  14. *
  15. * @var array
  16. */
  17. protected $usertags = array();
  18. /**
  19. * Contains available tagnames as id=>name
  20. *
  21. * @var array
  22. */
  23. protected $allnames = array();
  24. /**
  25. * Contains tags power based on assigns count as id=>power
  26. *
  27. * @var array
  28. */
  29. protected $tagspower = array();
  30. /**
  31. * Contains users with no tags assigned
  32. *
  33. * @var array
  34. */
  35. protected $notags = array();
  36. /**
  37. * Contains users that not have employee tags
  38. *
  39. * @var array
  40. */
  41. protected $noEmployeeTags = array();
  42. const URL_ME = '?module=tagcloud';
  43. const URL_GRID = 'gridview=true';
  44. const URL_REPORT = 'report=true';
  45. const NO_TAG = 'notags=true';
  46. const NO_EMPLOYEE_TAG = 'noemployeetags=true';
  47. public function __construct() {
  48. $this->loadTags();
  49. $this->loadTagNames();
  50. $this->loadUserTags();
  51. $this->tagPowerPreprocessing();
  52. $this->panel();
  53. }
  54. /**
  55. * loads all used tags into private data property
  56. *
  57. * @return void
  58. */
  59. protected function loadTags() {
  60. $this->alltags = $this->getAllTagged();
  61. }
  62. /**
  63. * loads all tag names into private data property
  64. *
  65. * @return void
  66. */
  67. protected function loadTagNames() {
  68. $this->allnames = $this->getAllTagNames();
  69. }
  70. /**
  71. * loads all users tags into private data property
  72. *
  73. * @return void
  74. */
  75. protected function loadUserTags() {
  76. $query = "SELECT `login`,`tagid`,`id` from `tags`";
  77. $all = simple_queryall($query);
  78. if (!empty($all)) {
  79. foreach ($all as $io => $each) {
  80. $this->usertags[$each['id']]['login'] = $each['login'];
  81. $this->usertags[$each['id']]['tagid'] = $each['tagid'];
  82. }
  83. }
  84. }
  85. /**
  86. * preprocessing of tagspower by alltags private property
  87. *
  88. * @return void
  89. */
  90. protected function tagPowerPreprocessing() {
  91. if (!empty($this->usertags)) {
  92. foreach ($this->usertags as $io => $each) {
  93. if (isset($this->tagspower[$each['tagid']])) {
  94. $this->tagspower[$each['tagid']] ++;
  95. } else {
  96. $this->tagspower[$each['tagid']] = 1;
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Gets all tagged users
  103. *
  104. * @return array
  105. */
  106. protected function getAllTagged() {
  107. $query = 'SELECT DISTINCT `tagid` from `tags` ORDER BY `tagid` ASC';
  108. $alltags = simple_queryall($query);
  109. return ($alltags);
  110. }
  111. /**
  112. * Gets some tag power by its ID
  113. *
  114. * @param $tagid - existing tag ID
  115. *
  116. * @return int
  117. */
  118. protected function getTagPower($tagid) {
  119. if (!empty($this->tagspower)) {
  120. if (isset($this->tagspower[$tagid])) {
  121. $result = $this->tagspower[$tagid];
  122. } else {
  123. $result = 0;
  124. }
  125. } else {
  126. $result = 0;
  127. }
  128. return ($result);
  129. }
  130. /**
  131. * Gets all tags names as array tagid=>tagname
  132. *
  133. * @return array
  134. */
  135. protected function getAllTagNames() {
  136. $query = "SELECT `id`,`tagname` from `tagtypes`";
  137. $result = array();
  138. $alltags = simple_queryall($query);
  139. if (!empty($alltags)) {
  140. foreach ($alltags as $io => $eachtag) {
  141. $result[$eachtag['id']] = $eachtag['tagname'];
  142. }
  143. }
  144. return($result);
  145. }
  146. /**
  147. * returns control panel for tagcloud
  148. *
  149. * @return string
  150. */
  151. protected function panel() {
  152. $result = wf_Link(self::URL_ME, wf_img('skins/icon_cloud.png') . ' ' . __('Tag cloud'), false, 'ubButton');
  153. $result .= wf_Link(self::URL_ME . '&' . self::URL_GRID, wf_img('skins/icon_table.png') . ' ' . __('Grid view'), false, 'ubButton');
  154. $result .= wf_Link(self::URL_ME . '&' . self::URL_REPORT, wf_img('skins/ukv/report.png') . ' ' . __('Report'), false, 'ubButton');
  155. $result .= wf_Link(self::URL_ME . '&' . self::NO_TAG, wf_img('skins/track_icon.png') . ' ' . __('No tags'), false, 'ubButton');
  156. $result .= wf_Link(self::URL_ME . '&' . self::NO_EMPLOYEE_TAG, wf_img('skins/menuicons/employee.png') . ' ' . __('No employee tags'), true, 'ubButton');
  157. $result .= show_window('', $result);
  158. return ($result);
  159. }
  160. /**
  161. * loads users that no have employee tags
  162. *
  163. * @return void
  164. */
  165. protected function loadNoEmployeeTagss() {
  166. $this->noEmployeeTags = $this->getNoEmployeeTagged();
  167. }
  168. /**
  169. * loads no tag user names into private data property
  170. *
  171. * @return void
  172. */
  173. protected function loadNoTagUsers() {
  174. $this->notags = $this->getNoTagged();
  175. }
  176. /**
  177. * Returns array of users without tags
  178. *
  179. * @return array
  180. */
  181. protected function getNoTagged() {
  182. $query = 'SELECT `users`.`login`,`tags`.`id` FROM `users` LEFT JOIN `tags` ON `users`.`login`=`tags`.`login` WHERE `tags`.`id` IS NULL ORDER BY `tags`.`id` ASC';
  183. $notags = simple_queryall($query);
  184. return ($notags);
  185. }
  186. /**
  187. * Returns array of users that no have employee tags
  188. *
  189. * @return array
  190. */
  191. protected function getNoEmployeeTagged() {
  192. $result = array();
  193. $query = 'SELECT login,employee.id FROM `tags` LEFT JOIN (SELECT `id`,`tagid`,`name` FROM `employee` WHERE `tagid` IS NOT NUll) as employee USING (`tagid`) GROUP by login';
  194. $resultQuery = simple_queryall($query);
  195. if (!empty($resultQuery)) {
  196. foreach ($resultQuery as $key => $raw) {
  197. if (empty($raw['id'])) {
  198. $result[] = $raw['login'];
  199. }
  200. }
  201. }
  202. return ($result);
  203. }
  204. /**
  205. * Renders tag grid for users that no tagged
  206. *
  207. * @return void
  208. */
  209. public function renderNoEmployeeTags() {
  210. $result = '';
  211. $userArr = array();
  212. //usage of this in constructor significantly reduces performance
  213. $this->loadNoEmployeeTagss();
  214. if (!empty($this->noEmployeeTags)) {
  215. foreach ($this->noEmployeeTags as $key => $user) {
  216. $userArr[] = $user;
  217. }
  218. }
  219. $result .= web_UserArrayShower($userArr);
  220. show_window(__('No employee tags'), $result);
  221. }
  222. /**
  223. * Renders tag grid for users that no tagged
  224. *
  225. * @return void
  226. */
  227. public function renderNoTagGrid() {
  228. $result = '';
  229. $userArr = array();
  230. //usage of this in constructor significantly reduces performance
  231. $this->loadNoTagUsers();
  232. if (!empty($this->notags)) {
  233. foreach ($this->notags as $key => $user) {
  234. $userArr[] = $user['login'];
  235. }
  236. }
  237. $result .= web_UserArrayShower($userArr);
  238. show_window(__('No tags'), $result);
  239. }
  240. /**
  241. * Renders tag grid for tagged users
  242. *
  243. * @return void
  244. */
  245. public function renderTagGrid() {
  246. $cells = wf_TableCell(__('ID'));
  247. $cells .= wf_TableCell(__('Tags'));
  248. $cells .= wf_TableCell(__('Users'));
  249. $rows = wf_TableRow($cells, 'row1');
  250. if (!empty($this->alltags)) {
  251. foreach ($this->alltags as $key => $eachtag) {
  252. if (isset($this->allnames[$eachtag['tagid']])) {
  253. $userCount = $this->getTagPower($eachtag['tagid']);
  254. $cells = wf_TableCell($eachtag['tagid']);
  255. $cells .= wf_TableCell(wf_Link('?module=tagcloud&gridview=true&tagid=' . $eachtag['tagid'], $this->allnames[$eachtag['tagid']], false));
  256. $cells .= wf_TableCell($userCount);
  257. $rows .= wf_TableRow($cells, 'row3');
  258. }
  259. }
  260. }
  261. $result = wf_TableBody($rows, '100%', '0', 'sortable');
  262. show_window(__('Tags'), $result);
  263. }
  264. /**
  265. * Renders tag cloud for tagged users
  266. *
  267. * @return void
  268. */
  269. public function renderTagCloud() {
  270. $result = wf_tag('center');
  271. if (!empty($this->alltags)) {
  272. foreach ($this->alltags as $key => $eachtag) {
  273. $power = $this->getTagPower($eachtag['tagid']);
  274. $fsize = $power / 2;
  275. if (isset($this->allnames[$eachtag['tagid']])) {
  276. $sup = wf_tag('sup') . $power . wf_tag('sup', true);
  277. $result .= wf_tag('font', false, '', 'size="' . $fsize . '"');
  278. $result .= wf_Link(self::URL_ME . '&tagid=' . $eachtag['tagid'], $this->allnames[$eachtag['tagid']] . $sup, false);
  279. $result .= wf_tag('font', true);
  280. }
  281. }
  282. }
  283. $result .= wf_tag('center', true);
  284. show_window(__('Tags'), $result);
  285. }
  286. /**
  287. * Renders tagged users by tag ID
  288. *
  289. * @param $tagid - existing tag ID
  290. *
  291. * @return void
  292. */
  293. public function renderTagUsers($tagid) {
  294. $userarr = array();
  295. if (!empty($this->usertags)) {
  296. foreach ($this->usertags as $io => $each) {
  297. if ($each['tagid'] == $tagid) {
  298. $userarr[] = $each['login'];
  299. }
  300. }
  301. }
  302. $result = web_UserArrayShower($userarr);
  303. show_window(__('Tag') . ': ' . @$this->allnames[$tagid], $result);
  304. }
  305. /**
  306. * Renders tags assign report
  307. *
  308. * @return void
  309. */
  310. public function renderReport() {
  311. $result = '';
  312. $resultUsers = '';
  313. $messages = new UbillingMessageHelper();
  314. $months = months_array_localized();
  315. $reportTmp = array();
  316. $loginsTmp = array();
  317. $totalCount = 0;
  318. if (!empty($this->allnames)) {
  319. $result .= wf_tag('br');
  320. $curYear = (wf_CheckPost(array('reportyear'))) ? vf($_POST['reportyear'], 3) : curyear();
  321. $inputs = wf_YearSelectorPreset('reportyear', __('Year'), false, $curYear) . ' ';
  322. $curTagid = (wf_CheckPost(array('reporttagid'))) ? vf($_POST['reporttagid'], 3) : '';
  323. $inputs .= wf_Selector('reporttagid', $this->allnames, __('Tag'), $curTagid, false) . ' ';
  324. $inputs .= wf_CheckInput('renderusers', __('Users'), false, ubRouting::checkPost('renderusers')) . ' ';
  325. $inputs .= wf_Submit(__('Show'));
  326. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  327. } else {
  328. $result .= $messages->getStyledMessage(__('Nothing found'), 'info');
  329. }
  330. if (wf_CheckPost(array('reportyear', 'reporttagid'))) {
  331. $tagid = vf($_POST['reporttagid'], 3);
  332. $year = vf($_POST['reportyear'], 3);
  333. $datemask = $year . '-%';
  334. $query = "SELECT * from `weblogs` WHERE `date` LIKE '" . $datemask . "' AND `event` LIKE 'TAGADD (%TAGID%'";
  335. $raw = simple_queryall($query);
  336. if (!empty($raw)) {
  337. foreach ($raw as $io => $each) {
  338. $eventtagid = preg_match("/\[[^\]]*\]/", $each['event'], $matches);
  339. $eventLogin = preg_match('!\((.*?)\)!si', $each['event'], $tmpLoginMatches);
  340. @$eventtagid = ubRouting::filters($matches[0], 'int');
  341. @$eventLogin = $tmpLoginMatches[1];
  342. if (!empty($eventtagid)) {
  343. if ($eventtagid == $tagid) {
  344. $eventTime = strtotime($each['date']);
  345. $eventMonth = date("m", $eventTime);
  346. if (!isset($reportTmp[$eventMonth])) {
  347. $reportTmp[$eventMonth] = 1;
  348. } else {
  349. $reportTmp[$eventMonth] ++;
  350. }
  351. $totalCount++;
  352. //login stats
  353. if (!empty($eventLogin)) {
  354. $loginsTmp[$eventMonth][$eventLogin] = $eventLogin;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. $cells = wf_TableCell($year);
  361. $cells .= wf_TableCell(__('Month'));
  362. $cells .= wf_TableCell($this->allnames[$tagid]);
  363. $cells .= wf_TableCell(__('Visual'));
  364. $rows = wf_TableRow($cells, 'row1');
  365. foreach ($months as $monthNumber => $monthName) {
  366. $cells = wf_TableCell($monthNumber);
  367. $cells .= wf_TableCell($monthName);
  368. $monthData = (isset($reportTmp[$monthNumber])) ? $reportTmp[$monthNumber] : 0;
  369. $cells .= wf_TableCell($monthData);
  370. $cells .= wf_TableCell(web_bar($monthData, $totalCount), '', '', 'sorttable_customkey="' . $monthData . '"');
  371. $rows .= wf_TableRow($cells, 'row3');
  372. }
  373. $result .= wf_TableBody($rows, '100%', '0', 'sortable');
  374. $result .= wf_tag('b') . __('Total') . ':' . wf_tag('b', true) . ' ' . $totalCount;
  375. }
  376. show_window(__('Tags'), $result);
  377. //optional users rendering
  378. if (ubRouting::checkPost('renderusers')) {
  379. if (!empty($loginsTmp)) {
  380. foreach ($loginsTmp as $io => $each) {
  381. show_window($months[$io], web_UserArrayShower($each));
  382. }
  383. }
  384. }
  385. }
  386. }
  387. ?>