PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/dataTypes/Region/Region.class.php

http://github.com/benkeen/generatedata
PHP | 199 lines | 143 code | 33 blank | 23 comment | 26 complexity | 80bd1b80d699c58dd8b0d0cb47cd46d5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package DataTypes
  4. */
  5. class DataType_Region extends DataTypePlugin {
  6. protected $isEnabled = true;
  7. protected $dataTypeName = "Region";
  8. protected $dataTypeFieldGroup = "geo";
  9. protected $dataTypeFieldGroupOrder = 40;
  10. protected $processOrder = 2;
  11. protected $jsModules = array("Region.js");
  12. protected $cssFiles = array("Region.css");
  13. private $countryRegionHash;
  14. public function __construct($runtimeContext) {
  15. parent::__construct($runtimeContext);
  16. if ($runtimeContext == "generation") {
  17. $this->countryRegionHash = Core::$geoData->getCountryRegionHash();
  18. }
  19. }
  20. /**
  21. * Generate a random region, and return the display string and additional meta data for use
  22. * by any other Data Type.
  23. */
  24. public function generate($generator, $generationContextData) {
  25. $generationOptions = $generationContextData["generationOptions"];
  26. $regionInfo = array();
  27. $keys = array("region", "region_short");
  28. // if no country plugins were defined, we just randomly grab a region from what's available
  29. if ($generationOptions["resultType"] == "any") {
  30. $randRegionInfo = $this->getRandRegion($this->countryRegionHash);
  31. $index = mt_rand(0, 1);
  32. $regionInfo["display"] = $randRegionInfo[$keys[$index]];
  33. $regionInfo["region_slug"] = $randRegionInfo["region_slug"];
  34. $regionInfo["country_slug"] = $randRegionInfo["countrySlug"];
  35. // here, one or more Country plugins were included
  36. // - if there's a country row, pick a region within it.
  37. // - if not, pick any region within the list of country plugins
  38. } else {
  39. // see if this row has a country - TODO memoize
  40. $rowCountryInfo = array();
  41. while (list($key, $info) = each($generationContextData["existingRowData"])) {
  42. if ($info["dataTypeFolder"] == "Country") {
  43. $rowCountryInfo = $info;
  44. break;
  45. }
  46. }
  47. // if the data set didn't include a Country, just generate any old region pulled from any of the
  48. // Country plugins selected
  49. if (empty($rowCountryInfo)) {
  50. $randRegionInfo = $this->getRandRegion($generationOptions["countries"]);
  51. $randCountrySlug = $randRegionInfo["countrySlug"];
  52. // pick a format (short / long) based on whatever the specified through the UI
  53. $formatIndex = $this->getRandIndex($generationOptions["countries"], $randCountrySlug);
  54. $regionInfo["display"] = $randRegionInfo[$keys[$formatIndex]];
  55. $regionInfo["region_slug"] = $randRegionInfo["region_slug"];
  56. $regionInfo["country_slug"] = $randCountrySlug;
  57. // here, there *was* a country Data Type chosen and the Country row is pulling from the subset of
  58. // Country plugins
  59. } else {
  60. // the slug exists if the row country was one of the Country plugins
  61. $currRowCountrySlug = array_key_exists("slug", $rowCountryInfo["randomData"]) ? $rowCountryInfo["randomData"]["slug"] : "";
  62. // here, we've gotten the slug of the country for this particular row, but the user may have unselected
  63. // it from the row's generation options. See if it's available and if so, use that; otherwise, display
  64. // any old region from the selected Country plugins
  65. if (array_key_exists($currRowCountrySlug, $generationOptions["countries"])) {
  66. $regions = $this->countryRegionHash[$currRowCountrySlug];
  67. $regionInfo = $regions["regions"][mt_rand(0, $regions["numRegions"]-1)];
  68. $index = $this->getRandIndex($generationOptions["countries"], $currRowCountrySlug);
  69. $regionInfo["display"] = $regionInfo[$keys[$index]];
  70. } else {
  71. $randRegionInfo = $this->getRandRegion($generationOptions["countries"]);
  72. $currRowCountrySlug = $randRegionInfo["countrySlug"];
  73. $formatIndex = $this->getRandIndex($generationOptions["countries"], $currRowCountrySlug);
  74. $randCountry = $this->countryRegionHash[$currRowCountrySlug];
  75. $regionInfo = $randCountry["regions"][mt_rand(0, $randCountry["numRegions"]-1)];
  76. $regionInfo["display"] = $regionInfo[$keys[$formatIndex]];
  77. }
  78. $regionInfo["country_slug"] = $currRowCountrySlug;
  79. }
  80. }
  81. return $regionInfo;
  82. }
  83. public function getRowGenerationOptions($generator, $postdata, $colNum, $numCols) {
  84. $countries = $generator->getCountries();
  85. $generationOptions = array();
  86. // if the user didn't select any Country plugins, they want ANY old region
  87. if (empty($countries)) {
  88. $generationOptions["resultType"] = "any";
  89. } else {
  90. $generationOptions["resultType"] = "specificCountries";
  91. $generationOptions["countries"] = array();
  92. foreach ($countries as $slug) {
  93. if (isset($postdata["dtIncludeRegion_{$slug}_$colNum"])) {
  94. $region_full = (isset($postdata["dtIncludeRegion_{$slug}_Full_$colNum"])) ? true : false;
  95. $region_short = (isset($postdata["dtIncludeRegion_{$slug}_Short_$colNum"])) ? true : false;
  96. $generationOptions["countries"][$slug] = array(
  97. "full" => $region_full,
  98. "short" => $region_short
  99. );
  100. }
  101. }
  102. }
  103. return $generationOptions;
  104. }
  105. public function getOptionsColumnHTML() {
  106. $countryPlugins = Core::$countryPlugins;
  107. $html = "<div class=\"dtRegionCountry_noCountries\">{$this->L["no_countries_selected"]}</div>";
  108. foreach ($countryPlugins as $pluginInfo) {
  109. $slug = $pluginInfo->getSlug();
  110. $regionName = $pluginInfo->getRegionNames();
  111. $html .= <<<EOF
  112. <div class="dtRegionCountry dtRegionCountry_$slug">
  113. <input type="checkbox" name="dtIncludeRegion_{$slug}_%ROW%" id="dtIncludeRegion_{$slug}_%ROW%" class="dtIncludeRegion dtIncludeRegion_{$slug}"
  114. checked="checked" /><label for="dtIncludeRegion_{$slug}_%ROW%">$regionName</label>
  115. <span class="dtRegionFull">
  116. <input type="checkbox" name="dtIncludeRegion_{$slug}_Full_%ROW%" id="dtIncludeRegion_{$slug}_Full_%ROW%"
  117. checked="checked" /><label for="dtIncludeRegion_{$slug}_Full_%ROW%"
  118. id="dtIncludeRegion_{$slug}_FullLabel_%ROW%" class="dtRegionSuboptionActive">{$this->L["full"]}</label>
  119. </span>
  120. <span class="dtRegionShort">
  121. <input type="checkbox" name="dtIncludeRegion_{$slug}_Short_%ROW%" id="dtIncludeRegion_{$slug}_Short_%ROW%" checked="checked"
  122. /><label for="dtIncludeRegion_{$slug}_Short_%ROW%" id="dtIncludeRegion_{$slug}_ShortLabel_%ROW%"
  123. class="dtRegionSuboptionActive">{$this->L["short"]}</label>
  124. </span>
  125. </div>
  126. EOF;
  127. }
  128. $html .= '<div id="dtRegionCountry_Complete%ROW%"></div>';
  129. return $html;
  130. }
  131. public function getHelpHTML() {
  132. return "<p>{$this->L["help_text"]}</p>";
  133. }
  134. public function getDataTypeMetadata() {
  135. return array(
  136. "SQLField" => "varchar(50) default NULL",
  137. "SQLField_Oracle" => "varchar2(50) default NULL",
  138. "SQLField_MSSQL" => "VARCHAR(50) NULL"
  139. );
  140. }
  141. private function getRandIndex($options, $randCountrySlug) {
  142. $index = null;
  143. if ($options[$randCountrySlug]["full"] == 1 && $options[$randCountrySlug]["short"] == 1) {
  144. $index = mt_rand(0, 1); // weird, mt_rand()&1 doesn't work - always returns 1 0 1 0 1 0...
  145. } else if ($options[$randCountrySlug]["full"] == 1) {
  146. $index = 0;
  147. } else if ($options[$randCountrySlug]["short"] == 1) {
  148. $index = 1;
  149. }
  150. return $index;
  151. }
  152. private function getRandRegion($countries) {
  153. $randCountrySlug = array_rand($countries);
  154. $randCountry = $this->countryRegionHash[$randCountrySlug];
  155. // now get a random region, taking into account it's weight
  156. $weightedValues = array();
  157. $regions = $randCountry["regions"];
  158. for ($i=0; $i<count($regions); $i++) {
  159. $weightedValues["{$i}"] = $regions[$i]["weight"];
  160. }
  161. $randomIndex = Utils::weightedRand($weightedValues);
  162. $regionInfo = $regions[$randomIndex];
  163. $regionInfo["countrySlug"] = $randCountrySlug;
  164. return $regionInfo;
  165. }
  166. }