Overview
Comment:Add list of siblings on user profile
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA3-256: d86dac61e78401c628b696a22d94c95827832e2ce877fc3f0481fdbe51a41525
User & Date: bohwaz on 2022-08-07 01:56:43
Other Links: branch diff | manifest | tags
Context
2022-08-07
02:21
Fix issues with user search, add search for user, and search for children check-in: 8252183fe4 user: bohwaz tags: dev
01:56
Add list of siblings on user profile check-in: d86dac61e7 user: bohwaz tags: dev
01:56
Make sure upgrade cannot start if database does not exist check-in: 9d0c21fc6c user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Entities/Users/User.php from [896b22680e] to [b1659fab5f].

1

2
3
4
5
6
7
8
<?php


namespace Garradin\Entities\Users;

use KD2\DB\EntityManager;

use Garradin\Entity;
use Garradin\DB;

>







1
2
3
4
5
6
7
8
9
<?php
declare(strict_types=1);

namespace Garradin\Entities\Users;

use KD2\DB\EntityManager;

use Garradin\Entity;
use Garradin\DB;
104
105
106
107
108
109
110

111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129


















130
131
132
133
134
135
136
			else {
				$login_exists = $db->test(self::TABLE, sprintf('%s = ? COLLATE NOCASE AND id != ?', $db->quoteIdentifier($field)), $this->$field, $this->id());
			}

			$this->assert(!$login_exists, sprintf('Le champ "%s" (utilisé comme identifiant de connexion) est déjà utilisé par un autre membre. Il doit être unique pour chaque membre.', $df->fieldByKey($field)->label));
		}


		$this->assert(!$this->id_parent || !count($this->listChildren()), 'Un membre ne peut avoir un parent et des enfants en même temps.');
	}

	public function delete(): bool
	{
		$session = Session::getInstance();

		if ($session->isLogged()) {
			$user = $session->getUser();

			if ($user->id == $this->id) {
				throw new UserException('Il n\'est pas possible de supprimer son propre compte. Merci de demander à un administrateur de le faire.');
			}
		}

		Files::delete($this->attachementsDirectory());

		return parent::delete();
	}



















	public function category(): Category
	{
		return Categories::get($this->id_category);
	}

	public function attachementsDirectory(): string







>
|


















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
			else {
				$login_exists = $db->test(self::TABLE, sprintf('%s = ? COLLATE NOCASE AND id != ?', $db->quoteIdentifier($field)), $this->$field, $this->id());
			}

			$this->assert(!$login_exists, sprintf('Le champ "%s" (utilisé comme identifiant de connexion) est déjà utilisé par un autre membre. Il doit être unique pour chaque membre.', $df->fieldByKey($field)->label));
		}

		$this->assert($this->id_parent === null || $this->id_parent > 0, 'Invalid parent ID');
		$this->assert($this->id_parent === null || !count($this->listChildren()), 'Un membre ne peut avoir un parent et des enfants en même temps.');
	}

	public function delete(): bool
	{
		$session = Session::getInstance();

		if ($session->isLogged()) {
			$user = $session->getUser();

			if ($user->id == $this->id) {
				throw new UserException('Il n\'est pas possible de supprimer son propre compte. Merci de demander à un administrateur de le faire.');
			}
		}

		Files::delete($this->attachementsDirectory());

		return parent::delete();
	}

	public function save(bool $selfcheck = true): bool
	{
		parent::save($selfcheck);

		$columns = array_intersect(DynamicFields::getInstance()->getSearchColumns(), array_keys($this->_modified));

		if (count($columns)) {
			$db = EM::getInstance(self::class)->DB();
			$keys = array_map(fn ($a) => ':' . $db->quoteIdentifier($a), $columns);
			$args = substr(str_repeat('?, ', count($columns)), 0, -2);
			$values = array_intersect_key($this->modifiedProperties(true), array_flip($columns));
			$values = array_map([Utils::class, 'unicodeTransliterate'], $values);
			$db->preparedQuery(sprintf('REPLACE INTO %s_search (%s) VALUES (%s);', self::TABLE, $keys, $args), ...$values);
		}

		return true;
	}

	public function category(): Category
	{
		return Categories::get($this->id_category);
	}

	public function attachementsDirectory(): string
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214










215
216
217
218
219
220
221
		$email_field = DynamicFields::getFirstEmailField();

		return sprintf('"%s" <%s>', $this->name(), $this->{$email_field});
	}

	public function getParentName(): ?string
	{
		if (null === $this->id_parent) {
			return null;
		}

		return Users::getName($this->id_parent);
	}

	public function getParentSelector(): ?array
	{
		if (null === $this->id_parent) {
			return null;
		}

		return [$this->id_parent => $this->getParentName()];
	}

	public function hasChildren(): bool
	{
		return DB::getInstance()->test(self::TABLE, 'id_parent = ?', $this->id());
	}

	public function listChildren(): array
	{
		$name = DynamicFields::getNameFieldsSQL();
		return DB::getInstance()->getGrouped(sprintf('SELECT id, %s AS name FROM %s WHERE id_parent = ?;', $name, self::TABLE), $this->id());
	}











	public function sendMessage(string $subject, string $message, bool $send_copy, ?User $from = null)
	{
		$config = Config::getInstance();
		$email_field = DynamicFields::getFirstEmailField();

		$from = $from ? $from->getNameAndEmail() : null;







|








|
















>
>
>
>
>
>
>
>
>
>







202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		$email_field = DynamicFields::getFirstEmailField();

		return sprintf('"%s" <%s>', $this->name(), $this->{$email_field});
	}

	public function getParentName(): ?string
	{
		if (!$this->id_parent) {
			return null;
		}

		return Users::getName($this->id_parent);
	}

	public function getParentSelector(): ?array
	{
		if (!$this->id_parent) {
			return null;
		}

		return [$this->id_parent => $this->getParentName()];
	}

	public function hasChildren(): bool
	{
		return DB::getInstance()->test(self::TABLE, 'id_parent = ?', $this->id());
	}

	public function listChildren(): array
	{
		$name = DynamicFields::getNameFieldsSQL();
		return DB::getInstance()->getGrouped(sprintf('SELECT id, %s AS name FROM %s WHERE id_parent = ?;', $name, self::TABLE), $this->id());
	}

	public function listSiblings(): array
	{
		if (!$this->id_parent) {
			return [];
		}

		$name = DynamicFields::getNameFieldsSQL();
		return DB::getInstance()->getGrouped(sprintf('SELECT id, %s AS name FROM %s WHERE id_parent = ? AND id != ?;', $name, self::TABLE), $this->id_parent, $this->id());
	}

	public function sendMessage(string $subject, string $message, bool $send_copy, ?User $from = null)
	{
		$config = Config::getInstance();
		$email_field = DynamicFields::getFirstEmailField();

		$from = $from ? $from->getNameAndEmail() : null;

Modified src/templates/users/details.tpl from [99b7c4e1e1] to [13225465f9].

54
55
56
57
58
59
60






61
62
63
64
65
66
67
	<dd>{link href="?id=%d"|args:$user.id_parent label=$parent_name}</dd>
	{/if}
	{if count($children)}
	<dt>Membres enfants</dt>
	{foreach from=$children item="child"}
		<dd>{link href="?id=%d"|args:$child.id label=$child.name}</dd>
	{/foreach}






	{/if}
</dl>

<aside class="describe">
	<dl class="describe">
		<dt>Catégorie</dt>
		<dd>{$category.name}</dd>







>
>
>
>
>
>







54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	<dd>{link href="?id=%d"|args:$user.id_parent label=$parent_name}</dd>
	{/if}
	{if count($children)}
	<dt>Membres enfants</dt>
	{foreach from=$children item="child"}
		<dd>{link href="?id=%d"|args:$child.id label=$child.name}</dd>
	{/foreach}
	{/if}
	{if count($siblings)}
	<dt>Membres de la même famille</dt>
	{foreach from=$siblings item="sibling"}
		<dd>{link href="?id=%d"|args:$sibling.id label=$sibling.name}</dd>
	{/foreach}
	{/if}
</dl>

<aside class="describe">
	<dl class="describe">
		<dt>Catégorie</dt>
		<dd>{$category.name}</dd>

Modified src/www/admin/users/details.php from [f76b2aa137] to [843cbc3047].

21
22
23
24
25
26
27

28
29
30
31
if ($session->canAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ)) {
	$tpl->assign('transactions_linked', Transactions::countForUser($user->id));
	$tpl->assign('transactions_created', Transactions::countForCreator($user->id));
}

$parent_name = $user->getParentName();
$children = $user->listChildren();


$tpl->assign(compact('services', 'user', 'category', 'children', 'parent_name'));

$tpl->display('users/details.tpl');







>

|


21
22
23
24
25
26
27
28
29
30
31
32
if ($session->canAccess($session::SECTION_ACCOUNTING, $session::ACCESS_READ)) {
	$tpl->assign('transactions_linked', Transactions::countForUser($user->id));
	$tpl->assign('transactions_created', Transactions::countForCreator($user->id));
}

$parent_name = $user->getParentName();
$children = $user->listChildren();
$siblings = $user->listSiblings();

$tpl->assign(compact('services', 'user', 'category', 'children', 'siblings', 'parent_name'));

$tpl->display('users/details.tpl');