Question about an integration module

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Question about an integration module

Post by Jo Morg »

I'm working on integrating a forums script with cmsms as fully as possible. So far I've been successful in pulling contents from the forums, and the CMSMS-->Forums Script is basically working. What I've been struggling a bit is with the CMSMS<--Forums side of it.
That is to say: triggering my module events from an outside script...

What I need (and have been failing to achieve...) is to get a full initiated instance of CMSMS object from an external script. I only need to get access to my own module, and its methods, from the cms object...

So my question is: what's the right way of calling CMS API from an outside script, in order to have access to my module and it's methods?

Note that this script is an include file that will be called from inside a function on the forums script side.

I have most of the functionality of the module already implemented and I expect to have a beta version on the forge in a few weeks.

Any help, pointers to any online docs, etc. is most welcome. I've been searching everywhere for this but haven't find anything useful, so far...
Thanks in advance...
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Question about an integration module

Post by Wishbone »

Not necessarily the answer to your question, but whenever I need a forum or other software to display data from a module, or show the main menu, or sidebar elements, etc, I do it in an <__iframe>. I create a simple template, stylesheet, and a page that uses the template to display the data I want. In the forum template, I <__iframe> the data in, so it looks like the forum is using CMSMS data.

Another alternative is to use JQuery's ajax methods to fetch the data.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

lol, as you said: Not necessarily the answer...

Appreciated the attempt though. That was the fastest reply I ever had to a topic I started! ;)

Yap, that would be a possibility if all I wanted was to visually integrate another script, whatever it may be. The thing here is that I need to synchronize both scripts, mainly the Front End users. The module I'm working on, is going to to this with Hooks. I's easy to do that with a script that doesn't use Objects. Now this is a different situation: the forums script doesn't use OOP while CMSMS does, which is good as far as I'm concerned, but... I'm not sure how to get access to cmsms object from an external script....
The way the bridge I'm working on is, at the moment, I can register a Front End user in cmsms and get it registered at the forums... I also need to be able to do it the other way around: register at the forums and get it registered automatically in Front End Users.
Now, Calguy provided an API that allows another module to do that easily in FEU. The problem is that I need to trigger my own module from another script, and if I'm right, I need an instance of cmsms object fully initiated in order to do that.... or am I wrong?
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Question about an integration module

Post by Wishbone »

Yeah.. I wasn't sure of the detail of integration you needed.. My answer helps 60% of the time. :)

Take a look at CMSMS's index.php .. It loads all the classes necessary, loads the module, then determines which page is being requested, and displays the template.

Inside the forum software, it might be possible to load the same classes, then load the modules, and you might have access to the objects.... Not sure how it works with 1.10's lazy loading of modules.
Last edited by Wishbone on Wed Dec 28, 2011 11:15 pm, edited 1 time in total.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Wishbone wrote: Take a look at CMSMS's index.php .. It loads all the classes necessary, loads the module, then determines which page is being requested, and displays the template.
Yap, I got as far as that, even had to take a look at the include.php and got an instance of cmsms on the other side....

However I end up running into a few issues that I'm not sure I know how to solve properly... (that is without taking a few shortcuts that end up becoming weaknesses in the project... if you know what I mean... :( ). That is way I'm trying to figure out how to do it properly.

The two MORE important issues are:

1 - as cmsms object is being loaded from inside a function it was giving me a stack-full of errors from other modules that use the GLOBAL $gcms variable instead of getting an instance of the cmsms object on their own... that was kind of solved by making $gcms global INSIDE my own function call (here's one of the things I'm no sure if I should be doing at this point....)...

2 - However I do it, I can't get an instance of my module (or any module, for that matter...) from inside the function... it keeps giving me NULL ...

Code: Select all

$modops = $gCms->GetModuleOperations();// fine so far: I do have this object...
$mBridge = $modops->get_module_instance('Bridge')); // NULL
var_dump($modops->get_module_instance('Bridge')); // NULL
I must be doing something wrong here!...

Anyway thanks for your help Wishbone. If you have any pointers to my problem (or any one???)...
Thanks all! :)
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Well...
As a follow up:
- apparently only a few modules appear to be loaded (very few...) when I get a ModuleOperations instance, and after calling LoadModules...

Why that happens is, at this point, a mystery to me... I have been inspecting the Lazy Load issue, but doesn't seem to be the problem here...

I actually made my module not lazy:

Code: Select all

function LazyLoadFrontend() { return FALSE; }
but it sill doesn't appear to be loaded...
Still stuck on this...
Any help? Please?
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

Re: Question about an integration module

Post by Wishbone »

look at the LoadModules code... Maybe you can find out how to load your module directly.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Wishbone wrote:look at the LoadModules code... Maybe you can find out how to load your module directly.
;) I did. No luck! It seems there are a few Globals that need to be passed along for it to work in several parts of the code. Maybe even the simple ...

Code: Select all

if (!isset($gCms)) exit;
... may be avoiding modules to load from inside a function that doesn't belong to the normal flow of CMSMS. If nobody comes up with a new idea or workaround for my problem, I will probably have to work my way into a sort of REST Server pattern, which is not so straightforward as I would like :( , but will work the same way... ::)

What is cryptic for me is the fact that a few modules actually DO get loaded... but the majority just come NULL and it doesn't seem to have anything to do with lazy_loading or being system modules.

Anyway... I'll be trying to get this done, as there isn't a proper forums script that can be well integrated into CMSMS and this one looks promising...

If anyone thinks of something, I'm still hoping to get some help with this bit....

TIA
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Actually....

The REST server API that is on the Forge (I just found it...) is marked as stale. Can any of the developers just say if it works?
That might very well be the answer to my problem...
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Question about an integration module

Post by calguy1000 »

I would write my own restful sort of interface.

It's simple enough since you already have a module started.

- Create a new action for each new function you want to call.
- Register your paremters with the SetParameterType call
- Double check that the request method is POST.
(I tend to use POST for anything that is a write based action and GET for anything that reads).
- Clean and validate your data
- Include a security mechanism so that not just anybody can use these actions.
- Use the CMSMS Core and/or module api functions to do what you need.
- Now all you need is a URL to this action. This can be retrieved by just calling $this->create_url() with its appropriate parameters, and displaying it in your admin interface somewhere.

Here's an overly simplified example.

Code: Select all

// in your modules InitializeFrontend() method.
$this->SetParameterType('uid',CLEAN_INT);
$this->SetParameterType('username',CLEAN_STRING);
$this->SetParameterType('password',CLEAN_STRING);
$this->SetParameterType('pskey',CLEAN_STRING);
// in your action.foo.php

Code: Select all

if( !isset($gCms) ) exit;
if( $_SERVER['REQUEST_METHOD'] != 'POST' ) exit;
if( !isset($params['uid']) || !isset($params['username']) || !isset($params['password']) || !isset($params['pskey']) ) exit; // error

if( $params['uid'] < 1 || $params['uid'] > 100000 ) exit; // bad
if( $params['pskey'] != 'mysecretvalue' ) exit; // bad

$feu = cms_utils::get_module('FrontEndUsers');
$res = $feu->SetUser($params['uid'],$params['username'],$params['password']);
// handle the result here, send back error message, whatever.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

1st of all: thanks for your help, appreciated!

Yea, I was starting to work on that too. Apparently it solves most of my problems as long as security is enforced, and has some added advantages. What I was actually not sure about was precisely this what would be the URL to the action.
So... this is the bit that I'm not too sure about:
calguy1000 wrote: - Now all you need is a URL to this action. This can be retrieved by just calling $this->create_url() with its appropriate parameters, and displaying it in your admin interface somewhere.
You mean that as long as I have registered the URL with CMSMS, the action on my module will be triggered, when the URL is used?
That was was the thing that was not so clear to me...
The rest seems pretty straightforward. And if that is what it takes, it simplifies the whole process, since I had most of the module functionality pretty much working...
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Question about an integration module

Post by calguy1000 »

if you call:

Code: Select all

$contentops = cmsms()->GetContentOperations();
$defaultpage = $contentops->GetDefaultContent();
$my_module_action_url = $this->CreateUrl('m1_','myaction',$defaultpage);
echo $my_module_action_url;
It should output a URL that you can POST to from an external application.

You will have to be careful to prepend all of your POST variables with the 'id' you specified (in this case m1_).

If i was calling one instance of CMSMS from another I would have the cms_http_request class available, and I would do code like this:

Code: Select all

$req = new cms_http_request();
$req->setMethod('POST');
$req->setTarget($my_module_action_url);
$req->addParam('m1_uid',100);
$req->addParam('m1_pskey','mysecretvalue');
$req->addParam('m1_username','johndoe');
$req->addParam('m1_password','password');
$result = $req->execute();
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Thank you so much!!!
You have pointed the way. Although I still have some work to do on the module, it seems simple enough. Your samples were clear and to the point.
That's all I needed. :)

Thanks again! Have a great New Year!
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1942
Joined: Mon Jan 29, 2007 4:47 pm

Re: Question about an integration module

Post by Jo Morg »

Ok! 1st of all: things are working pretty much as expected! calguy1000, your help was invaluable! And thank you too, Wishbone, for giving your thoughts on the subject.

Also, the fact that CMSMS has a cms_http_request class will come in handy when implementing cross domain integration (which will be a good thing to tackle in a future version of the module).

I still have a few questions though, I hope someone can help me with.

This has to do more with coherence than anything else:

1 - I need to set a few (2 or 3) persistent flags, which are internal to the module:

can I use the setPreference() and getPreference() methods for this purpose, or is it a better practice to use a module own db table to this?

I ask this mainly because, I believe it probably is a waste of resources to create a table just for a few settings, but if that's the way it should be, that's fine.

2 - is there a way to avoid processing the template when processing calguy1000's example action.foo.php?
calguy1000 wrote: // in your action.foo.php

Code: Select all

if( !isset($gCms) ) exit;
if( $_SERVER['REQUEST_METHOD'] != 'POST' ) exit;
if( !isset($params['uid']) || !isset($params['username']) || !isset($params['password']) || !isset($params['pskey']) ) exit; // error

if( $params['uid'] < 1 || $params['uid'] > 100000 ) exit; // bad
if( $params['pskey'] != 'mysecretvalue' ) exit; // bad

$feu = cms_utils::get_module('FrontEndUsers');
$res = $feu->SetUser($params['uid'],$params['username'],$params['password']);
// handle the result here, send back error message, whatever.

I do need to send/get a result in two situations, and avoid all the template processing.

Thanks in advance for your help.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Post Reply

Return to “Developers Discussion”