Relate attribute to catalog

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!
Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Relate attribute to catalog

Post by Oleh » 16 Jul 2020, 09:38

Hi,
In admin panel I can connect attribute to product, all data saved to mshop_index_attribute.
If I can in admin panel connect attribute to catalog item? Or I need to create custom extension?
If custom extension, creating new table which consist foreign keys from mshop_catalog and mshop_product is a good approach?

Thank you

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

Re: Relate attribute to catalog

Post by aimeos » 16 Jul 2020, 21:42

You can attach attributes to categories using the mshop_catalog_list table. You only have to implement a sub-panel similar to the catalog product sub-panel:
https://github.com/aimeos/ai-admin-jqad ... andard.php

Also have a look at this thread:
post6044.html#p6044
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Re: Relate attribute to catalog

Post by Oleh » 17 Jul 2020, 05:48

Thank you for reply,
I created my extension with name test
I aded in composer in classmap:

Code: Select all

 "autoload": {
        "classmap": [
            "ext/<yourext>/lib/custom/src",
            "ext/<yourext>/controller/common/src",
            "ext/<yourext>/controller/frontend/src",
            "ext/<yourext>/controller/jobs/src",
            "ext/<yourext>/client/html/src",
            "ext/<yourext>/client/jsonapi/src",
            "ext/<yourext>/admin/html/src",
            "ext/<yourext>/admin/jsonadm/src"
        ]
    },
Then in ./ext/test/config/admin.php I added subpart:

Code: Select all

'jqadm' => [
        'catalog' =>[
            'standard' => [
                'subparts' => [
                    'test'
                ]
            ]
        ]

    ],
But I don't understand why aimeos search Test class in \Aimeos\Admin\JQAdm\Catalog\Test\Standard, but not in my extension.
I got an error:"Class "\Aimeos\Admin\JQAdm\Catalog\Test\Standard" not available"

Also when I add this class in \Aimeos\Admin\JQAdm\Catalog\Test\Standard in ai-admin-qadm I can see subpart test in catalog, but when I press on it it does nothing.

Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Re: Relate attribute to catalog

Post by Oleh » 17 Jul 2020, 09:35

I did composer dump-autload and my extension working.
But I have a problem with subpart
This is admin.php in my ext:

Code: Select all

'catalog' =>[
            'standard' => [
                'subparts' => [
                    'test'
                ]
            ]
        ]
Then I created Test folder in my extension in jqadm/src/Catalog/Test/Standard.php
This is content of file:

Code: Select all

<?php

/**
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
 * @copyright Aimeos (aimeos.org), 2017-2020
 * @package Admin
 * @subpackage JQAdm
 */


namespace Aimeos\Admin\JQAdm\Catalog\Test;

sprintf( 'attribute' ); // for translation


/**
 * Default implementation of catalog product JQAdm client.
 *
 * @package Admin
 * @subpackage JQAdm
 */
class Standard
	extends \Aimeos\Admin\JQAdm\Common\Admin\Factory\Base
	implements \Aimeos\Admin\JQAdm\Common\Admin\Factory\Iface
{
	/** admin/jqadm/catalog/product/name
	 * Name of the product subpart used by the JQAdm catalog implementation
	 *
	 * Use "Myname" if your class is named "\Aimeos\Admin\Jqadm\Catalog\Product\Myname".
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
	 *
	 * @param string Last part of the JQAdm class name
	 * @since 2017.07
	 * @category Developer
	 */


	/**
	 * Adds the required data used in the template
	 *
	 * @param \Aimeos\MW\View\Iface $view View object
	 * @return \Aimeos\MW\View\Iface View object with assigned parameters
	 */
	public function addData( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
	{
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists/type' );

		$search = $manager->createSearch( true )->setSlice( 0, 10000 );
		$search->setConditions( $search->compare( '==', 'catalog.lists.type.domain', 'product' ) );
		$search->setSortations( [$search->sort( '+', 'catalog.lists.type.position' )] );

		$view->productListTypes = $manager->searchItems( $search );

		return $view;
	}


	/**
	 * Copies a resource
	 *
	 * @return string|null HTML output
	 */
	public function copy() : ?string
	{
		$view = $this->getObject()->addData( $this->getView() );
		$view->productBody = '';

		foreach( $this->getSubClients() as $client ) {
			$view->productBody .= $client->copy();
		}

		return $this->render( $view );
	}


	/**
	 * Creates a new resource
	 *
	 * @return string|null HTML output
	 */
	public function create() : ?string
	{
		$view = $this->getObject()->addData( $this->getView() );
		$view->productBody = '';

		foreach( $this->getSubClients() as $client ) {
			$view->productBody .= $client->create();
		}

		return $this->render( $view );
	}


	/**
	 * Returns a single resource
	 *
	 * @return string|null HTML output
	 */
	public function get() : ?string
	{
		$view = $this->getObject()->addData( $this->getView() );

		$total = 0;
		$params = $this->storeSearchParams( $view->param( 'cp', [] ), 'catalogproduct' );
		$listItems = $this->getListItems( $view->item, $params, $total );

		$view->productItems = $this->getProductItems( $listItems );
		$view->productData = $this->toArray( $listItems );
		$view->productTotal = $total;
		$view->productBody = '';

		foreach( $this->getSubClients() as $client ) {
			$view->productBody .= $client->get();
		}

		return $this->render( $view );
	}


	/**
	 * Saves the data
	 *
	 * @return string|null HTML output
	 */
	public function save() : ?string
	{
		$view = $this->getView();

		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );
		$manager->begin();

		try
		{
			$this->storeSearchParams( $view->param( 'cp', [] ), 'catalogproduct' );
			$this->fromArray( $view->item, $view->param( 'product', [] ) );
			$view->productBody = '';

			foreach( $this->getSubClients() as $client ) {
				$view->productBody .= $client->save();
			}

			$manager->commit();
		}
		catch( \Exception $e )
		{
			$manager->rollback();
			throw $e;
		}

		return null;
	}


	/**
	 * Returns the sub-client given by its name.
	 *
	 * @param string $type Name of the client type
	 * @param string|null $name Name of the sub-client (Default if null)
	 * @return \Aimeos\Admin\JQAdm\Iface Sub-client object
	 */
	public function getSubClient( string $type, string $name = null ) : \Aimeos\Admin\JQAdm\Iface
	{
		/** admin/jqadm/catalog/product/decorators/excludes
		 * Excludes decorators added by the "common" option from the catalog JQAdm client
		 *
		 * Decorators extend the functionality of a class by adding new aspects
		 * (e.g. log what is currently done), executing the methods of the underlying
		 * class only in certain conditions (e.g. only for logged in users) or
		 * modify what is returned to the caller.
		 *
		 * This option allows you to remove a decorator added via
		 * "admin/jqadm/common/decorators/default" before they are wrapped
		 * around the JQAdm client.
		 *
		 *  admin/jqadm/catalog/product/decorators/excludes = array( 'decorator1' )
		 *
		 * This would remove the decorator named "decorator1" from the list of
		 * common decorators ("\Aimeos\Admin\JQAdm\Common\Decorator\*") added via
		 * "admin/jqadm/common/decorators/default" to the JQAdm client.
		 *
		 * @param array List of decorator names
		 * @since 2017.07
		 * @category Developer
		 * @see admin/jqadm/common/decorators/default
		 * @see admin/jqadm/catalog/product/decorators/global
		 * @see admin/jqadm/catalog/product/decorators/local
		 */

		/** admin/jqadm/catalog/product/decorators/global
		 * Adds a list of globally available decorators only to the catalog JQAdm client
		 *
		 * Decorators extend the functionality of a class by adding new aspects
		 * (e.g. log what is currently done), executing the methods of the underlying
		 * class only in certain conditions (e.g. only for logged in users) or
		 * modify what is returned to the caller.
		 *
		 * This option allows you to wrap global decorators
		 * ("\Aimeos\Admin\JQAdm\Common\Decorator\*") around the JQAdm client.
		 *
		 *  admin/jqadm/catalog/product/decorators/global = array( 'decorator1' )
		 *
		 * This would add the decorator named "decorator1" defined by
		 * "\Aimeos\Admin\JQAdm\Common\Decorator\Decorator1" only to the JQAdm client.
		 *
		 * @param array List of decorator names
		 * @since 2017.07
		 * @category Developer
		 * @see admin/jqadm/common/decorators/default
		 * @see admin/jqadm/catalog/product/decorators/excludes
		 * @see admin/jqadm/catalog/product/decorators/local
		 */

		/** admin/jqadm/catalog/product/decorators/local
		 * Adds a list of local decorators only to the catalog JQAdm client
		 *
		 * Decorators extend the functionality of a class by adding new aspects
		 * (e.g. log what is currently done), executing the methods of the underlying
		 * class only in certain conditions (e.g. only for logged in users) or
		 * modify what is returned to the caller.
		 *
		 * This option allows you to wrap local decorators
		 * ("\Aimeos\Admin\JQAdm\Catalog\Decorator\*") around the JQAdm client.
		 *
		 *  admin/jqadm/catalog/product/decorators/local = array( 'decorator2' )
		 *
		 * This would add the decorator named "decorator2" defined by
		 * "\Aimeos\Admin\JQAdm\Catalog\Decorator\Decorator2" only to the JQAdm client.
		 *
		 * @param array List of decorator names
		 * @since 2017.07
		 * @category Developer
		 * @see admin/jqadm/common/decorators/default
		 * @see admin/jqadm/catalog/product/decorators/excludes
		 * @see admin/jqadm/catalog/product/decorators/global
		 */
		return $this->createSubClient( 'catalog/product/' . $type, $name );
	}


	/**
	 * Returns the catalog list items referencing the products
	 *
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object
	 * @param array $params Associative list of GET/POST parameters
	 * @param integer $total Value/result parameter that will contain the item total afterwards
	 * @return \Aimeos\Map Catalog list items implementing \Aimeos\MShop\Common\Item\List\Iface  referencing the products
	 */
	protected function getListItems( \Aimeos\MShop\Catalog\Item\Iface $item, array $params = [], &$total = null ) : \Aimeos\Map
	{
		$manager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );

		$search = $manager->createSearch();
		$search->setSortations( [
			$search->sort( '+', 'catalog.lists.position' ),
			$search->sort( '+', 'catalog.lists.refid' )
		] );

		$search = $this->initCriteria( $search, $params, 'catalogproduct' );
		$expr = [
			$search->getConditions(),
			$search->compare( '==', 'catalog.lists.parentid', $item->getId() ),
			$search->compare( '==', 'catalog.lists.domain', 'product' ),
		];
		$search->setConditions( $search->combine( '&&', $expr ) );

		return $manager->searchItems( $search, [], $total );
	}


	/**
	 * Returns the product items referenced by the given list items
	 *
	 * @param \Aimeos\Map $listItems Catalog list items implementing \Aimeos\MShop\Common\Item\List\Iface and referencing the products
	 * @return \Aimeos\Map List of product IDs as keys and items implementing \Aimeos\MShop\Product\Item\Iface
	 */
	protected function getProductItems( \Aimeos\Map $listItems ) : \Aimeos\Map
	{
		$list = $listItems->getRefId()->toArray();
		$manager = \Aimeos\MShop::create( $this->getContext(), 'product' );

		$search = $manager->createSearch()->setSlice( 0, count( $list ) );
		$search->setConditions( $search->compare( '==', 'product.id', $list ) );

		return $manager->searchItems( $search );
	}


	/**
	 * Returns the list of sub-client names configured for the client.
	 *
	 * @return array List of JQAdm client names
	 */
	protected function getSubClientNames() : array
	{
		/** admin/jqadm/catalog/product/standard/subparts
		 * List of JQAdm sub-clients rendered within the catalog product section
		 *
		 * The output of the frontend is composed of the code generated by the JQAdm
		 * clients. Each JQAdm client can consist of serveral (or none) sub-clients
		 * that are responsible for rendering certain sub-parts of the output. The
		 * sub-clients can contain JQAdm clients themselves and therefore a
		 * hierarchical tree of JQAdm clients is composed. Each JQAdm client creates
		 * the output that is placed inside the container of its parent.
		 *
		 * At first, always the JQAdm code generated by the parent is printed, then
		 * the JQAdm code of its sub-clients. The product of the JQAdm sub-clients
		 * determines the product of the output of these sub-clients inside the parent
		 * container. If the configured list of clients is
		 *
		 *  array( "subclient1", "subclient2" )
		 *
		 * you can easily change the product of the output by reproducting the subparts:
		 *
		 *  admin/jqadm/<clients>/subparts = array( "subclient1", "subclient2" )
		 *
		 * You can also remove one or more parts if they shouldn't be rendered:
		 *
		 *  admin/jqadm/<clients>/subparts = array( "subclient1" )
		 *
		 * As the clients only generates structural JQAdm, the layout defined via CSS
		 * should support adding, removing or reproducting content by a fluid like
		 * design.
		 *
		 * @param array List of sub-client names
		 * @since 2017.07
		 * @category Developer
		 */
		return $this->getContext()->getConfig()->get( 'admin/jqadm/catalog/product/standard/subparts', [] );
	}


	/**
	 * Creates new and updates existing items using the data array
	 *
	 * @param \Aimeos\MShop\Catalog\Item\Iface $item Catalog item object without referenced domain items
	 * @param array $data Data array
	 * @return \Aimeos\MShop\Catalog\Item\Iface Modified catalog item
	 */
	protected function fromArray( \Aimeos\MShop\Catalog\Item\Iface $item, array $data ) : \Aimeos\MShop\Catalog\Item\Iface
	{
		$listIds = $this->getValue( $data, 'catalog.lists.id', [] );
		$listManager = \Aimeos\MShop::create( $this->getContext(), 'catalog/lists' );

		$search = $listManager->createSearch()->setSlice( 0, count( $listIds ) );
		$search->setConditions( $search->compare( '==', 'catalog.lists.id', $listIds ) );

		$listItem = $listManager->createItem();
		$listItem->setParentId( $item->getId() );
		$listItem->setDomain( 'product' );


		foreach( (array) $listIds as $idx => $listid )
		{
			if( isset( $listItems[$listid] ) ) {
				$litem = $listItems[$listid];
			} else {
				$litem = clone $listItem;
			}

			$litem->setId( $listid ?: null );

			if( isset( $data['catalog.lists.refid'][$idx] ) ) {
				$litem->setRefId( $this->getValue( $data, 'catalog.lists.refid/' . $idx ) );
			}

			if( isset( $data['catalog.lists.status'][$idx] ) ) {
				$litem->setStatus( (int) $this->getValue( $data, 'catalog.lists.status/' . $idx ) );
			}

			if( isset( $data['catalog.lists.type'][$idx] ) ) {
				$litem->setType( $this->getValue( $data, 'catalog.lists.type/' . $idx ) );
			}

			if( isset( $data['catalog.lists.position'][$idx] ) ) {
				$litem->setPosition( (int) $this->getValue( $data, 'catalog.lists.position/' . $idx ) );
			}

			if( isset( $data['catalog.lists.datestart'][$idx] ) ) {
				$litem->setDateStart( $this->getValue( $data, 'catalog.lists.datestart/' . $idx ) );
			}

			if( isset( $data['catalog.lists.dateend'][$idx] ) ) {
				$litem->setDateEnd( $this->getValue( $data, 'catalog.lists.dateend/' . $idx ) );
			}

			if( isset( $data['catalog.lists.config'][$idx] )
				&& ( $conf = json_decode( $this->getValue( $data, 'catalog.lists.config/' . $idx ), true ) ) !== null
			) {
				$litem->setConfig( $conf );
			}

			if( isset( $data['config'][$idx]['key'] ) )
			{
				$conf = [];

				foreach( (array) $data['config'][$idx]['key'] as $pos => $key )
				{
					if( trim( $key ) !== '' && isset( $data['config'][$idx]['val'][$pos] ) ) {
						$conf[$key] = $data['config'][$idx]['val'][$pos];
					}
				}

				$litem->setConfig( $conf );
			}

			$listManager->saveItem( $litem, false );
		}

		return $item;
	}


	/**
	 * Constructs the data array for the view from the given item
	 *
	 * @param \Aimeos\Map $listItems Catalog list items implementing \Aimeos\MShop\Common\Item\Lists\Iface and referencing the products
	 * @return string[] Multi-dimensional associative list of item data
	 */
	protected function toArray( \Aimeos\Map $listItems ) : array
	{
		$data = [];

		foreach( $listItems as $listItem )
		{
			foreach( $listItem->toArray( true ) as $key => $value ) {
				$data[$key][] = $value;
			}
		}

		return $data;
	}


	/**
	 * Returns the rendered template including the view data
	 *
	 * @param \Aimeos\MW\View\Iface $view View object with data assigned
	 * @return string HTML output
	 */
	protected function render( \Aimeos\MW\View\Iface $view ) : string
	{
		/** admin/jqadm/catalog/product/template-item
		 * Relative path to the HTML body template of the product subpart for catalogs.
		 *
		 * The template file contains the HTML code and processing instructions
		 * to generate the result shown in the body of the frontend. The
		 * configuration string is the path to the template file relative
		 * to the templates directory (usually in admin/jqadm/templates).
		 *
		 * You can overwrite the template file configuration in extensions and
		 * provide alternative templates. These alternative templates should be
		 * named like the default one but with the string "default" replaced by
		 * an unique name. You may use the name of your project for this. If
		 * you've implemented an alternative client class as well, "default"
		 * should be replaced by the name of the new class.
		 *
		 * @param string Relative path to the template creating the HTML code
		 * @since 2016.04
		 * @category Developer
		 */
		$tplconf = 'admin/jqadm/catalog/product/template-item';
		$default = 'catalog/item-attribute-standard';

		return $view->render( $view->config( $tplconf, $default ) );
	}
}
I see test tab in admin->catagories. But when I tap on button Test it does nothing

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

Re: Relate attribute to catalog

Post by aimeos » 17 Jul 2020, 09:37

As long as you add no template ./templates/catalog/item-attribute-standard.php, there will be no output.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Re: Relate attribute to catalog

Post by Oleh » 17 Jul 2020, 11:36

Thank you, I resolved my mistake. I did composer dump-autoload and all work.
Also I changed all variables in item-attribute-standard.php to attribute and ibuttonwork!

Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Re: Relate attribute to catalog

Post by Oleh » 17 Jul 2020, 12:55

Hi,
I got from Products tab subpart Options and put in catalog.
Then I want to save data which in options I added.
How can I do this.
I found method save() in

Code: Select all

namespace Aimeos\Admin\JQAdm\Catalog;
But I got an error message

Code: Select all

ErrorError saving data
in this line of code:

Code: Select all

return $manager->insertItem( $item, $data['catalog.parentid'] ?: null );

Oleh
Posts: 14
Joined: 13 Jul 2020, 08:11

Re: Relate attribute to catalog

Post by Oleh » 20 Jul 2020, 13:56

I added options in catalog like this is in products.
I resolved all errors and saved options to mshop_catalog_list with domain attribute.
Then I need to show all options related to catelog in options subpart.
How I can do this, I try to debug code in Catalog/Standart.php, but cannot find something.
I attached file where I want to show attributes related to catalog.
Thank you
Attachments
options.png
options.png (28.48 KiB) Viewed 7303 times

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

Re: Relate attribute to catalog

Post by aimeos » 22 Jul 2020, 07:37

Use the code in the product characteristics panel as reference for saving the attribute references:
https://github.com/aimeos/ai-admin-jqad ... acteristic
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply