* @copyright 2005 by http://wombat.exit0.net * @package wombatModule * @subpackage Gallery */ /** * Display picture(s) from a gallery * * @version 1.2.0 * @package wombatModule * @subpackage Gallery */ class wbModule_Gallery extends wbModule { /** * galler parameter * * tmpldir * gallery: either the name of the gallery or "_frompage" * mode: either album or picture * picture: picture id if mode = picture (can also be first,last,random or randomsession) * sizealbum: thumbnail size * sizepicture: view size for single pictures * * @access private * @var array $_params */ var $_params = array( 'tmpldir' => 'module/Gallery', 'gallery' => null, 'mode' => 'album', 'albummode' => 'table', 'picture' => 'first', 'sizealbum' => 'thumb', 'sizepicture' => 'web', ); /** * request defaults * * action: can be view * * @access private * @var array $_params */ var $_requestDefaults = array( 'action' => '_no_action_set', 'goto' => 'current', 'id' => null, 'gallery' => null, ); /** * gallery entry * @var array $_gallery */ var $_gallery = array(); /** * image size * @var string $_imageSize */ var $_imageSize = 'thumb'; /** * wbImage object * @var array $_im */ var $_im; /** * wbDatasource object * @var array $_ds */ var $_ds; /** * patSession object * @var array $_sess */ var $_sess; /** * create classes * * @access public * @return boolean $result true on success */ function __construct() { $this->_tmpl =& wbFactory::singleton( 'patTemplate' ); $this->_sess =& wbFactory::singleton( 'patSession' ); $this->_im =& wbFactory::singleton( 'wbImage' ); $this->_ds =& wbFactory::singleton( 'wbDatasource' ); $this->_ds->setCallback( $this ); } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbModule_Gallery() { $this->__construct(); } /** * recieve image data binary * * @return string $html */ function showImage( $gallery, $id, $size = 'web' ) { $this->_initGalleryConfig( $gallery ); $this->_imageSize = 'web'; if( empty( $gallery ) || empty( $id ) ) { return false; } // get picture data wbDebugger::addMsg( 'Module:Gallery', 'Load image "' . $id . '" of gallery "'. $gallery .'"', 'Picture' ); if( $this->_sess->has( 'wbModule_Gallery_pic_' . $gallery . '_' . $id ) ) { $pic = $this->_sess->get( 'wbModule_Gallery_pic_' . $gallery . '_' . $id ); } else { $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); $pic = $this->_ds->getEntry( 'gallery_img', $id, $this->_gallery['id'] ); if( patErrorManager::isError( $pic ) ) { return $pic; } $this->_sess->set( 'wbModule_Gallery_pic_' . $gallery . '_' . $id, $pic ); } header( 'Location: ' . $pic['img_src'] ); /* header( 'Content-Type: ' . $pic['img_mime'] ); echo file_get_contents( $pic['img_file'] ); */ return true; } /** * recieve content * * @return string $html */ function getHtml() { // try to load gallery info from session $gallery = $this->_params['gallery']; if( !$gallery ) { $gallery = $this->_request['gallery']; } // select gallery from page-name if( $gallery == '_frompage' ) { $site =& wbFactory::singleton( 'wbSite' ); $page = $site->getPageInfo(); $gallery = $page['name']; } $this->_initGalleryConfig( $gallery ); // no matching gallery found (happen in "_frompage") if( $this->_gallery == 0 ) { $this->_loadTemplates( 'empty' ); return $this->_tmpl->getParsedTemplate( 'wbModule_Gallery' ); } // the gallery is now selected $this->_tmpl->addGlobalVars( $this->_gallery, 'GALLERY_' ); $this->_im->setBaseDir( 'gal/' . $this->_gallery['gallery'] ); $mode = $this->_params['mode']; $function = '_processMode' . ucfirst( $mode ); if( !method_exists( $this, $function ) ) { $function = '_processModePicture'; } $this->$function(); return $this->_tmpl->getParsedTemplate( 'wbModule_Gallery' ); } /** * recieve content * * @return string $html */ function _processModePicture() { $id = $this->_params['picture']; $this->_imageSize = $this->_params['sizepicture']; // use session for caching $pic = null; $useSess = false; if( !is_numeric( $id ) ) { $useSess = true; if( $id == 'randomsession' ) { $id = 'random'; } else if( $id == 'random' ) { $useSess = false; } // try to load picture details from session if( $useSess ) { $pic = $this->_sess->get( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_' . $id ); } } // load picture details from database if( empty( $pic ) ) { $pic = $this->_ds->getEntry( 'gallery_img', $id, $this->_gallery['id'] ); if( patErrorManager::isError( $pic ) ) { return $pic; } wbDebugger::addMsg( 'Module:Gallery', 'Load image "' . $id . '" of gallery "'. $this->_gallery['gallery'] .'"', 'Picture' ); if( $useSess ) { $this->_sess->set( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_' . $id, $pic ); } } $this->_loadTemplates( 'picture' ); $this->_tmpl->addVars( 'wbModule_Gallery', $pic ); return true; } /** * recieve content * * @return string $html */ function _processModeAlbum() { $this->_imageSize = $this->_params['sizealbum']; $id = $this->_request['id']; // view single picture? if( $this->_request['action'] == 'view' && strlen( $id ) ) { $this->_imageSize = $this->_params['sizepicture']; $pic = $this->_loadPictureData( $id ); // load list of image ids if( $this->_sess->has( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_ids' ) ) { $ids = $this->_sess->get( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_ids' ); } else { $ids = $this->_ds->getIds( 'gallery_img', $this->_gallery['id'] ); $ids = array_flip( $ids ); $this->_sess->set( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_ids', $ids ); } // find neighbours $neigh = array( 'prev' => 'no', 'next' => 'no', 'next_id' => false, 'prev_id' => false ); $prev = false; $next = false; if( $ids[$id] > 0 ) { $prev = $ids[$id] - 1; } if( $ids[$id] < ( count( $ids ) - 1 ) ) { $next = $ids[$id] + 1; } $ids = array_flip( $ids ); if( $prev !== false ) { $neigh['prev'] = 'yes'; $neigh['prev_id'] = $ids[$prev]; } if( $next !== false ) { $neigh['next'] = 'yes'; $neigh['next_id'] = $ids[$next]; } $this->_loadTemplates( 'album_view' ); $this->_tmpl->addVars( 'wbModule_Gallery', $pic ); $this->_tmpl->addVars( 'wbModule_Gallery', $neigh, 'gallery_' ); return true; } wbDebugger::addMsg( 'Module:Gallery', 'Load images of gallery "'. $this->_gallery['gallery'] .'"', 'Album' ); // pager $pager = $this->_ds->getPager( 'gallery_img', $this->_request['goto'], $this->_gallery['id'] ); if( patErrorManager::isError( $pager ) ) { return $pager; } $this->_tmpl->addGlobalVars( $pager, 'PAGER_' ); // no images? if( !$pager['total'] ) { $this->_loadTemplates( 'empty' ); wbDebugger::addMsg( 'Module:Gallery', 'Empty gallery "'. $this->_gallery['gallery'] .'"', 'Album' ); return true; } // actual entries $offset = $pager['offset'] * $pager['limit']; $pics = $this->_ds->getEntries( 'gallery_img', $this->_gallery['id'], null, $offset ); if( patErrorManager::isError( $pics ) ) { return $pics; } if( $this->_params['albummode'] == 'table' ) { $album = $this->_pics2table( $pics ); } else { $album = $this->_pics2list( $pics ); } $this->_loadTemplates( 'album' ); $this->_tmpl->addVar( 'wbModule_Gallery', 'album', $album ); return true; } /** * insert pictures in a list * * @access public * @param array $list * @return boolean true */ function _pics2list( &$pics ) { $this->_loadTemplates( 'album_list' ); $this->_tmpl->addRows( 'wbModule_Gallery_list_entry', $pics ); return $this->_tmpl->getParsedTemplate( 'wbModule_Gallery_list' ); } /** * load gallery configuration * * @access private * @param string $gallery * @return boolean true */ function _initGalleryConfig( $gallery ) { // no gallery specified! if( empty( $gallery ) ) { return patErrorManager::raiseWarning( 'wbModule:Gallery:1', 'No gallery specified!', 'Gallery must be specified either in parameter or request!' ); } $this->_gallery = $this->_sess->get( 'wbModule_Gallery_gallery_' . $gallery ); // load gallery information from database and cache it into session if( empty( $this->_gallery ) ) { $clause = array( array( 'field' => 'gallery', 'value' => $gallery ) ); $this->_gallery = $this->_ds->getEntry( 'gallery', null, null, $clause ); if( patErrorManager::isError( $this->_gallery ) ) { return $this->_gallery; } $this->_sess->set( 'wbModule_Gallery_gallery_' . $gallery, $this->_gallery ); } return true; } /** * load gallery configuration * * @access private * @param string $gallery * @return boolean true */ function _loadPictureData( $id ) { // get picture data wbDebugger::addMsg( 'Module:Gallery', 'Load image "' . $id . '" of gallery "'. $this->_gallery['gallery'] .'"', 'Picture' ); if( $this->_sess->has( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_' . $id ) ) { $pic = $this->_sess->get( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_' . $id ); } else { $pic = $this->_ds->getEntry( 'gallery_img', $id, $this->_gallery['id'] ); if( patErrorManager::isError( $pic ) ) { return $pic; } $this->_sess->set( 'wbModule_Gallery_pic_' . $this->_gallery['gallery'] . '_' . $id, $pic ); } return $pic; } /** * insert pictures in a table * * @access public * @param array $list * @return boolean true */ function _pics2table( &$pics ) { $this->_loadTemplates( 'album_table' ); $cols = 4; if( $this->_tmpl->exists( 'wbModule_Gallery_table_cols' ) ) { $cols = $this->_tmpl->getParsedTemplate( 'wbModule_Gallery_table_cols' ); if( !is_numeric( $cols ) ) { return patErrorManager::raiseWarning( 'wbModule:Gallery:2', 'Cannot find number of columns!', 'You must specify the number of columns in template: "wbModule_Gallery_table_cols"' ); } } $i = 0; $row = array(); for(; $i < count( $pics ); ++$i ) { array_push( $row, $pics[$i] ); if( !( ($i +1) % $cols) ) { $this->_tmpl->addRows( 'wbModule_Gallery_table_entry', $row ); $this->_tmpl->parseTemplate( 'wbModule_Gallery_table_row', 'a' ); $row = array(); $this->_tmpl->clearTemplate( 'wbModule_Gallery_table_entry' ); } } // fill last row if( !empty( $row ) ) { $fill = $cols - ( $i % $cols ); while( $fill ) { --$fill; array_push( $row, array() ); } $this->_tmpl->addRows( 'wbModule_Gallery_table_entry', $row ); $this->_tmpl->parseTemplate( 'wbModule_Gallery_table_row', 'a' ); } return $this->_tmpl->getParsedTemplate( 'wbModule_Gallery_table' ); } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentries( $table, &$data, $id ) { $data['id'] = $id; if( $table != 'gallery_img' ) { return true; } $info = $this->_im->getInfo( $data['file'], $this->_imageSize ); foreach( $info as $key => $value ) { $data['img_' . $key] = $value; } return true; } /** * callback function * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForGetentry( $table, &$data, $id ) { $data['id'] = $id; if( $table != 'gallery_img' ) { return true; } $info = $this->_im->getInfo( $data['file'], $this->_imageSize ); foreach( $info as $key => $value ) { $data['img_' . $key] = $value; } return true; } /** * callback function - this won't be used at all * * @access public * @param string $table * @param array $data * @param int $id * @return boolean true */ function callForSave( $table, &$data, $id ) { return true; } } ?>