3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Social networks/Contacts
10 * Copyright © 2021 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
37 namespace Galette\Entity
;
39 use Galette\Core\GaletteMail
;
40 use Galette\Features\I18n
;
41 use Laminas\Db\ResultSet\ResultSet
;
42 use Laminas\Db\Sql\Expression
;
48 * Social networks/Contacts
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
64 public const TABLE
= 'socials';
65 public const PK
= 'id_social';
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';
93 * @param Db $zdb Database instance
94 * @param mixed $args Arguments
96 public function __construct(Db
$zdb, $args = null)
101 } elseif (is_object($args)) {
102 $this->loadFromRs($args);
107 * Load a social from its identifier
109 * @param integer $id Identifier
113 private function load(int $id): void
116 $select = $this->zdb
->select(self
::TABLE
);
117 $select->limit(1)->where(self
::PK
. ' = ' . $id);
119 $results = $this->zdb
->execute($select);
120 $res = $results->current();
121 $this->loadFromRs($res);
122 } catch (Throwable
$e) {
124 'An error occurred loading social #' . $id . "Message:\n" .
132 * Get socials for a member
134 * @param int|null $id_adh Member id
135 * @param string|null $type Type to retrieve
141 public static function getListForMember(int $id_adh = null, string $type = null): array
146 $select = $zdb->select(self
::TABLE
);
148 if ($id_adh === null) {
149 $select->where(Adherent
::PK
. ' IS NULL');
151 $select->where([Adherent
::PK
=> $id_adh]);
154 if ($type !== null) {
155 $select->where(['type' => $type]);
158 $select->order(self
::PK
);
160 $results = $zdb->execute($select);
162 foreach ($results as $r) {
163 $socials[$r->{self
::PK
}] = new Social($zdb, $r);
166 } catch (Throwable
$e) {
168 'An error occurred loading socials for member #' . $id_adh . "Message:\n" .
177 * Load social from a db ResultSet
179 * @param ResultSet $rs ResultSet
183 private function loadFromRs($rs)
185 $this->id
= $rs->{self
::PK
};
186 $this->setLinkedMember((int)$rs->{Adherent
::PK
});
187 $this->type
= $rs->type
;
188 $this->url
= $rs->url
;
192 * Store social in database
196 public function store(): bool
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
204 $this->zdb
->execute($update);
206 $insert = $this->zdb
->insert(self
::TABLE
);
207 $id_adh = $this->{Adherent
::PK
} > 0 ?
$this->{Adherent
::PK
} : new Expression('NULL');
209 'type' => $this->type
,
211 Adherent
::PK
=> $id_adh
213 $add = $this->zdb
->execute($insert);
214 if (!$add->count() > 0) {
215 Analog
::log('Not stored!', Analog
::ERROR
);
219 $this->id
= $this->zdb
->getLastGeneratedValue($this);
220 if (!in_array($this->type
, $this->getSystemTypes(false))) {
221 $this->addTranslation($this->type
);
225 } catch (Throwable
$e) {
227 'An error occurred storing social: ' . $e->getMessage(),
235 * Remove current social
237 * @param array|null $ids IDs to remove, default to current id
241 public function remove(array $ids = null): bool
248 $delete = $this->zdb
->delete(self
::TABLE
);
249 $delete->where([self
::PK
=> $ids]);
250 $this->zdb
->execute($delete);
252 'Social #' . implode(', #', $ids) . ' deleted successfully.',
256 } catch (Throwable
$e) {
258 'Unable to delete social #' . implode(', #', $ids) . ' | ' . $e->getMessage(),
268 * @param string $name Property name
272 public function __get(string $name)
278 * Display URL the best way
282 public function displayUrl(): string
284 if (isValidWebUrl($this->url
)) {
285 return sprintf('<a href="%1$s">%1$s</a>', $this->url
);
288 if (GaletteMail
::isValidEmail($this->url
)) {
289 return sprintf('<a href="mailto:%1$s">%1$s</a>', $this->url
);
298 * @param string $type Type
302 public function setType(string $type): self
311 * @param int|null $id Member id
315 public function setLinkedMember(int $id = null): self
317 $this->{Adherent
::PK
} = $id;
318 if ($this->{Adherent
::PK
} > 0) {
319 $this->member
= new Adherent($this->zdb
, $this->{Adherent
::PK
});
327 * @param string $url Value to set
331 public function setUrl(string $url): self
338 * Get system social types
340 * @param boolean $translated Return translated types (default) or not
344 public function getSystemTypes(bool $translated = true): array
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')
360 self
::MASTODON
=> 'mastodon',
361 self
::TWITTER
=> 'twitter',
362 self
::FACEBOOK
=> 'facebook',
363 self
::LINKEDIN
=> 'linkedin',
364 self
::VIADEO
=> 'viadeo',
365 self
::JABBER
=> 'jabber',
367 self
::WEBSITE
=> 'website',
375 * Get system social types
377 * @param string $type Social type
378 * @param boolean $translated Return translated types (default) or not
382 public function getSystemType(string $type, bool $translated = true): string
384 return $this->getSystemTypes($translated)[$type] ??
_T($type);