1 /* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 inherit "/classes/Room";
19 #include <attributes.h>
21 //! A Calendar is a room with Date Objects in it.
22 class Calendar : public Room{
27 //#define CALENDAR_DEBUG 1
30 #define DEBUG_CALENDAR(s, args...) write("Calendar: "+s+"\n", args)
32 #define DEBUG_CALENDAR(s, args...)
38 object creator = query_attribute(CALENDAR_OWNER);
39 return "Calendar("+(objectp(creator) ?
40 creator->get_identifier() + "'s calendar" :
41 "Noones calendar")+")";
44 void create_links(object date, object creator, array groups)
46 object factory = get_factory(CLASS_LINK);
47 object link, calendar, ulink;
50 DEBUG_CALENDAR("Creating links for date %O", date);
51 foreach(creator->get_members(), object member) {
52 if ( member->get_object_class() & CLASS_USER )
53 calendar = member->query_attribute(USER_CALENDAR);
54 else if ( member->get_object_class() & CLASS_GROUP )
55 calendar = member->query_attribute(GROUP_CALENDAR);
57 DEBUG_CALENDAR("Checking link for %O (%O)", member->get_identifier(), member->get_object_id());
58 if ( !objectp(calendar) ) {
59 werror( "Calendar: invalid calendar of member %O\n", member );
62 if ( search( haves, calendar ) < 0 ) {
63 DEBUG_CALENDAR("Creating link for %O (%O)", member->get_identifier(), member->get_object_id());
64 link = factory->execute(([ "name": date->get_identifier(),
65 "attributes": date->query_attributes(),
66 "link_to": date, ]) );
67 if ( link->move(calendar) ) haves += ({ calendar });
71 foreach(groups, object grp) {
72 calendar = grp->query_attribute(GROUP_CALENDAR);
73 if ( search( haves, calendar ) < 0 ) {
74 DEBUG_CALENDAR("Creating link for group %O (%O)", grp->get_identifier(), grp->get_object_id());
75 link = factory->execute(([ "name": date->get_identifier(),
76 "attributes": date->query_attributes(),
77 "link_to": date, ]) );
78 if ( link->move(calendar) ) haves += ({ calendar });
80 foreach(grp->get_members(), object member) {
81 if ( member->get_object_class() & CLASS_USER )
82 calendar = member->query_attribute(USER_CALENDAR);
83 else if ( member->get_object_class() & CLASS_GROUP )
84 calendar = member->query_attribute(GROUP_CALENDAR);
86 if ( !objectp(calendar) ) {
87 werror( "Calendar: invalid calendar of member %O\n", member );
90 if ( search( haves, calendar ) < 0 ) {
91 DEBUG_CALENDAR("Creating sub link for group member %O (%O)", member->get_identifier(), member->get_object_id());
92 ulink = factory->execute(([ "name": date->get_identifier(),
93 "attributes": date->query_attributes(),
94 "link_to": link, ]) );
95 if ( ulink->move(calendar) ) haves += ({ calendar });
101 object add_entry_recursive(mapping entry_map, void|int link, void|array groups)
103 array groups_recursive = groups;
104 if ( arrayp(groups) ) {
105 foreach ( groups, object grp ) {
106 array subgroups = grp->get_sub_groups_recursive();
107 if ( arrayp(subgroups) ) {
108 foreach ( subgroups, object subgrp ) {
109 if ( objectp(subgrp) && search( groups_recursive, subgrp ) < 0 )
110 groups_recursive += ({ subgrp });
115 return add_entry( entry_map, link, groups_recursive );
119 add_entry(mapping entry_map,void|int link, void|array groups)
121 DEBUG_CALENDAR("add_entry(%O, %O)", entry_map, link);
122 object entry = _Server->get_factory(CLASS_DATE)->execute(entry_map);
123 // is this a group calendar ?
124 object creator = do_query_attribute(CALENDAR_OWNER);
125 DEBUG_CALENDAR("creator of calendar is %O", creator);
126 if ( (link || entry_map->verteilung) &&
127 creator->get_object_class() & CLASS_GROUP )
129 object t = Task.Task();
130 t->func = "create_links";
131 if ( !arrayp(groups) )
134 t->params = ({ entry, creator, groups });
135 DEBUG_CALENDAR("New task %O", t);
136 get_module("tasks")->run_task(t);
142 object filter_datelinks ( object link ) {
143 object linkobj = link;
145 if ( !objectp(linkobj) ) return UNDEFINED;
146 if ( linkobj->get_object_class() & CLASS_LINK )
147 linkobj = linkobj->get_link_object();
148 else if ( linkobj->get_object_class() & CLASS_DATE )
150 else return UNDEFINED;
154 array get_all_entries_day ( void|int offset, void|int type )
156 object start = Calendar.Day() + offset;
157 object end = start + offset + 1;
158 return get_all_entries( start->unix_time(), end->unix_time()-1, type );
161 array get_all_entries_week ( void|int offset, void|int type )
163 object start = Calendar.Week() + offset;
164 object end = start + offset + 1;
165 return get_all_entries( start->unix_time(), end->unix_time()-1, type );
168 array get_all_entries_month ( void|int offset, void|int type )
170 object start = Calendar.Month() + offset;
171 object end = start + offset + 1;
172 return get_all_entries( start->unix_time(), end->unix_time()-1, type );
175 array get_all_entries_year ( void|int offset, void|int type )
177 object start = Calendar.Year() + offset;
178 object end = start + offset + 1;
179 return get_all_entries( start->unix_time(), end->unix_time()-1, type );
182 array get_all_entries(void|int start, void|int end, void|int type)
187 array dates = get_inventory_by_class( CLASS_DATE );
188 array dlinks = get_inventory_by_class( CLASS_LINK );
189 dlinks = filter( dlinks, filter_datelinks );
191 array matches = ({ });
192 foreach(dates, object date) {
193 err = catch( result = date->match(start, end) );
195 FATAL("While getting entries: %s\n%O", err[0], err[1]);
198 matches += ({ date });
200 foreach(dlinks, object link) {
201 object linkobj = filter_datelinks( link );
202 err = catch( result = linkobj->match(start, end) );
204 FATAL("While getting entries: %s\n%O", err[0], err[1]);
207 matches += ({ link });
209 if ( intp(type) && type > 0 ) {
210 array type_matches = ({ });
211 foreach(matches, object match) {
212 if ( !objectp(match) ) continue;
215 if ( match->get_object_class() & CLASS_LINK )
216 obj = match->get_link_object();
217 if ( match->query_attribute(DATE_TYPE) == type )
218 type_matches += ({ match });
225 return dates + dlinks;
228 array check_conflicts(int startdate, int enddate, int starttime, int endtime)
230 array dates = get_all_entries(startdate, enddate);
231 array matches = ({ });
232 foreach(dates, object date) {
233 if ( date->match_time(starttime, endtime) )
234 matches += ({ date });
239 int get_object_class()
241 return ::get_object_class() | CLASS_CALENDAR;