-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgallery.album.inc
395 lines (373 loc) · 12.7 KB
/
rgallery.album.inc
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
<?php
/**
* Gallery
*/
function rgallery_my_gallery($account = NULL) {
global $user;
// no user pass from url
if (!isset($account)) $account = $user;
// not found
if (empty($account)) drupal_access_denied();
drupal_add_css(rgallery_path() .'/css/rgallery.css');
// Privacy album
if (user_access('view any gallery') || $account->uid == $user->uid) {
$albums = db_query("SELECT nid FROM {node} WHERE type = :type AND uid = :uid ORDER BY created DESC", array(':type' => 'ralbum', ':uid' => $account->uid))->fetchAll();
}
else {
$albums = db_query("SELECT nid FROM {node} WHERE type = :type AND uid = :uid AND promote = 1 ORDER BY created DESC", array(':type' => 'ralbum', ':uid' => $account->uid))->fetchAll();
}
$output = '';
foreach ($albums as $album) {
$node = node_load($album->nid);
$photos = rgallery_get_photos_by_album($node);
$thumbnail = rgallery_get_thumbnail($node->nid);
if (!$thumbnail) {
if ($preset = variable_get('rgallery_imagestyle_cover', FALSE)) {
$thumbnail = theme('image_style', array('style_name' => $preset, 'path' => 'public://rgallery/default.jpg'));
}
}
$output .= '<li>'. theme('rgallery_album_item', array('thumbnail' => $thumbnail, 'title' => $node->title, 'count' => count($photos), 'link' => 'node/'. $node->nid)) .'</li>';
}
if (empty($output)) {
$output = '<h6>There are no albums in this gallery.</h6>';
}
// set title
drupal_set_title(t('Gallery of @name', array('@name' => $account->name)));
$output = '<ul id="rgallery-view-albums-list">' . $output .'</ul>';
if ($user->uid == $account->uid && user_access('create ralbum content', $account)) {
$output = l(t('Create new album'), 'node/add/ralbum').$output;
}
return theme('rgallery_wrapper', array('data' => $output));
}
function rgallery_view_gallery($account) {
return rgallery_my_gallery($account);
}
/**
* Add photos form
*/
function rgallery_album_add_photos($node) {
global $user;
//swfupload
$types = explode(',', str_replace(' ', '', variable_get('rgallery_file_types', '*')));
for ($i = 0; $i < count($types); $i++) {
$types[$i] = '*.' . $types[$i];
}
$type = implode(';', $types);
$setting = array(
'rgallery_swfupload' => array(
'uploader' => base_path() . rgallery_path() . '/swfupload/swfupload.swf',
'uploaderv9' => base_path() . rgallery_path() . '/swfupload/swfupload_fp9.swf',
'uploadUrl' => $GLOBALS['base_url'] . '/rgallery/photos/upload',
'session' => array(
'id' => session_id(),
'name' => session_name(),
'uid' => $user->uid,
),
'fileSizeLimit' => variable_get('rgallery_file_size_limit', 0),
'fileTypes' => $type,
'fileTypesDescription' => variable_get('rgallery_file_types_description', 'All Files'),
'fileUploadLimit' => variable_get('rgallery_file_upload_limit', 0),
'fileQueueLimit' => variable_get('rgallery_file_queue_limit', 0),
'buttonImage' => base_path() . rgallery_path() . '/images/swfupload-button-61x22.png',
'debug' => variable_get('rgallery_file_debug', 0),
'album_id' => $node->nid,
),
);
drupal_add_js($setting, 'setting');
drupal_add_css(rgallery_path() . '/css/swfupload.css');
drupal_add_css(rgallery_path() . '/css/rzgallery.css');
drupal_add_js(rgallery_path() . '/swfupload/swfupload.js');
drupal_add_js(rgallery_path() . '/swfupload/swfupload.queue.js');
drupal_add_js(rgallery_path() . '/swfupload/fileprogress.js');
drupal_add_js(rgallery_path() . '/swfupload/handlers.js');
drupal_add_js(rgallery_path() . '/js/swfu.js');
drupal_add_js(rgallery_path() . '/js/upload.js');
// form upload
$output = '<form id="ragllery-photos-upload" method="post" enctype="multipart/form-data">';
$output .= ' <div class="swfupload-fieldset swfupload-flash" id="fsUploadProgress">';
$output .= ' <span class="legend">Upload Queue</span>';
$output .= ' </div>';
$output .= ' <div id="divStatus">0 Files Uploaded</div>';
$output .= ' <div>';
$output .= ' <span id="spanButtonPlaceHolder"></span>';
$output .= ' <input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px 0px 0px 2px; font-size: 8pt; height: 21px;" />';
$output .= ' </div>';
$output .= '</form>';
$output .= '<ul id="new-photos-list"></ul>';
return $output;
}
/**
* JSON upload photo
*
* Return HTTP 1.1 Status Code Definitions
*
* 200: OK
* 400: Bad request
* 403: Forbiden
* 409: Conflict (no definition)
*/
function rgallery_album_photos_upload() {
if(isset($_FILES['Filedata'])) {
$sid = $_POST['PHPSESSID'];
$aid = $_POST['AID'];
$user = db_query("SELECT uid FROM {sessions} WHERE sid = :sid ORDER BY timestamp DESC LIMIT 0, 1", array(':sid' => $sid))->fetch();
if (!$user->uid) {
drupal_json_output(array('status' => 403));
return;
}
if ($user->uid != 1) {
$user = user_load($user->uid);
if (!$user) {
drupal_json_output(array('status' => 403));
return;
}
}
// check permission
if (rgallery_manage_access('add', node_load($aid), $user)) {
$uploadpath = rgallery_album_uri($aid);
$filename = $_FILES['Filedata']['name'];
$tmp_name = $_FILES['Filedata']['tmp_name'];
$size = $_FILES['Filedata']['size'];
// filter no thai and rename
if(ereg("[ก-๙]", $filename)) {
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
}
else {
$name = $filename;
}
$name = 'error-thai-name';
$filename = $name . $ext;
}
$filepath = $uploadpath . '/' . $filename;
$source = new stdClass();
$source->uri = $tmp_name;
$source->uid = $user->uid;
$source->filemime = file_get_mimetype(trim(basename($filename), '.'));
if ($file = file_copy($source, $filepath, FILE_EXISTS_RENAME)) {
$node = new stdClass();
$node->title = $file->filename;
$node->type = 'rphoto';
$node->body[LANGUAGE_NONE][0]['value'] = $file->filename;
$node->uid = $_POST['UID'];
$node->name = $user->name;
$node->language = LANGUAGE_NONE;
$node->status = 1;
$node->promote = 0;
if (module_exists('comment')) {
$node->comment = variable_get("comment_" . $node->type, COMMENT_NODE_OPEN);
}
node_save($node);
if ($node->nid) {
$last_weight = db_query("SELECT weight FROM {ralbum} WHERE aid = :aid ORDER BY weight DESC", array(':aid' => $aid))->fetchCol();
$weight = 0;
if (!empty($last_weight)) $weight = $last_weight[0] + 1;
$is_thumbnail = $weight == 0 ? 1 : 0;
db_insert('ralbum')
->fields(array(
'aid' => $aid,
'pid' => $node->nid,
'fid' => $file->fid,
'weight' => $weight,
'is_thumbnail' => $is_thumbnail
))
->execute();
drupal_json_output(array('status' => 200));
}
else {
drupal_json_output(array('status' => 400));
}
}
}
else {
drupal_json_output(array('status' => 403));
}
}
else {
drupal_json_output(array('status' => 204));
}
}
/**
* Edit photo page
*/
function rgallery_album_edit_photos($node) {
if (rgallery_manage_access('edit_photo', $node)) {
drupal_add_js(rgallery_path() .'/js/ui.custom.js');
drupal_add_js(rgallery_path() .'/js/edit.js');
drupal_add_js(array('rgallery' => array('aid' => $node->nid,)), 'setting');
}
if (rgallery_manage_access('delete', $node)) {
drupal_add_js(rgallery_path() .'/js/delete.js');
}
drupal_add_css(rgallery_path() .'/css/rgallery.css');
// render items
$output = theme('rgallery_photos_list', array('photos' => rgallery_get_photos_by_album($node)));
if (rgallery_manage_access('edit_photo', $node)) {
$output .= '<div id="rgallery-edit-photos-buttons" class="rgallery-buttons-wrapper">';
$output .= '<input type="button" id="rgallery-save-button" value="Save Order" />';
$output .= '<input type="button" id="rgallery-cancel-button" value="Cancel" />';
$output .= '</div>';
}
return theme('rgallery_wrapper', array('data' => $output, 'attributes' => array('id' => 'rgallery-edit-photos-wrapper')));
}
/**
* Edit photo form
*/
function rgallery_album_edit_photos_form(&$form_state, $node) {
$form['rgallery_edit_photos']['#tree'] = TRUE;
$photos = _rgallery_get_photos_by_album($node);
if (count($photos)) {
foreach ($photos as $nid => $photo) {
$form['rgallery_edit_photos'][$nid] = rgallery_album_edit_photos_form_fields($photo);
}
}
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
'#disabled' => empty($photos),
);
return $form;
}
/**
* Edit photo form field
*/
function rgallery_album_edit_photos_form_fields($photo) {
$form['delete'] = array(
'#type' => 'checkbox',
'#value' => $photo['nid'],
);
$form['title'] = array(
'#type' => 'textfield',
'#value' => $photo['title'],
);
$form['description'] = array(
'#type' => 'textarea',
'#rows' => 3,
'#value' => $photo['description'],
);
$form['image'] = array(
'#type' => 'markup',
'#value' => $photo['image'],
);
$form['weight'] = array(
'#type' => 'weight',
'#default_value' => $photo['weight'],
);
return $form;
}
/**
* Delete photo callback
*
*/
function rgallery_album_photo_delete($node = NULL) {
$node = node_load($node->nid);
$album_node = node_load($node->album_id);
node_delete($node->nid);
drupal_get_messages();
$photo_left = rgallery_get_photos_by_album($album_node);
$count = count($photo_left);
if ($node->is_thumbnail && $count > 0) {
$pids = array_keys($photo_left);
$new_pid = $pids[0];
db_update('ralbum')
->fields(array(
'is_thumbnail' => 1,
))
->condition('pid', $new_pid)
->execute();
}
drupal_json_output(array('status' => 200, 'data' => array('count' => $count, 'new_thumbnail' => $new_pid)));
}
/**
* Save photo by JSON
*/
function rgallery_album_photo_save($node = NULL) {
if (!empty($node->nid)) {
if (rgallery_manage_access('edit_photo', $node)) {
// Save title
switch ($node->type) {
case 'rphoto':
if (isset($_POST['content'])) {
$node->body[LANGUAGE_NONE][0]['value'] = $_POST['content'];
node_save($node);
$json = array(
'status' => 200,
'data' => $node->body[LANGUAGE_NONE][0]['value'],
);
drupal_json_output($json);
}
break;
case 'ralbum':
if (isset($_POST['order'])) {
$pids = explode(',', $_POST['order']);
if (count($pids)) {
foreach ($pids as $weight => $pid) {
$is_thumbnail = ($pid == $_POST['thumbnail']) ? 1 : 0;
db_update('ralbum')
->fields(array(
'weight' => $weight,
'is_thumbnail' => $is_thumbnail,
))
->condition('aid', $node->nid)
->condition('pid', $pid)
->execute();
}
drupal_json_output(array('status' => 200));
}
}
break;
default:
drupal_json_output(array('status' => 204));
}
}
else {
drupal_json_output(array('status' => 403));
}
}
else {
drupal_json_output(array('status' => 403));
}
}
/*function rgallery_album_photo_save($node) {
if ($nid = $_POST['nid']) {
// check permission
$node = node_load($nid);
if (rgallery_manage_access('edit', $node)) {
// update description
if (isset($_POST['content'])) {
if ($node) {
$node->body[LANGUAGE_NONE][0]['value'] = $_POST['content'];
node_save($node);
$json = array(
'status' => 200,
'data' => $node->body[LANGUAGE_NONE][0]['value'],
);
drupal_json_output($json);
}
}
// update order
elseif (isset($_POST['order'])) {
$pids = explode(',', $_POST['order']);
if (count($pids)) {
foreach ($pids as $weight => $pid) {
$is_thumbnail = $pid == $_POST['thumbnail'] ? 1 : 0;
db_update('ralbum')
->fields(array(
'weight' => $weight,
'is_thumbnail' => $is_thumbnail,
))
->condition('aid', $nid)
->condition('pid', $pid)
->execute();
}
drupal_json_output(array('status' => 200));
}
}
}
// permission denied
else {
drupal_json_output(array('status' => 403));
}
}
}*/