userList.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00034 class userList extends WebObject
00035 {
00036   function init()
00037   {
00038     $this->addSVar('recs_per_page', '10');
00039     $this->addSVar('current_page', '1');
00040 
00041     $this->addSVar('currentUser', UNDEFINED);      
00042     //set current the first user in the list
00043     $this->selectFirst();  
00044   }
00045 
00047   function selectFirst()
00048   {
00049     $this->add_users_rs();
00050 
00051     global $webPage;
00052     $rs = $webPage->getRecordset('users');
00053 
00054     $rs->MoveFirst();
00055     $first_user = $rs->Field('username');
00056 
00057     $this->setSVar('currentUser', $first_user);
00058   }
00059 
00060   function on_next($event_args)
00061   {
00062     $page = $event_args["page"];
00063     $this->setSVar('current_page', $page);
00064     $this->selectFirst();
00065   }
00066 
00067   function on_refresh($event_args)
00068   {
00069     //select the first page
00070     $this->setSVar('current_page', '1');
00071 
00072     //select the first user
00073     $this->selectFirst();
00074   }
00075 
00076   function on_select($event_args)
00077   {
00078     $user = $event_args['username'];
00079     $this->setSVar('currentUser', $user);
00080   }
00081 
00082   function on_delete($event_args)
00083   {
00084     $username = $event_args['username'];
00085     shell(SCRIPTS."users/del_user.sh $username");
00086 
00087     //set current the first user in the list
00088     $this->selectFirst();
00089 
00090     //acknowledgment message
00091     $msg = T_("User v_username deleted.");
00092     $msg = str_replace('v_username', $username, $msg);
00093     WebApp::message($msg);
00094   }
00095 
00096   function on_add_new($event_args)
00097   {
00098     //make currentUser UNDEFINED
00099     $this->setSVar('currentUser', UNDEFINED);
00100   }
00101 
00102   function onRender()
00103   {
00104     $this->add_users_rs();
00105     WebApp::addVars($this->get_page_vars());
00106   }
00107 
00109   function add_users_rs()
00110   {
00111     //check that it is not already added
00112     global $webPage;
00113     $rs = $webPage->getRecordset('users');
00114     if ($rs != UNDEFINED)  return;
00115 
00116     //calculate $first and $last records
00117     $rp = $this->getSVar('recs_per_page');
00118     $cp = $this->getSVar('current_page');
00119     $first = 1 + ($cp - 1)*$rp;
00120     if ($first < 1)  $first = 1;
00121     $last = $cp * $rp;
00122 
00123     //get the results
00124     $filter = WebApp::getSVar('userFilter->filter');
00125     $cmd = SCRIPTS."users/get_filtered_users.sh \"$filter\" $first $last";
00126     $results = shell($cmd);
00127     //print "<xmp>$cmd \n\n$results\n</xmp>";  //debug
00128 
00129     //process the results and fill the recordset
00130     $lines = explode("\n", $results);
00131     $rs = new EditableRS('users');
00132     for ($i=0; $i < sizeof($lines); $i++)
00133       {
00134         $username = trim($lines[$i]);
00135         if ($username=='')  continue;
00136         $rs->addRec(array('username'=>$username));
00137       }
00138 
00139     //add the recordset to the page
00140     global $webPage;
00141     $webPage->addRecordset($rs);
00142   }
00143 
00144   function get_nr_of_recs()
00145   {
00146     $filter = WebApp::getSVar('userFilter->filter');
00147     $str_cnt = shell(SCRIPTS."users/count_filtered_users.sh \"$filter\"");
00148     $nr_of_recs = 0 + $str_cnt;
00149     return $nr_of_recs;
00150   }
00151 
00152   function get_page_vars()
00153   {           
00154     $current_page = $this->getSVar('current_page');
00155     $recs_per_page = $this->getSVar('recs_per_page');
00156     $nr_of_recs = $this->get_nr_of_recs();
00157 
00158     $nr_of_pages = ceil($nr_of_recs / $recs_per_page);
00159     if ($current_page > $nr_of_pages)  $current_page=$nr_of_pages;
00160 
00161     if ($current_page==$nr_of_pages)
00162       $next_page = "1"; //next page of the last page is the first page
00163     else
00164       $next_page = $current_page + 1;
00165 
00166     if ($current_page <= 1)
00167       {
00168         //prev page of the first page is first page itself
00169         $prev_page = $current_page;
00170       }
00171     else
00172       $prev_page = $current_page - 1;
00173 
00174     $first_rec = 1 + ($current_page - 1)*$recs_per_page;
00175     if ($first_rec < 1)  $first_rec = 1;
00176     $last_rec   = $current_page * $recs_per_page;
00177     if ($first_rec > $nr_of_recs)  $first_rec = $nr_of_recs; 
00178     if ($last_rec  > $nr_of_recs)  $last_rec  = $nr_of_recs; 
00179 
00180     $page_vars = 
00181       array(
00182             //the number of the first record of the page
00183             "FirstRec" => $first_rec,
00184             //the number of the last record of the page               
00185             "LastRec"  => $last_rec,
00186             //the number of all records that can be retrieved by the query
00187             "AllRecs"  => $nr_of_recs,
00188             //current page of records that is retrieved
00189             "CurrPage" => $current_page,
00190             //the number of the next page
00191             "NextPage" => $next_page,
00192             //the number of the previous page
00193             "PrevPage" => $prev_page,
00194             //the number of the last page
00195             "LastPage" => $nr_of_pages
00196             );
00197     return $page_vars;
00198   }
00199 }
00200 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2