This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTFilePicker.php
89 lines (76 loc) · 2.6 KB
/
TFilePicker.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
<?php class TFilePicker extends CInputWidget{
private $defaultFileView='_fileView';
public $directory;
public $itemView;
private function getSubdirectoriesTree($directory){
if(!is_writable($directory))
throw new Exception('cannot read target directory');
$subdirectoriesStructure=array();
if($handle=opendir($directory)){
while (false !== ($entry = readdir($handle))) {
$subdirFullPath=$directory.'/'.$entry;
if ($entry != "." && $entry != ".." && is_dir($subdirFullPath)) {
$subdirectoriesStructure[$entry]=
$this->getSubdirectoriesTree($subdirFullPath);
}
}
closedir($handle);
}
if(count($subdirectoriesStructure)==0){
return null;
}else{
return $subdirectoriesStructure;
}
}
private function toStringTree($directoriesTree, $name, $nodeTemplatePath,
$selectedBranch=null, $currentBranch="."){
$result='';
foreach($directoriesTree as $directoryName=>$contents){
$subBranch=$currentBranch.'/'.$directoryName;
if($contents){
$subRender=$this->toStringTree($contents, $name, $nodeTemplatePath,
$selectedBranch, $subBranch);
}else{
$subRender='';
}
$result.=$this->controller->renderFile($nodeTemplatePath, array(
'fileName'=>$directoryName,
'name'=>$name,
'value'=>$subBranch,
'selected'=>$selectedBranch==$subBranch,
'children'=>$subRender,
), true);
}
return $result;
}
public function run(){
if(!isset($this->directory)){
throw new Exception('directory needs to be set');
}
list($name, $id) = $this->resolveNameId();
if(!isset($this->itemView)){
// use default view for rendering tree nodes, located inside this
// extension dir.
$fileViewPath=$this->getViewFile($this->defaultFileView);
}else{
// use custom view
$fileViewPath=$this->controller->getViewFile($this->fileView);
if($fileViewPath===false)
throw new Exception(
'Provided fileView('.$this->fileView.') could not be found');
}
$structure=$this->getSubdirectoriesTree(getcwd().$this->directory);
$value=CHtml::resolveValue($this->model, $this->attribute);
echo '<div id="'.$id.'">'.
$this->toStringTree($structure, $name, $fileViewPath, $value).'</div>';
// JS
$cs = Yii::app()->getClientScript();
$assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__).
'/assets', false, -1, true);
$cs->registerScriptFile($assets . '/nodeToggle.js');
$jscode="niezgoda.filePicker.nodeToggle('".$id.
"', '.filePicker.button.toggle', '.filePicker.children.list', ".
"'.filePicker.node','".$value."');";
Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $id, $jscode);
}
}