PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/minical/minicallib.php

https://gitlab.com/ElvisAns/tiki
PHP | 226 lines | 188 code | 22 blank | 16 comment | 21 complexity | d5efaa0b593af13da20ff86f2115e3df 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. //this script may only be included - so its better to die if called directly.
  8. if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
  9. header("location: index.php");
  10. exit;
  11. }
  12. class MiniCalLib extends TikiLib
  13. {
  14. // Returns an array where each member of the array has:
  15. // start: unix timestamp of the interval start time
  16. // end : unix timestamp of the interval end time
  17. // events : array of events for the slot listing:
  18. // title, description and duration
  19. public function minical_events_by_slot($user, $start, $end, $interval)
  20. {
  21. // since interval is in hour convert it to seconds
  22. //$interval = $interval * 60 * 60;
  23. $slots = [];
  24. while ($start <= $end) {
  25. $aux = [];
  26. $aux['start'] = $start;
  27. $end_p = $start + $interval;
  28. $aux['end'] = $end_p;
  29. $query = "select * from `tiki_minical_events` where `user`=? and `start`>=? and `start`<? order by " . $this->convertSortMode("start_asc");
  30. //print($query);print("<br />");
  31. $result = $this->query($query, [$user,(int)$start,(int)$end_p]);
  32. $events = [];
  33. while ($res = $result->fetchRow()) {
  34. $res['end'] = $res['start'] + $res['duration'];
  35. $res2 = [];
  36. if ($res['topicId']) {
  37. $query2 = "select `topicId`,`isIcon`,`path`,`name` from `tiki_minical_topics` where `topicId`=?";
  38. $result = $this->query($query2, [(int)$res['topicId']]);
  39. $res2 = $result->fetchRow();
  40. }
  41. $res['topic'] = $res2;
  42. $events[] = $res;
  43. }
  44. $aux['events'] = $events;
  45. $slots[] = $aux;
  46. $start += $interval;
  47. }
  48. return $slots;
  49. }
  50. public function minical_upload_topic($user, $topicname, $name, $type, $size, $data, $path)
  51. {
  52. if (strlen($data) == 0) {
  53. $isIcon = 'y';
  54. } else {
  55. $isIcon = 'n';
  56. }
  57. $query = "insert into `tiki_minical_topics`(`user`,`name`,`filename`,`filetype`,`filesize`,`data`,`isIcon`,`path`) values(?,?,?,?,?,?,?,?)";
  58. $this->query($query, [$user,$topicname,$name,$type,(int)$size,$data,$isIcon,$path]);
  59. }
  60. public function minical_list_topics($user, $offset, $maxRecords, $sort_mode, $find)
  61. {
  62. $bindvars = [$user];
  63. if ($find) {
  64. $mid = " and (`name` like ? or `filename` like ?)";
  65. $bindvars[] = "%$find%";
  66. $bindvars[] = "%$find%";
  67. } else {
  68. $mid = "";
  69. }
  70. $query = "select `isIcon`,`path`,`name`,`topicId` from `tiki_minical_topics` where `user`=? $mid order by " . $this->convertSortMode($sort_mode);
  71. $query_cant = "select count(*) from `tiki_minical_topics` where `user`=? $mid";
  72. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  73. $cant = $this->getOne($query_cant, $bindvars);
  74. $ret = [];
  75. while ($res = $result->fetchRow()) {
  76. $ret[] = $res;
  77. }
  78. $retval = [];
  79. $retval["data"] = $ret;
  80. $retval["cant"] = $cant;
  81. return $retval;
  82. }
  83. public function minical_get_topic($user, $topicId)
  84. {
  85. $query = "select * from `tiki_minical_topics` where `user`=? and `topicId`=?";
  86. $result = $this->query($query, [$user,(int)$topicId]);
  87. $res = $result->fetchRow();
  88. return $res;
  89. }
  90. public function minical_list_events($user, $offset, $maxRecords, $sort_mode, $find)
  91. {
  92. $bindvars = [$user];
  93. if ($find) {
  94. $mid = " and (`title` like ? or `description` like ?)";
  95. $bindvars[] = "%$find%";
  96. $bindvars[] = "%$find%";
  97. } else {
  98. $mid = "";
  99. }
  100. $query = "select * from `tiki_minical_events` where `user`=? $mid order by " . $this->convertSortMode($sort_mode);
  101. $query_cant = "select count(*) from `tiki_minical_events` where `user`=? $mid";
  102. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  103. $cant = $this->getOne($query_cant, $bindvars);
  104. $ret = [];
  105. while ($res = $result->fetchRow()) {
  106. $res2 = [];
  107. if ($res['topicId']) {
  108. $query2 = "select `topicId`,`isIcon`,`path`,`name` from `tiki_minical_topics` where `topicId`=?";
  109. $result2 = $this->query($query2, [(int)$res['topicId']]);
  110. $res2 = $result2->fetchRow();
  111. }
  112. $res['topic'] = $res2;
  113. $ret[] = $res;
  114. }
  115. $retval = [];
  116. $retval["data"] = $ret;
  117. $retval["cant"] = $cant;
  118. return $retval;
  119. }
  120. public function minical_list_events_from_date($user, $offset, $maxRecords, $sort_mode, $find, $pdate)
  121. {
  122. $bindvars = [(int)$pdate,$user];
  123. if ($find) {
  124. $mid = " and (`title` like ? or `description` like ?)";
  125. $bindvars[] = "%$find%";
  126. $bindvars[] = "%$find%";
  127. } else {
  128. $mid = "";
  129. }
  130. $query = "select * from `tiki_minical_events` where `start`>? and `user`=? $mid order by " . $this->convertSortMode($sort_mode);
  131. $query_cant = "select count(*) from `tiki_minical_events` where `start`>? and `user`=? $mid";
  132. $result = $this->query($query, $bindvars, $maxRecords, $offset);
  133. $cant = $this->getOne($query_cant, $bindvars);
  134. $ret = [];
  135. while ($res = $result->fetchRow()) {
  136. $res2 = [];
  137. if ($res['topicId']) {
  138. $query2 = "select `topicId`,`isIcon`,`path`,`name` from `tiki_minical_topics` where `topicId`=?";
  139. $result2 = $this->query($query2, [(int)$res['topicId']]);
  140. $res2 = $result2->fetchRow();
  141. }
  142. $res['topic'] = $res2;
  143. $ret[] = $res;
  144. }
  145. $retval = [];
  146. $retval["data"] = $ret;
  147. $retval["cant"] = $cant;
  148. return $retval;
  149. }
  150. public function minical_get_event($user, $eventId)
  151. {
  152. $query = "select * from `tiki_minical_events` where `user`=? and `eventId`=?";
  153. $result = $this->query($query, [$user,(int)$eventId]);
  154. $res = $result->fetchRow();
  155. return $res;
  156. }
  157. public function minical_remove_topic($user, $topicId)
  158. {
  159. $query = "delete from `tiki_minical_topics` where `user`=? and `topicId`=?";
  160. $this->query($query, [$user,(int)$topicId]);
  161. }
  162. public function minical_event_reminded($user, $eventId)
  163. {
  164. $query = "update `tiki_minical_events` set `reminded`=? where `user`=? and `eventId`=?";
  165. $this->query($query, ["y",$user,(int)$eventId]);
  166. }
  167. public function minical_replace_event($user, $eventId, $title, $description, $start, $duration, $topicId)
  168. {
  169. if ($eventId) {
  170. $query = "update `tiki_minical_events` set `topicId`=?,`end`=?,`title`=?,`description`=?,`start`=?,`duration`=?,`reminded`=? where `user`=? and `eventId`=?";
  171. $this->query($query, [(int)$topicId,$start + $duration,$title,$description,(int)$start,(int)$duration,"n",$user,(int)$eventId]);
  172. return $eventId;
  173. } else {
  174. $query = "insert into `tiki_minical_events`(`user`,`title`,`description`,`start`,`duration`,`end`,`topicId`,`reminded`) values(?,?,?,?,?,?,?,?)";
  175. $this->query($query, [$user,$title,$description,(int)$start,(int)$duration,$start + $duration,(int)$topicId,"n"]);
  176. $Id = $this->getOne("select max(`eventId`) from `tiki_minical_events` where `user`=? and `start`=?", [$user,(int)$start]);
  177. return $Id;
  178. }
  179. }
  180. public function minical_remove_event($user, $eventId)
  181. {
  182. $query = "delete from `tiki_minical_events` where `user`=? and `eventId`=?";
  183. $this->query($query, [$user,(int)$eventId]);
  184. }
  185. public function minical_get_events_to_remind($user, $rem)
  186. {
  187. // Search for events that are not reminded and will start
  188. // in less than $rem
  189. $limit = $this->now + $rem;
  190. $query = "select * from `tiki_minical_events` where `user`=? and `reminded`<>? and `start`<=? and `start`>?";
  191. $result = $this->query($query, [$user,'y',(int)$limit,(int)$this->now]);
  192. $ret = [];
  193. while ($res = $result->fetchRow()) {
  194. $ret[] = $res;
  195. }
  196. return $ret;
  197. }
  198. public function minical_remove_old($user, $pdate)
  199. {
  200. $query = "delete from `tiki_minical_events` where `user`=? and `start`<?";
  201. $this->query($query, [$user,(int)$pdate]);
  202. }
  203. }
  204. $minicallib = new MiniCalLib();