User Tools

Site Tools


privat:typo3_piwigo

TYPO3 Piwigo Anbindung

Typoscript

piwigo.ts
includeLibs.piwigoConnect = fileadmin/templates/php/piwigo.php
lib.piwigoConnect = USER
lib.piwigoConnect {
  userFunc = user_piwigoConnect->main
  user = USERNAME
  password = PASSWORD
  url = http://neu.lacordee-bildarchiv.de/
  catDefault = 0
  catCount = 1
  imageCount = 7
}

PHP Script für Piwigo API

piwigo.php
<?php
require_once(PATH_tslib.'class.tslib_pibase.php');
 
class user_piwigoConnect {
 
	var $cObj; // BACK REFS UPPER cObj
 
	/**
	 * CALL FUNCTION
	 */
 
	function main($content,$conf){
		// SET $CONF AS VARIABLE
		$user = $conf['user'];
		$password = $conf['password'];
		$siteUrl = $conf['url'];
		$catDefault = $conf['catDefault'];
		$browserCatCount = $conf['catCount'];
		$browserImageCount = $conf['imageCount'];
		$browserCatCount = $conf['catCount'];
 
		// LOGIN TO PIWIGO
		$url= $siteUrl . "ws.php?format=json";
		$postdata = "method=pwg.session.login&username=" . $user . "&password=" . $password;
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
		curl_setopt($ch, CURLOPT_REFERER, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
		curl_setopt($ch, CURLOPT_POST, 1);
		$resultLogin = curl_exec($ch);
 
		preg_match_all('|Set-Cookie: (.*);|U', $resultLogin, $results);
		$cookies = $results[1][2];
 
		// GET URL PARAMS
		$get_vars = t3lib_div::_GET();
 
		// SET CURRENT CATEGORY
		if(isset($get_vars['piwigoCat'])) {
			$currentCat = $get_vars['piwigoCat'];
		} else {
			$currentCat = $catDefault;
		}
 
		if(isset($get_vars['imagePage'])) {
			$imagePage = $get_vars['imagePage'];
		} else {
			$imagePage = '0';
		}
 
		if(isset($get_vars['catPage'])) {
			$catPage = $get_vars['catPage'];
		} else {
			$catPage = '0';
		}
 
		// INCLUDE DYNAMIC JS
		$script .= "
		<script type='text/javascript'>
		</script>
		";
 
		// GETCATGEGORIES
		$ch = curl_init($siteUrl . "ws.php?format=json&method=pwg.categories.getList&cat_id=" . $currentCat);
		curl_setopt($ch, CURLOPT_COOKIE, $cookies);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$getCat = curl_exec($ch);
 
		$catsJson = json_decode($getCat, true);
 
		if(!empty($catsJson['result']['categories']['1']['id'])) {
			$categories = '<h2>' . $catsJson['result']['categories']['0']['name'] . '</h2>';
		}
 
		foreach ($catsJson['result']['categories'] as $item) {
 
			$id = $item['id'];
			$catName = $item['name'];
			$catComment = $item['comment'];
			$catImageCount = $item['total_nb_images'];
			$catSubCount = $item['nb_categories'];
			$catImage = $item['tn_url'];
 
			// HIDE IMAGE IF EMPTY
			if(!empty($catImage)) {
				$catImage = '<img src="' . $catImage . '"/>';
			} else {
 
			}
 
			// HIDE COMMENT IF EMPTY
			if($item['comment'] == '') {
				$comment = '';
			} else {
				$comment = '<br>' . $item['comment'];
			}
 
			$catCount = '';
 
			// SHOW IMAGE COUNT
			if($catImageCount == 0 && $catSubCount == 0) {
 
			} elseif($catImageCount > 1) {
				$catCount .=  $catImageCount . ' Fotos';
			} else {
				$catCount .=  $catImageCount . ' Foto';
			}
 
			// SHOW SUB CATEGORIES COUNT
			if($catSubCount == 0) {
 
			} elseif($catSubCount > 1) {
				$catCount .= ' in ';
				$catCount .= $catSubCount . ' Unteralben';
			} else {
				$catCount .= ' in ';
				$catCount .= $catSubCount . ' Unteralbum';
			}
 
			// HIDE CURRENT CATEGORY
			if($id == $currentCat || empty($catImage)) {
				$thisTotalImages = $catImageCount;
				$thisCatName = $catName;
			} else {
				$categories .= $this->cObj->typolink($catImage . '<div>' . '<h3>' . $catName . '</h3>' . $catCount . '</div>',
					array( // parameter
						'parameter' => 52,
						'additionalParams' => '&piwigoCat=' . $id,
						'useCacheHash' => 0
					)
				);
			}
		}
 
		// GET IMAGES
		$ch = curl_init($siteUrl . "ws.php?format=json&method=pwg.categories.getImages&cat_id=" . $currentCat .'&per_page=' . $browserImageCount . '&page=' . $imagePage);
		curl_setopt($ch, CURLOPT_COOKIE, $cookies);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$getImages = curl_exec($ch);
		$imagesJson = json_decode($getImages, true);
 
		if($currentCat == '0') {
 
		} else {
			if($imagesJson['result']['images']['count'] == 0) {
 
			} else {
 
				$images = '<h2>' . $thisCatName . '</h2>';
				foreach ($imagesJson['result']['images']['_content'] as $item) {
 
					$image = $item['derivatives']['square']['url'];
					$width = $item['derivatives']['square']['width'];
					$height = $item['derivatives']['square']['height'];
					$imageHd = $item['derivatives']['xlarge']['url'];
					$imageHdWidth = $item['derivatives']['xlarge']['width'] + 20;
					$imageHdHeight = $item['derivatives']['xlarge']['height'] + 20;
					$imageCat = $item['categories']['id'];
 
					if($item['comment'] == null) {
						$comment = '';
					} else {
						$comment = '<br>' . $item['comment'];
					}
 
					$images .= '<div class="image" style="width: ' . $width . 'px">';
					$images .= '<a title="<b>' . $item['name'] . '</b>' . $comment . '" href="' . $imageHd . '" rel="shadowbox[piwigo];width=' . $imageHdWidth . ';height=' . $imageHdHeight . '"><img src="' . $image . '" width="' . $width . '" height="' . $height . '"/></a>';
					$images .= '<div class="caption">' . $item['name'] . '</div>';
					$images .= '</div>';
				}
 
				$pagesTotal = ceil($thisTotalImages / $browserImageCount);
 
				$backId = $imagePage - 1;
				if($imagePage == 0) {
					$back = '';
				} else {
					$back .= $this->cObj->typolink('BACK',
						array( // parameter
							'parameter' => 52,
							'additionalParams' => '&piwigoCat=' . $id . '&imagePage=' . $backId,
							'useCacheHash' => 0
						)
					);
 
				}
 
				$nextId = $imagePage + 1;
				if($pagesTotal == $nextId) {
					$next = '';
				} else {
					$next .= $this->cObj->typolink('NEXT',
						array( // parameter
							'parameter' => 52,
							'additionalParams' => '&piwigoCat=' . $id . '&imagePage=' . $nextId,
							'useCacheHash' => 0
						)
					);
				}
 
				for ($i=0; $i<(int)$pagesTotal; $i++) {
					$pageNumber = $i+1;
					$currentPage = $imagePage + 1;
					if($pageNumber == $currentPage) {
						$imagePages .= $pageNumber;
					} else {
 
						$imagePages .= $this->cObj->typolink($pageNumber,
							array( // parameter
								'parameter' => 52,
								'additionalParams' => '&piwigoCat=' . $id . '&imagePage=' . $i,
								'useCacheHash' => 0
							)
						);
					}
				}
 
				$imageBrowser = '<div id="imageBrowser">' .  $back . $imagePages . $next . '</div>';
			}
		}
 
		$content .=  $link . '<div id="piwigoImages">' . $images . $imageBrowser . '</div>' . '<div id="piwigoCategories">' . $categories . $catBrowser . '</div>';
		return $content;
	}
}
?>
privat/typo3_piwigo.txt · Last modified: 2013/09/11 11:25 by admin