+ // The event changed. Update it.
+
+ $r = q("UPDATE `event` SET
+ `edited` = '%s',
+ `start` = '%s',
+ `finish` = '%s',
+ `summary` = '%s',
+ `desc` = '%s',
+ `location` = '%s',
+ `type` = '%s',
+ `adjust` = %d,
+ `nofinish` = %d
+ WHERE `id` = %d AND `uid` = %d",
+
+ dbesc($arr['edited']),
+ dbesc($arr['start']),
+ dbesc($arr['finish']),
+ dbesc($arr['summary']),
+ dbesc($arr['desc']),
+ dbesc($arr['location']),
+ dbesc($arr['type']),
+ intval($arr['adjust']),
+ intval($arr['nofinish']),
+ intval($arr['id']),
+ intval($arr['uid'])
+ );
+ $r = q("SELECT * FROM `item` WHERE `event-id` = %d AND `uid` = %d LIMIT 1",
+ intval($arr['id']),
+ intval($arr['uid'])
+ );
+ if(count($r)) {
+ $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
+ $object .= '<content>' . xmlify(format_event_bbcode($arr)) . '</content>';
+ $object .= '</object>' . "\n";
+
+
+ q("UPDATE `item` SET `body` = '%s', `object` = '%s', `edited` = '%s' WHERE `id` = %d AND `uid` = %d",
+ dbesc(format_event_bbcode($arr)),
+ dbesc($object),
+ dbesc($arr['edited']),
+ intval($r[0]['id']),
+ intval($arr['uid'])
+ );
+
+ $item_id = $r[0]['id'];
+ } else
+ $item_id = 0;
+
+ call_hooks("event_updated", $arr['id']);
+
+ return $item_id;
+ }
+ else {
+
+ // 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`)
+ VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s' ) ",
+ intval($arr['uid']),
+ intval($arr['cid']),
+ dbesc($arr['uri']),
+ dbesc($arr['created']),
+ dbesc($arr['edited']),
+ dbesc($arr['start']),
+ dbesc($arr['finish']),
+ dbesc($arr['summary']),
+ dbesc($arr['desc']),
+ dbesc($arr['location']),
+ dbesc($arr['type']),
+ intval($arr['adjust']),
+ intval($arr['nofinish']),
+ dbesc($arr['allow_cid']),
+ dbesc($arr['allow_gid']),
+ dbesc($arr['deny_cid']),
+ dbesc($arr['deny_gid'])
+
+ );
+
+ $r = q("SELECT * FROM `event` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($arr['uri']),
+ intval($arr['uid'])
+ );
+ if(count($r))
+ $event = $r[0];
+
+ $item_arr = array();
+
+ $item_arr['uid'] = $arr['uid'];
+ $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'];
+ $item_arr['owner-name'] = $contact['name'];
+ $item_arr['owner-link'] = $contact['url'];
+ $item_arr['owner-avatar'] = $contact['thumb'];
+ $item_arr['author-name'] = $contact['name'];
+ $item_arr['author-link'] = $contact['url'];
+ $item_arr['author-avatar'] = $contact['thumb'];
+ $item_arr['title'] = '';
+ $item_arr['allow_cid'] = $arr['allow_cid'];
+ $item_arr['allow_gid'] = $arr['allow_gid'];
+ $item_arr['deny_cid'] = $arr['deny_cid'];
+ $item_arr['deny_gid'] = $arr['deny_gid'];
+ $item_arr['private'] = $arr['private'];
+ $item_arr['last-child'] = 1;
+ $item_arr['visible'] = 1;
+ $item_arr['verb'] = ACTIVITY_POST;
+ $item_arr['object-type'] = ACTIVITY_OBJ_EVENT;
+ $item_arr['origin'] = ((intval($arr['cid']) == 0) ? 1 : 0);
+ $item_arr['body'] = format_event_bbcode($event);
+
+
+ $item_arr['object'] = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($arr['uri']) . '</id>';
+ $item_arr['object'] .= '<content>' . xmlify(format_event_bbcode($event)) . '</content>';
+ $item_arr['object'] .= '</object>' . "\n";
+
+ $item_id = item_store($item_arr);
+
+ $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
+ intval($arr['uid'])
+ );
+ //if(count($r))
+ // $plink = $a->get_baseurl() . '/display/' . $r[0]['nickname'] . '/' . $item_id;
+
+
+ if($item_id) {
+ //q("UPDATE `item` SET `plink` = '%s', `event-id` = %d WHERE `uid` = %d AND `id` = %d",
+ // dbesc($plink),
+ // intval($event['id']),
+ // intval($arr['uid']),
+ // intval($item_id)
+ //);
+ q("UPDATE `item` SET `event-id` = %d WHERE `uid` = %d AND `id` = %d",
+ intval($event['id']),
+ intval($arr['uid']),
+ intval($item_id)
+ );
+ }
+
+ call_hooks("event_created", $event['id']);
+
+ return $item_id;
+ }
+}
+
+function get_event_strings() {
+ // First day of the week (0 = Sunday)
+ $firstDay = get_pconfig(local_user(),'system','first_day_of_week');
+ if ($firstDay === false) $firstDay=0;
+
+ $i18n = array(
+ "firstDay" => $firstDay,
+ "Sun" => t("Sun"),
+ "Mon" => t("Mon"),
+ "Tue" => t("Tue"),
+ "Wed" => t("Wed"),
+ "Thu" => t("Thu"),
+ "Fri" => t("Fri"),
+ "Sat" => t("Sat"),
+ "Sunday" => t("Sunday"),
+ "Monday" => t("Monday"),
+ "Tuesday" => t("Tuesday"),
+ "Wednesday" => t("Wednesday"),
+ "Thursday" => t("Thursday"),
+ "Friday" => t("Friday"),
+ "Saturday" => t("Saturday"),
+ "Jan" => t("Jan"),
+ "Feb" => t("Feb"),
+ "Mar" => t("Mar"),
+ "Apr" => t("Apr"),
+ "May" => t("May"),
+ "Jun" => t("Jun"),
+ "Jul" => t("Jul"),
+ "Aug" => t("Aug"),
+ "Sep" => t("Sept"),
+ "Oct" => t("Oct"),
+ "Nov" => t("Nov"),
+ "Dec" => t("Dec"),
+ "January" => t("January"),
+ "February" => t("February"),
+ "March" => t("March"),
+ "April" => t("April"),
+ "May" => t("May"),
+ "June" => t("June"),
+ "July" => t("July"),
+ "August" => t("August"),
+ "September" => t("September"),
+ "October" => t("October"),
+ "November" => t("November"),
+ "December" => t("December"),
+ "today" => t("today"),
+ "month" => t("month"),
+ "week" => t("week"),
+ "day" => t("day"),
+ "allday" => t("all-day"),
+
+ "noevent" => t("No events to display"),
+
+ "dtstart_label" => t("Starts:"),
+ "dtend_label" => t("Finishes:"),
+ "location_label" => t("Location:")
+ );
+
+ return $i18n;
+}
+
+/**
+ * @brief Get an event by its event ID
+ *
+ * @param type $owner_uid The User ID of the owner of the event
+ * @param type $event_params An assoziative array with
+ * int 'event_id' => The ID of the event in the event table
+ * @param type $sql_extra
+ * @return array Query result
+ */
+function event_by_id($owner_uid = 0, $event_params, $sql_extra = '') {
+ // ownly allow events if there is a valid owner_id
+ if($owner_uid == 0)
+ return;
+
+ // query for the event by event id
+ $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
+ `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
+ STRAIGHT_JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
+ WHERE `event`.`uid` = %d AND `event`.`id` = %d $sql_extra",
+ intval($owner_uid),
+ intval($event_params["event_id"])
+ );
+
+ if(count($r))
+ return $r;
+
+}
+
+/**
+ * @brief Get all events in a specific timeframe
+ *
+ * @param int $owner_uid The User ID of the owner of the events
+ * @param array $event_params An assoziative array with
+ * int 'ignored' =>
+ * string 'start' => Start time of the timeframe
+ * string 'finish' => Finish time of the timeframe
+ * string 'adjust_start' =>
+ * string 'adjust_start' =>
+ *
+ * @param string $sql_extra Additional sql conditions (e.g. permission request)
+ * @return array Query results
+ */
+function events_by_date($owner_uid = 0, $event_params, $sql_extra = '') {
+ // ownly allow events if there is a valid owner_id
+ if($owner_uid == 0)
+ return;
+
+ // query for the event by date
+ $r = q("SELECT `event`.*, `item`.`id` AS `itemid`,`item`.`plink`,
+ `item`.`author-name`, `item`.`author-avatar`, `item`.`author-link` FROM `event`
+ STRAIGHT_JOIN `item` ON `item`.`event-id` = `event`.`id` AND `item`.`uid` = `event`.`uid`
+ WHERE `event`.`uid` = %d AND event.ignore = %d
+ AND ((`adjust` = 0 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s')
+ OR (`adjust` = 1 AND (`finish` >= '%s' OR (nofinish AND start >= '%s')) AND `start` <= '%s'))
+ $sql_extra ",
+ intval($owner_uid),
+ intval($event_params["ignored"]),
+ dbesc($event_params["start"]),
+ dbesc($event_params["start"]),
+ dbesc($event_params["finish"]),
+ dbesc($event_params["adjust_start"]),
+ dbesc($event_params["adjust_start"]),
+ dbesc($event_params["adjust_finish"])
+ );
+
+ if(count($r))
+ return $r;