CFDBIntegrationGravityForms

"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).

/CFDBIntegrationGravityForms.php  
  1. class CFDBIntegrationGravityForms { 
  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. add_action('gform_after_submission', array(&$this, 'saveFormData'), 10, 2); 
  16.  
  17. public function saveFormData($entry, $form) { 
  18. try { 
  19. $data = $this->convertData($entry, $form); 
  20.  
  21. return $this->plugin->saveFormData($data); 
  22. } catch (Exception $ex) { 
  23. $this->plugin->getErrorLog()->logException($ex); 
  24. return true; 
  25.  
  26. /** 
  27. * http://www.gravityhelp.com/documentation/page/Gform_after_submission 
  28. * @param $entry Entry Object The entry that was just created. 
  29. * http://www.gravityhelp.com/documentation/page/Entry_Object 
  30. * @param $form Form Object The current form 
  31. * http://www.gravityhelp.com/documentation/page/Form_Object 
  32. * @return object 
  33. */ 
  34. public function convertData($entry, $form) { 
  35.  
  36. //$errorLog = $this->plugin->getErrorLog(); 
  37. //$errorLog->log('Form Definition: ' . print_r($form, true)); // debug 
  38. //$errorLog->log('Entry Definition: ' . print_r($entry, true)); // debug 
  39.  
  40. $postedData = array(); 
  41. $uploadFiles = array(); 
  42.  
  43. // Iterate through the field definitions and get their values 
  44. if (!is_array($form['fields'])) { 
  45. return true; 
  46. foreach ($form['fields'] as $field) { 
  47.  
  48. // Gravity Forms 1.8.5 $field was an array 
  49. // Gravity Forms 1.9.1.2 $field is an object 
  50. if (is_object($field)) { 
  51. $field = (array)$field; 
  52.  
  53. $fieldName = $field['label']; 
  54.  
  55. if (!empty($field['inputs']) && is_array($field['inputs'])) { 
  56. if ($field['type'] == 'checkbox') { 
  57. // This is a multi-input field 
  58. if (!isset($postedData[$fieldName]) || $postedData[$fieldName] === '') { // handle duplicate empty hidden fields 
  59. $values = array(); 
  60. foreach ($field['inputs'] as $input) { 
  61. $inputId = strval($input['id']); // Need string value of number like '1.3' 
  62. if (!empty($entry[$inputId])) { 
  63. $values[] = $entry[$inputId]; 
  64. $postedData[$fieldName] = implode(', ', $values); 
  65. } else { 
  66. foreach ($field['inputs'] as $input) { 
  67. $inputId = strval($input['id']); // Need string value of number like '1.3' 
  68. $label = $input['label']; // Assumption: all inputs have diff labels 
  69. $effectiveFieldName = $fieldName; 
  70. if (!empty($label)) { 
  71. $effectiveFieldName = $fieldName . ' ' . $label; 
  72. if (!isset($postedData[$effectiveFieldName]) || $postedData[$effectiveFieldName] === '') { // handle duplicate empty hidden fields 
  73. $postedData[$effectiveFieldName] = $entry[$inputId]; 
  74. } else { 
  75. $fieldId = $field['id']; 
  76. switch ($field['type']) { 
  77. case 'list' : 
  78. $list = unserialize($entry[$fieldId]); 
  79. if ($list) { 
  80. // $list may be a list of strings or 
  81. // or in the case of Gravity Form List with columns,  
  82. /** 
  83. Array 
  84. [0] => Array 
  85. [Column 1] => hi 
  86. [Column 2] => there 
  87. [Column 3] => howdy 
  88. */ 
  89. if (! empty($list) && is_array($list[0])) { 
  90. $colMatrix = array(); 
  91. foreach ($list as $colArray) { 
  92. $colList = array(); 
  93. foreach ($colArray as $colKey => $colValue) { 
  94. $colList[] = $colKey . '=' . $colValue; 
  95. $colMatrix[] = implode('|', $colList); 
  96. $postedData[$fieldName] = implode("\n", $colMatrix); 
  97. } else { 
  98. $postedData[$fieldName] = implode('|', $list); 
  99. } else { 
  100. if (!isset($postedData[$fieldName]) || $postedData[$fieldName] === '') { // handle duplicate empty hidden fields 
  101. // List - value is serialized array 
  102. $valueArray = @unserialize($entry[$fieldId]); 
  103. if (is_array($valueArray)) { 
  104. //$postedData[$fieldName] = ''; 
  105. // Array of (Array of column-name => value) 
  106. $tmpArray = array(); 
  107. foreach ($valueArray as $listArray) { 
  108. $tmpArray[] = implode(', ', array_values($listArray)); 
  109. $postedData[$fieldName] = implode('|', $tmpArray); 
  110. } else { 
  111. $postedData[$fieldName] = $entry[$fieldId]; 
  112. break; 
  113.  
  114. case 'fileupload': 
  115. if (!isset($postedData[$fieldName]) || $postedData[$fieldName] === '') { // handle duplicate empty hidden fields 
  116. // File Upload - value is file URL 
  117. // http://<SITE>/wp-content/uploads/gravity_forms/<PATH>/<FILE> 
  118. $url = $entry[$fieldId]; 
  119. $fileName = basename($url); 
  120. $postedData[$fieldName] = $fileName; 
  121.  
  122. $filePath = ABSPATH . substr($url, strlen(get_site_url())); 
  123. $uploadFiles[$fieldName] = $filePath; 
  124. break; 
  125.  
  126. default: 
  127. if (!isset($postedData[$fieldName]) || $postedData[$fieldName] === '') { // handle duplicate empty hidden fields 
  128. $postedData[$fieldName] = $entry[$fieldId]; 
  129. break; 
  130.  
  131.  
  132. // Other form metadata 
  133. $paymentMetaData = array( 
  134. //'currency',  
  135. 'payment_status', 'payment_date',  
  136. 'transaction_id', 'payment_amount', 'payment_method',  
  137. 'is_fulfilled', 'transaction_type'); 
  138. foreach ($paymentMetaData as $pmt) { 
  139. $hasPaymentInfo = false; 
  140. if (!empty($entry[$pmt])) { 
  141. $postedData[$pmt] = $entry[$pmt]; 
  142. $hasPaymentInfo = true; 
  143. if ($hasPaymentInfo && !empty($entry['currency'])) { 
  144. // It seems currency is always set but only meaningful 
  145. // if the other payment info is set. 
  146. $postedData['currency'] = $entry['currency']; 
  147.  
  148.  
  149. return (object)array( 
  150. 'title' => $form['title'],  
  151. 'posted_data' => $postedData,  
  152. 'uploaded_files' => $uploadFiles); 
  153.  
  154.