/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/DataListSource.inc
ViewVC logotype

Diff of /nfo/php/libs/com.newsblob.phphtmllib/widgets/data_list/DataListSource.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by jonen, Sat Feb 22 21:08:37 2003 UTC revision 1.4 by jonen, Thu May 6 16:27:48 2004 UTC
# Line 3  Line 3 
3  /**  /**
4   * This file contains the Base DataListSource   * This file contains the Base DataListSource
5   * class which abstracts the data fetching/sorting   * class which abstracts the data fetching/sorting
6   * from the DataList.     * from the DataList.
7   *   *
8   *   *
9   * $Id$   * $Id$
# Line 15  Line 15 
15    
16    
17  /**  /**
18   *   *
19   *  This is the base class for managing data for   *  This is the base class for managing data for
20   * the DataList class.  This abstracts away the   * the DataList class.  This abstracts away the
21   * underlying data layer from the DataList, so   * underlying data layer from the DataList, so
22   * the data can come from multiple sources.     * the data can come from multiple sources.
23   * Most of the time the data will come from a   * Most of the time the data will come from a
24   * data base such as Mysql or Oracle.  This   * data base such as Mysql or Oracle.  This
25   * abstraction enables the data to also come from   * abstraction enables the data to also come from
26   * a tab delimited file, xml, php array   * a tab delimited file, xml, php array
27   *   *
28   * @author Walter A. Boring IV <waboring@buildabetterweb.com>   * @author Walter A. Boring IV <waboring@buildabetterweb.com>
29   * @package phpHtmlLib   * @package phpHtmlLib
30   */   */
31  class DataListSource {  class DataListSource {
32    
33          /**      /**
34           * The column descriptions       * The column descriptions
35           * for the data we are working on       * for the data we are working on
36           *       *
37           * @var array       * @var array
38           */       */
39          var $_columns = array();      var $_columns = array();
40    
41      /**      /**
42       * This is the message displayed when no data       * This is the message displayed when no data
# Line 45  class DataListSource { Line 45  class DataListSource {
45      var $_not_found_message = "No data was found";      var $_not_found_message = "No data was found";
46    
47    
48          /**      /**
49           * This holds various parameters relating       * This holds various parameters relating
50           * to the query of the data       * to the query of the data
51           *       *
52           */       */
53          var $_query_params = array("num_total_rows" => 0,      var $_query_params = array("num_total_rows" => 0,
54                                                             "offset" => 0,                                 "offset" => 0,
55                                                             "limit" => -1,                                 "limit" => -1,
56                                                             "orderby" => '',                                 "orderby" => '',
57                                                             "reverseorder" => '',                                 "secondaryorderby" => array(),
58                                                             "searchby" => '',                                 "reverseorder" => '',
59                                                             "searchvalue" => '',                                 "searchby" => '',
60                                                             "searchmodifier" => '',                                 "searchvalue" => '',
61                                                             "searchtype" => 'simple');                                 "searchmodifier" => '',
62                                   "searchtype" => 'simple');
63          /**  
64           * Holds the index into the array of data      /**
65           * so we can keep track of where we are       * Holds the index into the array of data
66           * when we are walking the array       * so we can keep track of where we are
67           * (only usefull for non DB children)       * when we are walking the array
68           *       * (only usefull for non DB children)
69           */       *
70          var $_data_index = 0;       */
71        var $_data_index = 0;
72          /**  
73           * A placeholder for data that is read/built      /**
74           * and stored locally.  Not all data sources       * A placeholder for data that is read/built
75           * have to use this.       * and stored locally.  Not all data sources
76           * Each entry in the array corresponds to 1 row       * have to use this.
77           * of data.       * Each entry in the array corresponds to 1 row
78           */       * of data.
79          var $_data = array();       */
80        var $_data = array();
81          /**  
82           *      /**
83           * The constructor       *
84           */       * The constructor
85          function DataListSource() {       */
86        function DataListSource() {
87          }  
88        }
89          /**  
90           * The main external API methods      /**
91           * that are used by the DataList       * The main external API methods
92           * Class       * that are used by the DataList
93           *       * Class
94           */       *
95         */
96          /**  
97           * Add a column of data to manage      /**
98           *       * Add a column of data to manage
99           * @param string - the title of the column       *
100           * @param string - the data value name       * @param string - the title of the column
101           * @param boolean - is the column sortable?       * @param string - the data value name
102           *                  default: FALSE       * @param boolean - is the column sortable?
103           * @param boolean - is the column searchable       *                  default: FALSE
104           *                  default: FALSE       * @param boolean - is the column searchable
105           * @param string - the sort order (ASC, DESC)       *                  default: FALSE
106           *                 default: ASC       * @param string - the sort order (ASC, DESC)
107           */       *                 default: ASC
108          function add_column( $title, $data_name, $sortable=FALSE,       */
109                                                    $searchable=FALSE, $sortorder="ASC") {      function add_column( $title, $data_name, $sortable=FALSE,
110                  $col = array();                            $searchable=FALSE, $sortorder="ASC") {
111                  $col["data_name"] = $data_name;          $col = array();
112                  $col["sortable"] = $sortable;          $col["data_name"] = $data_name;
113                  $col["searchable"] = $searchable;          $col["sortable"] = $sortable;
114                  $col["sortorder"] = $sortorder;          $col["searchable"] = $searchable;
115            $col["sortorder"] = $sortorder;
116                  $this->_columns[$title] = $col;          $col["reverseorder"] = false;
117          }  
118            $this->_columns[$title] = $col;
119        }
120          /**  
121           * The main Query function.  
122           * This function is responsible for doing      /**
123           * any data prefetching from a db,file       * The main Query function.
124           * and doing any sorting and searching       * This function is responsible for doing
125           * on it depending on the values passed       * any data prefetching from a db,file
126           * in from the DataList object       * and doing any sorting and searching
127           *       * on it depending on the values passed
128           * @param int - the offset into the data set       * in from the DataList object
129           * @param int - the # of rows to get       *
130           * @param string - the column to order the data by       * @param int - the offset into the data set
131           * @param string - order in asc or reverse?       * @param int - the # of rows to get
132           * @param string - the column in the dataset       * @param string - the column to order the data by
133           *                 to search by       * @param string - order in asc or reverse?
134           * @param string - the value to look for       * @param string - the column in the dataset
135           * @param string - the simple search modifier.       *                 to search by
136           *       * @param string - the value to look for
137           */       * @param string - the simple search modifier.
138          function query($offset, $limit, $orderby, $reverseorder,       *
139                                     $searchby, $searchby_value,       */
140                                     $simplesearch_modifier, $search_type ) {      function query($offset, $limit, $orderby, $reverseorder,
141                  $this->set_offset($offset);                     $searchby, $searchby_value,
142                  $this->set_limit($limit);                     $simplesearch_modifier, $search_type ) {
143                  $this->set_orderby($orderby);          $this->set_offset($offset);
144                  $this->set_reverseorder($reverseorder);          $this->set_limit($limit);
145                  $this->set_searchby($searchby);          $this->set_orderby($orderby);
146                  $this->set_searchby_value($searchby_value);          $this->set_reverseorder($reverseorder);
147                  $this->set_simplesearch_modifier( $simplesearch_modifier );          $this->set_searchby($searchby);
148                  $this->set_search_type( $search_type );          $this->set_searchby_value($searchby_value);
149            $this->set_simplesearch_modifier( $simplesearch_modifier );
150                  //now call child methods          $this->set_search_type( $search_type );
151    
152                  //do any pre query setup          //now call child methods
153                  //This depends on the source of the data  
154                  //but an example would be to build a          //do any pre query setup
155                  //sql query string if the source is a database          //This depends on the source of the data
156                  //or to validate/open a file from disk if the          //but an example would be to build a
157                  //source is a file.          //sql query string if the source is a database
158                  //or to do any checks on a memory cache          //or to validate/open a file from disk if the
159                  //if the data source is a sysvshm segment.          //source is a file.
160                  $this->do_prequery();          //or to do any checks on a memory cache
161            //if the data source is a sysvshm segment.
162                  //do the actual data fetching/sorting          $this->do_prequery();
163                  $this->do_query();  
164          }          //do the actual data fetching/sorting
165            return $this->do_query();
166          /**      }
167           * This function gets the next data row  
168           * from the query()      /**
169           *       * This function gets the next data row
170           * @return array()       * from the query()
171           */       *
172          function get_next_data_row() {       * @return array()
173             user_error("DataListSource::get_next_data_row() - Child must override");       */
174          }      function get_next_data_row() {
175           user_error("DataListSource::get_next_data_row() - Child must override");
176        }
177          /**  
178           * This is a method that should be defined by the      /**
179           * child class to do any pre-query type of things.       * This is called by the DataList object to allow
180           * Such as building a sql query string for a DB,       * us a chance to run the next row through a filter
181           * or checking to make sure the file on disk exists       *
182           * if the source is a file on disk.       * @param array - the row to run through the filter
183           *       * @return boolean - TRUE = allow the row. FALSE = drop it.
184           */       */
185          function do_prequery() {      function row_filter(&$row_data) {
186                  user_error("DataListSource::do_prequery() - Child must override");          return TRUE;
187          }      }
188    
189    
190          /**      /**
191           * This is the function that does the data fetching,       * This is a method that should be defined by the
192           * and sorting if needed.         * child class to do any pre-query type of things.
193           * If the source is a sql database, this is where the       * Such as building a sql query string for a DB,
194           * query gets called.  This function doesn't actually read the       * or checking to make sure the file on disk exists
195           * data from the DB yet.  That is what get_next_data_row()       * if the source is a file on disk.
196           * does.         *
197           *       */
198           */      function do_prequery() {
199          function do_query() {          user_error("DataListSource::do_prequery() - Child must override");
200                  user_error("DataListSource::do_query() - Child must override");      }
201          }  
202    
203          /**      /**
204           * A generic method API that can be used at the bottom       * This is the function that does the data fetching,
205           * half of the do_query() method to sort data that is       * and sorting if needed.
206           * stored locally.  This is only needed when the source       * If the source is a sql database, this is where the
207           * is a non database.       * query gets called.  This function doesn't actually read the
208           *       * data from the DB yet.  That is what get_next_data_row()
209           * It should operate on the $this->_data array       * does.
210           */       *
211          function sort() {       * @return boolean - the query passed/failed.
212          }       */
213        function do_query() {
214            user_error("DataListSource::do_query() - Child must override");
215      /**          return false;
216           * This function is used to set the      }
217           * message displayed when no data is found  
218           *      /**
219           * @param string       * A generic method API that can be used at the bottom
220           */       * half of the do_query() method to sort data that is
221         * stored locally.  This is only needed when the source
222         * is a non database.
223         *
224         * It should operate on the $this->_data array
225         */
226        function sort() {
227        }
228    
229    
230        /**
231         * This function is used to set the
232         * message displayed when no data is found
233         *
234         * @param string
235         */
236      function set_not_found_message( $mesg ) {      function set_not_found_message( $mesg ) {
237          $this->_not_found_message = $mesg;          $this->_not_found_message = $mesg;
238      }      }
# Line 229  class DataListSource { Line 244  class DataListSource {
244       * @return string       * @return string
245       */       */
246      function get_not_found_message() {      function get_not_found_message() {
247          return $this->_not_found__message;          return $this->_not_found_message;
248        }
249    
250        /**
251         * This returns the total number of rows
252         * in our entire data set
253         *
254         * @return int
255         */
256        function get_total_rows() {
257            return $this->_query_params["num_total_rows"];
258        }
259    
260        /**
261         * This is used to set the total # of
262         * rows we have in our data set
263         *
264         * @param int
265         */
266        function set_total_rows($num) {
267            $this->_query_params["num_total_rows"] = $num;
268        }
269    
270    
271        /**
272         * This sets the offset value
273         * and resets the index into the data
274         * array (in non DB children)
275         *
276         * @param int offset
277         */
278        function set_offset($offset) {
279            $this->_query_params["offset"] = $offset;
280            $this->_data_index = $offset;
281        }
282    
283        /**
284         * This function returns the value of the
285         * offset
286         *
287         * @return int
288         */
289        function get_offset() {
290            return $this->_query_params["offset"];
291        }
292    
293        /**
294         * This sets the orderby column name.
295         * This corresponds to the column
296         * that wants to be sorted/ordered, but
297         * not the actual direction (asc, desc)
298         *
299         * @param int offset
300         */
301        function set_orderby($orderby) {
302            $this->_query_params["orderby"] = $orderby;
303        }
304    
305        /**
306         * This function returns the value of the
307         * orderby
308         *
309         * @return int
310         */
311        function get_orderby() {
312            return $this->_query_params["orderby"];
313        }
314    
315        /**
316         * This sets the flag that tells us the
317         * direction in which to order the orderby
318         * column.
319         *
320         * @param int offset
321         */
322        function set_reverseorder($order) {
323            $this->_query_params["reverseorder"] = $order;
324        }
325    
326        /**
327         * This function returns the value of the
328         * reverseorder
329         *
330         * @return int
331         */
332        function get_reverseorder() {
333            return $this->_query_params["reverseorder"];
334        }
335    
336        /**
337         * This sets the column that we want to search
338         * from.
339         *
340         * @param int offset
341         */
342        function set_searchby($search_col) {
343            $this->_query_params["searchby"] = $search_col;
344        }
345    
346        /**
347         * This function returns the value of the
348         * searchby
349         *
350         * @return int
351         */
352        function get_searchby() {
353            return $this->_query_params["searchby"];
354        }
355    
356        /**
357         * This sets the data that we want to search
358         * for.
359         *
360         * @param int offset
361         */
362        function set_searchby_value($search_value) {
363            $this->_query_params["searchvalue"] = $search_value;
364        }
365    
366        /**
367         * This function returns the value of the
368         * search value
369         *
370         * @return int
371         */
372        function get_searchby_value() {
373            return $this->_query_params["searchvalue"];
374        }
375    
376        /**
377         * This sets the simple search modifier
378         *
379         *
380         * @param int offset
381         */
382        function set_simplesearch_modifier($search_modifier) {
383            $this->_query_params["searchmodifier"] = $search_modifier;
384        }
385    
386        /**
387         * This function returns the value of the
388         * search value
389         *
390         * @return int
391         */
392        function get_simplesearch_modifier() {
393            return $this->_query_params["searchmodifier"];
394      }      }
395    
396          /**      /**
397           * This returns the total number of rows       * This function is used to set
398           * in our entire data set       * the limit value, which limits
399           *       * the # of rows of data to allow
400           * @return int       * to return
401           */       */
402          function get_total_rows() {      function set_limit( $limit ) {
                 return $this->_query_params["num_total_rows"];  
         }  
   
         /**  
          * This is used to set the total # of  
          * rows we have in our data set  
          *  
          * @param int  
          */  
         function set_total_rows($num) {  
                 $this->_query_params["num_total_rows"] = $num;  
         }  
   
   
         /**  
          * This sets the offset value  
          * and resets the index into the data  
          * array (in non DB children)  
          *  
          * @param int offset  
          */  
         function set_offset($offset) {  
                 $this->_query_params["offset"] = $offset;  
                 $this->_data_index = $offset;  
         }  
   
         /**  
          * This function returns the value of the  
          * offset  
          *  
          * @return int  
          */  
         function get_offset() {  
                 return $this->_query_params["offset"];  
         }  
   
         /**  
          * This sets the orderby column name.  
          * This corresponds to the column  
          * that wants to be sorted/ordered, but  
          * not the actual direction (asc, desc)  
          *  
          * @param int offset  
          */  
         function set_orderby($orderby) {  
                 $this->_query_params["orderby"] = $orderby;  
         }  
   
         /**  
          * This function returns the value of the  
          * orderby  
          *  
          * @return int  
          */  
         function get_orderby() {  
                 return $this->_query_params["orderby"];  
         }  
   
         /**  
          * This sets the flag that tells us the  
          * direction in which to order the orderby  
          * column.  
          *  
          * @param int offset  
          */  
         function set_reverseorder($order) {  
                 $this->_query_params["reverseorder"] = $order;  
         }  
   
         /**  
          * This function returns the value of the  
          * reverseorder  
          *  
          * @return int  
          */  
         function get_reverseorder() {  
                 return $this->_query_params["reverseorder"];  
         }  
   
         /**  
          * This sets the column that we want to search  
          * from.  
          *  
          * @param int offset  
          */  
         function set_searchby($search_col) {  
                 $this->_query_params["searchby"] = $search_col;  
         }  
   
         /**  
          * This function returns the value of the  
          * searchby  
          *  
          * @return int  
          */  
         function get_searchby() {  
                 return $this->_query_params["searchby"];  
         }  
   
         /**  
          * This sets the data that we want to search  
          * for.  
          *  
          * @param int offset  
          */  
         function set_searchby_value($search_value) {  
                 $this->_query_params["searchvalue"] = $search_value;  
         }  
   
         /**  
          * This function returns the value of the  
          * search value  
          *  
          * @return int  
          */  
         function get_searchby_value() {  
                 return $this->_query_params["searchvalue"];  
         }  
   
         /**  
          * This sets the simple search modifier  
          *  
          *  
          * @param int offset  
          */  
         function set_simplesearch_modifier($search_modifier) {  
                 $this->_query_params["searchmodifier"] = $search_modifier;  
         }  
   
         /**  
          * This function returns the value of the  
          * search value  
          *  
          * @return int  
          */  
         function get_simplesearch_modifier() {  
                 return $this->_query_params["searchmodifier"];  
         }  
   
         /**  
          * This function is used to set  
          * the limit value, which limits  
          * the # of rows of data to allow  
          * to return  
          */  
         function set_limit( $limit ) {  
403          $this->_query_params["limit"] = $limit;          $this->_query_params["limit"] = $limit;
404          }      }
405            
406          /**      /**
407           * This function gets the current       * This function gets the current
408           * value of the limit value       * value of the limit value
409           *       *
410           * @return int       * @return int
411           */       */
412          function get_limit() {      function get_limit() {
413                  return $this->_query_params["limit"];          return $this->_query_params["limit"];
414          }      }
415    
416    
417          /**      /**
418           * This function sets the search type       * This function sets the search type
419           * (simple or advanced)       * (simple or advanced)
420           *       *
421           * @param string       * @param string
422           */       */
423          function set_search_type( $search_type ) {      function set_search_type( $search_type ) {
424                  $this->_query_params["searchtype"] = $search_type;          $this->_query_params["searchtype"] = $search_type;
425          }      }
426    
427          /**      /**
428           * this function returns the current search type       * this function returns the current search type
429           * for the DataList query       * for the DataList query
430           *       *
431           * @return string       * @return string
432           */       */
433          function get_search_type() {      function get_search_type() {
434                  return $this->_query_params["searchtype"];          return $this->_query_params["searchtype"];
435          }      }
436    
437          /**      /**
438           * This function returns the       * This method is used to set a secondary
439           * data_index value and increments it       * list of columns to order/sort by.
440           *       *
441           * @return int       * @param mixed - n numbers of column names
442           */       *                to order by
443          function get_data_index() {       */
444                  $this->_data_index++;      function set_secondary_orderby() {
445                  return $this->_data_index-1;          $this->_query_params["secondaryorderby"] = func_get_args();
446          }      }
447    
448          /**      /**
449           * This function determines if the column       * This gets the list of secondary order by
450           * associated w/ a data_name is sortable       * columns.
451           * or not       *
452           *       * @return array
453           * @param string - the data_name filed in the       */
454           *                 _columns array to look for      function get_secondary_orderby() {
455           * @return boolean - is that column sortable?          return $this->_query_params["secondaryorderby"];
456           */      }
457          function _is_column_sortable( $data_name ) {  
458                  foreach( $this->_columns as $name => $data ) {      /**
459                          if ($data["data_name"] == $data_name &&       * This function returns the
460                                  $data["sortable"] == TRUE) {       * data_index value and increments it
461                                  return TRUE;       *
462                          }       * @return int
463                  }       */
464                  return FALSE;      function get_data_index() {
465          }          $this->_data_index++;
466            return $this->_data_index-1;
467        }
468    
469        /**
470         * This function determines if the column
471         * associated w/ a data_name is sortable
472         * or not
473         *
474         * @param string - the data_name filed in the
475         *                 _columns array to look for
476         * @return boolean - is that column sortable?
477         */
478        function _is_column_sortable( $data_name ) {
479            foreach( $this->_columns as $name => $data ) {
480                if ($data["data_name"] == $data_name &&
481                    ($data["sortable"] == SORTABLE ||
482                     $data["sortable"] == SORTABLE_ICASE ||
483                     $data["sortable"] == SORTABLE_NUMERIC)) {
484                    return TRUE;
485                }
486            }
487            return FALSE;
488        }
489  }  }
490  ?>  ?>

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed