Page 1 of 1

List unsorted first time in

PostPosted: Sun Jul 10, 2011 12:16 pm
by TBriggs
This is a follow up to my previous question. In that question my index.php included:
Code: Select all
if ( !isset( $_REQUEST['-sort'])){
$_REQUEST['-sort'] = 'dog_name asc';
$_GET['-sort'] = 'dog_name' asc';
}

which Steve suggested I change to:
Code: Select all
    if ( !$_POST and !isset($_GET['-sort']) ){
        $_GET['-sort'] = $_REQUEST['-sort'] = 'dog_name asc';
    }

Now, I don't know if this was true before and I just hadn't noticed, but my database (which opens with the list view) isn't sorted. If I go to any other view and come back to List, then it's sorted by dog_name as I would expect.

Does anyone know why it isn't sorted on initial opening?

Thanks.

Re: List unsorted first time in

PostPosted: Sun Jul 10, 2011 1:05 pm
by TBriggs
This is even weirder than I thought! Although the list isn't sorted, if you click on an entry, it brings up the record as if the list WAS sorted! For instance, say the list displays initially like this:

Charles
Arthur
Dave
Bert

If you click on the second record (Arthur), it brings up Bert's record!

Totally flummoxed now!

Re: List unsorted first time in

PostPosted: Sun Jul 10, 2011 1:11 pm
by shannah
Take your logic out of the index file, put it in the before handle request method and use app->getQuery() as the query instead of the php super globals.

Re: List unsorted first time in

PostPosted: Mon Jul 11, 2011 7:15 pm
by TBriggs
Sorry, Steve, that's way beyond my comprehension level.

Re: List unsorted first time in

PostPosted: Tue Jul 12, 2011 7:29 am
by ADobkin
These two pages in the documentation have some examples that should help explain what Steve proposed:

Configuring and Customizing the List Tab
http://xataface.com/documentation/how-to/list_tab

beforeHandleRequest Application Delegate Class Method
http://www.xataface.com/wiki/beforeHandleRequest

Perhaps the original how-to document needs to be updated to indicate other (better?) methods for default sorting. It can also be done using an __sql__ directive at the top of the fields.ini file with an ORDER BY clause if you prefer that method.

Steve, would it be possible in a future version of Xataface to put a default sort parameter in the __prefs__ section of fields.ini or something like that to make it easier to control?

Thanks,
Alan

Re: List unsorted first time in

PostPosted: Tue Jul 12, 2011 8:01 am
by shannah
Example. In the application delegate class:
Code: Select all
function beforeHandleRequest(){
    $query =& Dataface_Application::getInstance()->getQuery();
    if ( !$_POST and $query['-table'] == 'mytable' and !@$query['-sort'] ){
        $query['-sort'] = 'my_field desc';  // or 'my_field asc'
    }
}


And remove your code from the index.php file pertaining to default sorting.

The query returned in this method has already been processed and thus *always* has the -table parameter set. The reason you were having weird things happen in your previous approach is because it is possible that $_GET['-table'] won't be set at all but there would still be a default table. Hence in the list view without -table explicitly set, you get no default sort, but once you click on a record the -table parameter is part of the URL so the default sort is set.


-Steve

Re: List unsorted first time in

PostPosted: Sat Aug 06, 2011 1:44 pm
by TBriggs
Hmmm... That didn't work. I added the following code to my Application Delegates file (slightly different from Steve's example, which gave me an error, but copied from the link that Alan gave):

Code: Select all
<?
/**
* A delegate class for the entire application to handle custom handling of
* some functions such as permissions and preferences.
*/
class conf_ApplicationDelegate {
    /**
     * Returns permissions array.  This method is called every time an action is
     * performed to make sure that the user has permission to perform the action.
     * @param record A Dataface_Record object (may be null) against which we check
     *               permissions.
     * @see Dataface_PermissionsTool
     * @see Dataface_AuthenticationTool
     */
     function getPermissions(&$record){
         $auth =& Dataface_AuthenticationTool::getInstance();
         $user =& $auth->getLoggedInUser();
         if ( !isset($user) ) return Dataface_PermissionsTool::NO_ACCESS();
             // if the user is null then nobody is logged in... no access.
             // This will force a login prompt.
         $role = $user->val('Role');
         return Dataface_PermissionsTool::getRolePermissions($role);
             // Returns all of the permissions for the user's current role.
      }
    
     function beforeHandleRequest(){
         $app = Dataface_Application::getInstance(); 
          $query =& $app->getQuery();
          if ( !$_POST and $query['-table'] == 'greyhounds' and !@$query['-sort'] ){
           $query['-sort'] = 'dog_name asc';
         }
      }
}
?>


And my Index.php file now looks like this:

Code: Select all
<?
require_once '../Interface/xataface-1.1.5r2/dataface-public-api.php';

df_init(__FILE__, '../Interface/xataface-1.1.5r2');
$app =& Dataface_Application::getInstance();
$app->display();
?>


But now the file is never sorted by dog_name, before or after the initial display!

Re: List unsorted first time in

PostPosted: Sat Aug 06, 2011 5:06 pm
by shannah
What version of xataface are you using? Confirm that your beforeHandleRequest method is getting picked up (e.g. echo something these and exit see if it shows up).

-Steve