CityController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Geo\City;
  3. use ORM\DBInstance;
  4. use \App\Core\Template\Output as Output;
  5. use App\Core\Datatables\Datatables as Datatables;
  6. use \App\Geo\City\Classes\City as City;
  7. class CityController extends \DefaultController
  8. {
  9. protected $_class = City::class;
  10. protected $_baseUrl = 'city';
  11. /**
  12. * Index
  13. * Show the main City list
  14. */
  15. function index()
  16. {
  17. Output::render('index');
  18. }
  19. /**
  20. * Create
  21. *
  22. * Render the main City formulary
  23. */
  24. function create()
  25. {
  26. Output::render('form', ['id' => 0]);
  27. }
  28. /**
  29. * Store
  30. *
  31. * Store the param on the database
  32. * @param City $city
  33. */
  34. function store(City $city)
  35. {
  36. }
  37. /**
  38. * Search
  39. *
  40. * Store the param on the database
  41. * @param City $city
  42. */
  43. function search()
  44. {
  45. }
  46. /**
  47. * Show
  48. *
  49. * Render one register
  50. *
  51. * @param City $city
  52. */
  53. function show($city)
  54. {
  55. if(is_ajax_request()){
  56. \RR\Response::json( City::findOne(['code' => ['=', $city]]) )->send();
  57. return;
  58. }else{
  59. $city = City::findOne($city);
  60. }
  61. }
  62. /**
  63. * Edit
  64. *
  65. * Render the formular for a database City
  66. *
  67. * @param City $city
  68. */
  69. function edit(City $city)
  70. {
  71. Output::render('form', $city);
  72. }
  73. /**
  74. * Update
  75. * Store the changes of the param on the database
  76. *
  77. * @param City $city
  78. */
  79. function update(City $city)
  80. {
  81. }
  82. /**
  83. * Destroy
  84. * If the object has soft delete.
  85. *
  86. * @param City $city
  87. */
  88. function destroy(City $city)
  89. {
  90. }
  91. /**
  92. * Purge
  93. * Remove object even with soft delete.
  94. *
  95. * @param City $city
  96. */
  97. function purge(City $city)
  98. {
  99. }
  100. public static function searchOrCreate($data)
  101. {
  102. if ($city = City::findOne(['code' => ['=', $data->ibge]])) {
  103. return $city;
  104. }
  105. $city = new City();
  106. $city->name = $data->localidade;
  107. $city->code = $data->ibge;
  108. $city->state_id = $data->state;
  109. $city->country_id = $data->country;
  110. $city->save();
  111. return $city;
  112. }
  113. function searchTable()
  114. {
  115. $records = [];
  116. $total = 0;
  117. try {
  118. if ($_POST['search']['value'] != '') {
  119. $search = [];
  120. foreach ($this->_class::_searchable as $searchable) {
  121. $search[$searchable] = ['OR like', $_POST['search']['value']];
  122. }
  123. $records = $this->_class::findMany($search, $this->_class::_listable)->get(true);
  124. } else {
  125. $records = $this->_class::findAll($this->_class::_listable, ['limit' => $_POST['length'], 'offset' => $_POST['start']])->get();
  126. }
  127. $total = DBInstance::queryOne("SELECT count(*) as count FROM {" . $this->_class::_tableName . '}')->count;
  128. } catch (\Exception $e) {
  129. if (APP_STATUS != 'production') {
  130. for ($i = $_POST['start']; $i <= $_POST['length'] + $_POST['start']; $i++) {
  131. $line = new \stdClass();
  132. $line->id = $i;
  133. $line->name = md5($i);
  134. $records[] = $line;
  135. }
  136. $total = 10000;
  137. }
  138. }
  139. if (!is_array($records)) {
  140. $records = [$records];
  141. }
  142. foreach ($records as $record) {
  143. if ($_GET['selective'] == 'true') {
  144. $record->id = Datatables::getActionMenu($record->id, $this->_baseUrl);
  145. } else {
  146. $record->id = Datatables::getSelectMenu($record->code);
  147. }
  148. }
  149. \RR\Response::json([
  150. 'draw' => $_POST['draw'],
  151. 'recordsTotal' => $total,
  152. 'recordsFiltered' => $total,
  153. 'data' => $records
  154. ])->send();
  155. }
  156. }