Locations.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Admin;
  3. use AdminColumn;
  4. use AdminColumnEditable;
  5. use AdminDisplay;
  6. use AdminForm;
  7. use AdminFormElement;
  8. use AdminSection;
  9. use App\User;
  10. use App\Watcher;
  11. use Illuminate\Database\Eloquent\Model;
  12. use Illuminate\Support\Facades\Auth;
  13. use Illuminate\Support\Facades\Log;
  14. use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
  15. use SleepingOwl\Admin\Contracts\Form\FormInterface;
  16. use SleepingOwl\Admin\Contracts\Initializable;
  17. use SleepingOwl\Admin\Form\Buttons\Cancel;
  18. use SleepingOwl\Admin\Form\Buttons\Save;
  19. use SleepingOwl\Admin\Section;
  20. /**
  21. * Class Users
  22. *
  23. * @property \App\User $model
  24. *
  25. * @see http://sleepingowladmin.ru/docs/model_configuration_section
  26. */
  27. class Locations extends Section implements Initializable
  28. {
  29. /**
  30. * @see http://sleepingowladmin.ru/docs/model_configuration#ограничение-прав-доступа
  31. *
  32. * @var bool
  33. */
  34. protected $checkAccess = false;
  35. protected $redirect = ['edit' => 'display', 'create' => 'display'];
  36. /**
  37. * @var string
  38. */
  39. protected $title;
  40. public function initialize()
  41. {
  42. $this->creating(function ($config, Model $model) {
  43. });
  44. }
  45. /**
  46. * @var string
  47. */
  48. protected $alias;
  49. /**
  50. * @return DisplayInterface
  51. */
  52. public function onDisplay()
  53. {
  54. $form = AdminDisplay::datatablesAsync()->setDisplaySearch(true)
  55. ->setOrder([[0, 'desc']])
  56. ->setHtmlAttribute('class', 'table-primary')
  57. ->setColumns(
  58. AdminColumn::link('id', '#')->setWidth('30px'),
  59. AdminColumnEditable::text('lat', 'lat')->setWidth('100px'),
  60. AdminColumnEditable::text('lng', 'lng')->setWidth('100px'),
  61. AdminColumnEditable::text('created_at', 'Date')->setWidth('100px')
  62. )->paginate(20);
  63. $filters = request()->get('payload', []);
  64. Log::info($filters);
  65. if (! empty($filters['user_id'])) {
  66. $userId = $filters['user_id'];
  67. $form->setApply(function ($query) use ($userId) {
  68. $query->where('user_id', $userId);
  69. });
  70. }
  71. return $form;
  72. }
  73. /**
  74. * @param int $id
  75. *
  76. * @return FormInterface
  77. */
  78. public function onEdit($id)
  79. {
  80. $users = [];
  81. foreach (User::all() as $user) {
  82. $users[$user->id] = $user['name'];
  83. }
  84. $formElements = [
  85. AdminFormElement::select('user_id', 'User', $users),
  86. AdminFormElement::columns()
  87. ->addColumn(function () {
  88. return [
  89. AdminFormElement::text('lat', 'lat')
  90. ];
  91. })
  92. ->addColumn(function () {
  93. return [
  94. AdminFormElement::text('lng', 'lng')
  95. ];
  96. })
  97. ];
  98. $form = AdminForm::panel();
  99. $form->addBody($formElements);
  100. $form->getButtons()->setButtons([
  101. 'save' => new Save(),
  102. 'cancel' => new Cancel(),
  103. ]);
  104. return $form;
  105. }
  106. /**
  107. * @return FormInterface
  108. */
  109. public function onCreate()
  110. {
  111. return $this->onEdit(null);
  112. }
  113. /**
  114. * @return void
  115. */
  116. public function onDelete($id)
  117. {
  118. // remove if unused
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function onRestore($id)
  124. {
  125. // remove if unused
  126. }
  127. }