3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Lists config handling
10 * Copyright © 2020 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 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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-13
38 namespace Galette\Entity
;
42 use Laminas\Db\Adapter\Adapter
;
44 use Galette\Core\Login
;
45 use Galette\Core\Authentication
;
48 * Lists config class for galette:
49 * defines fields order and visibility
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2020 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
58 * @since Available since 0.9.4dev - 2020-05-13
60 class ListsConfig
extends FieldsConfig
62 protected $listed_fields = array();
65 * Fields that are not part of lists
69 private $non_list_elements = array(
79 * ACL mapping for list elements not present in form configuration
83 private $acl_mapping = array(
84 'list_adh_name' => 'nom_adh',
85 'list_adh_contribstatus' => 'id_statut'
89 * Prepare a field (required data, automation)
91 * @param ArrayObject $rset DB ResultSet row
95 protected function buildField(ArrayObject
$rset): array
97 $f = parent
::buildField($rset);
98 $f['list_position'] = (int)$rset->list_position
;
99 $f['list_visible'] = ($f['list_position'] >= 0);
104 * Create field array configuration,
105 * Several lists of fields are kept (visible, requireds, etc), build them.
109 protected function buildLists()
111 //Specific list fields does not have rights; fix this from mapping
112 //Cannot be done preparing fields, cannot be sure of the order it is processed
113 foreach ($this->acl_mapping
as $list_key => $field_key) {
114 $this->core_db_fields
[$list_key]['visible'] = $this->core_db_fields
[$field_key]['visible'];
117 //handle parent field: is always inactive on form. Hardcode to STAFF.
118 if (isset($this->core_db_fields
['parent_id'])) {
119 $this->core_db_fields
['parent_id']['visible'] = self
::STAFF
;
122 parent
::buildLists();
123 //make sure array order is the same as in the database, since query is ordered differently
124 ksort($this->listed_fields
);
128 * Adds a field to lists
130 * @param array $field Field values
134 protected function addToLists(array $field)
136 if (in_array($field['field_id'], $this->non_list_elements
)) {
139 parent
::addToLists($field);
141 if ($field['list_visible'] ??
false) {
142 $this->listed_fields
[(int)$field['list_position']] = $field;
147 * Retrieve display elements
149 * @param Login $login Login instance
153 public function getDisplayElements(Login
$login)
157 $display_elements = [];
158 $access_level = $login->getAccessLevel();
160 $elements = $this->listed_fields
;
162 foreach ($elements as $elt) {
164 $this->handleLabel($o);
166 if ($o->field_id
== 'id_adh') {
167 // ignore access control, as member ID is always needed
168 //if (!isset($preferences) || !$preferences->pref_show_id) {
169 $o->type
= self
::TYPE_STR
;
170 $display_elements[] = $o;
173 // skip fields blacklisted for display
174 if (in_array($o->field_id
, $this->non_list_elements
)) {
178 // skip fields according to access control
180 $o->visible
== self
::NOBODY ||
181 ($o->visible
== self
::ADMIN
&&
182 $access_level < Authentication
::ACCESS_ADMIN
) ||
183 ($o->visible
== self
::STAFF
&&
184 $access_level < Authentication
::ACCESS_STAFF
) ||
185 ($o->visible
== self
::MANAGER
&&
186 $access_level < Authentication
::ACCESS_MANAGER
)
190 $display_elements[] = $o;
194 return $display_elements;
195 } catch (\Exception
$e) {
197 'An error occurred getting list elements to display',
207 * @param stdClass $field Field data
211 private function handleLabel($field)
213 switch ($field->field_id
) {
214 case 'bool_admin_adh':
215 $field->label
= __('Is admin');
217 case 'date_modif_adh':
218 $field->label
= _T('Modified');
221 //TRANS: see https://www.urbandictionary.com/define.php?term=b-day
222 $field->label
= _('b-day');
225 $field->label
= _T('Phone');
227 case 'bool_display_info':
228 $field->label
= _T('Public');
232 $field->label
= trim(str_replace(' ', ' ', $field->label
));
233 $field->label
= preg_replace('/\s?:$/', '', $field->label
);
239 * Get all fields for list
243 public function getListedFields(): array
245 return $this->listed_fields
;
249 * Get remaining free fields for list
253 public function getRemainingFields(): array
255 $db_fields = $this->core_db_fields
;
258 foreach ($this->non_list_elements
as $todrop) {
259 unset($db_fields[$todrop]);
262 //remove already listed
263 foreach ($this->listed_fields
as $listed) {
264 unset($db_fields[$listed['field_id']]);
268 foreach ($db_fields as $key => $db_field) {
269 $remainings[$key] = $db_field;
278 * @param array $fields categorized fields array
282 public function setListFields($fields)
284 $this->listed_fields
= $fields;
285 return $this->storeList();
289 * Store list config in database
293 private function storeList()
295 $class = get_class($this);
298 if (!count($this->listed_fields
)) {
299 throw new \
RuntimeException('No fields for list, aborting.');
302 $this->zdb
->connection
->beginTransaction();
304 $update = $this->zdb
->update(self
::TABLE
);
307 'list_visible' => ':list_visible',
308 'list_position' => ':list_position'
312 'field_id' => ':field_id',
313 'table_name' => $this->table
316 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($update);
320 foreach ($this->listed_fields
as $pos => $field) {
322 'list_visible' => $field['list_visible'],
323 'list_position' => $pos,
324 'where1' => $field['field_id']
326 $stmt->execute($params);
329 foreach (array_keys($this->getRemainingFields()) as $field) {
331 'list_visible' => $this->zdb
->isPostgres() ?
'false' : 0,
332 'list_position' => -1,
335 $stmt->execute($params);
342 '[' . $class . '] List configuration for table %s stored ' .
348 $this->zdb
->connection
->commit();
349 return $this->load();
350 } catch (\Exception
$e) {
351 $this->zdb
->connection
->rollBack();
353 '[' . $class . '] An error occurred while storing list ' .
354 'configuration for table `' . $this->table
. '`.' .
359 $e->getTraceAsString(),
367 * Get ACL mapping for list elements not present in form configuration
371 public function getAclMapping(): array
373 return $this->acl_mapping
;
377 * Get visibility for specified field
379 * @param string $field The requested field
383 public function getVisibility($field)
385 if (in_array($field, $this->non_list_elements
)) {
388 return $this->all_visibles
[$field];