Page 1 of 1

Attributes images

Posted: 30 Dec 2017, 19:56
by Travin
I want to show attributes media (images) at the product page.
I'm modyfying the code snippet at client\html\templates\common\partials\attribute-default.php:

Code: Select all

<?php foreach( $attributes as $id => $attribute ) : ?>
// Some code here...
<?= $enc->html( $attribute->getName(), $enc::TRUST ); ?>
// Some code there
<?php endforeach; ?>
Now i want to place there image:

Code: Select all

<?php foreach( $attributes as $id => $attribute ) : ?>
// Some code here...
<?= $enc->html( $attribute->getName(), $enc::TRUST ); ?>
// Some code there
    <?php foreach( $attribute->getListItems( 'media', 'default' ) as $listItem ) : ?>
        <?php if( ( $mediaItem = $listItem->getRefItem() ) !== null ) : ?>
            <?=  $this->partial('common/partials/media-src.php', array( 'item' => $mediaItem, 'boxAttributes' => array( 'class' => 'media-item' ) )); ?>
        <?php endif; ?>
    <?php endforeach; ?>
<?php endforeach; ?>
Please tell me is it a good way to fetch attribute media? :?
It works for me but not normal. For example, i create Materials: wood, steel, glass, grass. Now i see only steel image at each item. And only wood and steel works fine. :shock:

Re: Attributes images

Posted: 31 Dec 2017, 11:49
by aimeos
Your code is OK, even if you can shorten it to

Code: Select all

    <?php foreach( $attribute->getRefItems( 'media', 'default', 'default' ) as $mediaItem ) : ?>
        <?=  $this->partial('common/partials/media-src.php', array( 'item' => $mediaItem, 'boxAttributes' => array( 'class' => 'media-item' ) )); ?>
    <?php endforeach; ?>
Please check if the object in $attribute contains the referenced items because they are only available in $this->get( 'attributeItems', [] ) :
- https://github.com/aimeos/ai-client-htm ... lt.php#L11
- https://github.com/aimeos/ai-client-htm ... lt.php#L24

Re: Attributes images

Posted: 31 Dec 2017, 16:40
by Travin
Hey guys
I think i found a bug. I want to fix it and do my first contribution. But i have a problem - it's my first Aimeos project and my first Laravel project. My past experience is at Yii Framework.
Please help me to understand Aimeos.

I created attribute Wood. And attached wood picture to it.
Then i clicked Save & Copy. So now i have Wood and Wood copy.
I want to change "Wood copy" title to "Steel" title and want to change picture of the copy to the new one with steel.
When i did so, both of images are changed. So there are just one images stack to all the copies. If we change image at one copy, it applies to each one. I think it's a bug :shock:

My investigation shows me that we have the same refid for each one row at mshop_attribute_list table.
I think we should do a copy of required rows at mshop_media, not just copy refid to mshop_attribute_list.

What do you think? Is it a good descision? What i have to do next to fix it?

Re: Attributes images

Posted: 31 Dec 2017, 17:08
by Travin
Or it just need to be explained clearly to a user at the media page. What he have to deattach media item and attach a new one if he don't want to change picture globally at all copies

Re: Attributes images

Posted: 01 Jan 2018, 23:39
by aimeos
It's an unexpected behavior that feels wrong for the users and therefore we should fix it.

The main problem is that the images are referenced. If a new image is uploaded, the link in the referenced media item is replaced by the new one. The correct behavior would be to create a new media item with the link to the uploaded file and reference that new media item. The old one can be removed if it's not referenced any more:
https://github.com/aimeos/ai-admin-jqad ... d.php#L450

We appreciate any pull request fixing the problem :-)

Re: Attributes images

Posted: 11 Jan 2018, 07:10
by Travin
Sorry guys I was busy :?
Don't know how to do pull request right. I will learn.
My solution is to create new media item anyway, and not copy existing media item:

Code: Select all

if( !isset( $listItems[$listid] ) )
			{
                // There are new media file at the form. Manage it
                // Let's create a new one media item
			    $litem = clone $listItem;
                $item = clone $mediaItem;

				if( ( $refId = $this->getValue( $data, 'product.lists.refid/' . $idx ) ) !== null ) {
                    // There are existing $refId
				    $existingMediaItem = $mediaManager->getItem( $refId ); // get existing item data
                    $item->setUrl( $existingMediaItem->getUrl() );
                    $item->setPreview( $existingMediaItem->getPreview() );
				}
			}
			else
			{
                // There are old media
				$litem = $listItems[$listid];
				$item = $litem->getRefItem();
			}
instead of

Code: Select all

if( !isset( $listItems[$listid] ) )
			{
				$litem = clone $listItem;

				if( ( $refId = $this->getValue( $data, 'product.lists.refid/' . $idx ) ) !== null ) {
					$item = $mediaManager->getItem( $refId ); // copy existing item
				} else {
					$item = clone $mediaItem;
				}
			}
			else
			{
				$litem = $listItems[$listid];
				$item = $litem->getRefItem();
			}

			if( ( $file = $this->getValue( $files, $idx ) ) !== null && $file->getError() !== UPLOAD_ERR_NO_FILE ) {
				$cntl->add( $item, $file );
			}
It works and we don't have problem while copying products. After copying we have different media items at different products.
But when i delete one of images, i need to add condition to delete image from server just if it don't using by another media item. It's somewhere at

Code: Select all

$cntl->add( $item, $file );
omg i feel silly

Re: Attributes images

Posted: 11 Jan 2018, 14:12
by aimeos
You should not create a new media file each time the product is saved. Instead, only when a new file is uploaded i.e. create a new media item just before "$cntl->add()". The old one is then removed automatically at the end of the method and also unreferenced image files are delete there.

Re: Attributes images

Posted: 12 Jan 2018, 11:44
by Travin
I understand it should not create a new media file each time. And it does not do so. I checked it twice right now.
It's create media file only if new file uploaded. And create media item only if new row at the form added or we copying existing product. I can send you link to the test website and db password at PM if you want.

Re: Attributes images

Posted: 12 Jan 2018, 11:47
by Travin
Maybe i don't understand something, but it works correctly for me, excepts next problem with deleting file we referenced at two mshop_media rows.

Re: Attributes images

Posted: 12 Jan 2018, 18:09
by aimeos
OK, so can you create a pull request with your changes in the aimeos/ai-admin-jqadm repository?
https://aimeos.org/docs/Developers/Git_workflow