4 require_once 'Pman.php';
8 * not really sure how save our factory method is....!!!
11 * Uses these methods of the dataobjects:
12 * - checkPerm('L'/'E'/'A', $authuser) - can we list the stuff
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
19 * - toRooSingleArray() // single fetch, add data..
20 * - toRooArray($request) /// toArray if you need to return different data.. for a list fetch.
23 * - beforeDelete() -- return false for fail and set DO->err;
24 * - onUpdate($old, $request,$roo) - after update
25 * - onInsert($request,$roo) - after insert
27 * - setFromRoo($ar) - values from post (deal with dates etc.) - return true|error string.
29 * - toEventString (for logging)
32 class Pman_Roo extends Pman
36 parent::getAuth(); // load company!
37 $au = $this->getAuthUser();
39 $this->jerr("Not authenticated", array('authFailure' => true));
41 $this->authUser = $au;
45 * GET method Roo/TABLENAME.php
46 * -- defaults to listing data. with args.
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.
56 * csvCols = return data as csv
58 * csvTitles = return data as csv
61 * dir = sort direction
63 * limit = limit number
65 * _toggleActive !:!:!:! - this hsould not really be here..
66 * query[add_blank] - add a line in with an empty option... - not really needed???
71 // $this->jerr("Not authenticated", array('authFailure' => true));
72 //DB_DataObject::debuglevel(1);
75 if (!empty($_GET['_post'])) {
77 DB_DAtaObject::debuglevel(1);
78 return $this->post($tab);
80 $tab = str_replace('/', '',$tab); // basic protection??
81 $x = DB_DataObject::factory($tab);
82 if (!is_a($x, 'DB_DataObject')) {
83 $this->jerr('invalid url');
85 $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
87 if (isset( $_REQUEST['lookup'] )) { // single fetch based on key/value pairs
88 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
89 $this->jerr("PERMISSION DENIED");
91 $this->loadMap($x, $_columns);
92 $x->setFrom($_REQUEST['lookup'] );
94 if (!$x->find(true)) {
97 $this->jok($x->toArray());
102 if (isset($_REQUEST['_id'])) { // single fetch
104 if (empty($_REQUEST['_id'])) {
105 $this->jok($x->toArray()); // return an empty array!
108 $this->loadMap($x, $_columns);
110 if (!$x->get($_REQUEST['_id'])) {
111 $this->jerr("no such record");
114 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
115 $this->jerr("PERMISSION DENIED");
118 $this->jok(method_exists($x, 'toRooSingleArray') ? $x->toRooSingleArray($this->authUser) : $x->toArray());
121 if (isset($_REQUEST['_delete'])) {
122 // do we really delete stuff!?!?!?
125 $clean = create_function('$v', 'return (int)$v;');
127 $bits = array_map($clean, explode(',', $_REQUEST['_delete']));
128 $x->whereAdd('id IN ('. implode(',', $bits) .')');
131 while ($x->fetch()) {
134 if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser)) {
135 $this->jerr("PERMISSION DENIED");
138 $this->addEvent("DELETE", $x, $x->toEventString());
139 if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
140 $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
146 $this->jerr(implode("\n<BR>", $errs));
148 $this->jok("Deleted");
151 if (isset($_REQUEST['_toggleActive'])) {
152 // do we really delete stuff!?!?!?
153 if (!$this->hasPerm("Core.Staff", 'E')) {
154 $this->jerr("PERMISSION DENIED");
156 $clean = create_function('$v', 'return (int)$v;');
157 $bits = array_map($clean, explode(',', $_REQUEST['_toggleActive']));
158 if (in_array($this->authUser->id, $bits) && $this->authUser->active) {
159 $this->jerr("you can not disable yourself");
161 $x->query('UPDATE Person SET active = !active WHERE id IN (' .implode(',', $bits).')');
162 $this->addEvent("USERTOGGLE", false, implode(',', $bits));
163 $this->jok("Updated");
166 // DB_DataObject::debugLevel(1);
167 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
168 $this->jerr("PERMISSION DENIED");
170 $map = $this->loadMap($x, $_columns);
172 $this->setFilters($x,$_REQUEST);
176 // build join if req.
178 if (!empty($_REQUEST['_distinct'])) {
182 if (isset($cols[$_REQUEST['_distinct']])) {
183 $countWhat = 'distinct ' . $_REQUEST['_distinct'];
185 $x->selectAdd('distinct('.$_REQUEST['_distinct'].')');
186 $_columns = array( $_REQUEST['_distinct'] );
188 $this->jerr('invalid distinct');
192 $total = $x->count($countWhat);
194 // DB_DataObject::debugLevel(1);
196 $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
197 $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
200 if (method_exists($x, 'applySort')) {
201 $sorted = $x->applySort($this->authUser, $sort, $dir, $this->cols);
203 if ($sorted === false) {
206 // echo '<PRE>';print_r(array($sort, $this->cols));
208 // $otherSorts = array('person_id_name');
210 if (strlen($sort) && isset($cols[$sort]) ) {
211 $sort = $x->tableName() .'.'.$sort . ' ' . $dir ;
213 } else if (in_array($sort, $this->cols)) {
214 $sort = $sort . ' ' . $dir ;
216 }// else other formatas?
217 //if ( in_array($sort, $otherSorts)) {
218 // $x->orderBy($sort . ' ' . $dir);
225 empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
226 min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 1000)
229 $queryObj = clone($x);
234 if (!empty($_REQUEST['query']['add_blank'])) {
235 $ret[] = array( 'id' => 0, 'name' => '----');
240 //if (($tab == 'Groups') && ($_REQUEST['type'] != 0)) { // then it's a list of teams..
241 if ($tab == 'Groups') {
243 $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
244 $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
245 //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
251 if (!empty($_REQUEST['csvCols']) && !empty($_REQUEST['csvTitles']) ) {
252 header('Content-type: text/csv');
254 header('Content-Disposition: attachment; filename="documentlist-export-'.date('Y-m-d') . '.csv"');
255 //header('Content-type: text/plain');
256 $fh = fopen('php://output', 'w');
257 fputcsv($fh, $_REQUEST['csvTitles']);
258 while ($x->fetch()) {
259 //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
261 foreach($_REQUEST['csvCols'] as $k) {
262 $line[] = isset($x->$k) ? $x->$k : '';
273 $rooar = method_exists($x, 'toRooArray');
275 while ($x->fetch()) {
276 $add = $rooar ? $x->toRooArray($_REQUEST) : $x->toArray();
278 $ret[] = !$_columns ? $add : array_intersect_key($add, array_flip($_columns));
280 //if ($x->tableName() == 'Documents_Tracking') {
281 // $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
285 if (method_exists($queryObj ,'postListExtra')) {
286 $extra = $queryObj->postListExtra($_REQUEST);
288 // filter results, and add any data that is needed...
289 if (method_exists($x,'postListFilter')) {
290 $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
295 // echo "<PRE>"; print_r($ret);
296 $this->jdata($ret,$total, $extra );
301 * POST method Roo/TABLENAME.php
302 * -- updates the data..
305 * _debug - forces debugging on.
306 * _get - forces a get request
307 * _ids - multiple update of records.
308 * {colid} - forces fetching
311 function post($tab) // update / insert (?? dleete??)
313 // DB_DataObject::debugLevel(1);
314 if (!empty($_REQUEST['_debug'])) {
315 DB_DataObject::debugLevel(1);
318 if (!empty($_REQUEST['_get'])) {
319 return $this->get($tab);
321 $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
323 $tab = str_replace('/', '',$tab); // basic protection??
324 $x = DB_DataObject::factory($tab);
325 if (!is_a($x, 'DB_DataObject')) {
326 $this->jerr('invalid url');
328 // find the key and use that to get the thing..
331 $this->jerr('no key');
335 if (!empty($_REQUEST['_ids'])) {
336 $ids = explode(',',$_REQUEST['_ids']);
337 $x->whereAddIn($keys[0], $ids, 'int');
338 $ar = $x->fetchAll();
340 $this->update($x, $_REQUEST);
344 $this->jok("UPDATED");
350 if (!empty($_REQUEST[$keys[0]])) {
352 if (!$x->get($keys[0], $_REQUEST[$keys[0]])) {
353 $this->jerr("Invalid request");
355 $this->jok($this->update($x, $_REQUEST));
357 $this->jok($this->insert($x, $_REQUEST));
364 function insert($x, $req)
368 if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req)) {
369 $this->jerr("PERMISSION DENIED");
372 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
374 if (method_exists($x, 'setFromRoo')) {
375 $res = $x->setFromRoo($req, $this);
376 if (is_string($res)) {
386 if (isset($cols['created'])) {
387 $x->created = date('Y-m-d H:i:s');
389 if (isset($cols['created_dt'])) {
390 $x->created_dt = date('Y-m-d H:i:s');
392 if (isset($cols['created_by'])) {
393 $x->created_by = $this->authUser->id;
397 if (isset($cols['modified'])) {
398 $x->modified = date('Y-m-d H:i:s');
400 if (isset($cols['modified_dt'])) {
401 $x->modified_dt = date('Y-m-d H:i:s');
403 if (isset($cols['modified_by'])) {
404 $x->modified_by = $this->authUser->id;
407 if (isset($cols['updated'])) {
408 $x->updated = date('Y-m-d H:i:s');
410 if (isset($cols['updated_dt'])) {
411 $x->updated_dt = date('Y-m-d H:i:s');
413 if (isset($cols['updated_by'])) {
414 $x->updated_by = $this->authUser->id;
422 if (method_exists($x, 'onInsert')) {
423 $x->onInsert($_REQUEST, $this);
425 $this->addEvent("ADD", $x, $x->toEventString());
427 // note setFrom might handle this before hand...!??!
428 if (!empty($_FILES) && method_exists($x, 'onUpload')) {
432 $r = DB_DataObject::factory($x->tableName());
434 $this->loadMap($r, $_columns);
438 $rooar = method_exists($r, 'toRooArray');
439 //print_r(var_dump($rooar)); exit;
440 return $rooar ? $r->toRooArray() : $r->toArray();
444 function update($x, $req)
446 if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser, $_REQUEST)) {
447 $this->jerr("PERMISSION DENIED");
450 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
455 // this lot is generic.. needs moving
456 if (method_exists($x, 'setFromRoo')) {
457 $res = $x->setFromRoo($req, $this);
458 if (is_string($res)) {
464 $this->addEvent("EDIT", $x, $x->toEventString());
470 if (isset($cols['modified'])) {
471 $x->modified = date('Y-m-d H:i:s');
473 if (isset($cols['modified_dt'])) {
474 $x->modified_dt = date('Y-m-d H:i:s');
476 if (isset($cols['modified_by'])) {
477 $x->modified_by = $this->authUser->id;
480 if (isset($cols['updated'])) {
481 $x->updated = date('Y-m-d H:i:s');
483 if (isset($cols['updated_dt'])) {
484 $x->updated_dt = date('Y-m-d H:i:s');
486 if (isset($cols['updated_by'])) {
487 $x->updated_by = $this->authUser->id;
492 if (method_exists($x, 'onUpdate')) {
493 $x->onUpdate($old, $req, $this);
496 $r = DB_DataObject::factory($x->tableName());
498 $this->loadMap($r, $_columns);
501 $rooar = method_exists($r, 'toRooArray');
502 //print_r(var_dump($rooar)); exit;
503 return $rooar ? $r->toRooArray() : $r->toArray();
509 function loadMap($do, $filter=false)
511 //DB_DataObject::debugLevel(1);
516 $mods = explode(',',$this->appModules);
518 $ff = HTML_FlexyFramework::geT();
519 //$db->databaseName();
521 //$ff->DB_DataObject['ini_'. $db->database()];
522 //echo '<PRE>';print_r($do->links());exit;
525 if (in_array('Builder', $mods) ) {
527 foreach(in_array('Builder', $mods) ? scandir($this->rootDir.'/Pman') : $mods as $m) {
529 if (!strlen($m) || $m[0] == '.' || !is_dir($this->rootDir."/Pman/$m")) {
532 $ini = $this->rootDir."/Pman/$m/DataObjects/pman.links.ini";
533 if (!file_exists($ini)) {
536 $conf = array_merge($conf, parse_ini_file($ini,true));
537 if (!isset($conf[$do->tableName()])) {
540 $map = $conf[$do->tableName()];
550 $tabdef = $do->table();
551 if (isset($tabdef['passwd'])) {
552 // prevent exposure of passwords..
553 unset($tabdef['passwd']);
556 $xx = array_keys($tabdef);
557 $do->selectAdd(); // we need thsi as normally it's only cleared by an empty selectAs call.
563 if (in_array($c, $filter)) {
567 $do->selectAs($cols);
581 foreach($map as $ocl=>$info) {
583 list($tab,$col) = explode(':', $info);
584 // what about multiple joins on the same table!!!
585 $xx = DB_DataObject::factory($tab);
586 if (!is_a($xx, 'DB_DataObject')) {
589 // this is borked ... for multiple jions..
590 $do->joinAdd($xx, 'LEFT', 'join_'.$ocl.'_'. $col, $ocl);
591 $tabdef = $xx->table();
592 $table = $xx->tableName();
593 if (isset($tabdef['passwd'])) {
595 unset($tabdef['passwd']);
598 $xx = array_keys($tabdef);
605 $tn = sprintf($ocl.'_%s', $c);
606 if (in_array($tn, $filter)) {
610 $do->selectAs($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
612 $do->selectAs($xx, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
620 $this->cols[] = sprintf($ocl.'_%s', $k);
625 //DB_DataObject::debugLevel(1);
630 function setFilters($x, $q)
632 // if a column is type int, and we get ',' -> the it should be come an inc clause..
633 // DB_DataObject::debugLevel(1);
634 if (method_exists($x, 'applyFilters')) {
635 // DB_DataObject::debugLevel(1);
636 $x->applyFilters($q, $this->authUser);
638 $q_filtered = array();
640 foreach($q as $key=>$val) {
642 if (is_array($val)) {
645 if ($key[0] == '!' && in_array(substr($key, 1), $this->cols)) {
647 $x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
648 (is_numeric($val) ? $val : "'". $x->escape($val) . "'")
658 // Events and remarks
659 case 'on_id': // where TF is this used...
660 if (!empty($q['query']['original'])) {
661 // DB_DataObject::debugLevel(1);
662 $o = (int) $q['query']['original'];
664 $x->whereAdd("(on_id = $oid OR
665 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o )
675 $q_filtered[$key] = $val;
681 $x->setFrom($q_filtered);
683 // nice generic -- let's get rid of it.. where is it used!!!!
686 if (!empty($q['query']['name'])) {
687 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
690 // - projectdirectory staff list - persn queuy