From fcc185a18261f5ae2addeb80bf32e3a9b69ca5ce Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Fri, 16 Oct 2015 23:50:34 +0200
Subject: [PATCH 1/8] Unsure to store the guid with new events

---
 include/event.php | 27 ++++++++++++++-------------
 include/items.php |  2 ++
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/include/event.php b/include/event.php
index fedbe24468..a87dba64fb 100644
--- a/include/event.php
+++ b/include/event.php
@@ -21,33 +21,33 @@ function format_event_html($ev) {
 
 	$o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
 		. datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
-		. '" >' 
-		. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
+		. '" >'
+		. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
 			$ev['start'] , $bd_format ))
-			:  day_translate(datetime_convert('UTC', 'UTC', 
+			:  day_translate(datetime_convert('UTC', 'UTC',
 			$ev['start'] , $bd_format)))
 		. '</abbr></p>' . "\r\n";
 
 	if(! $ev['nofinish'])
 		$o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
 			. datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
-			. '" >' 
-			. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(), 
+			. '" >'
+			. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
 				$ev['finish'] , $bd_format ))
-				:  day_translate(datetime_convert('UTC', 'UTC', 
+				:  day_translate(datetime_convert('UTC', 'UTC',
 				$ev['finish'] , $bd_format )))
 			. '</abbr></p>'  . "\r\n";
 
 	if(strlen($ev['location'])){
-		$o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">' 
-			. bbcode($ev['location']) 
+		$o .= '<p class="event-location"> ' . t('Location:') . ' <span class="location">'
+			. bbcode($ev['location'])
 			. '</span></p>' . "\r\n";
-		
+
 		if (strpos($ev['location'], "[map")===False) {
 			$map = generate_named_map($ev['location']);
 			if ($map!==$ev['location']) $o.=$map;
 		}
-		
+
 	}
 
 	$o .= '</div>' . "\r\n";
@@ -137,7 +137,7 @@ function format_event_bbcode($ev) {
 
 	if(($ev['finish']) && (! $ev['nofinish']))
 		$o .= '[event-finish]' . $ev['finish'] . '[/event-finish]';
- 
+
 	if($ev['location'])
 		$o .= '[event-location]' . $ev['location'] . '[/event-location]';
 
@@ -200,7 +200,7 @@ function ev_compare($a,$b) {
 
 	if($date_a === $date_b)
 		return strcasecmp($a['desc'],$b['desc']);
-	
+
 	return strcmp($date_a,$date_b);
 }
 
@@ -324,7 +324,7 @@ function event_store($arr) {
 	}
 	else {
 
-		// New event. Store it. 
+		// New event. Store it.
 
 		$r = q("INSERT INTO `event` ( `uid`,`cid`,`uri`,`created`,`edited`,`start`,`finish`,`summary`, `desc`,`location`,`type`,
 			`adjust`,`nofinish`,`allow_cid`,`allow_gid`,`deny_cid`,`deny_gid`)
@@ -362,6 +362,7 @@ function event_store($arr) {
 		$item_arr['contact-id']    = $arr['cid'];
 		$item_arr['uri']           = $arr['uri'];
 		$item_arr['parent-uri']    = $arr['uri'];
+		$item_arr['guid']          = $arr['guid'];
 		$item_arr['type']          = 'activity';
 		$item_arr['wall']          = (($arr['cid']) ? 0 : 1);
 		$item_arr['contact-id']    = $contact['id'];
diff --git a/include/items.php b/include/items.php
index 4e86a5200a..c14fcdcc99 100644
--- a/include/items.php
+++ b/include/items.php
@@ -2854,6 +2854,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 						$ev['uri'] = $item_id;
 						$ev['edited'] = $datarray['edited'];
 						$ev['private'] = $datarray['private'];
+						$ev['guid'] = $datarray['guid'];
 
 						if(is_array($contact))
 							$ev['cid'] = $contact['id'];
@@ -4079,6 +4080,7 @@ function local_delivery($importer,$data) {
 					$ev['uri'] = $item_id;
 					$ev['edited'] = $datarray['edited'];
 					$ev['private'] = $datarray['private'];
+					$ev['guid'] = $datarray['guid'];
 
 					$r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
 						dbesc($item_id),

From dd46a6ff680a42e2a8c826a99bf729f90c2afffc Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 00:58:22 +0200
Subject: [PATCH 2/8] Store the event when a summary or a description is set

---
 include/items.php | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/include/items.php b/include/items.php
index c14fcdcc99..795001bec6 100644
--- a/include/items.php
+++ b/include/items.php
@@ -2849,7 +2849,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 
 				if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
 					$ev = bbtoevent($datarray['body']);
-					if(x($ev,'desc') && x($ev,'start')) {
+					if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
 						$ev['uid'] = $importer['uid'];
 						$ev['uri'] = $item_id;
 						$ev['edited'] = $datarray['edited'];
@@ -4074,7 +4074,7 @@ function local_delivery($importer,$data) {
 
 			if((x($datarray,'object-type')) && ($datarray['object-type'] === ACTIVITY_OBJ_EVENT)) {
 				$ev = bbtoevent($datarray['body']);
-				if(x($ev,'desc') && x($ev,'start')) {
+				if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
 					$ev['cid'] = $importer['id'];
 					$ev['uid'] = $importer['uid'];
 					$ev['uri'] = $item_id;

From 7f9711ffe631c4bca7abb6b5cda6365660e2b386 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 01:25:25 +0200
Subject: [PATCH 3/8] Delete event when the item is deleted

---
 include/event.php |  6 ++++++
 include/items.php | 10 ++++++++++
 2 files changed, 16 insertions(+)

diff --git a/include/event.php b/include/event.php
index a87dba64fb..d56388a77a 100644
--- a/include/event.php
+++ b/include/event.php
@@ -204,7 +204,13 @@ function ev_compare($a,$b) {
 	return strcmp($date_a,$date_b);
 }
 
+function event_delete($event_id) {
+	if ($event_id == 0)
+		return;
 
+	q("DELETE FROM `event` WHERE `id` = %d", intval($event_id));
+	logger("Deleted event ".$event_id, LOGGER_DEBUG);
+}
 
 function event_store($arr) {
 
diff --git a/include/items.php b/include/items.php
index 795001bec6..7d1ab1cb36 100644
--- a/include/items.php
+++ b/include/items.php
@@ -2558,6 +2558,11 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0)
 					if(! $item['deleted'])
 						logger('consume_feed: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
+					if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
+						logger("Deleting event ".$item['event-id'], LOGGER_DEBUG);
+						event_delete($item['event-id']);
+					}
+
 					if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
 						$xo = parse_xml_string($item['object'],false);
 						$xt = parse_xml_string($item['target'],false);
@@ -3544,6 +3549,11 @@ function local_delivery($importer,$data) {
 
 					logger('local_delivery: deleting item ' . $item['id'] . ' uri=' . $item['uri'], LOGGER_DEBUG);
 
+					if($item['object-type'] === ACTIVITY_OBJ_EVENT) {
+						logger("Deleting event ".$item['event-id'], LOGGER_DEBUG);
+						event_delete($item['event-id']);
+					}
+
 					if(($item['verb'] === ACTIVITY_TAG) && ($item['object-type'] === ACTIVITY_OBJ_TAGTERM)) {
 						$xo = parse_xml_string($item['object'],false);
 						$xt = parse_xml_string($item['target'],false);

From 485e65871e109ccc51fccb91857e2dc296fdf379 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 02:36:16 +0200
Subject: [PATCH 4/8] Avoid wrong birthdays

---
 include/diaspora.php | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/diaspora.php b/include/diaspora.php
index 757cf1a6ba..c97abc28cd 100644
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -2421,7 +2421,8 @@ function diaspora_profile($importer,$xml,$msg) {
 
 	$birthday = str_replace('1000','1901',$birthday);
 
-	$birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
+	if ($birthday != "")
+		$birthday = datetime_convert('UTC','UTC',$birthday,'Y-m-d');
 
 	// this is to prevent multiple birthday notifications in a single year
 	// if we already have a stored birthday and the 'm-d' part hasn't changed, preserve the entry, which will preserve the notify year

From 316276cb6d6ec61c1c839c81fbe6d96ed1705b49 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 08:27:33 +0200
Subject: [PATCH 5/8] Editing an item with an event is now opeing the event
 edit form.

---
 mod/events.php  | 4 ++++
 object/Item.php | 9 ++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/mod/events.php b/mod/events.php
index bf53286c20..f2891a38d5 100644
--- a/mod/events.php
+++ b/mod/events.php
@@ -154,6 +154,7 @@ function events_post(&$a) {
 	if(! $cid)
 		proc_run('php',"include/notifier.php","event","$item_id");
 
+	goaway($_SESSION['return_url']);
 }
 
 
@@ -165,6 +166,9 @@ function events_content(&$a) {
 		return;
 	}
 
+	if($a->argc == 1)
+		$_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
+
 	if(($a->argc > 2) && ($a->argv[1] === 'ignore') && intval($a->argv[2])) {
 		$r = q("update event set ignore = 1 where id = %d and uid = %d",
 			intval($a->argv[2]),
diff --git a/object/Item.php b/object/Item.php
index 3050365f92..04c1a707e3 100644
--- a/object/Item.php
+++ b/object/Item.php
@@ -117,9 +117,12 @@ class Item extends BaseObject {
 			? t('Private Message')
 			: false);
 		$shareable = ((($conv->get_profile_owner() == local_user()) && ($item['private'] != 1)) ? true : false);
-		if(local_user() && link_compare($a->contact['url'],$item['author-link']))
-			$edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
-		else
+		if(local_user() && link_compare($a->contact['url'],$item['author-link'])) {
+			if ($item["event-id"] != 0)
+				$edpost = array($a->get_baseurl($ssl_state)."/events/event/".$item['event-id'], t("Edit"));
+			else
+				$edpost = array($a->get_baseurl($ssl_state)."/editpost/".$item['id'], t("Edit"));
+		} else
 			$edpost = false;
 		if(($this->get_data_value('uid') == local_user()) || $this->is_visiting())
 			$dropping = true;

From 2070c96dd1910cd16c36277b3ee3198a5f8eafbd Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 08:46:45 +0200
Subject: [PATCH 6/8] Changing the acl does not work when editing posts - so it
 is disabled for events.

---
 mod/events.php | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mod/events.php b/mod/events.php
index f2891a38d5..653ae489b8 100644
--- a/mod/events.php
+++ b/mod/events.php
@@ -509,7 +509,7 @@ function events_content(&$a) {
 		else
 			$sh_checked = (($orig_event['allow_cid'] === '<' . local_user() . '>' && (! $orig_event['allow_gid']) && (! $orig_event['deny_cid']) && (! $orig_event['deny_gid'])) ? '' : ' checked="checked" ' );
 
-		if($cid)
+		if($cid OR ($mode !== 'new'))
 			$sh_checked .= ' disabled="disabled" ';
 
 
@@ -540,6 +540,9 @@ function events_content(&$a) {
 
 		require_once('include/acl_selectors.php');
 
+		if ($mode === 'new')
+			$acl = (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user)));
+
 		$tpl = get_markup_template('event_form.tpl');
 
 		$o .= replace_macros($tpl,array(
@@ -567,7 +570,7 @@ function events_content(&$a) {
 			'$sh_text' => t('Share this event'),
 			'$sh_checked' => $sh_checked,
 			'$preview' => t('Preview'),
-			'$acl' => (($cid) ? '' : populate_acl(((x($orig_event)) ? $orig_event : $a->user))),
+			'$acl' => $acl,
 			'$submit' => t('Submit')
 
 		));

From 8395f67351b3ac2b3739a99967ba58bf8e731051 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 09:41:58 +0200
Subject: [PATCH 7/8] Events on Diaspora now looking okay.

---
 include/bbcode.php |  2 +-
 include/event.php  | 40 +++++++++++++++++++++++++++++-----------
 2 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/include/bbcode.php b/include/bbcode.php
index 2fcf6c3247..81536d3720 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -1210,7 +1210,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true, $simplehtml = fal
 	// start which is always required). Allow desc with a missing summary for compatibility.
 
 	if((x($ev,'desc') || x($ev,'summary')) && x($ev,'start')) {
-		$sub = format_event_html($ev);
+		$sub = format_event_html($ev, $simplehtml);
 
 		$Text = preg_replace("/\[event\-summary\](.*?)\[\/event\-summary\]/ism",'',$Text);
 		$Text = preg_replace("/\[event\-description\](.*?)\[\/event\-description\]/ism",'',$Text);
diff --git a/include/event.php b/include/event.php
index d56388a77a..c4111dc0b1 100644
--- a/include/event.php
+++ b/include/event.php
@@ -3,7 +3,7 @@
 require_once('include/bbcode.php');
 require_once('include/map.php');
 
-function format_event_html($ev) {
+function format_event_html($ev, $simple = false) {
 
 
 
@@ -12,6 +12,32 @@ function format_event_html($ev) {
 
 	$bd_format = t('l F d, Y \@ g:i A') ; // Friday January 18, 2011 @ 8 AM
 
+	$event_start = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
+			$ev['start'] , $bd_format ))
+			:  day_translate(datetime_convert('UTC', 'UTC',
+			$ev['start'] , $bd_format)));
+
+	$event_end = (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
+				$ev['finish'] , $bd_format ))
+				:  day_translate(datetime_convert('UTC', 'UTC',
+				$ev['finish'] , $bd_format )));
+
+	if ($simple) {
+		$o = "<h3>".bbcode($ev['summary'])."</h3>";
+
+		$o .= "<p>".bbcode($ev['desc'])."</p>";
+
+		$o .= "<h4>".t('Starts:')."</h4><p>".$event_start."</p>";
+
+		if(! $ev['nofinish'])
+			$o .= "<h4>".t('Finishes:')."</h4><p>".$event_end."</p>";
+
+		if(strlen($ev['location']))
+			$o .= "<h4>".t('Location:')."</h4><p>".$ev['location']."</p>";
+
+		return $o;
+	}
+
 	$o = '<div class="vevent">' . "\r\n";
 
 
@@ -21,21 +47,13 @@ function format_event_html($ev) {
 
 	$o .= '<p class="event-start">' . t('Starts:') . ' <abbr class="dtstart" title="'
 		. datetime_convert('UTC','UTC',$ev['start'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
-		. '" >'
-		. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
-			$ev['start'] , $bd_format ))
-			:  day_translate(datetime_convert('UTC', 'UTC',
-			$ev['start'] , $bd_format)))
+		. '" >'.$event_start
 		. '</abbr></p>' . "\r\n";
 
 	if(! $ev['nofinish'])
 		$o .= '<p class="event-end" >' . t('Finishes:') . ' <abbr class="dtend" title="'
 			. datetime_convert('UTC','UTC',$ev['finish'], (($ev['adjust']) ? ATOM_TIME : 'Y-m-d\TH:i:s' ))
-			. '" >'
-			. (($ev['adjust']) ? day_translate(datetime_convert('UTC', date_default_timezone_get(),
-				$ev['finish'] , $bd_format ))
-				:  day_translate(datetime_convert('UTC', 'UTC',
-				$ev['finish'] , $bd_format )))
+			. '" >'.$event_end
 			. '</abbr></p>'  . "\r\n";
 
 	if(strlen($ev['location'])){

From 8a79e1afb34cf8098bc0e34180bb3b17b826c824 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 17 Oct 2015 10:48:13 +0200
Subject: [PATCH 8/8] The datepicker is now localised

---
 include/datetime.php | 75 ++++++++++++++++++++++++++------------------
 1 file changed, 45 insertions(+), 30 deletions(-)

diff --git a/include/datetime.php b/include/datetime.php
index 6461298ba2..79964ef404 100644
--- a/include/datetime.php
+++ b/include/datetime.php
@@ -43,18 +43,18 @@ function select_timezone($current = 'America/Los_Angeles') {
 			if($continent != t('Miscellaneous')) {
 				$o .= '</optgroup>';
 				$continent = t('Miscellaneous');
-				$o .= '<optgroup label="' . t($continent) . '">';	
+				$o .= '<optgroup label="' . t($continent) . '">';
 			}
 		}
 		$city = str_replace('_', ' ',  t($city));
 		$selected = (($value == $current) ? " selected=\"selected\" " : "");
 		$o .= "<option value=\"$value\" $selected >$city</option>";
-	}    
+	}
 	$o .= '</optgroup></select>';
 	return $o;
 }}
 
-// return a select using 'field_select_raw' template, with timezones 
+// return a select using 'field_select_raw' template, with timezones
 // groupped (primarily) by continent
 // arguments follow convetion as other field_* template array:
 // 'name', 'label', $value, 'help'
@@ -63,12 +63,12 @@ function field_timezone($name='timezone', $label='', $current = 'America/Los_Ang
 	$options = select_timezone($current);
 	$options = str_replace('<select id="timezone_select" name="timezone">','', $options);
 	$options = str_replace('</select>','', $options);
-	
+
 	$tpl = get_markup_template('field_select_raw.tpl');
 	return replace_macros($tpl, array(
 		'$field' => array($name, $label, $current, $help, $options),
 	));
-	
+
 }}
 
 // General purpose date parse/convert function.
@@ -92,8 +92,8 @@ function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d
 
 	// Slight hackish adjustment so that 'zero' datetime actually returns what is intended
 	// otherwise we end up with -0001-11-30 ...
-	// add 32 days so that we at least get year 00, and then hack around the fact that 
-	// months and days always start with 1. 
+	// add 32 days so that we at least get year 00, and then hack around the fact that
+	// months and days always start with 1.
 
 	if(substr($s,0,10) == '0000-00-00') {
 		$d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
@@ -203,13 +203,25 @@ function timesel($format, $h, $m, $id='timepicker') {
  *  set maximum date from picker with id $maxfrom (none by default)
  * @param boolean $required default false
  * @return string Parsed HTML output.
- * 
+ *
  * @todo Once browser support is better this could probably be replaced with
  * native HTML5 date picker.
  */
 if(! function_exists('datetimesel')) {
 function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pickdate = true, $picktime = true, $minfrom = '', $maxfrom = '', $required = false) {
 
+	$a = get_app();
+
+	// First day of the week (0 = Sunday)
+	$firstDay = get_pconfig(local_user(),'system','first_day_of_week');
+	if ($firstDay === false) $firstDay=0;
+
+	$lang = substr(get_browser_language(), 0, 2);
+
+	// Check if the detected language is supported by the picker
+	if (!in_array($lang, array("ar", "ro", "id", "bg", "fa", "ru", "uk", "en", "el", "de", "nl", "tr", "fr", "es", "th", "pl", "pt", "ch", "se", "kr", "it", "da", "no", "ja", "vi", "sl", "cs", "hu")))
+		$lang = ((isset($a->config['system']['language'])) ? $a->config['system']['language'] : 'en');
+
 	$o = '';
 	$dateformat = '';
 	if($pickdate) $dateformat .= 'Y-m-d';
@@ -217,16 +229,17 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
 	if($picktime) $dateformat .= 'H:i';
 	$minjs = $min ? ",minDate: new Date({$min->getTimestamp()}*1000), yearStart: " . $min->format('Y') : '';
 	$maxjs = $max ? ",maxDate: new Date({$max->getTimestamp()}*1000), yearEnd: " . $max->format('Y') : '';
-	
+
 	$input_text = $default ? 'value="' . date($dateformat, $default->getTimestamp()) . '"' : '';
 	$defaultdatejs = $default ? ",defaultDate: new Date({$default->getTimestamp()}*1000)" : '';
 	$pickers = '';
 	if(!$pickdate) $pickers .= ',datepicker: false';
 	if(!$picktime) $pickers .= ',timepicker: false';
 	$extra_js = '';
-	if($minfrom != '') 
+	$pickers .= ",dayOfWeekStart: ".$firstDay.",lang:'".$lang."'";
+	if($minfrom != '')
 		$extra_js .= "\$('#$minfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#$id').data('xdsoft_datetimepicker').setOptions({minDate: currentDateTime})}})";
-	if($maxfrom != '') 
+	if($maxfrom != '')
 		$extra_js .= "\$('#$maxfrom').data('xdsoft_datetimepicker').setOptions({onChangeDateTime: function (currentDateTime) { \$('#$id').data('xdsoft_datetimepicker').setOptions({maxDate: currentDateTime})}})";
 	$readable_format = $dateformat;
 	$readable_format = str_replace('Y','yyyy',$readable_format);
@@ -236,7 +249,9 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
 	$readable_format = str_replace('i','MM',$readable_format);
 	$o .= "<div class='date'><input type='text' placeholder='$readable_format' name='$id' id='$id' $input_text />";
 	$o .= '</div>';
-	$o .= "<script type='text/javascript'>\$(function () {var picker = \$('#$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})</script>";
+	$o .= "<script type='text/javascript'>";
+	$o .= "\$(function () {var picker = \$('#$id').datetimepicker({step:5,format:'$dateformat' $minjs $maxjs $pickers $defaultdatejs}); $extra_js})";
+	$o .= "</script>";
 	return $o;
 }}
 
@@ -248,27 +263,27 @@ function datetimesel($format, $min, $max, $default, $id = 'datetimepicker', $pic
 if(! function_exists('relative_date')) {
 function relative_date($posted_date,$format = null) {
 
-	$localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
+	$localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date);
 
 	$abs = strtotime($localtime);
-    
-    if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
+
+	if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
 		 return t('never');
 	}
 
 	$etime = time() - $abs;
-    
+
 	if ($etime < 1) {
 		return t('less than a second ago');
 	}
-    
+
 	/*
 	$time_append = '';
 	if ($etime >= 86400) {
 		$time_append = ' ('.$localtime.')';
 	}
 	*/
-	
+
 	$a = array( 12 * 30 * 24 * 60 * 60  =>  array( t('year'),   t('years')),
 				30 * 24 * 60 * 60       =>  array( t('month'),  t('months')),
 				7  * 24 * 60 * 60       =>  array( t('week'),   t('weeks')),
@@ -277,7 +292,7 @@ function relative_date($posted_date,$format = null) {
 				60                      =>  array( t('minute'), t('minutes')),
 				1                       =>  array( t('second'), t('seconds'))
 	);
-    
+
 	foreach ($a as $secs => $str) {
 		$d = $etime / $secs;
 		if ($d >= 1) {
@@ -295,13 +310,13 @@ function relative_date($posted_date,$format = null) {
 // Returns age in years, given a date of birth,
 // the timezone of the person whose date of birth is provided,
 // and the timezone of the person viewing the result.
-// Why? Bear with me. Let's say I live in Mittagong, Australia, and my 
+// Why? Bear with me. Let's say I live in Mittagong, Australia, and my
 // birthday is on New Year's. You live in San Bruno, California.
 // When exactly are you going to see my age increase?
-// A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start 
-// celebrating and become a year older. If you wish me happy birthday 
-// on January 1 (San Bruno time), you'll be a day late. 
-   
+// A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start
+// celebrating and become a year older. If you wish me happy birthday
+// on January 1 (San Bruno time), you'll be a day late.
+
 function age($dob,$owner_tz = '',$viewer_tz = '') {
 	if(! intval($dob))
 		return 0;
@@ -357,7 +372,7 @@ function get_first_dim($y,$m) {
 
 // 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 
+// date 12 will be linked appropriately. Today's date is also noted by
 // altering td class.
 // Months count from 1.
 
@@ -376,7 +391,7 @@ function cal($y = 0,$m = 0, $links = false, $class='') {
 	  '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');
@@ -427,8 +442,8 @@ function cal($y = 0,$m = 0, $links = false, $class='') {
   if($dow)
     for($a = $dow; $a < 7; $a ++)
        $o .= '<td>&nbsp;</td>';
-  $o .= '</tr></table>'."\r\n";  
-  
+  $o .= '</tr></table>'."\r\n";
+
   return $o;
 }}
 
@@ -452,10 +467,10 @@ function update_contact_birthdays() {
 			 *
 			 * $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. 
+			 * 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]') ;