fisheye
[ class tree: fisheye ] [ index: fisheye ] [ all elements ]

Source for file FisheyeGallery.php

Documentation is available at FisheyeGallery.php

  1. <?php
  2. /**
  3.  * @package fisheye
  4.  */
  5.  
  6. /**
  7.  * required setup
  8.  */
  9. require_once( FISHEYE_PKG_PATH.'FisheyeImage.php' );        // A gallery is composed of FisheyeImages
  10.  
  11. define('FISHEYEGALLERY_CONTENT_TYPE_GUID', 'fisheyegallery' );
  12.  
  13. define( 'FISHEYE_PAGINATION_FIXED_GRID', 'fixed_grid' );
  14. define( 'FISHEYE_PAGINATION_AUTO_FLOW', 'auto_flow' );
  15. define( 'FISHEYE_PAGINATION_POSITION_NUMBER', 'position_number' );
  16. define( 'FISHEYE_PAGINATION_SIMPLE_LIST', 'simple_list' );
  17. define( 'FISHEYE_PAGINATION_MATTEO', 'matteo' );
  18. define( 'FISHEYE_PAGINATION_GALLERIFFIC', 'galleriffic' );
  19.  
  20. /**
  21.  * FisheyeBase extends LibertyMime, which this class doesn't need, but we need a common base class
  22.  *
  23.  * @package fisheye
  24.  */
  25. class FisheyeGallery extends FisheyeBase {
  26.     var $mGalleryId;        // fisheye_gallery.gallery_id
  27.     var $mItems;            // Array of FisheyeImage class instances which belong to this gallery
  28.  
  29.     function __construct($pGalleryId = NULL, $pContentId = NULL) {
  30.         parent::__construct();
  31.         $this->mGalleryId = (int)$pGalleryId;        // Set member variables according to the parameters we were passed
  32.         $this->mContentId = (int)$pContentId;        // liberty_content.content_id which this gallery references
  33.         $this->mItems = array();                    // Assume no images (if $pAutoLoad is TRUE we will populate this array later)
  34.         $this->mAdminContentPerm = 'p_fisheye_admin';
  35.  
  36.         // This registers the content type for FishEye galleries
  37.         // FYI: Any class which uses a table which inherits from liberty_content should create their own content type(s)
  38.         $this->registerContentType(
  39.             FISHEYEGALLERY_CONTENT_TYPE_GUID, array( 'content_type_guid' => FISHEYEGALLERY_CONTENT_TYPE_GUID,
  40.                 'content_name' => 'Image Gallery',
  41.                 'content_name_plural' => 'Image Galleries',
  42.                 'handler_class' => 'FisheyeGallery',
  43.                 'handler_package' => 'fisheye',
  44.                 'handler_file' => 'FisheyeGallery.php',
  45.                 'maintainer_url' => 'http://www.bitweaver.org'
  46.         ));
  47.  
  48.         // Permission setup
  49.         $this->mViewContentPerm  = 'p_fisheye_view';
  50.         $this->mCreateContentPerm  = 'p_fisheye_create';
  51.         $this->mUpdateContentPerm  = 'p_fisheye_update';
  52.         $this->mAdminContentPerm = 'p_fisheye_admin';
  53.     }
  54.  
  55.     function isValid() {
  56.         return( @$this->verifyId( $this->mGalleryId ) || @$this->verifyId( $this->mContentId ) );
  57.     }
  58.  
  59.     public static function lookup( $pLookupHash ) {
  60.         global $gBitDb;
  61.         $ret = NULL;
  62.  
  63.         $lookupContentId = NULL;
  64.         if (!empty($pLookupHash['gallery_id']) && is_numeric($pLookupHash['gallery_id'])) {
  65.             if( $lookup = $gBitDb->getRow( "SELECT lc.`content_id`, lc.`content_type_guid` FROM `".BIT_DB_PREFIX."fisheye_gallery` fg INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON(lc.`content_id`=fg.`content_id`) WHERE `gallery_id`=?", array( $pLookupHash['gallery_id'] ) ) ) {
  66.                 $lookupContentId = $lookup['content_id'];
  67.                 $lookupContentGuid = $lookup['content_type_guid'];
  68.             }
  69.         } elseif (!empty($pLookupHash['content_id']) && is_numeric($pLookupHash['content_id'])) {
  70.             $lookupContentId = $pLookupHash['content_id'];
  71.             $lookupContentGuid = NULL;
  72.         }
  73.  
  74.         if( BitBase::verifyId( $lookupContentId ) ) {
  75.             $ret = LibertyBase::getLibertyObject( $lookupContentId, $lookupContentGuid );
  76.         }
  77.  
  78.         return $ret;
  79.     }
  80.  
  81.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  82.         global $gBitSystem;
  83.         $bindVars = array();
  84.         $selectSql = $joinSql = $whereSql = '';
  85.  
  86.         if( @$this->verifyId( $this->mGalleryId ) ) {
  87.             $whereSql = " WHERE fg.`gallery_id` = ?";
  88.             $bindVars = array( $this->mGalleryId );
  89.         } elseif ( @$this->verifyId( $this->mContentId ) ) {
  90.             $whereSql = " WHERE fg.`content_id` = ?";
  91.             $bindVars = array($this->mContentId);
  92.         } else {
  93.             $whereSql = NULL;
  94.         }
  95.  
  96.         if ($whereSql) {    // If we have some way to know what fisheye_gallery row to load...
  97.             $this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  98.  
  99.             $query = "SELECT fg.*, lc.* $selectSql
  100.                         , uue.`login` AS modifier_user, uue.`real_name` AS `modifier_real_name`
  101.                         , uuc.`login` AS creator_user, uuc.`real_name` AS `creator_real_name`
  102.                     FROM `".BIT_DB_PREFIX."fisheye_gallery` fg
  103.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (fg.`content_id` = lc.`content_id`) $joinSql
  104.                         LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON (uue.`user_id` = lc.`modifier_user_id`)
  105.                         LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON (uuc.`user_id` = lc.`user_id`)
  106.                     $whereSql";
  107.  
  108.             if( $rs = $this->mDb->query($query, $bindVars) ) {
  109.                 $this->mInfo = $rs->fields;
  110.                 $this->mContentId = $rs->fields['content_id'];
  111.                 LibertyContent::load();
  112.                 if( @$this->verifyId($this->mInfo['gallery_id'] ) ) {
  113.  
  114.                     $this->mGalleryId = $this->mInfo['gallery_id'];
  115.                     $this->mContentId = $this->mInfo['content_id'];
  116.  
  117.                     $this->mInfo['creator'] = (isset( $rs->fields['creator_real_name'] ) ? $rs->fields['creator_real_name'] : $rs->fields['creator_user'] );
  118.                     $this->mInfo['editor'] = (isset( $rs->fields['modifier_real_name'] ) ? $rs->fields['modifier_real_name'] : $rs->fields['modifier_user'] );
  119.  
  120.                     // Set some basic defaults for how to display a gallery if they're not already set
  121.                     if (empty($this->mInfo['thumbnail_size'])) {
  122.                         $this->mInfo['thumbnail_size'] = $this->getPreference( 'fisheye_gallery_default_thumbnail_size', NULL );
  123.                     }
  124.                     if (empty($this->mInfo['rows_per_page'])) {
  125.                         $this->mInfo['rows_per_page'] = $this->getPreference('fisheye_gallery_default_rows_per_page', FISHEYE_DEFAULT_ROWS_PER_PAGE);
  126.                     }
  127.                     if (empty($this->mInfo['cols_per_page'])) {
  128.                         $this->mInfo['cols_per_page'] = $this->getPreference('fisheye_gallery_default_cols_per_page', FISHEYE_DEFAULT_COLS_PER_PAGE);
  129.                     }
  130.                     if (empty($this->mInfo['access_answer'])) {
  131.                         $this->mInfo['access_answer'] = '';
  132.                     }
  133.                     if (  $this->getPreference( 'gallery_pagination' ) == FISHEYE_PAGINATION_GALLERIFFIC and empty($this->mInfo['galleriffic_style'])) {
  134.                         $this->mInfo['galleriffic_style'] = $this->getPreference('galleriffic_style', 1);
  135.                     }
  136.  
  137.                     $this->mInfo['num_images'] = $this->getImageCount();
  138.                     if( $this->getPreference( 'gallery_pagination' ) == FISHEYE_PAGINATION_POSITION_NUMBER ) {
  139.                         $this->mInfo['num_pages'] = $this->mDb->getOne( "SELECT COUNT( distinct( floor(`item_position`) ) ) FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` WHERE gallery_content_id=?", array( $this->mContentId ) );
  140.                     } else {
  141.                         $this->mInfo['images_per_page'] = ($this->mInfo['cols_per_page'] * $this->mInfo['rows_per_page']);
  142.                         $this->mInfo['num_pages'] = (int)($this->mInfo['num_images'] / $this->mInfo['images_per_page'] + ($this->mInfo['num_images'] % $this->mInfo['images_per_page'] == 0 ? 0 : 1));
  143.                     }
  144.  
  145.                 } else {
  146.                     unset( $this->mContentId );
  147.                     unset( $this->mGalleryId );
  148.                 }
  149.  
  150.             }
  151.         }
  152.  
  153.         return count($this->mInfo);
  154.     }
  155.  
  156.     function loadCurrentImage( $pCurrentImageId ) {
  157.         if( $this->isValid() && @$this->verifyId( $pCurrentImageId ) ) {
  158.             // this code sucks but works - XOXO spiderr
  159.             $query = "SELECT fgim.*, fi.`image_id`, lf.`file_name`, lf.`user_id`, lf.`mime_type`, la.`attachment_id`
  160.                     FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim
  161.                         INNER JOIN `".BIT_DB_PREFIX."fisheye_image` fi ON ( fi.`content_id`=fgim.`item_content_id` )
  162.                         INNER JOIN `".BIT_DB_PREFIX."liberty_attachments` la ON ( fi.`content_id`=la.`content_id` )
  163.                         INNER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON ( lf.`file_id`=la.`foreign_id` )
  164.                     WHERE fgim.`gallery_content_id` = ?
  165.                     ORDER BY fgim.`item_position`, fi.`content_id` ";
  166.             if( $rs = $this->mDb->query($query, array( $this->mContentId ) ) ) {
  167.                 $tempImage = new FisheyeImage();
  168.                 $rows = $rs->getRows();
  169.                 for( $i = 0; $i < count( $rows ); $i++ ) {
  170.                     if( $rows[$i]['image_id'] == $pCurrentImageId ) {
  171.                         if( $i > 0 ) {
  172.                             $this->mInfo['previous_image_id'] = $rows[$i-1]['image_id'];
  173.                             $this->mInfo['previous_image_avatar'] = liberty_fetch_thumbnail_url( array(
  174.                                 'file_name' => $rows[$i-1]['file_name'],
  175.                                 'source_file'    => $tempImage->getSourceFile( $rows[$i-1] ),
  176.                                 'mime_image'   => TRUE,
  177.                                 'size'         => 'avatar',
  178.                             ));
  179.                         }
  180.                         if( $i + 1  < count( $rows ) ) {
  181.                             $this->mInfo['next_image_id'] = $rows[$i+1]['image_id'];
  182.                             $this->mInfo['next_image_avatar'] = liberty_fetch_thumbnail_url( array(
  183.                                 'file_name' => $rows[$i+1]['file_name'],
  184.                                 'source_file'    => $tempImage->getSourceFile( $rows[$i+1] ),
  185.                                 'mime_image'   => TRUE,
  186.                                 'size'         => 'avatar',
  187.                             ));
  188.                         }
  189.                     }
  190.                 }
  191.             }
  192.         }
  193.     }
  194.  
  195.     function loadImages( $pPage=-1, $pImagesPerPage=-1) {
  196.         global $gLibertySystem, $gBitSystem, $gBitUser;
  197.         if( !$this->isValid() ) {
  198.             return NULL;
  199.         }
  200.         $bindVars = array($this->mContentId);
  201.         $whereSql = $selectSql = $joinSql = $orderSql = '';
  202.         $rowCount = $offset = NULL;
  203.         $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  204.  
  205.         if( $gBitSystem->isFeatureActive( 'fisheye_gallery_default_sort_mode' ) ) {
  206.             $orderSql = ", ".$this->mDb->convertSortmode( $gBitSystem->getConfig( 'fisheye_gallery_default_sort_mode' ) );
  207.         } else {
  208.             $orderSql = ", fgim.`item_content_id`";
  209.         }
  210.  
  211.         // load for just a single page
  212.         if( $pPage != -1 ) {
  213.             if( $this->getLayout() == FISHEYE_PAGINATION_POSITION_NUMBER ) {
  214.                 $query = "SELECT DISTINCT(FLOOR(`item_position`))
  215.                           FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map`
  216.                           WHERE gallery_content_id=?
  217.                           ORDER BY floor(item_position)";
  218.                 $mantissa = $this->mDb->getOne( $query, array( $this->mContentId ), 1, ($pPage - 1) );
  219.                 // gallery image order with no positions set will have NULL mantissa, and all images will be shown
  220.                 if( !is_null( $mantissa ) ) {
  221.                     $whereSql .= " AND floor(item_position)=? ";
  222.                     array_push( $bindVars, $mantissa );
  223.                 }
  224.             } elseif( $this->getLayout() == FISHEYE_PAGINATION_FIXED_GRID ) {
  225.                 $rowCount = $this->getField( 'rows_per_page' ) * $this->getField( 'cols_per_page' );
  226.                 $offset = $rowCount * ($pPage - 1);
  227.             } else {
  228.                 $rowCount = $pImagesPerPage;
  229.                 $offset = $rowCount * ($pPage - 1);
  230.             }
  231.         }
  232.  
  233.         $this->mItems = array();
  234.  
  235.         $query = "SELECT fgim.*, lc.`user_id`, lct.*, ufm.`favorite_content_id` AS is_favorite $selectSql
  236.                 FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim
  237.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`=fgim.`item_content_id` )
  238.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON ( lct.`content_type_guid`=lc.`content_type_guid` )
  239.                     $joinSql
  240.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."users_favorites_map` ufm ON ( ufm.`favorite_content_id`=lc.`content_id` AND lc.`user_id`=ufm.`user_id` )
  241.                 WHERE fgim.`gallery_content_id` = ? $whereSql
  242.                 ORDER BY fgim.`item_position` $orderSql";
  243.         $rs = $this->mDb->query($query, $bindVars, $rowCount, $offset);
  244.  
  245.         $rows = $rs->getRows();
  246.         foreach ($rows as $row) {
  247.             $pass = TRUE;
  248.             if( $gBitSystem->isPackageActive( 'gatekeeper' ) ) {
  249.                 $pass = $gBitUser->hasPermission( 'p_fisheye_admin' ) || !@$this->verifyId( $row['security_id'] ) || ( $row['user_id'] == $gBitUser->mUserId ) || @$this->verifyId( $_SESSION['gatekeeper_security'][$row['security_id']] );
  250.             }
  251.             if( $pass ) {
  252.                 $type = $gLibertySystem->mContentTypes[$row['content_type_guid']];
  253.                 require_once( constant( strtoupper( $type['handler_package'] ).'_PKG_PATH' ).$type['handler_file'] );
  254.                 $item = new $type['handler_class']( NULL, $row['item_content_id'] );
  255.                 if( is_object( $item ) && $item->load() ) {
  256.                     $item->loadThumbnail( $this->mInfo['thumbnail_size'] );
  257.                     $item->setGalleryPath( $this->mGalleryPath.'/'.$this->mGalleryId );
  258.                     $item->mInfo['item_position'] = $row['item_position'];
  259.                     $this->mItems[$row['item_content_id']] = $item;
  260.                 }
  261.             }
  262.         }
  263.  
  264.         return count( $this->mItems );
  265.     }
  266.  
  267.     function getImageList() {
  268.         global $gLibertySystem, $gBitSystem, $gBitUser;
  269.         $ret = NULL;
  270.         if( $this->isValid() ) {
  271.             $bindVars = array($this->mContentId);
  272.             $whereSql = $selectSql = $joinSql = $orderSql = '';
  273.             $rows = $offset = NULL;
  274.             $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  275.  
  276.             if( $gBitSystem->isFeatureActive( 'fisheye_gallery_default_sort_mode' ) ) {
  277.                 $orderSql = ", ".$this->mDb->convertSortmode( $gBitSystem->getConfig( 'fisheye_gallery_default_sort_mode' ) );
  278.             } else {
  279.                 $orderSql = ", fgim.`item_content_id`";
  280.             }
  281.  
  282.             $this->mItems = array();
  283.  
  284.             $query = "SELECT lc.`content_id` AS `has_key`, fgim.*, lc.*, lct.*, fi.`image_id`, ufm.`favorite_content_id` AS is_favorite $selectSql
  285.                     FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim
  286.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`=fgim.`item_content_id` )
  287.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON ( lct.`content_type_guid`=lc.`content_type_guid` )
  288.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."fisheye_image` fi ON ( fgim.`item_content_id`=fi.`content_id` )
  289.                         $joinSql
  290.                         LEFT OUTER JOIN `".BIT_DB_PREFIX."users_favorites_map` ufm ON ( ufm.`favorite_content_id`=lc.`content_id` AND lc.`user_id`=ufm.`user_id` )
  291.                     WHERE fgim.`gallery_content_id` = ? $whereSql
  292.                     ORDER BY fgim.`item_position` $orderSql";
  293.             $ret = $this->mDb->getAssoc($query, $bindVars, $rows, $offset);
  294.         }
  295.         return $ret;
  296.     }
  297.  
  298.     function exportHash( $pPaginate = FALSE ) {
  299.         if( $ret = parent::exportHash() ) {
  300.             if( $this->loadImages() ) {
  301.                 foreach( array_keys( $this->mItems ) as $key ) {
  302.                     if( $pPaginate ) {
  303.                         if( $exp = $this->mItems[$key]->exportHash( $pPaginate ) ) {
  304.                             $ret['content']['page'][$this->getItemPage($key)][] = $exp;
  305.                         }
  306.                     } else {
  307.                         $ret['content'][] = $this->mItems[$key]->exportHash( $pPaginate );
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.         return $ret;
  313.     }
  314.  
  315.     function getItemPage( $pItemContentId ) {
  316.         $ret = NULL;
  317.         if( empty( $this->mPaginationLookup ) ) {
  318.             $this->mPaginationLookup = $this->mDb->getAssoc( "SELECT `item_content_id`, floor(`item_position`) FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` WHERE `gallery_content_id`=?", array( $this->mContentId ) );
  319.         }
  320.         if( !empty( $this->mPaginationLookup[$pItemContentId] ) ) {
  321.             $ret = $this->mPaginationLookup[$pItemContentId];
  322.         }
  323.         return $ret;
  324.     }
  325.  
  326.     function getPreviewHash() {
  327.         $ret = array();
  328.         if( !empty( $this->mInfo['preview_content'] ) ) {
  329.             $ret =  $this->mInfo['preview_content']->mInfo;
  330.         }
  331.         // override  $this->mInfo['preview_content']->mInfo['display_url'] so we don't drive directly to the image
  332.         $ret['display_url'] = $this->getDisplayUrl();
  333.         return $ret;
  334.     }
  335.  
  336.     function getImageCount() {
  337.         $ret = 0;
  338.  
  339.         if ($this->mGalleryId) {
  340.             $bindVars = array($this->mContentId);
  341.             $whereSql = $selectSql = $joinSql = $orderSql = '';
  342.             $rows = $offset = NULL;
  343.             $paramHash['no_fatal'] = TRUE;
  344.             $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, NULL, $paramHash );
  345.             $query = 'SELECT COUNT(*) AS "count"
  346.                     FROM `'.BIT_DB_PREFIX."fisheye_gallery_image_map` fgim
  347.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`=fgim.`item_content_id` )
  348.                     $joinSql WHERE `gallery_content_id` = ? $whereSql";
  349.             $rs = $this->mDb->query($query, $bindVars);
  350.             $ret = $rs->fields['count'];
  351.         }
  352.         return $ret;
  353.     }
  354.  
  355.     function verifyGalleryData(&$pStorageHash) {
  356.         global $gBitSystem;
  357.  
  358.         if (empty($pStorageHash['rows_per_page'])) {
  359.             $pStorageHash['rows_per_page'] = $gBitSystem->getConfig('fisheye_gallery_default_rows_per_page', (!empty($this->mInfo['rows_per_page']) ? $this->mInfo['rows_per_page'] : FISHEYE_DEFAULT_ROWS_PER_PAGE));
  360.         }
  361.  
  362.         if (empty($pStorageHash['cols_per_page'])) {
  363.             $pStorageHash['cols_per_page'] = $gBitSystem->getConfig('fisheye_gallery_default_cols_per_page', (!empty($this->mInfo['cols_per_page']) ? $this->mInfo['cols_per_page'] : FISHEYE_DEFAULT_COLS_PER_PAGE));
  364.         }
  365.  
  366.         if (empty($pStorageHash['thumbnail_size'])) {
  367.             $pStorageHash['thumbnail_size'] = $gBitSystem->getConfig('fisheye_gallery_default_thumbnail_size', (!empty($this->mInfo['thumbnail_size']) ? $this->mInfo['thumbnail_size'] : NULL ));
  368.         }
  369.  
  370.         if (empty($pStorageHash['title'])) {
  371.             $this->mErrors[] = "You must specify a title for this image gallery";
  372.         }
  373.  
  374.         $pStorageHash['content_type_guid'] = $this->getContentType();
  375.  
  376.         return (count($this->mErrors) == 0);
  377.     }
  378.  
  379.  
  380.     function generateThumbnails() {
  381.         if( $this->isValid() ) {
  382.             if( $this->loadImages() ) {
  383.                 foreach( array_keys( $this->mItems ) as $key ) {
  384.                     $this->mItems[$key]->generateThumbnails();
  385.                 }
  386.             }
  387.         }
  388.     }
  389.  
  390.  
  391.     function getThumbnailContentId() {
  392.         if( !$this->getField( 'thumbnail_content_id' ) ) {
  393.             $this->getThumbnailImage();
  394.         }
  395.         return( $this->getField( 'thumbnail_content_id' ) );
  396.     }
  397.  
  398.     function getThumbnailUri( $pSize='small', $pInfoHash = NULL ) {
  399.         if( empty( $this->mInfo['preview_content'] ) ) {
  400.             $this->loadThumbnail();
  401.         }
  402.  
  403.         if( is_object( $this->mInfo['preview_content'] ) ) {
  404.             return $this->mInfo['preview_content']->getThumbnailUri( $pSize );
  405.         }
  406.     }
  407.  
  408.  
  409.     function getThumbnailUrl( $pSize = 'small', $pInfoHash = NULL, $pSecondaryId = NULL, $pDefault=TRUE ) {
  410.         if( empty( $this->mInfo['preview_content'] ) ) {
  411.             $this->loadThumbnail();
  412.         }
  413.  
  414.         if( is_object( $this->mInfo['preview_content'] ) ) {
  415.             return $this->mInfo['preview_content']->getThumbnailUrl( $pSize );
  416.         }
  417.     }
  418.  
  419.  
  420.     function getThumbnailImage( $pContentId=NULL, $pThumbnailContentId=NULL, $pThumbnailContentType=NULL ) {
  421.         global $gLibertySystem, $gBitUser;
  422.         $ret = NULL;
  423.  
  424.         if( !@$this->verifyId( $pContentId ) && !empty( $this->mContentId ) ) {
  425.             $pContentId = $this->mContentId;
  426.         }
  427.  
  428.         if( !@$this->verifyId( $pThumbnailContentId ) ) {
  429.             if( @$this->verifyId( $this->mInfo['preview_content_id'] ) ) {
  430.                 $pThumbnailContentId = $this->mInfo['preview_content_id'];
  431.             } else {
  432.                 if( $this->mDb->isAdvancedPostgresEnabled() ) {
  433.                     $whereSql = '';
  434.                     $bindVars = array( $pContentId );
  435.                     if( !$gBitUser->isAdmin() ) {
  436.                         $whereSql = "  AND (cgm.`security_id` IS NULL OR lc.`user_id`=?) ";
  437.                         $bindVars[] = $gBitUser->mUserId;
  438.                     }
  439.                     $query =   "SELECT COALESCE( fg.`preview_content_id`, lc.`content_id` ) AS `content_id`, lc.`content_type_guid`
  440.                                 FROM connectby('`".BIT_DB_PREFIX."fisheye_gallery_image_map`', '`item_content_id`', '`gallery_content_id`', ?, 0, '/') AS t(`cb_item_content_id` int, `cb_parent_content_id` int, `level` int, `branch` text)
  441.                                 INNER JOIN liberty_content lc ON(content_id=cb_item_content_id)
  442.                                 LEFT OUTER JOIN `".BIT_DB_PREFIX."gatekeeper_security_map` cgm ON (cgm.`content_id`=lc.`content_id`), `".BIT_DB_PREFIX."fisheye_gallery` fg
  443.                                 WHERE `cb_parent_content_id`=fg.`content_id` $whereSql "; //  ORDER BY RANDOM() is DOG slow (seq scans)
  444.                     if( $row = $this->mDb->getRow( $query, $bindVars ) ) {
  445.                         $pThumbnailContentType = $row['content_type_guid'];
  446.                         $pThumbnailContentId = $row['content_id'];
  447.                     }
  448.                 } else {
  449.                     $query = "SELECT fgim.`item_content_id`, lc.`content_type_guid`
  450.                             FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( fgim.`item_content_id`=lc.`content_id` )
  451.                             WHERE fgim.`gallery_content_id` = ?
  452.                             ORDER BY ".$this->mDb->convertSortmode('random');
  453.                     $rs = $this->mDb->query($query, array( $pContentId ), 1);
  454.                     $pThumbnailContentId = $rs->fields['item_content_id'];
  455.                     $pThumbnailContentType = $rs->fields['content_type_guid'];
  456.                 }
  457.             }
  458.         }
  459.  
  460.         if( @$this->verifyId( $pThumbnailContentId ) ) {
  461.             $ret = LibertyBase::getLibertyObject( $pThumbnailContentId, $pThumbnailContentType );
  462.             if( is_a( $ret, 'FisheyeGallery' ) ) {
  463.                 //recurse down in to find the first image
  464.                 if( $ret = $ret->getThumbnailImage() ) {
  465.                     $this->mInfo['thumbnail_content_id'] = $ret->getField( 'content_id' );
  466.                 }
  467.             } else {
  468.                 $this->mInfo['thumbnail_content_id'] = $pThumbnailContentId;
  469.             }
  470.         }
  471.         return $ret;
  472.     }
  473.  
  474.  
  475.     function loadThumbnail( $pSize='small', $pContentId=NULL ) {
  476.         if( $this->mPreviewImage = $this->getThumbnailImage( $pContentId ) ) {
  477.             $this->mInfo['preview_content'] = &$this->mPreviewImage;
  478.             $this->mInfo['image_file'] = &$this->mPreviewImage->mInfo['image_file'];
  479.         }
  480.     }
  481.  
  482.  
  483.     function storeGalleryThumbnail($pContentId = NULL) {
  484.         $ret = FALSE;
  485.         if ($pContentId && !$this->isInGallery( $this->mContentId, $pContentId ) ) {
  486.             return FALSE;
  487.         }
  488.         if ($this->mGalleryId) {
  489.             if (!$pContentId)
  490.                 $pContentId = NULL;
  491.             $query = "UPDATE `".BIT_DB_PREFIX."fisheye_gallery` SET `preview_content_id` = ? WHERE `gallery_id`= ?";
  492.             $rs = $this->mDb->query($query, array($pContentId, $this->mGalleryId));
  493.             $this->mInfo['preview_content_id'] = $pContentId;
  494.             $ret = TRUE;
  495.         }
  496.         return $ret;
  497.     }
  498.  
  499.     function store(&$pStorageHash) {
  500.         if ($this->verifyGalleryData($pStorageHash)) {
  501.             $this->mDb->StartTrans();
  502.             if( LibertyContent::store($pStorageHash)) {
  503.                 $this->mContentId = $pStorageHash['content_id'];
  504.                 $this->mInfo['content_id'] = $this->mContentId;
  505.                 if ($this->galleryExistsInDatabase()) {
  506.                     $query = "UPDATE `".BIT_DB_PREFIX."fisheye_gallery`
  507.                             SET `rows_per_page` = ?, `cols_per_page` = ?, `thumbnail_size` = ?
  508.                             WHERE `gallery_id` = ?";
  509.                     $bindVars = array($pStorageHash['rows_per_page'], $pStorageHash['cols_per_page'], $pStorageHash['thumbnail_size'], $this->mGalleryId);
  510.                 } else {
  511.                     $this->mGalleryId = $this->mDb->GenID('fisheye_gallery_id_seq');
  512.                     $this->mInfo['gallery_id'] = $this->mGalleryId;
  513.                     $query = "INSERT INTO `".BIT_DB_PREFIX."fisheye_gallery` (`gallery_id`, `content_id`, `rows_per_page`, `cols_per_page`, `thumbnail_size`) VALUES (?,?,?,?,?)";
  514.                     $bindVars = array($this->mGalleryId, $this->mContentId, $pStorageHash['rows_per_page'], $pStorageHash['cols_per_page'], $pStorageHash['thumbnail_size']);
  515.                 }
  516.                 $rs = $this->mDb->query($query, $bindVars);
  517.                 $this->mDb->CompleteTrans();
  518.             } else {
  519.                 $this->mDb->RollbackTrans();
  520.                 $this->mErrors[] = "There were errors while attempting to save this gallery";
  521.             }
  522.         } else {
  523.             $this->mErrors[] = "There were errors while attempting to save this gallery";
  524.         }
  525.  
  526.         return (count($this->mErrors) == 0);
  527.     }
  528.  
  529.     function removeItem( $pContentId ) {
  530.         $ret = FALSE;
  531.         if( $this->isValid() && @$this->verifyId( $pContentId ) ) {
  532.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map`
  533.                       WHERE `item_content_id`=? AND `gallery_content_id`=?";
  534.             $rs = $this->mDb->query($query, array($pContentId, $this->mContentId ) );
  535.             $ret = TRUE;
  536.         }
  537.         return $ret;
  538.     }
  539.  
  540.     /**
  541.     * Adds a new item (image or gallery) to this gallery. We check to make sure we are not a member
  542.     * of this gallery and this gallery is not a member of the new item to avoid infinite recursion scenarios
  543.     * @return boolean wheter or not the item was added
  544.     */
  545.     function addItem( $pContentId, $pPosition=NULL ) {
  546.         global $gBitSystem;
  547.         $ret = FALSE;
  548.         if( @$this->verifyId( $this->mContentId ) && @$this->verifyId( $pContentId ) && ( $this->mContentId != $pContentId ) && !$this->isInGallery( $this->mContentId, $pContentId  )  && !$this->isInGallery( $pContentId, $this->mContentId ) ) {
  549.             $query = "INSERT INTO `".BIT_DB_PREFIX."fisheye_gallery_image_map` (`item_content_id`, `gallery_content_id`, `item_position`) VALUES (?,?,?)";
  550.             $rs = $this->mDb->query($query, array($pContentId, $this->mContentId, $pPosition ) );
  551.             $query = "UPDATE `".BIT_DB_PREFIX."liberty_content` SET `last_modified`=? WHERE `content_id`=?";
  552.             $rs = $this->mDb->query( $query, array( $gBitSystem->getUTCTime(), $this->mContentId ) );
  553.             $ret = TRUE;
  554.         }
  555.         return $ret;
  556.     }
  557.  
  558.     function expunge( $pRecursiveDelete = FALSE ) {
  559.         if( $this->isValid() ) {
  560.             $this->mDb->StartTrans();
  561.  
  562.             if( $this->loadImages() ) {
  563.                 foreach( array_keys( $this->mItems ) as $key ) {
  564.                     if( $pRecursiveDelete ) {
  565.                         $this->mItems[$key]->expunge( $pRecursiveDelete );
  566.                     } elseif( is_a( $this->mItems[$key], 'FisheyeImage' ) ) {
  567.                         // make sure we have a valid content_id before we exec
  568.                         if( is_numeric( $this->mItems[$key]->mContentId ) ) {
  569.                             $query = "SELECT COUNT(`item_content_id`) AS `other_gallery`
  570.                                       FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map`
  571.                                       WHERE `item_content_id`=? AND `gallery_content_id`!=?";
  572.                             if( !($inOtherGallery = $this->mDb->getOne($query, array($this->mItems[$key]->mContentId, $this->mContentId ) )) ) {
  573.                                 $this->mItems[$key]->expunge();
  574.                             }
  575.                         }
  576.                     }
  577.                 }
  578.             }
  579.  
  580.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` WHERE `gallery_content_id`=?";
  581.             $rs = $this->mDb->query($query, array( $this->mContentId ) );
  582.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` WHERE `item_content_id`=?";
  583.             $rs = $this->mDb->query($query, array( $this->mContentId ) );
  584.             $query = "DELETE FROM `".BIT_DB_PREFIX."fisheye_gallery` WHERE `content_id`=?";
  585.             $rs = $this->mDb->query($query, array( $this->mContentId ) );
  586.             if( LibertyContent::expunge() ) {
  587.                 $this->mDb->CompleteTrans();
  588.             } else {
  589.                 $this->mDb->RollbackTrans();
  590.                 error_log( "Error expunging fisheye gallery: " . vc($this->mErrors ) );
  591.             }
  592.         }
  593.         return( count( $this->mErrors ) == 0 );
  594.     }
  595.  
  596.  
  597.     function galleryExistsInDatabase() {
  598.         $ret = FALSE;
  599.  
  600.         if( @$this->verifyId( $this->mGalleryId ) ) {
  601.             $query = "SELECT COUNT(`gallery_id`) AS `gcount`
  602.                     FROM `".BIT_DB_PREFIX."fisheye_gallery`
  603.                     WHERE `gallery_id` = ?";
  604.             $rs = $this->mDb->query($query, array($this->mGalleryId));
  605.             if ($rs->fields['gcount'] > 0)
  606.                 $ret = TRUE;
  607.         }
  608.  
  609.         return $ret;
  610.     }
  611.  
  612.     /**
  613.     * Returns the layout of the gallery accounting for various defaults
  614.     * @return the layout string preference
  615.     */
  616.     function getLayout() {
  617.         global $gBitSystem;
  618.         return $this->getPreference( 'gallery_pagination', $gBitSystem->getConfig( 'default_gallery_pagination', FISHEYE_PAGINATION_GALLERIFFIC ) );
  619.     }
  620.  
  621.     public static function getAllLayouts() {
  622.         return array(
  623.             FISHEYE_PAGINATION_GALLERIFFIC     => 'Galleriffic',
  624.             FISHEYE_PAGINATION_FIXED_GRID      => 'Fixed Grid',
  625.             FISHEYE_PAGINATION_AUTO_FLOW       => 'Auto-Flow Images',
  626.             FISHEYE_PAGINATION_POSITION_NUMBER => 'Image Order Page Number',
  627.             FISHEYE_PAGINATION_SIMPLE_LIST     => 'Simple List',
  628. //            FISHEYE_PAGINATION_MATTEO           => 'Matteo',
  629.         );
  630.     }
  631.  
  632.     /**
  633.     * Returns include file that will setup the object for rendering
  634.     * @return the fully specified path to file to be included
  635.     */
  636.     function getRenderFile() {
  637.         return FISHEYE_PKG_PATH."display_fisheye_gallery_inc.php";
  638.     }
  639.  
  640.     /**
  641.     * Returns template file used for display
  642.     * @return the fully specified path to file to be included
  643.     */
  644.     function getRenderTemplate() {
  645.         return 'bitpackage:fisheye/view_gallery.tpl';
  646.     }
  647.  
  648.     /**
  649.     * Function that returns link to display a piece of content
  650.     * @param pGalleryId id of gallery to link
  651.     * @return the url to display the gallery.
  652.     */
  653.     public static function getDisplayUrlFromHash( &$pParamHash ) {
  654.         $path = NULL;
  655.  
  656.         if( BitBase::verifyIdParameter( $pParamHash, 'gallery_id' ) ) {
  657.             $ret = FISHEYE_PKG_URL;
  658.             global $gBitSystem;
  659.             if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
  660.                 $ret .= 'gallery'.$path.'/'.$pParamHash['gallery_id'];
  661.             } else {
  662.                 $ret .= 'view.php?gallery_id='.$pParamHash['gallery_id'];
  663.                 if( !empty( $pHash['path'] ) ) {
  664.                     $ret .= '&gallery_path='.$pParamHash['path'];
  665.                 }
  666.             }
  667.         } elseif( @BitBase::verifyId( $pParamHash['content_id'] ) ) {
  668.             $ret = FISHEYE_PKG_URL.'view.php?content_id='.$pParamHash['content_id'];
  669.         }
  670.         return $ret;
  671.     }
  672.  
  673.     function getTree( $pListHash ) {
  674.         global $gBitDb;
  675.  
  676.         $ret = array();
  677.         if( $this->mDb->isAdvancedPostgresEnabled() ) {
  678.             $bindVars = array();
  679.             $containVars = array();
  680.             $selectSql = '';
  681.             $joinSql = '';
  682.             $whereSql = '';
  683.             if( !empty( $pListHash['contain_item'] ) ) {
  684.                 $selectSql = " , tfgim3.`item_content_id` AS `in_gallery` ";
  685.                 $joinSql .= " LEFT OUTER JOIN  `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim3 ON (tfgim3.`gallery_content_id`=lc.`content_id`) AND tfgim3.`item_content_id`=? ";
  686.                 $bindVars[] = $pListHash['contain_item'];
  687.                 $containVars[] = $pListHash['contain_item'];
  688.             }
  689.             if( isset( $pListHash['contain_item'] ) ) {
  690.                 // contain item might have squeaked in as 0, clear our from pListHash
  691.                 unset( $pListHash['contain_item'] );
  692.             }
  693.             foreach( $pListHash as $key=>$val ) {
  694.                 $whereSql .= " $key=? AND ";
  695.                 $bindVars[] = $val;
  696.             }
  697.  
  698.             $query =   "SELECT lc.`content_id` AS `hash_key`, fg.*, lc.* $selectSql
  699.                         FROM `".BIT_DB_PREFIX."fisheye_gallery` fg
  700.                             INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON(fg.`content_id`=lc.`content_id`)
  701.                             $joinSql
  702.                         WHERE $whereSql NOT EXISTS (SELECT gallery_content_id FROM fisheye_gallery_image_map tfgim2 WHERE tfgim2.item_content_id=lc.content_id)
  703.                         ORDER BY lc.title";
  704.             $rootContent = $gBitDb->GetAssoc( $query, $bindVars );
  705.  
  706.             foreach( array_keys( $rootContent ) as $conId ) {
  707.                 $splitVars = array();
  708.                 $query = "SELECT branch AS hash_key, * $selectSql
  709.                           FROM connectby('`".BIT_DB_PREFIX."fisheye_gallery_image_map`', '`item_content_id`', '`gallery_content_id`', ?, 0, '/') AS t(cb_item_content_id int,cb_gallery_content_id int, level int, branch text)
  710.                             INNER JOIN `".BIT_DB_PREFIX."fisheye_gallery` fg ON (fg.`content_id`=cb_item_content_id)
  711.                             INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON(lc.`content_id`=fg.`content_id`)
  712.                             $joinSql
  713.                           ORDER BY branch, lc.`title`";
  714.                 $splitVars[] = $conId;
  715.                 if( !empty( $containVars ) ) {
  716.                     $splitVars[] = $containVars[0];
  717.                 }
  718.  
  719.                 FisheyeGallery::splitConnectByTree( $ret, $gBitDb->GetAssoc( $query, $splitVars ) );
  720.                 FisheyeGallery::getTreeSort( $ret );
  721.             }
  722.         } else if ( $this->mDb->mType == 'firebird' ) {
  723.             $bindVars = array();
  724.             $containVars = array();
  725.             $selectSql = '';
  726.             $joinSql = '';
  727.             $whereSql = '';
  728.  
  729.             if( !empty( $pListHash['contain_item'] ) ) {
  730.                 $selectSql = " , tfgim3.`item_content_id` AS `in_gallery` ";
  731.                 $joinSql .= " LEFT OUTER JOIN  `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim3 ON (tfgim3.`gallery_content_id`=lc.`content_id`) AND tfgim3.`item_content_id`=? ";
  732.                 $bindVars[] = $pListHash['contain_item'];
  733.                 $containVars[] = $pListHash['contain_item'];
  734.             }
  735.             $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  736.  
  737.             if( isset( $pListHash['contain_item'] ) ) {
  738.                 // contain item might have squeaked in as 0, clear our from pListHash
  739.                 unset( $pListHash['contain_item'] );
  740.             }
  741.             foreach( $pListHash as $key=>$val ) {
  742.                 $whereSql .= " AND lc.$key=? ";
  743.                 $bindVars[] = $val;
  744.             }
  745.  
  746.             $splitVars = array();
  747.                     $query = "WITH RECURSIVE
  748.                                 GALLERY_TREE AS (
  749.                                 SELECT B.`content_id` AS gallery_content_id, B.`content_id` AS item_content_id, 0 AS BLEVEL, CAST( lcp.`title` AS VARCHAR(255) ) AS BRANCH, 0 AS gallery_parent_id
  750.                                 FROM `".BIT_DB_PREFIX."fisheye_gallery` B
  751.                                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lcp ON(lcp.`content_id`=B.`content_id`)
  752.                                 WHERE NOT EXISTS (SELECT gallery_content_id FROM fisheye_gallery_image_map tfgim2 WHERE tfgim2.item_content_id=B.content_id)
  753.  
  754.                                 UNION ALL
  755.  
  756.                                 SELECT `item_content_id` AS gallery_content_id, `item_content_id`, G.BLEVEL + 1, G.BRANCH || '/' || `item_content_id` AS BRANCH, `gallery_content_id` AS gallery_parent_id
  757.                                 FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` G1
  758.                                 JOIN GALLERY_TREE G
  759.                                 ON G1.`gallery_content_id` = G.`item_content_id`
  760.                                 INNER JOIN `".BIT_DB_PREFIX."liberty_content` lcg1 ON(lcg1.`content_id`=`item_content_id`) and lcg1.`content_type_guid` = 'fisheyegallery'
  761.                             )
  762.                             SELECT T.BRANCH AS hash_key, T.BLEVEL, fg.*, lc.* $selectSql
  763.                             FROM GALLERY_TREE T
  764.                             INNER JOIN `".BIT_DB_PREFIX."fisheye_gallery` fg ON (fg.`content_id`=T.`gallery_content_id`)
  765.                             INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.`content_id`=T.`item_content_id`)
  766.                             LEFT OUTER JOIN  `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgimo ON (fgimo.`gallery_content_id`=T.gallery_parent_id) AND fgimo.`item_content_id`=T.gallery_content_id
  767.                             $joinSql
  768.                             WHERE lc.`content_type_guid` = 'fisheyegallery' $whereSql
  769.                           ORDER BY T.BRANCH, fgimo.`item_position`";
  770.  
  771.  
  772.  
  773.             if( !empty( $bindVars ) ) {
  774.                 FisheyeGallery::splitConnectByTree( $ret, $gBitDb->GetAssoc( $query, $bindVars ) );
  775.             } else {
  776.                 FisheyeGallery::splitConnectByTree( $ret, $gBitDb->GetAssoc( $query ) );
  777.             }
  778.  
  779.         } else {
  780. // this needs replacing with a more suitable list query ...
  781.             $pListHash['show_empty'] = TRUE;
  782.             $galList = $this->getList( $pListHash );
  783.             // index by content_id
  784.             foreach( $galList as $galId => $gal ) {
  785.                 $ret[$gal['content_id']] = $gal;
  786.             }
  787.             FisheyeGallery::splitConnectByTree( $ret, $ret );
  788.             FisheyeGallery::getTreeSort( $ret );
  789.         }
  790.         return( $ret );
  791.     }
  792.  
  793.     function getTreeSort( &$pTree ) {
  794.         if( $pTree ) {
  795.             foreach( array_keys( $pTree ) as $k ) {
  796.                 if( !empty( $pTree[$k]['children'] ) ) {
  797.                     FisheyeGallery::getTreeSort( $pTree[$k]['children'] );
  798.                 }
  799.             }
  800.             uasort( $pTree, array( 'FisheyeGallery', 'getTreeSortCmp' ) );
  801.         }
  802.     }
  803.  
  804.     static function getTreeSortCmp( $a, $b ) {
  805.         return strcmp( $a['content']['title'], $b['content']['title'] );
  806.     }
  807.  
  808.     function splitConnectByTree( &$pRet, $pTreeHash ) {
  809.         if( $pTreeHash ) {
  810.             foreach( array_keys( $pTreeHash ) as $conId ) {
  811.                 $path = explode( '/', $conId );
  812.                 FisheyeGallery::recurseConnectByPath( $pRet, $pTreeHash[$conId], $path );
  813.             }
  814.         }
  815.     }
  816.  
  817.     function recurseConnectByPath( &$pRet, $pTreeHash, $pPath ) {
  818.         $popId = array_shift( $pPath );
  819.         if( count( $pPath ) > 0 ) {
  820.             if( empty( $pRet[$popId]['children'] ) ) {
  821.                 $pRet[$popId]['children'] = array();
  822.             }
  823.             FisheyeGallery::recurseConnectByPath( $pRet[$popId]['children'], $pTreeHash, $pPath );
  824.         } else {
  825.  
  826.             $pRet[$popId]['content'] = $pTreeHash;
  827.         }
  828.     }
  829.  
  830.     // Generate a nested ul list of listed galleries
  831.     function generateList( $pListHash, $pOptions, $pLocate = FALSE ) {
  832.         $ret = '';
  833.         if( $hash = FisheyeGallery::getTree( $pListHash ) ) {
  834.  
  835.             $class = 'unstyled';
  836.             $ret = "<ul ";
  837.             foreach( array( 'class', 'name', 'id', 'onchange' ) as $key ) {
  838.                 if( !empty( $pOptions[$key] ) ) {
  839.                     if( $key == 'class' ) {
  840.                         $class .= ' '.$pOptions[$key];
  841.                     } else {
  842.                         $ret .= " $key=\"$pOptions[$key]\" ";
  843.                     }
  844.                 }
  845.             }
  846.             $ret .= ' class="'.$class.'">';
  847.             $ret .= FisheyeGallery::generateListItems( $hash, $pOptions, $pLocate );
  848.             $ret .= "</ul>";
  849.         }
  850.         return $ret;
  851.     }
  852.  
  853.     // Helper method for generateMenu. See that method. Is Recursive
  854.     function generateListItems( &$pHash, $pOptions, $pLocate ) {
  855.         $ret = '';
  856.         foreach( array_keys( $pHash ) as $conId ) {
  857.             $class = !empty( $pOptions['radio_checkbox'] ) ? 'checkbox' : '';
  858.             $ret .= '<li id="fisheyegallery'.$pHash[$conId]['content']['gallery_id'].'" gallery_id="'.$pHash[$conId]['content']['gallery_id'].'" ';
  859.             if( !empty( $pOptions['item_attributes'] ) ) {
  860.                 foreach( $pOptions['item_attributes'] as $key=>$value ) {
  861.                     if( $key == 'class' ) {
  862.                         $class .= ' '.$value;
  863.                     } else {
  864.                         $ret .= " $key=\"$value\" ";
  865.                     }
  866.                 }
  867.             }
  868.             $ret .= ' class="'.$class.'"><label>';
  869.             if ( $pLocate || $pHash[$conId]['content']['content_id'] != $this->mContentId ) {
  870.                 if( !empty( $pOptions['radio_checkbox'] ) ) {
  871.                     $ret .= '<input type="checkbox" name="gallery_additions[]" value="'.$pHash[$conId]['content']['gallery_id'].'" ';
  872.                     if( !empty( $pHash[$conId]['content']['in_gallery'] ) || $pHash[$conId]['content']['content_id'] == $this->mContentId ) {
  873.                         $ret .=    ' checked="checked" ';
  874.                     }
  875.                     $ret .= '/>';
  876.                 }
  877.             }
  878.             if ( $pHash[$conId]['content']['content_id'] == $this->mContentId
  879.                 or ( isset( $pHash[$conId]['content']['in_gallery'] ) and $pHash[$conId]['content']['in_gallery'] ) ) {
  880.                 $ret .= '<span class="active">'.htmlspecialchars( $pHash[$conId]['content']['title'] ).'</span>';
  881.             } else {
  882.                 $ret .= htmlspecialchars( $pHash[$conId]['content']['title'] );
  883.             }
  884.             $ret .= '</label></li>';
  885.             if( !empty( $pHash[$conId]['children'] ) ) {
  886.                 $ret .= '<li><ul>'.FisheyeGallery::generateListItems( $pHash[$conId]['children'], $pOptions, $pLocate ).'</ul></li>';
  887.             }
  888.         }
  889.         return $ret;
  890.     }
  891.  
  892.  
  893.     // Generate a select drop menu of listed galleries
  894.     function generateMenu( $pListHash, $pOptions, $pLocate=NULL ) {
  895.         $ret = "<select class='form-control' ";
  896.         foreach( array( 'class', 'name', 'id', 'onchange' ) as $key ) {
  897.             if( !empty( $pOptions[$key] ) ) {
  898.                 $ret .= " $key=\"$pOptions[$key]\" ";
  899.             }
  900.         }
  901.         $ret .= ">";
  902.         $ret .= !empty( $pOptions['first_option'] ) ? $pOptions['first_option'] : '';
  903.         if( $hash = FisheyeGallery::getTree( $pListHash ) ) {
  904.             $ret .= FisheyeGallery::generateMenuOptions( $hash, $pOptions, $pLocate );
  905.         }
  906.         $ret .= "</select>";
  907.         return $ret;
  908.     }
  909.  
  910.     // Helper method for generateMenu. See that method. Is Recursive
  911.     function generateMenuOptions( &$pHash, $pOptions, $pLocate, $pPrefix='' ) {
  912.         $ret = '';
  913.         foreach( array_keys( $pHash ) as $conId ) {
  914.             $ret .= '<option gallery_id="'.$pHash[$conId]['content']['gallery_id'].'" value="'.$pHash[$conId]['content']['gallery_id'].'"';
  915.             if( !empty( $pOptions['item_attributes'] ) ) {
  916.                 foreach( $pOptions['item_attributes'] as $key=>$value ) {
  917.                     $ret .= " $key=\"$value\" ";
  918.                 }
  919.             }
  920.             if ( $pLocate && $pLocate == $pHash[$conId]['content']['gallery_id'] ) {
  921.                 $ret .=    ' selected="selected" ';
  922.             }
  923.             $ret .= ' >'.($pPrefix?$pPrefix.'&raquo; ':'').htmlspecialchars( $pHash[$conId]['content']['title'] ).'</option>';
  924.  
  925.             if( !empty( $pHash[$conId]['children'] ) ) {
  926.                 $ret .= FisheyeGallery::generateMenuOptions( $pHash[$conId]['children'], $pOptions, $pLocate, ($pPrefix.'-') );
  927.             }
  928.         }
  929.         return $ret;
  930.     }
  931.  
  932.     function getList( &$pListHash ) {
  933.         global $gBitUser,$gBitSystem, $gBitDbType;
  934.  
  935.         $pListHash['valid_sort_modes'] = array( 'real_name', 'login', 'hits', 'title', 'created', 'last_modified', 'last_hit', 'event_time', 'ip' );
  936.  
  937.         LibertyContent::prepGetList( $pListHash );
  938.         $bindVars = array();
  939.         $selectSql = $joinSql = $whereSql = $sortSql = '';
  940.  
  941.         if( $gBitDbType == 'mysql' ) {
  942.             // loser mysql without subselects
  943.             if( !empty( $pListHash['root_only'] ) ) {
  944.                 $joinSql .= " LEFT OUTER JOIN  `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim2 ON (tfgim2.`item_content_id`=lc.`content_id`)";
  945.                 $whereSql .= ' AND tfgim2.`item_content_id` IS NULL ';
  946.             }
  947.         }
  948.  
  949.         if( !empty( $pListHash['contain_item'] ) ) {
  950.             $selectSql = " , tfgim3.`item_content_id` AS `in_gallery` ";
  951.             $joinSql .= " LEFT OUTER JOIN  `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim3 ON (tfgim3.`gallery_content_id`=lc.`content_id`) AND tfgim3.`item_content_id`=? ";
  952.             $bindVars[] = $pListHash['contain_item'];
  953.         }
  954.  
  955.         if( @$this->verifyId( $pListHash['user_id'] ) ) {
  956.             $whereSql .= " AND lc.`user_id` = ? ";
  957.             $bindVars[] = (int)$pListHash['user_id'];
  958.         }
  959.  
  960.         if( !empty( $pListHash['find'] ) ) {
  961.             $whereSql .= " AND UPPER( lc.`title` ) LIKE ? ";
  962.             $bindVars[] = '%'.strtoupper( $pListHash['find'] ).'%';
  963.         }
  964.  
  965.         if( !empty( $pListHash['show_public'] ) ) {
  966.             $joinSql .= " LEFT OUTER JOIN  `".BIT_DB_PREFIX."liberty_content_prefs` lcp ON( lcp.`content_id`=lc.`content_id` )";
  967.             $whereSql .= " OR  ( lcp.`pref_name`=? AND lcp.`pref_value`=? ) ";
  968.             $bindVars[] = 'is_public';
  969.             $bindVars[] = 'y';
  970.         }
  971.  
  972.         $mapJoin = "";
  973.         if( $gBitDbType != 'mysql' ) {
  974.             // weed out empty galleries if we don't need them. DO NOT get clever and change the IN and EXISTS choices here.
  975.             if( empty( $pListHash['show_empty'] ) ) {
  976.                 $whereSql .= " AND fg.`content_id` IN (SELECT `gallery_content_id` FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim WHERE fgim.`gallery_content_id`=fg.`content_id`)";
  977.             }
  978.             if( !empty( $pListHash['root_only'] ) ) {
  979.                 $whereSql .= " AND NOT EXISTS (SELECT `gallery_content_id` FROM `".BIT_DB_PREFIX."fisheye_gallery_image_map` tfgim2 WHERE tfgim2.`item_content_id`=lc.`content_id`)";
  980.             }
  981.         } else {
  982.             // weed out empty galleries if we don't need them
  983.             if( empty( $pListHash['show_empty'] ) ) {
  984.                 $mapJoin = "INNER JOIN `".BIT_DB_PREFIX."fisheye_gallery_image_map` fgim ON (fgim.`gallery_content_id`=lc.`content_id`)";
  985.             }
  986.         }
  987.  
  988.         if ( !empty( $pListHash['sort_mode'] ) ) {
  989.             //converted in prepGetList()
  990.             $sortSql .= " ORDER BY ".$this->mDb->convertSortmode( $pListHash['sort_mode'] )." ";
  991.         }
  992.         // Putting in the below hack because mssql cannot select distinct on a text blob column.
  993.         $selectSql .= $gBitDbType == 'mssql' ? " ,CAST(lc.`data` AS VARCHAR(250)) as `data` " : " ,lc.`data` ";
  994.  
  995.         $this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
  996.  
  997.         if( !empty( $whereSql ) ) {
  998.             $whereSql = substr_replace( $whereSql, ' WHERE ', 0, 4 );
  999.         }
  1000.  
  1001.         $query = "SELECT fg.`gallery_id` AS `hash_key`, fg.*,
  1002.                     lc.`user_id`, lc.`modifier_user_id`, lc.`created`, lc.`last_modified`,
  1003.                     lc.`content_type_guid`, lc.`format_guid`, lch.`hits`, lch.`last_hit`, lc.`event_time`, lc.`version`,
  1004.                     lc.`lang_code`, lc.`title`, lc.`ip`, uu.`login`, uu.`real_name`, plc.`content_type_guid` AS `preview_content_type_guid`
  1005.                     $selectSql
  1006.                 FROM `".BIT_DB_PREFIX."fisheye_gallery` fg
  1007.                     INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (fg.`content_id` = lc.`content_id`)
  1008.                     INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = lc.`user_id`)
  1009.                     LEFT JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON (lch.`content_id` = lc.`content_id`)
  1010.                     $mapJoin $joinSql
  1011.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` plc ON (fg.`preview_content_id` = plc.`content_id`)
  1012.                 $whereSql $sortSql";
  1013.         if( $rs = $this->mDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] ) ) {
  1014.             $data = $rs->GetAssoc();
  1015.             if( empty( $pListHash['no_thumbnails'] ) ) {
  1016.                 $thumbsize = !empty( $pListHash['thumbnail_size'] ) ? $pListHash['thumbnail_size'] : 'small';
  1017.                 foreach( array_keys( $data ) as $galleryId ) {
  1018.                     $data[$galleryId]['display_url'] = static::getDisplayUrlFromHash( $data[$galleryId] );
  1019.                     $data[$galleryId]['display_uri'] = static::getDisplayUriFromHash( $data[$galleryId] );
  1020.                     if( $thumbImage = $this->getThumbnailImage( $data[$galleryId]['content_id'], $data[$galleryId]['preview_content_id'], $data[$galleryId]['preview_content_type_guid'] ) ) {
  1021.                         $data[$galleryId]['thumbnail_url'] = $thumbImage->getThumbnailUrl( $thumbsize );
  1022.                         $data[$galleryId]['thumbnail_uri'] = $thumbImage->getThumbnailUri( $thumbsize );
  1023.                     } elseif( !empty( $pListHash['show_empty'] ) ) {
  1024.                         $data[$galleryId]['thumbnail_url'] = FISHEYE_PKG_URL.'image/no_image.png';
  1025.                     } else {
  1026.                         unset( $data[$galleryId] );
  1027.                     }
  1028.                 }
  1029.             }
  1030.         }
  1031.  
  1032.         // count galleries
  1033.         $query_c = "SELECT COUNT( fg.`gallery_id` )
  1034.                     FROM `".BIT_DB_PREFIX."fisheye_gallery` fg
  1035.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (fg.`content_id` = lc.`content_id` )
  1036.                         INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id` = lc.`user_id`)
  1037.                     LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` ptc ON( fg.`preview_content_id`=ptc.`content_id` )
  1038.                 $mapJoin $joinSql
  1039.                 $whereSql";
  1040.         $cant = $this->mDb->getOne( $query_c, $bindVars );
  1041.  
  1042.         // add all pagination info to $ret
  1043.         $pListHash['cant'] = $cant;
  1044.         LibertyContent::postGetList( $pListHash );
  1045.         return $data;
  1046.     }
  1047.  
  1048.     function download(){
  1049.         if($this->isValid()){
  1050.             $zip = new ZipArchive();
  1051.  
  1052.             $filename = tempnam(TEMP_PKG_PATH,"galleryzip");
  1053.             $path = '/';
  1054.  
  1055.             if( $zip->open ($filename, ZIPARCHIVE::OVERWRITE) !== TRUE ){
  1056.                 $this->mErrors['download'] = "Unable to create zip file";
  1057.             }else{
  1058.                 addGalleryRecursive( $this->mGalleryId , $path, $zip);
  1059.             }
  1060.             $zip->close();
  1061.  
  1062.             //escape backslashes
  1063.             $outputFileTitle = str_replace("\\",'\\\\',$this->getTitle());
  1064.             //escape double quotes
  1065.             $outputFileTitle = str_replace('"','\\"',$outputFileTitle);
  1066.  
  1067.             header('Content-Description: File Transfer');
  1068.             header('Content-Type: application/octet-stream');
  1069.             Header ("Content-disposition: attachment; filename=\"".$outputFileTitle.".zip\"");
  1070.             header('Content-Transfer-Encoding: binary');
  1071.             header('Expires: 0');
  1072.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  1073.             header('Pragma: public');
  1074.             Header ("Content-Length: ".filesize( $filename ) );
  1075.             ob_end_flush();
  1076.             readfile($filename);
  1077.             unlink($filename);
  1078.         }
  1079.     }
  1080.  
  1081.     public static function getServiceIcon() {
  1082.         return '<i class="icon-camera"></i>';
  1083.     }
  1084.  
  1085.     public static function getServiceKey() {
  1086.         return 'fisheye';
  1087.     }
  1088. }
  1089.  
  1090. function addGalleryRecursive( $pGalleryId , $pPath = '/', &$pZip ){
  1091.  
  1092.     $gallery = new FisheyeGallery($pGalleryId);
  1093.     $gallery->load();
  1094.     $gallery->loadImages();
  1095.     $pPath .= $gallery->getTitle().'/';
  1096.     foreach ( $gallery->mItems as $item ){
  1097.         if( is_a( $item , 'FisheyeImage' ) ){
  1098.             $sourcePath = $item->getSourceFile();
  1099.             $title = $item->getTitle();
  1100.             $pZip->addFile($sourcePath, $pPath.$title.substr($sourcePath,strrpos($sourcePath,'.')) );
  1101.         }elseif ( is_a( $item , 'FisheyeGallery' ) ){
  1102.             addGalleryRecursive($item->mGalleryId,$pPath,$pZip);
  1103.         }
  1104.     }
  1105. }
  1106.  
  1107. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:32 +0000 by phpDocumentor 1.5.0-lsces