Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * An attribute changes how a field is designated or behaves.
19
 *
20
 * @package    block_dash
21
 * @copyright  2019 bdecent gmbh <https://bdecent.de>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_dash\local\data_grid\field\attribute;
26
 
27
use block_dash\local\dash_framework\structure\field_interface;
28
use stdClass;
29
 
30
/**
31
 * An attribute changes how field output is formatted.
32
 *
33
 * @package block_dash
34
 */
35
abstract class abstract_field_attribute implements field_attribute_interface {
36
 
37
    /**
38
     * @var array
39
     */
40
    private $options = [];
41
 
42
    /**
43
     * Field.
44
     *
45
     * @var field_interface
46
     */
47
    private $field;
48
 
49
    /**
50
     * List of placeholder fields.
51
     *
52
     * @var [type]
53
     */
54
    protected $placeholders;
55
 
56
    /**
57
     * New attribute.
58
     *
59
     * @param array $options
60
     */
61
    public function __construct(array $options = []) {
62
        $this->set_options($options);
63
    }
64
 
65
    /**
66
     * Set the field this attribute is attached to.
67
     *
68
     * @param field_interface $field
69
     */
70
    public function set_field(field_interface $field) {
71
        $this->field = $field;
72
    }
73
 
74
    /**
75
     * Get the field this attribute is attached to.
76
     *
77
     * @return field_interface
78
     */
79
    public function get_field() {
80
        return $this->field;
81
    }
82
 
83
    /**
84
     * After records are relieved from database each field has a chance to transform the data.
85
     * Example: Convert unix timestamp into a human readable date format
86
     *
87
     * @param mixed $data Raw data associated with this field.
88
     * @param \stdClass $record Full record from database.
89
     * @return mixed
90
     */
91
    public function transform_data($data, \stdClass $record) {
92
        return $data;
93
    }
94
 
95
    // Region Options.
96
 
97
    /**
98
     * Get a single option.
99
     *
100
     * @param string $name
101
     * @return mixed|null
102
     */
103
    public function get_option($name) {
104
        return isset($this->options[$name]) ? $this->options[$name] : null;
105
    }
106
 
107
    /**
108
     * Set option on field.
109
     *
110
     * @param string $name
111
     * @param string $value
112
     */
113
    public function set_option($name, $value) {
114
        $this->options[$name] = $value;
115
    }
116
 
117
    /**
118
     * Set options on field.
119
     *
120
     * @param array $options
121
     */
122
    public function set_options($options) {
123
        foreach ($options as $name => $value) {
124
            $this->set_option($name, $value);
125
        }
126
    }
127
 
128
    /**
129
     * Get all options for this field.
130
     *
131
     * @return array
132
     */
133
    public function get_options() {
134
        return $this->options;
135
    }
136
 
137
    /**
138
     * Add option.
139
     *
140
     * @param string $name
141
     * @param string $value
142
     */
143
    public function add_option($name, $value) {
144
        $this->options[$name] = $value;
145
    }
146
 
147
    /**
148
     * Is the attribute needs to receive the data.
149
     *
150
     * @return bool
151
     */
152
    public function is_needs_construct_data() {
153
        return false;
154
    }
155
 
156
    /**
157
     * Set the custom data options to transform the data.
158
     *
159
     * @param mixed $field
160
     * @return void
161
     */
162
    public function set_transform_field($field) {
163
    }
164
 
165
    /**
166
     * Set the placeholders, this will help to use the values of table field in developer addon.
167
     *
168
     * @param array $placeholders
169
     * @return void
170
     */
171
    public function set_placeholders(array $placeholders) {
172
        $this->placeholders = $placeholders;
173
    }
174
 
175
    /**
176
     * Update the placeholder fields values with data.
177
     *
178
     * @param \stdClass $data
179
     * @param string $valuestr
180
     * @return void
181
     */
182
    public function update_placeholders(\stdClass $data, string $valuestr) {
183
 
184
        $amethods = $this->placeholders;
185
 
186
        foreach ($amethods as $fieldname) {
187
            $replacement = "{" . $fieldname . "}";
188
            // Message text placeholder update.
189
            if (stripos($valuestr, $replacement) !== false) {
190
                $alias = str_replace('.', '_', $fieldname);
191
                $val = $data->$alias ?? '';
192
                // Placeholder found on the text, then replace with data.
193
                $valuestr = str_ireplace($replacement, $val, $valuestr);
194
            }
195
        }
196
        return $valuestr ?? '';
197
    }
198
    // Endregion.
199
}