Page 1 of 1

Upload images through own Controller

Posted: 07 Jul 2015, 12:35
by Bananamoon
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!

Re: Upload images through own Controller

Posted: 07 Jul 2015, 13:21
by aimeos
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 ;-)

Re: Upload images through own Controller

Posted: 08 Jul 2015, 07:36
by Bananamoon
Superb! Thanks for the help :-)