]> code.citadel.org Git - citadel.git/blobdiff - webcit/calendar_view.c
Day view : events with minutes
[citadel.git] / webcit / calendar_view.c
index b9f212a765842b792a28399ae6826cada58a41e1..10e3542a39a17e2a4acd8faaca4cc959d4c47bb9 100644 (file)
 
 /****************************************************************************/
 
+
+void embeddable_mini_calendar(int year, int month, char *urlformat)
+{
+       struct tm starting_tm;
+       struct tm tm;
+       time_t thetime;
+       int i, len;
+       time_t previous_month;
+       time_t next_month;
+       time_t colheader_time;
+       struct tm colheader_tm;
+       char colheader_label[32];
+       int weekstart = 0;
+       char weekstart_buf[16];
+       char url[256];
+       char div_id[256];
+       char escaped_urlformat[256];
+
+       snprintf(div_id, sizeof div_id, "mini_calendar_%d", rand() );
+
+       /* Determine what day to start.
+        */
+       get_preference("weekstart", weekstart_buf, sizeof weekstart_buf);
+       weekstart = atoi(weekstart_buf);
+
+       /*
+        * Now back up to the 1st of the month...
+        */
+       memset(&starting_tm, 0, sizeof(struct tm));
+
+       starting_tm.tm_year = year - 1900;
+       starting_tm.tm_mon = month - 1;
+       starting_tm.tm_mday = 1;
+       thetime = mktime(&starting_tm);
+
+       memcpy(&tm, &starting_tm, sizeof(struct tm));
+       while (tm.tm_mday != 1) {
+               thetime = thetime - (time_t)86400;      /* go back 24 hours */
+               localtime_r(&thetime, &tm);
+       }
+
+       /** Determine previous and next months ... for links */
+       previous_month = thetime - (time_t)864000L;     /* back 10 days */
+       next_month = thetime + (time_t)(31L * 86400L);  /* ahead 31 days */
+
+       /** Now back up until we're on the user's preferred start day */
+       localtime_r(&thetime, &tm);
+       while (tm.tm_wday != weekstart) {
+               thetime = thetime - (time_t)86400;      /* go back 24 hours */
+               localtime_r(&thetime, &tm);
+       }
+
+       wprintf("<div class=\"mini_calendar\" id=\"%s\">\n", div_id);
+
+       /* Previous month link */
+       localtime_r(&previous_month, &tm);
+       wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&laquo;</a>", 
+               (int)(tm.tm_year)+1900, tm.tm_mon + 1);
+
+       wc_strftime(colheader_label, sizeof colheader_label, "%B", &starting_tm);
+       wprintf("&nbsp;&nbsp;"
+               "<span class=\"mini_calendar_month_label\">"
+               "%s %d"
+               "</span>"
+               "&nbsp;&nbsp;", colheader_label, year);
+
+       /* Next month link */
+       localtime_r(&next_month, &tm);
+       wprintf("<a href=\"javascript:minical_change_month(%d,%d);\">&raquo;</a>",
+               (int)(tm.tm_year)+1900, tm.tm_mon + 1);
+
+       wprintf("<table border=0 cellpadding=1 cellspacing=1 class=\"mini_calendar_days\">"
+               "<tr>");
+       colheader_time = thetime;
+       for (i=0; i<7; ++i) {
+               colheader_time = thetime + (i * 86400) ;
+               localtime_r(&colheader_time, &colheader_tm);
+               wc_strftime(colheader_label, sizeof colheader_label, "%A", &colheader_tm);
+               wprintf("<th>%c</th>", colheader_label[0]);
+
+       }
+       wprintf("</tr>\n");
+
+
+        /** Now do 35 or 42 days */
+        for (i = 0; i < 42; ++i) {
+                localtime_r(&thetime, &tm);
+
+                if (i < 35) {
+
+                       /** Before displaying Sunday, start a new row */
+                       if ((i % 7) == 0) {
+                               wprintf("<tr>");
+                       }
+
+                       if (tm.tm_mon == month-1) {
+                               snprintf(url, sizeof url, urlformat,
+                                       tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday);
+                               wprintf("<td><a href=\"%s\">%d</a></td>", url, tm.tm_mday);
+                       }
+                       else {
+                               wprintf("<td> </td>");
+                       }
+
+                       /** After displaying one week, end the row */
+                       if ((i % 7) == 6) {
+                               wprintf("</tr>\n");
+                       }
+
+               }
+
+               thetime += (time_t)86400;               /** ahead 24 hours */
+       }
+
+       wprintf("</table>"                      /** end of inner table */
+               "</div>\n");
+
+       /* javascript for previous and next month */
+       len = strlen(urlformat);
+       for (i=0; i<len; ++i) {
+               sprintf(&escaped_urlformat[i*2], "%02X", urlformat[i]);
+       }
+
+       wprintf("<script type=\"text/javascript\">                                                      "
+               "       function minical_change_month(year, month) {                                    "
+               "               p = 'year=' + year + '&month=' + month                                  "
+               "                       + '&urlformat=%s&r=' + CtdlRandomString();                      "
+               "               new Ajax.Updater('%s', 'mini_calendar',                                 "
+               "                       { method: 'get', parameters: p, evalScripts: true } );          "
+               "       }                                                                               "
+               "</script>\n"
+               ,
+               escaped_urlformat, div_id
+       );
+
+}
+
+/* ajax embedder for the above mini calendar */
+void ajax_mini_calendar(void) {
+       char urlformat[256];
+       int i, len;
+       char *escaped_urlformat;
+
+       escaped_urlformat = bstr("urlformat");
+        len = strlen(escaped_urlformat) * 2 ;
+       for (i=0; i<len; ++i) {
+               urlformat[i] = xtoi(&escaped_urlformat[i*2], 2);
+               urlformat[i+1] = 0;
+       }
+
+       embeddable_mini_calendar( atoi(bstr("year")), atoi(bstr("month")), urlformat );
+}
+
+
 /**
  * \brief Display one day of a whole month view of a calendar
  * \param thetime the month we want to see 
@@ -645,6 +799,10 @@ void calendar_day_view_display_events(time_t thetime, int year, int month,
        struct tm ending_tm;
        int top = 0;
        int height = 0;
+       int gap = 0;
+       int startmin = 0;
+       int diffmin = 0;
+       int endmin = 0;
 
        if (WCC->num_cal == 0) {
                // \todo FIXME wprintf("<br /><br /><br />\n");
@@ -719,64 +877,85 @@ void calendar_day_view_display_events(time_t thetime, int year, int month,
                p = icalcomponent_get_first_property(Cal->cal,ICAL_SUMMARY_PROPERTY);
                if ((show_event) && (p != NULL)) {
 
-                       if ((t.day != today_start_t.day) && (end_t.day != today_start_t.day)) ongoing_event = 1; 
+                       if ((event_te.tm_mday != today_start_t.day) && (event_tm.tm_mday != today_start_t.day)) ongoing_event = 1; 
 
                        if (all_day_event) 
                        {
-                               wprintf("<li><a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\">",
+                               wprintf("<li class=\"event\"> "
+                               "<a href=\"display_edit_event?"
+                               "msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\" "
+                               " class=\"event_title\" >",
                                        Cal->cal_msgnum, year, month, day, hour);
                                escputs((char *) icalproperty_get_comment(p));
-                               wprintf("</a> (");
+                               wprintf("</a> <span>(");
                                wprintf(_("All day event"));
-                               wprintf(")</li>\n");
+                               wprintf(")</span></li>\n");
                        }
                        else if (ongoing_event && (hour == -1)) 
                        {
-                               wprintf("<li><a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\">",
+                               wprintf("<li class=\"event\"> "
+                               "<a href=\"display_edit_event?"
+                               "msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d\" "
+                               " class=\"event_title\" >",
                                        Cal->cal_msgnum, year, month, day, hour);
                                escputs((char *) icalproperty_get_comment(p));
-                               wprintf("</a> (");
+                               wprintf("</a> <span>(");
                                wprintf(_("Ongoing event"));
-                               wprintf(")</li>\n");
+                               wprintf(")</span></li>\n");
                        }
                        else 
                        {
+                               gap++;
                                if ((hour == event_te.tm_hour) && ! ongoing_event ) {
 
-                                       if (t.day != today_start_t.day) event_te.tm_hour = 0;
-                                       if (end_t.day != today_start_t.day) event_tm.tm_hour = 24;
+                                       if (event_te.tm_mday != today_start_t.day)      event_te.tm_hour = 0;
+                                       if (event_tm.tm_mday != today_start_t.day) event_tm.tm_hour = 24;
 
                                        if ((event_te.tm_hour < dstart) && (event_tm.tm_hour <= dstart)) {
-                                               top = (event_te.tm_hour * 11) -1;
-                                               height= (event_tm.tm_hour - event_te.tm_hour) * 11;
+                                               startmin = diffmin = event_te.tm_min / 6;
+                                               endmin = ((event_tm.tm_hour == hour) ? (event_tm.tm_min / 2) : (event_tm.tm_min / 6)) ;
+                                               top = (event_te.tm_hour * 11) + startmin -1;
+                                               height= ((event_tm.tm_hour - event_te.tm_hour) * 11) + endmin - diffmin ;
                                        }
                                        if ((event_te.tm_hour < dstart) && (event_tm.tm_hour >= dstart)) {
-                                               top = (event_te.tm_hour * 11) - 1;
-                                               height = ((dstart - event_te.tm_hour) * 11) + ((event_tm.tm_hour - dstart) * 31);
+                                               startmin = diffmin = event_te.tm_min / 6;
+                                               endmin = event_tm.tm_min / 2;
+                                               top = (event_te.tm_hour * 11) + startmin - 1;
+                                               height = ((dstart - event_te.tm_hour) * 11) + ((event_tm.tm_hour - dstart) * 31) + endmin - (diffmin * 3);
                                        }
                                        if ((event_te.tm_hour <= dstart) && (event_tm.tm_hour > dend)) {
-                                               top = (event_te.tm_hour * 11) - 1;
-                                               height = ((dstart - event_te.tm_hour) * 11) + ((dend - dstart + 1) * 31) + ((event_tm.tm_hour - dend - 1) * 10);
+                                               startmin = diffmin = ((event_te.tm_hour == hour) ? (event_te.tm_min / 2) : (event_te.tm_min / 6)) ;
+                                               endmin = event_tm.tm_min / 6; 
+                                               top = (event_te.tm_hour * 11)  + startmin - 1;
+                                               height = ((dstart - event_te.tm_hour) * 11) + ((dend - dstart + 1) * 31) + ((event_tm.tm_hour - dend - 1) * 10) + endmin - diffmin;
                                        }
                                        if ((event_te.tm_hour >= dstart) && (event_tm.tm_hour <= dend)) {
-                                               top = (dstart * 11) + ((event_te.tm_hour - dstart) * 31) - 1;
-                                               height = ((event_tm.tm_hour - event_te.tm_hour) * 31);
+                                               startmin = diffmin = (event_te.tm_min / 2);
+                                               endmin = event_tm.tm_min / 2;
+                                               top = (dstart * 11) + ((event_te.tm_hour - dstart) * 31) + startmin - 1;
+                                               height = ((event_tm.tm_hour - event_te.tm_hour) * 31) + endmin - diffmin;
                                        }
                                        if ((event_te.tm_hour >= dstart) && (event_te.tm_hour <= dend) && (event_tm.tm_hour > dend)) {
-                                               top = (dstart * 11) + ((event_te.tm_hour - dstart) * 31) - 1;
-                                               height = (((dend - event_te.tm_hour + 1) * 31) + ((event_tm.tm_hour - dend - 1) * 11));
+                                               startmin = diffmin = (event_te.tm_min / 2);
+                                               endmin = event_tm.tm_min / 6;
+                                               top = (dstart * 11) + ((event_te.tm_hour - dstart) * 31)  + diffmin - 1;
+                                               height = (((dend - event_te.tm_hour + 1) * 31) + ((event_tm.tm_hour - dend - 1) * 11)) + endmin - diffmin;
                                        }
                                        if ((event_te.tm_hour > dend) && (event_tm.tm_hour > dend)) {
-                                               top = (dstart * 11) + ((dend - dstart + 1) * 31) + ((event_tm.tm_hour - event_te.tm_hour) * 11) - 1;
-                                               height = ((event_tm.tm_hour - event_te.tm_hour) * 11);
+                                               startmin = diffmin = event_te.tm_min / 6;
+                                               endmin = event_tm.tm_min / 6;
+                                               top = (dstart * 11) + ((dend - dstart + 1) * 31) + ((event_tm.tm_hour - event_te.tm_hour) * 11) + startmin - 1;
+                                               height = ((event_tm.tm_hour - event_te.tm_hour) * 11) + endmin - diffmin;
                                        }
                                wprintf("<dd  class=\"event\" "
                                        "style=\"position: absolute; "
-                                       "top:%dpx; left:100px; "
+                                       "top:%dpx; left:%dpx; "
                                        "height:%dpx; \" >",
-                                       top, height
+                                       top, (gap * 40), height
                                        );
-                               wprintf("<a href=\"display_edit_event?msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d&case=%d\">",
+                               wprintf("<a href=\"display_edit_event?"
+                                       "msgnum=%ld&calview=day&year=%d&month=%d&day=%d&hour=%d&case=%d\" "
+                                       "class=\"event_title\" >",
                                        Cal->cal_msgnum, year, month, day, t.hour, hour);
                                escputs((char *) icalproperty_get_comment(p));
                                wprintf("</a></dd>\n");
@@ -882,7 +1061,7 @@ void calendar_day_view(int year, int month, int day) {
 
        wprintf("</td>");       /** end extra on the middle */
 
-       wprintf("<td width=20%% valign=top>");  /** begin stuff-on-the-right */
+       wprintf("<td width=20%% align=center valign=top>");     /** begin stuff-on-the-right */
 
        /** Begin todays-date-with-left-and-right-arrows */
        wprintf("<table border=0 width=100%% "
@@ -917,7 +1096,9 @@ void calendar_day_view(int year, int month, int day) {
        wprintf("</tr></table>\n");
        /** End todays-date-with-left-and-right-arrows */
 
-       /** \todo In the future we might want to put a month-o-matic here */
+       /** Embed a mini month calendar in this space */
+       wprintf("<br />\n");
+       embeddable_mini_calendar(year, month, "readfwd?calview=day&year=%d&month=%d&day=%d");
 
        wprintf("</font></center>\n");
 
@@ -1201,6 +1382,9 @@ void do_tasks_view(void) {
        wprintf("</i></center><br />\n");
 }
 
+/**\brief stub for non-libical builds */
+void ajax_mini_calendar(void) {
+}
 
 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */