forked from amityweb/file_filelist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.file_filelist.php
185 lines (148 loc) · 3.99 KB
/
field.file_filelist.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* PyroStreams File List Field Type
*
* @package PyroCMS\addons\shared_addons\Field Types
* @author Laurence Cope, Adapted from Samul Goodwin File Folders
*/
class Field_file_filelist
{
public $field_type_slug = 'file_filelist';
public $db_col_type = 'int';
public $alt_process = false;
public $version = '1.0';
public $author = array('name'=>'Laurence Cope', 'url'=>'');
public $_folder_list = array();
public $custom_parameters = array('folder_id');
public function param_folder_id($value)
{
$options = $this->folders();
return form_dropdown('folder_id', $options, $value, 'id="folder_id"');
}
// --------------------------------------------------------------------------
/**
* Output form input
*
* @param array
* @param array
* @return string
*/
public function form_output($data, $entry_id, $field)
{
$forms = form_dropdown($data['form_slug'], $this->files($data['custom']['folder_id'], $field->is_required), $data['value'], 'id="'.$data['form_slug'].'"');
return $forms;
}
// --------------------------------------------------------------------------
/**
* Process before outputting
*
* @param array
* @param array
* @return string
*/
public function pre_output($input)
{
$folders = $this->folders('yes');
if (trim($input) != '')
{
return $folders[$input];
}
else
{
return null;
}
}
// --------------------------------------------------------------------------
/**
* Process before outputting for the plugin
*
* @param array
* @param array
* @return string
*/
public function pre_output_plugin($input, $params)
{
$this->CI->load->library('files/files');
$file = Files::get_file((int)$input);
$folders = $this->folders('yes');
if (trim($input) != '')
{
$return['id'] = (int)$input;
$return['filename'] = $file['data']->filename; //File name of the image.
$return['image'] = $file['data']->path; //Full path to the image.
$return['path'] = $file['data']->path; //Full path to the image.
$return['description'] = $file['data']->description; //The image description.
$return['ext'] = $file['data']->extension; //The image extension.
$return['mimetype'] = $file['data']->mimetype; //The image mimetype.
$return['width'] = $file['data']->width; //Width of the full image.
$return['height'] = $file['data']->height; //Height of the full image.
return $return;
}
else
{
return null;
}
}
/**
* Get a List of Files
*/
private function files($folder_id, $is_required)
{
$this->CI->load->library('files/files');
$choices = array();
if ($is_required == 'no')
{
$choices[null] = get_instance()->config->item('dropdown_choose_null');
}
$files = Files::folder_contents($folder_id);
// Convert array of objects to array of arrays
foreach($files['data']['file'] AS $files_object)
{
$files_array[] = (array)$files_object;
}
// Sort array by name
usort($files_array, array('Field_file_filelist','compareByName'));
foreach($files_array AS $file)
{
$choices[$file['id']] = $file['name'];
}
return $choices;
}
/**
* Get a List of Folders
*/
private function folders()
{
$this->CI->load->library('files/files');
$choices = array();
$this->_build_tree_select(Files::folder_tree());
return $choices + $this->_folder_list;
}
/**
* Build the folder hierarchy
*/
private function _build_tree_select($folders, $selected = 0, $level = 0)
{
foreach ($folders AS $folder)
{
if ($level > 0)
{
$indent = '';
for ($i = 0; $i < ($level*2); $i++)
{
$indent .= ' ';
}
$indent .= '- ';
}
$this->_folder_list[$folder['id']] = $indent . $folder['name'];
if ( isset( $folder["children"] ) )
{
$this->_build_tree_select($folder['children'], $selected, $level + 1) ;
}
}
}
function compareByName($a, $b)
{
return strcasecmp($a["name"], $b["name"]);
}
}