Passed
Push — master ( 4d8944...339eb8 )
by Iman
06:43
created

RowContent::addOtherColumns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 4
dl 0
loc 10
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
7
class RowContent
8
{
9
    private $cb;
10
11
    /**
12
     * HtmlContent constructor.
13
     *
14
     * @param $cb
15
     */
16
    public function __construct(CBController $cb)
17
    {
18
        $this->cb = $cb;
19
    }
20
21
    /**
22
     * @param $data
23
     * @param $tablePK
24
     * @param $number
25
     * @param $columnsTable
26
     * @param $table
27
     * @param $addAction
28
     *
29
     * @return array
30
     */
31
    public function calculate($data, $tablePK, $number, $columnsTable, $table, $addAction)
32
    {
33
        $tableRows = [];
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, $table, $row, $rowContent);
41
            $rowContent = $this->addActionButtons($addAction, $row, $rowContent);
42
            $rowContent = $this->performHookOnRow($rowContent);
43
            $tableRows[] = $rowContent;
44
            $number++;
45
        }
46
47
        return $tableRows;
48
    }
49
50
    /**
51
     * @param $id
52
     * @param $htmlContent
53
     * @return array
54
     */
55
    private function addCheckBox($id)
56
    {
57
        return "<input type='checkbox' class='checkbox' name='checkbox[]' value='".$id."'/>";
0 ignored issues
show
Bug Best Practice introduced by
The expression return '<input type='che... value='' . $id . ''/>' returns the type string which is incompatible with the documented return type array.
Loading history...
58
    }
59
60
    /**
61
     * @param $number
62
     * @param $htmlContent
63
     * @return array
64
     */
65
    private function addRowNumber($number, $htmlContent)
66
    {
67
        if ($this->cb->showNumbering) {
68
            $htmlContent[] = $number.'. ';
69
        }
70
71
        return $htmlContent;
72
    }
73
    /**
74
     * @param $columnsTable
75
     * @param $table
76
     * @param $row
77
     * @param $htmlContent
78
     * @return array
79
     */
80
    private function addOtherColumns($columnsTable, $table, $row, $htmlContent)
81
    {
82
        foreach ($columnsTable as $col) {
83
            if ($col['visible'] === false) {
84
                continue;
85
            }
86
            $htmlContent[] = (new ValueCalculator)->calculate($col, $row, $table, @$row->{$this->cb->titleField});
87
        }
88
89
        return $htmlContent;
90
    }
91
92
93
94
    /**
95
     * @param $htmlContent
96
     * @return mixed
97
     */
98
    private function performHookOnRow($htmlContent)
99
    {
100
        foreach ($htmlContent as $i => $v) {
101
            $this->cb->hookRowIndex($i, $v);
102
            $htmlContent[$i] = $v;
103
        }
104
105
        return $htmlContent;
106
    }
107
108
    /**
109
     * @param $addAction
110
     * @param $row
111
     * @param $htmlContent
112
     * @return array
113
     * @throws \Throwable
114
     */
115
    private function addActionButtons($addAction, $row, $htmlContent)
116
    {
117
        if (!$this->cb->buttonTableAction) {
118
            return $htmlContent;
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
        $htmlContent[] = "<div class='button_action' style='text-align:right'>".view('crudbooster::index.action', $data)->render().'</div>';
128
129
        return $htmlContent;
130
    }
131
132
}