api.whiteboard.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. /**
  3. * Just whiteboard. Helps to manage some non-urgent projets. Yep, without markers.
  4. */
  5. class WhiteBoard {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains available record categories as id=>name
  14. *
  15. * @var array
  16. */
  17. protected $categories = array();
  18. /**
  19. * Contains available whiteboard records as id=>recorddata
  20. *
  21. * @var array
  22. */
  23. protected $records = array();
  24. /**
  25. * Contains record priorities as id=>name
  26. *
  27. * @var array
  28. */
  29. protected $priorities = array();
  30. /**
  31. * Contains priority colors as priorityid=>color
  32. *
  33. * @var array
  34. */
  35. protected $prioColors = array();
  36. /**
  37. * Contains active employee as id=>employeename
  38. *
  39. * @var array
  40. */
  41. protected $activeEmployee = array();
  42. /**
  43. * Contains all employee as id=>employeename
  44. *
  45. * @var array
  46. */
  47. protected $allEmployee = array();
  48. /**
  49. * System message helper object placeholder
  50. *
  51. * @var object
  52. */
  53. protected $messages = '';
  54. /**
  55. * Additional comments object placeholder
  56. *
  57. * @var object
  58. */
  59. public $adcomments = '';
  60. /**
  61. * Additional comments scope
  62. */
  63. const SCOPE = 'WHITEBOARD';
  64. /**
  65. * Default control module URL
  66. */
  67. const URL_ME = '?module=whiteboard';
  68. /**
  69. * Table name to store whiteboard records
  70. */
  71. const REC_TABLE = 'whiteboard';
  72. /**
  73. * Creates new whiteboard instance
  74. *
  75. * @return void
  76. */
  77. public function __construct() {
  78. $this->loadAlter();
  79. $this->initMessages();
  80. $this->initAdcomments();
  81. $this->loadCategories();
  82. $this->setPriorities();
  83. $this->loadEmployeeData();
  84. $this->loadWhiteboardRecords();
  85. }
  86. /**
  87. * Loads system alter config into protected property
  88. *
  89. * @global object $ubillingConfig
  90. *
  91. * @return void
  92. */
  93. protected function loadAlter() {
  94. global $ubillingConfig;
  95. $this->altCfg = $ubillingConfig->getAlter();
  96. }
  97. /**
  98. * Inits adcomments obj for further usage
  99. *
  100. * @return void
  101. */
  102. protected function initAdcomments() {
  103. if ($this->altCfg['ADCOMMENTS_ENABLED']) {
  104. $this->adcomments = new ADcomments(self::SCOPE);
  105. }
  106. }
  107. /**
  108. * Loads/Sets categories
  109. *
  110. * @return void
  111. */
  112. protected function loadCategories() {
  113. $this->categories = array(
  114. 1 => __('Signups'),
  115. 2 => __('Network'),
  116. 3 => __('Marketing'),
  117. 4 => __('Finance'),
  118. 5 => __('Shopping'),
  119. 6 => __('Miscellaneous'),
  120. );
  121. }
  122. /**
  123. * Sets available records priorities
  124. *
  125. * @return void
  126. */
  127. protected function setPriorities() {
  128. $this->priorities = array(
  129. 1 => __('Indifferently'),
  130. 2 => __('Sometime later'),
  131. 3 => __('You need to do'),
  132. 4 => __('The faster the better'),
  133. 5 => __('Urgently') . '!',
  134. 6 => __('Matter of life and death'),
  135. );
  136. $this->prioColors = array(
  137. 1 => '820091',
  138. 2 => '202fa4',
  139. 3 => '005a20',
  140. 4 => '8a8500',
  141. 5 => 'f48a00',
  142. 6 => 'ea0000'
  143. );
  144. }
  145. /**
  146. * Inits system message helper obj for further usage
  147. *
  148. * @return void
  149. */
  150. protected function initMessages() {
  151. $this->messages = new UbillingMessageHelper();
  152. }
  153. /**
  154. * Loads all and active employee into protected props
  155. *
  156. * @return void
  157. */
  158. protected function loadEmployeeData() {
  159. $this->activeEmployee[0] = '-';
  160. $this->allEmployee = ts_GetAllEmployee();
  161. $this->activeEmployee += ts_GetActiveEmployee();
  162. }
  163. /**
  164. * Loads whiteboard records data from database
  165. *
  166. * @return void
  167. */
  168. protected function loadWhiteboardRecords() {
  169. $query = "SELECT * from `" . self::REC_TABLE . "` ORDER BY `priority` DESC";
  170. $all = simple_queryall($query);
  171. if (!empty($all)) {
  172. foreach ($all as $io => $each) {
  173. $this->records[$each['id']] = $each;
  174. }
  175. }
  176. }
  177. /**
  178. * Renders module controls
  179. *
  180. * @return string
  181. */
  182. public function renderControls() {
  183. $result = '';
  184. if (wf_CheckGet(array('showrecord'))) {
  185. $result.=wf_BackLink(self::URL_ME);
  186. }
  187. $result.=wf_modalAuto(web_icon_create() . ' ' . __('Create'), __('Create'), $this->renderCreateForm(), 'ubButton');
  188. if ((!wf_CheckGet(array('showrecord')))) {
  189. if (!wf_CheckGet(array('onlydone'))) {
  190. $result.=wf_Link(self::URL_ME . '&onlydone=true', wf_img('skins/done_icon.png') . ' ' . __('Done'), false, 'ubButton');
  191. } else {
  192. $result.=wf_Link(self::URL_ME, wf_img('skins/undone_icon.png') . ' ' . __('Undone'), false, 'ubButton');
  193. }
  194. }
  195. return ($result);
  196. }
  197. /**
  198. * Renders record creation form
  199. *
  200. * @return string
  201. */
  202. protected function renderCreateForm() {
  203. $result = '';
  204. if ((!empty($this->categories)) AND ( $this->priorities)) {
  205. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  206. $inputs = wf_HiddenInput('createnewrecord', 'true');
  207. $inputs.= wf_Selector('newcategory', $this->categories, __('Category') . $sup, '', true);
  208. $inputs.= wf_Selector('newpriority', $this->priorities, __('Priority') . $sup, '', true);
  209. $inputs.= wf_TextInput('newname', __('Name') . $sup, '', true, 40);
  210. $inputs.=__('Text') . wf_tag('br');
  211. $inputs.= wf_TextArea('newtext', '', '', true, '60x20');
  212. $inputs.= wf_Selector('newemployeeid', $this->activeEmployee, __('Who should do'), '', true);
  213. $inputs.= wf_delimiter();
  214. $inputs.=wf_Submit(__('Create'));
  215. $result.=wf_Form('', 'POST', $inputs, 'glamour');
  216. }
  217. return ($result);
  218. }
  219. /**
  220. * Renders record editing form
  221. *
  222. * @param int $recordId
  223. *
  224. * @return string
  225. */
  226. protected function renderEditForm($recordId) {
  227. $recordId = vf($recordId, 3);
  228. $result = '';
  229. if ((!empty($this->categories)) AND ( $this->priorities)) {
  230. if ($this->records[$recordId]) {
  231. $recordData = $this->records[$recordId];
  232. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  233. $inputs = wf_HiddenInput('editrecord', $recordId);
  234. $inputs.= wf_Selector('editcategory', $this->categories, __('Category') . $sup, $recordData['categoryid'], true);
  235. $inputs.= wf_Selector('editpriority', $this->priorities, __('Priority') . $sup, $recordData['priority'], true);
  236. $inputs.= wf_TextInput('editname', __('Name') . $sup, $recordData['name'], true, 40);
  237. $inputs.=__('Text') . wf_tag('br');
  238. $inputs.= wf_TextArea('edittext', '', $recordData['text'], true, '60x20');
  239. $inputs.= wf_Selector('editemployeeid', $this->activeEmployee, __('Who should do'), $recordData['employeeid'], true);
  240. $doneFlag = ($recordData['donedate']) ? true : false;
  241. $inputs.= wf_CheckInput('editdone', __('This task is done'), true, $doneFlag);
  242. $inputs.= wf_delimiter();
  243. if (cfr('ROOT')) {
  244. $inputs.=wf_tag('div', false, '', 'style="float:right;"');
  245. $inputs.=wf_JSAlertStyled(self::URL_ME . '&deleterecord=' . $recordId, web_delete_icon() . ' ' . __('Remove this task - it is an mistake'), $this->messages->getDeleteAlert(), '');
  246. $inputs.=wf_tag('div', true);
  247. }
  248. $inputs.=wf_Submit(__('Save'));
  249. $result.=wf_Form('', 'POST', $inputs, 'glamour');
  250. $result.=wf_delimiter();
  251. }
  252. }
  253. return ($result);
  254. }
  255. /**
  256. * Saves record if editing required
  257. *
  258. * @return void
  259. */
  260. public function saveRecord() {
  261. if (wf_CheckPost(array('editrecord', 'editcategory', 'editpriority', 'editname'))) {
  262. $recordId = vf($_POST['editrecord']);
  263. if ($this->isMyRecord($recordId)) {
  264. $where = "WHERE `id`='" . $recordId . "'";
  265. simple_update_field(self::REC_TABLE, 'categoryid', $_POST['editcategory'], $where);
  266. simple_update_field(self::REC_TABLE, 'priority', $_POST['editpriority'], $where);
  267. simple_update_field(self::REC_TABLE, 'name', $_POST['editname'], $where);
  268. simple_update_field(self::REC_TABLE, 'text', $_POST['edittext'], $where);
  269. simple_update_field(self::REC_TABLE, 'employeeid', $_POST['editemployeeid'], $where);
  270. if (wf_CheckPost(array('editdone'))) {
  271. simple_update_field(self::REC_TABLE, 'donedate', curdatetime(), $where);
  272. } else {
  273. simple_update_field(self::REC_TABLE, 'donedate', 'NULL', $where, true);
  274. }
  275. log_register('WHITEBOARD EDIT RECORD [' . $recordId . ']');
  276. }
  277. }
  278. }
  279. /**
  280. * Returns administrator realname or login
  281. *
  282. * @param int $recordId
  283. *
  284. * @return string
  285. */
  286. public function getCreator($recordId) {
  287. $result = '';
  288. if (isset($this->records[$recordId])) {
  289. $creatorLogin = $this->records[$recordId]['admin'];
  290. @$employeeLogins = unserialize(ts_GetAllEmployeeLoginsCached());
  291. $authorRealname = (isset($employeeLogins[$creatorLogin])) ? $employeeLogins[$creatorLogin] : $creatorLogin;
  292. $result = $authorRealname;
  293. }
  294. return ($result);
  295. }
  296. /**
  297. * Deletes record from database
  298. *
  299. * @param int $recordId
  300. *
  301. * @return void
  302. */
  303. public function delete($recordId) {
  304. $recordId = vf($recordId, 3);
  305. if (isset($this->records[$recordId])) {
  306. if (cfr('ROOT')) {
  307. $query = "DELETE FROM `whiteboard` WHERE `id`='" . $recordId . "';";
  308. nr_query($query);
  309. log_register('WHITEBOARD DELETE RECORD [' . $recordId . ']');
  310. }
  311. }
  312. }
  313. /**
  314. * Renders custom records styles by their priority colors
  315. *
  316. * @return string
  317. */
  318. protected function getStyles() {
  319. $result = '';
  320. if (!empty($this->prioColors)) {
  321. $result.=wf_tag('style');
  322. foreach ($this->prioColors as $io => $each) {
  323. $result.='.wbpriority_' . $io . ' { background-color:#' . $each . '; padding: 10px; }';
  324. }
  325. $result.=wf_tag('style', true);
  326. $result.=wf_tag('script');
  327. $result.='$( function() { $( ".whiteboard" ).draggable({ scroll: false }); } );';
  328. $result.=wf_tag('script', true);
  329. }
  330. return ($result);
  331. }
  332. /**
  333. * Creates new record in database
  334. *
  335. * @return void
  336. */
  337. public function createRecord() {
  338. if (wf_CheckPost(array('createnewrecord', 'newcategory', 'newpriority', 'newname'))) {
  339. $category = vf($_POST['newcategory'], 3);
  340. $priority = vf($_POST['newpriority'], 3);
  341. $employeeid = vf($_POST['newemployeeid'], 3);
  342. $name = mysql_real_escape_string($_POST['newname']);
  343. $text = mysql_real_escape_string($_POST['newtext']);
  344. $createdate = curdatetime();
  345. $admin = whoami();
  346. $query = "INSERT INTO `" . self::REC_TABLE . "` (`id`,`categoryid`,`admin`,`employeeid`,`createdate`,`donedate`,`priority`,`name`,`text`) VALUES ";
  347. $query.= "(NULL, '" . $category . "','" . $admin . "','" . $employeeid . "','" . $createdate . "',NULL,'" . $priority . "','" . $name . "','" . $text . "');";
  348. nr_query($query);
  349. $newId = simple_get_lastid(self::REC_TABLE);
  350. log_register('WHITEBOARD CREATE RECORD [' . $newId . ']');
  351. }
  352. }
  353. /**
  354. * Renders available records as default whiteboard view
  355. *
  356. * @return string
  357. */
  358. public function renderRecordsList() {
  359. $result = '';
  360. $result.=$this->getStyles();
  361. $tmpArr = array();
  362. $doneFlag = (wf_CheckGet(array('onlydone'))) ? true : false;
  363. if (!empty($this->records)) {
  364. foreach ($this->records as $io => $each) {
  365. if ($doneFlag) {
  366. if ($each['donedate']) {
  367. $tmpArr[$each['categoryid']][] = $each;
  368. }
  369. } else {
  370. if (!$each['donedate']) {
  371. $tmpArr[$each['categoryid']][] = $each;
  372. }
  373. }
  374. }
  375. /**
  376. * Вареники, борщ,
  377. * Позаторішнє сало,
  378. * Півметра ковбаси -
  379. * Усе попропадало!
  380. * То що мені робити?
  381. * Не з'їм, то будуть тапки...
  382. * І хто мене врятує?..
  383. * Капітан Канапка-а-а!
  384. */
  385. if (!empty($tmpArr)) {
  386. $result.=wf_tag('div', false, 'whiteboardbg');
  387. foreach ($tmpArr as $categoryId => $records) {
  388. $result.=wf_tag('div', false, 'whiteboard');
  389. $result.=wf_tag('h2') . $this->categories[$categoryId] . wf_tag('h2', true);
  390. if (!empty($records)) {
  391. $rows = '';
  392. foreach ($records as $io => $recordData) {
  393. $commentsCount = $this->adcomments->getCommentsCount($recordData['id']);
  394. $commentsLabel = ($commentsCount > 0) ? ' (' . $commentsCount . ')' : '';
  395. $cells = wf_TableCell(wf_Link(self::URL_ME . '&showrecord=' . $recordData['id'], $recordData['name'] . $commentsLabel), '', 'wbpriority_' . $recordData['priority']);
  396. $rows.=wf_TableRow($cells);
  397. }
  398. $result.=wf_TableBody($rows, '100%', 0, '');
  399. }
  400. $result.=wf_tag('div', true);
  401. }
  402. $result.=wf_CleanDiv();
  403. $result.=wf_tag('div', true);
  404. }
  405. } else {
  406. $result.=$this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  407. }
  408. return ($result);
  409. }
  410. /**
  411. * Checks is some record editable by current user
  412. *
  413. * @param int $recordId
  414. *
  415. * @return bool
  416. */
  417. protected function isMyRecord($recordId) {
  418. $result = false;
  419. if (isset($this->records[$recordId])) {
  420. $myLogin = whoami();
  421. if (($this->records[$recordId]['admin'] == $myLogin) OR ( cfr('ROOT'))) {
  422. $result = true;
  423. }
  424. }
  425. return ($result);
  426. }
  427. /**
  428. * Renders record view form with some edit controls if record is created by current user
  429. *
  430. * @param int $recordId
  431. *
  432. * @return string
  433. */
  434. public function renderRecord($recordId) {
  435. $result = '';
  436. if (isset($this->records[$recordId])) {
  437. $recordData = $this->records[$recordId];
  438. $taskCreateForm = ' ' . wf_modal(wf_img('skins/createtask.gif', __('Create task')), __('Create task'), ts_TaskCreateFormUnified($recordData['name'], '', ''), '', '450', '540');
  439. $cells = wf_TableCell(__('Category') . $taskCreateForm, '20%', 'row2');
  440. $cells.= wf_TableCell($this->categories[$recordData['categoryid']]);
  441. $rows = wf_TableRow($cells, 'row3');
  442. $cells = wf_TableCell(__('Priority'), '', 'row2');
  443. $fc = wf_tag('font', false, '', 'color="#' . $this->prioColors[$recordData['priority']] . '"');
  444. $fe = wf_tag('font', true);
  445. $cells.= wf_TableCell($fc . $this->priorities[$recordData['priority']] . $fe);
  446. $rows.= wf_TableRow($cells, 'row3');
  447. $cells = wf_TableCell(__('Name'), '', 'row2');
  448. $cells.= wf_TableCell($recordData['name']);
  449. $rows.= wf_TableRow($cells, 'row3');
  450. $cells = wf_TableCell(__('Who should do'), '', 'row2');
  451. $cells.= wf_TableCell(@$this->allEmployee[$recordData['employeeid']]);
  452. $rows.= wf_TableRow($cells, 'row3');
  453. $cells = wf_TableCell(__('Creation date') . ' / ' . __('Finish date'), '', 'row2');
  454. $doneDate = ($recordData['donedate']) ? $recordData['donedate'] : __('Undone');
  455. $cells.= wf_TableCell($recordData['createdate'] . ' / ' . $doneDate);
  456. $rows.= wf_TableRow($cells, 'row3');
  457. $cells = wf_TableCell(__('Text'), '', 'row2');
  458. $cells.= wf_TableCell(nl2br($recordData['text']));
  459. $rows.= wf_TableRow($cells, 'row3');
  460. $result.=wf_TableBody($rows, '100%', 0, '');
  461. if ($this->isMyRecord($recordId)) {
  462. $result.=wf_tag('br');
  463. $result.=wf_modalAuto(web_edit_icon() . ' ' . __('Edit'), __('Edit'), $this->renderEditForm($recordId), 'ubButton');
  464. }
  465. } else {
  466. $result.=$this->messages->getStyledMessage(__('Something went wrong') . ': EX_RECORD_ID_NOT_EXIST', 'error');
  467. }
  468. return ($result);
  469. }
  470. }