Rights::content()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: onysko
5
 * Date: 10.06.2015
6
 * Time: 16:43
7
 */
8
namespace samsoncms\app\security\tab;
9
10
use samson\cms\Navigation;
11
use samsoncms\form\tab\Generic;
12
use samsonframework\core\RenderInterface;
13
use samsonframework\orm\QueryInterface;
14
use samsonframework\orm\Record;
15
16
/**
17
 * SamsonCMS application form tab for group security rights selection
18
 * @package samsoncms\app\security\tab
19
 */
20
class Rights extends Generic
21
{
22
    /** @var string Tab name or identifier */
23
    protected $name = 'Доступные права';
24
25
    /** @var \samsoncms\app\security\Controller */
26
    protected $renderer;
27
28
    /**
29
     * Render checkboxes selection list
30
     * @param array $availableValues Collection of available entities for selection
31
     * @param array $selectedValueIDs Collection of selected entity identifiers
32
     * @param string $controller Select/Un-select controller action route
33
     * @param string $showField Entity field name for showing
34
     * @return string HTML rendered checkboxes list
35
     */
36
    public function renderList(array $availableValues, array $selectedValueIDs, $controller, $showField = 'Description')
37
    {
38
        // Iterate all available values
39
        $html = '';
40
        foreach ($availableValues as $availableValue) {
41
            // Define if this value is selected
42
            $checked = in_array($availableValue->id, $selectedValueIDs) ? 'checked' : '';
43
44
            // Translate all fields
45
            $finishTranslateParts = $this->translateCustomFields($availableValue->$showField);
46
47
            $html .= '<div class="input-container select-checkboxes-list-item">';
48
            // Render checkbox with label
49
            $html .= '<label><input type="checkbox" ' . $checked . ' href="' . url_build($controller, $availableValue->id) . '" value="' . $availableValue->id . '">' . $finishTranslateParts . '</label>';
50
            $html .= '</div>';
51
        }
52
53
        return $html;
54
    }
55
56
    /**
57
     * Function which translated custom fields for application "Rights"
58
     * @param array $inputData
59
     * @return string
60
     */
61
    public function translateCustomFields($inputData)
62
    {
63
        // Search all part this text block
64
        $allTranslateParts = explode("\"", $inputData);
65
        // Remove empty elements
66
        $allTranslateParts = array_filter($allTranslateParts);
67
68
        // First value empty
69
        $finishTranslateParts = '';
70
        // Counter elements in this array
71
        $count = 0;
72
        foreach ($allTranslateParts as $oneTranslateParts) {
73
            // First part (not have quotes)
74
            if ($count == 0) {
75
                $finishTranslateParts .= t($oneTranslateParts, true);
0 ignored issues
show
Deprecated Code introduced by
The function t() has been deprecated with message: Should be used as $this->system->getContainer()->geti18n()->translate()|plural()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
76
                // Last part with quotes
77
            } else {
78
                $finishTranslateParts .= ' "' . t($oneTranslateParts, true) . '"';
0 ignored issues
show
Deprecated Code introduced by
The function t() has been deprecated with message: Should be used as $this->system->getContainer()->geti18n()->translate()|plural()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
79
            }
80
            // Increment count
81
            $count++;
82
        }
83
84
        return $finishTranslateParts;
85
    }
86
87
    /** @inheritdoc */
88
    public function content()
89
    {
90
        // Translate header
91
        $this->name = t($this->name, true);
0 ignored issues
show
Deprecated Code introduced by
The function t() has been deprecated with message: Should be used as $this->system->getContainer()->geti18n()->translate()|plural()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
92
93
        // Access to the application
94
95
        // Render tab content
96
        $content = $this->renderer
97
            ->view('form/tab_item')
98
            ->set($this->renderList(
99
                $this->query->className('right')->exec(),
100
                $this->query->className('groupright')->cond('GroupID', $this->entity->id)->fields('RightID'),
101
                $this->renderer->id() . '/change/' . $this->entity->id
102
            ), 'chbView')
103
            ->output();
104
105
        return $this->renderer
106
            ->view($this->contentView)
107
            ->set($content, 'content')
108
            ->output();
109
    }
110
}
111