modified Album Select Module

Barre

Joined: 2004-07-14
Posts: 48
Posted: Fri, 2005-05-20 11:14

Hi.

Here’s my modified Album Select module that gives a new option in the admin interface to make the tree and dropdown list to actually use the sort order set in each album that was discussed in this topic

This may give some negative performance in large albums and/or slow hardware.

This is the changes I’ve done to the module:

file: module.inc

row 42

$this->setVersion('0.9.4');

row 64

    /**
     * @see GalleryModule::registerEventListeners()
     */
    function registerEventListeners() {
	GalleryCoreApi::registerEventListener(
	    'Gallery::ViewableTreeChange', new AlbumSelectModule());
	GalleryCoreApi::registerEventListener(
	    'GalleryEntity::save', new AlbumSelectModule());
    }

row 82

foreach (array('show' => 1, 'type' => 'select', 'sort' => 'manual',
		       'treeLines' => 1, 'treeIcons' => 0, 'treeCookies' => 0,
		       'treeExpandCollapse' => 0, 'treeCloseSameLevel' => 0) as $key => $value) {

row 161

$treeList = array();
$nodeId = 0;
$sorter = NULL;
switch ($params['sort']) {
	case "byAlbum" :
		list ($ret, $subAlbumsIds) = GalleryCoreApi::fetchChildAlbumItemIds($rootAlbum);
		foreach($subAlbumsIds as $subAlbumId){
			$ret = $this->_buildTree($subAlbumId, $treeList, $nodeId);
			if ($ret->isError()) {
			return array($ret->wrap(__FILE__, __LINE__), null);
			}
		}
		break;
	case "byTitle" :
		$sorter = new AlbumSelectTreeSorter($titles);
	case "manual" :
	default :
		$this->_parseTree($tree, $treeList, $sorter, $nodeId);
}

row 211

	if ($event->getEventName() == 'GalleryEntity::save') {
		list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'albumselect');
		if ($ret->isError()) {
			return array($ret->wrap(__FILE__, __LINE__), null);
		}
		$theEntity = $event->getEntity();
		if($theEntity->getEntityType() == 'GalleryAlbumItem' && $params['sort']=='byAlbum') {
	    GalleryDataCache::removeFromDisk(
			array('type' => 'module-data', 'module' => 'albumselect'));
		}
	}

row 247

/**
* Build template data for subalbum tree with album predefined sorting
* @private
*/
function _buildTree($albumId, &$treeList, &$nodeId, $parentNode=0, $depth=0) {
	list ($ret, $album) = GalleryCoreApi::loadEntitiesById($albumId);
	if ($ret->isError()) {
		return $ret;
	}	   
 	$treeList[] = array('id' => $album->getId(), 'nodeId' => ++$nodeId, 'parentNode' => $parentNode, 'depth' => $depth);
	list ($ret, $subAlbumsIds) = GalleryCoreApi::fetchChildAlbumItemIds($album);
	if ($ret->isError()) {
	return $ret;
	}
	$parentNode = $nodeId;
	foreach($subAlbumsIds as $subAlbumId){
		$ret = $this->_buildTree($subAlbumId, $treeList, $nodeId, $parentNode, $depth+1);
		if ($ret->isError()) {
			return $ret;
		}	   
	}
	return $ret;
}

file: AlbumSelectAdmin.inc

row 55

	    foreach (array('show', 'sort', 'treeLines', 'treeIcons', 'treeCookies',
			   'treeExpandCollapse', 'treeCloseSameLevel') as $key) {
			switch ($key) {
				case 'sort' :
					$ret = GalleryCoreApi::setPluginParameter('module', 'albumselect', 'sort', $form['sort']);
					break;
				default :
					$ret = GalleryCoreApi::setPluginParameter('module', 'albumselect', $key,
						(isset($form[$key]) && $form[$key]) ? 1 : 0);
					break;
			}
		if ($ret->isError()) {
		    return array($ret->wrap(__FILE__, __LINE__), null);
	    }
            }

file AlbumSelectAdmin.tpl

row 45

  <input type="radio" id="rbSortManual"{if $form.sort eq "manual"} checked="checked"{/if}
   name="{g->formVar var="form[sort]"}" value="manual"/>
  <label for="rbSortManual">
    {g->text text="Use manual sort order of albums"}
  </label>
  <br/>

  <input type="radio" id="rbSortTitle"{if $form.sort eq "byTitle"} checked="checked"{/if}
   name="{g->formVar var="form[sort]"}" value="byTitle"/>
  <label for="rbSortTitle">
    {g->text text="Sort albums by title"}
  </label>
  <br/>
  <input type="radio" id="rbSortAlbum"{if $form.sort eq "byAlbum"} checked="checked"{/if}
   name="{g->formVar var="form[sort]"}" value="byAlbum"/>
  <label for="rbSortAlbum">
    {g->text text="Use sort settings defined in album (could affect performance)"}
  </label>
AttachmentSize
albumselect.zip56.18 KB
 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Fri, 2005-05-20 11:28

very nice. and see the answer to your event question in the other thread to delete the cache on item->save (sortOrderBy != lastsorOrderBy || sortOrderDirection != lastSortOrderDirection) events.

 
Barre

Joined: 2004-07-14
Posts: 48
Posted: Fri, 2005-05-20 11:51

Thanks for the info valiant, I've updated my original post and attachment.

Cheers

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Fri, 2005-05-20 14:28

Barre, nice work.. any chance you could update the unit tests to cover your changes? lib/tools/phpunit/index.php?filter=albumselect
That will certainly help get your change into cvs sooner :-)

 
Barre

Joined: 2004-07-14
Posts: 48
Posted: Fri, 2005-05-20 14:54

mindless, i've updated the phpunit tests and uploaded a new zip file with them.

Cheers

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-05-21 06:02

Barre, I'm committing changes based on your code.. here's what I added:

  • in module upgrade change current 0/1 setting for sort to manual or title.
  • your code loaded the whole album tree using the current method (one big query) just to get all the album titles, then proceeds to query each album to get the right sort order.. i changed this to just process all the albums and get the titles as it goes.
  • currently we bail out if user doesn't have view permission on the root album.. do this for album sort order too.
  • also check if the orderBy or orderDirection fields were change in the GalleryEntity::save event handler, so we don't clear the cache unless the sort order for the album was changed.
  • set a particular sort order and added more test subalbums in the unit test so it can verify the album sort order is actually being applied.

Thanks![/]

 
kost

Joined: 2005-06-17
Posts: 3
Posted: Thu, 2005-08-18 14:31

Thanks, it works fine.

But also, how can I limit the depth of showing albums. E.g., I have root album -> sub-album -> sub-sub-album, and I want to show in album tree only root album and subalbum, something like in this topic.

I can show only two levels in "manual sort order" and "Sort albums by title".

How to do it with "sort settings defined in album"?

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2005-08-18 14:57

kost, sort on per-album basis is an option in Site Admin / Album Select.. upgrade your G2 if you don't see it.
There is no option for tree depth, but you can modify your code to do this.. edit modules/albumselect/Callbacks.inc and change:
GalleryCoreApi::fetchAlbumTree(null, null, $userId);
to:
GalleryCoreApi::fetchAlbumTree(null, 2, $userId);
(for depth 2).
This will work for manual or title sort.
For album sort, after the line with:
function _buildTree
add:
if ($depth > 2) return GalleryStatus::success();

 
kost

Joined: 2005-06-17
Posts: 3
Posted: Tue, 2005-08-23 12:08

I tried to upgrade the whole gallery, it gives me a lot of errors. I think, it because I made some changes in core files, to translate Gallery. When I try to open any page after upgrade, it hangs up my server. I return to older version.

Oh, problem solved!
I add
function _buildTree($albumId, &$treeList, &$nodeId, $parentNode=0, $depth=0) {
if ($depth > 1) return GalleryStatus::success();

and it works like I want.

Thanks!