Passed
Pull Request — master (#1139)
by Iman
03:48
created

CellContent::addRowNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
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 CellContent
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
    function calculate($data, $tablePK, $number, $columnsTable, $table, $addaction)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
32
    {
33
        $htmlContents = [];
34
        foreach ($data['result'] as $row) {
35
            $htmlContent = [];
36
37
            $htmlContent = $this->addCheckBox($tablePK, $row, $htmlContent);
38
            $htmlContent = $this->addRowNumber($number, $htmlContent);
39
            $htmlContent = $this->addOtherColumns($columnsTable, $table, $row, $htmlContent);
40
            $htmlContent = $this->addActionButtons($addaction, $row, $htmlContent);
41
            $htmlContent = $this->performHookOnRow($htmlContent);
42
            $htmlContents[] = $htmlContent;
43
            $number++;
44
        }
45
46
        return $htmlContents;
47
    }
48
49
    /**
50
     * @param $tablePK
51
     * @param $row
52
     * @param $htmlContent
53
     * @return array
54
     */
55
    private function addCheckBox($tablePK, $row, $htmlContent)
56
    {
57
        if ($this->cb->button_bulk_action) {
58
            $htmlContent[] = "<input type='checkbox' class='checkbox' name='checkbox[]' value='".$row->{$tablePK}."'/>";
59
        }
60
61
        return $htmlContent;
62
    }
63
64
    /**
65
     * @param $number
66
     * @param $htmlContent
67
     * @return array
68
     */
69
    private function addRowNumber($number, $htmlContent)
70
    {
71
        if ($this->cb->showNumbering) {
72
            $htmlContent[] = $number.'. ';
73
        }
74
75
        return $htmlContent;
76
    }
77
    /**
78
     * @param $columnsTable
79
     * @param $table
80
     * @param $row
81
     * @param $htmlContent
82
     * @return array
83
     */
84
    private function addOtherColumns($columnsTable, $table, $row, $htmlContent)
85
    {
86
        foreach ($columnsTable as $col) {
87
            if ($col['visible'] === false) {
88
                continue;
89
            }
90
            $htmlContent[] = (new ValueCalculator)->calculate($col, $row, $table, @$row->{$this->cb->title_field});
91
        }
92
93
        return $htmlContent;
94
    }
95
96
97
98
    /**
99
     * @param $htmlContent
100
     * @return mixed
101
     */
102
    private function performHookOnRow($htmlContent)
103
    {
104
        foreach ($htmlContent as $i => $v) {
105
            $this->cb->hookRowIndex($i, $v);
106
            $htmlContent[$i] = $v;
107
        }
108
109
        return $htmlContent;
110
    }
111
112
    /**
113
     * @param $addaction
114
     * @param $row
115
     * @param $htmlContent
116
     * @return array
117
     * @throws \Throwable
118
     */
119
    private function addActionButtons($addaction, $row, $htmlContent)
120
    {
121
        if (!$this->cb->buttonTableAction) {
122
            return $htmlContent;
123
        }
124
        $button_action_style = $this->cb->button_action_style;
125
        $button_edit = $this->cb->button_edit;
126
        $buttonDetail = $this->cb->buttonDetail;
127
        $deleteBtn = $this->cb->deleteBtn;
128
        $id = ($row->{$this->cb->primaryKey});
129
130
        $data = compact('addaction', 'row', 'id', 'button_action_style', 'parent_field', 'button_edit', 'deleteBtn', 'buttonDetail');
131
        $htmlContent[] = "<div class='button_action' style='text-align:right'>".view('crudbooster::index.action', $data)->render()."</div>";
132
133
        return $htmlContent;
134
    }
135
136
}