/[drupal]/contributions/modules/flickrhood/flickrhood.module
ViewVC logotype

Contents of /contributions/modules/flickrhood/flickrhood.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Thu Jan 4 03:18:00 2007 UTC (2 years, 10 months ago) by andrewlevine
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +6 -1 lines
File MIME type: text/x-php
#106081 fix error when there are no tags
1 <?php
2 // $Id: flickrhood.module,v 1.23 2006/12/28 19:19:56 andrewlevine Exp $
3
4 /* Implementation of hook_menu. */
5 function flickrhood_menu($may_cache) {
6 global $user;
7
8 if ($may_cache) {
9 //settings page
10 $items[] = array(
11 'path' => 'admin/settings/flickrhood',
12 'title' => 'Flickrhood',
13 'description' => 'Edit flickrhood options (some optional, some required)',
14 'callback' => 'drupal_get_form',
15 'callback arguments' => 'flickrhood_admin_settings',
16 'access' => user_access('administer site configuration'),
17 'type' => MENU_NORMAL_ITEM,
18 );
19 }
20 else {
21
22 $items = array();
23 $admin_access = user_access('administer flickr');
24 $view_access = user_access('view all photos');
25
26 if (arg(0) == 'flickrhood' && is_numeric(arg(1)) && arg(1) > 0) {
27 $account = user_load(array('uid' => arg(1)));
28 if ($account !== FALSE) {
29 // Always let a user view their own account
30 $view_access |= user_access('view own photos') && ($user->uid == arg(1));
31 // Only admins can view blocked accounts
32 $view_access &= $account->status || $admin_access;
33
34 //main flickrhood user page(photos)
35 $items[] = array(
36 'path' => 'flickrhood/'. arg(1),
37 'title' => $account->name . t("'s flickr"),
38 'type' => MENU_CALLBACK,
39 'callback' => 'flickrhood_viewuserphotos',
40 'callback arguments' => array(arg(1)),
41 'access' => $view_access,
42 );
43 $items[] = array(
44 'path' => 'flickrhood/'. arg(1) .'/view',
45 'title' => t('photos'),
46 'type' => MENU_DEFAULT_LOCAL_TASK,
47 'weight' => -10,
48 'access' => $view_access,
49 );
50 //flickrhood user set page
51 $items[] = array(
52 'path' => 'flickrhood/'. arg(1) .'/sets',
53 'title' => t("sets"),
54 'type' => MENU_LOCAL_TASK,
55 'callback' => 'flickrhood_viewusersetlist',
56 'callback arguments' => array(arg(1)),
57 'access' => $view_access,
58 );
59 $items[] = array(
60 'path' => 'flickrhood/'. arg(1) .'/sets/list',
61 'title' => t("list"),
62 'type' => MENU_DEFAULT_LOCAL_TASK,
63 'access' => $view_access,
64 );
65 if (arg(3)!=NULL && arg(2)=='sets') {
66 $set_info = _flickrhood_get_setinfo(arg(3));
67 if (!$set_info || (_flickrhood_get_fid(arg(1)) != $set_info['owner'])) {
68 drupal_goto('flickrhood/' . arg(1) . '/sets');
69 }
70 $items[] = array(
71 'path' => 'flickrhood/'. arg(1) .'/sets/' . arg(3),
72 'title' => t("set: ") . $set_info['title'],
73 'type' => MENU_LOCAL_TASK,
74 'callback' => 'flickrhood_viewuserset',
75 'callback arguments' => array(arg(1), $set_info),
76 'access' => $view_access,
77 );
78 }
79 //flickrhood main tags page(cloud)
80 $items[] = array(
81 'path' => 'flickrhood/'. arg(1) .'/tags',
82 'title' => t("tags"),
83 'type' => MENU_LOCAL_TASK,
84 'callback' => 'flickrhood_viewusertagcloud',
85 'callback arguments' => array(arg(1)),
86 'access' => $view_access,
87 );
88 $items[] = array(
89 'path' => 'flickrhood/'. arg(1) .'/tags/cloud',
90 'title' => t("cloud"),
91 'type' => MENU_DEFAULT_LOCAL_TASK,
92 'access' => $view_access,
93 );
94 //flickrhood tag list page
95 $items[] = array(
96 'path' => 'flickrhood/'. arg(1) .'/tags/list',
97 'title' => t("list"),
98 'type' => MENU_LOCAL_TASK,
99 'callback' => 'flickrhood_viewusertaglist',
100 'callback arguments' => array(arg(1)),
101 'access' => $view_access,
102 );
103 //flickrhood specific tag page
104 if ( (arg(3)!==NULL) && (arg(3)!='cloud') && (arg(3)!='list') ){
105 $items[] = array(
106 'path' => 'flickrhood/'. arg(1) . '/tags/' . arg(3),
107 'title' => t('tags: ') . str_replace(',', ', ', arg(3)),
108 'type' => MENU_LOCAL_TASK,
109 'callback' => 'flickrhood_viewuserphotosbytags',
110 'callback arguments' => array(arg(1), arg(3)),
111 'access' => $view_access,
112 );
113 }
114 }
115 }
116 }
117 return $items;
118 }
119
120 /* Implementation of hook_perm */
121 function flickrhood_perm () {
122 return array('view own photos', 'view all photos', 'view own tags', 'view all tags', 'administer flickr');
123 }
124
125 /* Implementation of hook_user */
126 function flickrhood_user ($op, &$edit, &$account, $category = NULL) {
127 if ($op == 'form' && $category == 'account') {
128 //create defaults
129 $user = user_load(array('uid'=>$account->uid));
130
131 $form['flickr_settings'] = array(
132 '#type' => 'fieldset',
133 '#title' => t('FlickrHood User Settings'),
134 '#tree' => TRUE,
135 '#collapsible' => TRUE,
136 '#collapsed' => FALSE,
137 '#description' => t('You must enter either your Flickr username or email to have your photos recognized by FlickrHood'),
138 '#weight' => 2,
139 );
140 $form['flickr_settings']['username'] = array(
141 '#type' => 'textfield',
142 '#title' => t('Flickr Username'),
143 '#size' => 40,
144 '#maxlength' => 40,
145 '#description' => t('The username of your Flickr account'),
146 '#default_value' => $user->flickrusername,
147 );
148 $form['flickr_settings']['mail'] = array(
149 '#type' => 'textfield',
150 '#title' => t('Flickr Email'),
151 '#size' => 40,
152 '#maxlength' => 40,
153 '#description' => t('The email used in your Flickr account'),
154 '#default_value' => $user->flickrmail,
155 );
156 return $form;
157 }
158 elseif ($op=='insert') {
159 if (!empty($edit['flickr_settings']['mail']) || !empty($edit['flickr_settings']['username'])) {
160 $f_user_array = array(
161 'username' => $edit['flickr_settings']['username'] ? $edit['flickr_settings']['username'] : '',
162 'mail' => $edit['flickr_settings']['mail'] ? $edit['flickr_settings']['mail'] : ''
163 );
164
165 $f_user = _flickrhood_find_user($f_user_array);
166 if ($f_user!==FALSE) {
167 $fid = $f_user['uid'];
168 db_query('INSERT INTO {flickrhood_users} (uid,fid,mail,username) VALUES (%d,\'%s\',\'%s\',\'%s\')',
169 $account->uid,$fid,$account->flickrmail,$account->flickrusername);
170 _flickrhood_update_user_tags($account->uid);
171 }
172 else {
173 drupal_set_message('Couldn\'t find a flickr user with the information given, the flickr functionality for your account will not be enabled.', 'error');
174 }
175 }
176 unset($account->flickrusername);unset($account->flickrmail);unset($account->flickrid);
177 }
178 elseif ($op=='update') {
179
180 if (!empty($edit['flickr_settings']['mail']) || !empty($edit['flickr_settings']['username'])) {
181 $f_user_array = array(
182 'username' => $edit['flickr_settings']['username'] ? $edit['flickr_settings']['username'] : '',
183 'mail' => $edit['flickr_settings']['mail'] ? $edit['flickr_settings']['mail'] : ''
184 );
185
186 $f_user = _flickrhood_find_user($f_user_array);
187 if ($f_user!==FALSE) {
188 $fid = $f_user['uid'];
189 //find if the user is already in the table
190 $exists = db_query('SELECT uid FROM {flickrhood_users} WHERE uid=%d', $account->uid);
191 //insert or update as neeeded
192 if (db_num_rows($exists) <= 0) {
193 db_query('INSERT INTO {flickrhood_users} (uid,fid,mail,username) VALUES (%d,\'%s\',\'%s\',\'%s\')',
194 $account->uid,$fid,$f_user_array['mail'],$f_user_array['username']);
195 }
196 else {
197 db_query('UPDATE {flickrhood_users} SET mail=\'%s\', username=\'%s\', fid=\'%s\' WHERE uid=\'%d\'',
198 $f_user_array['mail'], $f_user_array['username'], $fid, $account->uid);
199 }
200 _flickrhood_update_user_tags($account->uid);
201 }
202 else {
203 drupal_set_message('Couldn\'t find a flickr user with the information given, the flickr information you have changed will not be updated', 'error');
204 }
205 }
206 else {
207 //find if the user is already in the table
208 $exists = db_query('SELECT uid FROM {flickrhood_users} WHERE uid=%d', $account->uid);
209 if (db_num_rows($exists) > 0) {
210 //the user is attempting to delete their flickrhood account
211 db_query('DELETE FROM {flickrhood_users} WHERE uid=%d', $account->uid);
212 db_query('DELETE FROM {flickrhood_users_tags} WHERE uid=%d', $account->uid);
213 }
214 }
215 unset($account->flickrusername);unset($account->flickrmail);unset($account->flickrid);
216 }
217 elseif ($op=='load') {
218 $result = db_query('SELECT username, mail, fid FROM {flickrhood_users} WHERE uid=\'%d\'', $account->uid);
219 $fu_object = db_fetch_object($result);
220 $account->flickrmail = $fu_object->mail;
221 $account->flickrusername = $fu_object->username;
222 $account->flickrid = $fu_object->fid;
223 }
224 elseif ($op=='delete') {
225 db_query('DELETE FROM {flickrhood_users} WHERE uid=%d', $account->uid);
226 db_query('DELETE FROM {flickrhood_users_tags} WHERE uid=%d', $account->uid);
227 }
228 }
229
230 /* views a users photos and sets*/
231 function flickrhood_viewuserphotos($uid) {
232
233 //set some required pager stuff
234 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
235 global $pager_page_array, $pager_total,$pager_total_items;
236 //set this to something else if you want multiple pagers
237 $element = 0;
238 $pager_page_array[$element] = $_GET['page'] ? $_GET['page'] : '';
239
240 $per_page = variable_get('flickrhood_photos_per_page', 20);
241
242 $fp_arr = _flickrhood_public_photos($uid, array(), $pager_page_array[$element]+1, $per_page);
243
244 if ($fp_arr == FALSE) {
245 return '';
246 }
247
248 //set pager information we just acquired
249 $pager_total[$element] = $fp_arr['pages'];
250 $pager_total_items[$element] = $fp_arr['total'];
251
252 return theme('flickrhood_user_photos', $uid, $fp_arr);
253
254 }
255
256 function flickrhood_viewusertagcloud($uid) {
257
258 //get users fid
259 $fid = _flickrhood_get_fid($uid);
260
261 if ($fid===FALSE) {
262 // TODO watchdog error here
263 drupal_set_message('Problem getting users tags', 'error');
264 return '';
265 }
266
267 //get php flickr object
268 $pfo = _flickrhood_get_pfo();
269
270 //get tag info
271 $poptag_response = $pfo->tags_getListUserPopular($fid, variable_get('flickrhood_tags_in_cloud','150'));
272
273 if (!$poptag_response) {
274 drupal_set_message('Problem retrieving user\'s tags', 'error');
275 return FALSE;
276 }
277 elseif (empty($poptag_response)) {
278 drupal_set_message('User has no tags');
279 return '';
280 }
281
282 $tag_arr = array();
283 foreach ($poptag_response as $tag) {
284 $tag_arr[$tag['_content']] = $tag['count'];
285 }
286
287 if (count($tag_arr) < 1) {
288 drupal_set_message('This user has no tags');
289 return '';
290 }
291
292 //pass to theme function
293 return theme('flickrhood_user_tagcloud', $uid, $fid, $tag_arr);
294 }
295
296 function flickrhood_viewusertaglist($uid) {
297 //get users fid
298 $fid = _flickrhood_get_fid($uid);
299
300 if ($fid===FALSE) {
301 // TODO watchdog error here
302 drupal_set_message('Problem getting users tags', 'error');
303 return '';
304 }
305
306 //get all tags
307 $pfo = _flickrhood_get_pfo();
308 $taglist_response = $pfo->tags_getListUser($fid);
309
310 if (!$taglist_response) {
311 drupal_set_message('Problem retrieving user\'s tags', 'error');
312 return FALSE;
313 }
314 elseif (empty($taglist_response)) {
315 drupal_set_message('User has no tags');
316 return '';
317 }
318
319 //pass array to the theme function
320 return theme('flickrhood_user_taglist', $uid, $fid, $taglist_response);
321 }
322
323 function flickrhood_viewusersetlist($uid) {
324 //get users fid
325 $fid = _flickrhood_get_fid($uid);
326
327 if ($fid===FALSE) {
328 // TODO watchdog error here
329 drupal_set_message('Problem getting user\'s sets', 'error');
330 return '';
331 }
332
333 $pfo = _flickrhood_get_pfo();
334 $set_response = $pfo->photosets_getList($fid);
335
336 if (!$set_response) {
337 drupal_set_message('Problem retrieving user\'s sets', 'error');
338 return FALSE;
339 }
340 //TODO 0 set case
341
342 return theme('flickrhood_user_setlist', $uid, $fid, $set_response['photoset']);
343 }
344
345 function flickrhood_viewuserset($uid, $set_arr) {
346
347 //set some required pager stuff
348 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
349 global $pager_page_array, $pager_total,$pager_total_items;
350 //set this to something else if you want multiple pagers
351 $element = 0;
352 $pager_page_array[$element] = $_GET['page'] ? $_GET['page'] : '';
353
354 $per_page = variable_get('flickrhood_photos_per_page', 20);
355
356 //request set photos
357 $pfo = _flickrhood_get_pfo();
358 $set_response = $pfo->photosets_getPhotos($set_arr['id']);
359
360 if (!$set_response) {
361 drupal_set_message('Problem retrieving user\'s set photos', 'error');
362 return FALSE;
363 }
364 elseif (!array_key_exists('photo', $set_response) || empty($set_response['photo'])) {
365 drupal_set_message('This photoset is empty');
366 return '';
367 }
368
369 //set pager information we just acquired
370 $pager_total_items[$element] = sizeof($set_response['photo']);
371 $pager_total[$element] = ceil($pager_total_items[$element]/$per_page);
372
373 //get starting position
374 $page_num = is_numeric($_GET['page']) ? $_GET['page'] : 0;
375 if (sizeof($set_response['photo']) > ($page_num * $per_page)) {
376 $arr_pos = $page_num * $per_page;
377 }
378 else {
379 $arr_pos = 0;
380 }
381
382 //extract photos relevant to this page
383 $photo_info = array_slice($set_response['photo'], $arr_pos, $per_page);
384
385 return theme('flickrhood_user_set', $uid, $per_page, $photo_info, $set_arr);
386
387 }
388
389 function flickrhood_viewuserphotosbytags($uid, $tags) {
390
391 //set some required pager stuff
392 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
393 global $pager_page_array, $pager_total,$pager_total_items;
394 //set this to something else if you want multiple pagers
395 $element = 0;
396 $pager_page_array[$element] = $_GET['page'] ? $_GET['page'] : '';
397
398 $per_page = variable_get('flickrhood_photos_per_page', 20);
399
400 //parse the tags out into an array
401 $tag_arr = explode(',', $tags);
402
403 $fp_arr = _flickrhood_public_photos($uid, $tag_arr, $pager_page_array[$element]+1, $per_page);
404
405 if ($fp_arr===FALSE) {
406 return '';
407 }
408
409 //set pager information we just acquired
410 $pager_total[$element] = $fp_arr['pages'];
411 $pager_total_items[$element] = $fp_arr['total'];
412
413 return theme('flickrhood_user_photos', $uid, $fp_arr);
414
415 }
416
417 function theme_flickrhood_user_photos ($uid, $fp_arr) {
418
419 drupal_add_css(drupal_get_path('module', 'flickrhood') . '/flickrhood.css');
420
421 $output =
422 theme('pager', NULL, variable_get('flickrhood_photos_per_page', $fp_arr['perpage']));
423
424 $output .= "<ul id=\"flickrhood_photolist\">\n";
425
426 if (array_key_exists('photo', $fp_arr)) {
427 foreach ($fp_arr['photo'] as $photo) {
428 $imgpage_url = 'http://www.flickr.com/photos/' . $photo['owner'] . '/' . $photo['id'];
429 $img_url = 'http://static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_t.jpg';
430
431 $output .= '<li>';
432 $output .= l("<img src=\"$img_url\">", $imgpage_url, array(), NULL, NULL, FALSE, TRUE);
433 $output .= '</li>';
434 $output .= "\n";
435
436 }
437 }
438
439 $output .= '</ul>' . "\n";
440
441 $output .=
442 theme('pager', NULL, variable_get('flickrhood_photos_per_page', $fp_arr['perpage']));
443
444 return $output;
445 }
446
447 function theme_flickrhood_user_set($uid, $per_page, $photo_info, $set_arr) {
448
449 drupal_add_css(drupal_get_path('module', 'flickrhood') . '/flickrhood.css');
450
451 $fid = $set_arr['owner'];
452
453 $output =
454 theme('pager', NULL, variable_get('flickrhood_photos_per_page', $per_page));
455
456 $output .= "<ul id=\"flickrhood_photoset\">\n";
457
458 foreach ($photo_info as $photo) {
459 $imgpage_url = 'http://www.flickr.com/photos/' . $fid . '/' . $photo['id'];
460 $img_url = 'http://static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_t.jpg';
461
462 $output .= '<li>';
463 $output .= l("<img src=\"$img_url\">", $imgpage_url, array(), NULL, NULL, FALSE, TRUE);
464 $output .= '</li>';
465 $output .= "\n";
466
467 }
468
469
470 $output .= '</ul>' . "\n";
471
472 $output .=
473 theme('pager', NULL, variable_get('flickrhood_photos_per_page', $per_page));
474
475 return $output;
476
477 }
478
479 function theme_flickrhood_user_tagcloud($uid, $fid, $tag_arr) {
480
481 drupal_add_css(drupal_get_path('module', 'flickrhood') . '/flickrhood.css');
482
483 $max_occur = max($tag_arr);
484 $min_occur = max($tag_arr);
485
486 // Font size specified in em
487 $max_font = 3.5;
488 $min_font = 0.7;
489
490 $output = '<ul id="flickrhood_tagcloud">' . "\n";
491 foreach ($tag_arr as $tagname => $occurences) {
492
493 $font_size = round(($max_font-$min_font)*sqrt($occurences/$max_occur)+$min_font, 1);
494
495 $output .= '<li style="font-size: '.$font_size.'em">' . "\n";
496 $output .= l($tagname, 'flickrhood/' . $uid . '/tags/' . $tagname) . "\n";
497 $output .= "</li>\n";
498
499 }
500 $output .= '</ul>' . "\n";
501
502 return $output;
503
504 }
505
506 function theme_flickrhood_user_taglist($uid, $fid, $tag_arr) {
507
508 drupal_add_css(drupal_get_path('module', 'flickrhood') . '/flickrhood.css');
509
510 $output = '<ul id="flickrhood_taglist">' . "\n";
511 $first_char = '';
512 foreach ($tag_arr as $tag) {
513 if ($first_char != substr($tag,0,1)) {
514 $new_item = TRUE;
515 }
516 else {
517 $new_item = FALSE;
518 }
519
520 if ($first_char!='' && $new_item) {
521 $output .= "</ul></li>\n";
522 }
523 if ($new_item) {
524 $first_char = substr($tag,0,1);
525 $output .= "<li><ul>\n";
526 }
527
528 $output .= '<li>' . "\n";
529 $output .= l($tag, 'flickrhood/' . $uid . '/tags/' . $tag) . "\n";
530 $output .= '</li>' . "\n";
531 }
532 if ($first_char!='') {
533 $output .= "</ul></li>\n";
534 }
535 $output .= "</ul>\n";
536
537 return $output;
538 }
539
540 function theme_flickrhood_user_setlist($uid, $fid, $set_arr) {
541
542 drupal_add_css(drupal_get_path('module', 'flickrhood') . '/flickrhood.css');
543
544 $output = '<ul id="flickrhood_setlist">' . "\n";
545
546 foreach ($set_arr as $set) {
547 $output .= '<li>' . "\n";
548
549 //write photo
550 if (isset($set['primary'])) {
551 $thumb = '<img src="http://static.flickr.com/' . $set['server'] .
552 '/' . $set['primary'] . '_' . $set['secret'] .
553 '_t.jpg' . '">';
554 $output .= l($thumb, "flickrhood/$uid/sets/" . $set['id'] ,array(),NULL,NULL,FALSE,TRUE);
555
556 }
557
558 if (!empty($set['title'])) {
559 $output .= '<span class="flickrhood_settitle">' . "\n";
560 $output .= l($set['title'], "flickrhood/$uid/sets/" . $set['id']);
561 $output .= '</span>' . "\n";
562 }
563
564 if (!empty($set['description'])) {
565 $output .= '<span class="flickrhood_setdescription">' . "\n";
566 $output .= $set['description'];
567 $output .= '</span>' . "\n";
568 }
569
570 $output .= '<div class="clearleft"></div>' . "\n";
571 $output .= '</li>' . "\n";
572
573 }
574
575 $output .= "</ul>\n";
576
577 return $output;
578 }
579
580 function _flickrhood_get_fid($uid) {
581 //get users fid
582 $fid = db_result(db_query('SELECT fid FROM {flickrhood_users} where uid=%d', $uid));
583 if (!$fid) {
584 return FALSE;
585 }
586 else {
587 return $fid;
588 }
589 }
590
591 function _flickrhood_get_setinfo($setid) {
592
593 $pfo = _flickrhood_get_pfo();
594 $set_response = $pfo->photosets_getInfo($setid);
595
596 //check for error
597 if (!$set_response) {
598 //TODO watchdog
599 return FALSE;
600 }
601
602 //TODO set does not exist
603
604 return $set_response;
605 }
606
607 //returns an array with the flickr user info
608 //array['uid'] = unique flickr user id
609 //array['username'] = flickr user name
610 // TODO watchdog errors with more specific error message and better user error messages
611 function _flickrhood_find_user($f_user_array) {
612
613 $pfo = _flickrhood_get_pfo();
614
615 //booleans to keep track of success
616 $mail_success = FALSE;
617 $username_success = FALSE;
618
619 //try to find by email
620 if (array_key_exists('mail', $f_user_array) && !empty($f_user_array['mail'])) {
621
622 $mail_response = $pfo->people_findByEmail($f_user_array['mail']);
623
624 if ($mail_response!==FALSE) {
625 $mail_success = TRUE;
626 $user_info = array(
627 'uid' => $mail_response['nsid'],
628 'username' => $mail_response['username'],
629 );
630 }
631 }
632
633 //try to find by username
634 if (array_key_exists('username', $f_user_array) && !empty($f_user_array['username']) && !$mail_success) {
635
636 $username_response = $pfo->people_findByUsername($f_user_array['username']);
637
638 if ($username_response!==FALSE) {
639 $username_success = TRUE;
640 $user_info = array(
641 'uid' => $username_response['nsid'],
642 'username' => $username_response['username'],
643 );
644 }
645
646 }
647
648 if ($mail_success || $username_success) {
649 return $user_info;
650 }
651 else {
652 return FALSE;
653 }
654
655 }
656
657 /* Implementation of hook_settings */
658 function flickrhood_admin_settings() {
659 $form['flickrhood_apik'] = array('#type' => 'textfield', '#title' => t('Flickr API Key'),
660 '#size' => 60, '#maxlength' => 128, '#required' => TRUE,
661 '#default_value' => variable_get('flickrhood_apik',''));
662 $form['flickrhood_photos_per_page'] = array('#type' => 'textfield', '#title' => t('Photos per page'),
663 '#size' => 3, '#maxlength' => 3, '#required' => TRUE,
664 '#default_value' => variable_get('flickrhood_photos_per_page','20'));
665 $form['flickrhood_tags_in_cloud'] = array('#type' => 'textfield', '#title' => t('Tags in tag cloud page'),
666 '#size' => 3, '#maxlength' => 3, '#required' => TRUE,
667 '#default_value' => variable_get('flickrhood_tags_in_cloud','150'));
668 $form['flickrhood_updateinterval'] = array('#type' => 'textfield', '#title' => t('Update interval in minutes'),
669 '#size' => 5, '#maxlength' => 5, '#required' => TRUE,
670 '#description' => 'User settings will update when cron.php is run if the last update was at least this value of time apart from the current time',
671 '#default_value' => variable_get('flickrhood_updateinterval',0));
672
673 return system_settings_form($form);
674 }
675
676 /* Implementation of hook_cron */
677 function flickrhood_cron() {
678 //interval in seconds
679 $interval = time() - variable_get('flickrhood_lastupdate', 0);
680 //in minutes
681 $interval = $interval / 60;
682
683 if ($interval > variable_get('flickrhood_updateinterval', 0)) {
684 //update all users
685 variable_set('flickrhood_lastupdate', time());
686
687 $result = db_query('SELECT uid FROM {flickrhood_users}');
688
689 while ($user_obj = db_fetch_object($result)) {
690 $uid = $user_obj->uid;
691 _flickrhood_update_user_tags($uid);
692 }
693 }
694 }
695
696 // Help. XXX: TODO: Fill it in.
697 function flickrhood_help($section) {
698 switch ($section) {
699 case 'admin/modules#description':
700 return t('Allows a community to integrate their Flickr photos into Drupal');
701 }
702 }
703
704 /* Returns an array of the users most recent photos */
705 function _flickrhood_update_user_tags($uid) {
706
707 //get users fid
708 $fid = _flickrhood_get_fid($uid);
709
710 // TODO: watchdog
711 if ($fid === FALSE) {
712 return FALSE;
713 }
714
715 $tag_array = array();
716 $existing_tags = array();
717
718 //fetch users cached tags
719 $result = db_query('SELECT t.tag
720 FROM {flickrhood_tags} as t, {flickrhood_users_tags} as ut
721 WHERE t.tag_id=ut.tag_id AND ut.uid=%d', $uid);
722
723 while ($row = db_fetch_object($result)) {
724 $existing_tags[] = $row->tag;
725 }
726
727 //fetch tags from flickr
728 $pfo = _flickrhood_get_pfo();
729 $taglist_response = $pfo->tags_getListUser($fid);
730
731 if (!$taglist_response) {
732 return FALSE;
733 }
734
735 $delete_queue = array_diff($existing_tags, $taglist_response);
736 $insert_queue = array_diff($taglist_response, $existing_tags);
737
738 foreach ($insert_queue as $tag) {
739 //find out if it already exists
740 $result = db_query('SELECT tag_id FROM {flickrhood_tags}
741 WHERE tag=\'%s\'', $tag);
742
743 if (db_num_rows($result) > 0) {
744 //if it does, do save the id
745 $tag_id = db_result($result);
746 }
747 else {
748 //we need to insert the new tag and save the id
749 $tag_id = db_next_id('flickrhood_tags');
750 db_query('INSERT INTO {flickrhood_tags} (tag_id, tag)
751 VALUES (%d, \'%s\')', $tag_id, $tag);
752 }
753 //add users tag
754 db_query('INSERT INTO {flickrhood_users_tags} (uid, tag_id)
755 VALUES (%d, %d)', $uid, $tag_id);
756 }
757
758
759
760 //delete tags that are no longer used by the user
761 foreach ($delete_queue as $dead_tag) {
762 $dead_tag_id = db_result(db_query('SELECT tag_id
763 FROM {flickrhood_tags}
764 WHERE tag=\'%s\'', $dead_tag));
765 db_query('DELETE
766 FROM {flickrhood_users_tags}
767 WHERE uid=%d AND tag_id=%d', $uid, $dead_tag_id);
768 }
769 }
770
771 /*
772 * _flickrhood_public_photos will return an array of information about the photos you requested.
773 * Photo array returned is generated by a photos_search call.
774 */
775
776 function _flickrhood_public_photos($uid, $tags=array(), $page=1, $per_page=NULL) {
777 //get users fid
778 $fid = _flickrhood_get_fid($uid);
779
780 if ($fid===FALSE) {
781 drupal_set_message('Requested user has not entered a valid Flickr username or email', 'error');
782 // TODO watchdog error here
783 return FALSE;
784 }
785
786 //set $per_page
787 if ($per_page==NULL) {
788 $per_page = variable_get('flickrhood_photos_per_page', 20);
789 }
790
791 $params = array ('user_id' => $fid,
792 'per_page' => $per_page,
793 'page' => $page);
794
795 if (!empty($tags)) {
796 $params['tags'] = implode(',', $tags);
797 $params['tag_mode'] = 'all';
798 }
799
800 $pfo = _flickrhood_get_pfo();
801 $search_response = $pfo->photos_search($params);
802
803
804 if (!$search_response) {
805 drupal_set_message('Problem retrieving user\'s photos', 'error');
806 return FALSE;
807 }
808
809 if (!array_key_exists('photo', $search_response) || empty($search_response['photo']) ) {
810 drupal_set_message('No photos found');
811 return FALSE;
812 }
813
814 return $search_response;
815 }
816
817 /* get a phpFlickr object */
818 function _flickrhood_get_pfo($secret = NULL, $die_on_error = false, $api_key = NULL) {
819 include_once('lib/phpFlickr/phpFlickr.php');
820
821 if ($api_key == NULL) {
822 $api_key = variable_get('flickrhood_apik', '');
823 }
824
825 return new phpFlickr($api_key, $secret, $die_on_error);
826 }
827
828 ?>

  ViewVC Help
Powered by ViewVC 1.1.2