From fd53b541e568dd345787aa4e75d82da29d2b61ba Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Sun, 18 Feb 2018 13:19:47 +0000
Subject: [PATCH] Frontend to set the parent-uid field

---
 mod/delegate.php            | 75 ++++++++++++++++++++++++++++---------
 view/templates/delegate.tpl | 21 +++++++++--
 2 files changed, 75 insertions(+), 21 deletions(-)

diff --git a/mod/delegate.php b/mod/delegate.php
index 3152eb3708..a6a67c3b82 100644
--- a/mod/delegate.php
+++ b/mod/delegate.php
@@ -14,6 +14,24 @@ function delegate_init(App $a)
 	return settings_init($a);
 }
 
+function delegate_post(App $a)
+{
+	if (!local_user()) {
+		return;
+	}
+
+	if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
+		notice(L10n::t('Permission denied.') . EOL);
+		return;
+	}
+
+	check_form_security_token_redirectOnErr('/delegate', 'delegate');
+
+	$parent_uid = defaults($_POST, 'parent_user', 0);
+
+	dba::update('user', ['parent-uid' => $parent_uid], ['uid' => local_user()]);
+}
+
 function delegate_content(App $a)
 {
 	if (!local_user()) {
@@ -81,6 +99,7 @@ function delegate_content(App $a)
 	}
 
 	// find every contact who might be a candidate for delegation
+	$potentials = [];
 
 	$r = q("SELECT `nurl`
 		FROM `contact`
@@ -92,34 +111,56 @@ function delegate_content(App $a)
 		intval(local_user()),
 		dbesc(NETWORK_DFRN)
 	);
-	if (!DBM::is_result($r)) {
-		notice(L10n::t('No potential page delegates located.') . EOL);
-		return;
-	}
+	if (DBM::is_result($r)) {
+		$nicknames = [];
+		foreach ($r as $rr) {
+			$nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
+		}
 
-	$nicknames = [];
-	foreach ($r as $rr) {
-		$nicknames[] = "'" . dbesc(basename($rr['nurl'])) . "'";
+		$nicks = implode(',', $nicknames);
+
+		// get user records for all potential page delegates who are not already delegates or managers
+		$r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
+		if (DBM::is_result($r)) {
+			foreach ($r as $rr) {
+				if (!in_array($rr['uid'], $uids)) {
+					$potentials[] = $rr;
+				}
+			}
+		}
 	}
 
-	$potentials = [];
+	settings_init($a);
 
-	$nicks = implode(',', $nicknames);
+	$user = dba::selectFirst('user', ['parent-uid', 'email'], ['uid' => local_user()]);
 
-	// get user records for all potential page delegates who are not already delegates or managers
-	$r = q("SELECT `uid`, `username`, `nickname` FROM `user` WHERE `nickname` IN ($nicks)");
-	if (DBM::is_result($r)) {
-		foreach ($r as $rr) {
-			if (!in_array($rr['uid'], $uids)) {
-				$potentials[] = $rr;
+	$parent_user = null;
+
+	if (DBM::is_result($user)) {
+		if (!dba::exists('user', ['parent-uid' => local_user()])) {
+			$parent_uid = $user['parent-uid'];
+			$parents = [0 => L10n::t('No parent user')];
+
+			$fields = ['uid', 'username', 'nickname'];
+			$condition = ['email' => $user['email'], 'verified' => true, 'blocked' => false, 'parent-uid' => 0];
+			$parent_users = dba::select('user', $fields, $condition);
+			while ($parent = dba::fetch($parent_users)) {
+				if ($parent['uid'] != local_user()) {
+					$parents[$parent['uid']] = sprintf('%s (%s)', $parent['username'], $parent['nickname']);
+				}
 			}
+			$parent_user = ['parent_user', '', $parent_uid, '', $parents];
 		}
 	}
 
-	settings_init($a);
-
 	$o = replace_macros(get_markup_template('delegate.tpl'), [
+		'$form_security_token' => get_form_security_token('delegate'),
+		'$parent_header' => L10n::t('Parent User'),
+		'$parent_user' => $parent_user,
+		'$parent_desc' => L10n::t('Parent users have total control about this account, including the account settings. Please double check whom you give this access.'),
+		'$submit' => L10n::t('Save Settings'),
 		'$header' => L10n::t('Delegate Page Management'),
+		'$delegates_header' => L10n::t('Delegates'),
 		'$base' => System::baseUrl(),
 		'$desc' => L10n::t('Delegates are able to manage all aspects of this account/page except for basic account settings. Please do not delegate your personal account to anybody that you do not trust completely.'),
 		'$head_managers' => L10n::t('Existing Page Managers'),
diff --git a/view/templates/delegate.tpl b/view/templates/delegate.tpl
index 0897b8a577..2f791c48a3 100644
--- a/view/templates/delegate.tpl
+++ b/view/templates/delegate.tpl
@@ -1,10 +1,23 @@
-
 <h3>{{$header}}</h3>
 
+{{if $parent_user}}
+<h4>{{$parent_header}}</h4>
+<div id="delegate-parent-desc" class="delegate-parent-desc">{{$parent_desc}}</div>
+<div id="delegate-parent" class="delegate-parent">
+<form action="{{$baseurl}}/delegate" method="post">
+<input type='hidden' name='form_security_token' value='{{$form_security_token}}'>
+{{include file="field_select.tpl" field=$parent_user}}
+<div class="submit"><input type="submit" name="delegate" value="{{$submit|escape:'html'}}" /></div>
+</form>
+</div>
+{{/if}}
+
+<h4>{{$delegates_header}}</h4>
+
 <div id="delegate-desc" class="delegate-desc">{{$desc}}</div>
 
 {{if $managers}}
-<h3>{{$head_managers}}</h3>
+<h4>{{$head_managers}}</h4>
 
 {{foreach $managers as $x}}
 
@@ -20,7 +33,7 @@
 {{/if}}
 
 
-<h3>{{$head_delegates}}</h3>
+<h4>{{$head_delegates}}</h4>
 
 {{if $delegates}}
 {{foreach $delegates as $x}}
@@ -39,7 +52,7 @@
 <hr />
 
 
-<h3>{{$head_potentials}}</h3>
+<h4>{{$head_potentials}}</h4>
 {{if $potentials}}
 {{foreach $potentials as $x}}
 
-- 
2.39.5