21
phatjew
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/19 20:08

  • phatjew

  • Just popping in

  • Posts: 56

  • Since: 2003/10/8


Two questions:

1. I have two files where constants reside in the language/english directory, modinfo.php and main.php.

The following line is in modinfo.php:

define("_MI_PJREVIEWS_NAME","Reviews");

in my main.php, I want to do this:

define("_XD_PJREVIEWS_DOCS",_MI_PJREVIEWS_NAME);

But this does not work. Why can't one file see the other, if these are all defined globally? Does this mean that unless you specifically include a language constants file, it won't conflict with another one? And doesn't THAT mean that I could have two language constants in different "cloned" modules named the same with no conflict?


2.What do I have to do to get these to work in the main.php language file:
define("_XD_PJREVIEWS_DOCS",$module->getVar('name'));

or

define("_XD_PJREVIEWS_DOCS",$xoopsModule->name());

22
mvandam
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/19 22:31

  • mvandam

  • Quite a regular

  • Posts: 253

  • Since: 2003/2/7 2


Quote:

define("_XD_PJREVIEWS_DOCS",_MI_PJREVIEWS_NAME);

This should work, but you have to define _MI_PJREVIEWS_NAME *before* you reach this statement.

Note, you *can* have constants with the same name in different files. BUT if both files are loaded then you get the conflict. The trouble is XOOPS core loads several files from each module to e.g. display the menu block, or if you have blocks from several modules on the same page. This can lead to conflicts. So even if you are visiting somesite.com/xoops/modules/news/index.php which is loading modules/news/language/english/main.php; if you show the 'recent news' block from the 'news2' clone, then it is loading automatically modules/news2/language/english/main.php. So if you duplicate the constants you are likely to get errors. Try it.

In answer to your second question use $module->getVar('name'), as the functions like 'name()' are deprecated.

23
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/19 23:30

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2



Actualy this is quiet tricky, as there are many referances that needs to be changed to make the modules easy rename, moveable and parallel reuseable.

I've been trying to make a solusion, and I actualy belive the following is quiet useable.

To start from xoops_version.php.
This code will automatically change the dirname to the name of the dir the module is currently resident in:
(Put it in the top of the xoops_version.php file)
// First split and Reverse current file path.
$pathparts array_reverse(explode("/"$_SERVER["SCRIPT_FILENAME"])); 
// Then select the correct part as the dirname, and make sure it's a constant.
$modversion['dirname'] = constant($pathparts[1]);
// Aditionally prepare a prefix for the language constants.
$VarPrefix '_MI_'.strtoupper($pathparts[1]);
// And use the language constants
$modversion['name'] = constant($VarPrefix.'_NAME');
$modversion['description'] = constant($VarPrefix.'_DESC');


In the language file modinfo.php I have the following code:
// Prepare a prefix for the language constants.
$pathparts array_reverse(explode("/"$_SERVER["SCRIPT_FILENAME"])); 
$VarPrefix '_MI_'.strtoupper($pathparts[1]);
// Make all the defines.
define($VarPrefix.'_NAME''ModuleName');
define($VarPrefix.'_DESC''A description of the module.');


In the languagefile for all other modules, eg. /language/english/main.php I have the following
$VarPrefix '_MD_'.strtoupper($xoopsModule->getVar('dirname'));

define($VarPrefix.'_GREETING''Hello, ');
define($VarPrefix.'_ANONYMOUS','My friend, please log in!');


In the module, blocks etc. I use the language constants like this:
$VarPrefix '_MD_'.strtoupper($xoopsModule->getVar('dirname'));

$xoopsTpl->assign('greeting'constant($VarPrefix.'_GREETING'));

$uname = !empty($xoopsUser) ? $xoopsUser->getVar('uname','E') :  constant($VarPrefix.'_ANONYMOUS');


The important part to remember is to use constant() around the language variables.

I also use
$ModuleUrlPath XOOPS_URL."/modules/".$xoopsModule->getVar('dirname');
    
$ModuleRootPath XOOPS_ROOT_PATH."/modules/".$xoopsModule->getVar('dirname');

in modules, blocks and admin, aswell as in smarty - to make it easier to always use the correct path.

I hope I remembered everything important to explain the working of this.

Note: I'm not sure if it's okay to have $pathpart and $VarPrefix without module prefixex. Didn't have time to go into that yet.

I've not tried it together with the database, comments, search etc. (as I haven't used either of those yet), but I hope others will provide examples, and improve on this code.

I hope you can use this code.
I probably won't have time to look further at this, the comming day or so. But rest assured that I'll be back!

Best regards,
Daigoro

24
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/20 0:26

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2


Hmmm,

I just noticed that the following line in xoops_version.php
$modversion['dirname'] = constant($pathparts[1]);

may be wrong, as I get a Warning [PHP]: constant(): Couldn't find constant skeleton in file modules/skeleton/xoops_version.php". (skeleton is my moduledir)

It's working as it should, though, so I guess it's something very simple. Is php looking for a variable named/defined skeleton, instead of using skeleton as a variable?
I'm probably just too tired to see it right now.

Regards,
Daigoro

25
mvandam
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/20 4:11

  • mvandam

  • Quite a regular

  • Posts: 253

  • Since: 2003/2/7 2


Daigoro:

Wow, very impressive... looks like you have worked out most of the details!

I was just wondering about a couple of little things:

1) 'constant' would only be needed to retrieve the value of a constant (e.g. constant($varPrefix . '_GREETING')), but won't by needed for the line:

$modversion['dirname'] = constant($pathparts[1]);

2) $ModuleUrlPath, $ModuleRootPath... do you define these inside all function(s) which need these? I don't think you would be able to use globals for these, unfortunately.

3) For $xoopsModule->getVar('dirname'), do you the global value $xoopsModule? I'll need to look through the XOOPS core again, but I wonder if this might fail for modules included as blocks (for example a 'recent links' block when viewing a forum page; in this case $xoopsModule would refer to 'newbb' so it might not give the correct prefix when loading the language files for 'mylinks'.) Not sure, I'll comment again about this once I know more.

4) Is there some way to use $varPrefix for generating function and class names too? This seems like the only obstacle to making a module which is trivially clonable. It looks like maybe 'create_function' would be useful: http://www.php.net/manual/en/function.create-function.php. It might be a little tricky to give the functions meaningful names (though one of the user-contributed-notes on the php.net site looks helpful), and also might be tricky to actually call the functions. There must be a better way. I'm not sure at the moment how to create classes with 'dynamic' names.


Anyway, let me just say again, very impressive - thanks for posting this!

Best regards,
Mike

26
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/20 21:02

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2


Thanks Mike.

I belive I have a very good understanding of the problem, but since I'm a complete novice when it comes to PHP programming (All I know, comes from hacking xoops!) I've spend alot of time trying to get this to have the right functionality.

To answer your questions:

1)
I tried to use
$modversion['dirname'] = $pathparts[1];
instead of
$modversion['dirname'] = constant($pathparts[1]);
but then got an error when trying to install the module.
(Fatal error, that prevented the modulelist to be shown - I had to manualy remove the module from the database).
When using the constant() function, I get the result I want - and the non-fatal error I mentioned.

2)
$ModuleUrlPath and $ModuleRootPath are defined in all functions where I need them (which sofar are all of the parts of the module I've been working on).
I would very much like to see them defined automatically by the xoops-core (perhaps as part of the header code?) - if it's possible.

3)
Yes, I use the global $xoopsModule. I've not tested all posibilities with this code, only a few php files to make sure the theory of opperation is sound and relative easy useable.
I rely on others, who know PHP better then me, to point out all the errors and help iron it out to a useable piece of code.

4)
I'm totaly blank when it comes to the OO part of PHP and Xoops. I hope some PHP-guru can help here.



Finaly, I'm working on a module I call Skeleton. It's soposed to show how to actualy make a XOOPS module, and explain - for normal deadly people (like me) who didn't learn php as a first language - how to do many things in php.
I've implemented the code I have presented here into that module, and it appears to function. I still need to do many things to the module (and give it a serious cleanup) before I'll release it. - I just wanted you to know this.

Regards,
Daigoro
(Who's pretty exhausted now - but hope hes english is readable!)

27
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/21 21:08

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2


About the wierd error I made a update-post about, I think I've found out why. I just haven't found a way to change it.

I use $_SERVER["SCRIPT_FILENAME"] in xoops_version.php in order to find out the path to the script. But it appears this variable doesn't always return the same.
I thought the variable would always contain the path to the script, but if the script has been included into another script, then it returns the path to the script including xoops_version.php instead.

Now every time the main menu is build, the xoops_version.php script is beeing included - and most times $_SERVER["SCRIPT_FILENAME"] contains the wrong path - leading to the error mentioned.

At this moment I still haven't found a solusion, but I'm still looking (and learning about php at the same time!).

Natually, if you have an idear on how to go about with this, please let me know.

Regards,
Daigoro

28
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/21 23:58

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2


Okay, I did find a way to fix this error. It truned out that $_SERVER["SCRIPT_FILENAME"] isn't realy the way to go. What we are interested in is from what file are we checking the path, not what script is currently run.
So, the path to use is this: realpath(__FILE__).

This in turn, makes the following changes:

xoops_version.php:
$pathparts array_reverse(explode("/"realpath(__FILE__)));


and in the language files:

$pathparts array_reverse(explode("/"realpath(__FILE__)));
$VarPrefix '_MI_'.strtoupper($pathparts[3]);


With these fixes in place, the scripts appear to be moveable to another directory without problems.

Regards,
Daigoro

29
phatjew
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/22 18:50

  • phatjew

  • Just popping in

  • Posts: 56

  • Since: 2003/10/8


Daigoro, this is really good stuff. I am going to try to implement it in my module (since I'm rewriting it right now anyway), and I'll let you know all of the problems/solutions that I come up with. Thanks for posting your code.

30
Daigoro
Re: XOOPS Dev Team: Would it be hard to propagate module name in all reference to the module?
  • 2004/3/22 19:53

  • Daigoro

  • Quite a regular

  • Posts: 223

  • Since: 2003/7/3 2


Phatjew,

No problem, it's a pleasure to share.

I've been thinking about rewriting this to make the code in the various php files simmilar to each other - so developers of modules needs to think less
I'll try it out this evening or tomorrow, and post my findings.
The overall structure will be the same, so just go ahead, and work on your module.

If you have some examples on how to use it with the database, commenets, searches, notification etc.. please post them here.

Regards,

Daigoro

Login

Who's Online

483 user(s) are online (372 user(s) are browsing Support Forums)


Members: 0


Guests: 483


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits