+
+/**
+ * @brief Get days of a month in a given year.
+ *
+ * Returns number of days in the month of the given year.
+ * $m = 1 is 'January' to match human usage.
+ *
+ * @param int $y Year
+ * @param int $m Month (1=January, 12=December)
+ *
+ * @return int Number of days in the given month
+ */
+function get_dim($y,$m) {
+
+ $dim = array( 0,
+ 31, 28, 31, 30, 31, 30,
+ 31, 31, 30, 31, 30, 31);
+
+ if($m != 2)
+ return $dim[$m];
+
+ if(((($y % 4) == 0) && (($y % 100) != 0)) || (($y % 400) == 0))
+ return 29;
+
+ return $dim[2];
+}
+
+/**
+ * @brief Returns the first day in month for a given month, year.
+ *
+ * Months start at 1.
+ *
+ * @param int $y Year
+ * @param int $m Month (1=January, 12=December)
+ *
+ * @return string day 0 = Sunday through 6 = Saturday
+ */
+function get_first_dim($y,$m) {
+ $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
+
+ return datetime_convert('UTC','UTC',$d,'w');
+}
+
+/**
+ * @brief Output a calendar for the given month, year.
+ *
+ * If $links are provided (array), e.g. $links[12] => 'http://mylink' ,
+ * date 12 will be linked appropriately. Today's date is also noted by
+ * altering td class.
+ * Months count from 1.
+ *
+ * @param int $y Year
+ * @param int $m Month
+ * @param bool $links (default false)
+ * @param string $class
+ *
+ * @return string
+ *
+ * @todo Provide (prev,next) links, define class variations for different size calendars
+ */
+function cal($y = 0,$m = 0, $links = false, $class='') {
+
+
+ // month table - start at 1 to match human usage.
+
+ $mtab = array(' ',
+ 'January','February','March',
+ 'April','May','June',
+ 'July','August','September',
+ 'October','November','December'
+ );
+
+ $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
+ $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
+ if(! $y)
+ $y = $thisyear;
+ if(! $m)
+ $m = intval($thismonth);
+
+ $dn = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
+ $f = get_first_dim($y,$m);
+ $l = get_dim($y,$m);
+ $d = 1;
+ $dow = 0;
+ $started = false;
+
+ if(($y == $thisyear) && ($m == $thismonth))
+ $tddate = intval(datetime_convert('UTC',date_default_timezone_get(),'now','j'));
+
+ $str_month = day_translate($mtab[$m]);
+ $o = '<table class="calendar' . $class . '">';
+ $o .= "<caption>$str_month $y</caption><tr>";
+ for($a = 0; $a < 7; $a ++)
+ $o .= '<th>' . mb_substr(day_translate($dn[$a]),0,3,'UTF-8') . '</th>';
+
+ $o .= '</tr><tr>';
+
+ while($d <= $l) {
+ if(($dow == $f) && (! $started))
+ $started = true;
+
+ $today = (((isset($tddate)) && ($tddate == $d)) ? "class=\"today\" " : '');
+ $o .= "<td $today>";
+ $day = str_replace(' ',' ',sprintf('%2.2d', $d));
+ if($started) {
+ if(is_array($links) && isset($links[$d]))
+ $o .= "<a href=\"{$links[$d]}\">$day</a>";
+ else
+ $o .= $day;
+
+ $d ++;
+ } else {
+ $o .= ' ';
+ }
+
+ $o .= '</td>';
+ $dow ++;
+ if(($dow == 7) && ($d <= $l)) {
+ $dow = 0;
+ $o .= '</tr><tr>';
+ }
+ }
+ if($dow)
+ for($a = $dow; $a < 7; $a ++)
+ $o .= '<td> </td>';
+
+ $o .= '</tr></table>'."\r\n";
+
+ return $o;
+}
+
+/**
+ * @brief Create a birthday event.
+ *
+ * Update the year and the birthday.
+ */
+function update_contact_birthdays() {
+
+ // This only handles foreign or alien networks where a birthday has been provided.
+ // In-network birthdays are handled within local_delivery
+
+ $r = q("SELECT * FROM contact WHERE `bd` != '' AND `bd` != '0000-00-00' AND SUBSTRING(`bd`,1,4) != `bdyear` ");
+ if(count($r)) {
+ foreach($r as $rr) {
+
+ logger('update_contact_birthday: ' . $rr['bd']);
+
+ $nextbd = datetime_convert('UTC','UTC','now','Y') . substr($rr['bd'],4);
+
+ /**
+ *
+ * Add new birthday event for this person
+ *
+ * $bdtext is just a readable placeholder in case the event is shared
+ * with others. We will replace it during presentation to our $importer
+ * to contain a sparkle link and perhaps a photo.
+ *
+ */
+
+ $bdtext = sprintf( t('%s\'s birthday'), $rr['name']);
+ $bdtext2 = sprintf( t('Happy Birthday %s'), ' [url=' . $rr['url'] . ']' . $rr['name'] . '[/url]') ;
+
+ $r = q("INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`,`adjust`)
+ VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' ) ",
+ intval($rr['uid']),
+ intval($rr['id']),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert('UTC','UTC', $nextbd)),
+ dbesc(datetime_convert('UTC','UTC', $nextbd . ' + 1 day ')),
+ dbesc($bdtext),
+ dbesc($bdtext2),
+ dbesc('birthday'),
+ intval(0)
+ );
+
+
+ // update bdyear
+
+ q("UPDATE `contact` SET `bdyear` = '%s', `bd` = '%s' WHERE `uid` = %d AND `id` = %d",
+ dbesc(substr($nextbd,0,4)),
+ dbesc($nextbd),
+ intval($rr['uid']),
+ intval($rr['id'])
+ );
+
+ }
+ }
+}