Completed
Push — dev ( 726aeb...0f045d )
by Marc
02:06
created

hasOne   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 13
Bugs 3 Features 1
Metric Value
wmc 26
c 13
b 3
f 1
lcom 1
cbo 4
dl 0
loc 134
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A guessRelatedMethod() 0 14 4
C select() 0 34 8
B buttons() 0 29 2
A getData() 0 3 1
A input() 0 9 2
D show() 0 34 9
1
<?php namespace Mascame\Artificer\Fields\Types\Relations;
2
3
use Form;
4
use Illuminate\Support\Str;
5
use Input;
6
use Request;
7
use URL;
8
9
class hasOne extends Relation
10
{
11
    protected $id;
12
13
    public function guessRelatedMethod() {
14
        // case 'model_id'
15
        $method = str_replace('_id', '', $this->name);
16
17
        if ($this->modelHasMethod($method)) return $method;
18
19
        // case 'my_current_model_id'
20
        $method = explode('_', $this->name);
21
        $method = isset(array_reverse($method)[1]) ? array_reverse($method)[1] : null;
22
23
        if ($this->modelHasMethod($method)) return $method;
24
25
        return null;
26
    }
27
28
    protected function select($data, $show)
29
    {
30
        $select = array();
31
        foreach ($data as $d) {
32
33
            if (is_array($show)) {
34
                $value = '';
35
                foreach ($show as $show_key) {
36
                    $value .= Str::title($show_key) . ': ' . $d[$show_key];
37
38
                    if (end($show) != $show_key) {
39
                        $value .= ' | ';
40
                    }
41
                }
42
            } else {
43
                $value = $d[$show];
44
            }
45
46
            $select[$d['id']] = $value;
47
        }
48
49
        if (Input::has($this->name)) {
50
            $this->id = Input::get($this->name);
51
        } else {
52
            if (isset($this->value->id)) {
53
                $this->id = $this->value->id;
54
            } else {
55
                $this->id = $this->value;
56
            }
57
        }
58
59
        print Form::select($this->name, array('0' => Request::ajax() ? '(current)' : '(none)') + $select, $this->id,
0 ignored issues
show
Bug introduced by
The method select() does not seem to exist on object<form>.

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...
60
            $this->attributes);
61
    }
62
63
    protected function buttons()
64
    {
65
        // Todo: $this->showFullField ?
66
//        if (!Request::ajax() || $this->showFullField) {
67
        if ( ! Request::ajax()) {
68
            $new_url = \URL::route('admin.model.create', array('slug' => $this->relatedModel['route']));
69
            $edit_url = \URL::route('admin.model.edit', array('slug' => $this->relatedModel['route'], 'id' => ':id:'));
70
            ?>
71
            <br>
72
            <div class="text-right">
73
                <div class="btn-group">
74
                    <button class="btn btn-default" data-toggle="modal"
75
                            data-url="<?= $edit_url ?>"
76
                            data-target="#form-modal-<?= $this->relatedModel['route'] ?>">
77
                        <i class="fa fa-edit"></i>
78
                    </button>
79
80
                    <button class="btn btn-default" data-toggle="modal"
81
                            data-url="<?= $new_url ?>"
82
                            data-target="#form-modal-<?= $this->relatedModel['route'] ?>">
83
                        <i class="fa fa-plus"></i>
84
                    </button>
85
                </div>
86
            </div>
87
            <?php
88
89
            $this->relationModal($this->relatedModel['route'], $this->id);
90
        }
91
    }
92
93
    protected function getData() {
94
        return \View::getShared()['data'];
95
    }
96
97
    public function input()
98
    {
99
        $data = $this->getRelatedInstance()->all(
100
            (is_string($this->getShow())) ? ['id', $this->getShow()] : ['*']
101
        )->toArray();
102
103
        $this->select($data, $this->getShow());
104
        $this->buttons();
105
    }
106
107
    public function show($value = null)
108
    {
109
        $value = ($value) ?: $this->value;
110
111
        if ( ! $value) return "<em>(none)</em>";
112
113
        $show = $this->getShow();
114
115
        if ( ! is_object($value)) {
116
            $data = $this->getRelatedInstance()->findOrFail($value);
117
118
            if ( ! $data) return '(none)';
119
120
            if (is_array($show)) {
121
                foreach ($show as $item) {
122
                    print $data->$item . "<br>";
123
                }
124
125
                return null;
126
            } elseif (is_callable($show)) {
127
                return $show($data);
128
            } else {
129
                return $data->$show;
130
            }
131
        }
132
133
        if ( ! $value) {
134
            throw new \Exception('The (hasOne) value is null');
135
        }
136
137
        print $value->{$this->getShow()};
138
139
        return null;
140
    }
141
142
}