-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategory_tree.php
164 lines (146 loc) · 3.6 KB
/
category_tree.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
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
<?php
function getCategoryTree()
{
//atrod visus xml failus
$directory="faili/gatavie";
$files=array();
$extensions=array("xml");
$new_dirs=array($directory);
//izpilda ciklu kamēr ir atrastas jaunas direktorijas
while(!empty($new_dirs))
{
$dirs=$new_dirs;
$new_dirs=array();
//iet cauri visām atrastajām direktorijām
foreach($dirs as $dir)
{
$file = scandir($dir);
//iet cauri visiem failiem direktorijā
foreach($file as $this_file) {
if( is_dir("$dir/$this_file" ) ) //ja atrasta direktorija, peivieno to direktoriju sarakstam
{
if( $this_file != "." && $this_file != ".." ) //izvairāmies no ieciklošanās .. un . gadījumā
{
$new_dirs[] = "$dir/$this_file";
}
}
else //ja atrada failu
{
$ext = substr($this_file, strrpos($this_file, ".") + 1);
if(in_array($ext, $extensions) ) //ja faili ar šādu paplašinājumu ir jāapstrādā, pievieno to failu sarakstam
{
$files[] = "$dir/$this_file";
}
}
}
}
}
$cats=Array();
$sentence_cats=Array();
foreach($files as $file)
{
if(!file_exists($file))
{
//echo "Nevar atvērt ".$file."<br />";
continue;
}
$saturs=file_get_contents($file);
$DOMdoc = new DOMDocument();
if(!@$DOMdoc->loadXML($saturs))
{
//echo "Nevar atvērt ".$file."<br />";
continue;
}
$DOMPath = new DOMXPath( $DOMdoc );
$categories=$DOMPath->query("//CATEGORY");
foreach($categories AS $category)
{
$key=mb_strtolower($category->nodeValue, 'UTF-8');
if(!isset($cats[$key]))
{
$cats[$key]=array();
$cats[$key]['id']=$category->getAttribute("id");
$cats[$key]['name']=$category->nodeValue;
$cats[$key]['files']=array();
}
$cats[$key]['files'][]=$file;
}
$sentence_categories=$DOMPath->query("//SENTENCE_CATEGORY");
foreach($sentence_categories AS $sentence_category)
{
$key=mb_strtolower($sentence_category->nodeValue, 'UTF-8');
if(!isset($sentence_cats[$key]))
{
$sentence_cats[$key]=array();
$sentence_cats[$key]['id']=$sentence_category->getAttribute("id");
$sentence_cats[$key]['name']=$sentence_category->nodeValue;
$sentence_cats[$key]['files']=array();
}
$sentence_cats[$key]['files'][]=$file;
}
}
/*
echo "<pre>".print_r($cats,true)."</pre>";
echo "<pre>".print_r($sentence_cats,true)."</pre>";
*/
ksort($cats);
ksort($sentence_cats);
$resp='<ul class="php-file-tree">';
$resp.='
<li class="pft-directory">
<a href="#">Vārdu kategorijas</a>
<ul style="display:none">
';
foreach($cats AS $cat)
{
$resp.='
<li class="pft-directory">
<a href="#">'.$cat['name'].'</a>
<ul style="display:none">
';
sort($cat['files']);
foreach($cat['files'] AS $file)
{
$resp.='
<li class="pft-file ext-xml">
<a class="file" rel="'.urlencode($file).'" href="#">'.$file.'</a>
</li>
';
}
$resp.='
</ul>
</li>
';
}
$resp.='</ul></li>';
$resp.='
<li class="pft-directory">
<a href="#">Teikumu kategorijas</a>
<ul style="display:none">
';
foreach($sentence_cats AS $cat)
{
$resp.='
<li class="pft-directory">
<a href="#">'.$cat['name'].'</a>
<ul style="display:none">
';
sort($cat['files']);
foreach($cat['files'] AS $file)
{
$resp.='
<li class="pft-file ext-xml">
<a class="file" rel="'.$file.'" href="#">'.$file.'</a>
</li>
';
}
$resp.='
</ul>
</li>
';
}
$resp.='</ul></li>';
$resp.='</ul>';
return $resp;
}
?>