Passed
Push — master ( 199fe7...b856e3 )
by Dmitrijs
01:55
created

Card::renderActionItem()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 23
rs 9.9
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
/**
3
 * @link https://github.com/DMGPage/yii2-materialize
4
 * @copyright Copyright (c) 2018 Dmitrijs Reinmanis
5
 * @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE
6
 */
7
8
namespace dmgpage\yii2materialize\widgets;
9
10
use yii\base\Widget as BaseWidget;
11
use dmgpage\yii2materialize\helpers\Html;
12
use yii\helpers\ArrayHelper;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Cards are a convenient means of displaying content composed of different types of objects.
17
 * They’re also well-suited for presenting similar objects whose size or supported actions can vary considerably,
18
 * like photos with captions of variable length.
19
 *
20
 */
21
class Card extends BaseWidget
22
{
23
    /**
24
     * @var array the HTML attributes for the row container tag of the card view.
25
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
26
     */
27
    public $rowOptions = [];
28
29
    /**
30
     * @var array the HTML attributes for the column container tag of the card view.
31
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
32
     */
33
    public $colOptions = ['class' => 's12 m6'];
34
35
    /**
36
     * @var array the HTML attributes for the card container tag of the card view.
37
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
38
     */
39
    public $options = [];
40
41
    /**
42
     * @var array the HTML attributes for the card content wrapper tag of the card view.
43
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
44
     */
45
    public $contentOptions = [];
46
47
    /**
48
     * @var string title of the card
49
     */
50
    public $title;
51
52
    /**
53
     * @var array the HTML attributes for the card title tag of the card view. Uses only if "cardTitle" attribute is specified.
54
     * - encode: boolean, optional, whether this item`s label should be HTML-encoded.
55
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
56
     */
57
    public $titleOptions = [];
58
59
    /**
60
     * @var array list of card action items. Each action item should be an array of the following structure:
61
     * - label: string, specifies the action item label. When [[encodeLabels]] is true, the label
62
     *   will be HTML-encoded.
63
     * - encode: boolean, optional, whether this item`s label should be HTML-encoded. This param will override
64
     *   global [[encodeLabels]] param.
65
     * - url: string or array, optional, specifies the URL of the action item. It will be processed by [[Url::to]].
66
     * - options: array, optional, the HTML attributes for the action container tag.
67
     */
68
    public $actions = [];
69
70
    /**
71
     * @var array the HTML attributes for the card action tag of the card view. Uses only if "actions" attribute is specified.
72
     * @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
73
     */
74
    public $actionOptions = [];
75
76
    /**
77
     * @var bool whether to HTML-encode the link labels.
78
     */
79
    public $encodeLabels = true;
80
81
    /**
82
     * Initializes the widget.
83
     */
84
    public function init()
85
    {
86
        parent::init();
87
88
        Html::addCssClass($this->options, ['class' => 'card']);
89
        Html::addCssClass($this->contentOptions, ['class' => 'card-content']);
90
91
        $html = Html::beginGridRow($this->rowOptions);
92
        $html .= Html::beginGridCol($this->colOptions);
93
        $html .= Html::beginTag('div', $this->options);
94
        $html .= Html::beginTag('div', $this->contentOptions);
95
96
        if (!empty($this->title)) {
97
            $encode = isset($this->titleOptions['encode']) ? $this->titleOptions['encode'] : $this->encodeLabels;
98
            unset($this->titleOptions['encode']);
99
            Html::addCssClass($this->titleOptions, ['class' => 'card-title']);
100
            $title = $encode ? Html::encode($this->title) : $this->title;
101
            $html .= Html::tag('span', $title, $this->titleOptions);
102
        }
103
104
        echo $html;
105
    }
106
107
    /**
108
     * Renders the widget.
109
     */
110
    public function run()
111
    {
112
        $html = Html::endTag('div'); // ends container tag
113
114
        if (!empty($this->actions)) {
115
            Html::addCssClass($this->actionOptions, ['class' => 'card-action']);
116
            $html .= Html::beginTag('div', $this->actionOptions);
117
118
            foreach ($this->actions as $action) {
119
                $html .= $this->renderActionItem($action);
120
            }
121
122
            $html .= Html::endTag('div');
123
        }
124
125
        $html .= Html::endTag('div');
126
        $html .= Html::endGridCol();
127
        $html .= Html::endGridRow();
128
129
        echo $html;
130
    }
131
132
    /**
133
     * Renders a single card action item.
134
     *
135
     * @param array $link the link to be rendered. It must contain the "label" element. The "url" and "icon" element is optional.
136
     * @return string the rendering result
137
     * @throws InvalidConfigException if `$link` does not have "label" element.
138
     */
139
    protected function renderActionItem($link)
140
    {
141
        $encodeLabel = ArrayHelper::remove($link, 'encode', $this->encodeLabels);
142
143
        if (array_key_exists('label', $link)) {
144
            $label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
145
        } else {
146
            throw new InvalidConfigException('The "label" element is required for each link.');
147
        }
148
149
        // Add icon to label text
150
        // https://github.com/google/material-design-icons/issues/206
151
//        if (isset($link['icon'])) {
152
//            $label = $this->renderIcon($link['icon']) . $label;
153
//        }
154
155
        $options = $link;
156
        unset($options['label'], $options['url'], $options['icon']);
157
158
        if (isset($link['url'])) {
159
            return Html::a($label, $link['url'], $options);
160
        } else {
161
            return Html::a($label, '#', $options);
162
        }
163
    }
164
}
165