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