Location: PHPKode > projects > Gallery2flickr > Gallery2Flickr/FlickrSearch.inc
<?php
/*
 * Gallery2Flickr
 *
 * A bridge between your gallery2 installation and flickr.com
 *
 * File:       FlickrSearch.inc
 *
 *             Import a search result from flickr.
 *
 * Copyright:
 *             (c) 2006 - 2007 hide@address.com
 *             Distributed under the terms of the GNU General Public License v2
 *
 * Author(s):
 *             Gunnar Wrobel <hide@address.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * (Gallery2Flickr is a module for Gallery2 - a web based photo album
 *  viewer and editor - Copyright for Gallery2: (C) 2000-2006 Bharat
 *  Mediratta)
 *

 */
/**
 * @package Gallery2Flickr
 * @subpackage UserInterface
 * @version $Id: FlickrSearch.inc 10 2008-01-18 09:39:31Z luciferc $
 * @author Gunnar Wrobel <hide@address.com>
 */

/**
 * @package Gallery2Flickr
 * @subpackage UserInterface
 */
class FlickrSearchController extends GalleryController {
    /**
     * @see GalleryController::handleRequest()
     */
    function handleRequest($form) {
	global $gallery;

	$results['delegate']['view'] = 'Gallery2Flickr.FlickrSearch';
        $results['status'] = array();
        $results['error'] = array();
        return array(null, $results);
    }
}

/**
 * @package Flickr
 * @subpackage UserInterface
 *
 */
class FlickrSearchView extends GalleryView {

    /**
     * @see GalleryView::loadTemplate
     */
    function loadTemplate(&$template, &$form) {
	/* Load our item */
	list ($ret, $item) = $this->_getItem();
	if ($ret) {
	    return array($ret, null);
	}

	$itemId = GalleryUtilities::getRequestVariables('itemId');

	GalleryCoreApi::requireOnce('modules/Gallery2Flickr/classes/Gallery2Flickr.class.php');
	$f2g = new FlickrToGallery();

	$Flickr = array();

	if (isset($form['action']['search']) 
	    || isset($form['action']['nextPage'])
	    || isset($form['action']['previousPage'])
	    || isset($form['action']['import'])
	    || isset($form['action']['importresults'])
	    || isset($form['action']['importpage'])) {

	  $search = array();

	  if (isset($form['page']) && $form['page']) {
	    $page = (int)$form['page'];
	  } else {
	    $page = 1;
	  }

	  if (isset($form['action']['nextPage'])) {
	    $page++;
	  }

	  if (isset($form['action']['previousPage'])) {
	    $page--;
	  }

	  if (isset($form['action']['import'])) {

	    $pid_keys = array_keys($form['action']['import']);
	    $pid = $pid_keys[0];

	    GalleryCoreApi::requireOnce('modules/Gallery2Flickr/classes/Gallery2Flickr.class.php');
	    $f2g = new FlickrToGallery();

	    if ($f2g->import_image($pid, $itemId)) {
	      /* Send the user to the set import page */
	      $Flickr['imported'] = 1;
	    } else {
	      $Flickr['importerror'] = 1;
	    }
	  }

	  $search['page'] = (string)$page;
	  $search['per_page'] = '20';

	  if (isset($form['user']) && $form['user']) {
	    $uid = $f2g->flickr->people_findByUsername($form['user']);
	    if ($uid && isset($uid['nsid'])) {
	      $Flickr['uid'] = $f2g->flickr->people_getInfo($uid['nsid']);
	      $search['user_id'] = $uid['nsid'];
	    }
	  }

	  if (isset($form['tags']) && $form['tags']) {
	    $search['tags'] = $form['tags'];
	  }

	  if (isset($form['matchtags']) && $form['matchtags']) {
	    $search['tag_mode'] = $form['matchtags'];
	  }

	  if (isset($form['text']) && $form['text']) {
	    $search['text'] = $form['text'];
	  }

	  if (isset($form['license']) && $form['license']) {
	    $search['license'] = implode(',',array_keys($form['license']));
	  }

	  $result = $f2g->flickr->photos_search($search);
	  
	  if (isset($form['minpix']) && $form['minpix']) {
	    $minpix = (int)$form['minpix'];

	    $sized_result = array();

            if (isset($result['photo'])) {
              foreach ($result['photo'] as $photo) {
                $sizes = $f2g->flickr->photos_getSizes($photo['id']);
		foreach ($sizes as $size) {
		  if ($size['label'] = 'Original') {
                    $mpix = (int)$size['width'] * (int)$size['height'];
                    if ($mpix >= $minpix) {
                      $sized_result[] = $photo;
                    }
                  }
                }
              }

	      $result['photo'] = $sized_result;
	    }
	  }

	  if ($result) {

	    $Flickr['pages']['current'] = $page;

	    if (isset($result['pages'])) {
	      $Flickr['pages']['pages'] = $result['pages'];
	    }

	    if (isset($result['perpage'])) {
	      $per_page = (int)$result['perpage'];
	      $start = ($page - 1) * $per_page + 1;
	      $end   = $page * $per_page;
	      $Flickr['pages']['start'] = $start;
	      $Flickr['pages']['end']   = $end;
	    }

	    if (isset($result['total'])) {
	      $Flickr['pages']['total'] = $result['total'];
	    }

	    for ($i = 0; $i < count($result['photo']); $i++) {
	      $result['photo'][$i]['thumb'] = $f2g->flickr->buildPhotoURL($result['photo'][$i], 'thumbnail');
	      $result['photo'][$i]['oid'] = $f2g->flickr->people_getInfo($result['photo'][$i]['owner']);
	    }
	    
	    if (isset($form['action']['importpage'])) {

	      GalleryCoreApi::requireOnce('modules/Gallery2Flickr/classes/Gallery2Flickr.class.php');
	      $f2g = new FlickrToGallery();

	      foreach ($result['photo'] as $photo) {
		if (!$f2g->import_image($photo['id'], $itemId)) {
		  $Flickr['importerror'] = 1;
		  break;
		}
	      }
	      if (empty($Flickr['importerror'])) {
		$Flickr['imported'] = 1;
	      }
	    }

	    if (isset($form['action']['importresults'])) {

	      GalleryCoreApi::requireOnce('modules/Gallery2Flickr/classes/Gallery2Flickr.class.php');
	      $f2g = new FlickrToGallery();

	      for ($i = 1; $i <= $Flickr['pages']['pages']; $i++) {
		$search['page'] = (string)$i;
		$photo_page = $f2g->flickr->photos_search($search);
		foreach ($photo_page['photo'] as $photo) {
		  if (!$f2g->import_image($photo['id'], $itemId)) {
		    $Flickr['importerror'] = 1;
		    break;
		  }
		}
	      }
	      if (empty($Flickr['importerror'])) {
		$Flickr['imported'] = 1;
	      }
	    }

	    $Flickr['result'] = $result;
	  }
	}

	$Flickr['item'] = (array)$item;

	$template->setVariable('Flickr', $Flickr);

	return array(null, array('body' => 'modules/Gallery2Flickr/templates/FlickrSearch.tpl'));
    }
}
?>
Return current item: Gallery2flickr