Passed
Push — master ( 6a6602...be985d )
by Iman
03:27
created

RowContent::_handleSubModules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace crocodicstudio\crudbooster\CBCoreModule\Index;
4
5
use crocodicstudio\crudbooster\controllers\CBController;
6
use crocodicstudio\crudbooster\helpers\CRUDBooster;
7
use crocodicstudio\crudbooster\helpers\DbInspector;
8
9
class RowContent
10
{
11
    private $cb;
12
13
    /**
14
     * HtmlContent constructor.
15
     *
16
     * @param $cb
17
     */
18
    public function __construct(CBController $cb)
19
    {
20
        $this->cb = $cb;
21
    }
22
23
    /**
24
     * @param $data
25
     * @param $number
26
     * @param $columnsTable
27
     *
28
     * @return array
29
     */
30
    public function calculate($data, $number, $columnsTable)
31
    {
32
        $tableRows = [];
33
        $tablePK = DbInspector::findPk($this->cb->table);
34
        foreach ($data['result'] as $row) {
35
            $rowContent = [];
36
            if ($this->cb->buttonBulkAction) {
37
                $rowContent[] = $this->addCheckBox($row->{$tablePK});
38
            }
39
            $rowContent = $this->addRowNumber($number, $rowContent);
40
            $rowContent = $this->addOtherColumns($columnsTable, $row, $rowContent);
41
            $rowContent = $this->addActionButtons($row, $rowContent);
42
            $rowContent = $this->performHookOnRow($rowContent);
43
            $tableRows[] = $rowContent;
44
            $number++;
45
        }
46
47
        return $tableRows;
48
    }
49
50
    /**
51
     * @param $id
52
     * @return string
53
     */
54
    private function addCheckBox($id)
55
    {
56
        return "<input type='checkbox' class='checkbox' name='checkbox[]' value='".$id."'/>";
57
    }
58
59
    /**
60
     * @param $number
61
     * @param $htmlContent
62
     * @return array
63
     */
64
    private function addRowNumber($number, $htmlContent)
65
    {
66
        if ($this->cb->showNumbering) {
67
            $htmlContent[] = $number.'. ';
68
        }
69
70
        return $htmlContent;
71
    }
72
    /**
73
     * @param $columnsTable
74
     * @param $row
75
     * @param $rowCells
76
     * @return array
77
     */
78
    private function addOtherColumns($columnsTable, $row, $rowCells)
79
    {
80
        foreach ($columnsTable as $col) {
81
            $rowCells[] = (new ValueCalculator)->calculate($col, $row, @$row->{$this->cb->titleField});
82
        }
83
84
        return $rowCells;
85
    }
86
87
88
89
    /**
90
     * @param $rowCells
91
     * @return mixed
92
     */
93
    private function performHookOnRow($rowCells)
94
    {
95
        foreach ($rowCells as $i => $v) {
96
            $rowCells[$i] = $this->cb->hookRowIndex($i, $v);
97
        }
98
99
        return $rowCells;
100
    }
101
102
    /**
103
     * @param $row
104
     * @param $rowCells
105
     * @return array
106
     * @throws \Throwable
107
     */
108
    private function addActionButtons($row, $rowCells)
109
    {
110
        //LISTING INDEX HTML
111
        $addAction = $this->cb->data['addAction'];
112
113
        if (! empty($this->cb->sub_module)) {
114
            $addAction = $this->_handleSubModules($addAction);
115
        }
116
117
        if (!$this->cb->showButtonsOnIndexRows) {
118
            return $rowCells;
119
        }
120
        $buttonActionStyle = $this->cb->buttonActionStyle;
121
        $buttonEdit = $this->cb->buttonEdit;
122
        $buttonDetail = $this->cb->buttonDetail;
123
        $deleteBtn = $this->cb->deleteBtn;
124
        $id = ($row->{$this->cb->primaryKey});
125
126
        $data = compact('addAction', 'row', 'id', 'buttonActionStyle', 'parent_field', 'buttonEdit', 'deleteBtn', 'buttonDetail');
127
        $rowCells[] = "<div class='button_action' style='text-align:right'>".view('crudbooster::index.action', $data)->render().'</div>';
128
129
        return $rowCells;
130
    }
131
132
    /**
133
     * @param $addAction
134
     * @return array
135
     */
136
    private function _handleSubModules($addAction)
137
    {
138
        foreach ($this->cb->sub_module as $module) {
139
            $addAction[] = [
140
                'label' => $module['label'],
141
                'icon' => $module['button_icon'],
142
                'url' => $this->subModuleUrl($module, CRUDBooster::parseSqlTable($this->table)['table']),
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on crocodicstudio\crudboost...Module\Index\RowContent. Did you maybe forget to declare it?
Loading history...
143
                'color' => $module['button_color'],
144
                'showIf' => $module['showIf'],
145
            ];
146
        }
147
148
        return $addAction;
149
    }
150
151
    /**
152
     * @param $module
153
     * @param $parentTable
154
     * @return string
155
     */
156
    private function subModuleUrl($module, $parentTable)
157
    {
158
        return CRUDBooster::adminPath($module['path']).'?parent_table='.$parentTable.'&parent_columns='
159
            .$module['parent_columns'].'&parent_columns_alias='
160
            .$module['parent_columns_alias'].'&parent_id=['
161
            .(! isset($module['custom_parent_id']) ? "id" : $module['custom_parent_id'])
162
            .']&return_url='.urlencode(request()->fullUrl()).'&foreign_key='
163
            .$module['foreign_key'].'&label='.urlencode($module['label']);
164
    }
165
166
}