Upload images through own Controller

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Bananamoon
Posts: 26
Joined: 22 Jun 2015, 09:12

Upload images through own Controller

Post by Bananamoon » 07 Jul 2015, 12:35

Hello, and sorry to bother you once again! :-)

I'm trying to upload images through my own controller and my custom admin, so far I'm not having luck with getting the images uploaded. I found a _createItem() function, but I don't think I can access this through my custom Controller.

here's a bit of code:

Code: Select all

$mediaManager = \MShop_Factory::createManager($context, 'media');
 $mediaItem = $mediaManager->createItem(array());

$img = $request->file('image');

$mediaItem->setTypeId(1); //Product Type
$mediaItem->setDomain('product');
$mediaItem->setUrl($img);
$mediaItem->setPreview($img);
$mediaItem->setStatus($request->get('status'));
$mediaItem->setMimeType($img->getMimeType());

$mediaManager->saveItem($mediaItem, true);
Could you maybe point me in the right direction?

Thanks a lot!

User avatar
aimeos
Administrator
Posts: 7836
Joined: 01 Jan 1970, 00:00

Re: Upload images through own Controller

Post by aimeos » 07 Jul 2015, 13:21

Bananamoon wrote:Hello, and sorry to bother you once again! :-)
Don't excuse yourself, it's nice to see that you are still working on your project and already achieved so much! :-)
Bananamoon wrote: I'm trying to upload images through my own controller and my custom admin, so far I'm not having luck with getting the images uploaded. I found a _createItem() function, but I don't think I can access this through my custom Controller.

Code: Select all

$mediaManager = \MShop_Factory::createManager($context, 'media');
$mediaItem = $mediaManager->createItem(array());

$img = $request->file('image');

$mediaItem->setTypeId(1); //Product Type
$mediaItem->setDomain('product');
$mediaItem->setUrl($img);
$mediaItem->setPreview($img);
$mediaItem->setStatus($request->get('status'));
$mediaItem->setMimeType($img->getMimeType());

$mediaManager->saveItem($mediaItem, true);
If this is all code, you forgot to move your uploaded file to the new directory (somewhere in ./public/) first:

Code: Select all

$request->file('image')->move($destinationPath, $fileName);
Otherwise, it will get cleaned up before the PHP process terminates. For setUrl() and setPreview() you need the path relative to the ./public/ directory. The __toString() method of the file object will return the absolute path which won't work in the front-end later on.

Some more hints: createItem() accepts no parameters and the second parameter of saveItem() is true by default. To retrieve the dynamic type ID for any items, you should use this code instead:

Code: Select all

/**
 * Returns the attribute type item specified by the code.
 *
 * @param string $prefix Domain prefix for the manager, e.g. "media/type"
 * @param string $domain Domain of the type item
 * @param string $code Code of the type item
 * @return MShop_Common_Item_Type_Interface Type item
 * @throws Exception If no item is found
 */
protected function getTypeItem( $prefix, $domain, $code )
{
	$manager = MShop_Factory::createManager( $this->_getContext(), $prefix ); // you need the context from somewhere
	$prefix = str_replace( '/', '.', $prefix );

	$search = $manager->createSearch();
	$expr = array(
		$search->compare( '==', $prefix . '.domain', $domain ),
		$search->compare( '==', $prefix . '.code', $code ),
	);
	$search->setConditions( $search->combine( '&&', $expr ) );
	$result = $manager->searchItems( $search );

	if( ( $item = reset( $result ) ) === false ) {
		throw new Exception( sprintf( 'No type item for "%1$s/%2$s" in "%3$s" found', $domain, $code, $prefix ) );
	}

	return $item;
}
Then, you can use

Code: Select all

getTypeItem( 'media/type', 'product', 'default' )->getId();
and are not dependent on a fixed ID which will change for sure in the live database ;-)

Bananamoon
Posts: 26
Joined: 22 Jun 2015, 09:12

Re: Upload images through own Controller

Post by Bananamoon » 08 Jul 2015, 07:36

Superb! Thanks for the help :-)

Post Reply