-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathORM_Mptt.php
136 lines (109 loc) · 3.32 KB
/
ORM_Mptt.php
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
<?php
class ORM_Mptt_Core{
protected $model;
protected $class;
protected $table;
protected $left_column = 'lft';
protected $right_column = 'rgt';
protected $parent_column = 'parent_id';
protected $scope_column = 'scope';
public function __construct(ORM $model)
{
$this->model=$model;
$this->class=get_class($model);
isset(Kohana::$instance->db) or Kohana::$instance->db = Database::instance();
$this->db=Registry::get('db');
$this->table= inflector::plural(strtolower(substr($this->class, 0, -6)));
}
/**
* Locks tree table
* This is a straight write lock - the database blocks until the previous lock is released
*/
public function lock_tree($aliases = array())
{
$sql = "LOCK TABLE " . $this->table . " WRITE";
return $this->db->query($sql);
}
/**
* Unlocks tree table
* Releases previous lock
*/
public function unlock_tree()
{
$sql = "UNLOCK TABLES";
return $this->db->query($sql);
}
/**
* Get the path leading up to the current node
* @return array with ORM objects
*
*/
public function get_path()
{
$lft_col = $this->left_column;
$rgt_col = $this->right_column;
//$this->model->where($this->scope_column,$this->get_scope());
$this->model->where($lft_col . ' <= '.$this->model->$lft_col . ' AND ' . $rgt_col . ' >=' .$this->model->$rgt_col . ' ORDER BY '.$lft_col);
return $this->model->find_all();
}
/**
* Returns the root node
* @return array $resultNode The node returned
*/
function get_root()
{
$this->model->where('`'.$this->left_column . '` = 1 ');
return $this->model->find(FALSE,true);
}
/**
* Same as insertNewChild except the new node is added as the last child
* @param array $parentNode The node array of the parent to use
*
* @return
*/
function insert_as_last_child_of($parent_node)
{
$lft_col = $this->left_column;
$rgt_col = $this->right_column;
$scp_col=$this->scope_column;
$parent_column=$this->parent_column;
//Set parent id (id of the parent, is childs parent id)
//$this->$parent_column=$parent_node->id;
$this->model->$lft_col=$parent_node->$rgt_col;
$this->model->$rgt_col=$parent_node->$rgt_col+1;
// $this->$scp_col=$this->get_scope();
$this->lock_tree();
$this->modify_node($this->model->$lft_col,2);
//$this->save_node();
$this->model->save();
$this->unlock_tree();
return $this;
}
/**
* Test to see if node has children
*
* @return boolean
*/
function has_descendants()
{
return (($this->model->{$this->right_column} - $this->model->{$this->left_column}) > 1);
}
protected function modify_node($node_int, $changeVal)
{
$leftcol = $this->left_column;
$rightcol = $this->right_column;
// $table = $this->table;
$scope_col = $this->scope_column;
$sql = "UPDATE ".$this->table." " .
"SET $leftcol = $leftcol + $changeVal ".
"WHERE $leftcol >= $node_int";
// AND ".$this->scope_column.' = '.$this->$scope_col.';';
$this->db->query($sql);
$sql = "UPDATE ".$this->table." " .
"SET $rightcol = $rightcol + $changeVal ".
"WHERE $rightcol >= $node_int";
// AND ".$this->scope_column.' = '.$this->$scope_col.';';
$this->db->query($sql);
return true;
}
}