Post by straightlight » Tue Dec 22, 2020 2:15 am

madimar wrote:
Tue Dec 22, 2020 2:04 am
straightlight wrote:
Mon Dec 21, 2020 8:40 am
Directly into the install.xml file to search for lines and alter them.
DO YOU MEAN OCMOD ?!?
Yes and please don't use caps when posting.

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 madimar » Tue Dec 22, 2020 7:59 pm

straightlight wrote:
Tue Dec 22, 2020 2:15 am
madimar wrote:
Tue Dec 22, 2020 2:04 am
straightlight wrote:
Mon Dec 21, 2020 8:40 am
Directly into the install.xml file to search for lines and alter them.
DO YOU MEAN OCMOD ?!?
Yes and please don't use caps when posting.
Well, thanks for your confirmation. Regarding the caps it was just a few words and in this specific case they were used intentionally to shout my wonder!
So besides that, the point is, removing any ocmod/vqmod support in next releases, it will be necessary and accepted to have some extra queries in order to work with events. It can be ok, just to know.
Said that, in general, about extending Opencart by events, I still think it will be quite a mess to troubleshoot any possible problem/conflict, due to code fragmentation in several files. Is my concern correct according to your opinion?

Regards and thanks for your participation to this topic.

M

-----------------------------------------------------------------------
My last mods: Partita IVA e CF | Pro EU VAT Number | Sales Agents | Pricelist Pro
-----------------------------------------------------------------------


User avatar
Active Member

Posts

Joined
Thu Sep 24, 2009 6:27 pm


Post by OSWorX » Tue Dec 22, 2020 8:45 pm

Just a short clarification.

1. https://github.com/opencart/opencart/wi ... ion-System
Read there:
As of OpenCart 3.1.0.0+ OCMOD will be removed from the OpenCart source code. Only the event system will be used for modifications.
2. Ongoing discussion (closed by Daniel)
https://github.com/opencart/opencart/issues/8992

3. Based on 1. & 2.
OC 3.1.1.0 will be OC 4.x (nearly, OC 4.x replace 3.1.x)
From OC 4.x on, wether OC- nor VQMods are possible anymore, Events shall be handle everything in future.
The file modification.php is removed, which means: no replacements as we know them from OC- or VQMods are possible.

Looking at all of that, it makes me wonder why a replacement via the install.xml (which still exists and is used further) is recommended, because it won't work anymore.
While I am sure that some "clever codie boy" will introduce something and create a "modification" as already exists to eleminate the directory limit introduced with OC 3.x

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


User avatar
Guru Member

Posts

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

Post by JNeuhoff » Tue Dec 22, 2020 9:01 pm

Looking at all of that, it makes me wonder why a replacement via the install.xml (which still exists and is used further) is recommended, because it won't work anymore.
I think all the install.xml will do is to describe the extension, e.g. name, version, author, and link. No more core file modifications.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by straightlight » Tue Dec 22, 2020 9:05 pm

Well, thanks for your confirmation. Regarding the caps it was just a few words and in this specific case they were used intentionally to shout my wonder!
Which is why you were being asked why not to use the caps still.

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 » Tue Dec 22, 2020 10:53 pm

JNeuhoff wrote:
Tue Dec 22, 2020 9:01 pm
I think all the install.xml will do is to describe the extension, e.g. name, version, author, and link. No more core file modifications.
Correct, see: https://github.com/opencart/opencart/bl ... r.php#L217

Those infos are needed and stored:

Code: Select all

'extension_id'          => 0,
'extension_download_id' => 0,
'name'                  => $name,
'code'              	=> basename($filename, '.ocmod.zip'),
'version'               => $version,
'author'                => $author,
'link'                  => $link
While the first 2 are internal, the other have to be defined inside the install.xml
Unfortunetaly no version check is made.

Beside the already known install.xml, there is the folder upload [optional] which can contain several subfolders like system, admin, etc.
See: https://github.com/opencart/opencart/bl ... r.php#L289

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


User avatar
Guru Member

Posts

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

Post by kestas » Sat Jan 30, 2021 9:35 pm

madimar wrote:
Tue Dec 22, 2020 2:04 am
straightlight wrote:
Mon Dec 21, 2020 8:40 am
Directly into the install.xml file to search for lines and alter them.
DO YOU MEAN OCMOD ?!?
I do not understand something... @Straightlight always talking to use events everywhere and what is it? OCMOD again? And OCMOD has been removed from OC4.x.x.x... Can anybody explain. I'm confused :crazy:

Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am

Post by OSWorX » Sun Jan 31, 2021 1:45 am

kestas wrote:
Sat Jan 30, 2021 9:35 pm
.. OCMOD has been removed from OC4.x.x.x...
Can anybody explain.
OCMod (therefore also the function modification(..) as sometimes used since OC 2.x) is OBSOLETE!
Removed, niente, nothing, nichts, rien ne va plus ..

From the new Version on - 4.x (3.1.x was planned, but too many changes, therefore 4.0.x) - only events are allowed and possible.
Daniel has screwed that up to replace to old "modification" system with OCMod (and VQMod should be also not be allowed anymore).

But what is true is, that to upload new Extensions, you will use furthermore the well know package.
Including:

a. install.xml
b. upload folder

Infos inside the .xml are used only to store the infos (see the header inside) in the database (currently install.xml is not needed, only the upload folder - maybe Daniel will change that).
The tags as we know, file, path, etc. are not used anymore.

Extension package files will have the same convention as now:
my-extension-v.1.0.0-OC-4.0.1.ocmod.zip

Unfortunetaly I suggested to use another extension (not ocmod furthermore), but Daniel declined.

That's all.

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


User avatar
Guru Member

Posts

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

Post by straightlight » Sun Jan 31, 2021 3:29 am

I do not understand something... @Straightlight always talking to use events everywhere and what is it? OCMOD again? And OCMOD has been removed from OC4.x.x.x... Can anybody explain. I'm confused
Simple. That's because an Event is not OCMod whereas Events is rather an Engine handled by the core.

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 » Sun Jan 31, 2021 5:09 pm

kestas wrote:
Sat Jan 30, 2021 9:35 pm
madimar wrote:
Tue Dec 22, 2020 2:04 am
straightlight wrote:
Mon Dec 21, 2020 8:40 am
Directly into the install.xml file to search for lines and alter them.
DO YOU MEAN OCMOD ?!?
I do not understand something... @Straightlight always talking to use events everywhere and what is it? OCMOD again? And OCMOD has been removed from OC4.x.x.x... Can anybody explain. I'm confused :crazy:
One more: while you are a "developer", all Informations about what is an "Event" and why it is not a "OCMod" and why Events are used in future only and not OCMods (or other derivats), can be read in many postings here in this Forum and in the current code at GitHub - read there also the pull requests and discussions.

As already written, it will confuse many people in future, because the extension package names are only accepted by the system (to install), when they are named xyz.ocmod.zip
From OpenCart 4.0.x on, the already known OCMod is dead.

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


User avatar
Guru Member

Posts

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

Post by Exel » Sat Feb 13, 2021 5:30 pm

I've been looking into code injection into the template files and came across this thread. Lots of good ideas and talk here.

I came up with a simpler solution (I think) than what's been posted here. It works in the same principal as some of the other posts online but it has the nice added bonus that the only thing you need to worry about is finding the html line of what you want to either find / replace ($find var below).

For this we need to create a module / modification .twig file for whatever we are injecting into the target .twig file.

For example, for simplicity sake, if you want to add..

Code: Select all

{% if exel %}
<div class="test">{{ exel }}</div>
{% else %}
<div class="test">No</div>
{% endif %}
You would make a .twig file in your module / modification view folder.

I've seen some people do this inline with the template buffer posted in the OP but we can take advantage of the already included $this->load->view().

This way it's a lot cleaner to read and maintain.

We also need to pass whatever variables are needed to this .twig file.

In this instance, I am targeting: admin/view/catalog/product_form/after

My function looks something like this:

Code: Select all

		
public function injectView(&$route, &$data, &$output) {

	$module_twig = $this->load->view('extension/crafted/shipping/craftedanomalies', $data);

	$find = '<legend>Specification</legend>';

	$replace = '<legend>Specification</legend>' . $module_twig;

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

}

Where:

$module_twig = This is loading the .twig file / content you want to inject, merging the php / html / twig content and passing the $data to it.

extension/crafted/shipping/craftedanomalies = directory to the .twig file / content you want to inject

$find = The fully converted html string that you want your content to either replace, append to, etc. In my instance, You can see I just appended my .twig to this as it lined up perfectly to where I wanted it.

$replace = This is where you'll either replace or append data.

$output = We are using the /after event $output to push the data.


The nice added bonus of this is you can use the exact same $find for multiple modifications, etc and they will all output from different event calls.

Newbie

Posts

Joined
Sat Feb 13, 2021 5:16 pm

Post by OSWorX » Fri Mar 04, 2022 12:34 am

Exel wrote:
Sat Feb 13, 2021 5:30 pm
I've been looking into code injection into the template files and came across this thread. Lots of good ideas and talk here.

I came up with a simpler solution (I think) than what's been posted here. It works in the same principal as some of the other posts online but it has the nice added bonus that the only thing you need to worry about is finding the html line of what you want to either find / replace ($find var below).
What you describe here, is the usual way to "manipulate/edit" existing HTML output (stored in the $output variable).
This can be only achived, when after is used to call the function (only then you will get the parsed HTML code).

But when before is used, the $output variable does not exist, so when this method is used (and has some valid reasons to do so), the template must be fetched before.
This is what JNeuhoff tries to explain ..

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


User avatar
Guru Member

Posts

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

Post by straightlight » Fri Mar 04, 2022 1:04 am

The /before and /after triggers are already explained on the Wiki documentation on anyhow.

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 » Fri Mar 04, 2022 1:33 am

straightlight wrote:
Fri Mar 04, 2022 1:04 am
The /before and /after triggers are already explained on the Wiki documentation on anyhow.
Correct .. but who is reading it?
And (btw.), this "Wiki" must be found to be able to read it.
And "error free" is something else, also wether complete nor easy to understand sometimes ..

Just for those, who want to read more: https://github.com/opencart/opencart/wiki/Events-System

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


User avatar
Guru Member

Posts

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

Post by straightlight » Fri Mar 04, 2022 3:31 am

OSWorX wrote:
Fri Mar 04, 2022 1:33 am
straightlight wrote:
Fri Mar 04, 2022 1:04 am
The /before and /after triggers are already explained on the Wiki documentation on anyhow.
Correct .. but who is reading it?
And (btw.), this "Wiki" must be found to be able to read it.
And "error free" is something else, also wether complete nor easy to understand sometimes ..

Just for those, who want to read more: https://github.com/opencart/opencart/wiki/Events-System
Simple, it's to those who can. That's what true developers are about.

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 » Fri Mar 04, 2022 4:55 am

straightlight wrote:
Fri Mar 04, 2022 3:31 am
Simple, it's to those who can. That's what true developers are about.
Yes (who can read), but don't forget, this here aims to be a user forum, not a developers discussion board (that's what GH is for).

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


User avatar
Guru Member

Posts

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

Post by straightlight » Fri Mar 04, 2022 5:51 am

OSWorX wrote:
Fri Mar 04, 2022 4:55 am
straightlight wrote:
Fri Mar 04, 2022 3:31 am
Simple, it's to those who can. That's what true developers are about.
Yes (who can read), but don't forget, this here aims to be a user forum, not a developers discussion board (that's what GH is for).
Sadly, the only official documentation available from Opencart to show in details how Events works is on Github. In addition, there are also available free extensions on the Marketplace where extension developers were also able to achieve this, including one of my extensions that does show how to use Events on the same location. At this point, reading becomes secondary.

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 Majnoon » Tue Jun 07, 2022 1:28 pm

straightlight wrote:
Fri Mar 04, 2022 5:51 am
one of my extensions that does show how to use Events on the same location.
Which extension you are referring to ?

Active Member

Posts

Joined
Fri Feb 05, 2021 8:29 pm

Post by samdz » Tue Jan 09, 2024 2:21 am

JNeuhoff wrote:
Fri Dec 11, 2020 8:55 pm
It can be done with a before-event handler, though it's a bit convoluted:

Code: Select all

	// event handler for admin/view/catalog/product_form/before
	public function eventViewCatalogProductFormBefore( &$route, &$data, &$template_code=null ) {

		$template_buffer = $this->getTemplateBuffer( $route, $template_code );

		$search  = '{% if customer_group.customer_group_id == product_special.customer_group_id %}';
		$replace = '{% if customer_group.customer_group_id == product_special.customer_group_id and some_new_variable_equal_true %}';
		$template_buffer = str_replace( $search, $replace, $template_buffer );

		$template_code = $template_buffer;

		return null;
	}


	// return template file contents as a string
	protected function getTemplateBuffer( $route, $event_template_buffer ) {
		// if there already is a modified template from view/*/before events use that one
		if ($event_template_buffer) {
			return $event_template_buffer;
		}

		// load the template file (possibly modified by ocmod and vqmod) into a string buffer
		if ($this->isAdmin()) {
			$dir_template = DIR_TEMPLATE;
		} else {
			if ($this->config->get('config_theme') == 'default') {
				$theme = $this->config->get('theme_default_directory');
			} else {
				$theme = $this->config->get('config_theme');
			}
			$dir_template = DIR_TEMPLATE . $theme . '/template/';
		}
		$template_file = $dir_template . $route . '.twig';
		if (file_exists( $template_file ) && is_file( $template_file )) {
			$template_file = $this->modCheck( $template_file );
			return file_get_contents( $template_file );
		}
		if ($this->isAdmin()) {
			trigger_error("Cannot find template file for route '$route'");
			exit;
		}
		$dir_template = DIR_TEMPLATE . 'default/template/';
		$template_file = $dir_template . $route . '.twig';
		if (file_exists( $template_file ) && is_file( $template_file )) {
			$template_file = $this->modCheck( $template_file );
			return file_get_contents( $template_file );
		}
		trigger_error("Cannot find template file for route '$route'");
		exit;
	}


	protected function isAdmin() {
		return defined( 'DIR_CATALOG' ) ? true : false;
	}


	protected function modCheck( $file ) {
		// return a PHP file possibly modified by OpenCart's system/storage/modification,
		//   and then possibly modified by vqmod (see also https://github.com/vqmod/vqmod)

		// Use OpenCart's modified file is available
		$original_file = $file;
		if (defined('DIR_MODIFICATION')) {
			if ($this->startsWith($file,DIR_APPLICATION)) {
				if ($this->isAdmin()) {
					if (file_exists( DIR_MODIFICATION . 'admin/' . substr($file,strlen(DIR_APPLICATION)) )) {
						$file = DIR_MODIFICATION . 'admin/' . substr($file,strlen(DIR_APPLICATION));
					}
				} else {
					if (file_exists( DIR_MODIFICATION . 'catalog/' . substr($file,strlen(DIR_APPLICATION)) )) {
						$file = DIR_MODIFICATION . 'catalog/' . substr($file,strlen(DIR_APPLICATION));
					}
				}
			} else if ($this->startsWith($file,DIR_SYSTEM)) {
				if (file_exists( DIR_MODIFICATION . 'system/' . substr($file,strlen(DIR_SYSTEM)) )) {
					$file = DIR_MODIFICATION . 'system/' . substr($file,strlen(DIR_SYSTEM));
				}
			}
		}

		// Don't use VQmod 2.3.2 or earlier if available
		if (array_key_exists('vqmod', get_defined_vars())) {
			trigger_error( "You are using an old VQMod version '2.3.2' or earlier, please upgrade your VQMod!" );
			exit;
		}

		// Use modification through VQmod 2.4.0 or later if available
		if (class_exists('VQMod',false)) {
			if (VQMod::$directorySeparator) {
				if (strpos($file,'vq2-')!==FALSE) {
					return $file;
				}
				if (version_compare(VQMod::$_vqversion,'2.5.0','<')) {
					trigger_error( "You are using an old VQMod version '".VQMod::$_vqversion."', please upgrade your VQMod!" );
					exit;
				}
				if ($original_file != $file) {
					return VQMod::modCheck($file,$original_file);
				}
				return VQMod::modCheck($original_file);
			}
		}

		// no VQmod
		return $file;
	}


	protected function startsWith( $haystack, $needle ) {
		if (strlen( $haystack ) < strlen( $needle )) {
			return false;
		}
		return (substr( $haystack, 0, strlen($needle) ) == $needle);
	}
The basic idea is to use a simple str_replace call in the event handler.
thank you for the code..
I have tried, it's working but I need it to work with opencart extention folder
Please, have any idea for opencart version 4.x ??

Active Member

Posts

Joined
Fri Jan 28, 2011 3:02 am

Post by JNeuhoff » Tue Jan 09, 2024 2:33 am

It's a similar logic for OpenCart 4:

Code: Select all

	// event handler for admin/view/catalog/product_form/before
	public function eventViewCatalogProductFormBefore( string &$route, array &$data, string &$template_code ): void {
		// ....
		$code = $this->getTemplateCode($route,$code);
		// .... modify $code
		$template_code = $code;
	}

	protected function getTemplateCode( $route, &$code ) {
		if (!empty($code)) {
			return $code;
		}

		$file = DIR_TEMPLATE . $route . '.twig';
		if (is_file($file)) {
			$code = file_get_contents( $file );
		} else {
			trigger_error("Cannot find template file for route '$route'");
			exit;
		}

		return $code;
	}

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am

Who is online

Users browsing this forum: No registered users and 9 guests