Elxis CMS Forum

Extensions => Bots and plugins => Topic started by: cjstudio on April 07, 2024, 21:10:51

Title: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 07, 2024, 21:10:51
Hello. I recently discovered this CMS, and I have a question regarding plugins and triggers

I notice that plugins in this system seem to be tied specifically to content. Is there any way to implement plugins and related triggers that are not specific to content? For example, something similar to onUserLogin or onUserRegistration? The plugins I want to use do not render content but rather perform specific actions after a specific type of event (i.e., registration and login).

Is there a way to do this? I'm not new to coding so I don't mind rolling up my sleeves and digging in. Thanks!
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: datahell on April 09, 2024, 20:57:55
Hi and welcome to Elxis CMS. No, you cannot do that with content plugins. You can do it elsewhere but you need to be very specific on what you want to do in order to give you the proper answer. The problem is that login and/or registration may occur from very different/custom applications, not just from the usual/standard forms. So, it can become very tricky. If you have some flexibility on the exact event time it can become easier.
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 10, 2024, 06:00:29
Thank you for the response! Yes, I am flexible. I can adapt what I need to do based on the methodology used, so that shouldn't be a problem. Feel free to recommend an approach. Thanks again!
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 11, 2024, 20:15:58
Hello again. I think I answered incorrectly. Here is what I want to do:

I want to be able to include or setup "triggers" for 1) when a user registers and 2) when a member logs in. I wish to port an extension I coded for Joomla (it's similar to "An Archive of Our Own") over to this platform as Elxis seems to have sane developers. I also am using a standalone forum. This extension is crucial to a member site I'm developing. I want to be able to perform special actions after a user successfully registers and/or after a member logs in (i.e., redirect to a special page).

Additionally, I want to be able to stripe member data from Elxis to the forum once a user logs in.  I've written similar code in the past. For example, member logs into CMS successfully, system checks to see if that member exists in the forum member table. If not, the user data is then striped to the forum; whether I have to do a special hash of the "plain" password of course depends upon the encryption system the forum uses; if the forum uses the same method Elxis uses, then it's a simple copy of the user data from Elxis table(s) to corresponding forum user table fields. And finally, the "session" for that member would be started. No "bridge" code actually, just sharing of data and the forum code does what it needs to do. At least that has worked for me.

I hope this provides a clearer picture as to what I'm attempting to accomplish and thus why I need to create these "triggers". If there is a way to "inject" these triggers into Elxis code without hacking the core, that would be great. Thanks!
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: datahell on April 12, 2024, 21:24:57
As I wrote you before you can not do that with plugins as they are only for content. If you want a login form with a special action after login you can create such a functionality or use something pre-made. There are several Elxis extensions, mostly templates, that have such custom functionality built-in. Also the default login module (mod_login) supports ajax login and redirection to a custom page after login. So, it does already what you want.

Some general guidance regarding login.

You can login in 2 ways:
1. Submit a form with action to that page: user/login/elxis.html
2. Login via AJAX. The good about that is that you can execute additional things after login. AJAX login should be addressed to this page:
user/ilogin (but towards inner.php)

This is how you create the above link the proper way:
$action = $elxis->makeURL('user:ilogin', 'inner.php');

You can also have a fully custom login functionality but I will not get into this for now.

Note: In Elxis we use a portable URLs system called Elxis URIs. They are very easy to use, portable and multi-lingual.
Instead of writing a URL like that:
https://www.example.com/user/register.html
You write it like that:
user:register.html

The first part ( user: ) is the component in which we address the link. The rest part is related to that specific component. That component will handle the routing for the rest part of the Elxis URI.
An other example for a link to an article inside a category:
Instead of writing that:
https://www.example.com/mycategory/my-article.html (fixed, not multilingual, not portable)
We write it like that:
content:mycategory/my-article.html


You can do something similar for users registration. The best method to use depends on how you handle new user registrations. For instance if new accounts requires admin approval or email validation. In case of email validation which is something very common you could detect such actions by getting the current URL and check if it is the one that validates emails before activating a new user account (user/activate.html).

As for the forum I like that you have your own software, sounds great. However, if you need a forum solution integrated in Elxis you can use component Bubbles (https://www.elxis.net/edc/communication/167.html). It has all the functionality you need built-in, it is lightweight and secure.
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: datahell on April 12, 2024, 21:40:26
I also write you a javascript login (simplified) script and I mark where you can add custom actions after successful login.

function myLoginMethod() {
   var loginlink = document.getElementById('myform').action;
   var mytoken = document.getElementById('mysecuritytoken').value;
   var rnd = Math.floor((Math.random()*100)+1);
   var edata = { 'uname': 'some username', 'pword': 'some password', 'modtoken': mytoken, 'rnd': rnd };

   var successfunc = function(xreply) {
      try {
         var jsonObj = JSON.parse(xreply);
      } catch(e) {
         return false;
      }
      if (parseInt(jsonObj.success, 10) < 1) {
         alert(jsonObj.errormsg);
      } else {
         //do something custom, eg trigger actions and/or redirect the use somewhere:
         window.location.href = 'https://example.com/special-page.html';
      }
   };
   var errorfunc = function (XMLHttpRequest, textStatus, errorThrown) {
      alert('Login failed with error message '+errorThrown);
   };
   elxAjax('POST', loginlink, edata, null, null, successfunc, errorfunc);
}

Bind the above function on HTML:

<button type="button" onclick="myLoginMethod();">Login</button>

Note: From the upcoming Elxis 5.5 and afterwards users can also login with their email instead of their username (uname in Elxis).
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 18, 2024, 06:23:55
Hello again. I was away for a bit due to a family emergency.

I now understand that Elxis does not have a general "plugin engine" so to speak (i.e., like Joomla's JPlugin).

After reading what you've so graciously posted, and thank you for taking the time to provide such a detailed response, it looks like the best way to achieve what I want for my project is to use a combination of module and component coding (as this is going to be a fairly involved project). The module could contain custom forms that essentially provide the desired action as forms can be triggered via submission. I'll have to read up more on the URI system, as  this does indeed look very useful.

And, because forms can be hidden, I could pass parameters if needed via hidden forms and Ajax.

Please let me know if there are any mistakes in my understanding. Thanks again!
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: datahell on April 18, 2024, 20:59:47
I always reply with detailed instructions :)

You only need a module with an AJAX call to user:ilogin. If the default login module is not suitable for you, you can see how it works and build your own. Modules are very easy to develop. I will guide you if you need help.

The module may also execute its own tasks, AJAX driven, in a very secure way. There are many such modules you can see as examples. Here is a starter tip:

AJAX calls from modules should be addressed to "ajax" page of component content. The URL is constructed like this (PHP):
$elxis->makeURL('content:ajax', 'inner.php'); (returns something like https://example.com/inner.php/ajax - "content" is the default root for frontend so it is removed from the final URL)

When you request the above page you must pass a "f" variable which is the relative path to the file (a module file usually) you want to be included in order to execute further actions. Here are some sample variables (javascript):

var edata = {
   'f': 'modules/mod_mymodule/inc/rq.php',
   'task': 'dosomething',
   'color': 'red',
   'height': 172
};

File modules/mod_mymodule/inc/rq.php is the one that will be included and task dosomething is what it must execute.

An AJAX call starts with this (javascript):
elxAjax('POST', eurl, edata, null, null, successfunc, errorfunc);

Inside your "rq.php" file:

<?php
defined('_ELXIS_') or die ('Direct access to this location is not allowed');

$elxis = eFactory::getElxis();

if (isset($_POST['task'])) {
   $task = trim($elxis->sanitizeInput('task', INPUT_POST));
} else {
   $task = '';
}

if ($task == 'dosomething') {
    $color = trim($elxis->sanitizeInput('color', INPUT_POST));
   //do something
}

$response = array('success' => 0, 'message' => 'Invalid task!');

if (ob_get_length() > 0) { ob_end_clean(); }
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: application/json; charset=utf-8');
echo json_encode($response);
exit;
?>
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 20, 2024, 07:46:17
I think I understand it now. :D As I mentioned earlier, I'm coming from Joomla!, version 3 to be specific, and am one of the growing list of refugees from that project.

I was trying to understand how Elxis does things. It seems so easy - build a module, and either use AJAX to do a submit or otherwise code up a module with its own built-in form. This behavior effectively emulates what a Joomla plugin does - that is, it is called by an action based upon what process must fire at a certain point in the code execution. As I am also building a component, some of the component forms could likewise trigger actions on submit.

I am looking forward to digging in and having fun.... like way back in Mambo days.

And as a side note, thank you so much for your friendliness and help! I had gotten so used to the toxicity from that other CMS that it's going to take me a while to get used to it. ;D
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: datahell on April 20, 2024, 10:31:25
I am also a refugee, but from Mambo, long before Joomla existence. In Joomla's day 1, I decided not to go to that toxic, as you said, CMS, but build my own, Elxis (Actually Elxis borned before Joomla, on December 2005 and the first version was a modified version of Mambo). We kept naming (module, component, etc) almost as there were in Mambo and are in Joomla, but Elxis evolved and today it has its own framework and ecosystem. If you ever wonder how to do something in Elxis, think as a true developer, the CMS is there to help but doesn't limit you to follow the standard path. You can be yourself. Welcome to Elxis.
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on April 23, 2024, 20:54:52
Thanks for the welcome! Now digging through developer documentation to better familiarize myself with the system innards.
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: webgift on April 26, 2024, 11:35:29
Welcome to the Elxis community cjstudio  :)
Title: Re: Plugins and related "triggers" that are not tied to content
Post by: cjstudio on May 02, 2024, 03:13:43
Welcome to the Elxis community cjstudio  :)

Thank you. Much appreciated.  ;D