]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/DynamicFields/DynamicField.php
f869435b23e827a2b6040b01aad8a88021c806d8
[galette.git] / galette / lib / Galette / DynamicFields / DynamicField.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Abstract dynamic field
7 *
8 * PHP version 5
9 *
10 * Copyright © 2012-2014 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 DynamicFields
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2012-2014 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.1dev - 2012-07-28
36 */
37
38 namespace Galette\DynamicFields;
39
40 use Analog\Analog;
41 use Galette\Core\Db;
42 use Galette\Entity\DynamicFieldsHandle;
43 use Galette\Entity\TranslatableTrait;
44 use Galette\Entity\I18nTrait;
45 use Laminas\Db\Sql\Expression;
46 use Laminas\Db\Sql\Predicate\Expression as PredicateExpression;
47
48 /**
49 * Abstract dynamic field
50 *
51 * @name DynamicField
52 * @category DynamicFields
53 * @package Galette
54 *
55 * @author Johan Cwiklinski <johan@x-tnd.be>
56 * @copyright 2012-2014 The Galette Team
57 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
58 * @link http://galette.tuxfamily.org
59 */
60
61 abstract class DynamicField
62 {
63 use TranslatableTrait;
64 use I18nTrait;
65
66 const TABLE = 'field_types';
67 const PK = 'field_id';
68
69 /** Separator field */
70 const SEPARATOR = 0;
71 /** Simple text field */
72 const TEXT = 1;
73 /** Line field */
74 const LINE = 2;
75 /** Choice field (listbox) */
76 const CHOICE = 3;
77 /** Date field */
78 const DATE = 4;
79 /** Boolean field (checkbox) */
80 const BOOLEAN = 5;
81 /** File field (upload) */
82 const FILE = 6;
83
84 const PERM_USER_WRITE = 0;
85 const PERM_ADMIN = 1;
86 const PERM_STAFF = 2;
87 const PERM_MANAGER = 3;
88 const PERM_USER_READ = 4;
89
90 const DEFAULT_MAX_FILE_SIZE = 1024;
91 const VALUES_FIELD_LENGTH = 100;
92
93 protected $has_data = false;
94 protected $has_width = false;
95 protected $has_height = false;
96 protected $has_size = false;
97 protected $multi_valued = false;
98 protected $fixed_values = false;
99 protected $has_permissions = true;
100
101 protected $id;
102 protected $index;
103 protected $perm;
104 protected $required;
105 protected $width;
106 protected $height;
107 protected $repeat;
108 protected $size;
109 protected $old_size;
110 protected $values;
111 protected $form;
112
113 protected $errors;
114
115 protected $zdb;
116
117 /**
118 * Default constructor
119 *
120 * @param Db $zdb Database instance
121 * @param mixed $args Arguments
122 */
123 public function __construct(Db $zdb, $args = null)
124 {
125 $this->zdb = $zdb;
126
127 if (is_int($args)) {
128 $this->load($args);
129 } elseif ($args !== null && is_object($args)) {
130 $this->loadFromRs($args);
131 }
132 }
133
134 /**
135 * Load field from its id
136 *
137 * @param Db $zdb Database instance
138 * @param int $id Field id
139 *
140 * @return DynamicField|false
141 */
142 public static function loadFieldType(Db $zdb, $id)
143 {
144 try {
145 $select = $zdb->select(self::TABLE);
146 $select->where('field_id = ' . $id);
147
148 $results = $zdb->execute($select);
149 $result = $results->current();
150 if ($result) {
151 $field_type = $result->field_type;
152 $field_type = self::getFieldType($zdb, $field_type);
153 $field_type->loadFromRs($result);
154 return $field_type;
155 }
156 } catch (\Exception $e) {
157 Analog::log(
158 __METHOD__ . ' | Unable to retrieve field `' . $id .
159 '` information | ' . $e->getMessage(),
160 Analog::ERROR
161 );
162 return false;
163 }
164 return false;
165 }
166
167 /**
168 * Get correct field type instance
169 *
170 * @param Db $zdb Database instance
171 * @param int $t Field type
172 * @param int $id Optional dynamic field id (to load data)
173 *
174 * @return DynamicField
175 */
176 public static function getFieldType(Db $zdb, $t, $id = null)
177 {
178 $df = null;
179 switch ($t) {
180 case self::SEPARATOR:
181 $df = new Separator($zdb, $id);
182 break;
183 case self::TEXT:
184 $df = new Text($zdb, $id);
185 break;
186 case self::LINE:
187 $df = new Line($zdb, $id);
188 break;
189 case self::CHOICE:
190 $df = new Choice($zdb, $id);
191 break;
192 case self::DATE:
193 $df = new Date($zdb, $id);
194 break;
195 case self::BOOLEAN:
196 $df = new Boolean($zdb, $id);
197 break;
198 case self::FILE:
199 $df = new File($zdb, $id);
200 break;
201 default:
202 throw new \Exception('Unknown field type ' . $t . '!');
203 break;
204 }
205 return $df;
206 }
207
208 /**
209 * Load field
210 *
211 * @param integer $id Id
212 *
213 * @return void
214 */
215 public function load($id)
216 {
217 try {
218 $select = $this->zdb->select(self::TABLE);
219 $select->where(self::PK . ' = ' . $id);
220
221 $results = $this->zdb->execute($select);
222 $result = $results->current();
223
224 if ($result) {
225 $this->loadFromRs($result);
226 }
227 } catch (Exception $e) {
228 Analog::log(
229 'Unable to retrieve field type for field ' . $id . ' | ' .
230 $e->getMessage(),
231 Analog::ERROR
232 );
233 }
234 }
235
236 /**
237 * Load field type from a db ResultSet
238 *
239 * @param ResultSet $rs ResultSet
240 * @param boolean $values Whether to load values. Defaults to true
241 *
242 * @return void
243 */
244 public function loadFromRs($rs, $values = true)
245 {
246 $this->id = (int)$rs->field_id;
247 $this->name = $rs->field_name;
248 $this->index = (int)$rs->field_index;
249 $this->perm = (int)$rs->field_perm;
250 $this->required = ($rs->field_required == 1 ? true : false);
251 $this->width = $rs->field_width;
252 $this->height = $rs->field_height;
253 $this->repeat = $rs->field_repeat;
254 $this->size = $rs->field_size;
255 $this->form = $rs->field_form;
256 if ($values && $this->hasFixedValues()) {
257 $this->loadFixedValues();
258 }
259 }
260
261 /**
262 * Retrieve fixed values table name
263 *
264 * @param integer $id Field ID
265 * @param boolean $prefixed Whether table name should be prefixed
266 *
267 * @return string
268 */
269 public static function getFixedValuesTableName($id, $prefixed = false)
270 {
271 $name = 'field_contents_' . $id;
272 if ($prefixed === true) {
273 $name = PREFIX_DB . $name;
274 }
275 return $name;
276 }
277
278 /**
279 * Returns an array of fixed valued for a field of type 'choice'.
280 *
281 * @return void
282 */
283 private function loadFixedValues()
284 {
285 try {
286 $val_select = $this->zdb->select(
287 self::getFixedValuesTableName($this->id)
288 );
289
290 $val_select->columns(
291 array(
292 'val'
293 )
294 )->order('id');
295
296 $results = $this->zdb->execute($val_select);
297 $this->values = array();
298 if ($results) {
299 foreach ($results as $val) {
300 $this->values[] = $val->val;
301 }
302 }
303 } catch (\Exception $e) {
304 Analog::log(
305 __METHOD__ . ' | ' . $e->getMessage(),
306 Analog::WARNING
307 );
308 }
309 }
310
311 /**
312 * Get field type
313 *
314 * @return integer
315 */
316 abstract public function getType();
317
318 /**
319 * Get field type name
320 *
321 * @return String
322 */
323 public function getTypeName()
324 {
325 $types = $this->getFieldsTypesNames();
326 if (isset($types[$this->getType()])) {
327 return $types[$this->getType()];
328 } else {
329 throw new \RuntimeException(
330 'Unknow type ' . $this->getType()
331 );
332 }
333 }
334
335 /**
336 * Does the field handle data?
337 *
338 * @return boolean
339 */
340 public function hasData()
341 {
342 return $this->has_data;
343 }
344
345 /**
346 * Does the field has width?
347 *
348 * @return boolean
349 */
350 public function hasWidth()
351 {
352 return $this->has_width;
353 }
354
355 /**
356 * Does the field has height?
357 *
358 * @return boolean
359 */
360 public function hasHeight()
361 {
362 return $this->has_height;
363 }
364
365 /**
366 * Does the field has a size?
367 *
368 * @return boolean
369 */
370 public function hasSize()
371 {
372 return $this->has_size;
373 }
374
375 /**
376 * Is the field multi valued?
377 *
378 * @return boolean
379 */
380 public function isMultiValued()
381 {
382 return $this->multi_valued;
383 }
384
385 /**
386 * Does the field has fixed values?
387 *
388 * @return boolean
389 */
390 public function hasFixedValues()
391 {
392 return $this->fixed_values;
393 }
394
395 /**
396 * Does the field require permissions?
397 *
398 * @return boolean
399 */
400 public function hasPermissions()
401 {
402 return $this->has_permissions;
403 }
404
405
406 /**
407 * Get field id
408 *
409 * @return integer
410 */
411 public function getId()
412 {
413 return $this->id;
414 }
415
416 /**
417 * Get field Permissions
418 *
419 * @return integer
420 */
421 public function getPerm()
422 {
423 return $this->perm;
424 }
425
426
427 /**
428 * Is field required?
429 *
430 * @return boolean
431 */
432 public function isRequired()
433 {
434 return $this->required;
435 }
436
437 /**
438 * Get field width
439 *
440 * @return integer
441 */
442 public function getWidth()
443 {
444 return $this->width;
445 }
446
447 /**
448 * Get field height
449 *
450 * @return integer
451 */
452 public function getHeight()
453 {
454 return $this->height;
455 }
456
457 /**
458 * Is current field repeatable?
459 *
460 * @return boolean
461 */
462 public function isRepeatable()
463 {
464 return $this->repeat != null && trim($this->repeat) != '' && (int)$this->repeat > 1;
465 }
466
467 /**
468 * Get fields repetitions
469 *
470 * @return integer|boolean
471 */
472 public function getRepeat()
473 {
474 return $this->repeat;
475 }
476
477 /**
478 * Get field size
479 *
480 * @return integer
481 */
482 public function getSize()
483 {
484 return $this->size;
485 }
486
487 /**
488 * Get field index
489 *
490 * @return integer
491 */
492 public function getIndex()
493 {
494 return $this->index;
495 }
496
497 /**
498 * Retrieve permissions names for display
499 *
500 * @return array
501 */
502 public static function getPermsNames()
503 {
504 return [
505 self::PERM_USER_WRITE => _T("User, read/write"),
506 self::PERM_STAFF => _T("Staff member"),
507 self::PERM_ADMIN => _T("Administrator"),
508 self::PERM_MANAGER => _T("Group manager"),
509 self::PERM_USER_READ => _T("User, read only")
510 ];
511 }
512
513 /**
514 * Retrieve forms names
515 *
516 * @return array
517 */
518 public static function getFormsNames()
519 {
520 return [
521 'adh' => _T("Members"),
522 'contrib' => _T("Contributions"),
523 'trans' => _T("Transactions")
524 ];
525 }
526
527 /**
528 * Retrieve form name
529 *
530 * @param string $form_name Form name
531 *
532 * @return string
533 */
534 public static function getFormTitle($form_name)
535 {
536 $names = self::getFormsNames();
537 return $names[$form_name];
538 }
539
540 /**
541 * Get permission name
542 *
543 * @return string
544 */
545 public function getPermName()
546 {
547 $perms = self::getPermsNames();
548 return $perms[$this->getPerm()];
549 }
550
551 /**
552 * Get form
553 *
554 * @return string
555 */
556 public function getForm()
557 {
558 return $this->form;
559 }
560
561 /**
562 * Get field values
563 *
564 * @param boolean $imploded Whether to implode values
565 *
566 * @return array
567 */
568 public function getValues($imploded = false)
569 {
570 if (!is_array($this->values)) {
571 return false;
572 }
573 if ($imploded === true) {
574 return implode("\n", $this->values);
575 } else {
576 return $this->values;
577 }
578 }
579
580 /**
581 * Check posted values validity
582 *
583 * @param array $values All values to check, basically the $_POST array
584 * after sending the form
585 *
586 * @return true|array
587 */
588 public function check($values)
589 {
590 $this->errors = [];
591 $this->warnings = [];
592
593 if ((!isset($values['field_name']) || $values['field_name'] == '')
594 && get_class($this) != '\Galette\DynamicField\Separator'
595 ) {
596 $this->errors[] = _T('Missing required field name!');
597 } else {
598 if ($this->old_name === null && $this->name !== null && $this->name != $values['field_name']) {
599 $this->old_name = $this->name;
600 }
601 $this->name = $values['field_name'];
602 }
603
604 if (!isset($values['field_perm']) || $values['field_perm'] === '') {
605 $this->errors[] = _T('Missing required field permissions!');
606 } else {
607 if (in_array($values['field_perm'], array_keys(self::getPermsNames()))) {
608 $this->perm = $values['field_perm'];
609 } else {
610 $this->errors[] = _T('Unknown permission!');
611 }
612 }
613
614 if ($this->id === null) {
615 if (!isset($values['form']) || $values['form'] == '') {
616 $this->errors[] = _T('Missing required form!');
617 } else {
618 if (in_array($values['form'], array_keys(self::getFormsNames()))) {
619 $this->form = $values['form'];
620 } else {
621 $this->errors[] = _T('Unknown form!');
622 }
623 }
624 }
625
626 $this->required = $values['field_required'];
627
628 if (count($this->errors) === 0 && $this->isDuplicate($values['form'], $this->name, $this->id)) {
629 $this->errors[] = _T("- Field name already used.");
630 }
631
632 if ($this->hasWidth() && isset($values['field_width']) && trim($values['field_width']) != '') {
633 $this->width = $values['field_width'];
634 }
635
636 if ($this->hasHeight() && isset($values['field_height']) && trim($values['field_height']) != '') {
637 $this->height = $values['field_height'];
638 }
639
640 if ($this->hasSize() && isset($values['field_size']) && trim($values['field_size']) != '') {
641 $this->size = $values['field_size'];
642 }
643
644 if (isset($values['field_repeat']) && trim($values['field_repeat']) != '') {
645 $this->repeat = $values['field_repeat'];
646 }
647
648 if ($this->hasFixedValues() && isset($values['fixed_values'])) {
649 $fixed_values = [];
650 foreach (explode("\n", $values['fixed_values']) as $val) {
651 $val = trim($val);
652 $len = mb_strlen($val);
653 if ($len > 0) {
654 $fixed_values[] = $val;
655 if ($len > $this->size) {
656 if ($this->old_size === null) {
657 $this->old_size = $this->size;
658 }
659 $this->size = $len;
660 }
661 }
662 }
663
664 $this->values = $fixed_values;
665 }
666
667 if ($this->id == null) {
668 $this->index = $this->getNewIndex();
669 }
670
671 if (count($this->errors) === 0) {
672 return true;
673 } else {
674 return false;
675 }
676 }
677
678 /**
679 * Store the field type
680 *
681 * @param array $values All values to check, basically the $_POST array
682 * after sending the form
683 *
684 * @return boolean
685 */
686 public function store($values)
687 {
688 if (!$this->check($values)) {
689 return false;
690 }
691
692 $isnew = ($this->id === null);
693 if ($this->old_name !== null) {
694 $this->deleteTranslation($this->old_name);
695 $this->addTranslation($this->name);
696 }
697
698 try {
699 $values = array(
700 'field_name' => $this->name,
701 'field_perm' => $this->perm,
702 'field_required' => $this->required,
703 'field_width' => ($this->width === null ? new Expression('NULL') : $this->width),
704 'field_height' => ($this->height === null ? new Expression('NULL') : $this->height),
705 'field_size' => ($this->size === null ? new Expression('NULL') : $this->size),
706 'field_repeat' => ($this->repeat === null ? new Expression('NULL') : $this->repeat),
707 'field_form' => $this->form,
708 'field_index' => $this->index
709 );
710
711 if ($this->required === false) {
712 //Handle booleans for postgres ; bugs #18899 and #19354
713 $values['field_required'] = $this->zdb->isPostgres() ? 'false' : 0;
714 }
715
716 if (!$isnew) {
717 $update = $this->zdb->update(self::TABLE);
718 $update->set($values)->where(
719 self::PK . ' = ' . $this->id
720 );
721 $this->zdb->execute($update);
722 } else {
723 $values['field_type'] = $this->getType();
724 $insert = $this->zdb->insert(self::TABLE);
725 $insert->values($values);
726 $this->zdb->execute($insert);
727
728 if ($this->zdb->isPostgres()) {
729 $this->id = $this->zdb->driver->getLastGeneratedValue(
730 PREFIX_DB . 'field_types_id_seq'
731 );
732 } else {
733 $this->id = $this->zdb->driver->getLastGeneratedValue();
734 }
735
736 if ($this->name != '') {
737 $this->addTranslation($this->name);
738 }
739 }
740 } catch (Exception $e) {
741 Analog::log(
742 'An error occurred storing field | ' . $e->getMessage(),
743 Analog::ERROR
744 );
745 $this->errors[] = _T("An error occurred storing the field.");
746 }
747
748 if (count($this->errors) === 0 && $this->hasFixedValues()) {
749 $contents_table = self::getFixedValuesTableName($this->id, true);
750
751 try {
752 $this->zdb->connection->beginTransaction();
753 $this->zdb->db->query(
754 'DROP TABLE IF EXISTS ' . $contents_table,
755 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
756 );
757 $field_size = ((int)$this->size > 0) ? $this->size : 1;
758 $this->zdb->db->query(
759 'CREATE TABLE ' . $contents_table .
760 ' (id INTEGER NOT NULL,val varchar(' . $field_size .
761 ') NOT NULL)',
762 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
763 );
764 $this->zdb->connection->commit();
765 } catch (\Exception $e) {
766 $this->zdb->connection->rollBack();
767 Analog::log(
768 'Unable to manage fields values table ' .
769 $contents_table . ' | ' . $e->getMessage(),
770 Analog::ERROR
771 );
772 $this->errors[] = _T("An error occurred creating field values table");
773 }
774
775 if (count($this->errors) == 0 && is_array($this->values)) {
776 $contents_table = self::getFixedValuesTableName($this->id);
777 try {
778 $this->zdb->connection->beginTransaction();
779
780 $insert = $this->zdb->insert($contents_table);
781 $insert->values(
782 array(
783 'id' => ':id',
784 'val' => ':val'
785 )
786 );
787 $stmt = $this->zdb->sql->prepareStatementForSqlObject($insert);
788
789 for ($i = 0; $i < count($this->values); $i++) {
790 $stmt->execute(
791 array(
792 'id' => $i,
793 'val' => $this->values[$i]
794 )
795 );
796 }
797 $this->zdb->connection->commit();
798 } catch (\Exception $e) {
799 $this->zdb->connection->rollBack();
800 Analog::log(
801 'Unable to store field ' . $this->id . ' values (' .
802 $e->getMessage() . ')',
803 Analog::ERROR
804 );
805 $this->warnings[] = _T('An error occurred storing dynamic field values :(');
806 }
807 }
808 }
809
810 if (count($this->errors) === 0) {
811 return true;
812 } else {
813 return false;
814 }
815 }
816
817 /**
818 * Get new index
819 *
820 * @return integer
821 */
822 protected function getNewIndex()
823 {
824 $select = $this->zdb->select(self::TABLE);
825 $select->columns(
826 array(
827 'idx' => new \Laminas\Db\Sql\Expression('COUNT(*) + 1')
828 )
829 );
830 $select->where(['field_form' => $this->form]);
831 $results = $this->zdb->execute($select);
832 $result = $results->current();
833 $idx = $result->idx;
834 return $idx;
835 }
836
837 /**
838 * Is field duplicated?
839 *
840 * @return boolean
841 */
842 public function isDuplicate()
843 {
844 //let's consider field is duplicated, in case of future errors
845 $duplicated = true;
846 try {
847 $select = $this->zdb->select(self::TABLE);
848 $select->columns(
849 array(
850 'cnt' => new \Laminas\Db\Sql\Expression('COUNT('. self::PK.')')
851 )
852 )->where(
853 array(
854 'field_form' => $this->form,
855 'field_name' => $this->name
856 )
857 );
858
859 if ($this->id !== null) {
860 $select->where->addPredicate(
861 new PredicateExpression(
862 'field_id NOT IN (?)',
863 array($this->id)
864 )
865 );
866 }
867
868 $results = $this->zdb->execute($select);
869 $result = $results->current();
870 $dup = $result->cnt;
871 if (!$dup > 0) {
872 $duplicated = false;
873 }
874 } catch (\Exception $e) {
875 Analog::log(
876 'An error occurred checking field duplicity' . $e->getMessage(),
877 Analog::ERROR
878 );
879 }
880 return $duplicated;
881 }
882 /**
883 * Move a dynamic field
884 *
885 * @param string $action What to do (either 'up' or 'down' localized)
886 *
887 * @return boolean
888 */
889 public function move($action)
890 {
891 try {
892 $this->zdb->connection->beginTransaction();
893
894 $old_rank = $this->index;
895
896 $direction = $action == 'up' ? -1: 1;
897 $new_rank = $old_rank + $direction;
898 $update = $this->zdb->update(self::TABLE);
899 $update->set([
900 'field_index' => $old_rank
901 ])->where([
902 'field_index' => $new_rank,
903 'field_form' => $this->form
904 ]);
905 $this->zdb->execute($update);
906
907 $update = $this->zdb->update(self::TABLE);
908 $update->set(
909 array(
910 'field_index' => $new_rank
911 )
912 )->where(
913 array(
914 self::PK => $this->id
915 )
916 );
917 $this->zdb->execute($update);
918 $this->zdb->connection->commit();
919
920 return true;
921 } catch (\Exception $e) {
922 $this->zdb->connection->rollBack();
923 Analog::log(
924 'Unable to change field ' . $this->id . ' rank | ' .
925 $e->getMessage(),
926 Analog::ERROR
927 );
928 return false;
929 }
930 }
931
932 /**
933 * Delete a dynamic field
934 *
935 * @return boolean
936 */
937 public function remove()
938 {
939 try {
940 $this->zdb->connection->beginTransaction();
941 $old_rank = $this->index;
942
943 $update = $this->zdb->update(self::TABLE);
944 $update->set(
945 array(
946 'field_index' => new \Laminas\Db\Sql\Expression('field_index-1')
947 )
948 )->where
949 ->greaterThan('field_index', $old_rank)
950 ->equalTo('field_form', $this->form);
951 $this->zdb->execute($update);
952
953 //remove associated values
954 $delete = $this->zdb->delete(DynamicFieldsHandle::TABLE);
955 $delete->where(
956 array(
957 'field_id' => $this->id,
958 'field_form' => $this->form
959 )
960 );
961 $result = $this->zdb->execute($delete);
962 if (!$result) {
963 throw new \RuntimeException('Unable to remove associated values for field ' . $this->id . '!');
964 }
965
966 //remove field type
967 $delete = $this->zdb->delete(self::TABLE);
968 $delete->where(
969 array(
970 'field_id' => $this->id,
971 'field_form' => $this->form
972 )
973 );
974 $result = $this->zdb->execute($delete);
975 if (!$result) {
976 throw new \RuntimeException('Unable to remove field ' . $this->id . '!');
977 }
978
979 if ($this->hasFixedValues()) {
980 $contents_table = self::getFixedValuesTableName($this->id);
981 $this->zdb->db->query(
982 'DROP TABLE IF EXISTS ' . $contents_table,
983 \Laminas\Db\Adapter\Adapter::QUERY_MODE_EXECUTE
984 );
985 }
986 $this->deleteTranslation($this->name);
987
988 $this->zdb->connection->commit();
989 return true;
990 } catch (\Exception $e) {
991 $this->zdb->connection->rollBack();
992 Analog::log(
993 'An error occurred deleting field | ' . $e->getMessage(),
994 Analog::ERROR
995 );
996 return false;
997 }
998 }
999
1000 /**
1001 * Retrieve fields types names
1002 *
1003 * @return array
1004 */
1005 public static function getFieldsTypesNames()
1006 {
1007 $names = [
1008 self::SEPARATOR => _T("separator"),
1009 self::TEXT => _T("free text"),
1010 self::LINE => _T("single line"),
1011 self::CHOICE => _T("choice"),
1012 self::DATE => _T("date"),
1013 self::BOOLEAN => _T("boolean"),
1014 self::FILE => _T("file")
1015 ];
1016 return $names;
1017 }
1018
1019 /**
1020 * Get errors
1021 *
1022 * @return array
1023 */
1024 public function getErrors()
1025 {
1026 return $this->errors;
1027 }
1028
1029 /**
1030 * Get warnings
1031 *
1032 * @return array
1033 */
1034 public function getWarnings()
1035 {
1036 return $this->warnings;
1037 }
1038 }