From de5ee56d2f8e19b7c53e32d59e37eeae31f47d0c Mon Sep 17 00:00:00 2001
From: Hypolite Petovan <hypolite@mrpetovan.com>
Date: Thu, 22 Nov 2018 00:15:09 -0500
Subject: [PATCH] Move DFRN::birthdayEvent() to Event::createBirthday

---
 src/Model/Event.php   | 114 ++++++++++++++++++++++++++++++------------
 src/Protocol/DFRN.php |  39 +--------------
 2 files changed, 82 insertions(+), 71 deletions(-)

diff --git a/src/Model/Event.php b/src/Model/Event.php
index 735638287..886f12415 100644
--- a/src/Model/Event.php
+++ b/src/Model/Event.php
@@ -322,43 +322,48 @@ class Event extends BaseObject
 			// New event. Store it.
 			DBA::insert('event', $event);
 
-			$event['id'] = DBA::lastInsertId();
+			$item_id = 0;
 
-			$item_arr = [];
+			// Don't create an item for birthday events
+			if ($event['type'] == 'event') {
+				$event['id'] = DBA::lastInsertId();
 
-			$item_arr['uid']           = $event['uid'];
-			$item_arr['contact-id']    = $event['cid'];
-			$item_arr['uri']           = $event['uri'];
-			$item_arr['parent-uri']    = $event['uri'];
-			$item_arr['guid']          = $event['guid'];
-			$item_arr['plink']         = defaults($arr, 'plink', '');
-			$item_arr['post-type']     = Item::PT_EVENT;
-			$item_arr['wall']          = $event['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']     = $event['allow_cid'];
-			$item_arr['allow_gid']     = $event['allow_gid'];
-			$item_arr['deny_cid']      = $event['deny_cid'];
-			$item_arr['deny_gid']      = $event['deny_gid'];
-			$item_arr['private']       = $private;
-			$item_arr['visible']       = 1;
-			$item_arr['verb']          = ACTIVITY_POST;
-			$item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
-			$item_arr['origin']        = $event['cid'] === 0 ? 1 : 0;
-			$item_arr['body']          = self::getBBCode($event);
-			$item_arr['event-id']      = $event['id'];
+				$item_arr = [];
 
-			$item_arr['object']  = '<object><type>' . XML::escape(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
-			$item_arr['object'] .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
-			$item_arr['object'] .= '</object>' . "\n";
+				$item_arr['uid']           = $event['uid'];
+				$item_arr['contact-id']    = $event['cid'];
+				$item_arr['uri']           = $event['uri'];
+				$item_arr['parent-uri']    = $event['uri'];
+				$item_arr['guid']          = $event['guid'];
+				$item_arr['plink']         = defaults($arr, 'plink', '');
+				$item_arr['post-type']     = Item::PT_EVENT;
+				$item_arr['wall']          = $event['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']     = $event['allow_cid'];
+				$item_arr['allow_gid']     = $event['allow_gid'];
+				$item_arr['deny_cid']      = $event['deny_cid'];
+				$item_arr['deny_gid']      = $event['deny_gid'];
+				$item_arr['private']       = $private;
+				$item_arr['visible']       = 1;
+				$item_arr['verb']          = ACTIVITY_POST;
+				$item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
+				$item_arr['origin']        = $event['cid'] === 0 ? 1 : 0;
+				$item_arr['body']          = self::getBBCode($event);
+				$item_arr['event-id']      = $event['id'];
 
-			$item_id = Item::insert($item_arr);
+				$item_arr['object']  = '<object><type>' . XML::escape(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
+				$item_arr['object'] .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
+				$item_arr['object'] .= '</object>' . "\n";
+
+				$item_id = Item::insert($item_arr);
+			}
 
 			Addon::callHooks("event_created", $event['id']);
 		}
@@ -981,4 +986,47 @@ class Event extends BaseObject
 
 		return $location;
 	}
+
+	/**
+	 * @brief Add new birthday event for this person
+	 *
+	 * @param array  $contact  Contact array, expects: id, uid, url, name
+	 * @param string $birthday Birthday of the contact
+	 * @return bool
+	 */
+	public static function createBirthday($contact, $birthday)
+	{
+		// Check for duplicates
+		$condition = [
+			'uid' => $contact['uid'],
+			'cid' => $contact['id'],
+			'start' => DateTimeFormat::utc($birthday),
+			'type' => 'birthday'
+		];
+		if (DBA::exists('event', $condition)) {
+			return false;
+		}
+
+		/*
+		 * Add new birthday event for this person
+		 *
+		 * summary 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.
+		 */
+		$values = [
+			'uid'     => $contact['uid'],
+			'cid'     => $contact['id'],
+			'start'   => DateTimeFormat::utc($birthday),
+			'finish'  => DateTimeFormat::utc($birthday . ' + 1 day '),
+			'summary' => L10n::t('%s\'s birthday', $contact['name']),
+			'desc'    => L10n::t('Happy Birthday %s', ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]'),
+			'type'    => 'birthday',
+			'adjust'  => 0
+		];
+
+		self::store($values);
+
+		return true;
+	}
 }
diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php
index 7c03a2183..b67b7c021 100644
--- a/src/Protocol/DFRN.php
+++ b/src/Protocol/DFRN.php
@@ -1509,43 +1509,6 @@ class DFRN
 		return intval($res->status);
 	}
 
-	/**
-	 * @brief Add new birthday event for this person
-	 *
-	 * @param array  $contact  Contact record
-	 * @param string $birthday Birthday of the contact
-	 * @return void
-	 * @todo Add array type-hint for $contact
-	 */
-	private static function birthdayEvent($contact, $birthday)
-	{
-		// Check for duplicates
-		$condition = ['uid' => $contact['uid'], 'cid' => $contact['id'],
-			'start' => DateTimeFormat::utc($birthday), 'type' => 'birthday'];
-		if (DBA::exists('event', $condition)) {
-			return;
-		}
-
-		Logger::log('updating birthday: ' . $birthday . ' for contact ' . $contact['id']);
-
-		$bdtext = L10n::t('%s\'s birthday', $contact['name']);
-		$bdtext2 = L10n::t('Happy Birthday %s', ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
-
-		$r = q(
-			"INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
-			VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
-			intval($contact['uid']),
-			intval($contact['id']),
-			DBA::escape(DateTimeFormat::utcNow()),
-			DBA::escape(DateTimeFormat::utcNow()),
-			DBA::escape(DateTimeFormat::utc($birthday)),
-			DBA::escape(DateTimeFormat::utc($birthday . ' + 1 day ')),
-			DBA::escape($bdtext),
-			DBA::escape($bdtext2),
-			DBA::escape('birthday')
-		);
-	}
-
 	/**
 	 * @brief Fetch the author data from head or entry items
 	 *
@@ -1736,7 +1699,7 @@ class DFRN
 			$contact = array_merge($contact_old, $poco);
 
 			if ($contact_old["bdyear"] != $contact["bdyear"]) {
-				self::birthdayEvent($contact, $birthday);
+				Event::createBirthday($contact, $birthday);
 			}
 
 			// Get all field names