-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.php
More file actions
103 lines (79 loc) · 2.26 KB
/
Matrix.php
File metadata and controls
103 lines (79 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* An individual matrix item to be part of an MatrixArray for a Page
*
*/
class Matrix extends WireData {
/**
* We keep a copy of the $page that owns this matrix so that we can follow
* its outputFormatting state and change our output per that state
*
*/
protected $page;
/**
* Construct a new Matrix
*
*/
public function __construct() {
//define the fields that represent our matrix's items
$this->set('row', '');
$this->set('column', '');
$this->set('value', '');
$this->set('rowLabel', '');
$this->set('columnLabel', '');
}
/**
* Set a value to the matrix: row, column, value
*
*/
public function set($key, $value) {
if($key == 'page') {
$this->page = $value;
return $this;
}
//sanitize values as integers
elseif($key == 'row' || $key == 'column') $value = (int) $value;
//regular text sanitizer
elseif($key == 'value' || $key == 'rowLabel' || $key == 'columnLabel') $value = $this->sanitizer->text($value);
return parent::set($key, $value);//back to WireData
}
/**
* Retrieve a value from the matrix: row, column, value
*
*/
public function get($key) {
$value = parent::get($key);//retrieve from WireData method get()
//if the page's output formatting is on, then we'll return sanitized values
if($this->page && $this->page->of()) {
//for rows and columns show user friendly output (titles rather than IDs)
if($key == 'row' || $key == 'column') $value = (int) $value;
//regular text sanitizer for our values + row + column headers
elseif($key == 'value' || $key == 'rowLabel' || $key == 'columnLabel') $value = $this->sanitizer->text($value);
}
return $value;
}
/**
* Provide a default rendering for an matrix
*
*/
public function renderMatrix() {
$pages = wire('pages');
//remember page's output formatting state
$of = $this->page->of();
//turn on output formatting for our rendering (if it's not already on)
if(!$of) $this->page->of(true);
$out = "<p>" . $this->rowLabel . "<br>" .
$this->columnLabel . "<br>
<em>$this->value</em><br>
</p>";
if(!$of) $this->page->of(false);
return $out;
}
/**
* Return a string representing this matrix
*
*/
public function __toString() {
return $this->renderMatrix();
}
}