JoomGalleryfriends / JG4-dev

Development repository for JoomGallery v4.x
GNU General Public License v3.0
10 stars 6 forks source link

Config service #58

Closed Elfangor93 closed 1 year ago

Elfangor93 commented 1 year ago

What is this service for

The Config service will be used to handle the different configurations set in the component. Based on the chosen inheritance method in the Glocal component settings, the correct set of configurations are loaded depending on the current context. The following configuration sets are taken into account:

The following contexts are available:

How does it work

When you instantiate the service, you have to provide a context and optionally an ID telling the service for which part of the component you want to have the configuration set loaded. The service then will get the configuration sets and override the settings based on the chosen inheritance method. Default inheritance method will override the settings as follows:

  1. Global Configuration set
  2. User Configuartion set
  3. Category settings (taking into account all parent categories)
  4. Image settings
  5. Menu item settings (frontend only, lookign which site are you accessing)

How to use this service

You can load the service anywhere in Joomla by doing

$component = Factory::getApplication()->bootComponent('com_joomgallery');  // boot JoomGallery component
$component->createConfig('context', ID);   // instantiate service based on the provided context
$config = $component->getConfig();   // load the service into your scope
$config->get('jg_param'); // get parameter with key name 'jg_param'

How to test the service

Make sure "Debug System" is "Yes" in Joomla Global configuration. Open the file /administrator/com_joomgallery/src/View/Faulties/HtmlView.php and insert the following code on line 62 after throw new \Exception(implode("\n", $errors)); }

Different test instructions see comments below...

Elfangor93 commented 1 year ago

Test Methods in category model

\: Category ID (integer) \: Left or right side sibling (string, possibilities: ['left', 'right', 'both']) \: Include current category (optional, bool, default: false) \: Include root category (optional, bool, default: false)

Get all parent categories

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getParents(<CATID>, <SELF>, <ROOT>);
dump($res)

Get all child categories

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getChildren(<CATID>, <SELF>);
dump($res)

Get all child and parent categories

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getTree(<CATID>, <ROOT>);
dump($res)

Get direct sibling of category

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getSibling(<CATID>, <SIDE>);
dump($res)

Get all siblings of category

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getSiblings(<CATID>, <SIDE>);
dump($res)

You should see an output like this: grafik

Elfangor93 commented 1 year ago

Test Config service

\: Different contexts are listed above (string) \: LID of the context element (integer)

Create Config service

$this->component->createConfig(<CONTEXT>, <ID>);
$config = $this->component->getConfig();
dump($config)

You then should see the following: grafik

Here all possible configuration settings are listed with the correct value loaded based on the inheritance method chosen and the context and id provided.

AlexanderSupp commented 1 year ago

Sorry, for me, it's too abstract. I'm not able to understand what's behind this service. So I see, I have a lot to learn.

MrMusic commented 1 year ago

After a few tests, I still have a few questions about config service: Perhaps I have not understood the functionality correctly? :confused:

Actual situation:

Config sets: Global config: User group=Public, id=1 For Registered: User group=Registered, id=2 For Super User: User group=Super Users, id=3

Users: Admin: User group=Super Users, id=663 Tester: User group=Registered, id=664

Categories: First: Access=Public, id=2 Second: Access=Registered, id=3

1. Attempt: Code:

$this->component->createConfig('com_joomgallery.user.id', 664);
$config = $this->component->getConfig();
dump($config);

Expected: Loading the configuration valid for the user with id 664.

Result: Correct?: The config for registered is displayed. but also the message: Error deriving settings from configurations. Provided context may be wrong. Context: com_joomgallery.user.id

configservice-wrong-context

2. Attempt: Code:

$this->component->createConfig('com_joomgallery', );
$config = $this->component->getConfig();
dump($config);

Expected: Load global configuration valid for current user

Result: Correct?: Configuration for super user (is current user) is displayed. No error message.

3. Attempt: Code:

$this->component->createConfig('com_joomgallery.user.id', null);
$config = $this->component->getConfig();
dump($config);

Expected: Loading of the configuration valid for public (visitors)

Result: Correct?: The config for public is displayed. but also the message: Error deriving settings from configurations. Provided context may be wrong. Context: com_joomgallery.user.id

4. Attempt: Code:

$this->component->createConfig('com_joomgallery.category.id', 2);
$config = $this->component->getConfig();
dump($config);

Expected: Loading of the configuration for category with id = 2 (public).

Result: Wrong?: The configuration for the current user (Super user id=663) is loaded. but also the message:
Error deriving settings from configurations. Provided context may be wrong. Context: com_joomgallery.category.id

5. Attempt: Code:

$this->component->createConfig('com_joomgallery.category.id', 3);
$config = $this->component->getConfig();
dump($config);

Expected: Loading the configuration for category with id = 3 (registered).

Result: Wrong?: The configuration for the current user (Super user id=663) is loaded. but also the message: Error deriving settings from configurations. Provided context may be wrong. Context: com_joomgallery.category.id

szepty-ziemi commented 1 year ago

Test data: https://prnt.sc/cyYMX57jwgru Test ENV: https://prnt.sc/POC1XpMTIc-i

  1. Get all parent categories = OK
  2. Get all child categories

$model = $this->component->getMVCFactory()->createModel('Category'); $res = $model->getChildren(1, true); dump($res);

1 is Root category so I expected to see ALL child categories... but there is one missing ID 2 "Categorized". Also there is self = true... so Root category should be also listed?

https://prnt.sc/NgC5Zq7li7iN

  1. Get all child and parent categories = OK It also count self.
  2. Get direct sibling of category

$model = $this->component->getMVCFactory()->createModel('Category'); $res = $model->getSibling(2, 'both'); dump($res);

Category ID 2 is "Uncategorized"... with above code I expected to see both siblings... but it seems that the only sibling is ROOT. Is "Uncategorized" on the same level as ROOT or it should be child of ROOT category?

https://prnt.sc/DTqH-kMqNFXe

  1. Get all siblings of category = OK

  2. Create Config service = I can confirm issues mentioned by @MrMusic above.

Elfangor93 commented 1 year ago

Category ID 2 is "Uncategorized"... with above code I expected to see both siblings... but it seems that the only sibling is ROOT. Is "Uncategorized" on the same level as ROOT or it should be child of ROOT category?

Something seems to be weird with your categories structure. The root category is tho only category with level=0. All other categories including "Uncategorised" have levels > 0. This means that Root category does not have any siblings. You can check that by doing

$model = $this->component->getMVCFactory()->createModel('Category');
$res = $model->getSibling(1, 'both');
dump($res);

The output should be "false" since Root category (id=1) does not have any siblings.

grafik

Elfangor93 commented 1 year ago

@MrMusic and @szepty-ziemi are your found issues fixed now?

szepty-ziemi commented 1 year ago

Okay, when fired code you provided I get false indeed. But I did it on new installation, without any other categories than default.

When tried to create category I get error:

obraz

I downloaded fresh branch package (few minutes ago).

Elfangor93 commented 1 year ago

When tried to create category I get error:

This issue you found to create categories already exists in the current main branch and is not related to this PR. Please open a new issue for that.

I propose to merge this PR and fix the remaining issues with new PRs. This one here is already huge. I am scared of messing up new things by fixing issues not related to the content of this PR...

szepty-ziemi commented 1 year ago

Okay, the problem is that this is bloker for testing. I removed previous version and installed new one... so no categories left. I need to create them to be able to test this PR... or rather to confirm if issues I reported are fixed.

Is there a workaround I could use? Eg. creating categories straight in DB?

MrMusic commented 1 year ago

Since last updates a warning message is shown in the subform 'Image processing (static images)' in new configuration sets: Warning: Undefined variable $id in ...administrator\components\com_joomgallery\layouts\joomla\form\renderfield.php on line 41 This is only in new config sets not in 'Global Configuration'.

PS: Creating new categories is possible for me without problems.

Elfangor93 commented 1 year ago

Creating new categories also for me without any problems. I am testing with mysql v5.7.37 on Windows while you (@szepty-ziemi) are testing with MariaDB v10.3.37 on Linux. When did the creation of categories stop working? I dont remember a change which could result in the error you are experiencing...

Maybe you could try using the zip file with sample data created by @MrMusic until we solved the probelm of creating categories. The zip should work anyway since it adds the categories directly into the DB by SQL queries.

szepty-ziemi commented 1 year ago

I uninstalled, downloaded latest version and installed as a fresh one. Problem with category creation is gone. I retested the cases and all looks good :)

MrMusic commented 1 year ago

I propose to merge this PR and fix the remaining issues with new PRs...

Ok, then we do it so...