PostalController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Geo\Postal;
  3. use \App\Core\Template\Output as Output;
  4. use \App\Geo\State\Classes\State as State;
  5. use \App\Geo\Postal\Classes\Postal as Postal;
  6. use App\Geo\City\Classes\City;
  7. //Facades
  8. use ORM\DBInstance;
  9. class PostalController
  10. {
  11. /**
  12. * Index
  13. * Show the main Postal list
  14. */
  15. function index()
  16. {
  17. $list = array();
  18. for ($i = 0; $i <= 10; $i++) {
  19. $obj = array();
  20. $obj['id'] = $i;
  21. $obj['value'] = md5($i);
  22. $obj['description'] = sha1($i);
  23. $list[] = $obj;
  24. }
  25. Output::render('index', array('list' => $list));
  26. }
  27. /**
  28. * Create
  29. *
  30. * Render the main Postal formulary
  31. */
  32. function create()
  33. {
  34. Output::setView('form', ['id' => 0]);
  35. Output::render();
  36. }
  37. /**
  38. * Store
  39. *
  40. * Store the param on the database
  41. * @param Postal $postal
  42. */
  43. function store(Postal $postal)
  44. {
  45. }
  46. /**
  47. * Search
  48. *
  49. * Store the param on the database
  50. * @param Postal $postal
  51. */
  52. function search($code)
  53. {
  54. //Search database for the record
  55. if ($json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $code]])) {
  56. $json = json_decode(stripslashes($json[0]->json));
  57. $json->cached = true;
  58. } else {
  59. $json = json_decode(file_get_contents('https://viacep.com.br/ws/' . $code . '/json'));
  60. }
  61. //Respond with errror if fail #TODO add message
  62. if (!$json) {
  63. http_response_code(404);
  64. return;
  65. }
  66. $state = State::findOne(['shortname' => ['=', $json->uf]]);
  67. $json->state = $state->id;
  68. if (!$json->cached) {
  69. $record = [];
  70. $record['code'] = $code;
  71. $record['json'] = addslashes(json_encode($json));
  72. DBInstance::insertRecord('geo_postal_caches', $record);
  73. } else {
  74. return \RR\Response::json($json)->send();
  75. }
  76. if (!$city = City::findOne(['code' => ['=', $json->ibge]])) {
  77. $city = new City();
  78. $city->state_id = $state->id;
  79. $city->country_id = $state->country_id;
  80. $city->code = $json->ibge;
  81. $city->name = $json->localidade;
  82. $city->save();
  83. }
  84. if (!$postal = Postal::findOne(['code' => ['=', $code]])) {
  85. $postal = new Postal();
  86. $postal->country_id = $state->country_id;
  87. $postal->state_id = $state->id;
  88. $postal->city_id = $city->id;
  89. @$postal->road = $json->road;
  90. $postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
  91. $postal->description = $json->complemento;
  92. $postal->save();
  93. }
  94. return \RR\Response::json($json)->send();
  95. }
  96. /**
  97. * Show *
  98. * Render one register
  99. *
  100. * @param Postal $postal
  101. */
  102. function show(Postal $postal)
  103. {
  104. }
  105. /**
  106. * Edit
  107. *
  108. * Render the formular for a database Postal
  109. *
  110. * @param Postal $postal
  111. */
  112. function edit(Postal $postal)
  113. {
  114. Output::render('form', $postal);
  115. }
  116. /**
  117. * Update
  118. * Store the changes of the param on the database
  119. *
  120. * @param Postal $postal
  121. */
  122. function update(Postal $postal)
  123. {
  124. }
  125. /**
  126. * Destroy
  127. * If the object has soft delete.
  128. *
  129. * @param Postal $postal
  130. */
  131. function destroy(Postal $postal)
  132. {
  133. }
  134. /**
  135. * Purge
  136. * Remove object even with soft delete.
  137. *
  138. * @param Postal $postal
  139. */
  140. function purge(Postal $postal)
  141. {
  142. }
  143. public static function searchAndCreate($code)
  144. {
  145. $code = preg_replace('/[^0-9]/is', '', $code);
  146. if ($postal = Postal::findOne(['code' => ['=', $code]])) {
  147. return $postal;
  148. }
  149. //Search database for the record of load the database
  150. if ($json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $code]])) {
  151. $json = json_decode(stripslashes($json[0]->json));
  152. $json->cached = true;
  153. } else {
  154. $json = json_decode(file_get_contents('https://viacep.com.br/ws/' . $code . '/json/unicode'));
  155. }
  156. $state = State::findOne(['shortname' => ['=', $json->uf]]);
  157. $json->state = $state->id;
  158. if (!$json->cached) {
  159. $record = [];
  160. $record['code'] = $code;
  161. $record['json'] = addslashes(json_encode($json));
  162. DBInstance::insertRecord('geo_postal_caches', $record);
  163. }
  164. if (!$city = City::findOne(['code' => ['=', $json->ibge]])) {
  165. $city = new City();
  166. $city->state_id = $state->id;
  167. $city->country_id = $state->country_id;
  168. $city->code = $json->ibge;
  169. $city->name = $json->localidade;
  170. $city->save();
  171. }
  172. if (!$postal = Postal::findOne(['code' => ['=', $code]])) {
  173. $postal = new Postal();
  174. $postal->country_id = $state->country_id;
  175. $postal->state_id = $state->id;
  176. $postal->city_id = $city->id;
  177. $postal->road = $json->logradouro;
  178. $postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
  179. $postal->description = $json->complemento;
  180. $postal->save();
  181. }
  182. return $postal;
  183. }
  184. public static function fixPostal($postal)
  185. {
  186. $json = DBInstance::getRecord('geo_postal_caches', ['code' => ['=', $postal->code]]);
  187. $json = json_decode(stripslashes($json[0]->json));
  188. $city = City::findOne(['code' => ['=', $json->ibge]]);
  189. $postal->code = preg_replace('/[^0-9]/is', '', $json->cep);
  190. $postal->city_id = $city->id;
  191. $postal->state_id = $city->state_id;
  192. $postal->country_id = $city->country_id;
  193. $postal->save();
  194. return $postal;
  195. }
  196. }