Jump to content

Moving pictures from one page to another


pwired
 Share

Recommended Posts

Hi I have a bunch of pictures in a page called project. Now a project is finished and a new project will be started. That means I first have to copy all the pictures to a dedicated page where the finished project will be shown and then delete all the pictures in the page project to make room for new pictures. What would be the best way to copy a bunch of pictures from one page to another ?

Link to comment
Share on other sites

To be a bit more secure or serious, you want to check if everything went ok, before you delete all images on the old page.

If I undestood right, the new page images should be an exact copy of the old one. Therefore we can delete all (optionally) images on the new page first.

Then, after adding all images to it, we compare both collections. When on PHP >= 5.6.0, we can compare the ->items(), what contains a list of all image basenames, what is fine!

If we, unfortunately, on PHP lower then 5.6, we can at least compare the count of images, or have to loop through both collections and compare the basenames one by one.

$pageNew->of(false);
$pageNew->images->deleteAll();
$pageNew->save();
foreach($pageOld->images as $image) {
    $pageNew->images->add($image->filename);
}
$pageNew->save();

// now check for success or failures
$success = version_compare(phpversion(), '5.6.0', '>=') ? $pageNew->images->items() === $pageOld->images->items() : $pageNew->images->count() === $pageOld->images->count();
if($success) {
    // delete images on old page
    $pageOld->of(false);
    $pageOld->images->deleteAll()
    $pageOld->save();
}

 

  • Like 6
Link to comment
Share on other sites

Thanks Horst for adding this to it. I tried it and all code is working. Moving around pictures in the back end seems so easy now.

I simply added the code at the beginning of my home page template file, opened the website and reloaded the home page. Which makes me wonder now. Maybe it is an idea to reserve a template file in processwire specially for executing code occasionally like in this case ?

Anyway, the more I learn about the api, like in this case, the more it becomes a pleasure to work with processwire.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...