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