Advertisement
dustin_lund

Gravity PHP Postcode

Jun 27th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. // 1 - Tie our validation function to the 'gform_validation' hook
  2. add_filter('gform_validation_1', 'checkPostcode');
  3. function checkPostcode (&$toCheck) {
  4.  
  5. if (!checkPostcode ($postcode) ) {
  6. echo 'Invalid postcode <br>';
  7. }
  8.  
  9.  
  10. // Permitted letters depend upon their position in the postcode.
  11. $alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
  12. $alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
  13. $alpha3 = "[abcdefghjkpmnrstuvwxy]"; // Character 3
  14. $alpha4 = "[abehmnprvwxy]"; // Character 4
  15. $alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
  16. $BFPOa5 = "[abdefghjlnpqrst]{1}"; // BFPO character 5
  17. $BFPOa6 = "[abdefghjlnpqrstuwzyz]{1}"; // BFPO character 6
  18.  
  19. // Expression for BF1 type postcodes
  20. $pcexp[0] = '/^(bf1)([[:space:]]{0,})([0-9]{1}' . $BFPOa5 . $BFPOa6 .')$/';
  21.  
  22. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA with a space
  23. $pcexp[1] = '/^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
  24.  
  25. // Expression for postcodes: ANA NAA
  26. $pcexp[2] = '/^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
  27.  
  28. // Expression for postcodes: AANA NAA
  29. $pcexp[3] = '/^('.$alpha1.'{1}'.$alpha2.'{1}[0-9]{1}'.$alpha4.')([[:space:]]{0,})([0-9]{1}'.$alpha5.'{2})$/';
  30.  
  31. // Exception for the special postcode GIR 0AA
  32. $pcexp[4] = '/^(gir)([[:space:]]{0,})(0aa)$/';
  33.  
  34. // Standard BFPO numbers
  35. $pcexp[5] = '/^(bfpo)([[:space:]]{0,})([0-9]{1,4})$/';
  36.  
  37. // c/o BFPO numbers
  38. $pcexp[6] = '/^(bfpo)([[:space:]]{0,})(c\/o([[:space:]]{0,})[0-9]{1,3})$/';
  39.  
  40. // Overseas Territories
  41. $pcexp[7] = '/^([a-z]{4})([[:space:]]{0,})(1zz)$/';
  42.  
  43. // Anquilla
  44. $pcexp[8] = '/^ai-2640$/';
  45.  
  46. // Load up the string to check, converting into lowercase
  47. $postcode = strtolower($toCheck);
  48.  
  49. // Assume we are not going to find a valid postcode
  50. $valid = false;
  51.  
  52. // Check the string against the six types of postcodes
  53. foreach ($pcexp as $regexp) {
  54.  
  55. if (preg_match($regexp,$postcode, $matches)) {
  56.  
  57. // Load new postcode back into the form element
  58. $postcode = strtoupper ($matches[1] . ' ' . $matches [3]);
  59.  
  60. // Take account of the special BFPO c/o format
  61. $postcode = preg_replace ('/C\/O([[:space:]]{0,})/', 'c/o ', $postcode);
  62.  
  63. // Take acount of special Anquilla postcode format (a pain, but that's the way it is)
  64. if (preg_match($pcexp[7],strtolower($toCheck), $matches)) $postcode = 'AI-2640';
  65.  
  66. // Remember that we have found that the code is valid and break from loop
  67. $valid = true;
  68. break;
  69. }
  70. }
  71.  
  72. // Return with the reformatted valid postcode in uppercase if the postcode was
  73. // valid
  74. if ($valid){
  75. $toCheck = $postcode;
  76. return true;
  77. }
  78. else return false;
  79. }
  80. /*gravity dustin*/
  81. /**
  82. * Better Pre-submission Confirmation
  83. * http://gravitywiz.com/2012/08/04/better-pre-submission-confirmation/
  84. */
  85.  
  86. class GWPreviewConfirmation {
  87.  
  88. private static $lead;
  89.  
  90. function init() {
  91.  
  92. add_filter('gform_pre_render', array('GWPreviewConfirmation', 'replace_merge_tags'));
  93. add_filter('gform_replace_merge_tags', array('GWPreviewConfirmation', 'product_summary_merge_tag'), 10, 3);
  94.  
  95. }
  96.  
  97. public static function replace_merge_tags($form) {
  98.  
  99. $current_page = isset(GFFormDisplay::$submission[$form['id']]) ? GFFormDisplay::$submission[$form['id']]['page_number'] : 1;
  100. $fields = array();
  101.  
  102. // get all HTML fields on the current page
  103. foreach($form['fields'] as &$field) {
  104.  
  105. // skip all fields on the first page
  106. if(rgar($field, 'pageNumber') <= 1)
  107. continue;
  108.  
  109. $default_value = rgar($field, 'defaultValue');
  110. preg_match_all('/{.+}/', $default_value, $matches, PREG_SET_ORDER);
  111. if(!empty($matches)) {
  112. // if default value needs to be replaced but is not on current page, wait until on the current page to replace it
  113. if(rgar($field, 'pageNumber') != $current_page) {
  114. $field['defaultValue'] = '';
  115. } else {
  116. $field['defaultValue'] = self::preview_replace_variables($default_value, $form);
  117. }
  118. }
  119.  
  120. // only run 'content' filter for fields on the current page
  121. if(rgar($field, 'pageNumber') != $current_page)
  122. continue;
  123.  
  124. $html_content = rgar($field, 'content');
  125. preg_match_all('/{.+}/', $html_content, $matches, PREG_SET_ORDER);
  126. if(!empty($matches)) {
  127. $field['content'] = self::preview_replace_variables($html_content, $form);
  128. }
  129.  
  130. }
  131.  
  132. return $form;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement