Don't use the internals CMSMS in your code.

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
Jeff
Power Poster
Power Poster
Posts: 961
Joined: Mon Jan 21, 2008 5:51 pm
Location: MI

Re: Don't use the internals CMSMS in your code.

Post by Jeff »

bess wrote: in class Foo

Code: Select all

class Foo extends CMSModule
Which is better?

Code: Select all

$this->GetPreference("fooPref");
$this->SetPreference("fooPref", 'new value');
or

Code: Select all

cms_utils::get_app_data('fooPref');
cms_utils::set_app_data('fooPref','new value');
These do two different things. The first saves a preference variable in the db. The second save a variable for the page load only.

For variables used during page load/execution the best way is to add it to the module object:

Code: Select all

$this->fooPref = 'new value';
echo $this->fooPref
Which is better?

Code: Select all

$feu = &$gCms->modules["FrontEndUsers"]['object'];
or (i suppose :p)

Code: Select all

$feu = cms_utils::get_module('FrontEndUsers','1.10');
and for

Code: Select all

$db =& $gCms->GetDb();
do we must done something ?
Best (IIRC works in 1.8 as well)
$feu = cmsms()->GetModuleInstance('FrontEndUsers','1.10');
$db = cmsms()->GetDb();
$smarty = cmsms()->GetSmarty()
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Don't use the internals CMSMS in your code.

Post by calguy1000 »

Code: Select all

$feu = cmsms()->GetModuleInstance('FrontEndUsers','1.10');
ajprog is correct except that the GetModuleInstance call is new in CMSMS 1.9
The second (optional) parameter allows you to specify a minimum version number.
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.
bess
Language Partners
Language Partners
Posts: 282
Joined: Thu Dec 18, 2008 9:37 am
Location: Bretagne

Re: Don't use the internals CMSMS in your code.

Post by bess »

if I want to keep backwards compatibility I should do this

Code: Select all

if(function_exists(cmsms) && cmsms() != null)
{
  $feu = cmsms()->GetModuleInstance('FrontEndUsers','1.10');
  $db = cmsms()->GetDb();
  $smarty = cmsms()->GetSmarty();
}
else
{
  $feu = &$gCms->modules["FrontEndUsers"]['object'];
  $db = &$gCms->GetDb();
  $smarty = &$gCms->getSmarty();
}
Am I right?
Last edited by bess on Mon Oct 11, 2010 5:51 pm, edited 1 time in total.
NaN

Re: Don't use the internals CMSMS in your code.

Post by NaN »

Function cmsms() is already in CMSms 1.8.2
NaN

Re: Don't use the internals CMSMS in your code.

Post by NaN »

One question about the $gCms stuff again.
Most module actions starts with

Code: Select all


if(!isset($gCms)) exit;

If $gCms is deprecated what would be the replacement for that?
Duketown

Re: Don't use the internals CMSMS in your code.

Post by Duketown »

Two questions.

1.
NaN wrote:
Function cmsms() is already in CMSms 1.8.2
Please confirm note the following: cmsms() was not available in version 1.8.1 and earlier versions available as of version 1.8.
So if I prepare a new module (or change an existing module to be ready for the next CMSMS version), I need to make sure that the minimum CMSMS version that the module can run in is set to 1.8 .2.

I've always seen the News module as a base if I needed a specific piece of coding (some sort of a reference module since all types of functions are available). If I however look in the SVN version at this moment (Revision 188) it tells me that News.module.php is not using this new setting yet (it contains the 'global $gCms;' line).

Is there another module that I/we should take as an example module?
----------------
2.
Should the phrase that is often around when front end coding is in place:

Code: Select all

$manager =& $gCms->GetHierarchyManager();
be rewritten to:

Code: Select all

$manager = cmsms()->GetHierarchyManager();
?

Thanks,

Duketown
Last edited by Duketown on Wed Oct 13, 2010 9:09 pm, edited 1 time in total.
Peciura

Re: Don't use the internals CMSMS in your code.

Post by Peciura »

Function cmsms() was defined on CMSms 1.8 .
$manager = cmsms()->GetHierarchyManager();
This will be ok. Check any CGExtensions based module released on 2010-10-06 (Company Directory, CGCalendar)
One question about the $gCms stuff again.
Most module actions starts with

What about this question ?

Code: Select all

if(!isset($gCms)) exit;
If $gCms is deprecated what would be the replacement for that?
Duketown

Re: Don't use the internals CMSMS in your code.

Post by Duketown »

Can I draw a conclusion that if I'm going to upgrade my modules or create a new one, that in all situations I can change:
... & $gCMS->...
into:
... cmsms()->...

Duketown
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Don't use the internals CMSMS in your code.

Post by calguy1000 »

As a habit, when releasing a module I try to always set the MinimumCMSVersion to the latest supported version which is now 1.8.1.  This solves lots of problems, so therefore using the cmsms() function should be safe from 1.8.1 forwards.  I checked this once, but I could check again.

As far as to what to put in the top of each action file, this should work:

Code: Select all

$gCms = cmsms(); if( !is_object($gCms) ) exit; 
Then there may be only one line to change in each of your action files.
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.
Post Reply

Return to “Developers Discussion”