-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfield.multiple_files.php
More file actions
476 lines (383 loc) · 16.5 KB
/
field.multiple_files.php
File metadata and controls
476 lines (383 loc) · 16.5 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* PyroStreams Relationship Field Type,
*
* @package PyroCMS\Core\Modules\Streams Core\Field Types
* @author Rigo B Castro
* @copyright Copyright (c) 2011 - 2013, Rigo B Castro
*/
class Field_multiple_files {
public $field_type_slug = 'multiple_files';
public $db_col_type = false;
public $custom_parameters = array('folder', 'upload_url', 'create_table', 'new_table_name', 'table_name', 'resource_id_column', 'file_id_column', 'max_limit_files', 'allowed_types');
public $version = '1.1.0';
public $author = array('name' => 'Rigo B Castro', 'url' => 'http://rigobcastro.com');
// --------------------------------------------------------------------------
/**
* Run time cache
*/
private $cache;
// --------------------------------------------------------------------------
public function event() {
$this->CI->type->add_misc('<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">');
$this->CI->type->add_misc('<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.min.js"></script>');
$this->CI->type->add_css('multiple_files', 'style.css');
$this->CI->type->add_js('multiple_files', 'browserplus-min.js');
$this->CI->type->add_js('multiple_files', 'plupload.full.js');
}
/**
* Output form input
*
* @param array
* @param array
* @return string
*/
public function form_output($data, $entry_id, $field) {
if (is_null($entry_id)) {
return 'Puede subir múltiples archivos en el modo edición.';
}
$this->_clean_files($field);
$upload_url = site_url('admin/files/upload');
$data = array(
'multipart_params' => array(
$this->CI->security->get_csrf_token_name() => $this->CI->security->get_csrf_hash(),
'folder_id' => $field->field_data['folder'],
),
'upload_url' => $upload_url,
'is_new' => empty($entry_id),
'field_path' => $this->ft_path,
'field' => $field
);
if (!empty($entry_id)) {
$table_data = $this->_table_data($field);
$files_out = array();
$this->CI->db->join('files as F', "F.id = {$table_data->table}.{$table_data->file_id_column}");
$files = $this->CI->db->get_where($table_data->table, array(
$this->CI->db->dbprefix($table_data->table) . '.' . $table_data->resource_id_column => $entry_id
))->result();
if (!empty($files)) {
foreach ($files as $file) {
$files_out[] = array(
'id' => $file->{$table_data->file_id_column},
'name' => $file->name,
'size' => $file->filesize * 1000,
'url' => str_replace('{{ url:site }}', base_url(), $file->path),
'is_new' => false
);
}
$data['files'] = $files_out;
}
}
return $this->CI->type->load_view('multiple_files', 'plupload_js', $data);
}
// --------------------------------------------------------------------------
/**
* User Field Type Query Build Hook
*
* This joins our user fields.
*
* @access public
* @param array &$sql The sql array to add to.
* @param obj $field The field obj
* @param obj $stream The stream object
* @return void
*/
public function query_build_hook(&$sql, $field, $stream) {
$sql['select'][] = $this->CI->db->protect_identifiers($stream->stream_prefix . $stream->stream_slug . '.id', true) . "as `" . $field->field_slug . "||{$field->field_data['resource_id_column']}`";
}
// --------------------------------------------------------------------------
public function pre_save($files, $field, $stream, $row_id, $data_form) {
$table_data = $this->_table_data($field);
$table = $table_data->table;
$resource_id_column = $table_data->resource_id_column;
$file_id_column = $table_data->file_id_column;
$max_limit_files = (int) $field->field_data['max_limit_files'];
if (!empty($max_limit_files)) {
if (count($files) > $max_limit_files) {
$this->CI->session->set_flashdata('notice', sprintf(lang('streams:multiple_files.max_limit_error'), $max_limit_files));
}
}
if ($this->CI->db->table_exists($table)) {
$this->CI->db->trans_begin();
// Reset
if ($this->CI->db->delete($table, array($resource_id_column => (int) $row_id))) {
$count = 1;
// Insert new files
foreach ($files as $file_id) {
$check = !empty($max_limit_files) ? $count <= $max_limit_files : true;
if ($check) {
if (!$this->CI->db->insert($table, array(
$resource_id_column => $row_id,
$file_id_column => $file_id
))) {
$this->CI->session->set_flashdata('error', 'Error al guardar los archivos');
return false;
}
}
$count++;
}
}
if ($this->CI->db->trans_status() === FALSE) {
$this->CI->db->trans_rollback();
$this->CI->session->set_flashdata('error', 'Error al guardar los archivos');
return false;
} else {
$this->CI->db->trans_commit();
}
}
}
/**
* Pre Ouput
*
* Process before outputting on the CP. Since
* there is less need for performance on the back end,
* this is accomplished via just grabbing the title column
* and the id and displaying a link (ie, no joins here).
*
* @access public
* @param array $input
* @return mixed null or string
*/
public function pre_output($input, $data) {
if (!$input)
return null;
$stream = $this->CI->streams_m->get_stream($data['choose_stream']);
$title_column = $stream->title_column;
// -------------------------------------
// Data Checks
// -------------------------------------
// Make sure the table exists still. If it was deleted we don't want to
// have everything go to hell.
if (!$this->CI->db->table_exists($stream->stream_prefix . $stream->stream_slug)) {
return null;
}
// We need to make sure the select is NOT NULL.
// So, if we have no title column, let's use the id
if (trim($title_column) == '') {
$title_column = 'id';
}
// -------------------------------------
// Get the entry
// -------------------------------------
$row = $this->CI->db
->select()
->where('id', $input)
->get($stream->stream_prefix . $stream->stream_slug)
->row_array();
if ($this->CI->uri->segment(1) == 'admin') {
if (isset($data['link_uri']) and ! empty($data['link_uri'])) {
return '<a href="' . site_url(str_replace(array('-id-', '-stream-'), array($row['id'], $stream->stream_slug), $data['link_uri'])) . '">' . $row[$title_column] . '</a>';
} else {
return '<a href="' . site_url('admin/streams/entries/view/' . $stream->id . '/' . $row['id']) . '">' . $row[$title_column] . '</a>';
}
} else {
return $row;
}
}
// --------------------------------------------------------------------------
/**
* Pre Ouput Plugin
*
* This takes the data from the join array
* and formats it using the row parser.
*
* @access public
* @param array $row the row data from the join
* @param array $custom custom field data
* @param mixed null or formatted array
*/
public function pre_output_plugin($row, $custom) {
$table = $custom['field_data']['table_name'];
if (empty($table)) {
$table = "{$custom['stream_slug']}_{$custom['field_slug']}";
}
$file_id_column = !empty($custom['field_data']['file_id']) ? $custom['field_data']['file_id'] : 'file_id';
$files = $this->CI->db->where($custom['field_data']['resource_id_column'], (int) $row[$custom['field_data']['resource_id_column']])->get($table)->result_array();
$return = array();
if (!empty($files)) {
$this->CI->load->library('files/files');
foreach ($files as &$_file) {
$file_id = $_file[$file_id_column];
$file = Files::get_file($file_id);
$file_data = array();
if ($file['status']) {
$__file = $file['data'];
// If we don't have a path variable, we must have an
// older style image, so let's create a local file path.
if (!$__file->path) {
$file_data['url'] = base_url($this->CI->config->item('files:path') . $__file->filename);
} else {
$file_data['url'] = str_replace('{{ url:site }}', base_url(), $__file->path);
}
$file_data['filename'] = $__file->filename;
$file_data['name'] = $__file->name;
$file_data['description'] = $__file->description;
$file_data['ext'] = $__file->extension;
$file_data['mimetype'] = $__file->mimetype;
$file_data['id'] = $__file->id;
$file_data['filesize'] = $__file->filesize;
$file_data['download_count'] = $__file->download_count;
$file_data['date_added'] = $__file->date_added;
$file_data['folder_id'] = $__file->folder_id;
$file_data['folder_name'] = $__file->folder_name;
$file_data['folder_slug'] = $__file->folder_slug;
}
$return[] = $file_data;
}
}
return $return;
}
// ----------------------------------------------------------------------
/**
* Choose a folder to upload to.
*
* @access public
* @param [string - value]
* @return string
*/
public function param_folder($value = null) {
// Get the folders
$this->CI->load->model('files/file_folders_m');
$tree = (array) $this->CI->file_folders_m->get_folders();
if (!$tree) {
return '<em>' . lang('streams:file.folder_notice') . '</em>';
}
$choices = array();
foreach ($tree as $tree_item) {
// We are doing this to be backwards compat
// with PyroStreams 1.1 and below where
// This is an array, not an object
$tree_item = (object) $tree_item;
$choices[$tree_item->id] = $tree_item->name;
}
return form_dropdown('folder', $choices, $value);
}
// --------------------------------------------------------------------------
public function param_upload_url($value = null) {
return form_input(array(
'name' => 'upload_url',
'value' => !empty($value) ? $value : site_url('admin/files/upload'),
'type' => 'text'
));
}
// --------------------------------------------------------------------------
public function param_create_table($value = null) {
$options = array(
0 => lang('global:no'),
1 => lang('global:yes'),
);
$input = form_dropdown('create_table', $options, $value);
return array(
'input' => $input,
'instructions' => $this->CI->lang->line('streams:multiple_files.instructions_create_table')
);
}
// --------------------------------------------------------------------------
public function param_new_table_name($value = null) {
$input = form_input(array(
'name' => 'new_table_name',
'value' => $value,
'type' => 'text'
));
return array(
'input' => $input,
'instructions' => $this->CI->lang->line('streams:multiple_files.instructions_new_table_name')
);
}
// --------------------------------------------------------------------------
public function param_table_name($value = null) {
$tables = get_instance()->db->list_tables(true);
$tables_dropdown = array(
'' => '-----'
);
foreach ($tables as $table) {
$prefix = explode('_', $table);
if ($prefix[0] !== 'core') {
$tables_dropdown[$table] = $table;
}
}
return array(
'input' => form_dropdown('choice_table_name', $tables_dropdown, $value),
'instructions' => $this->CI->lang->line('streams:multiple_files.instructions_table_name')
);
}
/**
* Data for choice. In x : X format or just X format
*
* @access public
* @param [string - value]
* @return string
*/
public function param_resource_id_column($value = null) {
return form_input(array(
'name' => 'resource_id_column',
'value' => !empty($value) ? $value : 'resource_id',
'type' => 'text'
));
}
// --------------------------------------------------------------------------
/**
* Data for choice. In x : X format or just X format
*
* @access public
* @param [string - value]
* @return string
*/
public function param_file_id_column($value = null) {
return form_input(array(
'name' => 'file_id_column',
'value' => !empty($value) ? $value : 'file_id',
'type' => 'text'
));
}
// --------------------------------------------------------------------------
public function param_max_limit_files($value = null) {
return form_input(array(
'name' => 'max_limit_files',
'value' => !empty($value) ? $value : 5,
'type' => 'text'
));
}
// --------------------------------------------------------------------------
private function _table_data($field) {
$object = (object) array(
'table' => (!empty($field->field_data['table_name']) ? $field->field_data['table_name'] : "{$field->stream_slug}_{$field->field_slug}"),
'resource_id_column' => $field->field_data['resource_id_column'],
'file_id_column' => (!empty($field->field_data['file_id_column']) ? $field->field_data['file_id_column'] : 'file_id')
);
if ($field->field_data['create_table'] && !empty($field->field_data['new_table_name'])) {
$table_name = $field->field_data['new_table_name'];
$ci = get_instance();
if (!$ci->db->table_exists($table_name)) {
$fields[$field->field_data['resource_id_column']] = array('type' => 'INT', 'constraint' => 11, 'null' => false);
$fields[$field->field_data['file_id_column']] = array('type' => 'VARCHAR', 'constraint' => 200, 'null' => false);
$ci->dbforge->add_field($fields);
$ci->dbforge->create_table($table_name, true);
$object->table = $table_name;
}
}
return $object;
}
// ----------------------------------------------------------------------
private function _clean_files($field) {
$table_data = $this->_table_data($field);
$content = Files::folder_contents($field->field_data['folder']);
$files = $content['data']['file'];
$valid_files = $this->CI->db->select($table_data->file_id_column . ' as id')->from($table_data->table)->get()->result();
$valid_files_ids = array();
if (!empty($valid_files)) {
foreach ($valid_files as $vf) {
array_push($valid_files_ids, $vf->id);
}
}
if (!empty($files)) {
foreach ($files as $file) {
if (!in_array($file->id, $valid_files_ids)) {
Files::delete_file($file->id);
}
}
}
}
// ----------------------------------------------------------------------
}