Pman/Roo.php
[Pman.Base] / Pman / Roo.php
1 <?php
2
3
4 require_once 'Pman.php';
5 /**
6  * 
7  * 
8  * not really sure how save our factory method is....!!!
9  * 
10  * 
11  * Uses these methods of the dataobjects:
12  * - checkPerm('L'/'E'/'A', $authuser) - can we list the stuff
13  * 
14  * - applySort($au, $sortcol, $direction)
15  * - applyFilters($_REQUEST, $authUser) -- apply any query filters on data. and hide stuff not to be seen.
16  * - postListExtra - add extra column data on the results (like new messages etc.)
17  * -postListFilter($data, $authUser, $request) return $data - add extra data to an object
18  * 
19  * - toRooSingleArray() // single fetch, add data..
20  * - toRooArray($request) /// toArray if you need to return different data.. for a list fetch.
21  * 
22  * 
23  * - beforeDelete() -- return false for fail and set DO->err;
24  * - onUpdate($old, $request,$roo) - after update
25  * - onInsert($request,$roo) - after insert
26  * - onUpload($roo)
27  * - setFromRoo($ar) - values from post (deal with dates etc.) - return true|error string.
28  * 
29  * - toEventString (for logging)
30  */
31
32 class Pman_Roo extends Pman
33 {
34     
35    function getAuth() {
36         parent::getAuth(); // load company!
37         $au = $this->getAuthUser();
38         if (!$au) {
39             $this->jerr("Not authenticated", array('authFailure' => true));
40         }
41         $this->authUser = $au;
42         return true;
43     }
44     /**
45      * GET method   Roo/TABLENAME.php 
46      * -- defaults to listing data. with args.
47      * 
48      * !colname=....                 => colname != ....
49      * colname[0]=... colname[1]=... => colname IN (.....)
50      * 
51      * other opts:
52      * _post      = simulate a post with debuggin on.
53      * lookup     =  array( k=>v) single fetch based on a key/value pair
54      * _id        =  single fetch based on id.
55      * _delete    = delete a list of ids element. (seperated by ,);
56      * _columns   = comma seperated list of columns.
57      * _distinct   = a distinct column lookup.
58      * _requestMeta = default behaviour of Roo stores.. on first query..
59      * 
60      * csvCols    = return data as csv
61      * 
62      * csvTitles  = return data as csv
63      *
64      * sort        = sort column
65      * dir         = sort direction
66      * start       = limit start
67      * limit       = limit number 
68      * 
69      * _toggleActive !:!:!:! - this hsould not really be here..
70      * query[add_blank] - add a line in with an empty option...  - not really needed???
71      * 
72      */
73     function get($tab)
74     {
75          //  $this->jerr("Not authenticated", array('authFailure' => true));
76          //DB_DataObject::debuglevel(1);
77         
78         // debugging...
79         if (!empty($_GET['_post'])) {
80             $_POST  = $_GET;
81             DB_DAtaObject::debuglevel(1);
82             return $this->post($tab);
83         }
84         $tab = str_replace('/', '',$tab); // basic protection??
85         $x = DB_DataObject::factory($tab);
86         if (!is_a($x, 'DB_DataObject')) {
87             $this->jerr('invalid url');
88         }
89         $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
90         
91         if (isset( $_REQUEST['lookup'] )) { // single fetch based on key/value pairs
92             if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser))  {
93                 $this->jerr("PERMISSION DENIED");
94             }
95             $this->loadMap($x, $_columns);
96             $x->setFrom($_REQUEST['lookup'] );
97             $x->limit(1);
98             if (!$x->find(true)) {
99                 $this->jok(false);
100             }
101             $this->jok($x->toArray());
102         }
103         
104         
105         
106         if (isset($_REQUEST['_id'])) { // single fetch
107             
108             if (empty($_REQUEST['_id'])) {
109                 $this->jok($x->toArray());  // return an empty array!
110             }
111            
112             $this->loadMap($x, $_columns);
113             
114             if (!$x->get($_REQUEST['_id'])) {
115                 $this->jerr("no such record");
116             }
117             
118             if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser))  {
119                 $this->jerr("PERMISSION DENIED");
120             }
121             
122             $this->jok(method_exists($x, 'toRooSingleArray') ? $x->toRooSingleArray($this->authUser) : $x->toArray());
123             
124         }
125         if (isset($_REQUEST['_delete'])) {
126             // do we really delete stuff!?!?!?
127             return $this->delete($x,$_REQUEST);
128         } 
129         
130         
131         
132         
133         if (isset($_REQUEST['_toggleActive'])) {
134             // do we really delete stuff!?!?!?
135             if (!$this->hasPerm("Core.Staff", 'E'))  {
136                 $this->jerr("PERMISSION DENIED");
137             }
138             $clean = create_function('$v', 'return (int)$v;');
139             $bits = array_map($clean, explode(',', $_REQUEST['_toggleActive']));
140             if (in_array($this->authUser->id, $bits) && $this->authUser->active) {
141                 $this->jerr("you can not disable yourself");
142             }
143             $x->query('UPDATE Person SET active = !active WHERE id IN (' .implode(',', $bits).')');
144             $this->addEvent("USERTOGGLE", false, implode(',', $bits));
145             $this->jok("Updated");
146             
147         }
148       // DB_DataObject::debugLevel(1);
149         if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser))  {
150             $this->jerr("PERMISSION DENIED");
151         }
152         $map = $this->loadMap($x, $_columns);
153         
154         $this->setFilters($x,$_REQUEST);
155         
156          
157         
158         // build join if req.
159         $countWhat = false;
160         if (!empty($_REQUEST['_distinct'])) {
161             $cols = $x->table();
162            // print_r($cols);
163           
164             if (isset($cols[$_REQUEST['_distinct']])) {
165                 $countWhat = 'distinct ' . $_REQUEST['_distinct'];
166                 $x->selectAdd();
167                 $x->selectAdd('distinct('.$_REQUEST['_distinct'].')');
168                 $_columns = array( $_REQUEST['_distinct'] );
169             } else {
170                 $this->jerr('invalid distinct');
171             }
172              
173         }
174         $total = $x->count($countWhat);
175         // sorting..
176       //   DB_DataObject::debugLevel(1);
177         
178         $this->applySort($x);
179         
180         
181  
182         $x->limit(
183             empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
184             min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 1000)
185         );
186         
187         $queryObj = clone($x);
188         
189         $x->find();
190         $ret = array();
191         
192         if (!empty($_REQUEST['query']['add_blank'])) {
193             $ret[] = array( 'id' => 0, 'name' => '----');
194             $total+=1;
195         }
196         // MOVE ME...
197         
198         //if (($tab == 'Groups') && ($_REQUEST['type'] != 0))  { // then it's a list of teams..
199         if ($tab == 'Groups') {
200             
201             $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
202             $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
203             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
204             $total+=2;
205         }
206         
207         // DOWNLOAD...
208         
209         if (!empty($_REQUEST['csvCols']) && !empty($_REQUEST['csvTitles']) ) {
210             header('Content-type: text/csv');
211             
212             header('Content-Disposition: attachment; filename="documentlist-export-'.date('Y-m-d') . '.csv"');
213             //header('Content-type: text/plain');
214             $fh = fopen('php://output', 'w');
215             fputcsv($fh, $_REQUEST['csvTitles']);
216             while ($x->fetch()) {
217                 //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
218                 $line = array();
219                 foreach($_REQUEST['csvCols'] as $k) {
220                     $line[] = isset($x->$k) ? $x->$k : '';
221                 }
222                 fputcsv($fh, $line);
223             }
224             fclose($fh);
225             exit;
226             
227         
228         }
229         //die("DONE?");
230         
231         $rooar = method_exists($x, 'toRooArray');
232         
233         while ($x->fetch()) {
234             $add = $rooar  ? $x->toRooArray($_REQUEST) : $x->toArray();
235             
236             $ret[] =  !$_columns ? $add : array_intersect_key($add, array_flip($_columns));
237         }
238         //if ($x->tableName() == 'Documents_Tracking') {
239         //    $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
240        // }
241         
242         $extra = false;
243         if (method_exists($queryObj ,'postListExtra')) {
244             $extra = $queryObj->postListExtra($_REQUEST);
245         }
246         // filter results, and add any data that is needed...
247         if (method_exists($x,'postListFilter')) {
248             $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
249         }
250         
251         if (!empty($_REQUEST['_requestMeta']) &&  count($ret)) {
252             $meta = $this->meta($x, $ret);
253             if ($meta) {
254                 $extra['metaData'] = $meta;
255             }
256         }
257         
258        // echo "<PRE>"; print_r($ret);
259         $this->jdata($ret,$total, $extra );
260
261     
262     }
263     /**
264      * applySort
265      * 
266      * apply REQUEST[sort] and [dir]
267      * sort may be an array of columsn..
268      * 
269      * @arg   DB_DataObject $x
270      * 
271      */
272     function applySort($x)
273     {
274         
275     
276         $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
277         $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
278         
279         
280         
281         $sorted = false;
282         if (method_exists($x, 'applySort')) {
283             $sorted = $x->applySort($this->authUser, $sort, $dir, array_keys($this->cols));
284         }
285         if ($sorted === false) {
286             
287             $cols = $x->table();
288             $sort = explode(',', $sort);
289            // echo '<PRE>';print_r(array($sort, $this->cols));
290             // other sorts??? 
291            // $otherSorts = array('person_id_name');
292             
293             if (strlen($sort) && isset($cols[$sort]) ) {
294                 $sort = $x->tableName() .'.'.$sort . ' ' . $dir ;
295                 $x->orderBy($sort );
296             } else if (in_array($sort, array_keys($this->cols))) {
297                 $sort = $sort . ' ' . $dir ;
298                 $x->orderBy($sort );
299             }// else other formatas?
300             //if ( in_array($sort, $otherSorts)) {
301             //    $x->orderBy($sort . ' ' . $dir);
302             ////}
303         }
304     }
305      /**
306      * POST method   Roo/TABLENAME.php 
307      * -- updates the data..
308      * 
309      * other opts:
310      * _debug - forces debugging on.
311      * _get - forces a get request
312      * _ids - multiple update of records.
313      * {colid} - forces fetching
314      * 
315      */
316     function post($tab) // update / insert (?? dleete??)
317     {
318        // DB_DataObject::debugLevel(1);
319         if (!empty($_REQUEST['_debug'])) {
320             DB_DataObject::debugLevel(1);
321         }
322         
323         if (!empty($_REQUEST['_get'])) {
324             return $this->get($tab);
325         }
326         $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
327         
328         $tab = str_replace('/', '',$tab); // basic protection??
329         $x = DB_DataObject::factory($tab);
330         if (!is_a($x, 'DB_DataObject')) {
331             $this->jerr('invalid url');
332         }
333         // find the key and use that to get the thing..
334         $keys = $x->keys();
335         if (empty($keys) ) {
336             $this->jerr('no key');
337         }
338         $old = false;
339         
340         if (!empty($_REQUEST['_ids'])) {
341             $ids = explode(',',$_REQUEST['_ids']);
342             $x->whereAddIn($keys[0], $ids, 'int');
343             $ar = $x->fetchAll();
344             foreach($ar as $x) {
345                 $this->update($x, $_REQUEST);
346                 
347             }
348             // all done..
349             $this->jok("UPDATED");
350             
351             
352         }
353         
354         
355         if (!empty($_REQUEST[$keys[0]])) {
356             // it's a create..
357             if (!$x->get($keys[0], $_REQUEST[$keys[0]]))  {
358                 $this->jerr("Invalid request");
359             }
360             $this->jok($this->update($x, $_REQUEST));
361         } else {
362             $this->jok($this->insert($x, $_REQUEST));
363             
364         }
365         
366         
367         
368     }
369     function insert($x, $req)
370     {
371         
372     
373         if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req))  {
374             $this->jerr("PERMISSION DENIED");
375         }
376         
377         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
378    
379         if (method_exists($x, 'setFromRoo')) {
380             $res = $x->setFromRoo($req, $this);
381             if (is_string($res)) {
382                 $this->jerr($res);
383             }
384         } else {
385             $x->setFrom($req);
386         }
387         
388          
389         $cols = $x->table();
390      
391         if (isset($cols['created'])) {
392             $x->created = date('Y-m-d H:i:s');
393         }
394         if (isset($cols['created_dt'])) {
395             $x->created_dt = date('Y-m-d H:i:s');
396         }
397         if (isset($cols['created_by'])) {
398             $x->created_by = $this->authUser->id;
399         }
400         
401      
402          if (isset($cols['modified'])) {
403             $x->modified = date('Y-m-d H:i:s');
404         }
405         if (isset($cols['modified_dt'])) {
406             $x->modified_dt = date('Y-m-d H:i:s');
407         }
408         if (isset($cols['modified_by'])) {
409             $x->modified_by = $this->authUser->id;
410         }
411         
412         if (isset($cols['updated'])) {
413             $x->updated = date('Y-m-d H:i:s');
414         }
415         if (isset($cols['updated_dt'])) {
416             $x->updated_dt = date('Y-m-d H:i:s');
417         }
418         if (isset($cols['updated_by'])) {
419             $x->updated_by = $this->authUser->id;
420         }
421         
422      
423         
424         
425         
426         $x->insert();
427         if (method_exists($x, 'onInsert')) {
428             $x->onInsert($_REQUEST, $this);
429         }
430         $this->addEvent("ADD", $x, $x->toEventString());
431         
432         // note setFrom might handle this before hand...!??!
433         if (!empty($_FILES) && method_exists($x, 'onUpload')) {
434             $x->onUpload($this);
435         }
436         
437         $r = DB_DataObject::factory($x->tableName());
438         $r->id = $x->id;
439         $this->loadMap($r, $_columns);
440         $r->limit(1);
441         $r->find(true);
442         
443         $rooar = method_exists($r, 'toRooArray');
444         //print_r(var_dump($rooar)); exit;
445         return $rooar  ? $r->toRooArray() : $r->toArray();
446     }
447     
448     
449     function update($x, $req)
450     {
451         if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser, $_REQUEST))  {
452             $this->jerr("PERMISSION DENIED");
453         }
454        
455         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
456
457        
458         $old = clone($x);
459         $this->old = $x;
460         // this lot is generic.. needs moving 
461         if (method_exists($x, 'setFromRoo')) {
462             $res = $x->setFromRoo($req, $this);
463             if (is_string($res)) {
464                 $this->jerr($res);
465             }
466         } else {
467             $x->setFrom($req);
468         }
469         $this->addEvent("EDIT", $x, $x->toEventString());
470         //print_r($x);
471         //print_r($old);
472         
473         $cols = $x->table();
474         
475         if (isset($cols['modified'])) {
476             $x->modified = date('Y-m-d H:i:s');
477         }
478         if (isset($cols['modified_dt'])) {
479             $x->modified_dt = date('Y-m-d H:i:s');
480         }
481         if (isset($cols['modified_by'])) {
482             $x->modified_by = $this->authUser->id;
483         }
484         
485         if (isset($cols['updated'])) {
486             $x->updated = date('Y-m-d H:i:s');
487         }
488         if (isset($cols['updated_dt'])) {
489             $x->updated_dt = date('Y-m-d H:i:s');
490         }
491         if (isset($cols['updated_by'])) {
492             $x->updated_by = $this->authUser->id;
493         }
494         
495         
496         $x->update($old);
497         if (method_exists($x, 'onUpdate')) {
498             $x->onUpdate($old, $req, $this);
499         }
500         
501         $r = DB_DataObject::factory($x->tableName());
502         $r->id = $x->id;
503         $this->loadMap($r, $_columns);
504         $r->limit(1);
505         $r->find(true);
506         $rooar = method_exists($r, 'toRooArray');
507         //print_r(var_dump($rooar)); exit;
508         return $rooar  ? $r->toRooArray() : $r->toArray();
509     }
510     
511     function delete($x, $req)
512     {
513         // do we really delete stuff!?!?!?
514        
515        
516         // build a list of tables to queriy for dependant data..
517         $map = $x->links();
518         
519         $affects  = array();
520         
521         $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database];
522         foreach($all_links as $tbl => $links) {
523             foreach($links as $col => $totbl_col) {
524                 $to = explode(':', $totbl_col);
525                 if ($to[0] != $x->tableName()) {
526                     continue;
527                 }
528                 
529                 $affects[$tbl .'.' . $col] = true;
530             }
531         }
532         // collect tables
533         
534        // echo '<PRE>';print_r($affects);exit;
535         //DB_Dataobject::debugLevel(1);
536        
537         
538         $clean = create_function('$v', 'return (int)$v;');
539         
540         $bits = array_map($clean, explode(',', $req['_delete']));
541         
542        // print_r($bits);exit;
543         
544         $x->whereAdd('id IN ('. implode(',', $bits) .')');
545         $x->find();
546         $errs = array();
547         while ($x->fetch()) {
548             $xx = clone($x);
549             
550             
551             foreach($affects as $k=> $true) {
552                 $ka = explode('.', $k);
553                 $chk = DB_DataObject::factory($ka[0]);
554                 $chk->{$ka[1]} =  $xx->id;
555                 if ($chk->count()) {
556                     $this->jerr('Delete Dependant records first');
557                 }
558             }
559             
560             
561             
562             if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser))  {
563                 $this->jerr("PERMISSION DENIED");
564             }
565             
566             $this->addEvent("DELETE", $x, $x->toEventString());
567             if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
568                 $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
569                 continue;
570             }
571             $xx->delete();
572         }
573         if ($errs) {
574             $this->jerr(implode("\n<BR>", $errs));
575         }
576         $this->jok("Deleted");
577         
578     }
579    
580     
581     
582     
583     var $cols = array();
584     function loadMap($do, $filter=false) 
585     {
586         //DB_DataObject::debugLevel(1);
587         $conf = array();
588         
589         
590         $this->init();
591         $mods = explode(',',$this->appModules);
592         
593         $ff = HTML_FlexyFramework::geT();
594         //$db->databaseName();
595         
596         //$ff->DB_DataObject['ini_'. $db->database()];
597         //echo '<PRE>';print_r($do->links());exit;
598         //var_dump($mods);
599         
600         if (in_array('Builder', $mods) ) {
601             
602             foreach(in_array('Builder', $mods) ? scandir($this->rootDir.'/Pman') : $mods as $m) {
603                 
604                 if (!strlen($m) || $m[0] == '.' || !is_dir($this->rootDir."/Pman/$m")) {
605                     continue;
606                 }
607                 $ini = $this->rootDir."/Pman/$m/DataObjects/pman.links.ini";
608                 if (!file_exists($ini)) {
609                     continue;
610                 }
611                 $conf = array_merge($conf, parse_ini_file($ini,true));
612                 if (!isset($conf[$do->tableName()])) {
613                     return;
614                 }
615                 $map = $conf[$do->tableName()];
616             } 
617         } else {
618             $map = $do->links();
619         }
620          
621         
622         
623         
624         // current table..
625         $tabdef = $do->table();
626         if (isset($tabdef['passwd'])) {
627             // prevent exposure of passwords..
628             unset($tabdef['passwd']);     
629         }
630         $xx = clone($do);
631         $xx = array_keys($tabdef);
632         $do->selectAdd(); // we need thsi as normally it's only cleared by an empty selectAs call.
633         
634         if ($filter) {
635             $cols = array();
636          
637             foreach($xx as $c) {
638                 if (in_array($c, $filter)) {
639                     $cols[] = $c;
640                 }
641             }
642             $do->selectAs($cols);
643         } else {
644             $do->selectAs($xx);
645         }
646         
647         
648         $this->cols = array();
649         foreach($xx as $k) {
650             $this->cols[$k] = $do->tableName(). '.' . $k;
651         }
652         
653         
654         
655         
656          
657         
658         foreach($map as $ocl=>$info) {
659             
660             list($tab,$col) = explode(':', $info);
661             // what about multiple joins on the same table!!!
662             $xx = DB_DataObject::factory($tab);
663             if (!is_a($xx, 'DB_DataObject')) {
664                 continue;
665             }
666             // this is borked ... for multiple jions..
667             $do->joinAdd($xx, 'LEFT', 'join_'.$ocl.'_'. $col, $ocl);
668             $tabdef = $xx->table();
669             $table = $xx->tableName();
670             if (isset($tabdef['passwd'])) {
671              
672                 unset($tabdef['passwd']);
673               
674             } 
675             $xx = array_keys($tabdef);
676             
677             
678             if ($filter) {
679                 $cols = array();
680                 foreach($xx as $c) {
681                     
682                     $tn = sprintf($ocl.'_%s', $c);
683                     if (in_array($tn, $filter)) {
684                         $cols[] = $c;
685                     }
686                 }
687                 $do->selectAs($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
688             } else {
689                 $do->selectAs($xx,  $ocl.'_%s', 'join_'.$ocl.'_'. $col);
690             }
691              
692             
693             foreach($xx as $k) {
694                 $this->cols[sprintf($ocl.'_%s', $k)] = $tab.'.'.$k;
695             }
696             
697             
698         }
699         //DB_DataObject::debugLevel(1);
700         
701         
702         
703     }
704     /**
705      * generate the meta data neede by queries.
706      * 
707      */
708     function meta($x, $data)
709     {
710         // this is not going to work on queries where the data does not match the database def..
711         // for unknown columns we send them as stirngs..
712         $lost = 0;
713         $cols  = array_keys($data[0]);
714      
715         
716         
717         
718         //echo '<PRE>';print_r($this->cols); exit;
719         $options = &PEAR::getStaticProperty('DB_DataObject','options');
720         $reader = $options["ini_{$x->_database}"] .'.reader';
721         if (!file_exists( $reader )) {
722             return;
723         }
724         
725         $rdata = unserialize(file_get_contents($reader));
726         
727        // echo '<PRE>';print_r($rdata);exit;
728         
729         $meta = array();
730         foreach($cols as $c ) {
731             if (!isset($this->cols[$c]) || !isset($rdata[$this->cols[$c]]) || !is_array($rdata[$this->cols[$c]])) {
732                 $meta[] = $c;
733                 continue;    
734             }
735             $add = $rdata[$this->cols[$c]];
736             $add['name'] = $c;
737             $meta[] = $add;
738         }
739         return array(
740             'totalProperty' =>  'total',
741             'successProperty' => 'success',
742             'root' => 'data',
743             'id' => 'id',
744             'fields' => $meta
745         );
746          
747         
748     }
749     
750     function setFilters($x, $q)
751     {
752         // if a column is type int, and we get ',' -> the it should be come an inc clause..
753        // DB_DataObject::debugLevel(1);
754         if (method_exists($x, 'applyFilters')) {
755            // DB_DataObject::debugLevel(1);
756             $x->applyFilters($q, $this->authUser);
757         }
758         $q_filtered = array();
759         
760         foreach($q as $key=>$val) {
761             
762             if (is_array($val) ) {
763                 
764                 if (!in_array( $key,  array_keys($this->cols))) {
765                     continue;
766                 }
767                 
768                 // support a[0] a[1] ..... => whereAddIn(
769                 $ar = array();
770                 $quote = false;
771                 foreach($val as $k=>$v) {
772                     if (!is_numeric($k)) {
773                         $ar = array();
774                         break;
775                     }
776                     if (!is_numeric($v) || !is_long($v)) {
777                         $quote = true;
778                     }
779                     $ar[] = $v;
780                     
781                 }
782                 if (count($ar)) {
783                     $x->whereAddIn($key,$ar, $quote ? 'string' : 'int');
784                 }
785                 
786                 continue;
787             }
788             
789             
790             
791             if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
792                     
793                 $x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
794                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
795                 );
796                 continue;
797                 
798             }
799             
800             
801             
802             switch($key) {
803                     
804                 // Events and remarks
805                 case 'on_id':  // where TF is this used...
806                     if (!empty($q['query']['original'])) {
807                       //  DB_DataObject::debugLevel(1);
808                         $o = (int) $q['query']['original'];
809                         $oid = (int) $val;
810                         $x->whereAdd("(on_id = $oid  OR 
811                                 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o ) 
812                             )");
813                         continue;
814                                 
815                     }
816                     $x->on_id = $val;
817                 
818                 
819                 default:
820                     if (strlen($val)) {
821                         $q_filtered[$key] = $val;
822                     }
823                     
824                     continue;
825             }
826         }
827         $x->setFrom($q_filtered);
828        
829         // nice generic -- let's get rid of it.. where is it used!!!!
830         // used by: 
831         // Person / Group
832         if (!empty($q['query']['name'])) {
833             $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
834         }
835         
836         // - projectdirectory staff list - persn queuy
837       
838          
839        
840          
841           
842         
843         
844     }
845     
846 }