PageRenderTime 176ms CodeModel.GetById 60ms app.highlight 49ms RepoModel.GetById 57ms app.codeStats 1ms

/TreasuryGallery.php

https://github.com/tekimaki/treasury
PHP | 614 lines | 397 code | 70 blank | 147 comment | 91 complexity | d5a30abe1ac9d40364c522748d1a4e5e MD5 | raw file
  1<?php
  2/**
  3 * @version      $Header$
  4 *
  5 * @author       xing  <xing@synapse.plus.com>
  6 * @version      $Revision$
  7 * created      Monday Jul 03, 2006   11:53:42 CEST
  8 * @package      treasury
  9 * @copyright    2003-2006 bitweaver
 10 * @license      LGPL {@link http://www.gnu.org/licenses/lgpl.html}
 11 **/
 12
 13/**
 14 * Setup
 15 */
 16define( 'TREASURYGALLERY_CONTENT_TYPE_GUID', 'treasurygallery' );
 17require_once( TREASURY_PKG_PATH.'TreasuryBase.php' );
 18
 19/**
 20 *   TreasuryGallery
 21 *
 22 * @package treasury
 23 * @uses TreasuryBase
 24 */
 25class TreasuryGallery extends TreasuryBase {
 26
 27	/**
 28	 * If this content is being viewed within a structure
 29	 * @public
 30	 */
 31	var $mStructureId;
 32
 33	/**
 34	 * Initiate class
 35	 *
 36	 * @param $pContentId content id of the treasury - use either one of the ids.
 37	 * @param $pStructureId structure id of the treasury - use either one of the ids.
 38	 * @return none
 39	 * @access public
 40	 **/
 41	function TreasuryGallery( $pStructureId = NULL, $pContentId = NULL ) {
 42		TreasuryBase::TreasuryBase();
 43		$this->registerContentType( TREASURYGALLERY_CONTENT_TYPE_GUID, array(
 44			'content_type_guid' => TREASURYGALLERY_CONTENT_TYPE_GUID,
 45			'content_name' => 'File Gallery',
 46			'content_name_plural' => 'File Galleries',
 47			'handler_class' => 'TreasuryGallery',
 48			'handler_package' => 'treasury',
 49			'handler_file' => 'TreasuryGallery.php',
 50			'maintainer_url' => 'http://www.bitweaver.org'
 51		) );
 52		$this->mContentId = $pContentId;
 53		$this->mStructureId = $pStructureId;
 54		$this->mContentTypeGuid = TREASURYGALLERY_CONTENT_TYPE_GUID;
 55
 56		// Permission setup
 57		$this->mViewContentPerm  = 'p_treasury_view_gallery';
 58		$this->mCreateContentPerm  = 'p_treasury_create_gallery';
 59		$this->mUpdateContentPerm  = 'p_treasury_update_gallery';
 60		$this->mAdminContentPerm = 'p_treasury_admin';
 61	}
 62
 63	/**
 64	 * load the treasury gallery
 65	 *
 66	 * @param $pExtras boolean - if set to true, treasury content is added as well
 67	 * @return bool TRUE on success, FALSE if it's not valid
 68	 * @access public
 69	 **/
 70	function load( $pExtras = FALSE ) {
 71		if( @BitBase::verifyId( $this->mContentId ) || @BitBase::verifyId( $this->mStructureId ) ) {
 72			global $gBitSystem;
 73
 74			$lookupColumn = ( @BitBase::verifyId( $this->mContentId ) ? 'lc.`content_id`' : 'ls.`structure_id`' );
 75			$lookupId = ( @BitBase::verifyId( $this->mContentId ) ? $this->mContentId : $this->mStructureId );
 76
 77			$bindVars[] = $lookupId;
 78			$selectSql = $joinSql = $whereSql = '';
 79			$this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
 80
 81			$query = "SELECT trg.*, ls.`root_structure_id`, ls.`parent_id`,
 82				lc.`title`, lc.`format_guid`, lc.`data`, lc.`user_id`, lc.`content_type_guid`,
 83				uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name,
 84				uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name $selectSql
 85				FROM `".BIT_DB_PREFIX."treasury_gallery` trg
 86					INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = trg.`content_id` )
 87					LEFT JOIN `".BIT_DB_PREFIX."liberty_structures` ls ON ( ls.`structure_id` = trg.`structure_id` )
 88					LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON ( uue.`user_id` = lc.`modifier_user_id` )
 89					LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON ( uuc.`user_id` = lc.`user_id` ) $joinSql
 90				WHERE $lookupColumn = ? $whereSql";
 91			$result = $this->mDb->query( $query, $bindVars );
 92
 93			if( $result && $row = $result->fetchRow() ) {
 94				$this->mInfo                  = $row;
 95				$this->mContentId             = $row['content_id'];
 96				$this->mStructureId           = $row['structure_id'];
 97				$this->mInfo['user']          = $row['creator_user'];
 98				$this->mInfo['real_name']     = ( isset( $row['creator_real_name'] ) ? $row['creator_real_name'] : $row['creator_user'] );
 99				$this->mInfo['display_name']  = BitUser::getTitle( $this->mInfo );
100				$this->mInfo['editor']        = ( isset( $row['modifier_real_name'] ) ? $row['modifier_real_name'] : $row['modifier_user'] );
101				$this->mInfo['display_url']   = $this->getDisplayUrl();
102				$this->mInfo['thumbnail_url'] = liberty_fetch_thumbnails( array(
103					'storage_path' => $this->getGalleryThumbBaseUrl(),
104					'mime_image'   => FALSE
105				));
106
107				// get extra information if required
108				if( $pExtras ) {
109					$this->mInfo['gallery_path']         = $this->getGalleryPath();
110					$this->mInfo['gallery_display_path'] = $this->getDisplayPath( $this->mInfo['gallery_path'] );
111				}
112				LibertyContent::load();
113
114				// parse the data after parent load so we have our html prefs
115				$this->mInfo['parsed_data'] = $this->parseData();
116			}
117		}
118		return( count( $this->mInfo ) );
119	}
120
121	/**
122	 * Load all uploaded items in this gallery
123	 *
124	 * @param array $pListHash ListHash is passed on to TreasuryItem::getList();
125	 * @access public
126	 * @return TRUE on success, FALSE on failure - populates $this->mItems
127	 */
128	function loadItems( &$pListHash ) {
129		$ret = FALSE;
130		if( $this->isValid() ) {
131			require_once( TREASURY_PKG_PATH.'TreasuryItem.php');
132			$treasuryItem = new TreasuryItem();
133			if( empty( $pListHash['gallery_content_id'] ) ) {
134				$pListHash['gallery_content_id'] = $this->mContentId;
135			}
136
137			if( $this->mItems = $treasuryItem->getList( $pListHash, $this->mStructureId )) {
138				$ret = TRUE;
139			} elseif( !empty( $treasuryItem->mErrors )) {
140				error_log( "Error loading treasury items: ".vc( $treasuryItem->mErrors ));
141			}
142		}
143		return $ret;
144	}
145
146	/**
147	 * Get list of all treasury galleries
148	 *
149	 * @param $pListHash contains array of items used to limit search results
150	 * @param $pListHash[sort_mode] column and orientation by which search results are sorted
151	 * @param $pListHash[find] search for a gallery title - case insensitive
152	 * @param $pListHash[max_records] maximum number of rows to return
153	 * @param $pListHash[offset] number of results data is offset by
154	 * @param $pListHash[title] gallery name
155	 * @param $pListHash[parent_id] gallery parent_id
156	 * @param $pListHash[get_sub_tree] get the subtree to every gallery
157	 * @access public
158	 * @return List of galleries
159	 **/
160	function getList( &$pListHash ) {
161		global $gBitDbType, $gBitSystem, $gBitUser;
162		LibertyContent::prepGetList( $pListHash );
163
164		$ret = $bindVars = array();
165		$selectSql = $joinSql = $orderSql = $whereSql = '';
166
167		if( @BitBase::verifyId( $pListHash['root_structure_id'] ) ) {
168			$whereSql .= empty( $whereSql ) ? ' WHERE ' : ' AND ';
169			$whereSql .= " ls.`root_structure_id`=? ";
170			$bindVars[] = $pListHash['root_structure_id'];
171		}
172
173		if( !empty( $pListHash['get_sub_tree'] ) ) {
174			$whereSql .= empty( $whereSql ) ? ' WHERE ' : ' AND ';
175			$whereSql .= " ls.`structure_id`=ls.`root_structure_id` ";
176		}
177
178		if( !empty( $pListHash['find'] ) ) {
179			$whereSql .= empty( $whereSql ) ? ' WHERE ' : ' AND ';
180			$whereSql .= " UPPER( lc.`title` ) LIKE ? ";
181			$bindVars[] = '%'.strtoupper( $pListHash['find'] ).'%';
182		}
183
184		if ( isset( $pListHash['parent_id'] ) ) {
185			$whereSql .= empty( $whereSql ) ? ' WHERE ' : ' AND ';
186			$whereSql .= ' ls.`parent_id` = ? ';
187			$bindVars[] = $pListHash['parent_id'];
188		}
189
190		if( !empty( $pListHash['sort_mode'] ) ) {
191			$orderSql .= " ORDER BY ".$this->mDb->convertSortmode( $pListHash['sort_mode'] )." ";
192		} else {
193			// default sort mode makes list look nice
194			$orderSql .= " ORDER BY ls.`root_structure_id`, ls.`structure_id` ASC";
195		}
196
197		// update query with service sql
198		$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
199
200		// get the number of files in this gallery
201		if( $gBitDbType != 'mysql' && $gBitDbType != 'mysqli' ) {
202			$subselect = ", (
203				SELECT COUNT( trm.`item_content_id` )
204				FROM `".BIT_DB_PREFIX."treasury_map` trm
205				WHERE trm.`gallery_content_id`=trg.`content_id`
206			) AS item_count";
207		} else {
208			$subselect = "";
209		}
210
211		// don't fetch trg.`is_private` for list, because it conflicts with gks.is_private
212		// and it's not used on list anyway
213		$query = "
214			SELECT trg.`content_id`, trg.`structure_id`,
215			ls.`root_structure_id`, ls.`parent_id`,
216			lc.`title`, lc.`data`, lc.`user_id`, lc.`content_type_guid`, lc.`created`, lc.`format_guid`, lch.`hits`,
217			uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name,
218			uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name $subselect $selectSql
219			FROM `".BIT_DB_PREFIX."treasury_gallery` trg
220				INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = trg.`content_id` )
221				LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_hits` lch ON ( lc.`content_id` = lch.`content_id` )
222				LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uue ON ( uue.`user_id` = lc.`modifier_user_id` )
223				LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uuc ON ( uuc.`user_id` = lc.`user_id` )
224				INNER JOIN `".BIT_DB_PREFIX."liberty_structures` ls ON ( ls.`structure_id` = trg.`structure_id` )
225				$joinSql
226			$whereSql
227			$orderSql";
228
229		$result = $this->mDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
230
231		if( !empty( $pListHash['get_sub_tree'] ) ) {
232			$struct = new LibertyStructure();
233		}
234
235		while( $aux = $result->fetchRow() ) {
236			$hasUserPerm = TRUE;
237			// check to see if we have premissions to do someing specific with this gallery
238			if( !empty( $pListHash['content_permission'] ) ) {
239				$gal = new TreasuryGallery( NULL, $aux['content_id'] );
240			if( !$gal->hasUserPermission( $pListHash['content_permission'] )) {
241					$hasUserPerm = FALSE;
242				}
243			}
244
245			if( $hasUserPerm ) {
246				$content_ids[]             = $aux['content_id'];
247				$aux['user']               = $aux['creator_user'];
248				$aux['real_name']          = ( isset( $aux['creator_real_name'] ) ? $aux['creator_real_name'] : $aux['creator_user'] );
249				$aux['editor']             = ( isset( $aux['modifier_real_name'] ) ? $aux['modifier_real_name'] : $aux['modifier_user'] );
250				$aux['display_name']       = BitUser::getTitle( $aux );
251				$aux['display_url']        = $this->getDisplayUrl( $aux['content_id'] );
252				$aux['display_link']       = $this->getDisplayLink( $aux['title'], $aux );
253				$aux['thumbnail_url']      = liberty_fetch_thumbnails( array(
254					'storage_path' => $this->getGalleryThumbBaseUrl( $aux['content_id'] ),
255					'mime_image'   => FALSE
256				));
257				// deal with the parsing
258				$parseHash['format_guid']  = $aux['format_guid'];
259				$parseHash['content_id']   = $aux['content_id'];
260				$parseHash['user_id']      = $aux['user_id'];
261				$parseHash['data']         = $aux['data'];
262				$aux['parsed_data']        = $this->parseData( $parseHash );
263
264				// sucky additional query to fetch item number without subselect
265				if( $gBitDbType == 'mysql' || $gBitDbType == 'mysqli' ) {
266					$item_count_query = "SELECT COUNT( trm.`item_content_id` ) FROM `".BIT_DB_PREFIX."treasury_map` trm WHERE trm.`gallery_content_id`=?";
267					$aux['item_count'] = $this->mDb->getOne( $item_count_query, array( $aux['content_id'] ));
268				}
269
270				if( !empty( $pListHash['get_sub_tree'] ) ) {
271					$aux['subtree']        = $struct->getSubTree( $aux['structure_id'] );
272				}
273				$ret[$aux['content_id']]   = $aux;
274			}
275		}
276
277		$query = "SELECT COUNT( lc.`title` )
278			FROM `".BIT_DB_PREFIX."treasury_gallery` trg
279				INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id` = trg.`content_id` )
280				LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uue ON ( uue.`user_id` = lc.`modifier_user_id` )
281				LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uuc ON ( uuc.`user_id` = lc.`user_id` )
282				INNER JOIN `".BIT_DB_PREFIX."liberty_structures` ls ON ( ls.`structure_id` = trg.`structure_id` )
283			$joinSql $whereSql";
284		$pListHash['cant'] = $this->mDb->getOne( $query, $bindVars );
285
286		LibertyContent::postGetList( $pListHash );
287		return $ret;
288	}
289
290	/**
291	 * Store TreasuryGallery
292	 *
293	 * @param $pParamHash contains all data to store the gallery
294	 * @param $pParamHash[title] title of the new gallery
295	 * @param $pParamHash[edit] description of the gallery
296	 * @param $pParamHash[root_structure_id] if this is set, it will add the gallery to this structure. if it's not set, a new structure / top level gallery is created
297	 * @param $pParamHash[parent_id] set the structure_id that will server as the parent in the structure
298	 * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
299	 * @access public
300	 **/
301	function store( &$pParamHash ) {
302		$this->mDb->StartTrans();
303		if( $this->verify( $pParamHash ) && LibertyContent::store( $pParamHash ) ) {
304			$table = BIT_DB_PREFIX."treasury_gallery";
305
306			// this really confusing, strange order way of saving items is due to strange behaviour of GenID
307			// probably has to do with not null default nextval('public.liberty_structures_id_seq'::text)
308			if( !empty( $pParamHash['update'] ) ) {
309				if( !empty( $pParamHash['gallery_store'] ) ) {
310					$result = $this->mDb->associateUpdate( $table, $pParamHash['gallery_store'], array( "content_id" => $this->mContentId ));
311				}
312				$pParamHash['structure_location_id'] = $this->mStructureId;
313			} else {
314				// update the gallery_store and structure_store content_id with the one from LibertyMime::store()
315				$pParamHash['structure_store']['content_id'] = $pParamHash['content_id'];
316				$pParamHash['gallery_store']['content_id'] = $pParamHash['content_id'];
317
318				// we need to store the new structure node now
319				global $gStructure;
320				// create new object if needed
321				if( empty( $gStructure ) ) {
322					$gStructure = new LibertyStructure();
323				}
324				$pParamHash['structure_location_id'] = $gStructure->storeNode( $pParamHash['structure_store'] );
325
326				// get the corrent structure_id
327				// structure_id has to be done like this since it's screwed up in the schema
328				$pParamHash['gallery_store']['structure_id'] =  $this->mDb->getOne( "SELECT MAX( `structure_id` ) FROM `".BIT_DB_PREFIX."liberty_structures`" );
329				$result = $this->mDb->associateInsert( $table, $pParamHash['gallery_store'] );
330			}
331
332			$this->mDb->CompleteTrans();
333
334			// process image upload
335			if( empty( $this->mErrors )) {
336				// now deal with the uploaded image
337				if( !empty( $pParamHash['thumb']['tmp_name'] )) {
338					$checkFunc = liberty_get_function( 'can_thumbnail' );
339					if( $checkFunc( $pParamHash['thumb']['type'] )) {
340						$fileHash = $pParamHash['thumb'];
341						$fileHash['dest_path'] = $this->getGalleryThumbBaseUrl();
342						$fileHash['source_file'] = $fileHash['tmp_name'];
343						liberty_clear_thumbnails( $fileHash );
344						liberty_generate_thumbnails( $fileHash );
345					} else {
346						$this->mErrors['thumb'] = tra( "The file you uploaded doesn't appear to be a valid image. The reported mime type is" ).": ".$pParamHash['thumb']['type'];
347					}
348				}
349			}
350
351			$this->load();
352		}
353		return( count( $this->mErrors ) == 0 );
354	}
355
356	/**
357	 * Verify, clean up and prepare data to be stored
358	 *
359	 * @param $pParamHash all information that is being stored. will update $pParamHash by reference with fixed array of itmes
360	 * @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
361	 * @access private
362	 **/
363	function verify( &$pParamHash ) {
364		// make sure we're all loaded up if everything is valid
365		if( $this->isValid() && empty( $this->mInfo ) ) {
366			$this->load();
367		}
368
369		// It is possible a derived class set this to something different
370		if( empty( $pParamHash['content_type_guid'] ) ) {
371			$pParamHash['content_type_guid'] = $this->mContentTypeGuid;
372		}
373
374		// make sure we know this is an update
375		if( @BitBase::verifyId( $this->mContentId ) ) {
376			$pParamHash['content_id'] = $this->mContentId;
377			$pParamHash['update']     = TRUE;
378		}
379
380		// ---------- Gallery Store
381		$pParamHash['gallery_store']['is_private'] = empty( $pParamHash['is_private'] ) ? 'n' : 'y';
382
383		// ---------- Content store
384		// check for name issues, truncate length if too long
385		if( !empty( $pParamHash['title'] ) )  {
386			if( !@BitBase::verifyId( $this->mContentId ) ) {
387				$pParamHash['content_store']['title'] = substr( $pParamHash['title'], 0, BIT_CONTENT_MAX_TITLE_LEN );
388			} else {
389				$pParamHash['content_store']['title'] = ( isset( $pParamHash['title'] ) ) ? substr( $pParamHash['title'], 0, BIT_CONTENT_MAX_TITLE_LEN ) : $this->mInfo['title'];
390			}
391		} else {
392			$this->mErrors['title'] = 'You must enter a name for this gallery.';
393		}
394
395		// sort out the description
396		if( $this->isValid() && !empty( $this->mInfo['data'] ) && empty( $pParamHash['edit'] ) ) {
397			$pParamHash['content_store']['data'] = '';
398		} elseif( empty( $pParamHash['edit'] ) ) {
399			unset( $pParamHash['edit'] );
400		} else {
401			$pParamHash['content_store']['data'] = substr( $pParamHash['edit'], 0, 250 );
402		}
403
404		// Individual gallery preference store - dealt with by LibertyContent::store();
405		if( empty( $pParamHash['preferences']['allow_comments'] )) {
406			$pParamHash['preferences']['allow_comments'] = NULL;
407		}
408		$pParamHash['preferences_store'] = !empty( $pParamHash['preferences'] ) ? $pParamHash['preferences'] : NULL;
409
410		// structure store
411		if( @BitBase::verifyId( $pParamHash['root_structure_id'] ) ) {
412			$pParamHash['structure_store']['root_structure_id'] = $pParamHash['root_structure_id'];
413		} else {
414			$pParamHash['structure_store']['root_structure_id'] = NULL;
415		}
416
417		if( @BitBase::verifyId( $pParamHash['parent_id'] ) ) {
418			$pParamHash['structure_store']['parent_id'] = $pParamHash['parent_id'];
419		} else {
420			$pParamHash['structure_store']['parent_id'] = NULL;
421		}
422
423		return( count( $this->mErrors ) == 0 );
424	}
425
426	/**
427	 * expunge a gallery
428	 *
429	 * @param array $pParamHash
430	 * @access public
431	 * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
432	 */
433	function expunge( $pForceDeleteItems = FALSE ) {
434		$ret = FALSE;
435		if( $this->isValid() ) {
436			$this->mDb->StartTrans();
437
438			// get all items that are part of the sub tree
439			require_once( LIBERTYSTRUCTURE_PKG_PATH.'LibertyStructure.php' );
440			$struct = new LibertyStructure();
441			$tree = $struct->getSubTree( $this->mStructureId );
442
443			// include the current id as well - needed when there are no sub-galleries
444			$galleryContentIds[] = $this->mContentId;
445			foreach( $tree as $node ) {
446				$galleryContentIds[] = $node['content_id'];
447			}
448			$galleryContentIds = array_unique( $galleryContentIds );
449
450			// Create Item Object
451			require_once( TREASURY_PKG_PATH.'TreasuryItem.php');
452			$itemObject = new TreasuryItem();
453
454			// Go through all galleries we want to remove
455			foreach( $galleryContentIds as $gid ) {
456				// make sure the gallery is fully loaded
457				$this->mContentId = $gid;
458				$this->load();
459
460				$itemContentIds = $this->mDb->getCol( "SELECT `item_content_id` FROM `".BIT_DB_PREFIX."treasury_map` WHERE `gallery_content_id`=?", array( $gid ) );
461				$itemContentIds = array_unique( $itemContentIds );
462
463				// Delete items in galleries
464				foreach( $itemContentIds as $iid ) {
465					if( $pForceDeleteItems ) {
466						// Remove item even if it exists in other galleries
467						$count = 1;
468					} else {
469						// Only delete item if it doesn't exist in other galleries
470						$count = $this->mDb->getOne( "SELECT COUNT( `item_content_id` ) FROM `".BIT_DB_PREFIX."treasury_map` WHERE `item_content_id`=?", array( $iid ) );
471					}
472
473					// Only delete item if it doesn't exist in other galleries
474					if( $count == 1 ) {
475						$itemObject->mContentId = $iid;
476						$itemObject->load();
477						if( !$itemObject->expunge() ) {
478							$this->mErrors['expunge'][] = $itemObject->mErrors;
479						}
480					}
481				}
482
483				// Next, we remove any icons if they exist
484				if( $thumbdir = $this->getGalleryThumbBaseUrl() ) {
485					@unlink_r( BIT_ROOT_PATH.$thumbdir );
486				}
487
488				// Now that all the items are gone, we can start nuking gallery entries
489				// remove gallery preferences
490				$query = "DELETE FROM `".BIT_DB_PREFIX."liberty_content_prefs` WHERE `content_id`=?";
491				$result = $this->mDb->query( $query, array( $this->mContentId ) );
492
493				// Remove map entries
494				$sql = "DELETE FROM `".BIT_DB_PREFIX."treasury_map` WHERE `gallery_content_id`=?";
495				$rs = $this->mDb->query( $sql, array( $gid ) );
496
497				// Remove gallery entry
498				$sql = "DELETE FROM `".BIT_DB_PREFIX."treasury_gallery` WHERE `content_id`=?";
499				$rs = $this->mDb->query( $sql, array( $gid ) );
500
501				// Let liberty remove all the content entries for this gallery
502				if( !LibertyContent::expunge() ) {
503					$errors = TRUE;
504				}
505
506				// Finally nuke the structure in liberty_structures
507				$struct->removeStructureNode( $this->mStructureId, FALSE );
508			}
509
510			if( empty( $errors ) ) {
511				$this->mDb->CompleteTrans();
512				$ret = TRUE;
513			} else {
514				$this->mDb->RollbackTrans();
515				$ret = FALSE;
516			}
517		}
518		return $ret;
519	}
520
521	/**
522	 * Returns HTML link to display a gallery or item
523	 *
524	 * @param $pTitle is the gallery we want to see
525	 * @param $pContentId content id of the gallery in question
526	 * @return the link to display the page.
527	 **/
528	function getDisplayLink( $pTitle=NULL, $pMixed=NULL ) {
529		global $gBitSystem;
530		if( empty( $pTitle ) && !empty( $this ) ) {
531			$pTitle = $this->getTitle();
532		}
533
534		if( empty( $pMixed ) && !empty( $this ) ) {
535			$pMixed = $this->mInfo;
536		}
537
538		$ret = $pTitle;
539		if( !empty( $pTitle ) && !empty( $pMixed ) ) {
540			if( $gBitSystem->isPackageActive( 'treasury' ) ) {
541				$ret = '<a title="'.htmlspecialchars( $pTitle ).'" href="'.TreasuryGallery::getDisplayUrl( $pMixed['content_id'] ).'">'.htmlspecialchars( $pTitle ).'</a>';
542			}
543		}
544		return $ret;
545	}
546
547	/**
548	 * Generates the URL to this gallery
549	 * @param $pContentId is the gallery we want to see
550	 * @return the link to display the page.
551	 **/
552	function getDisplayUrl( $pContentId=NULL, $pMixed=NULL ) {
553		global $gBitSystem;
554		$ret = NULL;
555		// try to get the correct content_id from anywhere possible
556		if( !@BitBase::verifyId( $pContentId ) && $this->isValid() ) {
557			$pContentId = $this->mContentId;
558		} elseif( !@BitBase::verifyId( $pContentId ) && !empty( $pMixed['content_id'] ) ) {
559			$pContentId = $pMixed['content_id'];
560		}
561
562		if( @BitBase::verifyId( $pContentId ) ) {
563			if( $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
564				$ret = TREASURY_PKG_URL.'gallery/'.$pContentId;
565			} else {
566				$ret = TREASURY_PKG_URL.'view.php?content_id='.$pContentId;
567			}
568		}
569		return $ret;
570	}
571
572	/**
573	 * Get the base path to where the gallery thumbnail is stored - create directory if needed
574	 *
575	 * @param numeric $pContentId Content ID of gallery in question
576	 * @access public
577	 * @return Path to thumbnail directory on success, FALSE on failure
578	 */
579	function getGalleryThumbBaseUrl( $pContentId = NULL ) {
580		$ret = FALSE;
581		if( !@BitBase::verifyId( $pContentId ) && $this->isValid() ) {
582			$pContentId = $this->mContentId;
583		}
584
585		if( @BitBase::verifyId( $pContentId ) ) {
586			// getStorageBranch is a private function, so this is a no-no but necessary until LA offers something better
587			$ret = LibertyMime::getStorageBranch( 'gallery_thumbnails/'.$pContentId, NULL, TREASURY_PKG_NAME );
588		}
589		return $ret;
590	}
591	
592	/**
593	* Returns the create/edit url to a gallery
594	* @param number $pContentId a valid content id
595	* @param array $pMixed a hash of params to add to the url  
596	*/
597	function getEditUrl( $pContentId = NULL, $pMixed = NULL ){
598		if( @BitBase::verifyId( $pContentId ) ) {
599			$ret = TREASURY_PKG_URL.'edit_gallery.php?content_id='.$pContentId;
600		} elseif( $this->isValid() ) {
601			$ret = TREASURY_PKG_URL.'edit_gallery.php?content_id='.$this->mContentId;
602		} else {
603			$ret = TREASURY_PKG_URL.'edit_gallery.php'.(!empty( $pMixed )?"?":"");
604		}
605		foreach( $pMixed as $key => $value ){
606			if( $key != "content_id" || ( $key == "content_id" && @BitBase::verifyId( $value ) ) ) {
607				$ret .= (isset($amp)?"&":"").$key."=".$value;
608			}
609			$amp = TRUE;
610		}
611		return $ret;
612	}
613}
614?>