3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Lists config handling
10 * Copyright © 2020-2023 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 2020-2023 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.4dev - 2020-05-13
37 namespace Galette\Entity
;
43 use Galette\Core\Login
;
44 use Galette\Core\Authentication
;
47 * Lists config class for galette:
48 * defines fields order and visibility
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2020-2023 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.4dev - 2020-05-13
59 class ListsConfig
extends FieldsConfig
61 protected $listed_fields = array();
64 * Fields that are not part of lists
68 private $non_list_elements = array(
77 * ACL mapping for list elements not present in form configuration
81 private $acl_mapping = array(
82 'list_adh_name' => 'nom_adh',
83 'list_adh_contribstatus' => 'id_statut'
87 * Prepare a field (required data, automation)
89 * @param ArrayObject $rset DB ResultSet row
93 protected function buildField(ArrayObject
$rset): array
95 $f = parent
::buildField($rset);
96 $f['list_position'] = (int)$rset->list_position
;
97 $f['list_visible'] = ($f['list_position'] >= 0);
102 * Create field array configuration,
103 * Several lists of fields are kept (visible, requireds, etc), build them.
107 protected function buildLists()
109 //Specific list fields does not have rights; fix this from mapping
110 //Cannot be done preparing fields, cannot be sure of the order it is processed
111 foreach ($this->acl_mapping
as $list_key => $field_key) {
112 $this->core_db_fields
[$list_key]['visible'] = $this->core_db_fields
[$field_key]['visible'];
115 //handle parent field: is always inactive on form. Hardcode to STAFF.
116 if (isset($this->core_db_fields
['parent_id'])) {
117 $this->core_db_fields
['parent_id']['visible'] = self
::STAFF
;
120 parent
::buildLists();
121 //make sure array order is the same as in the database, since query is ordered differently
122 ksort($this->listed_fields
);
126 * Adds a field to lists
128 * @param array $field Field values
132 protected function addToLists(array $field)
134 if (in_array($field['field_id'], $this->non_list_elements
)) {
137 parent
::addToLists($field);
139 if ($field['list_visible'] ??
false) {
140 $this->listed_fields
[(int)$field['list_position']] = $field;
145 * Retrieve display elements
147 * @param Login $login Login instance
151 public function getDisplayElements(Login
$login)
155 $display_elements = [];
156 $access_level = $login->getAccessLevel();
158 $elements = $this->listed_fields
;
160 foreach ($elements as $elt) {
162 $this->handleLabel($o);
164 if ($o->field_id
== 'id_adh') {
165 // ignore access control, as member ID is always needed
166 //if (!isset($preferences) || !$preferences->pref_show_id) {
167 $o->type
= self
::TYPE_STR
;
168 $display_elements[] = $o;
171 // skip fields blacklisted for display
172 if (in_array($o->field_id
, $this->non_list_elements
)) {
176 // skip fields according to access control
178 $o->visible
== self
::NOBODY ||
179 ($o->visible
== self
::ADMIN
&&
180 $access_level < Authentication
::ACCESS_ADMIN
) ||
181 ($o->visible
== self
::STAFF
&&
182 $access_level < Authentication
::ACCESS_STAFF
) ||
183 ($o->visible
== self
::MANAGER
&&
184 $access_level < Authentication
::ACCESS_MANAGER
)
188 $display_elements[] = $o;
192 return $display_elements;
193 } catch (Throwable
$e) {
195 'An error occurred getting list elements to display',
205 * @param stdClass $field Field data
209 private function handleLabel($field)
211 switch ($field->field_id
) {
212 case 'bool_admin_adh':
213 $field->label
= __('Is admin');
215 case 'date_modif_adh':
216 $field->label
= _T('Modified');
219 $field->label
= _T('Phone');
221 case 'bool_display_info':
222 $field->label
= _T('Public');
226 $field->label
= trim(str_replace(' ', ' ', $field->label
));
227 $field->label
= preg_replace('/\s?:$/', '', $field->label
);
233 * Get all fields for list
237 public function getListedFields(): array
239 return $this->listed_fields
;
243 * Get remaining free fields for list
247 public function getRemainingFields(): array
249 $db_fields = $this->core_db_fields
;
252 foreach ($this->non_list_elements
as $todrop) {
253 unset($db_fields[$todrop]);
256 //remove already listed
257 foreach ($this->listed_fields
as $listed) {
258 unset($db_fields[$listed['field_id']]);
262 foreach ($db_fields as $key => $db_field) {
263 $remainings[$key] = $db_field;
272 * @param array $fields categorized fields array
276 public function setListFields($fields)
278 $this->listed_fields
= $fields;
279 return $this->storeList();
283 * Store list config in database
287 private function storeList()
289 $class = get_class($this);
292 if (!count($this->listed_fields
)) {
293 throw new \
RuntimeException('No fields for list, aborting.');
296 $this->zdb
->connection
->beginTransaction();
298 $update = $this->zdb
->update(self
::TABLE
);
301 'list_visible' => ':list_visible',
302 'list_position' => ':list_position'
306 'field_id' => ':field_id',
307 'table_name' => $this->table
310 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($update);
314 foreach ($this->listed_fields
as $pos => $field) {
316 'list_visible' => $field['list_visible'],
317 'list_position' => $pos,
318 'field_id' => $field['field_id']
320 $stmt->execute($params);
323 foreach (array_keys($this->getRemainingFields()) as $field) {
325 'list_visible' => $this->zdb
->isPostgres() ?
'false' : 0,
326 'list_position' => -1,
329 $stmt->execute($params);
336 '[' . $class . '] List configuration for table %s stored ' .
342 $this->zdb
->connection
->commit();
343 return $this->load();
344 } catch (Throwable
$e) {
345 $this->zdb
->connection
->rollBack();
347 '[' . $class . '] An error occurred while storing list ' .
348 'configuration for table `' . $this->table
. '`.' .
357 * Get ACL mapping for list elements not present in form configuration
361 public function getAclMapping(): array
363 return $this->acl_mapping
;
367 * Get visibility for specified field
369 * @param string $field The requested field
373 public function getVisibility($field)
375 if (in_array($field, $this->non_list_elements
)) {
378 return $this->all_visibles
[$field];