Dashboard error

A place for users and developers of the Xataface to discuss and receive support.

Dashboard error

Postby PolderBoy » Mon Apr 06, 2009 8:57 am

Dear All,

After following the tutorial Dashboard to the letter I get following error:

Fatal error: Call to undefined function getUser() in /home/administrator/www/BB/tables/Dashboard/Dashboard.php on line 4

It states in the tuorial that:
Note that we have defined the getUser() function elsewhere as a means of obtaining the current user and checking if a user is indeed logged in.

But where did we do that?

Thanks in advance,
PolderBoy
PolderBoy
 
Posts: 72
Joined: Fri Apr 03, 2009 12:53 am

Postby shannah » Sun Apr 12, 2009 11:09 am

I used the getUser() function as a placeholder to obtain the currently logged in user. You might implement this function as follows:

Code: Select all
function &getUser(){
    return Dataface_AuthenticationTool::getInstance()->getLoggedInUser();
}


-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Dashboard error

Postby Dataguru » Mon Feb 27, 2012 4:15 pm

I've spent an hour now looking for WHERE to put the getUser function.
I tried putting it before the getPermissions function in the tables/dashboard.php file and in the ApplicationDelegate.php file. Neither worked.to solve the Call to undefined function getUser() error.

My impression is that in your examples and the responses I've found by searching the forum for this error, you're assuming I would just know where to put it. Could you clarify please and please do so in the context of I'm just learning php.

on a sidenote, are the files for
http://xataface.com/wiki/Creating_a_Dashboard
somewhere downloadable?

Thanks,
Betty
Dataguru
 
Posts: 27
Joined: Mon Feb 20, 2012 8:12 am

Re: Dashboard error

Postby shannah » Mon Feb 27, 2012 4:35 pm

PHP functions can be defined in 3 scopes:

1. Global scope
2. As object methods
3. As class methods

If you see a function called like:
Code: Select all
myFunction()


Then this function must have been defined in the global scope and not inside any class definitions. E.g.

Code: Select all
function myFunction(){

    // do something
}


If you see a function called like:
Code: Select all
MyClass::myFunction()


Then this function must have been defined as a static method inside a class definition. E.g.
Code: Select all
class MyClass {
    public static function myFunction(){
        // do something
    }
}



If you see a function called like:
Code: Select all
$object = new MyClass();
$object->myFunction();


Then myFunction() must be a non-static method defined inside the MyClass definition. E.g.
Code: Select all
class MyClass {
    public function myFunction(){
        // do something
    }
}



So in the case of a getUser() function, in order to define a function that can be called the way you see in the examples, you must define it in the global scope. The global scope is any place that is NOT inside:
1. A class definition.
2. A function definition.

So where is a good place to define such a function in a Xataface application?

I usually create a file called functions.inc.php, then include it in the first line of my index.php file. E.g.
Code: Select all
<?php
require_once 'functions.inc.php';
//...
df_init(..., ...)->display(); ...  etc...


My getUser() function generally looks something like:
Code: Select all
function getUser(){
    static $user = -1;
    if ( is_int($user) and $user == -1 ){
        $auth = Dataface_AuthenticationTool::getInstance();
        $user = $auth->getLoggedInUser();
    }
    return $user;
}


In this example I declare $user static so that it will use the same variable across the entire request (so I only actually load the user from the authentication tool the first time). This is just a performance tweak since this function is likely to be called numerous times per request.


-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Dashboard error

Postby Dataguru » Mon Feb 27, 2012 4:42 pm

Thanks. I *think* I solved it by cobbling together a couple of different examples.

in tables/dashboard/deshboard.php
Code: Select all
<?php
class tables_dashboard {

    function getUser(&$record){
      $auth =& Dataface_AuthenticationTool::getInstance();
        $user =& $auth->getLoggedInUser();
    return $user;
    }

    function getPermissions(&$record){
        $the_user =$this->getUser($record);
        $user=$the_user->val('Role');
        if ( !$user){
            return Dataface_PermissionsTool::ALL();
        }
        return null;
    }
}

?>


Note that in your example
http://www.xataface.com/wiki/How_to_gra ... each_field
you had:
Code: Select all
        $user=$the_user->val('identifiant');

Since i have no idea what "identifiant" means, I guessed and changed that to the field that holds user roles from my ProgStaff table
Code: Select all
        $user=$the_user->val('Role');


The dashboard page is loading now (after I removed all the code that pointed at your bibliographies table which isn't specified in the http://xataface.com/wiki/Creating_a_Dashboard page).

Is this correct?

Thanks,
Betty
Dataguru
 
Posts: 27
Joined: Mon Feb 20, 2012 8:12 am

Re: Dashboard error

Postby Dataguru » Mon Feb 27, 2012 4:59 pm

ok.

To \index.php I added
Code: Select all
<?php
require_once 'functions.inc.php';


I created /functions.inc.php
Code: Select all
<?php

function getUser(){
    static $user = -1;
    if ( is_int($user) and $user == -1 ){
        $auth = Dataface_AuthenticationTool::getInstance();
        $user = $auth->getLoggedInUser();
    }
    return $user;
}

?>


to \tables\dashboard\dashboard.php I added
Code: Select all
<?php

class tables_dashboard {

    function getPermissions(&$record){
        $the_user =$this->getUser($record);
        $user=$the_user->val('Role');
        if ( !$user){
            return Dataface_PermissionsTool::ALL();
        }
        return null;
    }
}

?>


It's not happy again.
Fatal error: Call to undefined method tables_dashboard::getUser() in /var/www/virtual/mpower.eteam.ou.edu/tables/dashboard/dashboard.php on line 6
Dataguru
 
Posts: 27
Joined: Mon Feb 20, 2012 8:12 am

Re: Dashboard error

Postby shannah » Mon Feb 27, 2012 5:48 pm

You defined it in the global scope but are now trying to call it as an object method of the tables_dashboard table.
http://ca3.php.net/manual/en/langref.php
Read the "functions" and "Classes and Objects" sections for an introduction to PHP functions and classes. Best to have some fundamentals down before you get in too deep.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Dashboard error

Postby Dataguru » Wed Feb 29, 2012 9:08 am

Is there any chance of getting your relevant source files from the biblio app so I can see how you did it?
Dataguru
 
Posts: 27
Joined: Mon Feb 20, 2012 8:12 am

Re: Dashboard error

Postby shannah » Wed Feb 29, 2012 10:53 am

What biblio app are you referring to?
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Dashboard error

Postby Dataguru » Wed Feb 29, 2012 11:08 am

The bibliographies site you used for your example here
http://xataface.com/wiki/Creating_a_Dashboard

It does most of what I need to do in my project:
-limits users to viewing only their records
-Has a user-friendly dashboard

It would be very helpful to be able to see how you did it since you're the xataface guru and I'm php challenged!

If I ever get my xataface site working the way I want it to, I'd like to offer to help build documentation or provide feedback on existing documentation from the perspective of a challenged person who is learning both how to use xataface and php.e.g. it took me forever to figure the relationship.ini thing out, first adding multiple relationships for a table, and then for tables where there was more than one join needed. Might help reduce the demand on your time.

Thanks,
Betty
Dataguru
 
Posts: 27
Joined: Mon Feb 20, 2012 8:12 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 9 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved