summaryrefslogtreecommitdiff
path: root/tpl/notes/inc_relativestree.php
blob: be0e224ac88978a7afc267d9ae5566d719e0ed01 (plain) (blame)
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
<?php

/* OLD WAY

// ******* FETCH DATA

if ($note['parent_id'] != 0) {
	$brothers = array();
	$n = sql("SELECT id, title, owner FROM notes WHERE parent = " . $note['parent_id'] . " ".
		($note['owner'] == $user['id'] ? "" : " AND public != 0 ").
		"ORDER BY title ASC");
	while ($nn = mysql_fetch_assoc($n)) $brothers[] = $nn;
}
$children = array();
$n = sql("SELECT id, title, owner FROM notes WHERE parent = " . $note['id'] . " " .
	($note['owner'] == $user['id'] ? "" : " AND public != 0 ") .
	"ORDER BY title ASC");
while ($nn = mysql_fetch_assoc($n)) $children[] = $nn;

$user_root_notes = array();
$n = sql("SELECT id, title, owner FROM notes WHERE parent = 0 AND owner = " . $note['owner'] . " " .
	($note['owner'] == $user['id'] ? "" : " AND public != 0 ") .
	"ORDER BY title ASC");
while ($nn = mysql_fetch_assoc($n)) $user_root_notes[] = $nn;

// ****** DISPLAY TREE

echo '</div><div class="contents-left">';
echo '<h1>' . $note['ownername'] . '</h1><br />';
echo '<div class="small_right"><a href="user-notes-' . $note['owner'] . '">complete tree</a></div>';

function list_brothers_and_children() {
	global $brothers, $children, $note, $user, $can_new;
	echo '<div class="tree_branch">';
	foreach($brothers as $b) {
		if ($b['id'] == $note['id']) {
			echo '<p>' . $b['title'] . '</p><div class="tree_branch">';
			foreach($children as $nn) {
				echo '<p><a href="view-notes-' . $nn['id'] . '">' . $nn['title'] . '</a></p>';
			}
			if ($can_new) {
				echo '<p><a class="tool_link" href="new-notes-' . $b['id'] . '">+ new note</a></p>';
			}
			echo '</div>';
		} else {
			echo '<p><a href="view-notes-' . $b['id'] .'">' . $b['title'] . '</a></p>';
		}
	}
	if ($can_new) {
		echo '<p><a class="tool_link" href="new-notes-' . $note['parent_id'] . '">+ new note</a></p>';
	}
	echo '</div>';
}

echo '<div class="tree_branch">';
$did_show_up = false;
foreach($user_root_notes as $n) {
	if ($n['id'] == $note['id']) {
		$did_show_up = true;
		echo '<p>' . $n['title'] . '</p><div class="tree_branch">';
		foreach($children as $nn) {
			echo '<p><a href="view-notes-' . $nn['id'] . '">' . $nn['title'] . '</a></p>';
		}
		if ($can_new) {
			echo '<p><a class="tool_link" href="new-notes-' . $note['id'] . '">+ new note</a></p>';
		}
		echo '</div>';
	} else {
		echo '<p><a href="view-notes-' . $n['id'] . '">' . $n['title'] . '</a></p>';
		if ($n['id'] == $note['parent_id']) {
			$did_show_up = true;
			list_brothers_and_children();
		}
	}
}
if ($can_new) {
	echo '<p><a class="tool_link" href="new-notes-0">+ new note</a></p>';
}
echo '</div>';

if (!$did_show_up) {
	echo '<br /><br />';
	echo '<a href="view-notes-' . $note['parent_id'] . '">' . $note['parent_title'] . '</a>';
	list_brothers_and_children();
}

*/

// *** NEW WAY

$notes_tree = array();
$notes_parents = array();
$n = sql("SELECT id, parent, title FROM notes ".
	"WHERE owner = " . $note['owner'] .
	($note['owner'] == $user['id'] ? " " : " AND public != 0 ") .
	"ORDER BY title ASC");
while ($nn = mysql_fetch_assoc($n)) {
	$notes_parents[$nn['id']] = $nn['parent'];
	if (isset($notes_tree[$nn['parent']])) {
		$notes_tree[$nn['parent']][] = $nn;
	} else {
		$notes_tree[$nn['parent']] = array($nn);
	}
}

$notest = array(0 => @$notes_tree[0]);
for($id = $note['id']; $id != 0; $id = $notes_parents[$id]) {
	$notest[$id] = @$notes_tree[$id];
}

echo '</div><div class="contents-left">';
echo '<h1>' . $note['ownername'] . '</h1><br />';
echo '<div class="small_right"><a href="user-notes-' . $note['owner'] . '">complete tree</a></div>';

function n_tree_branch($id) {
	global $notest, $note, $can_new;
	if (!isset($notest[$id])) return;
	foreach($notest[$id] as $branch) {
		if ($branch['id'] == $note['id'])
			echo '<p>' . $branch['title'] . '</p>';
		else
			echo '<p><a href="view-notes-' . $branch['id'] . '">' . $branch['title'] . '</a></p>';
		if (isset($notest[$branch['id']])) {
			echo '<div class="tree_branch">';
			n_tree_branch($branch['id']);
			echo '</div>';
		} else if ($can_new && $branch['id'] == $note['id']) {
			echo '<div class="tree_branch">';
			if ($can_new) echo '<p><a class="tool_link" href="new-notes-' . $branch['id'] . '">+ new note</a></p>';
			echo '</div>';
		}
	}
	if ($can_new) echo '<p><a class="tool_link" href="new-notes-' . $id . '">+ new note</a></p>';
}

echo '<div class="tree_root">';
n_tree_branch(0);
echo '</div>';