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