1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 */
8namespace Piwik\Plugins\TagManager\Template\Variable;
9
10use Piwik\Settings\FieldConfig;
11use Piwik\Validators\NotEmpty;
12
13class DomElementVariable extends BaseVariable
14{
15    public function getCategory()
16    {
17        return self::CATEGORY_PAGE_VARIABLES;
18    }
19
20    public function getParameters()
21    {
22        $selectionMethod = $this->makeSetting('selectionMethod', 'elementId', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
23            $field->title = 'Selection Method';
24            $field->description = 'Select the way you want to identify the element you want to read the value from.';
25            $field->uiControl = FieldConfig::UI_CONTROL_SINGLE_SELECT;
26            $field->validators[] = new NotEmpty();
27            $field->availableValues = array(
28                'cssSelector' => 'CSS Selector',
29                'elementId' => 'Element ID',
30            );
31        });
32        return array(
33            $selectionMethod,
34            $this->makeSetting('cssSelector', '', FieldConfig::TYPE_STRING, function (FieldConfig $field) use ($selectionMethod) {
35                $field->title = 'CSS Selector';
36                $field->description = 'A CSS selector allows you to select an element by id, className, element names, etc. If multiple elements match this selector, the first matching element will be used to get the value from. Examples for valid selectors are ".classname", "#id" or "li a".';
37                $field->condition = 'selectionMethod == "cssSelector"';
38                $field->validate = function ($value) use ($selectionMethod, $field) {
39                    if ($selectionMethod->getValue() === 'cssSelector' && empty($value)) {
40                        throw new \Exception('Please specify a value for ' . $field->title);
41                    }
42                };
43            }),
44            $this->makeSetting('elementId', '', FieldConfig::TYPE_STRING, function (FieldConfig $field) use ($selectionMethod) {
45                $field->title = 'Element ID';
46                $field->description = 'The id attribute specifies a unique id for an HTML element. Insert here the value of an ID attribute of any element within your website.';
47                $field->condition = 'selectionMethod == "elementId"';
48                $field->validate = function ($value) use ($selectionMethod, $field) {
49                    if ($selectionMethod->getValue() === 'elementId' && empty($value)) {
50                        throw new \Exception('Please specify a value for ' . $field->title);
51                    }
52                };
53            }),
54            $this->makeSetting('attributeName', '', FieldConfig::TYPE_STRING, function (FieldConfig $field) {
55                $field->title = 'Attribute Name';
56                $field->inlineHelp = 'If a value is entered, the value of the attribute will be returned instead of the text content of the element.';
57            }),
58
59        );
60    }
61
62}
63