Completed
Push — dev ( 1153e7...459fc5 )
by Marc
02:46
created

ModelController::destroy()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 3
eloc 4
nc 4
nop 2
1
<?php namespace Mascame\Artificer\Controllers;
2
3
use Input;
4
use Mascame\Artificer\Artificer;
5
use Mascame\Artificer\Options\AdminOption;
6
use Mascame\Artificer\Requests\ArtificerFormRequest;
7
use Redirect;
8
use Request;
9
use Response;
10
use URL;
11
use View;
12
13
class ModelController extends BaseModelController
14
{
15
16
    /**
17
     * Show the form for creating a new resource.
18
     *
19
     * @return Response
20
     */
21
    public function create()
22
    {
23
        $this->handleData($this->modelObject->schema->getInstance());
24
25
        $form = array(
26
            'form_action_route' => 'admin.model.store',
27
            'form_method' => 'post'
28
        );
29
30
        return View::make($this->getView('edit'))->with('items', $this->data)->with($form);
31
    }
32
33
    /**
34
     * @param $modelName
35
     * @return $this
36
     */
37
    public function filter($modelName)
38
    {
39
        $this->handleData($this->model->firstOrFail());
40
41
        $sort = $this->getSort();
42
43
        $data = $this->model->where(function ($query) {
44
45
            foreach (\Request::all() as $name => $value) {
46
                if ($value != '' && isset($this->fields[$name])) {
47
                    $this->fields[$name]->filter($query, $value);
48
                }
49
            }
50
51
            return null;
52
        })
53
            ->with($this->modelObject->getRelations())
54
            ->orderBy($sort['column'], $sort['direction'])
55
            ->get();
56
57
        return parent::all($modelName, $data, $sort);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (all() instead of filter()). Are you sure this is correct? If so, you might want to change this to $this->all().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
58
    }
59
60
    /**
61
     * Display the specified resource.
62
     *
63
     * @param  int $id
64
     * @return Response
65
     */
66
    public function show($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
    {
68
        $this->handleData($this->model->findOrFail($id));
69
70
        return View::make($this->getView('show'))->with('item', $this->data);
71
    }
72
73
    /**
74
     * Display the specified post.
75
     *
76
     * @return Response
77
     */
78
    public function all($modelName, $data = null, $sort = null)
79
    {
80
        $sort = $this->getSort();
81
82
        $data = $this->model->with($this->modelObject->getRelations())->orderBy($sort['column'],
83
            $sort['direction'])->get();
84
85
        return parent::all($modelName, $data, $sort);
86
    }
87
88
    /**
89
     * Show the form for editing the specified post.
90
     *
91
     * @param  int $id
92
     * @return Response
93
     */
94
    public function edit($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        $this->handleData(
97
            $this->model->with($this->modelObject->getRelations())->findOrFail($id)
98
        );
99
100
        $form = array(
101
            'form_action_route' => 'admin.model.update',
102
            'form_method' => 'put'
103
        );
104
105
        return View::make($this->getView('edit'))
106
            ->with('items', $this->data)
107
            ->with($form);
108
    }
109
110
    public function field($modelName, $id, $field)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
111
    {
112
        $this->handleData($this->model->with($this->modelObject->getRelations())->findOrFail($id));
113
114
        $this->fields[$field]->showFullField = true;
115
116
        return \HTML::field($this->fields[$field], AdminOption::get('icons'));
117
    }
118
119
    protected function handleAjaxResponse($item)
120
    {
121
        return Response::json(array(
122
                'item' => $item->toArray(),
123
                'refresh' => URL::route('admin.model.field.edit', array(
124
                    'slug' => Input::get('_standalone_origin'),
125
                    'id' => Input::get('_standalone_origin_id'),
126
                    'field' => ':fieldName:'
127
                ))
128
            )
129
        );
130
    }
131
132
    /**
133
     * Update or create the specified resource in storage.
134
     *
135
     * @param  int $id
136
     * @return Response
0 ignored issues
show
Bug introduced by
There is no parameter named $id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
137
     */
138
139
    public function updateOrCreate(ArtificerFormRequest $request)
140
    {
141
        $request->persist();
142
143
        // Todo
144
        //if (Request::ajax()) {
145
        //    return $this->handleAjaxResponse($model);
146
        //}
147
148
        return Redirect::route('admin.model.all', array('slug' => $this->modelObject->getRouteName()));
0 ignored issues
show
Bug introduced by
The method route() does not seem to exist on object<redirect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
149
    }
150
151
    /**
152
     * Remove the specified resource from storage.
153
     *
154
     * @param $modelName
155
     * @param $id
156
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
157
     */
158
    public function destroy($modelName, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $modelName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
159
    {
160
        if ($this->model->destroy($id)) {
161
            // Todo Notify::success('<b>Success!</b> The record has been deleted!', true);
162
        } else {
163
            // Todo Notify::danger('<b>Failed!</b> The record could not be deleted!');
164
        }
165
166
        return Request::ajax() ? \Response::json([]) : Redirect::back();
0 ignored issues
show
Bug introduced by
The method back() does not seem to exist on object<redirect>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
    }
168
169
}