Passed
Push — master ( a14023...ec1592 )
by Ferry
05:48
created

RowContent::performHookOnRow()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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
40
            $rowContent = $this->addRowNumber($number, $rowContent);
41
42
            $rowContent = $this->addOtherColumns($columnsTable, $row, $rowContent);
43
44
            $rowContent = $this->addActionButtons($row, $rowContent);            
45
46
            $tableRows[] = $rowContent;
47
48
            $number++;
49
        }
50
51
        return $tableRows;
52
    }
53
54
    /**
55
     * @param $id
56
     * @return string
57
     */
58
    private function addCheckBox($id)
59
    {
60
        return "<input type='checkbox' class='checkbox' name='checkbox[]' value='".$id."'/>";
61
    }
62
63
    /**
64
     * @param $number
65
     * @param $htmlContent
66
     * @return array
67
     */
68
    private function addRowNumber($number, $htmlContent)
69
    {
70
        if ($this->cb->showNumbering) {
71
            $htmlContent[] = $number.'. ';
72
        }
73
74
        return $htmlContent;
75
    }
76
    /**
77
     * @param $columnsTable
78
     * @param $row
79
     * @param $rowCells
80
     * @return array
81
     */
82
    private function addOtherColumns($columnsTable, $row, $rowCells)
83
    {
84
        foreach ($columnsTable as $col) {
85
            $rowCells[] = (new ValueCalculator)->calculate($col, $row, @$row->{$this->cb->titleField});
86
        }
87
88
        return $rowCells;
89
    }
90
91
92
    /**
93
     * @param $row
94
     * @param $rowCells
95
     * @return array
96
     * @throws \Throwable
97
     */
98
    private function addActionButtons($row, $rowCells)
99
    {
100
        //LISTING INDEX HTML
101
        $addAction = $this->cb->data['addAction'];
102
103
        if (! empty($this->cb->sub_module)) {
104
            $addAction = $this->_handleSubModules($addAction);
105
        }
106
107
        if (!$this->cb->buttonAction) {
108
            return $rowCells;
109
        }
110
        $buttonActionStyle = $this->cb->buttonActionStyle;
111
        $buttonEdit = $this->cb->buttonEdit;
112
        $buttonDetail = $this->cb->buttonDetail;
113
        $buttonDelete = $this->cb->buttonDelete;
114
        $id = ($row->{$this->cb->primaryKey});
115
116
        $data = compact('addAction', 'row', 'id', 'buttonActionStyle', 'parent_field', 'buttonEdit', 'buttonDelete', 'buttonDetail');
117
        $rowCells[] = "<div class='button_action' style='text-align:right'>".view('crudbooster::index.action', $data)->render().'</div>';
118
119
        return $rowCells;
120
    }
121
122
    /**
123
     * @param $addAction
124
     * @return array
125
     */
126
    private function _handleSubModules($addAction)
127
    {
128
        foreach ($this->cb->sub_module as $module) {
129
            $addAction[] = [
130
                'label' => $module['label'],
131
                'icon' => $module['button_icon'],
132
                '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...
133
                'color' => $module['button_color'],
134
                'showIf' => $module['showIf'],
135
            ];
136
        }
137
138
        return $addAction;
139
    }
140
141
    /**
142
     * @param $module
143
     * @param $parentTable
144
     * @return string
145
     */
146
    private function subModuleUrl($module, $parentTable)
147
    {
148
        return CRUDBooster::adminPath($module['path']).'?parent_table='.$parentTable.'&parent_columns='
149
            .$module['parent_columns'].'&parent_columns_alias='
150
            .$module['parent_columns_alias'].'&parent_id=['
151
            .(! isset($module['custom_parent_id']) ? "id" : $module['custom_parent_id'])
152
            .']&return_url='.urlencode(request()->fullUrl()).'&foreign_key='
153
            .$module['foreign_key'].'&label='.urlencode($module['label']);
154
    }
155
156
}