CFDBIntegrationCalderaForms

"Contact Form to Database" Copyright (C) 2011-2015 Michael Simpson (email : michael.d.simpson@gmail.com).

Defined (1)

The class is defined in the following location(s).

/CFDBIntegrationCalderaForms.php  
  1. class CFDBIntegrationCalderaForms { 
  2.  
  3. /** 
  4. * @var CF7DBPlugin 
  5. */ 
  6. var $plugin; 
  7.  
  8. /** 
  9. * @param $plugin CF7DBPlugin 
  10. */ 
  11. function __construct($plugin) { 
  12. $this->plugin = $plugin; 
  13.  
  14. public function registerHooks() { 
  15. // http://docs.calderaforms.com/caldera_forms_submit_post_process_end/ 
  16. add_action('caldera_forms_submit_post_process_end', array(&$this, 'saveFormData'), 10, 3); 
  17.  
  18. /** 
  19. * @param $form array 
  20. * @param $referrer array 
  21. * @param $process_id string 
  22. * @return bool 
  23. */ 
  24. public function saveFormData($form, $referrer, $process_id) { 
  25. try { 
  26. // debug 
  27. // $this->plugin->getErrorLog()->log('$form: ' . print_r($form, true)); 
  28. // $this->plugin->getErrorLog()->log('$referrer: ' . print_r($referrer, true)); 
  29. // $this->plugin->getErrorLog()->log('$process_id: ' . print_r($process_id, true)); 
  30. // global $processed_data; 
  31. // $this->plugin->getErrorLog()->log('$processed_data: ' . print_r($processed_data, true)); 
  32.  
  33. $data = $this->convertData($form); 
  34. return $this->plugin->saveFormData($data); 
  35. } catch (Exception $ex) { 
  36. $this->plugin->getErrorLog()->logException($ex); 
  37. return true; 
  38.  
  39. /** 
  40. * @param $form array 
  41. * @return null|object 
  42. */ 
  43. public function convertData($form) { 
  44. global $processed_data; 
  45.  
  46. if (is_array($form) && is_array($processed_data)) { 
  47. $title = $form['name']; 
  48. $postedData = array(); 
  49. $uploadedFiles = array(); 
  50.  
  51. $results = $processed_data[$form['ID']]; 
  52.  
  53. foreach ($results as $field_id => $field_value) { 
  54.  
  55. if (!array_key_exists($field_id, $form['fields'])) { 
  56. // ignore non-field entries _entry_id and _entry_token 
  57. continue; 
  58. $field_name = $form['fields'][$field_id]['label']; 
  59. $is_file = $form['fields'][$field_id]['type'] == 'file'; 
  60.  
  61. if (is_array($field_value)) { 
  62. $postedData[$field_name] = implode(', ', $field_value); 
  63. } else if ($is_file && $field_value != null) { 
  64. // $field_value is a URL to the file like 
  65. // http://SITE.com/wp-content/uploads/2015/05/my_file.png 
  66. $postedData[$field_name] = basename($field_value); 
  67. $path = get_home_path() . $this->getUrlWithoutSchemeHostAndPort($field_value); 
  68. $uploadedFiles[$field_name] = $path; 
  69. } else { 
  70. $postedData[$field_name] = $field_value; 
  71.  
  72. return (object)array( 
  73. 'title' => $title,  
  74. 'posted_data' => $postedData,  
  75. 'uploaded_files' => $uploadedFiles); 
  76. return null; 
  77.  
  78. /** 
  79. * Return the end part of a URL minus the "http://host:port" part 
  80. * @param $url string 
  81. * @return string|null 
  82. */ 
  83. public function getUrlWithoutSchemeHostAndPort($url) { 
  84. $matches = array(); 
  85. preg_match('#^http(s)?://[^/]+(.*)#', $url, $matches); 
  86. if (count($matches) >= 3) { 
  87. return $matches[2]; 
  88. return null;