Location: PHPKode > scripts > phpFlickrSynch > phpflickrsynch/_classes/class_FlickrToPortfolio.php
<?php
	include 'class_Msg.php';
	
	class FlickrToPortfolio {
	
		private $f = NULL;
		private $por_id = NULL;
	
		/**
		  * Constructor: init the connection to Flickr
		  */
		public function __construct() {
			$this->f = new phpFlickr(PHPFLICKRSYNCH_API_KEY, PHPFLICKRSYNCH_SECRET);
			$this->f->setToken(PHPFLICKRSYNCH_TOKEN);			
		}
		
		/**
		  * Returns a list of all photo albums available in the Flickr user account
		  */
		public function getAllFlickrAlbums() {
			$tab = $this->f->photosets_getList();
			$res = $tab['photoset'];			
			return $res;
		}

		/**
		  * Returns the last Photo album ID created in the local DB (set by "photoalbum_register" or "photoalbum_update" method)
		  */
		public function getLastCreatedPhotoAlbumId() {
			return $this->por_id;
		}

		/**
		  * Register a new photo album and photos belonging to it
		  * @param $flickrid ID of the photo album on Flickr
		  * @return a Msg object
		  */
		public function photoalbum_register($flickrid) {	
			$MSGS = new Msg();
			$DBPORTFOLIO = new TableFlickrPortfolio();
						
			// We check the photo album already exists in the DB.
			$por_id = $DBPORTFOLIO->albumAlreadyExists($flickrid);			
			if ($por_id == 0) {						
				// Doesn't exist yet - Registering album
				$MSGS->add('Portfolio is not yet registered in the database');
				
				$infos = $this->f->photosets_getInfo($flickrid);
								
				$por_id = $DBPORTFOLIO->insertAlbum($flickrid, $infos['title'], $infos['description']);
				$MSGS->add('Portfolio created in the database with POR_ID=' . $por_id);
				
				$this->por_id = $por_id;
			} else {
				$MSGS->add('Portfolio already exists in the DB with POR_ID=' . $por_id);
			}
			
			return $MSGS; 
		}

		/**
		  * Resynch the attributes of a photo album btw Flickr and Local database - doesn't resynch photos belonging to the photo album!!
		  * @param $flickrid ID of the photo album on Flickr
		  * @return a Msg object		  
		  */
		public function photoalbum_update($flickrid) {
			$MSGS = new Msg();
			$DBPORTFOLIO = new TableFlickrPortfolio();

			// We check the photo album already exists in the DB.
			$por_id = $DBPORTFOLIO->albumAlreadyExists($flickrid);												
			if ($por_id > 0) {
				$MSGS->add('Portfolio already exists in the DB with POR_ID=' . $por_id);
			
				$infos = $this->f->photosets_getInfo($flickrid);
			
				$DBPORTFOLIO->updateAlbum($por_id, $infos['title'], $infos['description']);
				$MSGS->add('Title and description updated');
				
				$this->por_id = $por_id;
			}
			return $MSGS;
		}

		/**
		  * Retrieves the Flickr IDs of all the photos belonging to the given photo album
		  * @param $flickrid FlickrID of a photo album
		  * @return An array of FlickrID
		  */
		public function photoalbum_getPhotos($flickrid) {
			$photoFlickrIDs = array();
			$tab = $this->f->photosets_getPhotos($flickrid);
			foreach ($tab['photoset']['photo'] as $key => $val) {
				$photoFlickrIDs[] = $val['id'];
			}
			return $photoFlickrIDs;
		}
	
		/**
		  * Retrieves EXIF value that we want to store in the local DB
		  * @param $flickrid FlickrID of the photo
		  */
		private function photo_getExifValues($flickrid) {
			$exif = $this->f->photos_getExif($flickrid);
			$exiffiltered = array();
			$exifvalues = array('Model', 'Exposure', 'Aperture', 'ISO Speed', 'Date and Time (Original)', 'Focal Length');
			foreach ($exif['exif'] as $key => $val) {
				if (in_array($val['label'], $exifvalues)) {
					$value = isset($val['clean']) ? $val['clean'] :  $val['raw'];
					$exiffiltered[$val['label']] = $value;
				}
			}
			return $exiffiltered;	
		}
		
		private function photo_getSizes($flickrid) {
			$sizes = $this->f->photos_getSizes($flickrid);
			$sz = array();
			foreach ($sizes as $key => $val) {
				$sz[$val['label']] = $val['width'] . ';' . $val['height'];
			}
			return $sz;
		}
		
		private function photo_getTags($infos) {
			$tags = $infos['tags']['tag'];
			$tags2 = array();
			foreach ($infos['tags']['tag'] as $key => $val) {
				$tags2[$val['_content']] = $val['raw'];				
			}
			return $tags2;		
		}
	
	
		public function resynchPhoto($flickrid, $por_id) {
			// Retrieves photo data from Flickr
			$infos = $this->f->photos_getInfo($flickrid);
						
			// Retrieves EXIF value that we want to store in the local DB
			$exifs = $this->photo_getExifValues($flickrid);

			// Retrieves width/height for each size
			$sizes = $this->photo_getSizes($flickrid);

			// Retrieves all the data used to build Flickr URLs
			$urlinfo = array(
				'secret' => $infos['secret'],
				'server' => $infos['server'],
				'farm' => $infos['farm'],
				'originalsecret' => $infos['originalsecret'],
				'originalformat' => $infos['originalformat'],
				'flickr_url' => $infos['urls']['url'][0]['_content']
			);
		
									
			$latitude = isset($infos['location']) ? $infos['location']['latitude'] : '';
			$longitude = isset($infos['location']) ? $infos['location']['longitude'] : '';
			$woeid = isset($infos['location']) ? $infos['location']['woeid'] : '';
			$title = $infos['title'];						
			$description = $infos['description'];

			$DBPHOTO = new TableFlickrPhoto();
			// Checks if the photo already exists in the local DB
			$img_id = $DBPHOTO->photoAlreadyExists($flickrid);
			
			if ($img_id == 0) {
				$img_id = $DBPHOTO->insertPhoto($flickrid, $por_id);
			}
									
			$DBPHOTO->updatePhoto($img_id, $title, $description, $longitude, $latitude , $woeid, $urlinfo, $exifs, $sizes);	

			$tags = $this->photo_getTags($infos);
			$DBTAG = new TableFlickrTag();			
			$DBTAG->registerTags($img_id, $tags);										
		}

		/**
		  * Delete a photo album registered in the local database
		  * @param $flickrid ID of the photo album on Flickr
		  * @return a Msg object
		  */			
		public function unregisterAlbum($flickrid) {
			$MSGS = new Msg();
			$DBPORTFOLIO = new TableFlickrPortfolio();
			$por_id = $DBPORTFOLIO->albumAlreadyExists($flickrid);
			
			if ($por_id > 0) {
				$MSGS->add('Portfolio already exists in the DB with POR_ID=' . $por_id);
				$DBPORTFOLIO->deleteAlbum($por_id);
				$MSGS->add('Portfolio deleted');
			} else {
				$MSGS->add('Portfolio is not yet registered in the database. Stop there');
			}			
			return $MSGS;
		}	
	}	
?>
Return current item: phpFlickrSynch