]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Features/Socials.php
Social networks/contact externalization
[galette.git] / galette / lib / Galette / Features / Socials.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Socials feature
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 https://galette.eu
34 * @since 2021-10-25
35 */
36
37 namespace Galette\Features;
38
39 use Galette\Entity\Adherent;
40 use Galette\Entity\Social;
41
42 /**
43 * Replacements feature
44 *
45 * @category Features
46 * @name Replacements
47 * @package Galette
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2020-2021 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.eu
52 * @since 2020-12-20
53 */
54
55 trait Socials
56 {
57 protected $socials_input = [];
58
59 /**
60 * Check socials
61 *
62 * @param array $post User input
63 *
64 * @return void
65 */
66 protected function checkSocials(array $post)
67 {
68 $this->socials_input = [];
69 foreach ($post as $key => $value) {
70 if (str_starts_with($key, 'social_')) {
71 $this->socials_input[$key] = $value;
72 }
73 }
74 }
75
76 /**
77 * Store social networks/contacts
78 *
79 * @param int|null $id ID
80 *
81 * @return bool
82 */
83 protected function storeSocials(int $id = null): bool
84 {
85 $existings = Social::getListForMember($id);
86 foreach ($this->socials_input as $key => $value) {
87 if (
88 str_starts_with($key, 'social_new_type')
89 && !empty($value)
90 && isset($this->socials_input[str_replace('_type', '_value', $key)])
91 && !empty($this->socials_input[str_replace('_type', '_value', $key)])
92 ) {
93 //new social network
94 $new_index = (int)str_replace('social_new_type_', '', $key);
95 $social = new Social($this->zdb);
96 $social
97 ->setType($value)
98 ->setLinkedMember($id)
99 ->setUrl($this->socials_input['social_new_value_' . $new_index])
100 ->store();
101 } elseif (str_starts_with($key, 'social_') && !str_starts_with($key, 'social_new_')) {
102 //existing social network
103 $social_id = (int)str_replace('social_', '', $key);
104 $social = $existings[$social_id];
105 if ($value != $social->url) {
106 $social
107 ->setUrl($value)
108 ->store();
109 }
110 unset($existings[$social_id]);
111 }
112 }
113
114 if (count($existings)) {
115 $social = new Social($this->zdb);
116 $social->remove(array_keys($existings));
117 }
118
119 return true;
120 }
121
122 /**
123 * Get core registered types
124 * @return array
125 */
126 protected function getCoreRegisteredTypes(): array
127 {
128 return $this->getRegisteredTypes(true);
129 }
130
131 /**
132 * Get member registered types
133 *
134 * @return array
135 */
136 public function getMemberRegisteredTypes(): array
137 {
138 return $this->getRegisteredTypes(false);
139 }
140
141 /**
142 * Get registered types
143 *
144 * @param bool $core True for core type, false for members ones
145 *
146 * @return array
147 */
148 protected function getRegisteredTypes(bool $core): array
149 {
150 $select = $this->zdb->select(Social::TABLE, 's');
151 $select->quantifier('DISTINCT')->columns(['type']);
152 if ($core === true) {
153 $select->where(Adherent::PK . ' IS NULL');
154 } else {
155 $select->where(Adherent::PK . ' IS NOT NULL');
156 }
157
158 $results = $this->zdb->execute($select);
159 $types = [];
160 foreach ($results as $result) {
161 $types[$result->type] = $result->type;
162 }
163
164 return $types;
165 }
166 }