hasOne::select()   C
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 34
rs 5.3846
cc 8
eloc 21
nc 9
nop 2
1
<?php namespace Mascame\Artificer\Fields\Types\Relations;
2
3
use Form;
4
use Input;
5
use Request;
6
use URL;
7
8
class hasOne extends Relation
9
{
10
11
    protected $id;
12
13
    protected function select($data, $show)
14
    {
15
        $select = array();
16
        foreach ($data as $d) {
17
18
            if (is_array($show)) {
19
                $value = '';
20
                foreach ($show as $show_key) {
21
                    $value .= \Str::title($show_key) . ': ' . $d[$show_key];
22
23
                    if (end($show) != $show_key) {
24
                        $value .= ' | ';
25
                    }
26
                }
27
            } else {
28
                $value = $d[$show];
29
            }
30
31
            $select[$d['id']] = $value;
32
        }
33
34
        if (Input::has($this->name)) {
35
            $this->id = Input::get($this->name);
36
        } else {
37
            if (isset($this->value->id)) {
38
                $this->id = $this->value->id;
39
            } else {
40
                $this->id = $this->value;
41
            }
42
        }
43
44
        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...
45
            $this->attributes->all());
46
    }
47
48
    protected function buttons()
49
    {
50
        if (!Request::ajax() || $this->showFullField) {
51
            $new_url = \URL::route('admin.model.create', array('slug' => $this->model['route']));
52
            $edit_url = \URL::route('admin.model.edit', array('slug' => $this->model['route'], 'id' => ':id:'));
53
            ?>
54
            <br>
55
            <div class="text-right">
56
                <div class="btn-group">
57
                    <button class="btn btn-default" data-toggle="modal"
58
                            data-url="<?= $edit_url ?>"
59
                            data-target="#form-modal-<?= $this->model['route'] ?>">
60
                        <i class="fa fa-edit"></i>
61
                    </button>
62
63
                    <button class="btn btn-default" data-toggle="modal"
64
                            data-url="<?= $new_url ?>"
65
                            data-target="#form-modal-<?= $this->model['route'] ?>">
66
                        <i class="fa fa-plus"></i>
67
                    </button>
68
                </div>
69
            </div>
70
            <?php
71
72
            $this->relationModal($this->model['route'], $this->id);
73
        }
74
    }
75
76
    public function input()
77
    {
78
        if (!$this->relation->getRelatedModel()) {
79
            throw new \Exception("Missing relation in config for '{$this->name}'.");
80
        }
81
82
        $this->model = $this->modelObject->schema->models[$this->relation->getRelatedModel()];
83
        $this->model['class'] = $modelClass = $this->modelObject->schema->getClass($this->model['name']);
84
        $show = $this->relation->getShow();
85
86
        $show_query = (is_array($show)) ? array('*') : array('id', $show);
87
        $data = $modelClass::all($show_query)->toArray();
88
89
        $this->select($data, $show);
90
        $this->buttons();
91
    }
92
93
    public function show($value = null)
94
    {
95
        $value = ($value) ?: $this->value;
96
97
        if (!$value) {
98
            return "<em>(none)</em>";
99
        }
100
101
        $show = $this->relation->getShow();
102
103
        if (!is_object($value)) {
104
            $model = '\\' . $this->relation->getRelatedModel();
105
106
            $data = $model::find($value);
107
108
            if (!$data) {
109
                return '(none)';
110
            }
111
112
            if (is_array($show)) {
113
                foreach ($show as $item) {
114
                    print $data->$item . "<br>";
115
                }
116
117
                return null;
118
            } else {
119
                return $data->$show;
120
            }
121
        }
122
123
        if (!$value) {
124
            throw new \Exception('The (hasOne) value is null');
125
        }
126
127
        $show = $this->options['relationship']['show'];
128
129
        print $value->$show;
130
131
        return null;
132
    }
133
134
}