PluginProbe
Smart Forms – when you need more than just a contact form / 0.5.5
Smart Forms – when you need more than just a contact form v0.5.5
trunk 0.5 0.5.5 0.6 0.7 0.8 0.8.5 0.9.1
smart-forms / smart-forms-ajax.php

smart-forms-ajax.php in Smart Forms – when you need more than just a contact form 0.5.5, at smart-forms-ajax.php

312 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 function GetPostValue($parameterName)
4 {
5 if(isset($_POST[$parameterName]))
6 return stripslashes($_POST[$parameterName]);
7
8 return "";
9 }
10
11 function rednao_smart_forms_save()
12 {
13 $form_id=GetPostValue("id");
14 $element_options=GetPostValue("element_options");
15 $form_options=GetPostValue("form_options");
16 $client_form_options=GetPostValue("client_form_options");
17
18 //$form_options=str_replace("\\\"","\"",$form_options);
19 $formParsedValues=json_decode($form_options);
20
21
22 if($formParsedValues->Name=="")
23 {
24 $message=__("Name is mandatory");
25 }else{
26
27 global $wpdb;
28 if($form_id=="0")
29 {
30 $count= $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM ".SMART_FORMS_TABLE_NAME." where form_name=%s",$formParsedValues->Name));
31
32 if($count>0)
33 {
34 $message=__("Form name already exists");
35
36 }else
37 {
38 $values=array('form_name'=>$formParsedValues->Name,
39 'element_options'=>$element_options,
40 'form_options'=>$form_options,
41 'client_form_options'=>$client_form_options
42 );
43
44 $wpdb->insert(SMART_FORMS_TABLE_NAME,$values);
45 $form_id=$wpdb->insert_id;
46 $message="saved";
47 delete_transient("rednao_smart_forms_$form_id");
48 }
49 }else
50 {
51 $wpdb->update(SMART_FORMS_TABLE_NAME,array(
52 'form_name'=>$formParsedValues->Name,
53 'element_options'=>$element_options,
54 'form_options'=>$form_options,
55 'client_form_options'=>$client_form_options
56 ),array("form_id"=>$form_id));
57 $message="saved";
58 delete_transient("rednao_smart_forms_$form_id");
59
60 }
61
62 }
63
64
65 echo "{\"FormId\":\"$form_id\",\"Message\":\"$message\"}";
66
67 die();
68 }
69
70
71 function rednao_smart_form_list()
72 {
73 if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') ) {
74 return;
75 }
76
77 global $wpdb;
78 $result=$wpdb->get_results("SELECT form_id,form_name FROM ".SMART_FORMS_TABLE_NAME);
79
80 echo "[{\"Id\":\"0\",\"Name\":\"Select a Form\"}";
81 foreach($result as $key=>$row)
82 {
83 echo ",{\"Id\":\"$row->form_id\",\"Name\":\"$row->form_name\"}";
84 }
85 echo"]";
86 die();
87 }
88
89
90 function rednao_smart_forms_save_form_values()
91 {
92
93 $form_id=GetPostValue("form_id");
94 $formString=GetPostValue("formString");
95
96 $entryData=json_decode($formString,true);
97
98 global $wpdb;
99 $result=$wpdb->get_results($wpdb->prepare("select form_options,element_options from ".SMART_FORMS_TABLE_NAME." where form_id=%d",$form_id));
100
101 $formOptions=null;
102 $elementOptions=null;
103
104 if(count($result)>0){
105 $formOptions=json_decode($result[0]->form_options,true);
106 $elementOptions=json_decode($result[0]->element_options,true);
107 }
108
109 $result=$wpdb->insert(SMART_FORMS_ENTRY,array(
110 'form_id'=>$form_id,
111 'date'=>date('Y-m-d H:i:s'),
112 'data'=>$formString,
113 'ip'=>$_SERVER['REMOTE_ADDR'],
114 ));
115
116 if($formOptions["SendNotificationEmail"]=="y")
117 send_form_email($formOptions["Emails"][0],$entryData,$elementOptions,false);
118
119 if($result==true)
120 echo '{"message":"'.__("Information submitted successfully.").'"}';
121 else
122 echo '{"message":"'.__("An error occurred, please try again later.").'"}';
123 die();
124 }
125
126 function send_form_email($formOptions,$entryData,$elementOptions,$useTestData)
127 {
128 include(SMART_FORMS_DIR.'string_renderer/rednao_string_builder.php');
129
130
131
132 $stringBuilder=new rednao_string_builder();
133 $EmailText=$formOptions["EmailText"];
134 $FromName=$formOptions["FromName"];
135 $FromEmail=$formOptions["FromEmail"];
136 $ToEmail=$formOptions["ToEmail"];
137 $EmailSubject=$formOptions["EmailSubject"];
138
139
140 if($FromName=="")
141 $FromName="Wordpress";
142
143 if($EmailSubject=="")
144 $EmailSubject="Form Submitted";
145
146 if($ToEmail=="")
147 $ToEmail=get_option("admin_email");
148
149 preg_match_all('/\\[field ([^\\]]+)/',$EmailText, $matches, PREG_PATTERN_ORDER);
150
151 foreach($matches[1] as $match)
152 {
153 $value=GetValueByField($stringBuilder,$match,$entryData,$elementOptions,$useTestData);
154 $EmailText=str_replace("[field $match]",$value,$EmailText);
155 }
156
157
158 /*
159
160
161 foreach($entryData as $key=>$value)
162 {
163
164 $element=null;
165 foreach($elementOptions as $item)
166 {
167 if($item->Id==$key)
168 {
169 $element=$item;
170 break;
171 }
172 }
173
174 $emailText.="<tr>". $stringBuilder->GetStringFromColumn($element,$value)."</tr>";
175 }
176 $emailText.="</table>";*/
177
178 $headers = 'MIME-Version: 1.0' . "\r\n";
179 $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
180 $headers.= "$FromName <$FromEmail>";
181 if(trim($ToEmail)!="")
182 {
183 return wp_mail($ToEmail, $EmailSubject, $EmailText, $headers);
184 }
185
186 return false;
187 }
188
189 function GetValueByField($stringBuilder,$match,$entryData,$elementOptions,$useTestData)
190 {
191 foreach($entryData as $key=>$value)
192 {
193 $element=null;
194 if($key!=$match)
195 continue;
196
197 $element=null;
198 foreach($elementOptions as $item)
199 {
200 if($item["Id"]==$key)
201 {
202 $element=$item;
203 break;
204 }
205 }
206 if($element==null)
207 continue;
208
209 $value= $stringBuilder->GetStringFromColumn($element,$value);
210 if($value==""&&$useTestData)
211 $value="sample text";
212 return $value;
213 }
214
215 if($useTestData)
216 return "sample text";
217 }
218
219 function rednao_smart_forms_entries_list()
220 {
221 $startDate=GetPostValue("startDate");
222 $endDate=GetPostValue("endDate");
223 $formId=GetPostValue("form_id");
224
225 $startDate=date('Y-m-d H:i:s', strtotime($startDate));
226 $endDate=date('Y-m-d H:i:s', strtotime($endDate .' +1 day'));
227
228 $query="select data from ".SMART_FORMS_ENTRY."
229 where date between '$startDate' and '$endDate' and form_id=$formId";
230
231
232 global $wpdb;
233 $result=$wpdb->get_results($query);
234 $isFirstRecord=true;
235
236 echo '{"entries":[';
237 foreach($result as $row)
238 {
239 if($isFirstRecord)
240 $isFirstRecord=false;
241 else
242 echo ",";
243
244 echo $row->data;
245
246 }
247 echo '],"formOptions":';
248
249 $query="select element_options from ".SMART_FORMS_TABLE_NAME." where form_id=".$formId;
250 $elementOptions=$wpdb->get_var($query);
251 echo $elementOptions.'}';
252
253
254 die();
255 }
256
257 function rednao_smart_form_send_test_email()
258 {
259 $FromEmail=GetPostValue("FromEmail");
260 $FromName=GetPostValue("FromName");
261 $ToEmail=GetPostValue("ToEmail");
262 $EmailSubject=GetPostValue("EmailSubject");
263 $EmailText=GetPostValue("EmailText");
264 $elementOptions=GetPostValue("element_options");
265
266 $elementOptions=json_decode($elementOptions);
267
268
269 $valueArray=Array(
270 "FromEmail"=>$FromEmail,
271 "FromName"=>$FromName,
272 "ToEmail"=>$ToEmail,
273 "EmailSubject"=>$EmailSubject,
274 "EmailText"=>$EmailText
275 );
276 $entryData=Array();
277
278
279 if($EmailText=="")
280 {
281 echo '{"Message":"'.__("Email text can't be empty").'"}';
282 die();
283 }
284
285 if(send_form_email($valueArray,$entryData,$elementOptions,true))
286 echo '{"Message":"'.__("Email sent successfully").'"}';
287 else
288 echo '{"Message":"'.__("There was an error sending the email, please check the configuration").'"}';
289 die();
290 }
291
292 function rednao_smart_forms_submit_license()
293 {
294 include_once(SMART_FORMS_DIR.'smart-forms-license.php');
295
296 $email=GetPostValue("email");
297 $key=GetPostValue("key");
298
299 if(smart_forms_check_license($email,$key,$error))
300 {
301 echo '{"IsValid":"y","Message":"'.__("License submitted successfully, thank you!!").'"}';
302 }else
303 {
304 if($error==null)
305 {
306 echo '{"IsValid":"n", "Message":"'.__("Invalid user or license").'"}';
307 }else
308 echo '{"IsValid":"n","Message":"'.__("An error occurred $error").'"}';
309 }
310
311 die();
312 }