rzaglinskiy

Untitled

Jan 18th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.94 KB | None | 0 0
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3.  
  4. use Backpack\CRUD\app\Http\Controllers\CrudController;
  5. use Illuminate\Http\Request;
  6. use App\Models\Subscriber;
  7.  
  8. use App\Http\Requests\SubscribersStoreCrudRequest as StoreRequest;
  9. // VALIDATION
  10. use App\Http\Requests\SubscribersUpdateCrudRequest as UpdateRequest;
  11.  
  12. class SubscribersCrudController extends CrudController
  13. {
  14.  
  15.     private function setupAccess(){
  16.         $u = \Auth::user();
  17.         $this->crud->denyAccess(['list', 'update', 'delete', 'create']);
  18.         foreach($u->roles as $role) {
  19.             foreach($role->permissions as $permission) {
  20.                 $this->crud->allowAccess($permission->name);
  21.             }
  22.         }
  23.     }
  24.  
  25.     public function setup()
  26.     {
  27.         //parent::__construct();
  28.         $this->setupAccess();
  29.         $this->crud->enableAjaxTable();
  30.         $this->crud->enableDetailsRow();
  31.         $this->crud->enableExportButtons();
  32.         $this->crud->setModel("App\Models\Subscriber");
  33.         $this->crud->setRoute("admin/subscribers");
  34.         $this->crud->setEntityNameStrings('subscriber', 'subscribers');
  35.         $this->crud->orderBy('id', 'desc');
  36.         $this->crud->allowAccess('revisions');
  37.         $this->crud->setColumns([
  38.             ['name' => 'id', 'label' => 'Id'],
  39.             ['name' => 'email', 'label' => 'Email'],
  40.             ['name' => 'ip', 'label' => 'Ip'],
  41.             //['name' => 'visibility', 'label' => 'visibility'],
  42.             ['name' => 'active', 'label' => 'Subscribed', 'type'=>'boolean', 'options'=>[0=>'No', 1=>'yes']],
  43.             ['name' => 'create_date', 'label' => 'Created'],
  44.             ['label' => "Categories",
  45.                 'type' => 'select_multiple',
  46.                 'name' => 'category',
  47.                 'entity' => 'category',
  48.                 'attribute' => 'name',
  49.                 'model' => 'App\Models\Category',
  50.                 'pivot' => true],
  51.             ['name' => 'place', 'label' => 'Place', 'type'=>'subscriberstatus'],
  52.             ['name' => 'lon', 'label' => 'Lon', 'type'=>'subscriberstatus'],
  53.             ['name' => 'lat', 'label' => 'Lat', 'type'=>'subscriberstatus'],
  54.         ]);
  55.  
  56.         $this->crud->addField([
  57.             'name' => 'email',
  58.             'label' => "Email",
  59.             'type' => 'text'
  60.         ]);
  61.         $this->crud->addField([
  62.             'name' => 'place',
  63.             'label' => "Place",
  64.             'type' => 'text'
  65.         ]);
  66.         $this->crud->addField([
  67.             'name' => 'lon',
  68.             'label' => "lon",
  69.             'type' => 'text'
  70.         ]);
  71.         $this->crud->addField([
  72.             'name' => 'lat',
  73.             'label' => "lat",
  74.             'type' => 'text'
  75.         ]);
  76. //        $this->crud->addField([
  77. //            'name' => 'visibility',
  78. //            'label' => "Visibility",
  79. //            'type' => 'checkbox'
  80. //        ]);
  81.         $this->crud->addField([
  82.             'name' => 'active',
  83.             'label' => "Subscribed",
  84.             'type' => 'checkbox'
  85.         ]);
  86.         $this->crud->addField([
  87.             'name'=>'encryptedemail',
  88.             'label'=>'Encrypted email',
  89.             'type'=>'encryptedemail'
  90.         ]);
  91.         $this->crud->addField([       // SelectMultiple = n-n relationship (with pivot table)
  92.             'label' => "Reports",
  93.             'type' => 'select2_multiple',
  94.             'name' => 'reports', // the method that defines the relationship in your Model
  95.             'entity' => 'reports', // the method that defines the relationship in your Model
  96.             'attribute' => 'name', // foreign key attribute that is shown to user
  97.             'model' => 'App\Models\Report', // foreign key model
  98.             'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
  99.         ]);
  100.         $this->crud->addField([       // SelectMultiple = n-n relationship (with pivot table)
  101.             'label' => "Categories",
  102.             'type' => 'select2_multiple',
  103.             'name' => 'category', // the method that defines the relationship in your Model
  104.             'entity' => 'category', // the method that defines the relationship in your Model
  105.             'attribute' => 'name', // foreign key attribute that is shown to user
  106.             'model' => 'App\Models\Category', // foreign key model
  107.             'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
  108.         ]);
  109.  
  110.         $this->setupFilters();
  111.     }
  112.  
  113.     private function setupFilters(){
  114.         $this->crud->addFilter([
  115.             'name' => 'active',
  116.             'label' => 'Subscribed',
  117.             'type' => 'dropdown'
  118.         ],
  119.             function() {
  120.                 return [
  121.                     'no' => 'No',
  122.                     'yes' => 'Yes',
  123.                 ];
  124.             },
  125.             function($value) {
  126.                 if($value == 'no') $value = 0; else $value = 1;
  127.                 $this->crud->addClause('where', 'active', $value);
  128.             });
  129.         $this->crud->addFilter([
  130.             'name' => 'place',
  131.             'label' => 'NULL Place',
  132.             'type' => 'simple'
  133.         ],
  134.             false,
  135.             function() {
  136.                 $this->crud->addClause('whereNull', 'place');
  137.                 $this->crud->addClause('where', 'place', 'NULL');
  138.                 $this->crud->addClause('where', 'place', '');
  139.             });
  140.         $this->crud->addFilter([
  141.             'name' => 'lon',
  142.             'label' => 'NULL Longitude',
  143.             'type' => 'simple'
  144.         ],
  145.             false,
  146.             function() {
  147.                 $this->crud->addClause('whereNull', 'lon');
  148.             });
  149.         $this->crud->addFilter([
  150.             'name' => 'lat',
  151.             'label' => 'NULL Latitude',
  152.             'type' => 'simple'
  153.         ],
  154.             false,
  155.             function() {
  156.                 $this->crud->addClause('whereNull', 'lat');
  157.             });
  158.         $this->crud->addFilter([ // select2_multiple filter
  159.             'name' => 'category',
  160.             'type' => 'select2_multiple',
  161.             'label'=> 'Category'
  162.         ], function() {
  163.             foreach(\App\Models\Category::get() AS $k=>$v){
  164.                 $res[$v->id] = $v->name;
  165.             }
  166.             return($res);
  167.             //return \App\Models\Category::get()->keyBy('id')->pluck('name');
  168.         }, function($values) {
  169.             $values = json_decode($values);
  170.             unset($values[0]);
  171.              foreach ($values as $key => $value) {
  172.                  $this->crud->addClause('with', ['category' => function ($query) use($value) {
  173.                      $query->where('category_id', '=', $value);
  174.                  }]);
  175. //                 $this->crud->with(['category' => function ($query) use ($value) {
  176. //                     $query->where('category_id', '=', $value);
  177. //                 }]);
  178.              }
  179.         });
  180.     }
  181.  
  182.     public function edit($id)
  183.     {
  184.         $this->crud->hasAccessOrFail('update');
  185.         // get the info for that entry
  186.         $this->data['entry'] = $this->crud->getEntry($id);
  187.         $this->data['crud'] = $this->crud;
  188.         $this->data['fields'] = $this->crud->getUpdateFields($id);
  189.         $this->data['title'] = trans('backpack::crud.edit') . ' ' . $this->crud->entity_name;
  190.         $this->data['id'] = $id;
  191.         $dbRow = \DB::table('options')->where('name', 'googleMapsApiKey')->first();
  192.         $this->data['gApiKey'] = $dbRow->value;
  193.         // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
  194.         return view('admin.subscriber.edit', $this->data);
  195.     }
  196.  
  197.     public function showDetailsRow($id)
  198.     {
  199.         $collection = Subscriber::findOrFail($id);
  200.         return view('admin.subscriber.details', ['subscriber' => $collection]);
  201.     }
  202.  
  203.     public function store(StoreRequest $request)
  204.     {
  205.         return parent::storeCrud();
  206.     }
  207.  
  208.     public function update(UpdateRequest $request)
  209.     {
  210.         return parent::updateCrud();
  211.     }
  212. }
Add Comment
Please, Sign In to add comment