Help moving file from one album to another using PHP!

Help moving file from one album to another using PHP!

Postby ormstont on Wed May 26, 2010 11:51 am

Hi,

I'm trying to move a file that was uploaded via XML-RPC. It was of course added to the first gallery on the Blog so I wanted to write a small bit of PHP which would move the resource to a gallery of choice. I have written some code (see below) to try and do this but I can't get it to work.

I call the GalleryResource.setAlbum($album) method on the resource to be moved and that doesn't throw any errors. In fact if you check the album of the resource before and after calling this it says that it has changed. However, if I look at the Resource Centre there is no change (the resource is still in its original gallery), the same as if I look at the folders on the blog.

Please help me solve this problem as I'm so close to being able to do what I'm aiming for and don't want to stumble at this last hurdle. I'm very new to PHP so I'm sure there's some tiny bug in my code ruining everything, can anyone spot it?

Thank you so very much in advance!

*Update* The setAlbumId method does work and successfully moves the resource as seen from the Resource Center. However, the change is not reflected on the blog itself (the destination gallery shows no extra file).

Code: Select all
<?php
   /*
   Resource Moving Script
   Version: 1.0
   Release Date: 26/05/10
   
   This script is designed to move a resource from whichever album it is in to another album.
   This is to allow XML-RPC to upload images and then have these moved by command to the relevant gallery.
   */

   $resourcename=$HTTP_GET_VARS['resourcename'];
   $destination=$HTTP_GET_VARS['destination'];
   $user=$HTTP_GET_VARS['user'];
   $password=$HTTP_GET_VARS['pass'];
   $blogid=$HTTP_GET_VARS['blogid'];
   
   echo "Blog Resource Mover<br>";
   echo "------------------------<br>";

   // define the entry point
    if (!defined( "PLOG_CLASS_PATH" )) {
       define( "PLOG_CLASS_PATH", dirname(__FILE__)."/");
    }
   
   include( PLOG_CLASS_PATH."class/bootstrap.php" );
    lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
    lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
    lt_include( PLOG_CLASS_PATH."class/file/file.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
    lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );
    lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" );
    lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
    lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresources.class.php" );
    lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbums.class.php" );
    lt_include( PLOG_CLASS_PATH."plugins/moblog/class/log/mobloglogger.class.php" );
    lt_include( PLOG_CLASS_PATH."plugins/moblog/class/moblog/moblogrequest.class.php" );
    lt_include( PLOG_CLASS_PATH."plugins/moblog/class/moblog/moblogresponse.class.php" );
   lt_include( PLOG_CLASS_PATH."plugins/moblog/class/moblog/moblogconstants.properties.php" );
   lt_include( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
   lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" );
   lt_include( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
   lt_include( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );   
   lt_include( PLOG_CLASS_PATH."class/file/fileupload.class.php" );

    //
    // first, try to authenticate the user
    //
    $users = new Users();
    if( $users->authenticateUser( $user, $password ))
   {
      $userInfo = $users->getUserInfoFromUsername( $user );
      echo " - User ".$user." authenticated.<br>";
   }
    else
   {
        echo " - User ".$user." did not authenticate correctly, exiting!";
      die();
    }
   
    //
    // if user was authenticated, then proceed... and the first thing we should do
    // is see if the blog id is correct and if the user has permissions in that
    // blog
    //
    $blogs = new Blogs();
   $blogInfo = $blogs->getBlogInfo( $blogid );
   if( !$blogInfo )
   {
      echo " - Blog ".$blogid." is not valid, exiting!";
      die();
   }
   else
   {
      echo " - Blog ".$blogid." (".$blogInfo->getBlog().") successfully loaded.<br>";
   }
   
   //Next task is to check if the target gallery exists
   $albums = new GalleryAlbums();
   $userAlbums = $albums->getUserAlbums($blogid);
   $parentalbumid = -1;
   $destinationalbum = null;
   foreach ($userAlbums as $album)
   {
      if(strcmp($album->getName(),$destination) == 0)
      {
         $destinationalbum = $album;
         $parentalbumid = $album->getId();
      }
   }
   
   if( $parentalbumid == -1 )
   {
        echo " - Target album '".$destination."' does not exist, exiting!";
      die();       
    }
   else
   {
      echo " - Target album '".$destination."' found with ID ".$parentalbumid.".<br>";
   }
   
   //Then check if the indicated resource exists
   $masterreslist = new GalleryResources();
   $existres = null;
   $resalbum = null;
   if($masterreslist->isDuplicatedFilename($resourcename))
   {
      $existres = $masterreslist->getResourceFile($blogid,$resourcename);
      $resalbum = $albums->getAlbum($existres->getAlbumId(), $blogid);
      echo " - Resource '".$resourcename."' found with ID ".$existres->getId()." in album '".$resalbum->getName()."'.<br>";
   }
   else
   {
      echo " - Resource '".$resourcename."' does not exist, exiting!";
      die();
   }
   
   //Finally perform the actual move
   $existres->setAlbum($destinationalbum);
   echo $masterreslist->updateResource($existres);
   echo " - Move of '".$existres->getFileName()."' to album '".$destinationalbum->getName()."' performed.";
?>
ormstont
 
Posts: 5
Joined: Tue Mar 10, 2009 6:25 am
LifeType Version: lifetype-1.2.6_r6169

Re: Help moving file from one album to another using PHP!

Postby jondaley on Thu May 27, 2010 11:00 am

Perhaps it is just a caching issue?
(the admin side doesn't cache a lot of things)

Try erasing the tmp/1 (or whatever your blogId is) directory.

If that makes the resource show up properly, then you'll just need a function call to clean the cache.

CacheControl::resetBlogCache( $this->_blogInfo->getId());
jondaley
Lifetype Expert
 
Posts: 6169
Joined: Thu May 20, 2004 6:19 pm
Location: Pittsburgh, PA, USA
LifeType Version: 1.2.11 devel branch

Re: Help moving file from one album to another using PHP!

Postby ormstont on Fri May 28, 2010 8:39 am

Ah...that works perfectly - and solves a bunch of other problems I was having too with things in the gallery not refreshing!! Thank you so much, and sorry for being such a newbie at all of this!
ormstont
 
Posts: 5
Joined: Tue Mar 10, 2009 6:25 am
LifeType Version: lifetype-1.2.6_r6169


Return to Other Problems

cron