PluginProbe
Smart Forms – when you need more than just a contact form / trunk
Smart Forms – when you need more than just a contact form vtrunk
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 trunk, at smart-forms-ajax.php

864 lines 23.9 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 require_once SMART_FORMS_DIR.'utilities/JSONSanitizer.php';
6 if(isset($_POST[$parameterName]))
7 if(is_array(($_POST[$parameterName])))
8 return RednaoJSONSanitizer::Sanitize($_POST[$parameterName],(Object)['Data'=>(object)[]]);
9 else
10 return stripslashes(RednaoJSONSanitizer::Sanitize($_POST[$parameterName],(Object)['Data'=>(object)[]]));
11
12 return "";
13 }
14
15 function rednao_smart_forms_export(){
16
17 require_once SMART_FORMS_DIR.'smart-forms-export-form.php';
18
19
20 die();
21 }
22
23 function smart_forms_export(){
24 if(!wp_verify_nonce(GetPostValue('nonce'),'smart_forms_list_entries')||!current_user_can('edit_pages'))
25 {
26 echo json_encode(array(
27 'success'=>false,
28 'errorMessage'=>'Invalid Request'
29 ));
30 die();
31 }
32
33 require_once 'smart-forms-exporter.php';
34 }
35
36 function smart_forms_skip_tutorial(){
37 if(!wp_verify_nonce(GetPostValue('nonce'),'save_nonce')||!current_user_can('edit_pages'))
38 {
39 echo json_encode(array(
40 'success'=>false,
41 'errorMessage'=>'Invalid Request'
42 ));
43 die();
44 }
45
46 update_site_option('smart_forms_show_tutorial','y');
47 }
48
49 function smart_forms_save_settings(){
50 if((!wp_verify_nonce(GetPostValue('nonce'),'smart-forms-save-settings'))||!current_user_can('edit_pages'))
51 {
52 echo json_encode(array(
53 'success'=>false,
54 'message'=>'Invalid request'
55 ));
56 die();
57 }
58 $options=GetPostValue('options');
59 if($options=='')
60 return;
61 foreach($options as $value)
62 {
63 update_option($value['key'],$value['value']);
64 }
65 }
66
67 function rednao_smart_forms_resend_email(){
68 $data=GetPostValue('data');
69
70
71 require_once SMART_FORMS_DIR.'utilities/JSONSanitizer.php';
72 $data=RednaoJSONSanitizer::Sanitize(json_decode(stripslashes($_POST['data'])),(object)[
73 "EmailIndex"=>0,
74 "FormId"=>0,
75 "EntryId"=>0,
76 "Nonce"=>''
77 ]);
78
79 if(!wp_verify_nonce($data->Nonce,'smart_forms_list_entries')||!current_user_can('edit_pages'))
80 {
81 echo json_encode(array(
82 'success'=>false,
83 'errorMessage'=>'Invalid Request'
84 ));
85 die();
86 }
87 $emailIndex=$data->EmailIndex;
88 $form=$data->Form;
89 $entryId=$data->EntryId;
90
91 include_once(SMART_FORMS_DIR.'php_classes/save/php_entry_saver_base.php');
92
93 global $wpdb;
94 $entryRow=$wpdb->get_row($wpdb->prepare('select data from '.SMART_FORMS_ENTRY.' where entry_id=%d',$entryId));
95 $form=$wpdb->get_row($wpdb->prepare('select * from '.SMART_FORMS_TABLE_NAME.' where form_id=%d',$form));
96
97
98 $phpEntry=new php_entry_saver_base($data->Form,$entryRow->data,'n','',json_decode($form->form_options,true),json_decode($form->element_options,true));
99 if($phpEntry->FormOptions['Emails']!=null&&count($phpEntry->FormOptions['Emails'])>$emailIndex)
100 {
101 require_once SMART_FORMS_DIR.'php_classes/save/insert_entry_base.php';
102 require_once SMART_FORMS_DIR.'php_classes/save/pre_insert_entry.php';
103 $additionalData=[];
104 $preInsertedEntry=new PreInsertEntry($data->Form,$phpEntry->FormEntryData,$phpEntry->FormOptions,$phpEntry->ElementOptions,$additionalData);
105 $phpEntry->SendFormEmail($phpEntry->FormOptions['Emails'][$emailIndex],$phpEntry->FormEntryData,$phpEntry->ElementOptions,$preInsertedEntry,false);
106 }
107
108 echo json_encode(array(
109 'success'=>true,
110 'result'=>true
111 ));
112
113 die();
114
115 }
116
117 function rednao_smart_forms_save()
118 {
119 $form_id=GetPostValue("id");
120 $element_options=GetPostValue("element_options");
121 $form_options=GetPostValue("form_options");
122 $client_form_options=GetPostValue("client_form_options");
123 $donation_email=GetPostValue("donation_email");
124 $nonce='';
125 if(isset($_POST['nonce']))
126 $nonce=$_POST['nonce'];
127
128 if(!wp_verify_nonce($nonce,'save_nonce')||!current_user_can('manage_options'))
129 {
130 $form_id=intval($form_id);
131 echo "{\"FormId\":\"$form_id\",\"Message\":\"Invalid Request\"}";
132 die();
133 }
134 //$form_options=str_replace("\\\"","\"",$form_options);
135 $formParsedValues=json_decode($form_options);
136
137
138 if($formParsedValues->Name=="")
139 {
140 $message=__("Name is mandatory");
141 }else{
142
143 global $wpdb;
144 if($form_id=="0")
145 {
146 $count= $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM ".SMART_FORMS_TABLE_NAME." where form_name=%s",$formParsedValues->Name));
147
148 if($count>0)
149 {
150 $message=__("Form name already exists");
151
152 }else
153 {
154 $values=array('form_name'=>$formParsedValues->Name,
155 'element_options'=>$element_options,
156 'form_options'=>$form_options,
157 'client_form_options'=>$client_form_options,
158 'donation_email'=>$donation_email
159 );
160
161 $wpdb->insert(SMART_FORMS_TABLE_NAME,$values);
162 $form_id=$wpdb->insert_id;
163 $message="saved";
164 delete_transient("rednao_smart_forms_$form_id");
165 }
166 }else
167 {
168 $wpdb->update(SMART_FORMS_TABLE_NAME,array(
169 'form_name'=>$formParsedValues->Name,
170 'element_options'=>$element_options,
171 'form_options'=>$form_options,
172 'client_form_options'=>$client_form_options,
173 'donation_email'=>$donation_email
174 ),array("form_id"=>$form_id));
175 $message="saved";
176 delete_transient("rednao_smart_forms_$form_id");
177
178 }
179
180 }
181
182
183 echo json_encode(array(
184 'FormId'=>$form_id,
185 'Message'=>$message
186 ));
187
188 die();
189 }
190
191
192 function rednao_smart_form_short_code_setup()
193 {
194 if ( (! current_user_can('edit_posts') && ! current_user_can('edit_pages'))||!wp_verify_nonce($_POST['nonce'],'short_code_setup') ) {
195 return;
196 }
197
198 global $wpdb;
199
200 $shortCodeOptions=array();
201 array_push($shortCodeOptions,array(
202 "Name"=>"Forms",
203 "ShortCode"=>"sform",
204 "Elements"=>array()
205 ));
206
207 $result=$wpdb->get_results("SELECT form_id,form_name FROM ".SMART_FORMS_TABLE_NAME);
208 //echo "[{\"Id\":\"0\",\"Name\":\"Select a Form\"}";
209 foreach($result as $key=>$row)
210 {
211 array_push($shortCodeOptions[0]["Elements"],array(
212 "Id"=>$row->form_id,
213 "Name"=>$row->form_name
214 ));
215
216 }
217
218 $shortCodeOptions=apply_filters("smart_forms_get_short_code_options",$shortCodeOptions);
219
220 echo json_encode($shortCodeOptions);
221 die();
222 }
223
224
225
226
227 function rednao_smart_forms_save_form_values()
228 {
229 include_once(SMART_FORMS_DIR.'php_classes/save/php_entry_saver_base.php');
230
231 $form_id=GetPostValue("form_id");
232 $formString=GetPostValue("formString");
233
234 if(isset($_POST['nonce']))
235 $nonce=$_POST['nonce'];
236
237 $blockAnonymousSubmissions=get_option('smart_forms_block_anonymous_submissions');
238 if(apply_filters('smart_forms_block_annonymous_submissions',$blockAnonymousSubmissions)&&!wp_verify_nonce($nonce,'save_form'))
239 {
240 echo '{"message":"'.__("Invalid submission, please refresh your screen and try again.").'","refreshCaptcha":"n","success":"n"}';
241 die();
242 }
243
244 $captcha="";
245 if(isset($_POST["captcha"]))
246 if(is_array($_POST["captcha"]))
247 {
248 require_once SMART_FORMS_DIR.'utilities/JSONSanitizer.php';
249 $captcha=(array)RednaoJSONSanitizer::Sanitize($_POST["captcha"],(object)[
250 "version"=>"",
251 "response"=>''
252 ]);
253 }
254 else
255 $captcha=sanitize_text_field(stripslashes($_POST["captcha"]));
256
257 $phpEntry=new php_entry_saver_base($form_id,$formString,$captcha);
258 $phpEntry->ProcessEntry();
259 die();
260 }
261
262 function rednao_smart_forms_get_form_element_info()
263 {
264 $formId=GetPostValue("formId");
265 $nonce=GetPostValue("nonce");
266 if (( ! current_user_can('edit_posts') && ! current_user_can('edit_pages'))||!wp_verify_nonce($nonce,'save_nonce') ) {
267 return;
268 }
269
270 global $wpdb;
271 $result=$wpdb->get_var($wpdb->prepare("SELECT element_options FROM ".SMART_FORMS_TABLE_NAME.' where form_id=%d',$formId));
272
273 echo json_encode(array(
274 'elementsInfo'=>json_decode($result)
275 ));
276
277 die();
278 }
279
280 function rednao_smart_forms_get_form_options()
281 {
282 $formId=GetPostValue("formId");
283 $nonce=GetPostValue("nonce");
284 if (( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )||!wp_verify_nonce($nonce,'save_nonce')) {
285 return;
286 }
287
288 global $wpdb;
289 $result=$wpdb->get_results($wpdb->prepare("SELECT form_options,element_options FROM ".SMART_FORMS_TABLE_NAME.' where form_id=%d',$formId));
290
291 global $current_user;
292 echo json_encode(array(
293 'formOptions'=>json_decode($result[0]->form_options),
294 'elementOptions'=>json_decode($result[0]->element_options),
295 'CurrentEmail'=>strval($current_user->user_email)
296 ));
297 die();
298 }
299
300 function rednao_get_fixed_field_value($match,$entryData,$elementOptions,$useTestData)
301 {
302 require_once(SMART_FORMS_DIR.'filter_listeners/fixed-field-listeners.php');
303 $match=str_replace("'","\"",$match);
304 $fixedFieldParameters=json_decode($match,true);
305 if($fixedFieldParameters==null)
306 {
307 error_log('error parsing fixed field');
308 return '';
309 }
310
311 try{
312 $value=apply_filters("smart-forms-fixed-field-value-".$fixedFieldParameters["Op"],$fixedFieldParameters,$entryData,$elementOptions,$useTestData);
313 if($value==$fixedFieldParameters)
314 return "";
315 if($value==null)
316 $value='';
317 return strval($value);
318 }catch(Exception $e)
319 {
320 error_log("Couldn't format date ".$e->getMessage());
321 }
322
323 return "";
324 }
325
326 function GetValueByField($stringBuilder,$match,$entryData,$elementOptions,$useTestData)
327 {
328 if(strpos(trim($match),'{')===0)
329 {
330 return rednao_get_fixed_field_value($match,$entryData,$elementOptions,$useTestData);
331 }
332
333 foreach($entryData as $key=>$value)
334 {
335 $element=null;
336 if($key!=$match)
337 continue;
338
339 $element=null;
340 foreach($elementOptions as $item)
341 {
342 if($item["Id"]==$key)
343 {
344 $element=$item;
345 break;
346 }
347 }
348 if($element==null)
349 continue;
350
351 $value= $stringBuilder->GetStringFromColumn($element,$value);
352 if($value==""&&$useTestData)
353 $value="sample text";
354 return $value;
355 }
356
357 if($useTestData)
358 return "sample text";
359 }
360
361 function rednao_smart_forms_send_files(){
362 require_once SMART_FORMS_DIR.'smart_forms_uploader.php';
363 die();
364 }
365
366 function rednao_smart_form_delete_entries(){
367 $data=json_decode(GetPostValue('data'));
368
369 $nonce=$data->Nonce;
370
371 if(!wp_verify_nonce($nonce,'smart_delete_entry')||!current_user_can('edit_pages'))
372 {
373 echo json_encode(array(
374 'success'=>false,
375 'errorMessage'=>'Invalid nonce, please refresh your screen and try again'
376 ));
377 die();
378 }
379
380 if($data==null||!isset($data->Ids))
381 {
382 echo json_encode(array(
383 'success'=>false,
384 'errorMessage'=>'Invalid values'
385 ));
386 die();
387 }
388
389 global $wpdb;
390
391 $how_many = count($data->Ids);
392 $placeholders = array_fill(0, $how_many, '%d');
393 $format = implode(', ', $placeholders);
394 $query = "DELETE FROM ".SMART_FORMS_ENTRY." WHERE entry_id IN($format)";
395 $wpdb->query( $wpdb->prepare($query, $data->Ids) );
396
397
398 $query = "DELETE FROM ".SMART_FORMS_ENTRY_DETAIL." WHERE entry_id IN($format)";
399 $wpdb->query( $wpdb->prepare($query, $data->Ids) );
400
401 echo json_encode(array(
402 'success'=>true,
403 'result'=>true
404 ));
405
406 die();
407
408
409 }
410
411 function rednao_smart_forms_entries_list()
412 {
413 $data=json_decode(GetPostValue('data'));
414
415 if(!isset($data->nonce)||!wp_verify_nonce($data->nonce,'smart_forms_list_entries')||(!current_user_can('manage_options')&&!current_user_can('smart_forms_view_entries'))) {
416 echo json_encode(array(
417 'success' => false,
418 'errorMessage' => 'Invalid Request'
419 ));
420 die();
421 }
422 if($data==null)
423 {
424 echo json_encode(array(
425 'success'=>false,
426 'errorMessage'=>'Invalid values'
427 ));
428 die();
429 }
430
431 require_once SMART_FORMS_API;
432
433 $query=new SmartFormsQuery($data->Form);
434
435
436 global $wpdb;
437
438
439 foreach($data->Conditions as $condition)
440 {
441 if(isset($condition->FieldId)&&isset($condition->Comparison)&& $condition->FieldId!=''&&$condition->Comparison!='')
442 {
443 $query->AddCondition($condition->FieldId,$condition->Comparison,$condition->Value);
444 }
445 }
446
447 if($data->StartDate!=0)
448 {
449 $query->AddCondition('_Date','>=',get_gmt_from_date(date('Y-m-d H:i:s',$data->StartDate)));
450
451 }
452
453 if($data->EndDate!=0)
454 {
455 $query->AddCondition('_Date','<',get_gmt_from_date(date('Y-m-d H:i:s',$data->EndDate+86400)));
456 }
457
458
459 $result=$query->GetRaw($data->PageSize,$data->PageSize*$data->PageIndex);
460 $entries=array();
461 foreach($result as $row)
462 {
463 $entries[]=array(
464 "date"=>get_date_from_gmt($row['date']),
465 "entry_id"=>$row['entry_id'],
466 "user_name"=>$row['user_name'],
467 "user_id"=>$row['user_id'],
468 "user_ip"=>$row['user_ip'],
469 "data"=>json_decode($row['data'])
470 );
471
472 }
473 echo json_encode(array(
474 'success'=>true,
475 'result'=>array(
476 'rows'=>json_encode($entries),
477 'count'=>$query->GetCount()
478 )
479 ));
480 die();
481
482 }
483
484 function rednao_smart_form_send_test_email()
485 {
486 $FromEmail=GetPostValue("FromEmail");
487 $FromName=GetPostValue("FromName");
488 $ToEmail=rtrim(GetPostValue("ToEmail"),",");
489 $EmailSubject=GetPostValue("EmailSubject");
490 $EmailText=GetPostValue("EmailText");
491 $ReplyTo=GetPostValue("ReplyTo");
492 $Bcc=GetPostValue("Bcc");
493 $elementOptions=GetPostValue("element_options");
494 $pdfs=GetPostValue("PDFS");
495 $nonce=GetPostValue('nonce');
496
497 if(!wp_verify_nonce($nonce,'save_nonce')||!current_user_can('edit_pages'))
498 {
499 echo '{"Message":"'.__("Invalid request").'"}';
500 die();
501 }
502
503 if(!is_array($elementOptions))
504 $elementOptions=json_decode($elementOptions,true);
505
506
507 $valueArray=Array(
508 "FromEmail"=>$FromEmail,
509 "FromName"=>$FromName,
510 "ToEmail"=>$ToEmail,
511 "EmailSubject"=>$EmailSubject,
512 "EmailText"=>$EmailText,
513 "ReplyTo"=>$ReplyTo,
514 "PDFS"=>$pdfs,
515 "Bcc"=>$Bcc
516 );
517 $entryData=Array();
518
519
520 if($EmailText=="")
521 {
522 echo '{"Message":"'.__("Email text can't be empty").'"}';
523 die();
524 }
525
526 include_once(SMART_FORMS_DIR.'php_classes/save/php_entry_saver_base.php');
527 $entrySaver=new php_entry_saver_base("","","");
528 if($entrySaver->SendFormEmail($valueArray,$entryData,$elementOptions,null,true))
529 echo '{"Message":"'.__("Email sent successfully").'"}';
530 else
531 echo '{"Message":"'.__("There was an error sending the email, please check the configuration").'"}';
532 die();
533 }
534
535
536 function rednao_smart_forms_submit_license()
537 {
538 include_once(SMART_FORMS_DIR.'smart-forms-license.php');
539
540
541
542 $email=GetPostValue("email");
543 $key=GetPostValue("key");
544 $nonce=GetPostValue('nonce');
545 if(!wp_verify_nonce($nonce,'save_nonce')||!current_user_can('edit_pages'))
546 {
547 echo json_encode(array(
548 'success'=>false,
549 'errorMessage'=>'Invalid Request'
550 ));
551 die();
552 }
553
554 $license=smart_forms_check_license($email,$key,$error);
555 if($license["is_valid"])
556 {
557 echo '{"IsValid":"y","Message":"'.__("License submitted successfully, thank you!!").'","licenseType":""}';
558 }else
559 {
560 if($error==null)
561 {
562 echo '{"IsValid":"n", "Message":"'.__("Invalid user or license").'"}';
563 }else {
564 echo '{"IsValid":"n","Message":"' . __("An error occurred") . '"}';
565 }
566 }
567
568 die();
569 }
570
571 function rednao_smart_forms_execute_op()
572 {
573 $id=GetPostValue('TransactionId');
574 $oper=GetPostValue('oper');
575 $nonce=GetPostValue('nonce');
576
577 if (( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )||!wp_verify_nonce($nonce,'smart_forms_list_entries')) {
578 return;
579 }
580 if($oper=="del")
581 {
582 global $wpdb;
583 if($wpdb->query($wpdb->prepare("delete from ".SMART_FORMS_ENTRY." WHERE entry_id=%d",$id))>0)
584 {
585 $wpdb->query($wpdb->prepare("delete from ".SMART_FORMS_ENTRY_DETAIL." WHERE entry_id=%d",$id));
586 echo '{"success":"1"}';
587 }
588 else
589 echo '{"success":"0","message":"'.__("Could not delete row").'"}';
590 die();
591 }
592
593 if($oper=='massDelete')
594 {
595 global $wpdb;
596 $ids=GetPostValue('entriesToDelete');
597 $ids=json_decode($ids);
598 $idwhere='';
599 foreach($ids as $id)
600 {
601 if($idwhere!='')
602 $idwhere.=',';
603 $idwhere.=$wpdb->prepare('%d',$id);
604 }
605
606 $wpdb->query('delete from '.SMART_FORMS_ENTRY.' where entry_id in('.$idwhere.')');
607 $wpdb->query('delete from '.SMART_FORMS_ENTRY_DETAIL.' where entry_id in('.$idwhere.')');
608 echo '{"success":"1"}';
609 die();
610
611 }
612
613 }
614
615 function rednao_smart_forms_send_test()
616 {
617 if (( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )||!wp_verify_nonce(GetPostValue('nonce'),'save_nonce')) {
618 return;
619 }
620
621 require_once SMART_FORMS_DIR.'php_classes/smart_forms_troubleshoot/smart_forms_email.php';
622 switch(GetPostValue('Id'))
623 {
624 case "basic":
625 $smartFormsEmail=new smart_forms_email_troubleshoot_basic();
626 break;
627 case "custom":
628 $smartFormsEmail=new smart_forms_email_troubleshoot_custom_smtp();
629 break;
630 }
631 if($smartFormsEmail->Start())
632 echo json_encode(
633 array(
634 "Passed"=>'y'
635 ));
636 else
637 echo json_encode(
638 array(
639 "Passed"=>'n',
640 "Message"=>$smartFormsEmail->LatestError
641 ));
642 die();
643 }
644
645 function rednao_smart_forms_export_entries(){
646 if(!isset($_GET['data']))
647 {
648 echo "Invalid request";
649 die();
650 }
651
652 require_once SMART_FORMS_DIR.'utilities/JSONSanitizer.php';
653 $data=RednaoJSONSanitizer::Sanitize(json_decode(stripslashes($_GET['data'])),(object)[
654 "Nonce"=>'',
655 "Form"=>0,
656 "StartDate"=>0,
657 "EndDate"=>0,
658 "Conditions"=>[(object)[
659 "FieldId"=>"",
660 "Comparison"=>"",
661 "Value"=>"",
662
663 ]]
664 ]);
665
666 if($data==null)
667 {
668 echo "Invalid request";
669 die();
670 }
671
672 if(!wp_verify_nonce($data->Nonce,'smart_forms_export')||!current_user_can('manage_options'))
673 {
674 echo "Invalid request";
675 die();
676 }
677 require_once SMART_FORMS_API;
678 $query=new SmartFormsQuery($data->Form);
679
680
681 global $wpdb;
682
683
684 foreach($data->Conditions as $condition)
685 {
686 if(isset($condition->FieldId)&&isset($condition->Comparison)&& $condition->FieldId!=''&&$condition->Comparison!='')
687 {
688 $query->AddCondition($condition->FieldId,$condition->Comparison,$condition->Value);
689 }
690 }
691
692 if($data->StartDate!=0)
693 {
694 $query->AddCondition('_Date','>=',get_gmt_from_date(date('Y-m-d H:i:s',$data->StartDate)));
695
696 }
697
698 if($data->EndDate!=0)
699 {
700 $query->AddCondition('_Date','<',get_gmt_from_date(date('Y-m-d H:i:s',$data->EndDate+86400)));
701 }
702
703
704 $formName=$wpdb->get_var($wpdb->prepare('select form_name from '.SMART_FORMS_TABLE_NAME.' where form_id=%s',$data->Form));
705
706 $result=$query->GetResults('','','array');
707 $fh = fopen('php://output', 'w');
708 $headers=array();
709
710 header('Pragma: public');
711 header('Expires: 0');
712 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
713 header('Cache-Control: private', false);
714 header('Content-Type: application/octet-stream');
715 header('Content-Disposition: attachment; filename="' . str_replace('"', '\"', $formName[0].'_Export') . '.csv";');
716 header('Content-Transfer-Encoding: binary');
717
718 foreach($query->selectColumns as $column)
719 {
720 if($column=='_Date')
721 $headers[]='Date';
722 if($column=='_UserName')
723 $headers[]='User Name';
724
725 if(isset($query->fieldDictionary[$column]))
726 {
727 $field=$query->fieldDictionary[$column];
728 if($field['ClassName']=='rednaosurveytable')
729 {
730 foreach($field['Rows'] as $row)
731 {
732 $headers[]=$row['label'];
733 }
734 }else
735 $headers[]=$field['Label'];
736 }
737 }
738
739 fputcsv($fh,$headers);
740
741 foreach($result as $row)
742 {
743 $csvRow=array();
744 foreach($query->selectColumns as $column)
745 {
746 if($column=='_Date')
747 $csvRow[]=$row['_Date'];
748 if($column=='_UserName')
749 $csvRow[]=$row['_UserName'];
750
751
752 if(!isset($query->fieldDictionary[$column]))
753 continue;
754 $field=$query->fieldDictionary[$column];
755 if($field['ClassName']=='rednaosurveytable')
756 {
757 $raw=$row[$column]['raw'];
758 foreach($field['Rows'] as $fieldRow)
759 {
760 $value='';
761 if(isset($raw['values'])&&is_array($raw['values']))
762 {
763 foreach($raw['values'] as $surveyRow)
764 {
765 if(isset($surveyRow['QuestionLabel'])&&$surveyRow['QuestionLabel']==$fieldRow['label']){
766 $value=$surveyRow['ValueLabel'];
767 }
768 }
769 }
770
771 $csvRow[]=str_replace(array("\n", "\r"), ' ', $value);
772 }
773 }else
774 $csvRow[]=str_replace(array("\n", "\r"), ' ', $row[$column]['plain']);
775 }
776 fputcsv($fh,$csvRow);
777 }
778
779 die();
780
781
782 }
783
784 function rednao_smart_forms_edit_form_values()
785 {
786 $entryId=GetPostValue("entryId");
787 $entryString=GetPostValue("entryString");
788 $elementOptions=GetPostValue("elementOptions");
789 $nonce=GetPostValue("nonce");
790
791 if(!wp_verify_nonce($nonce,'smart_forms_edit_values')||!current_user_can('edit_pages'))
792 {
793 echo json_encode(array('result'=>false));
794 die();
795
796 }
797
798
799
800 include_once(SMART_FORMS_DIR.'php_classes/save/php_entry_editor.php');
801 $phpEditor=new php_entry_editor();
802 echo json_encode(array('result'=>$phpEditor->execute_editor($entryId,$entryString,$elementOptions)));
803 die();
804 }
805
806 function rednao_smart_forms_get_file(){
807 if(!isset($_GET['id']))
808 {
809 echo "Invalid request";
810 die();
811 }
812 $id=sanitize_text_field($_GET['id']);
813 global $wpdb;
814 $result=$wpdb->get_row($wpdb->prepare('select original_name,file_name,file_mime from '.SMART_FORMS_UPLOADED_FILES.' where file_key=%s',$id));
815 if($result==false)
816 {
817 echo 'File not found';
818 die();
819 }
820 header('Content-type: '.$result->file_mime);
821 header('Content-Disposition: attachment; filename="'.$result->original_name.'"');
822 readfile($result->file_name);
823 die();
824 }
825
826 function rednao_smart_forms_update_order()
827 {
828 if(!isset($_POST['nonce'])||!wp_verify_nonce($_POST['nonce'],'smart_forms_update_order')||!current_user_can('manage_options'))
829 {
830 echo json_encode(array(
831 'success'=>false,
832 'errorMessage'=>'Invalid Request'
833 ));
834 die();
835 }
836
837 if(!isset($_POST['order'])||!is_array($_POST['order']))
838 {
839 echo json_encode(array(
840 'success'=>false,
841 'errorMessage'=>'Invalid order data'
842 ));
843 die();
844 }
845
846 global $wpdb;
847 $order=array_map('intval',$_POST['order']);
848
849 foreach($order as $position=>$form_id)
850 {
851 $wpdb->update(
852 SMART_FORMS_TABLE_NAME,
853 array('form_order'=>$position),
854 array('form_id'=>$form_id),
855 array('%d'),
856 array('%d')
857 );
858 }
859
860 echo json_encode(array(
861 'success'=>true
862 ));
863 die();
864 }