PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/dataTypes/Region/generate.php

http://github.com/benkeen/generatedata
PHP | 188 lines | 134 code | 33 blank | 21 comment | 51 complexity | caa824b602240de68194583daaf1c257 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. // this lets the processor know that this data type relies on data defined in other fields. That
  3. // information is either hardcoded in the functions below, or passed via an option
  4. $StateProvince_process_order = 2;
  5. // initialized by StateProvince_get_template_options()
  6. $StateProvince_regions = array();
  7. $StateProvince_region_countries = array();
  8. function StateProvince_get_template_options($postdata, $col, $num_cols)
  9. {
  10. global $StateProvince_regions;
  11. $country_choice = $postdata["countryChoice"];
  12. $options = array();
  13. foreach ($country_choice as $slug)
  14. {
  15. if (isset($postdata["includeRegion_{$slug}_$col"]))
  16. {
  17. $region_full = (isset($postdata["includeRegion_{$slug}_Full_$col"])) ? true : false;
  18. $region_short = (isset($postdata["includeRegion_{$slug}_Short_$col"])) ? true : false;
  19. $options[$slug] = array(
  20. "full" => $region_full,
  21. "short" => $region_short
  22. );
  23. }
  24. }
  25. if (empty($StateProvince_regions))
  26. $StateProvince_regions = gd_get_regions();
  27. return $options;
  28. }
  29. /**
  30. * This is pretty complicated and could be improved a LOT.
  31. *
  32. * @param $row
  33. * @param $options
  34. * @param $existing_row_data
  35. * @return unknown_type
  36. */
  37. function StateProvince_generate_item($row, $options, $existing_row_data)
  38. {
  39. global $StateProvince_regions, $StateProvince_region_countries;
  40. // if the user didn't select any options (i.e. regions), just return - nothing to display!
  41. if (empty($options))
  42. return;
  43. // see if this row has a country [N.B. This is something that could be calculated ONCE on the first row]
  44. $row_country_info = array();
  45. while (list($key, $info) = each($existing_row_data))
  46. {
  47. if ($info["data_type_folder"] == "Country")
  48. {
  49. $row_country_info = $info;
  50. break;
  51. }
  52. }
  53. // if it's not defined, just show a random region from the selected list of regions
  54. $region_info = array();
  55. $keys = array("region", "region_short");
  56. if (empty($row_country_info))
  57. {
  58. $rand_country_slug = array_rand($options);
  59. $index = "";
  60. if ($options[$rand_country_slug]["full"] == 1 && $options[$rand_country_slug]["short"] == 1)
  61. $index = rand(0, 1); // weird, rand()&1 doesn't work - always returns 1 0 1 0 1 0...
  62. else if ($options[$rand_country_slug]["full"] == 1)
  63. $index = 0;
  64. else if ($options[$rand_country_slug]["short"] == 1)
  65. $index = 1;
  66. if ($index === "")
  67. return;
  68. $region_info = $StateProvince_regions[$rand_country_slug][rand(0, count($StateProvince_regions[$rand_country_slug])-1)];
  69. $region_info["display"] = $region_info[$keys[$index]];
  70. }
  71. else
  72. {
  73. // if the random country generated for this row is a country that has mapped data (currently just 5),
  74. // then pick a region WITHIN that country
  75. $country_slug = $row_country_info["random_data"]["slug"];
  76. if (array_key_exists($country_slug, $options))
  77. {
  78. $index = "";
  79. if ($options[$country_slug]["full"] == 1 && $options[$country_slug]["short"] == 1)
  80. $index = rand(0, 1); // weird, rand()&1 doesn't work - always returns 1 0 1 0 1 0...
  81. else if ($options[$country_slug]["full"] == 1)
  82. $index = 0;
  83. else if ($options[$country_slug]["short"] == 1)
  84. $index = 1;
  85. if ($index === "")
  86. return;
  87. $region_info = $StateProvince_regions[$country_slug][rand(0, count($StateProvince_regions[$country_slug])-1)];
  88. $region_info["display"] = $region_info[$keys[$index]];
  89. }
  90. // otherwise just pick any region for one of the selected regions
  91. else
  92. {
  93. $rand_country_slug = array_rand($options);
  94. $index = "";
  95. if ($options[$rand_country_slug]["full"] == 1 && $options[$rand_country_slug]["short"] == 1)
  96. $index = rand(0, 1); // weird, rand()&1 doesn't work - always returns 1 0 1 0 1 0...
  97. else if ($options[$rand_country_slug]["full"] == 1)
  98. $index = 0;
  99. else if ($options[$rand_country_slug]["short"] == 1)
  100. $index = 1;
  101. if ($index === "")
  102. return;
  103. $region_info = $StateProvince_regions[$rand_country_slug][rand(0, count($StateProvince_regions[$rand_country_slug])-1)];
  104. $region_info["display"] = $region_info[$keys[$index]];
  105. }
  106. }
  107. return $region_info;
  108. }
  109. function StateProvince_get_export_type_info($export_type, $options)
  110. {
  111. $info = "";
  112. switch ($export_type)
  113. {
  114. case "sql":
  115. if ($options == "MySQL" || $options == "SQLite")
  116. $info = "varchar(50) default NULL";
  117. else if ($options == "Oracle")
  118. $info = "varchar2(50) default NULL";
  119. break;
  120. }
  121. return $info;
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. /**
  125. * Returns an array of first names
  126. */
  127. function gd_get_regions()
  128. {
  129. global $g_table_prefix, $StateProvince_region_countries;
  130. $query = mysql_query("
  131. SELECT *
  132. FROM {$g_table_prefix}countries
  133. WHERE has_full_data_set = 'yes'
  134. ");
  135. $regions = array();
  136. while ($row = mysql_fetch_assoc($query))
  137. {
  138. $country_id = $row["id"];
  139. $country_slug = $row["country_slug"];
  140. $query2 = mysql_query("SELECT * FROM {$g_table_prefix}regions WHERE country_id = $country_id");
  141. $region_list = array();
  142. while ($row2 = mysql_fetch_assoc($query2))
  143. {
  144. $region_list[] = array(
  145. "region" => $row2["region"],
  146. "region_id" => $row2["region_id"],
  147. "region_short" => $row2["region_short"],
  148. "weight" => $row2["weight"]
  149. );
  150. }
  151. $regions[$country_slug] = $region_list;
  152. $StateProvince_region_countries[] = $country_slug;
  153. }
  154. return $regions;
  155. }