uList.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 
00030 class uList extends WebObject
00031 {
00032   function init()
00033   {
00034     $this->addSVar('recs_per_page', '10');
00035     $this->addSVar('current_page', '1');
00036 
00037     $this->addSVar('currentUser', UNDEFINED);      
00038     //set current the first user in the list
00039     $this->selectFirst();  
00040   }
00041 
00043   function selectFirst()
00044   {
00045     $this->add_users_rs();
00046 
00047     global $webPage;
00048     $rs = $webPage->getRecordset('users');
00049 
00050     $rs->MoveFirst();
00051     $first_user = ($rs->EOF() ? UNDEFINED : $rs->Field('username'));
00052 
00053     $this->setSVar('currentUser', $first_user);
00054   }
00055 
00056   function on_next($event_args)
00057   {
00058     $page = $event_args["page"];
00059     $this->setSVar('current_page', $page);
00060     $this->selectFirst();
00061   }
00062 
00063   function on_select($event_args)
00064   {
00065     $user = $event_args['username'];
00066     $this->setSVar('currentUser', $user);
00067   }
00068 
00069   function on_delete($event_args)
00070   {
00071     $book_id = WebApp::getSVar('books->selected_book');
00072     $username = $event_args['username'];
00073     shell(SCRIPTS."books/del_user.sh $book_id $username");
00074 
00075     //set current the first user in the list
00076     $this->selectFirst();
00077 
00078     //acknowledgment message
00079     $msg = T_("User v_username deleted.");
00080     $msg = str_replace('v_username', $username, $msg);
00081     WebApp::message($msg);
00082   }
00083 
00084   function on_add_user($event_args)
00085   {
00086     $username = $event_args['user'];
00087 
00088     //if username does not exist, add it
00089     include_once SCRIPTS.'user_data.php';
00090     $user_data = get_user_data($username);
00091     $username1 = $user_data['username'];
00092     if ($username1=='')
00093       {
00094         $user_data = array(
00095                            'username' => $username,
00096                            'password' => '', 
00097                            'name'     => $username,
00098                            'email'    => $username.'@domain.net',
00099                            'books'    => ''
00100                            );
00101         save_user_data($user_data);
00102       }
00103 
00104     //create the access file for this user in the current book
00105     $book_id = WebApp::getSVar('books->selected_book');
00106     shell(SCRIPTS."books/add_user.sh $book_id $username");
00107 
00108     //set the new user as the current user
00109     $this->setSVar('currentUser', $username);
00110   }
00111 
00112   function onRender()
00113   {
00114     $this->add_users_rs();
00115     $this->add_all_users_rs();
00116     WebApp::addVars($this->get_page_vars());
00117   }
00118 
00121   function add_all_users_rs()
00122   {
00123     //get the results
00124     $results = shell(SCRIPTS.'users/get_all_users.sh');
00125 
00126     //process the results and fill the recordset
00127     $lines = explode("\n", $results);
00128     $rs = new EditableRS('all_users');
00129     for ($i=0; $i < sizeof($lines); $i++)
00130       {
00131         $user = trim($lines[$i]);
00132         if ($user=='')  continue;
00133         $rs->addRec(array('user'=>$user));
00134       }
00135 
00136     //add the recordset to the page
00137     global $webPage;
00138     $webPage->addRecordset($rs);
00139   }
00140 
00142   function add_users_rs()
00143   {
00144     //check that it is not already added
00145     global $webPage;
00146     $rs = $webPage->getRecordset('users');
00147     if ($rs != UNDEFINED)  return;
00148 
00149     //calculate $first and $last records
00150     $rp = $this->getSVar('recs_per_page');
00151     $cp = $this->getSVar('current_page');
00152     $first = 1 + ($cp - 1)*$rp;
00153     if ($first < 1)  $first = 1;
00154     $last = $cp * $rp;
00155 
00156     //get the results
00157     $book_id = WebApp::getSVar('books->selected_book');
00158     $cmd = SCRIPTS."books/get_user_list.sh \"$book_id\" $first $last";
00159     $results = shell($cmd);
00160     //print "<xmp>$cmd \n\n$results\n</xmp>";  //debug
00161 
00162     //process the results and fill the recordset
00163     $lines = explode("\n", $results);
00164     $rs = new EditableRS('users');
00165     for ($i=0; $i < sizeof($lines); $i++)
00166       {
00167         $username = trim($lines[$i]);
00168         if ($username=='')  continue;
00169         $rs->addRec(array('username'=>$username));
00170       }
00171 
00172     //add the recordset to the page
00173     global $webPage;
00174     $webPage->addRecordset($rs);
00175   }
00176 
00177   function get_nr_of_recs()
00178   {
00179     $book_id = WebApp::getSVar('books->selected_book');
00180     $str_cnt = shell(SCRIPTS."books/get_user_nr.sh \"$book_id\"");
00181     $nr_of_recs = 0 + $str_cnt;
00182     return $nr_of_recs;
00183   }
00184 
00185   function get_page_vars()
00186   {           
00187     $current_page = $this->getSVar('current_page');
00188     $recs_per_page = $this->getSVar('recs_per_page');
00189     $nr_of_recs = $this->get_nr_of_recs();
00190 
00191     $nr_of_pages = ceil($nr_of_recs / $recs_per_page);
00192     if ($current_page > $nr_of_pages)  $current_page=$nr_of_pages;
00193 
00194     if ($current_page==$nr_of_pages)
00195       $next_page = "1"; //next page of the last page is the first page
00196     else
00197       $next_page = $current_page + 1;
00198 
00199     if ($current_page <= 1)
00200       {
00201         //previous page of the first page is the first page itself
00202         $prev_page = $current_page;
00203       }
00204     else
00205       $prev_page = $current_page - 1;
00206 
00207     $first_rec = 1 + ($current_page - 1)*$recs_per_page;
00208     if ($first_rec < 1)  $first_rec = 1;
00209     $last_rec   = $current_page * $recs_per_page;
00210     if ($first_rec > $nr_of_recs)  $first_rec = $nr_of_recs; 
00211     if ($last_rec  > $nr_of_recs)  $last_rec  = $nr_of_recs; 
00212 
00213     $page_vars = 
00214       array(
00215             //the number of the first record of the page
00216             "FirstRec" => $first_rec,
00217             //the number of the last record of the page               
00218             "LastRec"  => $last_rec,
00219             //the number of all records that can be retrieved by the query
00220             "AllRecs"  => $nr_of_recs,
00221             //current page of records that is retrieved
00222             "CurrPage" => $current_page,
00223             //the number of the next page
00224             "NextPage" => $next_page,
00225             //the number of the previous page
00226             "PrevPage" => $prev_page,
00227             //the number of the last page
00228             "LastPage" => $nr_of_pages
00229             );
00230     return $page_vars;
00231   }
00232 }
00233 ?>

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