Completed
Push — master ( cf70e2...baec11 )
by David
02:17
created

Field::displayPrimaryField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Taskforcedev\CrudApi\Helpers;
4
5
/**
6
 * Class Field.
7
 */
8
class Field
9
{
10
    public $crudApi;
11
12
    /**
13
     * Field constructor.
14
     *
15
     * @param CrudApi $crudApi
16
     */
17
    public function __construct(CrudApi $crudApi)
18
    {
19
        $this->crudApi = $crudApi;
20
    }
21
22
    /**
23
     * Determine if the field is an id field.
24
     *
25
     * @param $field
26
     *
27
     * @return bool
28
     */
29
    public function isIdField($field)
30
    {
31
        return strpos($field, '_id') === false ? false : true;
32
    }
33
34
    /**
35
     * Parse a relation field name into the relation name.
36
     *
37
     * @param string $field Field name
38
     *
39
     * @return string
40
     */
41
    public function getRelatedField($field)
42
    {
43
        $relation = str_replace('_id', '', $field);
44
        return $relation;
45
    }
46
47
    /**
48
     * Retrieve the models primary field for display purposes.
49
     *
50
     * @param            $item   Model to retrieve primary field of.
51
     * @param null|array $config CrudApi Configuration.
52
     *
53
     * @return string
54
     */
55
    public function getPrimaryField($item, $config = null)
56
    {
57
        /* If config is not overridden then load crudapi config */
58
        if ($config === null) {
59
            $config = config('crudapi');
60
        }
61
        if (!isset($config['models']['fields']['default'])) {
62
            $defaultField = 'name';
63
        } else {
64
            $defaultField = $config['models']['fields']['default'];
65
        }
66
67
        /* Get the items Class */
68
        $class = get_class($item);
69
70
        $stripped_class = str_replace($this->crudApi->namespace, '', $class);
71
        // if class starts with a \ remove it.
72
        if (substr($stripped_class, 0, 1) == '\\') {
73
            $stripped_class = substr($stripped_class, 1);
74
        }
75
76
        $primaryFields = $config['models']['fields']['primary'];
77
78
        if (array_key_exists($stripped_class, $primaryFields)) {
79
            return $primaryFields[$stripped_class];
80
        } else {
81
            //return the default
82
            return $defaultField;
83
        }
84
    }
85
86
    public function displayPrimaryField($item, $config = null)
87
    {
88
        $field = $this->getPrimaryField($item, $config);
89
        return $item->$field;
90
    }
91
92
    /**
93
     * Render fields into appropriate format for an item creation form.
94
     *
95
     * @param $fields
96
     *
97
     * @return string
98
     */
99
    public function formCreate($fields)
100
    {
101
        $output = '';
102
103 View Code Duplication
        foreach ($fields as $f) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            $ucF = ucfirst($f);
105
106
            $input_attr = [
107
                'class' => 'form-control',
108
                'id' => 'createItem'.$f,
109
                'name' => $f,
110
            ];
111
112
            $output .= '<fieldset class="form-group">';
113
114
            $output .= '<label for="'.$input_attr['id'].'">'.$ucF.'</label>';
115
116
            if ($this->isIdField($f)) {
117
                $input_attr['type'] = 'select';
118
119
                $output .= '<select ';
120
                foreach ($input_attr as $attr => $value) {
121
                    $output .= "{$attr}='{$value}'";
122
                }
123
124
                $relation = $this->crudApi->getRelatedModel($f);
125
                $output .= '>';
126
127
                $output .= $this->crudApi->getRelatedOptions($relation);
128
129
                $output .= '</select>';
130
            } else {
131
                $input_attr['type'] = 'text';
132
133
                $output .= '<input ';
134
                foreach ($input_attr as $attr => $value) {
135
                    $output .= "{$attr}='{$value}'";
136
                }
137
                $output .= '>';
138
            }
139
140
            $output .= '</fieldset>';
141
        }
142
        return $output;
143
    }
144
145
    /**
146
     * Return fields as table headings.
147
     *
148
     * @param $fields
149
     *
150
     * @return string
151
     */
152
    public function tableHeadings($fields)
153
    {
154
        $output = '';
155
        foreach ($fields as $f) {
156
            $output .= '<th>'.ucfirst($f).'</th>';
157
        }
158
        return $output;
159
    }
160
161
    public function tableContent($fields, $instance)
162
    {
163
        $output = '';
164
        foreach ($fields as $f) {
165
            if ($this->isIdField($f)) {
166
                $related = $this->getRelatedField($f);
167
                $relation = $instance->$related;
168
                $field = $this->getPrimaryField($relation);
169
                if (strpos($field, ',')) {
170
                    $subfields = explode(',', $field);
171
                    $display = '';
172
                    foreach ($subfields as $sf) {
173
                        $display .= $relation->$sf . ' ';
174
                    }
175
                } else {
176
                    $display = $relation->$field;
177
                }
178
                $output .= '<td>' . $display . '</td>';
179
            } else {
180
                $output .= '<td>' . $instance->$f . '</td>';
181
            }
182
        }
183
        return $output;
184
    }
185
}
186