Guest User

Untitled

a guest
Mar 21st, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.60 KB | None | 0 0
  1. Area Code Timezone State Abbr Country Continent
  2. 204 CST Manitoba MB Canada North America
  3. 226 EST Ontario ON Canada North America
  4.  
  5. <?php
  6. error_reporting(0);
  7. require_once('parser/parser.php'); ##includes the name parser plugin
  8. if(!empty($_FILES["uploadedfile"]["name"])){ ## gets the uploaded excel file
  9. $target_dir = "uploads/";
  10. $target_file = $target_dir.substr(md5(rand()), 0, 7).basename($_FILES["uploadedfile"]["name"]);
  11.  
  12. if (move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $target_file)) {
  13. if (file_exists($target_file)) {
  14. $file = fopen($target_file,"r");
  15. ##fetches a list of timezone per state or country (if outside USA)
  16. $timezone = fopen("timezones.csv","r");
  17.  
  18. ##defined country array with country codes
  19. ##had to do this since long code is not allowed here :)
  20. ## I'm using $countryArray
  21. https://gist.github.com/josephilipraja/8341837
  22.  
  23. ##time zone csv to array
  24. $timezone_array = array();
  25. $tiemzone_counter = 0;
  26. while (($data_time = fgetcsv($timezone)) !== FALSE) {
  27. $timezone_array[$tiemzone_counter]['area_code'] = $data_time[0];
  28. $timezone_array[$tiemzone_counter]['timezone'] = $data_time[1];
  29. $timezone_array[$tiemzone_counter]['state'] = $data_time[2];
  30. $timezone_array[$tiemzone_counter]['abbr'] = $data_time[3];
  31. $timezone_array[$tiemzone_counter]['country'] = $data_time[4];
  32. $timezone_array[$tiemzone_counter]['continent'] = $data_time[5];
  33. $tiemzone_counter++;
  34. }
  35. array_shift($timezone_array);
  36.  
  37. ##cleaner part 1; removes unnecessary characters on the phone number
  38. $clean_numbers = array();
  39. $counter = 0;
  40. while (($data = fgetcsv($file)) !== FALSE) {
  41.  
  42. if(!empty($data[2])){
  43. ##removes non number characters
  44. $data[2] = preg_replace('~D~', '', $data[2]);
  45. if(strlen($data[2]) >= 10){
  46. $clean_numbers[$counter]['name'] = $data[0];
  47. $clean_numbers[$counter]['email'] = $data[1];
  48. $clean_part_1 = preg_replace('/^(011)/', '', $data[2]); ##removes leading 011
  49. $clean_part_2 = preg_replace('/^(0+)/', '', $clean_part_1); ##removes leading 0's
  50. if(strlen($clean_part_2) === 11){
  51. $clean_part_3 = preg_replace('/^(1)/', '', $clean_part_2); ##removes leading 1's
  52. }else{
  53. $clean_part_3 = $clean_part_2;
  54. }
  55. $clean_numbers[$counter]['numbers'] = $clean_part_3;
  56. $counter++;
  57. }
  58. }
  59. }
  60. ##removes duplicates
  61. $cleaned_data = unique_multidim_array($clean_numbers,'numbers');
  62. $so_cleaned_data = array_values($cleaned_data);
  63. $cleaned_data_size = sizeof($so_cleaned_data);
  64. $almost_cleaned = array();
  65. $header_array = array("Salutation", "First Name", "Middle Name", "Last Name", "Email Address", "Office Phone", "Area Code", "Time Zone", "Primary Address State", "Primary Address Country", "Continent", "Lead Source", "Tags", "Assigned User Name");
  66. $parser = new FullNameParser();
  67. ##creates the array to be converted to CSV and downloaded
  68. for($cleaned_count = 0; $cleaned_count < $cleaned_data_size; $cleaned_count++){
  69. if(strlen($so_cleaned_data[$cleaned_count]['numbers']) > 10){
  70. $key_country = searchInternational($countryArray, $so_cleaned_data[$cleaned_count]['numbers']);
  71. if(!empty($key_country)){
  72. $temp_areacode = $countryArray[$key_country]['code'];
  73. $prime_country = $countryArray[$key_country]['name'];
  74. $prime_state = $key_country;
  75. $continent = $countryArray[$key_country]['continent'];
  76. }
  77.  
  78. $limited_num = $so_cleaned_data[$cleaned_count]['numbers'];
  79. $found_key = $key_country;
  80. $timezone = "";
  81.  
  82.  
  83. }else{
  84. $temp_areacode = substr($so_cleaned_data[$cleaned_count]['numbers'], 0, -7);
  85. $found_key = searchthearray($timezone_array, 'area_code', $temp_areacode);
  86. $prime_state = $timezone_array[$found_key]['abbr'];
  87. $prime_country = $timezone_array[$found_key]['country'];
  88. $limited_num = $so_cleaned_data[$cleaned_count]['numbers'];
  89. $timezone = $timezone_array[$found_key]['timezone'];
  90. $continent = $timezone_array[$found_key]['continent'];
  91. }
  92. $parsed_name = $parser->parse_name($so_cleaned_data[$cleaned_count]['name']);
  93.  
  94. $almost_cleaned[$cleaned_count]['salutation'] = $parsed_name['salutation'];
  95. $almost_cleaned[$cleaned_count]['first_name'] = $parsed_name['fname'];
  96. $almost_cleaned[$cleaned_count]['middle_name'] = $parsed_name['initials'];
  97. $almost_cleaned[$cleaned_count]['last_name'] = $parsed_name['lname']." ".$parsed_name['suffix'];
  98. $almost_cleaned[$cleaned_count]['email_address'] = $so_cleaned_data[$cleaned_count]['email'];
  99. $almost_cleaned[$cleaned_count]['office_phone'] = $limited_num;
  100. $almost_cleaned[$cleaned_count]['area_code'] = (!empty($found_key))?$temp_areacode:"";
  101. $almost_cleaned[$cleaned_count]['timezone'] = (!empty($found_key))?$timezone:"";
  102. $almost_cleaned[$cleaned_count]['prime_state'] = (!empty($found_key))?$prime_state:"";
  103. $almost_cleaned[$cleaned_count]['prime_country'] = (!empty($found_key))?$prime_country:"";
  104. $almost_cleaned[$cleaned_count]['continent'] = (!empty($found_key))?$continent:"";
  105. $almost_cleaned[$cleaned_count]['lead_source'] = "Vendor";
  106. $almost_cleaned[$cleaned_count]['tags'] = "Predictive Dialing";
  107. $almost_cleaned[$cleaned_count]['assigned_user'] = "Predictive Dialing";
  108. }
  109.  
  110.  
  111. header("Content-Type: text/csv");
  112. header('Content-disposition: attachment;filename=leadsfortoday.csv');
  113. $fp = fopen("php://output", "w");
  114.  
  115. fputcsv ($fp, $header_array);
  116. foreach($almost_cleaned as $row){
  117. fputcsv($fp, $row);
  118. }
  119. die;
  120. fclose($fp);
  121.  
  122. header("Location: index.php");
  123. exit;
  124. }
  125. }
  126. }
  127.  
  128. function searchInternational($country, $number){
  129. foreach($country as $key => $value){
  130. preg_match('/^('.$value['code'].')/',$number,$matches);
  131. if(!empty($matches[1])){
  132. return $key;
  133. break;
  134. }
  135. }
  136.  
  137. }
  138.  
  139. function searchthearray($products, $field, $value){
  140. foreach($products as $key => $product){
  141. if ( $product[$field] === $value )
  142. return $key;
  143. }
  144. return false;
  145. }
  146.  
  147. function unique_multidim_array($array, $key) {
  148. $temp_array = array();
  149. $i = 0;
  150. $key_array = array();
  151.  
  152. foreach($array as $val) {
  153. if (!in_array($val[$key], $key_array)) {
  154. $key_array[$i] = $val[$key];
  155. $temp_array[$i] = $val;
  156. }
  157. $i++;
  158. }
  159. return $temp_array;
  160. }
  161. ?>
Add Comment
Please, Sign In to add comment