Example #1
0
 function testConstructor_AssignsPhotosPerPageToMaximum()
 {
     $pl = new Phlickr_PhotoList($this->request, Phlickr_PhotoList::PER_PAGE_MAX + 1);
     $this->assertEquals(Phlickr_PhotoList::PER_PAGE_MAX, $pl->getPhotosPerPage(), "Didn't limit photos per page to the maximum.");
 }
require_once 'Phlickr/PhotoListIterator.php';
print "This script lets you select photos by tag and then set the taken date\n";
print "to a month-year date.\n\n";
// set up the api connection
$api = Phlickr_Api::createFrom(API_CONFIG_FILE);
$api->setCacheFilename(CACHE_FILE);
if (!$api->isAuthValid()) {
    die("invalid flickr logon");
}
// get a list of tags
print 'Enter a comma separated list of tags: ';
$tags = trim(fgets(STDIN));
// create a request to search for your photos with the tags.
$request = $api->createRequest('flickr.photos.search', array('tags' => $tags, 'tag_mode' => 'all', 'user_id' => $api->getUserId()));
// use the photo list to parse the search results
print "Searching for matching photos tagged with '{$tags}'...\n";
$pl = new Phlickr_PhotoList($request, Phlickr_PhotoList::PER_PAGE_MAX);
print "Found {$pl->getCount()} photos.\n";
print 'Year: ';
$year = (int) trim(fgets(STDIN));
print 'Month: ';
$month = (int) trim(fgets(STDIN));
print "The photo's taken date will be set to {$year}-{$month}.\n";
print "Press return to continue or CTRL+C to quit.\n";
fgets(STDIN);
$pi = new Phlickr_PhotoListIterator($pl);
foreach ($pi->getPhotos() as $p) {
    print "Changing date on '{$p->getTitle()}'...\n";
    $p->setTaken($year . '-' . $month, 4);
}
exit(0);
 /**
  * Constructor.
  *
  * @param object Phlickr_PhotoList $pl The photo list to iterate.
  * @param boolean $isCachedAllowed Is cached data acceptable?
  */
 public function __construct(Phlickr_PhotoList $pl, $isCachedAllowed = true)
 {
     $this->_pl = $pl;
     $this->_isCachedAllowed = $isCachedAllowed;
     $this->_perPage = $pl->getPhotosPerPage();
     $this->rewind();
 }
 public function getUserUI($subclass_user_interface = null, $force_data_refresh = false)
 {
     // Init values
     $html = '';
     if ($this->_PhlickrAvailable) {
         $api = $this->getFlickrApi();
         if (!is_null($api)) {
             // Load cache only on the User UI side
             if ($force_data_refresh === false) {
                 $this->loadCacheFromDatabase();
             }
             try {
                 $photos = null;
                 switch ($this->getSelectionMode()) {
                     case self::SELECT_BY_GROUP:
                         if ($this->getGroupId()) {
                             $photo_pool = new Phlickr_Group($api, $this->getGroupId());
                             $photos = $photo_pool->getPhotoList($this->getPhotoBatchSize())->getPhotos();
                         }
                         break;
                     case self::SELECT_BY_TAGS:
                         if ($this->getTags()) {
                             $request = $api->createRequest('flickr.photos.search', array('tags' => $this->getTags(), 'tag_mode' => $this->getTagMode()));
                             $photo_list = new Phlickr_PhotoList($request, $this->getPhotoBatchSize());
                             $photos = $photo_list->getPhotos();
                         }
                         break;
                     case self::SELECT_BY_USER:
                         if ($this->getFlickrUserId()) {
                             $user = new Phlickr_User($api, $this->getFlickrUserId());
                             $photos = $user->getPhotoList($this->getPhotoBatchSize())->getPhotos();
                         }
                         break;
                 }
                 if (is_array($photos) && !empty($photos)) {
                     $size = $this->getPreferredSize();
                     if (empty($size)) {
                         $size = null;
                     }
                     // Preload authors ( this will be cached )
                     foreach ($photos as $cache_authors) {
                         $author = new Phlickr_User($api, $cache_authors->getUserId());
                     }
                     switch ($this->getDisplayMode()) {
                         case self::DISPLAY_GRID:
                             // If there's enough photo show a grid
                             if (count($photos) >= self::GRID_X * self::GRID_Y) {
                                 $grid_photos_idx = array();
                                 $html .= "<table>\n";
                                 for ($i = 0; $i < self::GRID_X; $i++) {
                                     $html .= "<tr>\n";
                                     for ($j = 0; $j < self::GRID_Y; $j++) {
                                         $photo = $photos[$i * self::GRID_X + $j];
                                         if (is_object($photo)) {
                                             $author = new Phlickr_User($api, $photo->getUserId());
                                             $formats = $photo->getSizes();
                                             $html .= '<td><div class="flickr_photo"><a href="' . $photo->buildUrl() . '"><img width="' . $formats[$size][0] . '" height="' . $formats[$size][1] . '" title="[' . $author->getName() . "] " . $photo->getTitle() . '" src="' . $photo->buildImgUrl($size) . '"></a></div></td>' . "\n";
                                         }
                                     }
                                     $html .= "</tr>\n";
                                 }
                                 $html .= "</table>\n";
                             }
                             break;
                         case self::DISPLAY_FEATURE:
                             // Get the last photo
                             $photo = $photos[0];
                             if (is_object($photo)) {
                                 if ($this->shouldDisplayTitle()) {
                                     $title = $photo->getTitle();
                                     if (!empty($title)) {
                                         $html .= '<div class="flickr_title"><h3>' . $photo->getTitle() . '</h3></div>' . "\n";
                                     }
                                 }
                                 /**
                                  * @todo Find a way to display tags nicely
                                  */
                                 /*
                                                                     if ($this->shouldDisplayTags())
                                                                     {
                                                                         $tags = $photo->getTags();
                                                                         if (!empty ($tags))
                                                                         {
                                                                             $html .= '<div class="flickr_tags">'."\n";
                                                                             $html .= '<h3>'._("Tags")."</h3>\n";
                                                                             $html .= '<ul>'."\n";
                                                                             foreach ($tags as $tag)
                                                                             {
                                                                                 $url_encoded_tag = urlencode($tag);
                                                                                 $html .= '<li><a href="http://www.flickr.com/photos/tags/'.$url_encoded_tag.'/">'.$tag.'</a></li>'."\n";
                                                                             }
                                                                             $html .= '</ul>'."\n";
                                                                             $html .= '</div>'."\n";
                                                                         }
                                                                     }*/
                                 /**
                                  * @todo Display author's name along with it ...
                                  */
                                 /*
                                 foreach ($photos as $cache_authors)
                                     $author = new Phlickr_User($api, $cache_authors->getUserId());
                                 $author = new Phlickr_User($api, $photo->getUserId());
                                     $html .= '<div class="flickr_description"><a href="'.$author->buildUrl().'">'.$author->getName().'</a></div>'."\n";
                                 */
                                 //$html .= "</div>\n";
                                 $html .= '<div class="flickr_photo_block">' . "\n";
                                 $html .= "<div class=\"flickr_photo\"><a href=\"{$photo->buildUrl()}\"><img src=\"{$photo->buildImgUrl($size)}\" /></a></div>\n";
                                 $html .= "</div>\n";
                             }
                             break;
                         case self::DISPLAY_FEATURE_WITH_RANDOM:
                             break;
                     }
                 } else {
                     $html .= _("No Flickr content matches the request !");
                 }
             } catch (Phlickr_ConnectionException $e) {
                 $html .= sprintf(_("Unable to connect to Flickr API: %s"), "<pre>{$e}</pre>");
             } catch (Phlickr_MethodFailureException $e) {
                 $html .= _("Some of the request parameters provided to Flickr API are invalid.");
                 $html .= "<br>" . $e->getMessage();
             } catch (Phlickr_XmlParseException $e) {
                 $html .= _("Unable to parse Flickr's response.");
             } catch (Phlickr_Exception $e) {
                 $html .= _("Could not get content from Flickr : ") . $e;
             }
             // Overwrite cache if needed
             $this->writeCacheToDatabase($force_data_refresh);
         }
     } else {
         $html .= _("PEAR::Phlickr is not installed");
     }
     /* Handle hyperlink clicktrough logging */
     $html = $this->replaceHyperLinks($html);
     $this->setUserUIMainDisplayContent($html);
     return parent::getUserUI();
 }