1 | <?php |
||
2 | |||
3 | namespace Frontend\Modules\FormBuilder\Engine; |
||
4 | |||
5 | use Frontend\Core\Engine\Model as FrontendModel; |
||
6 | |||
7 | /** |
||
8 | * In this file we store all generic functions that we will be using in the form_builder module |
||
9 | */ |
||
10 | class Model |
||
11 | { |
||
12 | public static function get(string $id): array |
||
13 | { |
||
14 | // get form |
||
15 | $form = (array) FrontendModel::getContainer()->get('database')->getRecord( |
||
16 | 'SELECT i.id, i.email_subject, i.email_template, i.language, i.method, i.name, i.email, |
||
17 | i.success_message, i.identifier |
||
18 | FROM forms AS i |
||
19 | WHERE i.id = ?', |
||
20 | $id |
||
21 | ); |
||
22 | |||
23 | // unserialize the recipients |
||
24 | if (isset($form['email'])) { |
||
25 | $form['email'] = (array) unserialize($form['email'], ['allowed_classes' => false]); |
||
26 | } |
||
27 | |||
28 | // get validation |
||
29 | $form['fields'] = self::getFields($id); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
30 | |||
31 | return $form; |
||
32 | } |
||
33 | |||
34 | public static function getFields(int $formId): array |
||
35 | { |
||
36 | // get fields |
||
37 | $fields = (array) FrontendModel::getContainer()->get('database')->getRecords( |
||
38 | 'SELECT i.id, i.type, i.settings |
||
39 | FROM forms_fields AS i |
||
40 | WHERE i.form_id = ? |
||
41 | ORDER BY i.sequence ASC', |
||
42 | $formId, |
||
43 | 'id' |
||
44 | ); |
||
45 | |||
46 | if (empty($fields)) { |
||
47 | return []; |
||
48 | } |
||
49 | |||
50 | // create an array with an equal amount of questionmarks as ids provided |
||
51 | $idPlaceHolders = array_fill(0, count($fields), '?'); |
||
52 | |||
53 | // get field validations, each field can have multiple 'type' validations |
||
54 | $fieldValidations = (array) FrontendModel::getContainer()->get('database')->getRecords( |
||
55 | 'SELECT i.field_id, i.type, i.parameter, i.error_message |
||
56 | FROM forms_fields_validation AS i |
||
57 | WHERE i.field_id IN (' . implode(', ', $idPlaceHolders) . ')', |
||
58 | array_keys($fields) |
||
59 | ); |
||
60 | |||
61 | // loop fields to add extra parameters |
||
62 | foreach ($fields as &$field) { |
||
63 | // unserialize |
||
64 | if ($field['settings'] !== null) { |
||
65 | $field['settings'] = unserialize($field['settings'], ['allowed_classes' => false]); |
||
66 | } |
||
67 | |||
68 | // init validations |
||
69 | $field['validations'] = []; |
||
70 | } |
||
71 | |||
72 | // we need to loop because one field can have multiple 'type' validations |
||
73 | foreach ($fieldValidations as $fieldValidation) { |
||
74 | // add validation type to our field |
||
75 | $fields[$fieldValidation['field_id']]['validations'][$fieldValidation['type']] = $fieldValidation; |
||
76 | } |
||
77 | |||
78 | return $fields; |
||
79 | } |
||
80 | |||
81 | public static function insertData(array $formData): int |
||
82 | { |
||
83 | return FrontendModel::getContainer()->get('database')->insert('forms_data', $formData); |
||
84 | } |
||
85 | |||
86 | public static function insertDataField(array $dataField): int |
||
87 | { |
||
88 | return FrontendModel::getContainer()->get('database')->insert('forms_data_fields', $dataField); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Convert a PHP Date to jquery date format |
||
93 | * |
||
94 | * @param string $phpFormat The php date format |
||
95 | * |
||
96 | * @return string The jQuery date format |
||
97 | */ |
||
98 | public static function convertPHPDateToJquery(string $phpFormat): string |
||
99 | { |
||
100 | $symbolsMatching = [ |
||
101 | // Day |
||
102 | 'd' => 'dd', |
||
103 | 'D' => 'D', |
||
104 | 'j' => 'd', |
||
105 | 'l' => 'DD', |
||
106 | 'N' => '', |
||
107 | 'S' => '', |
||
108 | 'w' => '', |
||
109 | 'z' => 'o', |
||
110 | // Week |
||
111 | 'W' => '', |
||
112 | // Month |
||
113 | 'F' => 'MM', |
||
114 | 'm' => 'mm', |
||
115 | 'M' => 'M', |
||
116 | 'n' => 'm', |
||
117 | 't' => '', |
||
118 | // Year |
||
119 | 'L' => '', |
||
120 | 'o' => '', |
||
121 | 'Y' => 'yy', |
||
122 | 'y' => 'y', |
||
123 | // Time |
||
124 | 'a' => '', |
||
125 | 'A' => '', |
||
126 | 'B' => '', |
||
127 | 'g' => '', |
||
128 | 'G' => '', |
||
129 | 'h' => '', |
||
130 | 'H' => '', |
||
131 | 'i' => '', |
||
132 | 's' => '', |
||
133 | 'u' => '', |
||
134 | ]; |
||
135 | $jqueryuiFormat = ''; |
||
136 | $escaping = false; |
||
137 | for ($i = 0; $i < mb_strlen($phpFormat); ++$i) { |
||
138 | $char = $phpFormat[$i]; |
||
139 | if ($char === '\\') { |
||
140 | // PHP date format escaping character |
||
141 | |||
142 | ++$i; |
||
143 | if ($escaping) { |
||
144 | $jqueryuiFormat .= $phpFormat[$i]; |
||
145 | } else { |
||
146 | $jqueryuiFormat .= '\'' . $phpFormat[$i]; |
||
147 | } |
||
148 | $escaping = true; |
||
149 | } else { |
||
150 | if ($escaping) { |
||
151 | $jqueryuiFormat .= "'"; |
||
152 | $escaping = false; |
||
153 | } |
||
154 | if (isset($symbolsMatching[$char])) { |
||
155 | $jqueryuiFormat .= $symbolsMatching[$char]; |
||
156 | } else { |
||
157 | $jqueryuiFormat .= $char; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return $jqueryuiFormat; |
||
163 | } |
||
164 | } |
||
165 |