2 * Miscellaneous functions which handle calendar components.
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
22 "12am", "1am", "2am", "3am", "4am", "5am", "6am",
23 "7am", "8am", "9am", "10am", "11am", "12pm",
24 "1pm", "2pm", "3pm", "4pm", "5pm", "6pm",
25 "7pm", "8pm", "9pm", "10pm", "11pm"
29 * The display_icaltimetype_as_webform() and icaltime_from_webform() functions
30 * handle the display and editing of date/time properties in web pages. The
31 * first one converts an icaltimetype into valid HTML markup -- a series of form
32 * fields for editing the date and time. When the user submits the form, the
33 * results can be fed back into the second function, which turns it back into
34 * an icaltimetype. The "prefix" string required by both functions is prepended
35 * to all field names. This allows a form to contain more than one date/time
36 * property (for example, a start and end time) by ensuring the field names are
37 * unique within the form.
39 * NOTE: These functions assume that the icaltimetype being edited is in UTC, and
40 * will convert to/from local time for editing. "local" in this case is assumed
41 * to be the time zone in which the WebCit server is running. A future improvement
42 * might be to allow the user to specify his/her timezone.
45 void display_icaltimetype_as_webform(struct icaltimetype *t, char *prefix, int date_only) {
52 int all_day_event = 0;
56 time_format = get_time_format_cached ();
59 localtime_r(&now, &tm_now);
61 if (t == NULL) return;
62 if (t->is_date) all_day_event = 1;
63 tt = icaltime_as_timet(*t);
68 localtime_r(&tt, &tm);
71 wc_printf("<input type=\"text\" name=\"");
72 StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
73 wc_printf("\" id=\"");
74 StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
75 wc_printf("\" size=\"10\" maxlength=\"10\" value=\"");
76 wc_strftime(timebuf, 32, "%Y-%m-%d", &tm);
77 StrBufAppendBufPlain(WCC->WBuf, timebuf, -1, 0);
80 StrBufAppendPrintf(WC->trailing_javascript, "attachDatePicker('");
81 StrBufAppendPrintf(WC->trailing_javascript, prefix);
82 StrBufAppendPrintf(WC->trailing_javascript, "', '%s');\n", get_selected_language());
84 /* If we're editing a date only, we still generate the time boxes, but we hide them.
85 * This keeps the data model consistent.
88 wc_printf("<div style=\"display:none\">");
91 wc_printf("<span ID=\"");
92 StrBufAppendBufPlain(WCC->WBuf, prefix, -1, 0);
93 wc_printf("_time\">");
94 wc_printf(_("Hour: "));
95 wc_printf("<SELECT NAME=\"%s_hour\" SIZE=\"1\">\n", prefix);
96 for (i=0; i<=23; ++i) {
98 if (time_format == WC_TIMEFORMAT_24) {
99 wc_printf("<OPTION %s VALUE=\"%d\">%d</OPTION>\n",
100 ((tm.tm_hour == i) ? "SELECTED" : ""),
105 wc_printf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
106 ((tm.tm_hour == i) ? "SELECTED" : ""),
112 wc_printf("</SELECT>\n");
114 wc_printf(_("Minute: "));
115 wc_printf("<SELECT NAME=\"%s_minute\" SIZE=\"1\">\n", prefix);
116 for (i=0; i<=59; ++i) {
117 if ( (i % 5 == 0) || (tm.tm_min == i) ) {
118 wc_printf("<OPTION %s VALUE=\"%d\">:%02d</OPTION>\n",
119 ((tm.tm_min == i) ? "SELECTED" : ""),
124 wc_printf("</SELECT></span>\n");
132 * Get date/time from a web form and convert it into an icaltimetype struct.
134 void icaltime_from_webform(struct icaltimetype *t, char *prefix) {
139 /* Stuff with zero values */
140 memset(t, 0, sizeof(struct icaltimetype));
142 /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
143 sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
146 sprintf(vname, "%s_hour", prefix);
147 t->hour = IBSTR(vname);
150 sprintf(vname, "%s_minute", prefix);
151 t->minute = IBSTR(vname);
153 /* time zone is set to the default zone for this server */
156 t->zone = get_default_icaltimezone();
161 * Get date (no time) from a web form and convert it into an icaltimetype struct.
163 void icaltime_from_webform_dateonly(struct icaltimetype *t, char *prefix) {
166 /* Stuff with zero values */
167 memset(t, 0, sizeof(struct icaltimetype));
169 /* Get the year/month/date all in one shot -- it will be in ISO YYYY-MM-DD format */
170 sscanf((char*)BSTR(prefix), "%04d-%02d-%02d", &t->year, &t->month, &t->day);
172 /* time zone is set to the default zone for this server */
179 * Render a PARTSTAT parameter as a string (and put it in parentheses)
181 void partstat_as_string(char *buf, icalproperty *attendee) {
182 icalparameter *partstat_param;
183 icalparameter_partstat partstat;
185 strcpy(buf, _("(status unknown)"));
187 partstat_param = icalproperty_get_first_parameter(
189 ICAL_PARTSTAT_PARAMETER
191 if (partstat_param == NULL) {
195 partstat = icalparameter_get_partstat(partstat_param);
197 case ICAL_PARTSTAT_X:
200 case ICAL_PARTSTAT_NEEDSACTION:
201 strcpy(buf, _("(needs action)"));
203 case ICAL_PARTSTAT_ACCEPTED:
204 strcpy(buf, _("(accepted)"));
206 case ICAL_PARTSTAT_DECLINED:
207 strcpy(buf, _("(declined)"));
209 case ICAL_PARTSTAT_TENTATIVE:
210 strcpy(buf, _("(tenative)"));
212 case ICAL_PARTSTAT_DELEGATED:
213 strcpy(buf, _("(delegated)"));
215 case ICAL_PARTSTAT_COMPLETED:
216 strcpy(buf, _("(completed)"));
218 case ICAL_PARTSTAT_INPROCESS:
219 strcpy(buf, _("(in process)"));
221 case ICAL_PARTSTAT_NONE:
222 strcpy(buf, _("(none)"));
228 * Utility function to encapsulate a subcomponent into a full VCALENDAR.
230 * We also scan for any date/time properties that reference timezones, and attach
231 * those timezones along with the supplied subcomponent. (Increase the size of the array if you need to.)
233 * Note: if you change anything here, change it in Citadel server's ical_send_out_invitations() too.
235 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
236 icalcomponent *encaps;
238 struct icaltimetype t;
239 const icaltimezone *attached_zones[5] = { NULL, NULL, NULL, NULL, NULL };
241 const icaltimezone *z;
242 int num_zones_attached = 0;
243 int zone_already_attached;
245 if (subcomp == NULL) {
246 syslog(3, "ERROR: ical_encapsulate_subcomponent() called with NULL argument\n");
251 * If we're already looking at a full VCALENDAR component, this is probably an error.
253 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
254 syslog(3, "ERROR: component sent to ical_encapsulate_subcomponent() already top level\n");
259 for (p = icalcomponent_get_first_property(subcomp, ICAL_ANY_PROPERTY);
261 p = icalcomponent_get_next_property(subcomp, ICAL_ANY_PROPERTY))
263 if ( (icalproperty_isa(p) == ICAL_COMPLETED_PROPERTY)
264 || (icalproperty_isa(p) == ICAL_CREATED_PROPERTY)
265 || (icalproperty_isa(p) == ICAL_DATEMAX_PROPERTY)
266 || (icalproperty_isa(p) == ICAL_DATEMIN_PROPERTY)
267 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
268 || (icalproperty_isa(p) == ICAL_DTSTAMP_PROPERTY)
269 || (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
270 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
271 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
272 || (icalproperty_isa(p) == ICAL_LASTMODIFIED_PROPERTY)
273 || (icalproperty_isa(p) == ICAL_MAXDATE_PROPERTY)
274 || (icalproperty_isa(p) == ICAL_MINDATE_PROPERTY)
275 || (icalproperty_isa(p) == ICAL_RECURRENCEID_PROPERTY)
277 t = icalproperty_get_dtstart(p); /*/ it's safe to use dtstart for all of them */
278 if ((icaltime_is_valid_time(t)) && (z=icaltime_get_timezone(t), z)) {
280 zone_already_attached = 0;
281 for (i=0; i<5; ++i) {
282 if (z == attached_zones[i]) {
283 ++zone_already_attached;
284 syslog(9, "zone already attached!!\n");
287 if ((!zone_already_attached) && (num_zones_attached < 5)) {
288 syslog(9, "attaching zone %d!\n", num_zones_attached);
289 attached_zones[num_zones_attached++] = z;
292 icalproperty_set_parameter(p,
293 icalparameter_new_tzid(icaltimezone_get_tzid((icaltimezone *)z))
299 /* Encapsulate the VEVENT component into a complete VCALENDAR */
300 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
301 if (encaps == NULL) {
302 syslog(3, "ERROR: ical_encapsulate_subcomponent() could not allocate component\n");
306 /* Set the Product ID */
307 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
309 /* Set the Version Number */
310 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
312 /* Attach any timezones we need */
313 if (num_zones_attached > 0) for (i=0; i<num_zones_attached; ++i) {
315 zc = icalcomponent_new_clone(icaltimezone_get_component((icaltimezone *)attached_zones[i]));
316 icalcomponent_add_component(encaps, zc);
319 /* Encapsulate the subcomponent inside */
320 icalcomponent_add_component(encaps, subcomp);
322 /* Return the object we just created. */