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         $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
179         $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
180         
181         $sorted = false;
182         if (method_exists($x, 'applySort')) {
183             $sorted = $x->applySort($this->authUser, $sort, $dir, array_keys($this->cols));
184         }
185         if ($sorted === false) {
186             
187             $cols = $x->table();
188            // echo '<PRE>';print_r(array($sort, $this->cols));
189             // other sorts??? 
190            // $otherSorts = array('person_id_name');
191             
192             if (strlen($sort) && isset($cols[$sort]) ) {
193                 $sort = $x->tableName() .'.'.$sort . ' ' . $dir ;
194                 $x->orderBy($sort );
195             } else if (in_array($sort, array_keys($this->cols))) {
196                 $sort = $sort . ' ' . $dir ;
197                 $x->orderBy($sort );
198             }// else other formatas?
199             //if ( in_array($sort, $otherSorts)) {
200             //    $x->orderBy($sort . ' ' . $dir);
201             ////}
202         }
203         
204         
205  
206         $x->limit(
207             empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
208             min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 1000)
209         );
210         
211         $queryObj = clone($x);
212         
213         $x->find();
214         $ret = array();
215         
216         if (!empty($_REQUEST['query']['add_blank'])) {
217             $ret[] = array( 'id' => 0, 'name' => '----');
218             $total+=1;
219         }
220         // MOVE ME...
221         
222         //if (($tab == 'Groups') && ($_REQUEST['type'] != 0))  { // then it's a list of teams..
223         if ($tab == 'Groups') {
224             
225             $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
226             $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
227             //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
228             $total+=2;
229         }
230         
231         // DOWNLOAD...
232         
233         if (!empty($_REQUEST['csvCols']) && !empty($_REQUEST['csvTitles']) ) {
234             header('Content-type: text/csv');
235             
236             header('Content-Disposition: attachment; filename="documentlist-export-'.date('Y-m-d') . '.csv"');
237             //header('Content-type: text/plain');
238             $fh = fopen('php://output', 'w');
239             fputcsv($fh, $_REQUEST['csvTitles']);
240             while ($x->fetch()) {
241                 //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
242                 $line = array();
243                 foreach($_REQUEST['csvCols'] as $k) {
244                     $line[] = isset($x->$k) ? $x->$k : '';
245                 }
246                 fputcsv($fh, $line);
247             }
248             fclose($fh);
249             exit;
250             
251         
252         }
253         //die("DONE?");
254         
255         $rooar = method_exists($x, 'toRooArray');
256         
257         while ($x->fetch()) {
258             $add = $rooar  ? $x->toRooArray($_REQUEST) : $x->toArray();
259             
260             $ret[] =  !$_columns ? $add : array_intersect_key($add, array_flip($_columns));
261         }
262         //if ($x->tableName() == 'Documents_Tracking') {
263         //    $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
264        // }
265         
266         $extra = false;
267         if (method_exists($queryObj ,'postListExtra')) {
268             $extra = $queryObj->postListExtra($_REQUEST);
269         }
270         // filter results, and add any data that is needed...
271         if (method_exists($x,'postListFilter')) {
272             $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
273         }
274         
275         if (!empty($_REQUEST['_requestMeta']) &&  count($ret)) {
276             $meta = $this->meta($x, $ret);
277             if ($meta) {
278                 $extra['metaData'] = $meta;
279             }
280         }
281         
282        // echo "<PRE>"; print_r($ret);
283         $this->jdata($ret,$total, $extra );
284
285     
286     }
287      /**
288      * POST method   Roo/TABLENAME.php 
289      * -- updates the data..
290      * 
291      * other opts:
292      * _debug - forces debugging on.
293      * _get - forces a get request
294      * _ids - multiple update of records.
295      * {colid} - forces fetching
296      * 
297      */
298     function post($tab) // update / insert (?? dleete??)
299     {
300        // DB_DataObject::debugLevel(1);
301         if (!empty($_REQUEST['_debug'])) {
302             DB_DataObject::debugLevel(1);
303         }
304         
305         if (!empty($_REQUEST['_get'])) {
306             return $this->get($tab);
307         }
308         $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
309         
310         $tab = str_replace('/', '',$tab); // basic protection??
311         $x = DB_DataObject::factory($tab);
312         if (!is_a($x, 'DB_DataObject')) {
313             $this->jerr('invalid url');
314         }
315         // find the key and use that to get the thing..
316         $keys = $x->keys();
317         if (empty($keys) ) {
318             $this->jerr('no key');
319         }
320         $old = false;
321         
322         if (!empty($_REQUEST['_ids'])) {
323             $ids = explode(',',$_REQUEST['_ids']);
324             $x->whereAddIn($keys[0], $ids, 'int');
325             $ar = $x->fetchAll();
326             foreach($ar as $x) {
327                 $this->update($x, $_REQUEST);
328                 
329             }
330             // all done..
331             $this->jok("UPDATED");
332             
333             
334         }
335         
336         
337         if (!empty($_REQUEST[$keys[0]])) {
338             // it's a create..
339             if (!$x->get($keys[0], $_REQUEST[$keys[0]]))  {
340                 $this->jerr("Invalid request");
341             }
342             $this->jok($this->update($x, $_REQUEST));
343         } else {
344             $this->jok($this->insert($x, $_REQUEST));
345             
346         }
347         
348         
349         
350     }
351     function insert($x, $req)
352     {
353         
354     
355         if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req))  {
356             $this->jerr("PERMISSION DENIED");
357         }
358         
359         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
360    
361         if (method_exists($x, 'setFromRoo')) {
362             $res = $x->setFromRoo($req, $this);
363             if (is_string($res)) {
364                 $this->jerr($res);
365             }
366         } else {
367             $x->setFrom($req);
368         }
369         
370          
371         $cols = $x->table();
372      
373         if (isset($cols['created'])) {
374             $x->created = date('Y-m-d H:i:s');
375         }
376         if (isset($cols['created_dt'])) {
377             $x->created_dt = date('Y-m-d H:i:s');
378         }
379         if (isset($cols['created_by'])) {
380             $x->created_by = $this->authUser->id;
381         }
382         
383      
384          if (isset($cols['modified'])) {
385             $x->modified = date('Y-m-d H:i:s');
386         }
387         if (isset($cols['modified_dt'])) {
388             $x->modified_dt = date('Y-m-d H:i:s');
389         }
390         if (isset($cols['modified_by'])) {
391             $x->modified_by = $this->authUser->id;
392         }
393         
394         if (isset($cols['updated'])) {
395             $x->updated = date('Y-m-d H:i:s');
396         }
397         if (isset($cols['updated_dt'])) {
398             $x->updated_dt = date('Y-m-d H:i:s');
399         }
400         if (isset($cols['updated_by'])) {
401             $x->updated_by = $this->authUser->id;
402         }
403         
404      
405         
406         
407         
408         $x->insert();
409         if (method_exists($x, 'onInsert')) {
410             $x->onInsert($_REQUEST, $this);
411         }
412         $this->addEvent("ADD", $x, $x->toEventString());
413         
414         // note setFrom might handle this before hand...!??!
415         if (!empty($_FILES) && method_exists($x, 'onUpload')) {
416             $x->onUpload($this);
417         }
418         
419         $r = DB_DataObject::factory($x->tableName());
420         $r->id = $x->id;
421         $this->loadMap($r, $_columns);
422         $r->limit(1);
423         $r->find(true);
424         
425         $rooar = method_exists($r, 'toRooArray');
426         //print_r(var_dump($rooar)); exit;
427         return $rooar  ? $r->toRooArray() : $r->toArray();
428     }
429     
430     
431     function update($x, $req)
432     {
433         if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser, $_REQUEST))  {
434             $this->jerr("PERMISSION DENIED");
435         }
436        
437         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
438
439        
440         $old = clone($x);
441         $this->old = $x;
442         // this lot is generic.. needs moving 
443         if (method_exists($x, 'setFromRoo')) {
444             $res = $x->setFromRoo($req, $this);
445             if (is_string($res)) {
446                 $this->jerr($res);
447             }
448         } else {
449             $x->setFrom($req);
450         }
451         $this->addEvent("EDIT", $x, $x->toEventString());
452         //print_r($x);
453         //print_r($old);
454         
455         $cols = $x->table();
456         
457         if (isset($cols['modified'])) {
458             $x->modified = date('Y-m-d H:i:s');
459         }
460         if (isset($cols['modified_dt'])) {
461             $x->modified_dt = date('Y-m-d H:i:s');
462         }
463         if (isset($cols['modified_by'])) {
464             $x->modified_by = $this->authUser->id;
465         }
466         
467         if (isset($cols['updated'])) {
468             $x->updated = date('Y-m-d H:i:s');
469         }
470         if (isset($cols['updated_dt'])) {
471             $x->updated_dt = date('Y-m-d H:i:s');
472         }
473         if (isset($cols['updated_by'])) {
474             $x->updated_by = $this->authUser->id;
475         }
476         
477         
478         $x->update($old);
479         if (method_exists($x, 'onUpdate')) {
480             $x->onUpdate($old, $req, $this);
481         }
482         
483         $r = DB_DataObject::factory($x->tableName());
484         $r->id = $x->id;
485         $this->loadMap($r, $_columns);
486         $r->limit(1);
487         $r->find(true);
488         $rooar = method_exists($r, 'toRooArray');
489         //print_r(var_dump($rooar)); exit;
490         return $rooar  ? $r->toRooArray() : $r->toArray();
491     }
492     
493     function delete($x, $req)
494     {
495         // do we really delete stuff!?!?!?
496        
497        
498         // build a list of tables to queriy for dependant data..
499         $map = $x->links();
500         
501         $affects  = array();
502         
503         $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database];
504         foreach($all_links as $tbl => $links) {
505             foreach($links as $col => $totbl_col) {
506                 $to = explode(':', $totbl_col);
507                 if ($to[0] != $x->tableName()) {
508                     continue;
509                 }
510                 
511                 $affects[$tbl .'.' . $col] = true;
512             }
513         }
514         // collect tables
515         
516        // echo '<PRE>';print_r($affects);exit;
517         //DB_Dataobject::debugLevel(1);
518        
519         
520         $clean = create_function('$v', 'return (int)$v;');
521         
522         $bits = array_map($clean, explode(',', $req['_delete']));
523         
524        // print_r($bits);exit;
525         
526         $x->whereAdd('id IN ('. implode(',', $bits) .')');
527         $x->find();
528         $errs = array();
529         while ($x->fetch()) {
530             $xx = clone($x);
531             
532             
533             foreach($affects as $k=> $true) {
534                 $ka = explode('.', $k);
535                 $chk = DB_DataObject::factory($ka[0]);
536                 $chk->{$ka[1]} =  $xx->id;
537                 if ($chk->count()) {
538                     $this->jerr('Delete Dependant records first');
539                 }
540             }
541             
542             
543             
544             if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser))  {
545                 $this->jerr("PERMISSION DENIED");
546             }
547             
548             $this->addEvent("DELETE", $x, $x->toEventString());
549             if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
550                 $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
551                 continue;
552             }
553             $xx->delete();
554         }
555         if ($errs) {
556             $this->jerr(implode("\n<BR>", $errs));
557         }
558         $this->jok("Deleted");
559         
560     }
561    
562     
563     
564     
565     var $cols = array();
566     function loadMap($do, $filter=false) 
567     {
568         //DB_DataObject::debugLevel(1);
569         $conf = array();
570         
571         
572         $this->init();
573         $mods = explode(',',$this->appModules);
574         
575         $ff = HTML_FlexyFramework::geT();
576         //$db->databaseName();
577         
578         //$ff->DB_DataObject['ini_'. $db->database()];
579         //echo '<PRE>';print_r($do->links());exit;
580         //var_dump($mods);
581         
582         if (in_array('Builder', $mods) ) {
583             
584             foreach(in_array('Builder', $mods) ? scandir($this->rootDir.'/Pman') : $mods as $m) {
585                 
586                 if (!strlen($m) || $m[0] == '.' || !is_dir($this->rootDir."/Pman/$m")) {
587                     continue;
588                 }
589                 $ini = $this->rootDir."/Pman/$m/DataObjects/pman.links.ini";
590                 if (!file_exists($ini)) {
591                     continue;
592                 }
593                 $conf = array_merge($conf, parse_ini_file($ini,true));
594                 if (!isset($conf[$do->tableName()])) {
595                     return;
596                 }
597                 $map = $conf[$do->tableName()];
598             } 
599         } else {
600             $map = $do->links();
601         }
602          
603         
604         
605         
606         // current table..
607         $tabdef = $do->table();
608         if (isset($tabdef['passwd'])) {
609             // prevent exposure of passwords..
610             unset($tabdef['passwd']);     
611         }
612         $xx = clone($do);
613         $xx = array_keys($tabdef);
614         $do->selectAdd(); // we need thsi as normally it's only cleared by an empty selectAs call.
615         
616         if ($filter) {
617             $cols = array();
618          
619             foreach($xx as $c) {
620                 if (in_array($c, $filter)) {
621                     $cols[] = $c;
622                 }
623             }
624             $do->selectAs($cols);
625         } else {
626             $do->selectAs($xx);
627         }
628         
629         
630         $this->cols = array();
631         foreach($xx as $k) {
632             $this->cols[$k] = $do->tableName(). '.' . $k;
633         }
634         
635         
636         
637         
638          
639         
640         foreach($map as $ocl=>$info) {
641             
642             list($tab,$col) = explode(':', $info);
643             // what about multiple joins on the same table!!!
644             $xx = DB_DataObject::factory($tab);
645             if (!is_a($xx, 'DB_DataObject')) {
646                 continue;
647             }
648             // this is borked ... for multiple jions..
649             $do->joinAdd($xx, 'LEFT', 'join_'.$ocl.'_'. $col, $ocl);
650             $tabdef = $xx->table();
651             $table = $xx->tableName();
652             if (isset($tabdef['passwd'])) {
653              
654                 unset($tabdef['passwd']);
655               
656             } 
657             $xx = array_keys($tabdef);
658             
659             
660             if ($filter) {
661                 $cols = array();
662                 foreach($xx as $c) {
663                     
664                     $tn = sprintf($ocl.'_%s', $c);
665                     if (in_array($tn, $filter)) {
666                         $cols[] = $c;
667                     }
668                 }
669                 $do->selectAs($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
670             } else {
671                 $do->selectAs($xx,  $ocl.'_%s', 'join_'.$ocl.'_'. $col);
672             }
673              
674             
675             foreach($xx as $k) {
676                 $this->cols[sprintf($ocl.'_%s', $k)] = $tab.'.'.$k;
677             }
678             
679             
680         }
681         //DB_DataObject::debugLevel(1);
682         
683         
684         
685     }
686     /**
687      * generate the meta data neede by queries.
688      * 
689      */
690     function meta($x, $data)
691     {
692         // this is not going to work on queries where the data does not match the database def..
693         // for unknown columns we send them as stirngs..
694         $lost = 0;
695         $cols  = array_keys($data[0]);
696      
697         
698         
699         
700         //echo '<PRE>';print_r($this->cols); exit;
701         $options = &PEAR::getStaticProperty('DB_DataObject','options');
702         $reader = $options["ini_{$x->_database}"] .'.reader';
703         if (!file_exists( $reader )) {
704             return;
705         }
706         
707         $rdata = unserialize(file_get_contents($reader));
708         
709        // echo '<PRE>';print_r($rdata);exit;
710         
711         $meta = array();
712         foreach($cols as $c ) {
713             if (!isset($this->cols[$c]) || !isset($rdata[$this->cols[$c]]) || !is_array($rdata[$this->cols[$c]])) {
714                 $meta[] = $c;
715                 continue;    
716             }
717             $add = $rdata[$this->cols[$c]];
718             $add['name'] = $c;
719             $meta[] = $add;
720         }
721         return array(
722             'totalProperty' =>  'total',
723             'successProperty' => 'success',
724             'root' => 'data',
725             'id' => 'id',
726             'fields' => $meta
727         );
728          
729         
730     }
731     
732     function setFilters($x, $q)
733     {
734         // if a column is type int, and we get ',' -> the it should be come an inc clause..
735        // DB_DataObject::debugLevel(1);
736         if (method_exists($x, 'applyFilters')) {
737            // DB_DataObject::debugLevel(1);
738             $x->applyFilters($q, $this->authUser);
739         }
740         $q_filtered = array();
741         
742         foreach($q as $key=>$val) {
743             
744             if (is_array($val) ) {
745                 
746                 if (!in_array( $key,  array_keys($this->cols))) {
747                     continue;
748                 }
749                 
750                 // support a[0] a[1] ..... => whereAddIn(
751                 $ar = array();
752                 $quote = false;
753                 foreach($val as $k=>$v) {
754                     if (!is_numeric($k)) {
755                         $ar = array();
756                         break;
757                     }
758                     if (!is_numeric($v) || !is_long($v)) {
759                         $quote = true;
760                     }
761                     $ar[] = $v;
762                     
763                 }
764                 if (count($ar)) {
765                     $x->whereAddIn($key,$ar, $quote ? 'string' : 'int');
766                 }
767                 
768                 continue;
769             }
770             
771             
772             
773             if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
774                     
775                 $x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
776                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
777                 );
778                 continue;
779                 
780             }
781             
782             
783             
784             switch($key) {
785                     
786                 // Events and remarks
787                 case 'on_id':  // where TF is this used...
788                     if (!empty($q['query']['original'])) {
789                       //  DB_DataObject::debugLevel(1);
790                         $o = (int) $q['query']['original'];
791                         $oid = (int) $val;
792                         $x->whereAdd("(on_id = $oid  OR 
793                                 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o ) 
794                             )");
795                         continue;
796                                 
797                     }
798                     $x->on_id = $val;
799                 
800                 
801                 default:
802                     if (strlen($val)) {
803                         $q_filtered[$key] = $val;
804                     }
805                     
806                     continue;
807             }
808         }
809         $x->setFrom($q_filtered);
810        
811         // nice generic -- let's get rid of it.. where is it used!!!!
812         // used by: 
813         // Person / Group
814         if (!empty($q['query']['name'])) {
815             $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
816         }
817         
818         // - projectdirectory staff list - persn queuy
819       
820          
821        
822          
823           
824         
825         
826     }
827     
828 }