zone_data.module 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. use Drupal\node\Entity\Node;
  3. use Drupal\node\Entity;
  4. use Drupal\taxonomy\Entity\Term;
  5. function zone_data_run() {
  6. $boutiquesUpdated = array();
  7. $storeList = normalizeChars(file_get_contents('public://geojson/halte.dat'));
  8. $storesArr = explode(PHP_EOL, $storeList);
  9. $file = normalizeChars(file_get_contents('public://geojson/quartier6.json'));
  10. $matchedStores = 0;
  11. $matchedNodes = 0;
  12. $storesNotFound = array();
  13. $nodesNotFound = array();
  14. $createdZones = array();
  15. if (!empty($geojson = json_decode($file))) {
  16. $numFeatures = count($geojson->features);
  17. // usort($geojson->features, "featureCompare");
  18. $x = 0;
  19. foreach ($storesArr as $store) {
  20. $match = false;
  21. foreach ($geojson->features as $feature) {
  22. if (isset($feature->properties->name)) {
  23. $name = $feature->properties->name;
  24. if (trim(strtolower($store)) === trim(strtolower($feature->properties->name))) {
  25. $match = true;
  26. $matchedStores++;
  27. $nQuery = db_query('SELECT nid FROM node_field_data WHERE title LIKE :title AND type = :type AND langcode = :langcode', array(
  28. ':title' => $store,
  29. ':type' => 'boutique',
  30. ':langcode' => 'en',
  31. ))->fetchField();
  32. if ($nQuery > 0) {
  33. $featureData = json_encode($feature->geometry);
  34. $matchedNodes++;
  35. $term = \Drupal\taxonomy\Entity\Term::create([
  36. 'name' => 'AD' . $x,
  37. 'vid' => 'zone',
  38. ])->save();
  39. if ($term) {
  40. $tidQ = array_values(\Drupal::entityQuery('taxonomy_term')
  41. ->condition('vid', 'zone')
  42. ->condition('name', 'AD' . $x)
  43. ->execute());
  44. }
  45. if ($tidQ != null && is_array($tidQ) && count($tidQ) > 0) {
  46. $zoneNodeSaveAttempt = null;
  47. if (isset($feature->properties->level)) {
  48. $zoneNode = Node::create([
  49. 'type' => 'zone',
  50. 'body' => $name,
  51. 'title' => 'AD' . $x,
  52. 'field_sector_code' => 55,
  53. 'field_feature_data' => $featureData,
  54. 'field_zone_code' => $tidQ[0],
  55. 'field_levels' => $feature->properties->level,
  56. ]);
  57. }
  58. $zoneNodeSaveAttempt = $zoneNode->save();
  59. if ($zoneNodeSaveAttempt === 1 || $zoneNodeSaveAttempt === 2) {
  60. $createdZones[] = $name;
  61. $boutiqueNode = Drupal\node\Entity\Node::load($nQuery);
  62. $boutiqueNode->field_zone_code = $tidQ[0];
  63. $boutiqueNodeSaveAttempt = $boutiqueNode->save();
  64. if ($boutiqueNodeSaveAttempt === 1 || $boutiqueNodeSaveAttempt === 2) {
  65. $boutiquesUpdated[] = $name;
  66. }
  67. }
  68. } else {
  69. $errMsg = 'zone term was not created for: ' . $store;
  70. \Drupal::logger('zone_data')->error($errMsg);
  71. }
  72. } else {
  73. $nodesNotFound[] = $store;
  74. }
  75. }
  76. }
  77. }
  78. if ($match == false) {
  79. $storesNotFound[] = $store;
  80. $logStr = '';
  81. foreach ($storesNotFound as $storeName) {
  82. $logStr .= $storeName . ', ';
  83. }
  84. $logStr = substr($logStr, -2);
  85. $errMsgAll = 'The following stores did not have zones created for them: ' . $logStr;
  86. \Drupal::logger('zone_data')->error($errMsgAll);
  87. }
  88. $x++;
  89. }
  90. }
  91. $name = 'test';
  92. return $createdZones;
  93. // return $term;
  94. }
  95. function normalizeChars($s) {
  96. $replace = array(
  97. 'Ă'=>'A', 'Ą'=>'A', 'À'=>'A', 'Ã'=>'A', 'Á'=>'A', 'Æ'=>'A', 'Â'=>'A', 'Å'=>'A', 'Ä'=>'Ae',
  98. 'È'=>'E', 'Ę'=>'E', 'É'=>'E', 'Ë'=>'E', 'Ê'=>'E',
  99. 'İ'=>'I', 'Ï'=>'I', 'Î'=>'I', 'Í'=>'I', 'Ì'=>'I',
  100. 'Ù'=>'U', 'Û'=>'U', 'Ú'=>'U', 'Ü'=>'Ue',
  101. 'â'=>'a', 'ǎ'=>'a', 'ą'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