pwired Posted August 25, 2016 Share Posted August 25, 2016 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 More sharing options...
horst Posted August 25, 2016 Share Posted August 25, 2016 (edited) foreach($pageOld->images as $image) { $pageNew->images->add($image->filename); } $pageNew->save(); $pageOld->images->deleteAll(); see: http://cheatsheet.processwire.com/files/page-files-multiple-wirearray/files-deleteall/ Edited August 25, 2016 by horst updated with @netcarver s suggestion :) 5 Link to comment Share on other sites More sharing options...
netcarver Posted August 25, 2016 Share Posted August 25, 2016 You might need to do this after the foreach loop... $pageNew->save(); 3 Link to comment Share on other sites More sharing options...
pwired Posted August 25, 2016 Author Share Posted August 25, 2016 You guys make it look so easy Thanks for the example code and that link. Code works perfectly Link to comment Share on other sites More sharing options...
horst Posted August 25, 2016 Share Posted August 25, 2016 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(); } 6 Link to comment Share on other sites More sharing options...
pwired Posted August 26, 2016 Author Share Posted August 26, 2016 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. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 26, 2016 Share Posted August 26, 2016 Tracy Debugger has a console panel, where you can run one-time code to be executed. 3 Link to comment Share on other sites More sharing options...
tpr Posted August 26, 2016 Share Posted August 26, 2016 I'm thinking about to give this feature a GUI somehow in adminonsteroids. Any (simple) idea is welcomed. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now