api.stickynotes.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. <?php
  2. /**
  3. * Administator reminders and notes implementation
  4. */
  5. class StickyNotes {
  6. /**
  7. * Contains available user notes
  8. *
  9. * @var array
  10. */
  11. protected $allnotes = array();
  12. /**
  13. * Contains all available revelations
  14. *
  15. * @var array
  16. */
  17. protected $allRevelations = array();
  18. /**
  19. * Is revelations enabled?
  20. *
  21. * @var bool
  22. */
  23. protected $revelationsFlag = false;
  24. /**
  25. * Contains active user notes which may require notification
  26. *
  27. * @var array
  28. */
  29. protected $activenotes = array();
  30. /**
  31. * Contains current instance user login
  32. *
  33. * @var string
  34. */
  35. protected $myLogin = '';
  36. /**
  37. * System caching object placeholder
  38. *
  39. * @var object
  40. */
  41. protected $cache = '';
  42. /**
  43. * Just a flag of preview functionality in notes grid list
  44. *
  45. * @var bool
  46. */
  47. protected $notesPreview = true;
  48. /**
  49. * Default taskbar notifications caching timeout in seconds
  50. */
  51. const CACHE_TIMEOUT = 3600;
  52. /**
  53. * Default cache key name for personal notes
  54. */
  55. const CACHE_KEY_NOTES = 'STICKYNOTES';
  56. /**
  57. * Default cache key name for revelations
  58. */
  59. const CACHE_KEY_REVELATIONS = 'STICKYREVELATIONS';
  60. /**
  61. * Default notes management module URL
  62. */
  63. const URL_ME = '?module=stickynotes';
  64. /**
  65. * Default revelations management module URL
  66. */
  67. const URL_REVELATIONS = '?module=stickynotes&revelations=true';
  68. /**
  69. * Sets max uncutted note text size
  70. */
  71. const PREVIEW_LEN = 190;
  72. /**
  73. * Routes and other predefined stuff
  74. */
  75. const ROUTE_CALENDAR = 'calendarview';
  76. const ROUTE_BACK = 'backurl';
  77. const ROUTE_SHOW_NOTE = 'shownote';
  78. const ROUTE_EDIT_FORM = 'editform';
  79. const ROUTE_DEL_NOTE = 'delete';
  80. const PROUTE_NEW_NOTE = 'newtext';
  81. const PROUTE_EDIT_NOTE_TEXT = 'edittext';
  82. const PROUTE_EDIT_NOTE_ID = 'editnoteid';
  83. const PROUTE_NEW_REVELATION = 'newrevelationtext';
  84. const ROUTE_DEL_REV = 'deleterev';
  85. const PROUTE_EDIT_REV_TEXT = 'editrevelationtext';
  86. const PROUTE_EDIT_REV_ID = 'editrevelationid';
  87. const ROUTE_EDIT_REV_FORM = 'editrev';
  88. const ROUTE_REVELATIONS = 'revelations';
  89. const PROUTE_REMIND_DATE = 'reminddate';
  90. const PROUTE_REMIND_TIME = 'remindtime';
  91. /**
  92. * Creates new sticky notes instance
  93. *
  94. * @param bool $onlyActive
  95. *
  96. * @return void
  97. */
  98. public function __construct($onlyActive) {
  99. $this->setLogin();
  100. $this->loadConfig();
  101. $this->initCache();
  102. if ($onlyActive) {
  103. $this->loadActiveNotes();
  104. if ($this->revelationsFlag) {
  105. $this->loadRevelations(true);
  106. }
  107. } else {
  108. $this->loadAllNotes();
  109. if ($this->revelationsFlag) {
  110. $this->loadRevelations(false);
  111. }
  112. }
  113. }
  114. /**
  115. * Loads required options
  116. *
  117. * @global object $ubillingConfig
  118. *
  119. * @return void
  120. */
  121. protected function loadConfig() {
  122. global $ubillingConfig;
  123. $this->revelationsFlag = $ubillingConfig->getAlterParam('STICKY_REVELATIONS_ENABLED');
  124. $noPreviewOption = $ubillingConfig->getAlterParam('STICKY_NOTES_NOPREVIEW');
  125. if ($noPreviewOption) {
  126. $this->notesPreview = false;
  127. }
  128. }
  129. /**
  130. * Sets current instance user login into protected property
  131. *
  132. * @return void
  133. */
  134. protected function setLogin() {
  135. $this->myLogin = whoami();
  136. }
  137. /**
  138. * Inits system caching object instance
  139. *
  140. * @return void
  141. */
  142. protected function initCache() {
  143. $this->cache = new UbillingCache();
  144. }
  145. /**
  146. * Flushes cache keys on sticky notes or revelations changes
  147. *
  148. * @return void
  149. */
  150. protected function flushCache() {
  151. $this->cache->delete(self::CACHE_KEY_NOTES);
  152. $this->cache->delete(self::CACHE_KEY_REVELATIONS);
  153. }
  154. /**
  155. * Loads notes from database into private property
  156. *
  157. * @return void
  158. */
  159. protected function loadAllNotes() {
  160. $query = "SELECT * from `stickynotes` WHERE `owner`= '" . $this->myLogin . "' ORDER BY `id` DESC";
  161. $tmpArr = simple_queryall($query);
  162. //map id=>id
  163. if (!empty($tmpArr)) {
  164. foreach ($tmpArr as $io => $each) {
  165. $this->allnotes[$each['id']] = $each;
  166. }
  167. }
  168. }
  169. /**
  170. * Loads active/remind notes from database into private property
  171. *
  172. * @return void
  173. */
  174. protected function loadActiveNotes() {
  175. $cachedNotes = $this->cache->get(self::CACHE_KEY_NOTES, self::CACHE_TIMEOUT);
  176. if (empty($cachedNotes)) {
  177. $cachedNotes = array();
  178. }
  179. if (isset($cachedNotes[$this->myLogin])) {
  180. $this->activenotes = $cachedNotes[$this->myLogin];
  181. } else {
  182. $query = "SELECT * from `stickynotes` WHERE `owner`= '" . $this->myLogin . "' AND `active`='1' ORDER BY `id` ASC";
  183. $tmpArr = simple_queryall($query);
  184. //map id=>id
  185. if (!empty($tmpArr)) {
  186. foreach ($tmpArr as $io => $each) {
  187. $this->activenotes[$each['id']] = $each;
  188. }
  189. }
  190. $cachedNotes[$this->myLogin] = $this->activenotes;
  191. $this->cache->set(self::CACHE_KEY_NOTES, $cachedNotes, self::CACHE_TIMEOUT);
  192. }
  193. }
  194. /**
  195. * Loads all revelations from database
  196. *
  197. * @param bool $onlyForMe
  198. *
  199. * @return void
  200. */
  201. protected function loadRevelations($onlyForMe = false) {
  202. if ($onlyForMe) {
  203. $cachedRevelations = $this->cache->get(self::CACHE_KEY_REVELATIONS, self::CACHE_TIMEOUT);
  204. if (empty($cachedRevelations)) {
  205. $cachedRevelations = array();
  206. }
  207. if (isset($cachedRevelations[$this->myLogin])) {
  208. $this->allRevelations = $cachedRevelations[$this->myLogin];
  209. } else {
  210. //yeah, logins must be space-separated
  211. $query = "SELECT * from `stickyrevelations` WHERE `showto` LIKE '% " . $this->myLogin . " %' AND `active`='1' ORDER BY `id` ASC";
  212. $all = simple_queryall($query);
  213. if (!empty($all)) {
  214. foreach ($all as $io => $each) {
  215. $this->allRevelations[$each['id']] = $each;
  216. }
  217. }
  218. $cachedRevelations[$this->myLogin] = $this->allRevelations;
  219. $this->cache->set(self::CACHE_KEY_REVELATIONS, $cachedRevelations, self::CACHE_TIMEOUT);
  220. }
  221. } else {
  222. $query = "SELECT * from `stickyrevelations` ORDER BY `id` DESC";
  223. $all = simple_queryall($query);
  224. if (!empty($all)) {
  225. foreach ($all as $io => $each) {
  226. $this->allRevelations[$each['id']] = $each;
  227. }
  228. }
  229. }
  230. }
  231. /**
  232. * Returns note data extracted from private allnotes property
  233. *
  234. * @param int $noteId
  235. *
  236. * @return array
  237. */
  238. public function getNoteData($noteId) {
  239. $result = array();
  240. if (!empty($this->allnotes)) {
  241. if (isset($this->allnotes[$noteId])) {
  242. $result = $this->allnotes[$noteId];
  243. }
  244. }
  245. return ($result);
  246. }
  247. /**
  248. * Returns revelation data
  249. *
  250. * @param int $revelationId
  251. *
  252. * @return array
  253. */
  254. protected function getRevelationData($revelationId) {
  255. $result = array();
  256. if (!empty($this->allRevelations)) {
  257. if (isset($this->allRevelations[$revelationId])) {
  258. $result = $this->allRevelations[$revelationId];
  259. }
  260. }
  261. return ($result);
  262. }
  263. /**
  264. * Returns cutted string if needed
  265. *
  266. * @param string $string
  267. * @param int $size
  268. *
  269. * @return string
  270. */
  271. protected function cutString($string, $size) {
  272. if ((mb_strlen($string, 'UTF-8') > $size)) {
  273. $string = mb_substr($string, 0, $size, 'utf-8') . '...';
  274. }
  275. return ($string);
  276. }
  277. /**
  278. * Returns list of available sticky notes with it controls as grid
  279. *
  280. * @return string
  281. */
  282. public function renderListGrid() {
  283. $messages = new UbillingMessageHelper();
  284. $result = '';
  285. $cells = wf_TableCell(__('Creation date'));
  286. $cells .= wf_TableCell(__('Remind date'));
  287. $cells .= wf_TableCell(__('Time'));
  288. $cells .= wf_TableCell(__('Status'));
  289. $cells .= wf_TableCell(__('Text'));
  290. $cells .= wf_TableCell(__('Actions'));
  291. $rows = wf_TableRow($cells, 'row1');
  292. /**
  293. * Amadare wa chi no shizuku to natte hoho wo
  294. * Tsutaiochiru
  295. * Mou doko ni mo kaeru basho ga nai nara
  296. */
  297. if (!empty($this->allnotes)) {
  298. foreach ($this->allnotes as $io => $each) {
  299. $cells = wf_TableCell($each['createdate']);
  300. $cells .= wf_TableCell($each['reminddate']);
  301. $cells .= wf_TableCell($each['remindtime']);
  302. $cells .= wf_TableCell(web_bool_led($each['active']), '', '', 'sorttable_customkey="' . $each['active'] . '"');
  303. $viewLink = wf_Link(self::URL_ME . '&shownote=' . $each['id'], $this->cutString(htmlentities($each['text'], ENT_COMPAT, "UTF-8"), 100), false, '');
  304. $cells .= wf_TableCell($viewLink);
  305. $actLinks = '';
  306. if ($this->notesPreview) {
  307. //deletion dialog
  308. $deletingPreview = nl2br($this->cutString(strip_tags($each['text']), 50));
  309. $deletingPreview .= wf_delimiter();
  310. $deletingPreview .= wf_JSAlert(self::URL_ME . '&delete=' . $each['id'], web_delete_icon() . ' ' . __('Delete'), $messages->getDeleteAlert(), '', 'ubButton') . ' ';
  311. $deletingPreview .= wf_Link(self::URL_ME, wf_img('skins/back.png') . ' ' . __('Cancel'), false, 'ubButton');
  312. $deletingDialog = wf_modalAuto(web_delete_icon(), __('Delete'), $deletingPreview);
  313. $actLinks .= $deletingDialog;
  314. }
  315. //edit control
  316. $actLinks .= wf_Link(self::URL_ME . '&editform=' . $each['id'], web_edit_icon(), false) . ' ';
  317. if ($this->notesPreview) {
  318. //preview dialog
  319. $previewContent = nl2br($this->makeFullNoteLink($this->cutString(strip_tags(htmlentities($each['text'], ENT_COMPAT, "UTF-8")), self::PREVIEW_LEN), $each['id']));
  320. $actLinks .= wf_modal(wf_img('skins/icon_search_small.gif', __('Preview')), __('Preview'), $previewContent, '', '640', '480');
  321. }
  322. $cells .= wf_TableCell($actLinks);
  323. $rows .= wf_TableRow($cells, 'row5');
  324. }
  325. }
  326. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  327. return ($result);
  328. }
  329. /**
  330. * Returns available sticky notes list as full calendar
  331. *
  332. * @return string
  333. */
  334. public function renderListCalendar() {
  335. $calendarData = '';
  336. if (!empty($this->allnotes)) {
  337. foreach ($this->allnotes as $io => $each) {
  338. $timestamp = strtotime($each['createdate']);
  339. $date = date("Y, n-1, j", $timestamp);
  340. $remindTime = '';
  341. if ($each['active'] == 1) {
  342. $coloring = "className : 'undone',";
  343. } else {
  344. $coloring = '';
  345. }
  346. if (!empty($each['reminddate'])) {
  347. $remindtimestamp = strtotime($each['reminddate']);
  348. $reminddate = date("Y, n-1, j", $remindtimestamp);
  349. $remindTime = (!empty($each['remindtime'])) ? date("H:i", strtotime($each['remindtime'])) : '';
  350. $textLenght = 48;
  351. } else {
  352. $reminddate = $date;
  353. $textLenght = 24;
  354. }
  355. $shortText = $each['text'];
  356. $shortText = str_replace("\n", '', $shortText);
  357. $shortText = str_replace("\r", '', $shortText);
  358. $shortText = str_replace("'", '`', $shortText);
  359. $shortText = strip_tags($shortText);
  360. $shortText = $this->cutString($shortText, $textLenght);
  361. $calendarData .= "
  362. {
  363. title: '" . $remindTime . " " . $shortText . " ',
  364. url: '" . self::URL_ME . "&backurl=calendar&shownote=" . $each['id'] . "',
  365. start: new Date(" . $reminddate . "),
  366. end: new Date(" . $reminddate . "),
  367. " . $coloring . "
  368. },
  369. ";
  370. }
  371. }
  372. $result = wf_FullCalendar($calendarData);
  373. return ($result);
  374. }
  375. /**
  376. * Renders existing revelations list with some controls
  377. *
  378. * @return string
  379. */
  380. public function renderRevelationsList() {
  381. $result = '';
  382. $messages = new UbillingMessageHelper();
  383. $daysOfWeek = daysOfWeek(true);
  384. if (!empty($this->allRevelations)) {
  385. $cells = wf_TableCell(__('Creation date'));
  386. $cells .= wf_TableCell(__('Day') . ' ' . __('From'));
  387. $cells .= wf_TableCell(__('Day') . ' ' . __('To'));
  388. $cells .= wf_TableCell(__('Day of week'));
  389. $cells .= wf_TableCell(__('Status'));
  390. $cells .= wf_TableCell(__('Text'));
  391. $cells .= wf_TableCell(__('Actions'));
  392. $rows = wf_TableRow($cells, 'row1');
  393. foreach ($this->allRevelations as $io => $each) {
  394. $cells = wf_TableCell($each['createdate']);
  395. $dayFrom = (!empty($each['dayfrom'])) ? $each['dayfrom'] : '';
  396. $dayTo = (!empty($each['dayto'])) ? $each['dayto'] : '';
  397. $dayWeek = (!empty($each['dayweek'])) ? $each['dayweek'] : 1488; //any day of week
  398. $cells .= wf_TableCell($dayFrom);
  399. $cells .= wf_TableCell($dayTo);
  400. $cells .= wf_TableCell($daysOfWeek[$dayWeek]);
  401. $cells .= wf_TableCell(web_bool_led($each['active']));
  402. $previewContent = nl2br($this->cutString(strip_tags($each['text']), self::PREVIEW_LEN));
  403. $cells .= wf_TableCell($previewContent);
  404. $actiLinks = wf_JSAlert(self::URL_REVELATIONS . '&deleterev=' . $each['id'], web_delete_icon(), $messages->getDeleteAlert()) . ' ';
  405. $actiLinks .= wf_Link(self::URL_REVELATIONS . '&editrev=' . $each['id'], web_edit_icon());
  406. $cells .= wf_TableCell($actiLinks);
  407. $rows .= wf_TableRow($cells, 'row5');
  408. }
  409. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  410. } else {
  411. $result .= $messages->getStyledMessage(__('Nothing to show'), 'info');
  412. }
  413. return ($result);
  414. }
  415. /**
  416. * Creates new note in database
  417. *
  418. * @param string $remindDate
  419. * @param int $activity
  420. * @param string $text
  421. */
  422. protected function createNote($remindDate, $remindTime, $activity, $text) {
  423. $createDate = curdatetime();
  424. $owner = mysql_real_escape_string($this->myLogin);
  425. $text = strip_tags($text);
  426. $text = mysql_real_escape_string($text);
  427. $activity = vf($activity, 3);
  428. $createDate = mysql_real_escape_string($createDate);
  429. if (!empty($remindDate)) {
  430. $remindDate = "'" . mysql_real_escape_string($remindDate) . "'";
  431. } else {
  432. $remindDate = 'NULL';
  433. }
  434. if (!empty($remindTime)) {
  435. $remindTime = "'" . mysql_real_escape_string($remindTime) . "'";
  436. } else {
  437. $remindTime = 'NULL';
  438. }
  439. $query = "INSERT INTO `stickynotes` (`id`, `owner`, `createdate`, `reminddate`,`remindtime`, `active`, `text`) "
  440. . "VALUES (NULL, '" . $owner . "', '" . $createDate . "', " . $remindDate . ", " . $remindTime . " , '" . $activity . "', '" . $text . "');";
  441. nr_query($query);
  442. $this->flushCache();
  443. }
  444. /**
  445. * Creates new sticky revelation in database
  446. *
  447. * @param int $dayFrom
  448. * @param int $dayTo
  449. * @param int $activity
  450. * @param string $text
  451. * @param string $showTo
  452. * @param int $dayWeek
  453. *
  454. * @return void
  455. */
  456. protected function createRevelation($dayFrom, $dayTo, $activity, $text, $showTo, $dayWeek) {
  457. $owner = $this->myLogin;
  458. $createDate = curdatetime();
  459. $text = strip_tags($text);
  460. $text = mysql_real_escape_string($text);
  461. $activity = vf($activity, 3);
  462. if (!empty($dayFrom)) {
  463. $dayFrom = "'" . mysql_real_escape_string($dayFrom) . "'";
  464. } else {
  465. $dayFrom = 'NULL';
  466. }
  467. if (!empty($dayTo)) {
  468. $dayTo = "'" . mysql_real_escape_string($dayTo) . "'";
  469. } else {
  470. $dayTo = 'NULL';
  471. }
  472. if ($dayWeek == 1488) {
  473. $dayWeek = 0; //replace any day of week to zero
  474. }
  475. $query = "INSERT INTO `stickyrevelations` (`id`, `owner`, `showto`,`createdate`, `dayfrom`,`dayto`,`dayweek` ,`active`, `text`) "
  476. . "VALUES (NULL, '" . $owner . "', '" . $showTo . "' ,'" . $createDate . "', " . $dayFrom . ", " . $dayTo . " , '" . $dayWeek . "', '" . $activity . "', '" . $text . "');";
  477. nr_query($query);
  478. $this->flushCache();
  479. }
  480. /**
  481. * Renders notify container with some text inside
  482. *
  483. * @param string $text
  484. * @param int $offsetLeft
  485. * @param bool $isRevelation
  486. *
  487. * @return string
  488. */
  489. protected function renderStickyNote($text, $offsetLeft = 0, $isRevelation = false) {
  490. $result = '';
  491. if (!empty($text)) {
  492. if ($offsetLeft) {
  493. $offsetLeft = 35 + $offsetLeft . 'px';
  494. $dividedLeftOffset = vf($offsetLeft, 3) / 5;
  495. $roundedLeftOffset = round($dividedLeftOffset);
  496. $offsetTop = 25 + $roundedLeftOffset . 'px';
  497. } else {
  498. $offsetLeft = '35px';
  499. $offsetTop = '30px';
  500. }
  501. $result .= wf_tag('div', false, 'stickynote', 'style="margin:' . $offsetTop . ' ' . $offsetLeft . ' 20px 20px;"');
  502. if ($isRevelation) {
  503. $result .= wf_img('skins/pigeon_icon.png') . wf_tag('br');
  504. } else {
  505. $result .= wf_Link(self::URL_ME, wf_img('skins/pushpin.png'), false, '') . wf_tag('br');
  506. }
  507. $result .= wf_tag('div', false, 'stickynotetext');
  508. $result .= $text;
  509. $result .= wf_tag('div', true);
  510. $result .= wf_tag('div', true);
  511. }
  512. return ($result);
  513. }
  514. /**
  515. * Returns control panel
  516. *
  517. * @return string
  518. */
  519. public function panel() {
  520. $result = '';
  521. if (!wf_CheckGet(array('revelations'))) {
  522. $result .= wf_modalAuto(wf_img('skins/pushpin.png') . ' ' . __('Create new personal note'), __('Create new personal note'), $this->createForm(), 'ubButton');
  523. if (wf_CheckGet(array('calendarview'))) {
  524. $result .= wf_Link(self::URL_ME, wf_img('skins/icon_table.png') . ' ' . __('Grid view'), false, 'ubButton');
  525. } else {
  526. $result .= wf_Link(self::URL_ME . '&calendarview=true', wf_img('skins/icon_calendar.gif') . ' ' . __('As calendar'), false, 'ubButton');
  527. }
  528. if ($this->revelationsFlag) {
  529. if (cfr('REVELATIONS')) {
  530. $result .= wf_link(self::URL_REVELATIONS, wf_img('skins/pigeon_icon.png') . ' ' . __('Revelations'), false, 'ubButton');
  531. }
  532. }
  533. } else {
  534. if (!wf_CheckGet(array('editrev'))) {
  535. if (ubRouting::get('backurl') == 'calendar') {
  536. $result .= wf_BackLink(self::URL_ME . '&calendarview=true');
  537. } else {
  538. $result .= wf_BackLink(self::URL_ME);
  539. }
  540. } else {
  541. $result .= wf_BackLink(self::URL_REVELATIONS);
  542. }
  543. if (cfr('REVELATIONS')) {
  544. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Create'), __('Create'), $this->revelationCreateForm(), 'ubButton');
  545. }
  546. }
  547. return ($result);
  548. }
  549. /**
  550. * Returns note create form
  551. *
  552. * @return string
  553. */
  554. protected function createForm() {
  555. $inputs = wf_tag('label') . __('Text') . ': ' . wf_tag('br') . wf_tag('label', true);
  556. $inputs .= wf_TextArea('newtext', '', '', true, '50x15');
  557. $inputs .= wf_CheckInput('newactive', __('Create note as active'), true, true);
  558. $inputs .= wf_DatePickerPreset('newreminddate', '');
  559. $inputs .= wf_tag('label') . __('Remind only after this date') . wf_tag('label', true);
  560. $inputs .= wf_tag('br');
  561. $inputs .= wf_TimePickerPreset('newremindtime', '', __('Remind time'), false);
  562. $inputs .= wf_tag('br');
  563. $inputs .= wf_tag('br');
  564. $inputs .= wf_Submit(__('Create'));
  565. $result = wf_Form('', 'POST', $inputs, 'glamour');
  566. return ($result);
  567. }
  568. /**
  569. * Returns revelation creation form
  570. *
  571. * @return string
  572. */
  573. protected function revelationCreateForm() {
  574. $daysWeek = daysOfWeek(true);
  575. $days = array('' => '-');
  576. for ($i = 1; $i <= 31; $i++) {
  577. $days[$i] = $i;
  578. }
  579. $alladmins = rcms_scandir(USERS_PATH);
  580. $adminNames = ts_GetAllEmployeeLoginsCached();
  581. $adminNames = unserialize($adminNames);
  582. $inputs = wf_tag('label') . __('Text') . ': ' . wf_tag('br') . wf_tag('label', true);
  583. $inputs .= wf_TextArea('newrevelationtext', '', '', true, '50x15');
  584. $inputs .= wf_CheckInput('newrevelationactive', __('Active'), true, true);
  585. $inputs .= wf_tag('label') . __('Remind only between this days of month') . ' ' . wf_tag('label', true) . ' ';
  586. $inputs .= wf_Selector('newrevelationdayfrom', $days, __('From'), '', false) . ' ';
  587. $inputs .= wf_Selector('newrevelationdayto', $days, __('To'), '', true) . ' ';
  588. $inputs .= wf_Selector('newrevelationdayweek', $daysWeek, __('Day of week'), '', true);
  589. $inputs .= wf_tag('br');
  590. if (!empty($alladmins)) {
  591. foreach ($alladmins as $io => $eachAdmin) {
  592. $eachAdminName = (isset($adminNames[$eachAdmin])) ? $adminNames[$eachAdmin] : $eachAdmin;
  593. $inputs .= wf_CheckInput('newrevelationshowto[' . $eachAdmin . ']', $eachAdminName, false, false) . ' ' . wf_tag('br');
  594. }
  595. }
  596. $inputs .= wf_tag('br');
  597. $inputs .= wf_Submit(__('Create'));
  598. $result = wf_Form('', 'POST', $inputs, 'glamour');
  599. return ($result);
  600. }
  601. /**
  602. * Returns revelation editing form
  603. *
  604. * @return string
  605. */
  606. public function revelationEditForm($id) {
  607. $id = vf($id, 3);
  608. $result = '';
  609. $messages = new UbillingMessageHelper();
  610. if (isset($this->allRevelations[$id])) {
  611. $daysWeek = daysOfWeek(true);
  612. $days = array('' => '-');
  613. for ($i = 1; $i <= 31; $i++) {
  614. $days[$i] = $i;
  615. }
  616. $revData = $this->allRevelations[$id];
  617. $revDayWeek = (!empty($revData['dayweek'])) ? $revData['dayweek'] : 1488; //zero value in database
  618. $alladmins = rcms_scandir(USERS_PATH);
  619. $adminNames = ts_GetAllEmployeeLoginsCached();
  620. $adminNames = unserialize($adminNames);
  621. $inputs = wf_tag('label') . __('Text') . ': ' . wf_tag('br') . wf_tag('label', true);
  622. $inputs .= wf_HiddenInput('editrevelationid', $id);
  623. $inputs .= wf_TextArea('editrevelationtext', '', $revData['text'], true, '50x15');
  624. $inputs .= wf_CheckInput('editrevelationactive', __('Active'), true, $revData['active']);
  625. $inputs .= wf_tag('label') . __('Remind only between this days of month') . ' ' . wf_tag('label', true) . ' ';
  626. $inputs .= wf_Selector('editrevelationdayfrom', $days, __('From'), $revData['dayfrom'], false) . ' ';
  627. $inputs .= wf_Selector('editrevelationdayto', $days, __('To'), $revData['dayto'], true) . ' ';
  628. $inputs .= wf_Selector('editrevelationdayweek', $daysWeek, __('Day of week'), $revData['dayweek'], true);
  629. $inputs .= wf_tag('br');
  630. if (!empty($alladmins)) {
  631. foreach ($alladmins as $io => $eachAdmin) {
  632. $stateFlag = (ispos($revData['showto'], ' ' . $eachAdmin . ' ')) ? true : false;
  633. $eachAdminName = (isset($adminNames[$eachAdmin])) ? $adminNames[$eachAdmin] : $eachAdmin;
  634. $inputs .= wf_CheckInput('editrevelationshowto[' . $eachAdmin . ']', $eachAdminName, false, $stateFlag) . ' ';
  635. }
  636. }
  637. $inputs .= wf_tag('br');
  638. $inputs .= wf_tag('br');
  639. $inputs .= wf_Submit(__('Save'));
  640. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  641. } else {
  642. $result .= $messages->getStyledMessage(__('Something went wrong') . ': EX_ID_NOT_EXISTS', 'error');
  643. }
  644. return ($result);
  645. }
  646. /**
  647. * Saves changes of revelation in database
  648. *
  649. * @return void
  650. */
  651. public function saveMyRevelation() {
  652. if (wf_CheckPost(array('editrevelationtext', 'editrevelationid'))) {
  653. $revelationId = vf($_POST['editrevelationid'], 3);
  654. $revelationData = $this->getRevelationData($revelationId);
  655. if (!empty($revelationData)) {
  656. $where = "WHERE `id`='" . $revelationId . "';";
  657. //text
  658. $text = strip_tags($_POST['editrevelationtext']);
  659. $oldText = $revelationData['text'];
  660. if ($text != $oldText) {
  661. simple_update_field('stickyrevelations', 'text', $text, $where);
  662. log_register("REVELATION CHANGED TEXT [" . $revelationId . "]");
  663. }
  664. //days intervals
  665. $dayFrom = $_POST['editrevelationdayfrom'];
  666. $oldDayFrom = $revelationData['dayfrom'];
  667. if ($dayFrom != $oldDayFrom) {
  668. simple_update_field('stickyrevelations', 'dayfrom', $dayFrom, $where);
  669. log_register("REVELATION CHANGED DAYFROM [" . $revelationId . "] ON " . $dayFrom . "");
  670. }
  671. $dayTo = $_POST['editrevelationdayto'];
  672. $oldDayTo = $revelationData['dayto'];
  673. if ($dayTo != $oldDayTo) {
  674. simple_update_field('stickyrevelations', 'dayto', $dayTo, $where);
  675. log_register("REVELATION CHANGED DAYTO [" . $revelationId . "] ON " . $dayTo . "");
  676. }
  677. $dayWeek = ubRouting::post('editrevelationdayweek', 'int');
  678. if ($dayWeek == 1488) {
  679. $dayWeek = 0; //any day of week
  680. }
  681. $oldDayWeek = $revelationData['dayweek'];
  682. if ($dayWeek != $oldDayWeek) {
  683. simple_update_field('stickyrevelations', 'dayweek', $dayWeek, $where);
  684. log_register("REVELATION CHANGED DAYWEEK [" . $revelationId . "] ON " . $dayWeek . "");
  685. }
  686. //activity flag
  687. $activity = (isset($_POST['editrevelationactive'])) ? 1 : 0;
  688. $oldActivity = $revelationData['active'];
  689. if ($activity != $oldActivity) {
  690. simple_update_field('stickyrevelations', 'active', $activity, $where);
  691. log_register("REVELATION CHANGED ACTIVE [" . $revelationId . "] ON " . $activity . "");
  692. }
  693. //admins selection
  694. $showTo = '';
  695. $oldsShowTo = $revelationData['showto'];
  696. if (!empty($_POST['editrevelationshowto'])) {
  697. foreach ($_POST['editrevelationshowto'] as $io => $each) {
  698. $showTo .= ' ' . $io . ' ';
  699. }
  700. }
  701. if ($showTo != $oldsShowTo) {
  702. simple_update_field('stickyrevelations', 'showto', $showTo, $where);
  703. log_register("REVELATION CHANGED SHOWTO [" . $revelationId . "]");
  704. }
  705. $this->flushCache();
  706. }
  707. }
  708. }
  709. /**
  710. * Creates new revelation in database
  711. *
  712. * @return void
  713. */
  714. public function addMyRevelation() {
  715. if (wf_CheckPost(array('newrevelationtext'))) {
  716. $dayFrom = (!empty($_POST['newrevelationdayfrom'])) ? $_POST['newrevelationdayfrom'] : '';
  717. $dayTo = (!empty($_POST['newrevelationdayto'])) ? $_POST['newrevelationdayto'] : '';
  718. $dayWeek = ubRouting::post('newrevelationdayweek', 'int');
  719. if ($dayWeek == 1488) {
  720. $dayWeek = 0; //any day of week
  721. }
  722. $showTo = '';
  723. if (!empty($_POST['newrevelationshowto'])) {
  724. foreach ($_POST['newrevelationshowto'] as $io => $each) {
  725. $showTo .= ' ' . $io . ' ';
  726. }
  727. }
  728. $activity = (isset($_POST['newrevelationactive'])) ? 1 : 0;
  729. $text = $_POST['newrevelationtext'];
  730. $this->createRevelation($dayFrom, $dayTo, $activity, $text, $showTo, $dayWeek);
  731. $newId = simple_get_lastid('stickyrevelations');
  732. log_register("REVELATION CREATE [" . $newId . "]");
  733. }
  734. }
  735. /**
  736. * Returns edit form for some sticky note
  737. *
  738. * @param int $noteId
  739. * @param bool $wideForm
  740. *
  741. * @return string
  742. */
  743. public function editForm($noteId, $wideForm = false) {
  744. $noteData = $this->getNoteData($noteId);
  745. if (!empty($noteData)) {
  746. $noteText = htmlentities($noteData['text'], ENT_COMPAT, "UTF-8");
  747. $textAreaDimensions = ($wideForm) ? '80x25' : '50x15';
  748. $inputs = wf_HiddenInput('editnoteid', $noteId);
  749. $inputs .= wf_tag('label') . __('Text') . ': ' . wf_tag('br') . wf_tag('label', true);
  750. $inputs .= wf_TextArea('edittext', '', $noteText, true, $textAreaDimensions);
  751. $checkState = ($noteData['active'] == 1) ? true : false;
  752. $inputs .= wf_CheckInput('editactive', __('Personal note active'), true, $checkState);
  753. $inputs .= wf_DatePickerPreset('editreminddate', $noteData['reminddate']);
  754. $inputs .= wf_tag('label') . __('Remind only after this date') . wf_tag('label', true);
  755. $inputs .= wf_tag('br');
  756. $inputs .= wf_TimePickerPreset('editremindtime', $noteData['remindtime'], __('Remind time'), true);
  757. $inputs .= wf_tag('br');
  758. $inputs .= wf_tag('br');
  759. $inputs .= wf_Submit(__('Save'));
  760. $result = wf_Form('', 'POST', $inputs, 'glamour');
  761. } else {
  762. $result = __('Strange exeption');
  763. }
  764. return ($result);
  765. }
  766. /**
  767. * Returns sticky note deletion dialog to render below of editing form
  768. *
  769. * @param int $noteId
  770. *
  771. * @return string
  772. */
  773. public function getEditFormDeleteControls($noteId) {
  774. $result = '';
  775. if (!$this->notesPreview) {
  776. $messages = new UbillingMessageHelper();
  777. $noteData = $this->getNoteData($noteId);
  778. if (!empty($noteData)) {
  779. $deletingPreview = nl2br($this->cutString(strip_tags($noteData['text']), 50));
  780. $deletingPreview .= wf_delimiter();
  781. $deletingPreview .= wf_JSAlert(self::URL_ME . '&delete=' . $noteData['id'], web_delete_icon() . ' ' . __('Delete'), $messages->getDeleteAlert(), '', 'ubButton') . ' ';
  782. $deletingPreview .= wf_Link(self::URL_ME . '&editform=' . $noteId, wf_img('skins/back.png') . ' ' . __('Cancel'), false, 'ubButton');
  783. $deletingDialog = wf_modalAuto(web_delete_icon() . ' ' . __('Delete'), __('Delete'), $deletingPreview, 'ubButton');
  784. $result .= $deletingDialog;
  785. }
  786. }
  787. return($result);
  788. }
  789. /**
  790. * Creates new personal note in database
  791. *
  792. * @return void
  793. */
  794. public function addMyNote() {
  795. if (wf_CheckPost(array('newtext'))) {
  796. $remindDate = (!empty($_POST['newreminddate'])) ? $_POST['newreminddate'] : '';
  797. $remindTime = (!empty($_POST['newremindtime'])) ? $_POST['newremindtime'] : '';
  798. $activity = (isset($_POST['newactive'])) ? 1 : 0;
  799. $text = $_POST['newtext'];
  800. $this->createNote($remindDate, $remindTime, $activity, $text);
  801. $newId = simple_get_lastid('stickynotes');
  802. log_register("STICKY CREATE [" . $newId . "]");
  803. }
  804. }
  805. /**
  806. * Saves changes of note in database
  807. *
  808. * @return void
  809. */
  810. public function saveMyNote() {
  811. if (wf_CheckPost(array('edittext', 'editnoteid'))) {
  812. $noteId = vf($_POST['editnoteid'], 3);
  813. $noteData = $this->getNoteData($noteId);
  814. if (!empty($noteData)) {
  815. $where = "WHERE `id`='" . $noteId . "';";
  816. //text
  817. $text = strip_tags($_POST['edittext']);
  818. $oldText = $noteData['text'];
  819. if ($text != $oldText) {
  820. simple_update_field('stickynotes', 'text', $text, $where);
  821. log_register("STICKY CHANGED TEXT [" . $noteId . "]");
  822. $this->flushCache();
  823. }
  824. //remind date
  825. $remindDate = $_POST['editreminddate'];
  826. $oldRemindDate = $noteData['reminddate'];
  827. if ($remindDate != $oldRemindDate) {
  828. if (!empty($remindDate)) {
  829. $remindDate = "'" . mysql_real_escape_string($remindDate) . "'";
  830. } else {
  831. $remindDate = 'NULL';
  832. }
  833. $query = "UPDATE `stickynotes` SET `reminddate` = " . $remindDate . " " . $where; // ugly hack, i know
  834. nr_query($query);
  835. log_register("STICKY CHANGED REMINDDATE [" . $noteId . "] ON " . $remindDate . "");
  836. $this->flushCache();
  837. }
  838. //remind time
  839. $remindTime = $_POST['editremindtime'];
  840. $oldRemindTime = $noteData['remindtime'];
  841. if (ispos($oldRemindTime, ':')) {
  842. $oldRemindTime = explode(':', $oldRemindTime);
  843. $oldRemindTime = $oldRemindTime[0] . ':' . $oldRemindTime[1];
  844. }
  845. if ($remindTime != $oldRemindTime) {
  846. if (!empty($remindTime)) {
  847. $remindTime = "'" . mysql_real_escape_string($remindTime) . "'";
  848. } else {
  849. $remindTime = 'NULL';
  850. }
  851. $query = "UPDATE `stickynotes` SET `remindtime` = " . $remindTime . " " . $where;
  852. nr_query($query);
  853. log_register("STICKY CHANGED REMINDTIME [" . $noteId . "] ON " . $remindTime . "");
  854. $this->flushCache();
  855. }
  856. //activity flag
  857. $activity = (isset($_POST['editactive'])) ? 1 : 0;
  858. $oldActivity = $noteData['active'];
  859. if ($activity != $oldActivity) {
  860. simple_update_field('stickynotes', 'active', $activity, $where);
  861. log_register("STICKY CHANGED ACTIVE [" . $noteId . "] ON " . $activity . "");
  862. $this->flushCache();
  863. }
  864. }
  865. }
  866. }
  867. /**
  868. * Deletes some note by its ID
  869. *
  870. * @param int $id
  871. *
  872. * @return void
  873. */
  874. public function deleteNote($id) {
  875. $id = vf($id, 3);
  876. if (!empty($id)) {
  877. $query = "DELETE FROM `stickynotes` WHERE `id`='" . $id . "';";
  878. nr_query($query);
  879. log_register("STICKY DELETE [" . $id . "]");
  880. $this->flushCache();
  881. }
  882. }
  883. /**
  884. * Deletes some revelation by its ID
  885. *
  886. * @param int $id
  887. *
  888. * @return void
  889. */
  890. public function deleteRevelation($id) {
  891. $id = vf($id, 3);
  892. if (!empty($id)) {
  893. $query = "DELETE FROM `stickyrevelations` WHERE `id`='" . $id . "';";
  894. nr_query($query);
  895. log_register("REVELATION DELETE [" . $id . "]");
  896. $this->flushCache();
  897. }
  898. }
  899. /**
  900. * Returns full text of sticky note with check of owner
  901. *
  902. * @param int $noteId
  903. *
  904. * @return string
  905. */
  906. public function renderNote($noteId) {
  907. $noteId = vf($noteId, 3);
  908. $result = '';
  909. $noteData = $this->getNoteData($noteId);
  910. if (!empty($noteData)) {
  911. $messages = new UbillingMessageHelper();
  912. if ($noteData['owner'] == $this->myLogin) {
  913. $result = htmlentities($noteData['text'], ENT_COMPAT, "UTF-8");
  914. $result = strip_tags($result);
  915. $result = nl2br($result);
  916. $result .= wf_delimiter(2);
  917. $backUrl = '';
  918. $customLink = '';
  919. if (ubRouting::get('backurl') == 'calendar') {
  920. $backUrl .= '&calendarview=true';
  921. $customLink .= '&backurl=calendar';
  922. }
  923. $result .= wf_BackLink(self::URL_ME . $backUrl);
  924. $result .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit'), __('Edit'), $this->editForm($noteId), 'ubButton') . ' ';
  925. $deletingPreview = nl2br($this->cutString(strip_tags($noteData['text']), 50));
  926. $deletingPreview .= wf_delimiter();
  927. $deletingPreview .= wf_JSAlert(self::URL_ME . $customLink . '&delete=' . $noteData['id'], web_delete_icon() . ' ' . __('Delete'), $messages->getDeleteAlert(), '', 'ubButton') . ' ';
  928. $deletingPreview .= wf_Link(self::URL_ME . $customLink . '&shownote=' . $noteData['id'], wf_img('skins/back.png') . ' ' . __('Cancel'), false, 'ubButton');
  929. $deletingDialog = wf_modalAuto(web_delete_icon() . ' ' . __('Delete'), __('Delete'), $deletingPreview, 'ubButton');
  930. $result .= $deletingDialog;
  931. } else {
  932. $result = __('Access denied');
  933. }
  934. } else {
  935. $result = __('Strange exeption');
  936. }
  937. return ($result);
  938. }
  939. /**
  940. * Returns note text with link to full note view if required
  941. *
  942. * @param int $text
  943. * @param int $noteId
  944. *
  945. * @return string
  946. */
  947. protected function makeFullNoteLink($text, $noteId) {
  948. $ending = substr($text, -3);
  949. $result = $text;
  950. if ($ending == '...') {
  951. $noteId = vf($noteId, 3);
  952. if (!empty($noteId)) {
  953. $noteViewLink = wf_link(self::URL_ME . '&shownote=' . $noteId, '...', false, '', 'title="' . __('View full note') . '"');
  954. $result = substr_replace($text, $noteViewLink, -3, 3);
  955. }
  956. }
  957. return ($result);
  958. }
  959. /**
  960. * Returns available taskbar notifications as floating widget
  961. *
  962. * @return string
  963. */
  964. public function renderTaskbarNotify() {
  965. $result = '';
  966. $output = '';
  967. $delimiterId = '{' . zb_rand_string(8) . '}';
  968. $delimiterCode = '';
  969. $offsetLeft = 0;
  970. if (!empty($this->activenotes)) {
  971. foreach ($this->activenotes as $io => $each) {
  972. $noteDate = $each['reminddate'];
  973. $noteTime = $each['remindtime'];
  974. if (!empty($noteTime)) {
  975. if (!empty($noteDate)) {
  976. $noteDate = $noteDate . ' ' . $noteTime;
  977. } else {
  978. $noteDate = curdate() . ' ' . $noteTime;
  979. }
  980. }
  981. if (empty($noteDate) OR ( strtotime($noteDate) <= time())) {
  982. $tmpText = $each['text'];
  983. $tmpText = htmlentities($tmpText, ENT_COMPAT, "UTF-8");
  984. $tmpText = strip_tags($tmpText);
  985. $output = $tmpText;
  986. $output .= $delimiterId;
  987. $output = str_replace($delimiterId, $delimiterCode, $output);
  988. $output = $this->cutString($output, self::PREVIEW_LEN);
  989. $output = $this->makeFullNoteLink($output, $each['id']);
  990. $output = nl2br($output);
  991. $result .= $this->renderStickyNote($output, $offsetLeft);
  992. $offsetLeft = $offsetLeft + 10;
  993. }
  994. }
  995. }
  996. if (!empty($this->allRevelations)) {
  997. foreach ($this->allRevelations as $io => $each) {
  998. $needToShow = false;
  999. $curDay = date("j");
  1000. $curDayOfWeek = date("w");
  1001. if ($curDayOfWeek == 0) {
  1002. $curDayOfWeek = 7; //thats is sunday!
  1003. }
  1004. if (!empty($each['dayfrom']) AND (!empty($each['dayto']))) {
  1005. if (($curDay >= $each['dayfrom']) AND ( $curDay <= $each['dayto'])) {
  1006. $needToShow = true;
  1007. }
  1008. }
  1009. if (!empty($each['dayfrom']) AND ( empty($each['dayto']))) {
  1010. if ($curDay >= $each['dayfrom']) {
  1011. $needToShow = true;
  1012. }
  1013. }
  1014. if (empty($each['dayfrom']) AND (!empty($each['dayto']))) {
  1015. if ($curDay <= $each['dayto']) {
  1016. $needToShow = true;
  1017. }
  1018. }
  1019. if (empty($each['dayfrom']) AND ( empty($each['dayto']))) {
  1020. $needToShow = true;
  1021. }
  1022. if (!empty($each['dayweek'])) {
  1023. if (empty($each['dayfrom']) AND empty($each['dayto'])) {
  1024. //just check for day of week on empty dates
  1025. if ($curDayOfWeek == $each['dayweek']) {
  1026. $needToShow = true;
  1027. } else {
  1028. $needToShow = false;
  1029. }
  1030. } else {
  1031. //or guess is day-time interval valid
  1032. if ($needToShow) {
  1033. if ($curDayOfWeek == $each['dayweek']) {
  1034. $needToShow = true;
  1035. } else {
  1036. $needToShow = false; //now is wrong day :(
  1037. }
  1038. }
  1039. }
  1040. }
  1041. if ($needToShow) {
  1042. $tmpText = $each['text'];
  1043. $tmpText = strip_tags($tmpText);
  1044. $output = $tmpText;
  1045. $output .= $delimiterId;
  1046. $output = str_replace($delimiterId, $delimiterCode, $output);
  1047. $output = $this->cutString($output, self::PREVIEW_LEN);
  1048. $output = nl2br($output);
  1049. $result .= $this->renderStickyNote($output, $offsetLeft, true);
  1050. $offsetLeft = $offsetLeft + 10;
  1051. }
  1052. }
  1053. }
  1054. if (!empty($result)) {
  1055. $result .= wf_tag('script');
  1056. $result .= '$( function() { $( ".stickynote" ).draggable({ scroll: false, cancel: ".stickynotetext" }); } );';
  1057. $result .= wf_tag('script', true);
  1058. }
  1059. return ($result);
  1060. }
  1061. }