-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNodeCore.php
More file actions
167 lines (151 loc) · 4.6 KB
/
TreeNodeCore.php
File metadata and controls
167 lines (151 loc) · 4.6 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* TreeNodeCore - The core class for a single node in a Survloop tree.
*
* Survloop - All Our Data Are Belong
* @package rockhopsoft/survloop
* @author Morgan Lesko <rockhoppers@runbox.com>
* @since v0.0.18
*/
namespace RockHopSoft\Survloop\Controllers\Tree;
use DB;
use App\Models\SLNode;
class TreeNodeCore
{
public $nodeID = 1;
public $parentID = 1;
public $parentOrd = 0;
public $nodeOpts = 1;
public $nodeType = '';
public $dataBranch = '';
public $dataStore = '';
public $responseSet = '';
public $defaultVal = '';
public $nodeRow = NULL;
public $nodeRowFilled = false;
public $nodeTierPath = [];
function __construct($nID = -3, $nRow = [], $nCache = [])
{
$this->nodeID = $nID;
if (sizeof($nCache) > 0) {
return $this->loadNodeCache($nID, $nCache);
}
$this->loadNodeRow($nID, $nRow);
return true;
}
public function loadNodeCache($nID = -3, $nCache = [])
{
if (sizeof($nCache) > 0) {
if (isset($nCache["pID"])) {
$this->parentID = $nCache["pID"];
}
if (isset($nCache["pOrd"])) {
$this->parentOrd = $nCache["pOrd"];
}
if (isset($nCache["opts"])) {
$this->nodeOpts = $nCache["opts"];
}
if (isset($nCache["type"])) {
$this->nodeType = $nCache["type"];
}
}
return true;
}
public function loadNodeRow($nID = -3, $nRow = NULL)
{
$this->nodeRow = null;
if ($nRow) {
$this->nodeRow = $nRow;
} elseif ($nID > 0) {
$this->nodeRow = SLNode::find($nID)
->select('node_id', 'node_parent_id', 'node_parent_order', 'node_opts', 'node_type');
} elseif ($this->nodeID > 0) {
$this->nodeRow = SLNode::find($this->nodeID)
->select('node_id', 'node_parent_id', 'node_parent_order', 'node_opts', 'node_type');
}
if (!$this->nodeRow) {
$this->nodeRow = new SLNode;
return false;
}
$this->chkNodeInvalidFields();
$this->parentID = $this->nodeRow->node_parent_id;
$this->parentOrd = $this->nodeRow->node_parent_order;
$this->nodeOpts = $this->nodeRow->node_opts;
$this->nodeType = $this->nodeRow->node_type;
//$this->fillNodeRow();
return true;
}
public function chkNodeInvalidFields()
{
if ($this->nodeRow && isset($this->nodeRow->node_id)) {
if (!isset($this->nodeRow->node_parent_order)
|| intVal($this->nodeRow->node_parent_order) < 0) {
$this->nodeRow->node_parent_order = 0;
$this->nodeRow->save();
}
if (!isset($this->nodeRow->node_opts)
|| intVal($this->nodeRow->node_opts) <= 0) {
$this->nodeRow->node_opts = 1;
$this->nodeRow->save();
}
}
return true;
}
public function listCore()
{
return [
"parentID" => $this->parentID,
"parentOrd" => $this->parentOrd,
"nodeOpts" => $this->nodeOpts,
"nodeType" => $this->nodeType
];
}
public function fillNodeRow($nID = -3, $nRow = NULL)
{
if ($nID <= 0 && $this->nodeID > 0) {
$nID = $this->nodeID;
}
if (!$this->nodeRowFilled) {
if ($nRow) {
$this->nodeRow = $nRow;
} else {
$this->nodeRow = SLNode::find($nID);
}
$this->chkNodeInvalidFields();
$this->initiateNodeRow();
$this->nodeRowFilled = true;
}
return true;
}
public function initiateNodeRow()
{
return true;
}
/**
* Returns the Node ID, in the simplest case.
* @return array Results
*/
public function nodePreview()
{
return number_format($this->nodeID);
}
public function tierPathStr($tierPath = [])
{
if (sizeof($tierPath) == 0) {
return implode('-', $this->nodeTierPath).'-';
}
return implode('-', $tierPath).'-';
}
public function checkBranch($tierPath = [])
{
$tierPathStr = $this->tierPathStr($tierPath);
if ($tierPathStr != '') {
return (strpos($this->tierPathStr($this->nodeTierPath), $tierPathStr) === 0);
}
return 0;
}
public function getParent()
{
return intVal($this->parentID);
}
}