Calendar._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2006 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
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.
7  *
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.
12  *
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
16  */
17 inherit "/classes/Room";
18 #include <classes.h>
19 #include <attributes.h>
20 #include <macros.h>
21 //! A Calendar is a room with Date Objects in it.
22 class Calendar : public Room{
23 public:
24 
25 
26 
27 //#define CALENDAR_DEBUG 1
28 
29 #ifdef CALENDAR_DEBUG
30 #define DEBUG_CALENDAR(s, args...) write("Calendar: "+s+"\n", args)
31 #else
32 #define DEBUG_CALENDAR(s, args...)
33 #endif
34 
35 
36 string describe()
37 {
38  object creator = query_attribute(CALENDAR_OWNER);
39  return "Calendar("+(objectp(creator) ?
40  creator->get_identifier() + "'s calendar" :
41  "Noones calendar")+")";
42 }
43 
44 void create_links(object date, object creator, array groups)
45 {
46  object factory = get_factory(CLASS_LINK);
47  object link, calendar, ulink;
48  array haves = ({ });
49 
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);
56  else continue;
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 );
60  continue;
61  }
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 });
68  }
69  }
70 
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 });
79  }
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);
85  else continue;
86  if ( !objectp(calendar) ) {
87  werror( "Calendar: invalid calendar of member %O\n", member );
88  continue;
89  }
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 });
96  }
97  }
98  }
99 }
100 
101 object add_entry_recursive(mapping entry_map, void|int link, void|array groups)
102 {
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 });
111  }
112  }
113  }
114  }
115  return add_entry( entry_map, link, groups_recursive );
116 }
117 
118 object
119 add_entry(mapping entry_map,void|int link, void|array groups)
120 {
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 )
128  {
129  object t = Task.Task();
130  t->func = "create_links";
131  if ( !arrayp(groups) )
132  groups = ({ });
133 
134  t->params = ({ entry, creator, groups });
135  DEBUG_CALENDAR("New task %O", t);
136  get_module("tasks")->run_task(t);
137  }
138 
139  return entry;
140 }
141 
142 object filter_datelinks ( object link ) {
143  object linkobj = link;
144  do {
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 )
149  return linkobj;
150  else return UNDEFINED;
151  } while ( true );
152 }
153 
154 array get_all_entries_day ( void|int offset, void|int type )
155 {
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 );
159 }
160 
161 array get_all_entries_week ( void|int offset, void|int type )
162 {
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 );
166 }
167 
168 array get_all_entries_month ( void|int offset, void|int type )
169 {
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 );
173 }
174 
175 array get_all_entries_year ( void|int offset, void|int type )
176 {
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 );
180 }
181 
182 array get_all_entries(void|int start, void|int end, void|int type)
183 {
184  int result;
185  mixed err;
186 
187  array dates = get_inventory_by_class( CLASS_DATE );
188  array dlinks = get_inventory_by_class( CLASS_LINK );
189  dlinks = filter( dlinks, filter_datelinks );
190  if ( start > 0 ) {
191  array matches = ({ });
192  foreach(dates, object date) {
193  err = catch( result = date->match(start, end) );
194  if ( err ) {
195  FATAL("While getting entries: %s\n%O", err[0], err[1]);
196  }
197  else if ( result )
198  matches += ({ date });
199  }
200  foreach(dlinks, object link) {
201  object linkobj = filter_datelinks( link );
202  err = catch( result = linkobj->match(start, end) );
203  if ( err ) {
204  FATAL("While getting entries: %s\n%O", err[0], err[1]);
205  }
206  else if ( result )
207  matches += ({ link });
208  }
209  if ( intp(type) && type > 0 ) {
210  array type_matches = ({ });
211  foreach(matches, object match) {
212  if ( !objectp(match) ) continue;
213 
214  object obj = match;
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 });
219  }
220  return type_matches;
221  }
222  return matches;
223  }
224 
225  return dates + dlinks;
226 }
227 
228 array check_conflicts(int startdate, int enddate, int starttime, int endtime)
229 {
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 });
235  }
236  return matches;
237 }
238 
239 int get_object_class()
240 {
241  return ::get_object_class() | CLASS_CALENDAR;
242 }
243 
244 
245 };