Post by straightlight » Sat Mar 19, 2022 2:49 am

OSWorX wrote:
Sat Mar 19, 2022 2:04 am
by mona wrote:
Fri Mar 18, 2022 11:40 pm
Maybe it would be better to have two independent threads - one for chatter and one exclusively fit for purpose ?
Forgot completely, that I have enabled the Discussion board here:
https://github.com/osworx/opencart-even ... iscussions

Guess for true interested devs that this should fit more than this forum here (which is basically a "user" forum).
Well, as a forum moderator, what you are also implying at this point is that forum users are rather encouraged to post on your Github repository than they should on the Opencart Forum.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by OSWorX » Sat Mar 19, 2022 3:46 am

straightlight wrote:
Sat Mar 19, 2022 2:49 am
Well, as a forum moderator, what you are also implying at this point is that forum users are rather encouraged to post on your Github repository than they should on the Opencart Forum.
Well, I am sure your can read:
Guess for true interested devs that this should fit more than this forum here (which is basically a "user" forum).

Devs = Developer
This forum here is for users (like you).

A ususal "user" has no interest how the code is working, it has to work.

So stop complaining.
And it does no matter if mine, yours or someone else!
You can participate, or let it.
Last time I write this.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by OSWorX » Sat Mar 19, 2022 2:53 pm

Based on this discussion here, where one user (mikeinterserv) asked how his OCMod based solution could be solved with an event:
viewtopic.php?f=202&t=227654&start=20#p839322

The solution with event only.

Fields for the database (table event) where a new entry has to be made:
code: admin_limit_images
trigger: admin/view/catalog/product_form/after
action: extension/module/product_images/eventViewTemplateCatalogProductFormAfter

And the event code itself:

Code: Select all

/**
	 * code: admin_limit_images
	 * trigger: admin/view/catalog/product_form/after
	 * action: extension/module/product_notification/eventViewTemplateCatalogProductFormAfter
	 */
	public function eventViewTemplateCatalogProductFormAfter( &$route, &$args, &$output ) {
		preg_match_all( '/onclick="\$\(\'#image-row.\'\)\.remove\(\);/', $output, $matches );

		if( $matches[0] ) {
			foreach( $matches[0] as $match ) {
				$output = str_replace( $match, $match . ' myFunction();', $output );
			}
		}

		$search = 'function addImage() {';
		$replace = '
		if (image_row < 5 ) {';

		$output = str_replace( $search, $search . $replace, $output );

		$search = 'image_row++;';
		$replace = '
		} else {
			html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
			$(\'#images tfoot\').replaceWith(html);
		}';

		$output = str_replace( $search, $search . $replace, $output );

		$search = <<<JS
onclick="$(\'#image-row' + image_row + '\').remove();
JS;
		$replace = <<<JS
onclick="$(\'#image-row' + image_row + '\').remove(); myFunction();
JS;

		$output = str_replace( $search, $replace, $output );

		$search = '</body>';
		$replace = <<<JS
<script>
	function myFunction() {
		html = '<tfoot><tr><td colspan="2"></td><td class="text-left"><button type="button" onclick="addImage();" data-toggle="tooltip" title="{{ button_image_add }}" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td></tr></tfoot>';
		$('#images tfoot').replaceWith(html);
		image_row--;
	}
</script>

JS;

		$output = str_replace( $search, $replace . $search, $output );
	}
}
Add this function inside a new file, called e.g.:
product_notification.php
which has to be stored in the folder:
../admin/controller/extension/module/

A complete solution would be, to create a whole extension which is installable by the commin OpenCart installer.
And with that, the event could be easely en- and disabled, as well as permission can be set (for user groups).

Note: this is only one way to reach the "goal" (at least 3 are possible).
But maybe another one here has a better solution, then don't hasitate to post it.

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by OSWorX » Sat Mar 19, 2022 3:09 pm

by mona wrote:
Thu Mar 17, 2022 9:49 am
So my first question ::)

For the sake of argument you need to add something to system/framework.php
For simplicity lets say you need to add

Code: Select all

$response->addHeader('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$response->addHeader('Pragma: no-cache');
Sorry for the delay in answering, too much work ..

Counter-questions:

1. when shall these headers be added
2. and what for

Guess has to do with the caching issue mentioned earlier somewhere ..

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by halfhope » Sat Mar 19, 2022 5:50 pm

Hi!

Short quotes from my article (2.3, 3.x, 4.x). If you see some errors in translation, write in PM.

Features when using events

  • Events can only be added from admin controllers. The most convenient way to do this is when installing the module, in the install function.
  • The paths of all triggers begin with the name of the desired section, admin, catalog or library. The admin and catalog sections contain the controller, view, language, and config.
  • You can use "*" in trigger paths to assign triggers by mask. For example catalog/view/theme/*/template/common/header/after.
  • To change the data in the config and language event handlers, use $this->config->set(), $this->language->set() respectively.
  • Data received from event handlers can be stored within the class and used in other handlers that are run later.
To edit events from the admin panel, use the "Event Manager" or adminer ([ctrl+click] for quick editing rows of oc_event table).

Features for different versions of Opencart

  • The event code for version 2.3 must be no longer than 32 characters. For versions 3.x and 4.x no more than 64.
  • In version 2.3, events (registration, deletion, etc.) are handled by the extension/event model, in 3.x and 4.x setting/event.
  • In version 2.3, in the catalog section, the before/after view trigger paths will be different. For example, catalog/view/common/header/before, catalog/view/default/template/common/header/after. This is due to the use of themes in the catalog section and assigned events.
  • Since version 3.x, the sort order of events has been added.
  • Since version 4.x each event must have a description.
  • Triggers for libraries (library) are available only from version 4.x.

Arguments

Arguments are passed to event handlers by reference. This means that you can change the values of the arguments without worrying about passing the result somewhere else.
Image

Return Values

In addition to changing data through arguments, event handlers can also return values using return. For example, if the controller/common/home/before event handler returns the generated html code via return, then the entire output of the common/header controller will be replaced by it, and the common/header controller itself will not be executed, but the after event will be fired. Those. it is possible to replace the execution data of functions without executing them.
Image

Original article has important comment from opencartforum.com user @Vladzimir:
I will add from myself that event triggers eat up 10-20% of the page generation time.
The reason is banal - the implementation is made through the gluteal muscle.
Attached is the system/engine/event.php file for 2.3, which allows you to significantly speed up this moment (and even reduce PeakMemory)
Last edited by halfhope on Sat Aug 26, 2023 2:33 am, edited 2 times in total.

My FREE extensions in marketplace. [ security | flexibility | speedup ]


User avatar
Active Member

Posts

Joined
Tue Dec 10, 2013 9:44 pm
Location - San Diego

Post by straightlight » Sat Mar 19, 2022 9:30 pm

OSWorX wrote:
Sat Mar 19, 2022 2:53 pm
html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
Hardcoded language which limits the knowledge of language packs to be used with Events. Besides, there's no need to add black slashes nor the need to use: <<<JS at the beginning of replacements.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by OSWorX » Sat Mar 19, 2022 10:05 pm

straightlight wrote:
Sat Mar 19, 2022 9:30 pm
OSWorX wrote:
Sat Mar 19, 2022 2:53 pm
html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
Hardcoded language which limits the knowledge of language packs to be used with Events. Besides, there's no need to add black slashes nor the need to use: <<<JS at the beginning of replacements.
Your're really a funny guy.
First check the original script (OCMod file) and then you may post here further.
The script from me was only the answer to the question (challenge).
And the original had a similiar line in it.

Having "Programmer" in his signature and does not know what <<<JS stands for or why the backslashes are at that positions.
But you can try it without ..

The only argument from you - which is valid, that "hardcoded language strings" should be avoided.
But as "Programmer" you could join this topic and present your better solution of all.
Instead of posting useless comments!

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by straightlight » Sat Mar 19, 2022 10:37 pm

OSWorX wrote:
Sat Mar 19, 2022 10:05 pm
straightlight wrote:
Sat Mar 19, 2022 9:30 pm
OSWorX wrote:
Sat Mar 19, 2022 2:53 pm
html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
Hardcoded language which limits the knowledge of language packs to be used with Events. Besides, there's no need to add black slashes nor the need to use: <<<JS at the beginning of replacements.
Your're really a funny guy.
First check the original script (OCMod file) and then you may post here further.
The script from me was only the answer to the question (challenge).
And the original had a similiar line in it.

Having "Programmer" in his signature and does not know what <<<JS stands for or why the backslashes are at that positions.
But you can try it without ..

The only argument from you - which is valid, that "hardcoded language strings" should be avoided.
But as "Programmer" you could join this topic and present your better solution of all.
Instead of posting useless comments!
Then, based on my last post, that's all we need to know.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by mikeinterserv » Sat Mar 19, 2022 10:54 pm

straightlight wrote:
Sat Mar 19, 2022 9:30 pm
html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
Hardcoded language which limits the knowledge of language packs to be used with Events.
Well I did that in my Mod - Its hardly OSWorX is it. OSWorX has gone out of his way to do this and I am extremely grateful to OSWorX and halfhope for the information they are providing and the help being given. OSWorX conversion is how I would have gone about it also at this stage and I have been reading the method of views manipulation by J Neuhoff here viewtopic.php?f=144&t=221533&hilit=even ... de#p807696.

We are called DEVELOPERS for a reason - You start off with something that is NOT PERFECT usually. Then you DEVELOP during testing etc etc.
The hardcoded lines are ONLY in the initial development of what I do. If the CONCEPT proves worthy and worth FURTHER DEVELOPMENT these things then get FIXED as we go. If the CONCEPT is NOT worthy or went in the wrong direction then NO NEEDLESS time is wated.

Thanks again.

Active Member

Posts

Joined
Thu May 28, 2020 6:55 am
Location - Wales

Post by straightlight » Sat Mar 19, 2022 11:08 pm

mikeinterserv wrote:
Sat Mar 19, 2022 10:54 pm
straightlight wrote:
Sat Mar 19, 2022 9:30 pm
html = \'<tfoot><tr><td colspan="3"><h2>You have already reached the limit of 5 images.</h2></td></tr></tfoot>\';
Hardcoded language which limits the knowledge of language packs to be used with Events.
Well I did that in my Mod - Its hardly OSWorX is it. OSWorX has gone out of his way to do this and I am extremely grateful to OSWorX and halfhope for the information they are providing and the help being given. OSWorX conversion is how I would have gone about it also at this stage and I have been reading the method of views manipulation by J Neuhoff here viewtopic.php?f=144&t=221533&hilit=even ... de#p807696.

We are called DEVELOPERS for a reason - You start off with something that is NOT PERFECT usually. Then you DEVELOP during testing etc etc.
The hardcoded lines are ONLY in the initial development of what I do. If the CONCEPT proves worthy and worth FURTHER DEVELOPMENT these things then get FIXED as we go. If the CONCEPT is NOT worthy or went in the wrong direction then NO NEEDLESS time is wated.

Thanks again.
While JNeuhoff does indeed not use <<<JS nor back slashes in his Event code on that post, he doesn't seem to use the $this->load->view either but this dismissal isn't critical based on the concept to show how the core extensions' are already coded.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by mikeinterserv » Sat Mar 19, 2022 11:16 pm

straightlight wrote:
Sat Mar 19, 2022 11:08 pm
While JNeuhoff does indeed not use <<<JS nor back slashes in his Event code on that post, he doesn't seem to use the $this->load->view either but this dismissal isn't critical based on the concept to show how the core extensions' are already coded.
You can be so frustrating straightlight. You comments are nearly ALLWAYS destructive rather than constructive. If you switched this around you would get a lot more respect.

Active Member

Posts

Joined
Thu May 28, 2020 6:55 am
Location - Wales

Post by straightlight » Sat Mar 19, 2022 11:18 pm

mikeinterserv wrote:
Sat Mar 19, 2022 11:16 pm
straightlight wrote:
Sat Mar 19, 2022 11:08 pm
While JNeuhoff does indeed not use <<<JS nor back slashes in his Event code on that post, he doesn't seem to use the $this->load->view either but this dismissal isn't critical based on the concept to show how the core extensions' are already coded.
You can be so frustrating straightlight. You comments are nearly ALLWAYS destructive rather than constructive. If you switched this around you would get a lot more respect.
As said on the above, it isn't a big deal in another words which means that demonstration is fine.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Sun Mar 20, 2022 4:40 am

@halfhope: A bit of improvement with variable structures and clean codes.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by by mona » Sun Mar 20, 2022 8:32 am

OSWorX wrote:
Sat Mar 19, 2022 3:09 pm
Sorry for the delay in answering, too much work ..

Counter-questions:

1. when shall these headers be added
2. and what for

Guess has to do with the caching issue mentioned earlier somewhere ..
No problem for the delay, I have quite a lot on both work and personally, there is no rush.

I used it as a simple issue that has arisen where a system file is required to be edited, essential rather than cosmetic. My question is can/how would it be done using Events exclusively today in OC3?

Currently in OCMOD

Code: Select all

<file path="system/framework.php">
		<operation>
			<search><![CDATA[$response->addHeader('Content-Type: text/html; charset=utf-8');]]></search>
			<add position="after"><![CDATA[
$response->addHeader('Cache-Control: public, max-age=1, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$response->addHeader('Pragma: no-cache');
			]]></add>
		</operation>
		</file>

To answer your question when. That is part of the question because there is no function in the system/framework.php to add it before or after.
To answer question why. In the case above it is caching, it is the simplest thing I can think of to request a simplest example to follow. On a Varnish Server and other issues without it Opencart is rendered useless, the cart simply will not function correctly.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by by mona » Sun Mar 20, 2022 8:39 am

OSWorX wrote:
Sat Mar 19, 2022 1:12 am
You wrote that you have already some extensions working with Events only.
Why not link to some of them here (no extensive promotion!).
I doubt anyone is going to purchase a module just to learn how Events work :laugh:
Anyway, I created a simple "how to add a custom css stylesheet using Events only".
https://www.opencart.com/index.php?rout ... n_id=43491

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by by mona » Sun Mar 20, 2022 8:42 am

halfhope wrote:
Sat Mar 19, 2022 5:50 pm
Arguments

Arguments are passed to event handlers by reference. This means that you can change the values of the arguments without worrying about passing the result somewhere else.
Image

Return Values

In addition to changing data through arguments, event handlers can also return values using return. For example, if the controller/common/home/before event handler returns the generated html code via return, then the entire output of the common/header controller will be replaced by it, and the common/header controller itself will not be executed, but the after event will be fired. Those. it is possible to replace the execution data of functions without executing them.
Image
:good:
great reference table - thank you

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by OSWorX » Sun Mar 20, 2022 7:26 pm

by mona wrote:
Sun Mar 20, 2022 8:39 am
OSWorX wrote:
Sat Mar 19, 2022 1:12 am
You wrote that you have already some extensions working with Events only.
Why not link to some of them here (no extensive promotion!).
I doubt anyone is going to purchase a module just to learn how Events work
Correct, but to be honest: creating only "paid" extensions is also not the way it should be.
Maybe some see people OpenCart to make money only, but those should also not forget that they benefit from OpenCart itself - which is free (in every sense).
So, creating also "free" extensions (even if they are small) and share/publish should be the minimum (for a good developer).

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by OSWorX » Sun Mar 20, 2022 8:28 pm

by mona wrote:
Sun Mar 20, 2022 8:32 am
I used it as a simple issue that has arisen where a system file is required to be edited, essential rather than cosmetic. My question is can/how would it be done using Events exclusively today in OC3?

Currently in OCMOD

Code: Select all

<file path="system/framework.php">
		<operation>
			<search><![CDATA[$response->addHeader('Content-Type: text/html; charset=utf-8');]]></search>
			<add position="after"><![CDATA[
$response->addHeader('Cache-Control: public, max-age=1, no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
$response->addHeader('Pragma: no-cache');
			]]></add>
		</operation>
		</file>
Well, because adding headers are done before any output is made, means it should (and can) be done whenever the common header is called.

Therefore:

Code: Select all

<?php
/**
 * @version		$Id: event.php 6487 2022-03-20 13:13:00Z mic $
 * @package		Event
 * @author		mic - https://osworx.net
 * @copyright	2022 OSWorX
 * @license		MIT
 */

class ControllerExtensionModuleEvent extends Controller {

	public $version = '1.0.0';

	/**
	 * code:	ox_catalog_header
	 * trigger:	catalog/view/common/header/after
	 * action:	extension/module/event/eventCatalogViewCommonHeaderAfter
	 */
	public function eventCatalogViewCommonHeaderAfter( &$route, &$args, &$output ) {
		header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
		header('Pragma: no-cache');
	}
}
File: event.php (or any name you like - then change also the function name and the action in the event table)
Folder: ../catalog/controller/extension/module/
by mona wrote:
Sun Mar 20, 2022 8:32 am
To answer your question when. That is part of the question because there is no function in the system/framework.php to add it before or after.
To answer question why. In the case above it is caching, it is the simplest thing I can think of to request a simplest example to follow. On a Varnish Server and other issues without it Opencart is rendered useless, the cart simply will not function correctly.
It' s what I thought (caching) - saw some discussions here or at GH about it.
But adding such header via any method (does no matter which one) should be avoided primary.
Better if the core would have a method to add custom headers - e.g. via the system config (settings page).

While I have never seen such an issue in all the years (and have many clients over the whole world at different hosters and servers), making a PR at GH is free for everyone.
And I am sure that Daniel - when provided with enough details why and what for - will not decline such a request.

Attachments

catalog/controller/extension/module/event.php


Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Administrator
Online

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by by mona » Mon Mar 21, 2022 9:22 am

Thank you, it is not really what I was hoping but it is what I suspected was the case.
In part it is a new way of thinking and approach. I am sure we will get used to it, or not ..

The example was just for simplicity. I am pretty sure for OC4 Daniel has added it already.
Last edited by by mona on Mon Mar 21, 2022 9:36 am, edited 1 time in total.

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by by mona » Mon Mar 21, 2022 9:25 am

OSWorX wrote:
Sun Mar 20, 2022 7:26 pm
by mona wrote:
Sun Mar 20, 2022 8:39 am
OSWorX wrote:
Sat Mar 19, 2022 1:12 am
You wrote that you have already some extensions working with Events only.
Why not link to some of them here (no extensive promotion!).
I doubt anyone is going to purchase a module just to learn how Events work
Correct, but to be honest: creating only "paid" extensions is also not the way it should be.
Maybe some see people OpenCart to make money only, but those should also not forget that they benefit from OpenCart itself - which is free (in every sense).
So, creating also "free" extensions (even if they are small) and share/publish should be the minimum (for a good developer).
I do and did offer the example posted above for free ;D

I was answering why I did not previously post links to my modules that use Events !

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am
Who is online

Users browsing this forum: No registered users and 3 guests