--- flickr_block.module.orig 2007-06-09 19:56:44.000000000 +0100 +++ flickr_block.module 2007-11-30 12:54:45.000000000 +0000 @@ -6,20 +6,24 @@ /** * Implementation of hook_block(). */ -function flickr_block($op = 'list', $delta = 0, $edit = array()) { +function flickr_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': - // User base blocks - $blocks[0]['info'] = t("Flickr user page recent photos"); - $blocks[1]['info'] = t("Flickr user page photosets"); - $blocks[2]['info'] = t("Flickr user page random photos"); - - // Site wide blocks - $blocks[3]['info'] = t('Flickr recent photos'); - $blocks[4]['info'] = t('Flickr recent photosets'); - $blocks[5]['info'] = t('Flickr random photos'); + + if ($flickr_entries = variable_get('flickr_entries',array())) { + $counter = 0; + $blocks = array(); + foreach ($flickr_entries as $flickr_entry) { + $blocks[$counter]['info'] = "Flickr entry : ".$flickr_entry['name'] ; + ++$counter; + } + } case 'configure': $count_options = array(1 => '1', 2 => '2', 3 => '3', 4 => '4', 5 => '5', 6 => '6', 7 => '7', 8 => '8', 9 => '9', 10 => '10', 15 => '15', 20 => '20', 25 => '25', 30 => '30'); @@ -30,24 +34,29 @@ function flickr_block($op = 'list', $del } unset($size_options['b']); unset($size_options['o']); - - $settings = variable_get('flickr_block_'. $delta, array('user_id' => '', 'show_n' => 4, 'size' => 's')); + + $flickr_entries = variable_get('flickr_entries',''); + $show_n = $flickr_entries[$delta]['show_n'] ? $flickr_entries[$delta]['show_n'] : 4; + $size = $flickr_entries[$delta]['size'] ? $flickr_entries[$delta]['size'] : 's'; + $user_id = $flickr_entries[$delta]['user_id'] ? $flickr_entries[$delta]['user_id'] : variable_get('flickr_default_userid',''); + $settings = array('user_id' => $user_id, 'show_n' => $show_n, 'size' => $size); $form = array(); - $form["flickr_block_{$delta}_user_id"] = array( + $form["flickr_block_user_id"] = array( '#type' => 'textfield', '#title' => t('Flickr User Id'), '#default_value' => $settings['user_id'], '#description' => t("The user id of a Flickr user. If this is left blank, the sites's default user will be used. Current default id is " . variable_get('flickr_default_userid', '')), ); - $form["flickr_block_{$delta}_show_n"] = array( + $form["flickr_block_show_n"] = array( '#type' => 'select', '#options' => $count_options, '#title' => t('Show the last n photos'), '#default_value' => $settings['show_n'], '#description' => t("The block will display this many photos.") ); - $form["flickr_block_{$delta}_size"] = array( + $form["flickr_block_size"] = array( '#type' => 'select', '#options' => $size_options, '#title' => t('Size of photos'), @@ -55,113 +64,40 @@ function flickr_block($op = 'list', $del '#description' => t("Select the size of photos you'd like to display in the block.") ); - switch ($delta) { - case 0: // user page, recent - unset($form["flickr_block_{$delta}_user_id"]); - break; - case 1: // user page, photosets - unset($form["flickr_block_{$delta}_user_id"]); - // photoset, not photos - $form["flickr_block_{$delta}_show_n"]['#title'] = t('Show the last n photosets'); - $form["flickr_block_{$delta}_show_n"]['#description'] = t("The block will show this many of the user's photosets."); - break; - case 2: // user page, random - unset($form["flickr_block_{$delta}_user_id"]); - break; - - case 3: // sitewide, recent - break; - case 4: // sitewite photoset, not photos - $form["flickr_block_{$delta}_show_n"]['#title'] = t('Show the last n photosets'); - $form["flickr_block_{$delta}_show_n"]['#description'] = t("The block will show this many of the user's photosets."); - break; - case 5: // sitewide, random - break; - } - return $form; - - case 'save': - switch ($delta) { - case 0: - case 1: - case 2: - variable_set('flickr_block_'. $delta, array( - 'show_n' => (int) $edit["flickr_block_{$delta}_show_n"], - 'size' => $edit["flickr_block_{$delta}_size"], - )); - break; - - case 3: - case 4: - case 5: - variable_set('flickr_block_'. $delta, array( - 'user_id' => $edit["flickr_block_{$delta}_user_id"], - 'show_n' => (int) $edit["flickr_block_{$delta}_show_n"], - 'size' => $edit["flickr_block_{$delta}_size"], - )); - break; - } + + + case 'save': + // TODO - first entry of $flickr_entries is empty + $flickr_entries = variable_get('flickr_entries',array()); + $flickr_entries[$delta]['user_id'] = $edit["flickr_block_user_id"]; + $flickr_entries[$delta]['show_n'] = (int)$edit["flickr_block_show_n"]; + $flickr_entries[$delta]['size'] = $edit["flickr_block_size"]; + variable_set('flickr_entries',$flickr_entries); break; case 'view': default: - $settings = variable_get('flickr_block_'. $delta, array( - 'user_id' => '', - 'show_n' => 4, - 'size' => 's', - )); - // Get the default user id as a fallback - if (isset($settings['user_id'])) { - $settings['user_id'] = variable_get('flickr_default_userid', ''); - } + $flickr_entries = variable_get('flickr_entries',''); + $user_id = $flickr_entries[$delta]['user_id'] ? $flickr_entries[$delta]['user_id'] : variable_get('flickr_default_userid', ''); + $show_n = $flickr_entries[$delta]['show_n'] ? $flickr_entries[$delta]['show_n'] : 4; + $size = $flickr_entries[$delta]['size'] ? $flickr_entries[$delta]['size'] : 's'; + $settings = array('user_id' => $user_id, 'show_n' => $show_n, 'size' => $size); + $block['subject'] = t('Flickr entry : '.$flickr_entries[$delta]['name']); + $block['content'] = _flickr_entry($settings['user_id'], $settings['show_n'], $settings['size'], $flickr_entries[$delta]); - // Get per user nsid if necessary - if ($delta < 3) { - if (arg(0) == 'user' && ($uid = (int) arg(1))) { - if ($user = user_load(array('uid' => $uid))) { - if ($user->flickr['nsid']) { - if ($delta == 0) { - $block['subject'] = t("%username's recent Flickr photos", array('%username' => $user->name)); - $block['content'] = _flickr_block_recent($user->flickr['nsid'], $settings['show_n'], $settings['size']); - } - elseif ($delta == 1) { - $block['subject'] = t("%username's recent Flickr photosets", array('%username' => $user->name)); - $block['content'] = _flickr_block_photosets($user->flickr['nsid'], $settings['show_n'], $settings['size']); - } - elseif ($delta == 2) { - $block['subject'] = t("%username's random Flickr photos", array('%username' => $user->name)); - $block['content'] = _flickr_block_random($user->flickr['nsid'], $settings['show_n'], $settings['size']); - } - } - } - } - } - elseif ($delta == 3) { - $block['subject'] = t('Flickr recent photos'); - $block['content'] = _flickr_block_recent($settings['user_id'], $settings['show_n'], $settings['size']); - } - elseif ($delta == 4) { - $block['subject'] = t('Flickr recent photosets'); - $block['content'] = _flickr_block_photosets($settings['user_id'], $settings['show_n'], $settings['size']); - } - elseif ($delta == 5) { - $block['subject'] = t('Flickr random photos'); - $block['content'] = _flickr_block_random($settings['user_id'], $settings['show_n'], $settings['size']); - } return $block; } } -function _flickr_block_recent($nsid, $show_n, $size) { - $output = ''; - if ($photos = flickr_photos_search($nsid, 1, array('per_page' => $show_n))) { - foreach($photos['photo'] as $photo) { - $output .= theme('flickr_block_photo', $photo, $size); - } - } - return $output; -} - function _flickr_block_photosets($nsid, $show_n, $size) { $photosets = flickr_photoset_get_list($nsid); $output = ''; @@ -172,30 +108,76 @@ function _flickr_block_photosets($nsid, return $output; } -function _flickr_block_random($nsid, $show_n, $size) { - $output = ''; - if ($photos = flickr_photos_search($nsid, 1, array('per_page' => 500))) { - $page_count = $photos['pages']; - // we shouldn't try to return more than the total number of photos - $to = min($show_n, $photos['total']); - $output = ''; - for ($i = 0; $i < $to; $i++) { - sleep(0.125); - // request a random page - $photos = flickr_photos_search($nsid, rand(1, $page_count), array('per_page' => 500)); - // then select a random photo - $index = rand(0, count($photos['photo']) - 1); - $output .= theme('flickr_block_photo', $photos['photo'][$index], $size); - } - } - return $output; } function theme_flickr_block_photo($p, $size = NULL) { return theme_flickr_photo($p, $size); } +function _flickr_block_random($nsid, $show_n, $size, $tags = "") { + $request = array('user_id' => $nsid,'per_page' => $show_n ,'tags' => $tags); + $result = flickr_request('flickr.photos.search', $request); + $page_count = $result['photos']['pages']; + $to = min($show_n, $result['photos']['total']); + $output = ''; + for ($i = 0; $i < $to; $i++) { + sleep(0.125); + // request a random page + $request['page'] = rand(1, $page_count); + $result = flickr_request('flickr.photos.search', $request); + // then select a random photo + $index = rand(0, count($result['photos']['photo']) - 1); + $output .= theme('flickr_photo', $result['photos']['photo'][$index], $size); + } + return $output ; +} + + +function _flickr_block_recent($nsid, $show_n, $size, $tags = "") { + $request = array('user_id' => $nsid, 'per_page' => $show_n, 'tags' => $tags); + $result = flickr_request('flickr.photos.search', $request); + $output = ''; + foreach ((array)$result['photos']['photo'] as $photo) { + $output .= theme('flickr_photo', $photo, $size); + } + return $output; } + + +function _flickr_entry($user_id, $show_n, $size, $flickr_entry = array()) { + $tags = $flickr_entry['tags'] ; + if (($flickr_entry['genre'] == 'ra') && ($flickr_entry['scope'] == 'pu')) { + return _flickr_block_random( NULL,$show_n, $size,$tags); + } + if (($flickr_entry['genre'] == 're') && ($flickr_entry['scope'] == 'pu')) { + return _flickr_block_recent(NULL,$show_n, $size,$tags); + } + if (($flickr_entry['genre'] == 'ra') && ($flickr_entry['scope'] == 'pr')) { + return _flickr_block_random($user_id, $show_n, $size, $tags); + } + if (($flickr_entry['genre'] == 're') && ($flickr_entry['scope'] == 'pr')) { + return _flickr_block_recent($user_id,$show_n, $size,$tags); + } +}