How to attach files to user by code ?

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!
MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

How to attach files to user by code ?

Post by MikaelNazarenko » 15 Jul 2019, 10:12

Hi, guys! While I am going deeply into Aimeos i need more custom things to do. Here I need to ask you, probably it will be faster than I figure out it by myself. Also, I hope, my questions will be useful for somebody.

My environment is the following:
Laravel Framework 5.8.26
aimeos-laravel 2019.04
PHP Version 7.2.19-1+ubuntu18.04.1+deb.sury.org+1

How can I attach several files to specific user by code ? I don't need attach uploaded files, but I need attach some existing files from filesystem. How can I do it ? In database I see mshop_media table, so it must be possible. Can you, please, provide me example code ?

Thank you a lot!

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

Re: How to attach files to user by code ?

Post by aimeos » 15 Jul 2019, 13:54

If this is releated to your former post, please have a look at
laravel-package-f18/how-to-implement-ne ... t2103.html

For those who want to know how to do it by hand:

Code: Select all

$mediaItem = \Aimeos\MShop:create( $context, 'media' )->createItem();
$mediaItem->setUrl( 'relative/path/to/file' )->setPreview( 'relative/path/to/file' );
$mediaItem = $manager->saveItem( $mediaItem );

$manager = \Aimeos\MShop:create( $context, 'customer' );
$customerItem = $manager->getItem( '<customer id>', ['media] );
$listItem = $manager->createListItem()->setRefId( $mediaItem->getId() );
$customerItem->addListItem( 'media', $listItem );
$manager->saveItem( $customerItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 15 Jul 2019, 15:55

I am very thankful for your help!

But I am trying to attach file to the user and I am getting error, the main code looks like:

Code: Select all

$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' );
$context->setEditor( 'aimeos:account' );

$localeManager = \Aimeos\MShop::create( $context, 'locale' );
$localeItem = $localeManager->bootstrap( 'default', '', '', false );
$context->setLocale( $localeItem );
$groupManager = \Aimeos\MShop::create( $context, 'customer/group' );

$customerListsManager = \Aimeos\MShop::create( $context, 'customer/lists' );
$mediaManager = \Aimeos\MShop::create( $context, 'media' );
$customerManager = \Aimeos\MShop::create( $context, 'customer' );

$editorGroupId = $groupManager->findItem( 'editor' )->getId();

$mediaItem = \Aimeos\MShop::create( $context, 'media' )->createItem();
$mediaItem->setUrl($filePath);
$mediaItem->setType('default');
$mediaManager->saveItem($mediaItem);
$customerItem = $customerManager->getItem( $user->id, ['media'] );
$listItem = $customerManager->createListItem()->setRefId( $mediaItem->getId() );
$customerItem->addListItem( 'media', $listItem );
$mediaManager->saveItem( $customerItem );

And the error is:

Code: Select all

Aimeos\MShop\Exception  : Unable to call method "createListItem"
Thank you a lot for the help!

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

Re: How to attach files to user by code ?

Post by aimeos » 15 Jul 2019, 15:59

Sorry, it's "createListsItem()"
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 15 Jul 2019, 16:26

Hmm, I have changed the code:

Code: Select all

$listItem = $customerManager->createListsItem()->setRefId( $mediaItem->getId() );
But I still get error:

Code: Select all

 Aimeos\MShop\Exception  : Unable to call method "createListsItem"

  at /var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Base.php:52
    48|          * @throws \Aimeos\MShop\Manager\Exception If method call failed
    49|          */
    50|         public function __call( $name, array $param )
    51|         {
  > 52|                 throw new \Aimeos\MShop\Exception( sprintf( 'Unable to call method "%1$s"', $name ) );
    53|         }
    54| 
    55| 
    56|         /**

  Exception trace:

  1   Aimeos\MShop\Common\Manager\Base::__call("createListsItem", [])
      /var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Decorator/Base.php:51

  2   call_user_func_array([])
      /var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Decorator/Base.php:51

  Please use the argument -v to see more details.
By the way, this code I try in Laravel command class.

I really need help (

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 16 Jul 2019, 09:42

I have fixed error and seems media is adding correctly. The code is:

Code: Select all

/** @var \Aimeos\MShop\Media\Item\Standard $mediaItem */
$mediaItem = \Aimeos\MShop::create( $context, 'media' )->createItem();
$mediaItem->setUrl($filePath);
$mediaItem->setType('default');
$mediaManager->saveItem($mediaItem);
$customerItem = $customerManager->getItem( $user->id, ['media'] );

$listItem = $cntl->createListItem()->setRefId( $mediaItem->getId() );
$listItem->setType('default');
$customerItem->addListItem( 'media', $listItem );
$customerManager->saveItem( $customerItem );
But now problem is that I can't display all media of current logged in user.
I have the code:

$view = $this->getView();
$context = $this->getContext();

Code: Select all

try
{
            $manager = \Aimeos\MShop::create( $context, 'media' );
            $search = $manager->createSearch();

            $total = 0;
            $view->items = $manager->searchItems( $search, [], $total );
            $view->total = $total;

}

but if I make dump of single item I see the properties:

Code: Select all

    -values: array:14 [▼
      "media.id" => "43"
      "media.siteid" => "1"
      "media.languageid" => null
      "media.type" => "default"
      "media.url" => "some/path"
      "media.label" => null
      "media.status" => "1"
      "media.mimetype" => null
      "media.domain" => null
      "media.preview" => null
      "media.mtime" => "2019-07-16 08:43:52"
      "media.editor" => "aimeos:account"
      "media.ctime" => "2019-07-16 08:43:52"
      "languageid" => "en"
    ]

Here I don't see any user or userId field. Or I don't know how it works ? Please let me know how can I get all media of current logged in user ?

Thank you!

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 16 Jul 2019, 13:37

Also I have figured out, that products have 'Product Lists Types' and there is 'media' type! Seems I have to add media type for users ? Because I attached files to user but I can't get them! I can't get files of specific user. I see only 'group' list type for user

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 16 Jul 2019, 13:52

Hmmmm(
I have added Customer Lists Type for customer - media.
when I add media to user entities in DB are created:
users_list table:

https://prnt.sc/ofsujj

mshop_media table:

https://prnt.sc/ofsvba

But media doesn't appear in the customer media tab!

https://prnt.sc/ofsw0c


When I dump my \Aimeos\MShop\Customer\Item\Standard object, there I have the following thing:

Code: Select all

-listItems: array:1 [▼
    "customer/group" => array:1 [▶]
  ]
But I think there also must be "customer/media"

I did it like admin said:
aimeos wrote: 15 Jul 2019, 13:01 The easiest way is to copy the product media subpanel (class + template) and rename every occurence of "Product" to "Customer" and "product" to "customer". Then, add "media" to the list of subparts in the customer panel configuration:
https://github.com/aimeos/ai-admin-jqad ... dm.php#L61

That should be all you need to get the media tab into the customer panel. The customer panel lists all customers if you haven't restricted it to the current logged in user ( $context->getUserId() should to the trick). For the customer, in the detail view only his media files will be listed in the new tab.

The manager code you mention can be used to retrieve media items from the database manually like this:

Code: Select all

$manager = \Aimeos\MShop::create( $context, 'media' );
$search = manager->createSearch();
$total = 0;
$view->items = $manager->searchItems( $search, [], $total );
$view->total = $total;
The initCriteria() method only sets the conditions and the sorting automatically depending on the given parameters.

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

Re: How to attach files to user by code ?

Post by aimeos » 16 Jul 2019, 14:59

If you copy&paste the panel, you have to add "media" in this line (or the configuration) to retrieve your media items:
https://github.com/aimeos/ai-admin-jqad ... d.php#L441
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

MikaelNazarenko
Expert
Posts: 274
Joined: 27 Jun 2019, 16:19

Re: How to attach files to user by code ?

Post by MikaelNazarenko » 16 Jul 2019, 15:02

Ahhhhh! )) I have figured out it 3 minutes before you answered) Thank you a lot !

But is there a difference about clashes ? Should I use domains with slashes like this ?

Code: Select all

$domains = ['customer/address', 'customer/group', 'customer/property', 'media'];
As you see I added just 'media', but not 'customer/media'

Post Reply