Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Admin;
- use Backpack\CRUD\app\Http\Controllers\CrudController;
- use Illuminate\Http\Request;
- use App\Models\Subscriber;
- use App\Http\Requests\SubscribersStoreCrudRequest as StoreRequest;
- // VALIDATION
- use App\Http\Requests\SubscribersUpdateCrudRequest as UpdateRequest;
- class SubscribersCrudController extends CrudController
- {
- private function setupAccess(){
- $u = \Auth::user();
- $this->crud->denyAccess(['list', 'update', 'delete', 'create']);
- foreach($u->roles as $role) {
- foreach($role->permissions as $permission) {
- $this->crud->allowAccess($permission->name);
- }
- }
- }
- public function setup()
- {
- //parent::__construct();
- $this->setupAccess();
- $this->crud->enableAjaxTable();
- $this->crud->enableDetailsRow();
- $this->crud->enableExportButtons();
- $this->crud->setModel("App\Models\Subscriber");
- $this->crud->setRoute("admin/subscribers");
- $this->crud->setEntityNameStrings('subscriber', 'subscribers');
- $this->crud->orderBy('id', 'desc');
- $this->crud->allowAccess('revisions');
- $this->crud->setColumns([
- ['name' => 'id', 'label' => 'Id'],
- ['name' => 'email', 'label' => 'Email'],
- ['name' => 'ip', 'label' => 'Ip'],
- //['name' => 'visibility', 'label' => 'visibility'],
- ['name' => 'active', 'label' => 'Subscribed', 'type'=>'boolean', 'options'=>[0=>'No', 1=>'yes']],
- ['name' => 'create_date', 'label' => 'Created'],
- ['label' => "Categories",
- 'type' => 'select_multiple',
- 'name' => 'category',
- 'entity' => 'category',
- 'attribute' => 'name',
- 'model' => 'App\Models\Category',
- 'pivot' => true],
- ['name' => 'place', 'label' => 'Place', 'type'=>'subscriberstatus'],
- ['name' => 'lon', 'label' => 'Lon', 'type'=>'subscriberstatus'],
- ['name' => 'lat', 'label' => 'Lat', 'type'=>'subscriberstatus'],
- ]);
- $this->crud->addField([
- 'name' => 'email',
- 'label' => "Email",
- 'type' => 'text'
- ]);
- $this->crud->addField([
- 'name' => 'place',
- 'label' => "Place",
- 'type' => 'text'
- ]);
- $this->crud->addField([
- 'name' => 'lon',
- 'label' => "lon",
- 'type' => 'text'
- ]);
- $this->crud->addField([
- 'name' => 'lat',
- 'label' => "lat",
- 'type' => 'text'
- ]);
- // $this->crud->addField([
- // 'name' => 'visibility',
- // 'label' => "Visibility",
- // 'type' => 'checkbox'
- // ]);
- $this->crud->addField([
- 'name' => 'active',
- 'label' => "Subscribed",
- 'type' => 'checkbox'
- ]);
- $this->crud->addField([
- 'name'=>'encryptedemail',
- 'label'=>'Encrypted email',
- 'type'=>'encryptedemail'
- ]);
- $this->crud->addField([ // SelectMultiple = n-n relationship (with pivot table)
- 'label' => "Reports",
- 'type' => 'select2_multiple',
- 'name' => 'reports', // the method that defines the relationship in your Model
- 'entity' => 'reports', // the method that defines the relationship in your Model
- 'attribute' => 'name', // foreign key attribute that is shown to user
- 'model' => 'App\Models\Report', // foreign key model
- 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
- ]);
- $this->crud->addField([ // SelectMultiple = n-n relationship (with pivot table)
- 'label' => "Categories",
- 'type' => 'select2_multiple',
- 'name' => 'category', // the method that defines the relationship in your Model
- 'entity' => 'category', // the method that defines the relationship in your Model
- 'attribute' => 'name', // foreign key attribute that is shown to user
- 'model' => 'App\Models\Category', // foreign key model
- 'pivot' => true, // on create&update, do you need to add/delete pivot table entries?
- ]);
- $this->setupFilters();
- }
- private function setupFilters(){
- $this->crud->addFilter([
- 'name' => 'active',
- 'label' => 'Subscribed',
- 'type' => 'dropdown'
- ],
- function() {
- return [
- 'no' => 'No',
- 'yes' => 'Yes',
- ];
- },
- function($value) {
- if($value == 'no') $value = 0; else $value = 1;
- $this->crud->addClause('where', 'active', $value);
- });
- $this->crud->addFilter([
- 'name' => 'place',
- 'label' => 'NULL Place',
- 'type' => 'simple'
- ],
- false,
- function() {
- $this->crud->addClause('whereNull', 'place');
- $this->crud->addClause('where', 'place', 'NULL');
- $this->crud->addClause('where', 'place', '');
- });
- $this->crud->addFilter([
- 'name' => 'lon',
- 'label' => 'NULL Longitude',
- 'type' => 'simple'
- ],
- false,
- function() {
- $this->crud->addClause('whereNull', 'lon');
- });
- $this->crud->addFilter([
- 'name' => 'lat',
- 'label' => 'NULL Latitude',
- 'type' => 'simple'
- ],
- false,
- function() {
- $this->crud->addClause('whereNull', 'lat');
- });
- $this->crud->addFilter([ // select2_multiple filter
- 'name' => 'category',
- 'type' => 'select2_multiple',
- 'label'=> 'Category'
- ], function() {
- foreach(\App\Models\Category::get() AS $k=>$v){
- $res[$v->id] = $v->name;
- }
- return($res);
- //return \App\Models\Category::get()->keyBy('id')->pluck('name');
- }, function($values) {
- $values = json_decode($values);
- unset($values[0]);
- foreach ($values as $key => $value) {
- $this->crud->addClause('with', ['category' => function ($query) use($value) {
- $query->where('category_id', '=', $value);
- }]);
- // $this->crud->with(['category' => function ($query) use ($value) {
- // $query->where('category_id', '=', $value);
- // }]);
- }
- });
- }
- public function edit($id)
- {
- $this->crud->hasAccessOrFail('update');
- // get the info for that entry
- $this->data['entry'] = $this->crud->getEntry($id);
- $this->data['crud'] = $this->crud;
- $this->data['fields'] = $this->crud->getUpdateFields($id);
- $this->data['title'] = trans('backpack::crud.edit') . ' ' . $this->crud->entity_name;
- $this->data['id'] = $id;
- $dbRow = \DB::table('options')->where('name', 'googleMapsApiKey')->first();
- $this->data['gApiKey'] = $dbRow->value;
- // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
- return view('admin.subscriber.edit', $this->data);
- }
- public function showDetailsRow($id)
- {
- $collection = Subscriber::findOrFail($id);
- return view('admin.subscriber.details', ['subscriber' => $collection]);
- }
- public function store(StoreRequest $request)
- {
- return parent::storeCrud();
- }
- public function update(UpdateRequest $request)
- {
- return parent::updateCrud();
- }
- }
Add Comment
Please, Sign In to add comment