Jump to content

add and move image from frontend


rushy
 Share

Recommended Posts

Hi all

Continuing my first project where I am creating and manipulating stuff from the frontend. Till now I've always added things like images from the backend, but in this project I need to add and move images from a frontend control. This is a photo album where images are stored in albums, each album being a page containing a Pageimages array in the usual way. So what I want to know is how do I move an image from one album (page) to another in the frontend? I just need some guidance on how to approach this.  I suppose I need to do a copy and delete - but how do I copy an image or images from one page to another? What function should I use to create a new image on an existing page? I include a code snippet from the server side of my delete image request and it works fine. I'd like to implement something similar for a move and upload new image request. 

Many thanks for any help. Paul 

<?php namespace ProcessWire;

// sanitize inputs as 1-line text
$action = $sanitizer->text($input->post('action'));
$instr = $sanitizer->text($input->post('input')); 
$sel = $input->post('selected');

// Expect JSON for image selected image list
$selected = json_decode($sel);
$nosel = count($selected);

$response = array(); // for building JSON response

switch($action) {
	// delete selected images
	case 'delete':
		$out = "<p>Deleted $nosel image(s)</p>";
	
		foreach($selected as $item){
			$album = $pages->get($item->album);
			$album->of(false);
			$out .= "<p>Image {$item->file} from album {$album->title}</p>";
			$album->images->delete($item->file);
			$album->of(true);
		}			
		
		$out .= saveUpdatedAlbums($pages, $selected);
			
		// add the response message for the delete
		$response['message'] = $out;				
		
	break;
...... 

// save any album that had an image deleted
function saveUpdatedAlbums($pages, $selected) {
	$cur = '';
	$out = '';
	foreach($selected as $item){
		$album = $pages->get($item->album);
		if($album->id != $cur) {
			$album->of(false);
			$album->save('images');
			$album->of(true);
			$cur = $album->id;	
			$out .= "<p>Updated album {$pages->get($cur)->title}</p>";
		}
	}
	
	return $out;
}

 

 

 

Link to comment
Share on other sites

SOLVED. As usual with processwire it's very easy. I just needed to do use add() function to add a copy to the album and then delete the relocated image. In the code below $instr contains the album name for destination album.

 

case 'move':	
		// Move image(s) from one album to another
		$out = "<p>Move $nosel images(s) to album $instr</p>";
		$newalb = $pages->get("/{$instr}/");
		$newalb->of(false);
		
		foreach($selected as $item){
			$album = $pages->get($item->album);
			$album->of(false);
			$image = $album->images->getFile($item->file);	
			$basename = $image->basename;	
			$filename = $image->filename();
			$out .= "<p>{$basename} in {$album->title}</p>";
			$newalb->images->add($filename);
			// delete the relocated image			
			$album->images->delete($image);			
		}					
		
		$newalb->save('images');
		$newalb->of(true);	
		
		$out .= saveUpdatedAlbums($pages, $selected);			
		$out .= "<p>Updated album $newalb->title</p>";
		
		$response['message'] = $out;
	break;			

 

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

×
×
  • Create New...