1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Demos |
4
|
|
|
*/ |
5
|
|
|
class FileSelectField extends Field |
6
|
|
|
{ |
7
|
|
|
public $request; |
8
|
|
|
public $files = array(); |
9
|
|
|
public $options; |
10
|
|
|
|
11
|
|
|
function __construct($name, $path, $options = array()) |
12
|
|
|
{ |
13
|
|
|
$this->name = $name; |
14
|
|
|
$this->path = $path; |
|
|
|
|
15
|
|
|
$this->options = $options; |
16
|
|
|
|
17
|
|
|
if (!isset($options['show'])) |
18
|
|
|
$this->options['show'] = true; |
19
|
|
|
|
20
|
|
|
if (!isset($options['pattern'])) |
21
|
|
|
$this->options['pattern'] = '/(.*)/'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function init($request) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->value = null; |
27
|
|
|
$di = new DirectoryIterator(DEMO_PATH . $this->path); |
28
|
|
|
foreach ($di as $file) |
29
|
|
|
if (!$file->isDot() && strpos($file->getFilename(), '.') !== 0 && preg_match($this->options['pattern'], $file->getFilename())) |
30
|
|
|
{ |
31
|
|
|
$this->files[] = $file->getFilename(); |
32
|
|
|
if ($this->value === null && isset($this->options['default']) && $this->options['default'] == $file->getFilename()) |
33
|
|
|
$this->value = $this->options['default']; |
34
|
|
|
|
35
|
|
|
if ($this->request->get($this->name) == $file->getFilename()) |
36
|
|
|
$this->value = $file->getFilename(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
sort($this->files); |
40
|
|
|
|
41
|
|
|
if (!$this->value && count($this->files) > 0) |
42
|
|
|
$this->value = $this->files[0]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function renderBody($name, $id) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if ($this->options['show']) |
48
|
|
|
{ |
49
|
|
|
$onch = "document.getElementById('sel_{$id}').src = '{$this->path}/' + this.options[this.selectedIndex].value;"; |
50
|
|
|
} |
51
|
|
|
else |
52
|
|
|
$onch = ''; |
53
|
|
|
|
54
|
|
|
echo '<select id="' . $id . '" name="' . $name . '" onchange="' . $onch . '">'; |
55
|
|
|
$selected_file = null; |
56
|
|
|
foreach ($this->files as $file) |
57
|
|
|
{ |
58
|
|
|
if ($this->value == $file) |
59
|
|
|
{ |
60
|
|
|
$sel = 'selected="selected"'; |
61
|
|
|
$selected_file = $file; |
62
|
|
|
} |
63
|
|
|
else |
64
|
|
|
$sel = ''; |
65
|
|
|
|
66
|
|
|
echo '<option ' . $sel . ' value="' . $file . '">' . $file . '</option>' . PHP_EOL; |
67
|
|
|
} |
68
|
|
|
echo '</select>'; |
69
|
|
|
|
70
|
|
|
if ($this->options['show'] && $selected_file) |
71
|
|
|
{ |
72
|
|
|
echo '<div style="display: inline; min-width: 50px; min-height: 50px">'; |
73
|
|
|
echo '<img style="position: absolute" id="sel_' . $id . '" width="50" src="' . $this->path . '/' . $selected_file . '" /> '; |
74
|
|
|
echo " "; |
75
|
|
|
echo '</div>'; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function getURLValue() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return $this->name . '=' . $this->value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|