Jump to:

7938 Posts in 1541 Topics by 945 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Image Gallery: Creating too many folders...

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 3241 Views
  • Terry Apodaca
    Avatar
    Community Member
    109 Posts

    Image Gallery: Creating too many folders... Link to this post

    I am seeing some weird stuff with Image Gallery. I have installed all the latest versions as per UncleCheese, and every time i add a new Image Gallery page, it creates a folder caller "new ImageGallery page" and then when i go to modify my page and change it to say....just "Gallery" it will create THREE (yes 3) new folders called "Gallery".

    So, when i go to actually add images, and save/pulblish the page it errors out because it can't find the correct stuff. it's doing this on both my dev site (that still just uses the black_candy theme, and my clients site. I can then go into the Files & Images and select "Look for new files" and it will clean it up for me. But...I don't want my client to see that or have to do that every time they make a change to the gallery.

    No idea what's causing this!!!

  • Terry Apodaca
    Avatar
    Community Member
    109 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    Forgot to metion:

    SS 2.4.3 and latest trunk versions of DOM, SWFUpload, Uploadify, and Image Gallery

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    Yes, I have been going crazy over this bug.

    Did you ever find a fix for this Terry?

  • Anatol
    Avatar
    126 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    Hi,

    I had similar problems here. When I renamed the Image Gallery page from the default "Image Gallery" to "Gallery", every time I saved the gallery page it created two additional folders called "Gallery" in the "Files & Images" section. I never got an error message as Terry describes, but I may have manually renamed the folder with album sub folders to "Gallery". I don't remember. Still I got these two additional "Gallery" folders on every save.

    I could find a fix that works for my site: I checked the database table ImageGalleryPage (which contains the GalleryPage config settings) and in my case the field RootFolderID had the value '18'. I checked this against the File table which RootFolderID refers to, but there was no entry with ID = '18'. Instead I noticed that the Gallery folder in my case had ID '17'. So all I did was to change ImageGalleryPage.RootFolderID to 17. Now I can save and no new folders are created. Everything works fine. I can add/delete images and albums.

    In my case that's sufficient because I only intend to have this one Gallery page anyway. If you want to fix this in your project just make sure that the ID numbers are most likely different, so just double-check.

    I don't have a bugfix for the code, but it seems that the unnecessary folders are created in ImageGalleryPage.php, in the ImageGalleryPage class, function checkFolder.

    function checkFolder() {
          if( ! $this->RootFolderID ) {
             $galleries = Folder::findOrMake('image-gallery');
             $galleries->Title = 'Image Gallery';
             $galleries->write();
             $folder = Folder::findOrMake('image-gallery/' . $this->URLSegment);
             $folder->Title = $this->Title;
             $folder->setName($this->Title);
             $folder->write();
             
             $this->RootFolderID = $folder->ID;
             $this->write();
             
             $this->requireDefaultAlbum();
             FormResponse::add( "\$( 'Form_EditForm' ).getPageFromServer( $this->ID );" );
          }
          else {
             $this->RootFolder()->setName($this->Title);
             $this->RootFolder()->write();
          }
             
       }

    In my case it definitely didn't find the RootFolderID in the highlighted line, so it created a new one. So there seems a little bug that doesn't update or set the RootFolderID correctly somewhere else in the code.

    Just to add this to the issue as well. When I create a new album now it creates the folder structure /image-gallery/Gallery/my-album-name/

    So my assets folder structure is

    - Gallery
    -- album1
    -- album2

    - image-gallery
    --Gallery
    --- album3

    (I created album1 and album2 before I renamed the gallery page from "Image Gallery" to "Gallery".)

    This is not a big issue and I could probably just recreate album 1 and album 2 and delete the old ones if I feel the assets structure is so important, but just to make people aware. The whole thing is probably just a small issue to fix, but I couldn't find a solution yet.

    Cheers!
    Anatol

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    Cheers Anatol.

    Yes it looks like my issue is exactly what you describe, I have changed my RootFolderID to match and it has solved the bug of Gallery folder appearing in the File and Images tab multiple times.

    Maybe something that should be added into the issues on Uncle Cheese's github repository for imagegallery module?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    If there's a patch that fixes this problem, please submit a pull request!

    --------------------
    SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

  • camfindlay
    Avatar
    Forum Moderator
    148 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    Havent yet tracked down the underlying cause as to why the records get out of sync yet... if I do however, totally will get a pull request on the way

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: Image Gallery: Creating too many folders... Link to this post

    I have done some work on fixing the multiple folders being created. Im not sure what a "pull request" is but I can past the functions I updated.

    In the onAfterWrite() function the parrent::onAfterWrite() was being called after if($this->ID) $this->checkFolder(); There may be a good reason for this and I would like to know what it is. But I put the Parent call first as it makes most sense to me.

    The other function I made changes to is the checkFolder() function where I really just put in a lot more checks for existing files and tried to make it a lot more robust. I am probably duplicating things that are done by Folder::findormake() but I don't trust that function any more.

    function onAfterWrite()
    {
    parent::onAfterWrite();
    if( $this->ID ) $this->checkFolder();
    }

    /**
    * checkFolder
    * Makes sure that the GalleryPage has it's own folder and that there is a Root folder for all the galleries.
    * @return void
    */
    function checkFolder() {
    // Sometimes I have found that the Page has a Folder ID for a folder that does not actually exist so I
    // check here to make sure that if the ID exists so also does the entry in the Files table and that it also exists
    // in the filesystem.
    if ($this->RootFolderID && is_numeric($this->RootFolderID ))
    {
    $supposedFolder = DataObject::get_by_id("Folder", $this->RootFolderID ); // If the ID exists, find if there is a folder in the database of that ID
    if ($supposedFolder && file_exists($_SERVER{'DOCUMENT_ROOT'} . "/" . $supposedFolder->Filename)) // Make sure the Folder exists in the file system.
    {
        $supposedFolder->Title = $this->Title;
        $supposedFolder->setName($this->URLSegment); // If all is good so far make sure the Folder has the right name.
        $supposedFolder->write();
    }
    else
    {
    // If the ID exists and the database knows about the Folder but for some reason it was deleted from the filesystem,
    // make a new folder and set the new ID on the GalleryPage.
    $folder = Folder::findOrMake('Galleries/' . $this->URLSegment);
    $folder->Title = $this->Title;
    $folder->setName($this->URLSegment); //URLSegment is safe to set Name since it won't have spaces etc. Name is used for Filename as well.
    $folder->write();

    $this->RootFolderID = $folder->ID;
    $this->write();
    }
    }
    else if( !$this->RootFolderID ) // If there is no folder anywhere then go ahead and make the dang thing.
    {
    $galleries = Folder::findOrMake('Galleries');
    $galleries->Title = 'Image Gallery';
    $galleries->write();
    $folder = Folder::findOrMake('Galleries/' . $this->URLSegment);
    $folder->Title = $this->Title;
    $folder->setName($this->URLSegment);
    $folder->write();

    $this->RootFolderID = $folder->ID;
    $this->write();
    }
    if ($this->Albums()->Count() == 0) { //Don't just make a default album willy nilly. Check to make sure there isn't an album already.
          $this->requireDefaultAlbum();
       }
    FormResponse::add( "\$( 'Form_EditForm' ).getPageFromServer( $this->ID );" ); // I wish I knew what this was for.
    }

    3241 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.