-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixArray.php
More file actions
154 lines (112 loc) · 3.63 KB
/
MatrixArray.php
File metadata and controls
154 lines (112 loc) · 3.63 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Contains multiple Matrix objects for a single page
*
*/
class MatrixArray extends WireArray {
protected $page;
protected $fn;
public function __construct(Page $page) {
$this->page = $page;
}
public function isValidItem($item) {
return $item instanceof Matrix;//item returned as instance of Matrix
}
public function add($item) {
$item->page = $this->page;
return parent::add($item);//back to WireArray
}
public function __toString() {
$out = '';
foreach($this as $item) $out .= $item;
return $out;
}
/**
* Determine data type of passed row/column.
*
* Use by in-memory selector in getRow()/getColumn().
*
* @access private
* @param Page|string|int|path $sv The row/column whose values to return.
* @param String $type Whether dealing with 'row' or 'column'.
* @return String $rowSel Selector string to search for.
*
*/
private function getValidgetRC($sv, $type) {
$rowSel = '';
$this->fn = FieldtypeMatrix::$name;//name of the field to query
//if we got an integer, we assume it is the ID
if(is_int($sv)) $rowSel = $type . "=" . $sv;
//if we got a path
elseif (preg_match('#^(\/)#', $sv)) {
$p = wire('pages')->get($this->sanitizer->pagePathName($sv));
if($p && $p->id > 0) $rowSel = $type . "=" . (int) $p->id;
}
//if we got a string, we assume page title
elseif(is_string($sv)) $rowSel = $type . "Label=" . $this->sanitizer->text($sv);//row||columnLabel
//if we got a Page object
elseif ($sv instanceof Page) $rowSel = $type . "Label=" . $this->sanitizer->text($sv->title);//row||columnLabel
return $rowSel;
}
/**
* Return all|limited values of the specified Row.
*
* @access public
* @param Page|string|int|path $sv The row whose values to return.
* @param int $limit Limit number of values to return.
* @param string $sort How to sort the results on fetch.
* @return Object $values Retrieved results.
*
*/
public function getRow($sv, $limit = '', $sort ='') {
//@@todo: update to work with $pages->get?
$rowSel = $this->getValidgetRC($sv, 'row');
$n = $this->fn;//name of the field to search through
//limit
if($limit) $limit = (int) $limit;
//sort
if($sort == 'asc') $sort = 'value';
elseif($sort == 'desc') $sort = '-value';
$values = wire('page')->$n->find("$rowSel, limit=$limit, sort=$sort");
return $values;
}
/**
* Return all|limited values of the specified Column.
*
* @access public
* @param Page|string|int|path $sv The row whose values to return.
* @param int $limit Limit number of values to return.
* @param string $sort How to sort the results on fetch.
* @return Object $values Retrieved results.
*
*/
public function getColumn($sv, $limit = '', $sort ='') {
//@@todo: update to work with $pages->get?
$colSel = $this->getValidgetRC($sv, 'column');
$n = $this->fn;//the name of the field to query
//limit
if($limit) $limit = (int) $limit;
//sort
if($sort == 'asc') $sort = 'value';
elseif($sort == 'desc') $sort = '-value';
$values = wire('page')->$n->find("$colSel, limit=$limit, sort=$sort");
return $values;
}
/**
* Return a single value at the given coordinates (row, column).
*
* @access public
* @param Page|string|int|path $r The row whose value to return.
* @param Page|string|int|path $c The column whose value to return.
* @return Object|Bool False $value Retrieved result.
*
*/
public function getValue($r, $c) {
//@@todo: update to work with $pages->get?
$rowSel = $this->getValidgetRC($r, 'row');
$colSel = $this->getValidgetRC($c, 'column');
$n = $this->fn;//name of the field to search through
$value = wire('page')->$n->get("$rowSel, $colSel");
return $value;
}
}