Disable action add new record

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

Disable action add new record

Postby fantomasdm » Sat May 10, 2008 3:12 am

Hi, I need to disable action add new record for a table when there is just another record with same userid filed, for example:
Code: Select all
if ( isset($_REQUEST['-new']) ){
if ( $_REQUEST['-table'] == 'personal_info') {
   $_GET['-action'] = 'list';
   $_REQUEST['-action'] = 'list';

   $auth =& Dataface_AuthenticationTool::getInstance();
   $user =& $auth->getLoggedInUser();


   $app =& Dataface_Application::getInstance();
   $query = "SELECT * FROM personal_info where iduser=".$user->val('id');
   $risultato = mysql_query($query,$app->db()) or die("Query fallita: " . mysql_error() );
   $num_righe = mysql_num_rows($risultato);

    /* Liberazione delle risorse del risultato */
    mysql_free_result($risultato);

   if ($num_righe <= 1)
   {
      $_GET['-action'] = 'list';
      $_REQUEST['-action'] = 'list';
   }
   else
   {
      unset( $_REQUEST['-new']);
      unset( $_GET['-new']); // disables creation of new records
   }
}
}


Sorry for my English!!
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby shannah » Sat May 10, 2008 6:36 pm

My first instinct is: Why not just make the userid field unique in mysql?
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby fantomasdm » Sun May 11, 2008 3:02 am

Hi, yes I have try to do this, but if is adding a new record is possible to catch exception for a more beautiful view?
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby fantomasdm » Sun May 11, 2008 3:05 am

Hi, yes I have try to do this, but if is adding a new record is possible to catch exception for a more beautiful view?
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby shannah » Tue May 13, 2008 1:57 pm

What do you mean by a more beautiful view. It should just come back to the edit form with a message saying "Error, record already exists..." or something like that. Are you experiencing something different? What is the preferred behavior?

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

Postby fantomasdm » Fri May 16, 2008 1:28 am

Hi, I get this error:
Fatal error: [pear_error: message="Duplicate entry into table 'filtri" code=112 mode= level=notice prefix="" info="On line 101 of file /var/www/vhosts/youtraffic.it/httpdocs/dataface/Dataface/Error.php in function printStackTrace()

I'm still usign xataface 0.7.1.

Isn't possible to disable add new record for a table when there is another record?
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby shannah » Fri May 16, 2008 6:29 am

Ah.. yes. in 0.7.1 it still had a bit of an ugly error message for this. This is fixed in 1.0

Best way is probably to do this is with a validate method.

e.g. validate__userid(&$record, $value, &$params){
$res = mysql_query("select userid from foo where userid='".addslashes($value)."' limit 1", df_db());
if ( mysql_num_rows($res) > 0 ){
$params['message'] = 'This user already has an entry...';
return false;
}
return true;
}


In 0.7.1 there may also be a bit of a bug you need to fix as well for this to work. Check
http://xataface.com/forum/viewtopic.php ... c&start=15
for info on how to fix this.

Ref: http://xataface.com/documentation/how-t ... validation

An alternative approach is to do this in the beforeInsert() method. See the handling errors section in:
http://xataface.com/documentation/tutor ... x/triggers

Now if you want the "new record" button to just not appear altogether there are two ways to do this.

One way is in the getPermissions() method, the other is to override the 'new' action with a different condition. And for this to work you need to know the value that the user would input into the new record form before he gets there (otherwise we don't know if the record is a duplicate).

Now suppose you have a function alreadyPostedRecord() which checks to see if this user has already posted a record. We might implement the getPermissions() method as follows:

Code: Select all
function getPermissions(&$record){
    $perms = Dataface_PermissionsTool::ALL();
    if ( alreadyPostedRecord() ) unset($perms['new']);
    return $perms;
}


!!!!Important. The getPermissions() method may be called dozens (or even hundreds of times per request, so it is important that your alreadyPostedRecord() function doesn't do a lot of work. You should cache results. E.g. make sure it doesn't perform any database or filesystem calls or any other expensive function, unless it only performs this the first time it is called, and returns the cached value the rest of the time.

One way to do this is with a static variable to cache the value.

e.g.
Code: Select all
function alreadyPostedRecord(){
    static $ret = -1;
    if ( $ret == -1 ){
        $res = mysql_query("select userid from foo where userid='".getUserID()."' limit 1", df_db());
        if ( mysql_num_rows($res) > 0 ) $ret = 1;
        else $res = 0;
    }
    return $ret;
}


That way it only executes the code inside the if statement once....




Finally you could override the new action in your actions.ini file to use a different condition as follows:

Code: Select all
[new > new]
    condition="!alreadyPostedRecord()"


Essentially this says to display the 'new' action if the alreadyPostedRecord() function returns false (i.e. the record is not already posted.


But the best solution is probably to start working with 1.0
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby fantomasdm » Sat May 17, 2008 2:55 am

HI, Thank for all!! I have choice for add [new > new ] in action.ini, and all works!!

I still using dataface 0.7.1 because with 1.0 php crash, i have to see apache log and post to you! but I have problem with ssh!

If I upgrade to 1.0, I have to make more modify? Is it still good this code?
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby shannah » Sat May 17, 2008 5:45 pm

It would be very helpful to know what is causing the crash in 1.0 for you. The goal is for applications to be completely compatible when you upgrade. If there is a bug, I'd like to squash it.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 25 guests

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