PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 1.07.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v1.07.11
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmForm.php

FrmForm.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 1.07.11, at classes/models/FrmForm.php

350 lines 15.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if(!defined('ABSPATH')) die(__('You are not allowed to call this page directly.', 'formidable'));
3
4 if(class_exists('FrmForm'))
5 return;
6
7 class FrmForm{
8
9 function create( $values ) {
10 global $wpdb, $frm_settings;
11
12 $new_values = array(
13 'form_key' => FrmAppHelper::get_unique_key($values['form_key'], $wpdb->prefix .'frm_forms', 'form_key'),
14 'name' => $values['name'],
15 'description' => $values['description'],
16 'status' => isset($values['status']) ? $values['status'] : 'draft',
17 'is_template' => isset($values['is_template']) ? (int) $values['is_template'] : 0,
18 'editable' => isset($values['editable']) ? (int) $values['editable'] : 0,
19 'default_template' => isset($values['default_template']) ? (int) $values['default_template'] : 0,
20 'created_at' => isset($values['created_at']) ? $values['created_at'] : current_time('mysql', 1),
21 );
22
23 $options = array();
24
25 $defaults = FrmFormsHelper::get_default_opts();
26 foreach ($defaults as $var => $default) {
27 $options[$var] = isset($values['options'][$var]) ? $values['options'][$var] : $default;
28 unset($var);
29 unset($default);
30 }
31
32 $options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before');
33 $options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after');
34 $options['submit_html'] = isset($values['options']['submit_html']) ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit');
35
36 $options = apply_filters('frm_form_options_before_update', $options, $values);
37 $new_values['options'] = serialize($options);
38
39 //if(isset($values['id']) && is_numeric($values['id']))
40 // $new_values['id'] = $values['id'];
41
42 $query_results = $wpdb->insert( $wpdb->prefix .'frm_forms', $new_values );
43
44 return $wpdb->insert_id;
45 }
46
47 function duplicate( $id, $template = false, $copy_keys = false, $blog_id = false ) {
48 global $wpdb;
49
50 $frm_form = new FrmForm();
51 $values = $frm_form->getOne( $id, $blog_id );
52 if ( !$values ) {
53 return false;
54 }
55
56 $new_key = $copy_keys ? $values->form_key : '';
57
58 $new_values = array(
59 'form_key' => FrmAppHelper::get_unique_key($new_key, $wpdb->prefix .'frm_forms', 'form_key'),
60 'name' => $values->name,
61 'description' => $values->description,
62 'status' => $template ? '' : 'draft',
63 'logged_in' => $values->logged_in ? $values->logged_in : 0,
64 'editable' => $values->editable ? $values->editable : 0,
65 'created_at' => current_time('mysql', 1),
66 'is_template' => $template ? 1 : 0,
67 );
68
69 if ( $blog_id ) {
70 $new_values['status'] = 'published';
71 $new_options = maybe_unserialize($values->options);
72 $new_options['email_to'] = get_option('admin_email');
73 $new_options['copy'] = false;
74 $new_values['options'] = $new_options;
75 } else {
76 $new_values['options'] = $values->options;
77 }
78
79 if ( is_array($new_values['options']) ) {
80 $new_values['options'] = serialize($new_values['options']);
81 }
82
83 $query_results = $wpdb->insert( $wpdb->prefix .'frm_forms', $new_values );
84
85 if ( $query_results ) {
86 global $frm_field;
87 $form_id = $wpdb->insert_id;
88 $frm_field->duplicate($id, $form_id, $copy_keys, $blog_id);
89
90 // update form settings after fields are created
91 do_action('frm_after_duplicate_form', $form_id, $new_values);
92 return $form_id;
93 } else {
94 return false;
95 }
96 }
97
98 function after_duplicate($form_id, $values) {
99 $new_opts = $values['options'] = maybe_unserialize($values['options']);
100
101 if ( isset($new_opts['success_msg']) ) {
102 $new_opts['success_msg'] = FrmFieldsHelper::switch_field_ids($new_opts['success_msg']);
103 }
104
105 $new_opts = apply_filters('frm_after_duplicate_form_values', $new_opts, $form_id);
106
107 if ( $new_opts != $values['options'] ) {
108 global $wpdb;
109 $wpdb->update($wpdb->prefix .'frm_forms', array('options' => maybe_serialize($new_opts)), array('id' => $form_id));
110 }
111 }
112
113 function update( $id, $values, $create_link = false ) {
114 global $wpdb, $frm_field, $frm_settings;
115
116 if ( $create_link || isset($values['options']) || isset($values['item_meta']) || isset($values['field_options']) ) {
117 $values['status'] = 'published';
118 }
119
120 if ( isset($values['form_key']) ) {
121 $values['form_key'] = FrmAppHelper::get_unique_key($values['form_key'], $wpdb->prefix .'frm_forms', 'form_key', $id);
122 }
123
124 $form_fields = array( 'form_key', 'name', 'description', 'status', 'prli_link_id' );
125
126 $new_values = array();
127
128 if (isset($values['options'])){
129 $options = array();
130
131 $defaults = FrmFormsHelper::get_default_opts();
132 foreach ($defaults as $var => $default) {
133 if ( $var == 'notification' && !defined('WP_IMPORTING')) {
134 $options[$var] = isset($values[$var]) ? $values[$var] : $default;
135 } else {
136 $options[$var] = isset($values['options'][$var]) ? $values['options'][$var] : $default;
137 }
138 }
139
140 $options['custom_style'] = isset($values['options']['custom_style']) ? $values['options']['custom_style'] : 0;
141 $options['before_html'] = isset($values['options']['before_html']) ? $values['options']['before_html'] : FrmFormsHelper::get_default_html('before');
142 $options['after_html'] = isset($values['options']['after_html']) ? $values['options']['after_html'] : FrmFormsHelper::get_default_html('after');
143 $options['submit_html'] = (isset($values['options']['submit_html']) && $values['options']['submit_html'] != '') ? $values['options']['submit_html'] : FrmFormsHelper::get_default_html('submit');
144
145 $options = apply_filters('frm_form_options_before_update', $options, $values);
146 $new_values['options'] = serialize($options);
147 }
148
149 foreach ( $values as $value_key => $value ) {
150 if ( in_array($value_key, $form_fields) ) {
151 $new_values[$value_key] = $value;
152 }
153 }
154
155 if ( !empty($new_values) ) {
156 $query_results = $wpdb->update( $wpdb->prefix .'frm_forms', $new_values, array( 'id' => $id ) );
157 if ( $query_results ) {
158 wp_cache_delete( $id, 'frm_form');
159 }
160 } else {
161 $query_results = true;
162 }
163
164 $all_fields = $frm_field->getAll(array('fi.form_id' => $id), 'field_order');
165 if ( $all_fields && (isset($values['options']) || isset($values['item_meta']) || isset($values['field_options'])) ) {
166 if ( !isset($values['item_meta']) ) {
167 $values['item_meta'] = array();
168 }
169 $existing_keys = array_keys($values['item_meta']);
170
171 foreach ( $all_fields as $fid ) {
172 if ( !in_array($fid->id, $existing_keys) && ( isset($values['frm_fields_submitted']) && in_array($fid->id, $values['frm_fields_submitted']) ) || isset($values['options']) ) {
173 $values['item_meta'][$fid->id] = '';
174 }
175 }
176
177 foreach ( $values['item_meta'] as $field_id => $default_value ) {
178 $field = $frm_field->getOne($field_id);
179 if (!$field) continue;
180 $field_options = maybe_unserialize($field->field_options);
181
182 if ( isset($values['options']) || isset($values['field_options']['custom_html_'. $field_id]) ) {
183 //updating the settings page
184 if(isset($values['field_options']['custom_html_'.$field_id])){
185 $field_options['custom_html'] = isset($values['field_options']['custom_html_'.$field_id]) ? $values['field_options']['custom_html_'.$field_id] : (isset($field_options['custom_html']) ? $field_options['custom_html'] : FrmFieldsHelper::get_default_html($field->type));
186 $field_options = apply_filters('frm_update_form_field_options', $field_options, $field, $values);
187 $frm_field->update($field_id, array('field_options' => $field_options));
188 }else if($field->type == 'hidden' || $field->type == 'user_id'){
189 $prev_opts = $field_options;
190 $field_options = apply_filters('frm_update_form_field_options', $field_options, $field, $values);
191 if($prev_opts != $field_options)
192 $frm_field->update($field_id, array('field_options' => $field_options));
193 unset($prev_opts);
194 }
195 }
196
197 if ( (!isset($values['options']) && !isset($values['field_options']['custom_html_'. $field_id])) || defined('WP_IMPORTING') ) {
198 //updating the form
199 foreach ( array('size', 'max', 'label', 'invalid', 'blank', 'classes') as $opt ) {
200 $field_options[$opt] = isset($values['field_options'][$opt.'_'.$field_id]) ? trim($values['field_options'][$opt.'_'.$field_id]) : '';
201 }
202
203 $field_options['required_indicator'] = isset($values['field_options']['required_indicator_'. $field_id]) ? trim($values['field_options']['required_indicator_'. $field_id]) : '*';
204 $field_options['separate_value'] = isset($values['field_options']['separate_value_'.$field_id]) ? trim($values['field_options']['separate_value_'.$field_id]) : 0;
205
206 $field_options = apply_filters('frm_update_field_options', $field_options, $field, $values);
207 $default_value = maybe_serialize($values['item_meta'][$field_id]);
208 $field_key = (isset($values['field_options']['field_key_'.$field_id])) ? $values['field_options']['field_key_'.$field_id] : $field->field_key;
209 $required = (isset($values['field_options']['required_'.$field_id])) ? $values['field_options']['required_'.$field_id] : false;
210 $field_type = (isset($values['field_options']['type_'.$field_id])) ? $values['field_options']['type_'.$field_id] : $field->type;
211 $field_description = (isset($values['field_options']['description_'.$field_id])) ? $values['field_options']['description_'.$field_id] : $field->description;
212
213 $frm_field->update($field_id, array('field_key' => $field_key, 'type' => $field_type, 'default_value' => $default_value, 'field_options' => $field_options, 'description' => $field_description, 'required' => $required));
214 }
215 }
216 }
217
218 do_action('frm_update_form', $id, $values);
219 do_action('frm_update_form_'. $id, $values);
220
221 return $query_results;
222 }
223
224 function destroy( $id ){
225 global $wpdb, $frm_entry;
226
227 $form = $this->getOne($id);
228 if ( !$form || $form->default_template ) {
229 return false;
230 }
231
232 // Disconnect the items from this form
233 $entries = $frm_entry->getAll(array('it.form_id' => $id));
234 foreach ( $entries as $item ) {
235 $frm_entry->destroy($item->id);
236 unset($item);
237 }
238 // Disconnect the fields from this form
239 $query_results = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}frm_fields WHERE form_id=%d", $id));
240
241 $query_results = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}frm_forms WHERE id=%d", $id));
242 if ( $query_results ) {
243 do_action('frm_destroy_form', $id);
244 do_action('frm_destroy_form_'. $id);
245 }
246 return $query_results;
247 }
248
249 function getName( $id ) {
250 global $wpdb;
251 $cache = wp_cache_get($id, 'frm_form');
252 if ( $cache ) {
253 return stripslashes($cache->name);
254 }
255 $query = "SELECT name FROM {$wpdb->prefix}frm_forms WHERE ";
256 $query .= (is_numeric($id)) ? "id=%d" : "form_key=%s";
257 $query = $wpdb->prepare($query, $id);
258
259 $r = $wpdb->get_var($query);
260 return stripslashes($r);
261 }
262
263 function getIdByKey( $key ){
264 global $wpdb;
265 $query = $wpdb->prepare("SELECT id FROM {$wpdb->prefix}frm_forms WHERE form_key=%s LIMIT 1", $key);
266 return $wpdb->get_var($query);
267 }
268
269 function getOne( $id, $blog_id=false ){
270 global $wpdb, $frmdb;
271
272 if ( $blog_id && is_multisite() ) {
273 global $wpmuBaseTablePrefix;
274 $prefix = $wpmuBaseTablePrefix ? "{$wpmuBaseTablePrefix}{$blog_id}_" : $wpdb->get_blog_prefix( $blog_id );
275
276 $table_name = "{$prefix}frm_forms";
277 } else {
278 $table_name = $wpdb->prefix .'frm_forms';
279 $cache = wp_cache_get($id, 'frm_form');
280 if ( $cache ) {
281 if ( isset($cache->options) ) {
282 $cache->options = maybe_unserialize($cache->options);
283 }
284
285 return stripslashes_deep($cache);
286 }
287 }
288
289 $where = $wpdb->prepare( is_numeric($id) ? 'id=%d' : 'form_key=%s', $id );
290
291 $results = $wpdb->get_row("SELECT * FROM $table_name WHERE $where");
292
293 if ( isset($results->options) ) {
294 wp_cache_set($results->id, $results, 'frm_form');
295 $results->options = maybe_unserialize($results->options);
296 }
297 return stripslashes_deep($results);
298 }
299
300 function getAll( $where = array(), $order_by = '', $limit = '' ){
301 global $wpdb, $frmdb;
302
303 if(is_numeric($limit))
304 $limit = " LIMIT {$limit}";
305
306 $query = 'SELECT * FROM ' . $wpdb->prefix .'frm_forms' . FrmAppHelper::prepend_and_or_where(' WHERE ', $where) . $order_by . $limit;
307
308 if ($limit == ' LIMIT 1' || $limit == 1){
309 if(is_array($where))
310 $results = $frmdb->get_one_record($wpdb->prefix .'frm_forms', $where, '*', $order_by);
311 else
312 $results = $wpdb->get_row($query);
313
314 if($results){
315 wp_cache_set($results->id, $results, 'frm_form');
316 $results->options = maybe_unserialize($results->options);
317 }
318 }else{
319 if ( is_array($where) && !empty($where) ) {
320 $results = $frmdb->get_records($wpdb->prefix .'frm_forms', $where, $order_by, $limit);
321 } else {
322 $results = $wpdb->get_results($query);
323 }
324
325 if($results){
326 foreach($results as $result){
327 wp_cache_set($result->id, $result, 'frm_form');
328 $result->options = maybe_unserialize($result->options);
329 }
330 }
331 }
332
333 return stripslashes_deep($results);
334 }
335
336 function validate( $values ){
337 $errors = array();
338
339 /*if( $values['form_key'] == null || $values['form_key'] == '' ){
340 if( $values['name'] == null || $values['name'] == '' )
341 $errors[] = "Key can't be blank";
342 else
343 $_POST['form_key'] = $values['name'];
344 }*/
345
346 return apply_filters('frm_validate_form', $errors, $values);
347 }
348
349 }
350