1
phillipd
Using forms in functions
  • 2005/4/3 3:11

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


When I put my XOOPS form code in a separate file and simply execute it like this it works fine:

if ($op == "whatever) {

include menu.php

}

But when I try to make a function out of what is in "menu.php" nothing seems to work. I then included:
include '../../../mainfile.php';
include '../../../include/cp_header.php';

This is "admin" code and I get a "You don't have permission to access this area" message.

Is it unusual to put smarty and XOOPS form code in a function? What might I be doing wrong?

Thanks

Doug P

Here is sample code of function which doesn't work but does when NOT a function:

function menu($which) {
global xoopsDB;
require_once SMARTY_DIR.'Smarty.class.php';
$xoopsTpl = new Smarty;
include XOOPS_ROOT_PATH."/header.php";
include_once XOOPS_ROOT_PATH."/class/xoopsformloader.php";

$form = new XoopsThemeForm("", "buttonmenuform", "index.php");

$sql = 'SELECT id,label,op,file,template, title
FROM '.$xoopsDB->prefix('bdr_buttons').'
WHERE grp = ".$which."
ORDER BY id';
$result = $xoopsDB->query($sql);
$i = 0;
$row = array();
$idx = array();
while(list($id,$label,$op,$file,$template, $title) = $xoopsDB->fetchRow($result)) {
$row['label'] = $label;
$row['op'] = $op;
$row['source'] = $file;
$idx[$i] = $row;
$i++;
}
echo " Which = $which Template = $template<br>";
$xoopsOption['template_main'] = $template;
$xoopsTpl->append('idx',$idx);
$xoopsTpl->assign('title',$title);

$form->assign($xoopsTpl);
$xoopsTpl->display('db:'.$xoopsOption['template_main']);

}

2
Dave_L
Re: Using forms in functions
  • 2005/4/3 4:07

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


One thing I notice is that you should be using the class XoopsTpl, rather than Smarty.

Include class/template.php instead of SMARTY_DIR.'Smarty.class.php', and instantiate the class as $xoopsTpl = new XoopsTpl.

3
phillipd
Re: Using forms in functions
  • 2005/4/3 5:24

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


Thanks for the tip but smarty and XOOPS form stuff still doesn't work in a function even with those changes. Any other suggestions? Has anyone done this? What might I be missing?

Doug P

4
phillipd
Re: Using forms in functions
  • 2005/4/3 6:03

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I do notice that in other modules, admin functions are included from one dir back (include ../functions.php). This seems to have solved the permissions problem but I'm still working on getting forms to work. I can't find a module with Smarty templates/forms in admin modules to compare code from. I'm doing Smarty easily in admin modules now but would like to automate some menu/template stuff by putting the form code in functions with callable parameters. The explanation below really doesn't matter. I just need to get smarty/forms to work in a PHP function. I'll keep hammerng at it but I'm just not having any luck.

I put my menu button (URL) text, ops, etc in the DB and have a general routine to display anything in the DB table as a menu. To change the menu text or operation called when the link is pressed, you change the DB rather than the code. I.E.


CREATE TABLE bdr_buttons (
id int(10) unsigned NOT NULL auto_increment,
grp int(10) unsigned NOT NULL default '0',
label varchar(255) NOT NULL default '',
op varchar(255) NOT NULL default '',
file varchar(255) NOT NULL default '',
template varchar(255) NOT NULL default '',
title varchar(255) NOT NULL default '',

PRIMARY KEY (id)
) TYPE=MyISAM;

INSERT INTO bdr_buttons VALUES (1,1,'Publish','publish','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');
INSERT INTO bdr_buttons VALUES (2,1,'Issues','issues','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');
INSERT INTO bdr_buttons VALUES (3,1,'Readyness','readyness','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');
INSERT INTO bdr_buttons VALUES (4,1,'Site Stats','stats','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');
INSERT INTO bdr_buttons VALUES (5,1,'Site Participation','participate','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');
INSERT INTO bdr_buttons VALUES (6,1,'Site Admin','admin','index.php','bdr_menu.html','Bechtel Daily Report Admin Functions');

INSERT INTO bdr_buttons VALUES (7,2,'Manage Sites','mansites','index.php','bdr_menu.html','Misc Admin Functions');
INSERT INTO bdr_buttons VALUES (8,2,'Manage Colors','mancolors','index.php','bdr_menu.html','Misc Admin Functions');
INSERT INTO bdr_buttons VALUES (9,2,'Manage issues','manissues','index.php','bdr_menu.html','Misc Admin Functions');
INSERT INTO bdr_buttons VALUES (10,2,'Return to Main Menu','datechanged','index.php','bdr_menu.html','Misc Admin Functions');

INSERT INTO bdr_buttons VALUES (11,3,'Add a site','addsite','index.php','bdr_menu.html','Site Admin Functions');
INSERT INTO bdr_buttons VALUES (12,3,'Edit a site','editsite','index.php','bdr_menu.html','Site Admin Functions');
INSERT INTO bdr_buttons VALUES (13,3,'Delete a site','deletesite','index.php','bdr_menu.html','Site Admin Functions');


Doug P

5
phillipd
Re: Using forms in functions
  • 2005/4/3 6:26

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


OK, even though I'm including XOOPS_ROOT_PATH.'/include/cp_header.php'; from a PHP function in an admin script, I don't have permssion to access it. Why is this?

I use something like this in an admin script:

function myMenu($which) {

include '../../../mainfile.php';
include XOOPS_ROOT_PATH.'/include/cp_header.php';

}

The cp_header.php line gives a permission denied when included from within the function...

I'm confused...

Thanks

Doug P

6
Dave_L
Re: Using forms in functions
  • 2005/4/3 6:29

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Do you have PHP debug on? What's the exact error you get?

7
phillipd
Re: Using forms in functions
  • 2005/4/3 6:32

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


Debug is on, no errors, just the "Sorry, You don't have permission to access this area" message from xoops. BTW I can make all this admin code work as long as I don't put the form code in a function. But then I have to create a separate php code file for each different form I want to display. i would rather pass a function some args and use only one function. Am I explaining myself sufficiently?

I guess my general question is:

To make smarty and XOOPS forms work in admin script PHP function, what is the correct order of the "Include" statements for XOOPS headers (cp_header) etc. HAve you ever done smarty and XOOPS forms from inside a PHP function?

Doug P

8
phillipd
Re: Using forms in functions
  • 2005/4/3 7:15

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


I guess my general question is:

To make smarty and XOOPS forms work in admin script PHP function, what is the correct order of the "Include" statements for XOOPS headers (cp_header) etc. Have you ever done smarty and XOOPS forms from inside a admin PHP function?

Doug P

9
Dave_L
Re: Using forms in functions
  • 2005/4/3 7:29

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Is there any reason why the "include"s need to be inside a function? You might try putting them at the beginning of the file instead, and use "require" instead of "include", so that you'll get a fatal error if any of them fail.

If the included file contains code that's executed immediately, as opposed to class or function definitions, having the "include" inside a function may cause it to behave differently than if it were outside a function.

10
phillipd
Re: Using forms in functions
  • 2005/4/3 8:26

  • phillipd

  • Quite a regular

  • Posts: 219

  • Since: 2004/4/20


Great idea, I didn't think of that, thanks. I'll try it tomorrow and let you know. I just thought that PHP functions were islands unto themselves and if I were to do anything like DB stuff for example I would have to re-include/re-require the XOOPS stuff in order to access the XOOPS API stuff in the function.

Although I know you only have to add "global xoopsDB" to access MySQL functions. Have you done forms/templates in a function yourself?

Doug P

Login

Who's Online

242 user(s) are online (147 user(s) are browsing Support Forums)


Members: 0


Guests: 242


more...

Donat-O-Meter

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

Latest GitHub Commits