]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Social.php
1084bb3e171385601f3f6703b72b2ec9dd58f5be
[galette.git] / galette / lib / Galette / Entity / Social.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Social networks/Contacts
7 *
8 * PHP version 5
9 *
10 * Copyright © 2021 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2021 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.9.6dev - 2021-10-23
35 */
36
37 namespace Galette\Entity;
38
39 use Galette\Core\GaletteMail;
40 use Galette\Features\I18n;
41 use Laminas\Db\ResultSet\ResultSet;
42 use Laminas\Db\Sql\Expression;
43 use Throwable;
44 use Galette\Core\Db;
45 use Analog\Analog;
46
47 /**
48 * Social networks/Contacts
49 *
50 * @category Entity
51 * @name Social
52 * @package Galette
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2021 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.9.6dev - 2021-10-23
58 */
59
60 class Social
61 {
62 use I18n;
63
64 public const TABLE = 'socials';
65 public const PK = 'id_social';
66
67 public const MASTODON = 'mastodon';
68 public const TWITTER = 'twitter';
69 public const FACEBOOK = 'facebook';
70 public const LINKEDIN = 'linkedin';
71 public const VIADEO = 'viadeo';
72 public const JABBER = 'jabber';
73 public const ICQ = 'icq';
74 public const WEBSITE = 'website';
75 public const BLOG = 'blog';
76
77 /** @var Db */
78 private $zdb;
79 /** @var int */
80 private $id;
81 /** @var string */
82 private $type;
83 /** @var string */
84 private $url;
85 /** @var int */
86 private $id_adh;
87 /** @var Adherent */
88 private $member;
89
90 /**
91 * Main constructor
92 *
93 * @param Db $zdb Database instance
94 * @param mixed $args Arguments
95 */
96 public function __construct(Db $zdb, $args = null)
97 {
98 $this->zdb = $zdb;
99 if (is_int($args)) {
100 $this->load($args);
101 } elseif (is_object($args)) {
102 $this->loadFromRs($args);
103 }
104 }
105
106 /**
107 * Load a social from its identifier
108 *
109 * @param integer $id Identifier
110 *
111 * @return void
112 */
113 private function load(int $id): void
114 {
115 try {
116 $select = $this->zdb->select(self::TABLE);
117 $select->limit(1)->where(self::PK . ' = ' . $id);
118
119 $results = $this->zdb->execute($select);
120 $res = $results->current();
121 $this->loadFromRs($res);
122 } catch (Throwable $e) {
123 Analog::log(
124 'An error occurred loading social #' . $id . "Message:\n" .
125 $e->getMessage(),
126 Analog::ERROR
127 );
128 }
129 }
130
131 /**
132 * Get socials for a member
133 *
134 * @param int|null $id_adh Member id
135 * @param string|null $type Type to retrieve
136 *
137 * @return array
138 *
139 * @throws Throwable
140 */
141 public static function getListForMember(int $id_adh = null, string $type = null): array
142 {
143 global $zdb;
144
145 try {
146 $select = $zdb->select(self::TABLE);
147
148 if ($id_adh === null) {
149 $select->where(Adherent::PK . ' IS NULL');
150 } else {
151 $select->where([Adherent::PK => $id_adh]);
152 }
153
154 if ($type !== null) {
155 $select->where(['type' => $type]);
156 }
157
158 $select->order(self::PK);
159
160 $results = $zdb->execute($select);
161 $socials = [];
162 foreach ($results as $r) {
163 $socials[$r->{self::PK}] = new Social($zdb, $r);
164 }
165 return $socials;
166 } catch (Throwable $e) {
167 Analog::log(
168 'An error occurred loading socials for member #' . $id_adh . "Message:\n" .
169 $e->getMessage(),
170 Analog::ERROR
171 );
172 throw $e;
173 }
174 }
175
176 /**
177 * Load social from a db ResultSet
178 *
179 * @param ResultSet $rs ResultSet
180 *
181 * @return void
182 */
183 private function loadFromRs($rs)
184 {
185 $this->id = $rs->{self::PK};
186 $this->setLinkedMember((int)$rs->{Adherent::PK});
187 $this->type = $rs->type;
188 $this->url = $rs->url;
189 }
190
191 /**
192 * Store social in database
193 *
194 * @return boolean
195 */
196 public function store(): bool
197 {
198 try {
199 if ($this->id !== null && $this->id > 0) {
200 $update = $this->zdb->update(self::TABLE);
201 $update->set(['url' => $this->url])->where(
202 self::PK . '=' . $this->id
203 );
204 $this->zdb->execute($update);
205 } else {
206 $insert = $this->zdb->insert(self::TABLE);
207 $id_adh = $this->{Adherent::PK} > 0 ? $this->{Adherent::PK} : new Expression('NULL');
208 $insert->values([
209 'type' => $this->type,
210 'url' => $this->url,
211 Adherent::PK => $id_adh
212 ]);
213 $add = $this->zdb->execute($insert);
214 if (!$add->count() > 0) {
215 Analog::log('Not stored!', Analog::ERROR);
216 return false;
217 }
218
219 $this->id = $this->zdb->getLastGeneratedValue($this);
220 if (!in_array($this->type, $this->getSystemTypes(false))) {
221 $this->addTranslation($this->type);
222 }
223 }
224 return true;
225 } catch (Throwable $e) {
226 Analog::log(
227 'An error occurred storing social: ' . $e->getMessage(),
228 Analog::ERROR
229 );
230 throw $e;
231 }
232 }
233
234 /**
235 * Remove current social
236 *
237 * @param array|null $ids IDs to remove, default to current id
238 *
239 * @return boolean
240 */
241 public function remove(array $ids = null): bool
242 {
243 if ($ids == null) {
244 $ids[] = $this->id;
245 }
246
247 try {
248 $delete = $this->zdb->delete(self::TABLE);
249 $delete->where([self::PK => $ids]);
250 $this->zdb->execute($delete);
251 Analog::log(
252 'Social #' . implode(', #', $ids) . ' deleted successfully.',
253 Analog::INFO
254 );
255 return true;
256 } catch (Throwable $e) {
257 Analog::log(
258 'Unable to delete social #' . implode(', #', $ids) . ' | ' . $e->getMessage(),
259 Analog::ERROR
260 );
261 throw $e;
262 }
263 }
264
265 /**
266 * Getter
267 *
268 * @param string $name Property name
269 *
270 * @return mixed
271 */
272 public function __get(string $name)
273 {
274 return $this->$name;
275 }
276
277 /**
278 * Display URL the best way
279 *
280 * @return string
281 */
282 public function displayUrl(): string
283 {
284 if (isValidWebUrl($this->url)) {
285 return sprintf('<a href="%1$s">%1$s</a>', $this->url);
286 }
287
288 if (GaletteMail::isValidEmail($this->url)) {
289 return sprintf('<a href="mailto:%1$s">%1$s</a>', $this->url);
290 }
291
292 return $this->url;
293 }
294
295 /**
296 * Set type
297 *
298 * @param string $type Type
299 *
300 * @return $this
301 */
302 public function setType(string $type): self
303 {
304 $this->type = $type;
305 return $this;
306 }
307
308 /**
309 * Set linked member
310 *
311 * @param int|null $id Member id
312 *
313 * @return $this
314 */
315 public function setLinkedMember(int $id = null): self
316 {
317 $this->{Adherent::PK} = $id;
318 if ($this->{Adherent::PK} > 0) {
319 $this->member = new Adherent($this->zdb, $this->{Adherent::PK});
320 }
321 return $this;
322 }
323
324 /**
325 * Set URL
326 *
327 * @param string $url Value to set
328 *
329 * @return $this
330 */
331 public function setUrl(string $url): self
332 {
333 $this->url = $url;
334 return $this;
335 }
336
337 /**
338 * Get system social types
339 *
340 * @param boolean $translated Return translated types (default) or not
341 *
342 * @return array
343 */
344 public function getSystemTypes(bool $translated = true): array
345 {
346 if ($translated) {
347 $systypes = [
348 self::MASTODON => _T('Mastodon'),
349 self::TWITTER => _T('Twitter'),
350 self::FACEBOOK => _T('Facebook'),
351 self::LINKEDIN => _T('LinkedIn'),
352 self::VIADEO => _T('Viadeo'),
353 self::JABBER => _T('Jabber'),
354 self::ICQ => _T('ICQ'),
355 self::WEBSITE => _T('Website'),
356 self::BLOG => _T('Blog')
357 ];
358 } else {
359 $systypes = [
360 self::MASTODON => 'mastodon',
361 self::TWITTER => 'twitter',
362 self::FACEBOOK => 'facebook',
363 self::LINKEDIN => 'linkedin',
364 self::VIADEO => 'viadeo',
365 self::JABBER => 'jabber',
366 self::ICQ => 'icq',
367 self::WEBSITE => 'website',
368 self::BLOG => 'blog'
369 ];
370 }
371 return $systypes;
372 }
373
374 /**
375 * Get system social types
376 *
377 * @param string $type Social type
378 * @param boolean $translated Return translated types (default) or not
379 *
380 * @return string
381 */
382 public function getSystemType(string $type, bool $translated = true): string
383 {
384 return $this->getSystemTypes($translated)[$type] ?? _T($type);
385 }
386 }