-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfield.bootstrap_image.php
More file actions
executable file
·317 lines (265 loc) · 9.62 KB
/
field.bootstrap_image.php
File metadata and controls
executable file
·317 lines (265 loc) · 9.62 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
<?php
defined('BASEPATH') or exit('No direct script access allowed');
/**
* PyroStreams Bootstrap Image Field Type
*
* @package PyroCMS\Core\Modules\Streams Core\Field Types
* @author Rigo B Castro
* @author Parse19
* @copyright Copyright (c) 2011 - 2013
* @license https://github.com/rigobcastro/PyroCMS-Bootstrap-Image-FieldType/blob/master/LICENSE
* @link https://github.com/rigobcastro/PyroCMS-Bootstrap-Image-FieldType
*/
class Field_bootstrap_image {
public $field_type_slug = 'bootstrap_image';
// Files are saved as 15 character strings.
public $db_col_type = 'char';
public $col_constraint = 15;
public $custom_parameters = array('folder', 'resize_width', 'resize_height', 'keep_ratio', 'allowed_types');
public $version = '1.0.1';
public $author = array('name' => 'Rigo B Castro', 'url' => 'http://rigobcastro.com');
public $input_is_file = true;
// --------------------------------------------------------------------------
public function __construct()
{
get_instance()->load->library('image_lib');
}
// --------------------------------------------------------------------------
/**
* Output form input
*
* @param array
* @param array
* @return string
*/
public function form_output($params)
{
$this->CI->load->config('files/files');
$current_file = null;
// Get the file
if ($params['value'])
{
$current_file = $this->CI->db
->where('id', $params['value'])
->limit(1)
->get('files')
->row();
}
$this->CI->type->add_js('bootstrap_image', 'bootstrap-fileupload.min.js');
$this->CI->type->add_css('bootstrap_image', 'bootstrap-fileupload.min.css');
$view = $this->CI->type->load_view('bootstrap_image', 'input', array(
'params' => $params,
'current_file' => $current_file,
'width' => !empty($params['custom']['resize_width']) ? $params['custom']['resize_width'] : 200,
'height' => !empty($params['custom']['resize_height']) ? $params['custom']['resize_height'] : 150,
'accept_input_html5' => 'image/*'
));
return $view;
}
// --------------------------------------------------------------------------
/**
* Process before saving to database
*
* @access public
* @param array
* @param obj
* @return string
*/
public function pre_save($input, $field, $stream, $row_id, $form_data)
{
// If we do not have a file that is being submitted. If we do not,
// it could be the case that we already have one, in which case just
// return the numeric file record value.
if (!isset($_FILES[$field->field_slug . '_file']['name']) or !$_FILES[$field->field_slug . '_file']['name'])
{
// allow dummy as a reset
if (isset($form_data[$field->field_slug]) and $form_data[$field->field_slug])
{
return $form_data[$field->field_slug];
}
else
{
return null;
}
}
$this->CI->load->library('files/files');
// Resize options
$resize_width = (isset($field->field_data['resize_width'])) ? $field->field_data['resize_width'] : null;
$resize_height = (isset($field->field_data['resize_height'])) ? $field->field_data['resize_height'] : null;
$keep_ratio = (isset($field->field_data['keep_ratio']) and $field->field_data['keep_ratio'] == 'yes') ? true : false;
$return = Files::upload($field->field_data['folder'], null, $field->field_slug . '_file', $resize_width, $resize_height, $keep_ratio, $allowed_types);
if (!$return['status'])
{
$this->CI->session->set_flashdata('notice', $return['message']);
return null;
}
else
{
// Return the ID of the file DB entry
return $return['data']['id'];
}
}
// --------------------------------------------------------------------------
/**
* Pre Output
*
* @access public
* @param array
* @return string
*/
public function pre_output($input, $params)
{
if (!$input or $input == 'dummy')
return null;
// Get image data
$image = $this->CI->db->select('filename, alt_attribute, description, name')->where('id', $input)->get('files')->row();
if (!$image)
return null;
// This defaults to 100px wide
return '<img src="' . site_url('files/thumb/' . $input) . '" alt="' . $this->obvious_alt($image) . '" />';
}
// --------------------------------------------------------------------------
/**
* Process before outputting for the plugin
*
* This creates an array of data to be merged with the
* tag array so relationship data can be called with
* a {field.column} syntax
*
* @access public
* @param string
* @param string
* @param array
* @return array
*/
public function pre_output_plugin($input, $params)
{
if (!$input or $input == 'dummy')
return null;
$this->CI->load->library('files/files');
$file = Files::get_file($input);
if ($file['status'])
{
$image = $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 (!$image->path)
{
$image_data['image'] = base_url($this->CI->config->item('files:path') . $image->filename);
}
else
{
$image_data['image'] = str_replace('{{ url:site }}', base_url(), $image->path);
}
// For <img> tags only
$alt = $this->obvious_alt($image);
$image_data['filename'] = $image->filename;
$image_data['name'] = $image->name;
$image_data['alt'] = $image->alt_attribute;
$image_data['description'] = $image->description;
$image_data['img'] = img(array('alt' => $alt, 'src' => $image_data['image']));
$image_data['ext'] = $image->extension;
$image_data['mimetype'] = $image->mimetype;
$image_data['width'] = $image->width;
$image_data['height'] = $image->height;
$image_data['id'] = $image->id;
$image_data['filesize'] = $image->filesize;
$image_data['download_count'] = $image->download_count;
$image_data['date_added'] = $image->date_added;
$image_data['folder_id'] = $image->folder_id;
$image_data['folder_name'] = $image->folder_name;
$image_data['folder_slug'] = $image->folder_slug;
$image_data['thumb'] = site_url('files/thumb/' . $input);
$image_data['thumb_img'] = img(array('alt' => $alt, 'src' => site_url('files/thumb/' . $input)));
return $image_data;
}
}
// --------------------------------------------------------------------------
/**
* 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 = $this->CI->file_folders_m->get_folders();
$tree = (array) $tree;
if (!$tree)
{
return '<em>' . lang('streams:bootstrap_image.need_folder') . '</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);
}
// --------------------------------------------------------------------------
/**
* Param Resize Width
*
* @access public
* @param [string - value]
* @return string
*/
public function param_resize_width($value = null)
{
return form_input('resize_width', $value);
}
// --------------------------------------------------------------------------
/**
* Param Resize Height
*
* @access public
* @param [string - value]
* @return string
*/
public function param_resize_height($value = null)
{
return form_input('resize_height', $value);
}
// --------------------------------------------------------------------------
/**
* Param Allowed Types
*
* @access public
* @param [string - value]
* @return string
*/
public function param_keep_ratio($value = null)
{
$choices = array('yes' => lang('global:yes'), 'no' => lang('global:no'));
return array(
'input' => form_dropdown('keep_ratio', $choices, $value),
'instructions' => lang('streams:bootstrap_image.keep_ratio_instr'));
}
// --------------------------------------------------------------------------
/**
* Obvious alt attribute for <img> tags only
*
* @access private
* @param obj
* @return string
*/
private function obvious_alt($image)
{
if ($image->alt_attribute)
{
return $image->alt_attribute;
}
if ($image->description)
{
return $image->description;
}
return $image->name;
}
}