Grid::renderNavigationBar()   B
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 15
nc 3
nop 1
1
<?php
2
/**
3
 * @company MTE Telecom, Ltd.
4
 * @author Roman Malashin <[email protected]>
5
 */
6
7
namespace Nnx\DataGrid\View\Helper\JqGrid;
8
9
use Nnx\DataGrid\Mutator\HighlightMutatorInterface;
10
use Nnx\DataGrid\PaginatorGridInterface;
11
use Zend\View\Helper\AbstractHelper;
12
use Nnx\DataGrid\GridInterface;
13
use Zend\View\Helper\EscapeHtml;
14
use Zend\View\Renderer\PhpRenderer;
15
use Nnx\DataGrid\View\Helper\Exception;
16
use Nnx\DataGrid\NavigationBar\NavigationBarInterface;
17
use Nnx\DataGrid\Button\ButtonInterface;
18
19
/**
20
 * Class Grid
21
 * @package Nnx\DataGrid\View\Helper
22
 */
23
class Grid extends AbstractHelper
24
{
25
    /**
26
     * @param GridInterface $grid
27
     * @return string
28
     * @throws Exception\RuntimeException
29
     */
30
    public function __invoke(GridInterface $grid)
31
    {
32
        $columns = $grid->getColumns();
33
        if (count($columns) === 0) {
34
            throw new Exception\RuntimeException('В гриде нет колонок!');
35
        }
36
        /** @var PhpRenderer $view */
37
        $view = $this->getView();
38
        /** @var EscapeHtml $escape */
39
        $escape = $view->plugin('escapeHtml');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
40
        $bottomNavigationBar = $this->renderNavigationBar($grid->getBottomNavigationBar());
41
        $topNavigationBar = $this->renderNavigationBar($grid->getTopNavigationBar());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
42
        $res = $topNavigationBar['html'] . '<table id="grid-' . $escape($grid->getName()) . '"></table>' . $bottomNavigationBar['html'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
43
        $buttonsJs = $topNavigationBar['js'] . $bottomNavigationBar['js'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
44
        /** @var PhpRenderer $view */
45
        $view = $this->getView();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
        $config = $this->getGridConfig($grid);
47
        foreach ($columns as $column) {
48
            $columnClass = get_class($column);
49
            $columnPath = explode('\\', $columnClass);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
50
            $colName = array_pop($columnPath);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
            $helperName = 'nnxGridJqGrid' . $colName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
            /** @var string $columnsJqOptions */
53
            $config['colModel'][] = $view->$helperName($column);
54
        }
55
56
        $rowAttr = null;
57
        if ($mutators = $grid->getMutators()) {
58
            foreach ($mutators as $mutator) {
59
                if ($mutator instanceof HighlightMutatorInterface) {
60
                    $config['rowattr'] = '%rowAttrFunction%';
61
                    $rowAttr = 'function(rd) {' .
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
62
                        'if(rd.' . $mutator->getDataName() . ') {'
63
                        . 'return {"class": rd.' . $mutator->getDataName() . '};'
64
                        . '}'
65
                        . '}';
66
                }
67
            }
68
        }
69
70
        $gridName = $grid->getName();
71
        if ($grid instanceof PaginatorGridInterface) {
72
            $config['rowNum'] = $grid->getPaginator()->getItemCountPerPage();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
            $config['rowList'] = $grid->getPaginator()->getPossibleItemCountPerPage();
74
            $config['pager'] = 'pager-grid-' . $gridName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75
            $res .= "<div id='pager-grid-{$gridName}'>";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
76
        }
77
78
        $options = \Zend\Json\Json::encode(
79
            $config,
80
            false,
81
            ['enableJsonExprFinder' => true]
82
        );
83
84
        $view->headScript()->appendScript('$(function(){'
85
            . 'var grid = $("#grid-' . $grid->getName() . '").jqGrid('
86
            . str_replace('"%rowAttrFunction%"', $rowAttr, $options) . ');'
87
            . str_replace('%gridName%', $grid->getName(), $buttonsJs) . '});');
88
        return $res;
89
    }
90
91
    /**
92
     * @param NavigationBarInterface $navigationBar
0 ignored issues
show
Documentation introduced by
Should the type for parameter $navigationBar not be null|NavigationBarInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
93
     * @return array
94
     */
95
    protected function renderNavigationBar(NavigationBarInterface $navigationBar = null)
96
    {
97
        $html = '';
98
        $js = '';
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $js. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
99
        if ($navigationBar) {
100
            /** @var PhpRenderer $view */
101
            $view = $this->getView();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
            $navigationBarOptions = $navigationBar->getOptions();
103
            $urlVariables = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104
            if (isset($navigationBarOptions['urlVariables'])) {
105
                $urlVariables = $navigationBarOptions['urlVariables'];
106
            }
107
            /** @var ButtonInterface $button */
108
            foreach ($navigationBar->getButtons() as $button) {
109
                $buttonResult = $view->nnxGridJqGridButton($button, $urlVariables);
110
                $html .= $buttonResult['html'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
111
                $js .= $buttonResult['js'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
112
            }
113
            $html = "<div class='buttons-panel'>$html</div><br>";
114
        }
115
        return ['html' => $html, 'js' => $js];
116
    }
117
118
    /**
119
     * @param GridInterface $grid
120
     * @return array
121
     */
122
    protected function getGridConfig(GridInterface $grid)
123
    {
124
        $attributes = $grid->getAttributes();
125
        $config = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
126
            'shrinkToFit' => false,
127
            'width' => $this->getConfigVal('width', $attributes, '100%'),
128
            'datatype' => $this->getConfigVal('datatype', $attributes, 'local')
129
        ];
130
        if (!array_key_exists('width', $attributes) && !array_key_exists('autowidth', $attributes)) {
131
            $config['autowidth'] = true;
132
        }
133
134
        $config = array_merge($config, $attributes);
135
        return $config;
136
    }
137
138
    /**
139
     * @param string $key
140
     * @param array $options
141
     * @param mixed $default
142
     * @return null|string|array
143
     */
144
    protected function getConfigVal($key, array $options, $default = null)
145
    {
146
        return array_key_exists($key, $options) ? $options[$key] : $default;
147
    }
148
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
149