Completed
Branch master (83a2f0)
by De Cramer
02:40
created

TitleLineFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 117
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui\Factory;
4
5
use eXpansion\Framework\Gui\Components\uiCheckbox;
6
use eXpansion\Framework\Gui\Components\uiInput;
7
use eXpansion\Framework\Gui\Components\uiLabel;
8
use eXpansion\Framework\Gui\Components\uiTooltip;
9
use eXpansion\Framework\Gui\Ui\Factory;
10
use FML\Controls\Control;
11
use FML\Controls\Frame;
12
use FML\Controls\Label;
13
use oliverde8\AssociativeArraySimplified\AssociativeArray;
14
15
/**
16
 * Class LineBuilder
17
 *
18
 * @package eXpansion\Framework\Core\Model\Gui\Builders;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class TitleLineFactory
22
{
23
    /** @var Factory */
24
    protected $uiFactory;
25
26
    /**
27
     * TitleLineFactory constructor.
28
     *
29
     * @param Factory $uiFactory
30
     */
31
    public function __construct(
32
        Factory $uiFactory
33
    ) {
34
        $this->uiFactory = $uiFactory;
35
    }
36
37
    /**
38
     * Create a multi column line.
39
     *
40
     * @param float $totalWidth
41
     * @param array $columns
42
     * @param int $index
43
     * @param float $height
44
     * @param bool $autoNewLine
45
     * @param int $maxLines
46
     *
47
     * @return Frame
48
     *
49
     * @throws \Exception
50
     */
51
    public function create($totalWidth, $columns, $index = 0, $height = 5.0)
52
    {
53
54
        $tooltip = $this->uiFactory->createTooltip();
55
56
        $totalCoef
57
            = ($totalWidth - 1) / array_reduce($columns, function ($carry, $item) {
58
                return $carry + $item['width'];
59
            });
60
61
        $frame = Frame::create();
62
        $postX = 1;
63
        foreach ($columns as $columnData) {
64
            $action = null;
65
            if (isset($columnData['action'])) {
66
                $action = $columnData['action'];
67
            }
68
69
            if (isset($columnData['title'])) {
70
                $element = $this->createTitleColumn($totalCoef, $columnData, $postX, $height, $action, $tooltip);
71
            }
72
73
            if (!isset($element)) {
74
                throw new \Exception('Element not found.');
75
            }
76
77
            $frame->addChild($element);
78
            $postX += $columnData["width"] * $totalCoef;
79
        }
80
81
        $line = $this->uiFactory->createLine(0, -$height);
82
        $line->setLength($totalWidth)->setStroke(0.33)->setColor("fff");
83
84
        $frame->addChild($line);
85
86
        return $frame;
87
    }
88
89
    /**
90
     * @param float $totalCoef
91
     * @param array $columnData
92
     * @param float $postX
93
     * @param float $height
94
     * @param string $action
95
     * @param uiTooltip $tooltip
96
     * @return \eXpansion\Framework\Gui\Layouts\layoutLine
97
     */
98
    protected function createTitleColumn($totalCoef, $columnData, $postX, $height, $action, uiTooltip $tooltip)
99
    {
100
        $sort = AssociativeArray::getFromKey($columnData, 'sort', "");
101
        switch ($sort) {
102
            case "ASC":
103
                $sortType = " ";
104
                break;
105
            case "DESC":
106
                $sortType = " ";
107
                break;
108
            default:
109
                $sortType = "";
110
                break;
111
        }
112
113
        $sortLabel = $this->uiFactory->createLabel($sortType, uiLabel::TYPE_HEADER);
114
        $sortLabel->setSize(3, $height - 1);
115
116
        $label = $this->uiFactory->createLabel($columnData['title'], uiLabel::TYPE_HEADER);
117
118
        $translate = AssociativeArray::getFromKey($columnData, 'translatable', false);
119
        if ($translate) {
120
            $label->setTextId($columnData['title']);
121
            $label->setTranslate(true);
122
        }
123
124
        $label->setHeight($height - 1)
125
            ->setWidth(($columnData["width"] * $totalCoef) - 3.5)
126
            ->setPosition($postX, -0.5)
127
            ->setAutoNewLine(false)
128
            ->setMaxLines(1);
129
130
        if (!empty($action)) {
131
            $label->setAction($action);
132
            $tooltip->addTooltip($label, "click to sort");
133
        }
134
135
        return $this->uiFactory->createLayoutLine($postX, -0.5, [$label, $sortLabel]);
136
    }
137
}
138