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 * csvCols = return data as csv
55 * csvTitles = return data as csv
58 * dir = sort direction
60 * limit = limit number
62 * _toggleActive !:!:!:! - this hsould not really be here..
63 * query[add_blank] - add a line in with an empty option... - not really needed???
68 // $this->jerr("Not authenticated", array('authFailure' => true));
69 //DB_DataObject::debuglevel(1);
72 if (!empty($_GET['_post'])) {
74 DB_DAtaObject::debuglevel(1);
75 return $this->post($tab);
77 $tab = str_replace('/', '',$tab); // basic protection??
78 $x = DB_DataObject::factory($tab);
79 if (!is_a($x, 'DB_DataObject')) {
80 $this->jerr('invalid url');
82 $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
84 if (isset( $_REQUEST['lookup'] )) { // single fetch based on key/value pairs
85 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
86 $this->jerr("PERMISSION DENIED");
88 $this->loadMap($x, $_columns);
89 $x->setFrom($_REQUEST['lookup'] );
91 if (!$x->find(true)) {
94 $this->jok($x->toArray());
99 if (isset($_REQUEST['_id'])) { // single fetch
101 if (empty($_REQUEST['_id'])) {
102 $this->jok($x->toArray()); // return an empty array!
105 $this->loadMap($x, $_columns);
107 if (!$x->get($_REQUEST['_id'])) {
108 $this->jerr("no such record");
111 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
112 $this->jerr("PERMISSION DENIED");
115 $this->jok(method_exists($x, 'toRooSingleArray') ? $x->toRooSingleArray($this->authUser) : $x->toArray());
118 if (isset($_REQUEST['_delete'])) {
119 // do we really delete stuff!?!?!?
122 $clean = create_function('$v', 'return (int)$v;');
124 $bits = array_map($clean, explode(',', $_REQUEST['_delete']));
125 $x->whereAdd('id IN ('. implode(',', $bits) .')');
128 while ($x->fetch()) {
131 if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser)) {
132 $this->jerr("PERMISSION DENIED");
135 $this->addEvent("DELETE", $x, $x->toEventString());
136 if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
137 $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
143 $this->jerr(implode("\n<BR>", $errs));
145 $this->jok("Deleted");
148 if (isset($_REQUEST['_toggleActive'])) {
149 // do we really delete stuff!?!?!?
150 if (!$this->hasPerm("Core.Staff", 'E')) {
151 $this->jerr("PERMISSION DENIED");
153 $clean = create_function('$v', 'return (int)$v;');
154 $bits = array_map($clean, explode(',', $_REQUEST['_toggleActive']));
155 if (in_array($this->authUser->id, $bits) && $this->authUser->active) {
156 $this->jerr("you can not disable yourself");
158 $x->query('UPDATE Person SET active = !active WHERE id IN (' .implode(',', $bits).')');
159 $this->addEvent("USERTOGGLE", false, implode(',', $bits));
160 $this->jok("Updated");
163 // DB_DataObject::debugLevel(1);
164 if (method_exists($x, 'checkPerm') && !$x->checkPerm('S', $this->authUser)) {
165 $this->jerr("PERMISSION DENIED");
167 $map = $this->loadMap($x, $_columns);
169 $this->setFilters($x,$_REQUEST);
173 // build join if req.
175 if (!empty($_REQUEST['_distinct'])) {
179 if (isset($cols[$_REQUEST['_distinct']])) {
180 $countWhat = 'distinct ' . $_REQUEST['_distinct'];
184 $total = $x->count($countWhat);
186 // DB_DataObject::debugLevel(1);
188 $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
189 $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
192 if (method_exists($x, 'applySort')) {
193 $sorted = $x->applySort($this->authUser, $sort, $dir, $this->cols);
195 if ($sorted === false) {
198 // echo '<PRE>';print_r(array($sort, $this->cols));
200 // $otherSorts = array('person_id_name');
202 if (strlen($sort) && isset($cols[$sort]) ) {
203 $sort = $x->tableName() .'.'.$sort . ' ' . $dir ;
205 } else if (in_array($sort, $this->cols)) {
206 $sort = $sort . ' ' . $dir ;
208 }// else other formatas?
209 //if ( in_array($sort, $otherSorts)) {
210 // $x->orderBy($sort . ' ' . $dir);
217 empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
218 min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 1000)
221 $queryObj = clone($x);
226 if (!empty($_REQUEST['query']['add_blank'])) {
227 $ret[] = array( 'id' => 0, 'name' => '----');
232 //if (($tab == 'Groups') && ($_REQUEST['type'] != 0)) { // then it's a list of teams..
233 if ($tab == 'Groups') {
235 $ret[] = array( 'id' => 0, 'name' => 'EVERYONE');
236 $ret[] = array( 'id' => -1, 'name' => 'NOT_IN_GROUP');
237 //$ret[] = array( 'id' => 999999, 'name' => 'ADMINISTRATORS');
243 if (!empty($_REQUEST['csvCols']) && !empty($_REQUEST['csvTitles']) ) {
244 header('Content-type: text/csv');
246 header('Content-Disposition: attachment; filename="documentlist-export-'.date('Y-m-d') . '.csv"');
247 //header('Content-type: text/plain');
248 $fh = fopen('php://output', 'w');
249 fputcsv($fh, $_REQUEST['csvTitles']);
250 while ($x->fetch()) {
251 //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
253 foreach($_REQUEST['csvCols'] as $k) {
254 $line[] = isset($x->$k) ? $x->$k : '';
265 $rooar = method_exists($x, 'toRooArray');
267 while ($x->fetch()) {
268 $add = $rooar ? $x->toRooArray($_REQUEST) : $x->toArray();
270 $ret[] = !$_columns ? $add : array_intersect_key($add, array_flip($_columns));
272 //if ($x->tableName() == 'Documents_Tracking') {
273 // $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
277 if (method_exists($queryObj ,'postListExtra')) {
278 $extra = $queryObj->postListExtra($_REQUEST);
280 // filter results, and add any data that is needed...
281 if (method_exists($x,'postListFilter')) {
282 $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
287 // echo "<PRE>"; print_r($ret);
288 $this->jdata($ret,$total, $extra );
293 * POST method Roo/TABLENAME.php
294 * -- updates the data..
297 * _debug - forces debugging on.
298 * _get - forces a get request
299 * _ids - multiple update of records.
300 * {colid} - forces fetching
303 function post($tab) // update / insert (?? dleete??)
305 // DB_DataObject::debugLevel(1);
306 if (!empty($_REQUEST['_debug'])) {
307 DB_DataObject::debugLevel(1);
310 if (!empty($_REQUEST['_get'])) {
311 return $this->get($tab);
313 $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
315 $tab = str_replace('/', '',$tab); // basic protection??
316 $x = DB_DataObject::factory($tab);
317 if (!is_a($x, 'DB_DataObject')) {
318 $this->jerr('invalid url');
320 // find the key and use that to get the thing..
323 $this->jerr('no key');
327 if (!empty($_REQUEST['_ids'])) {
328 $ids = explode(',',$_REQUEST['_ids']);
329 $x->whereAddIn($keys[0], $ids, 'int');
330 $ar = $x->fetchAll();
332 $this->update($x, $_REQUEST);
336 $this->jok("UPDATED");
342 if (!empty($_REQUEST[$keys[0]])) {
344 if (!$x->get($keys[0], $_REQUEST[$keys[0]])) {
345 $this->jerr("Invalid request");
347 $this->jok($this->update($x, $_REQUEST));
349 $this->jok($this->insert($x, $_REQUEST));
356 function insert($x, $req)
360 if (method_exists($x, 'checkPerm') && !$x->checkPerm('A', $this->authUser, $req)) {
361 $this->jerr("PERMISSION DENIED");
364 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
366 if (method_exists($x, 'setFromRoo')) {
367 $res = $x->setFromRoo($req, $this);
368 if (is_string($res)) {
378 if (isset($cols['created'])) {
379 $x->created = date('Y-m-d H:i:s');
381 if (isset($cols['created_dt'])) {
382 $x->created_dt = date('Y-m-d H:i:s');
384 if (isset($cols['created_by'])) {
385 $x->created_by = $this->authUser->id;
389 if (isset($cols['modified'])) {
390 $x->modified = date('Y-m-d H:i:s');
392 if (isset($cols['modified_dt'])) {
393 $x->modified_dt = date('Y-m-d H:i:s');
395 if (isset($cols['modified_by'])) {
396 $x->modified_by = $this->authUser->id;
399 if (isset($cols['updated'])) {
400 $x->updated = date('Y-m-d H:i:s');
402 if (isset($cols['updated_dt'])) {
403 $x->updated_dt = date('Y-m-d H:i:s');
405 if (isset($cols['updated_by'])) {
406 $x->updated_by = $this->authUser->id;
414 if (method_exists($x, 'onInsert')) {
415 $x->onInsert($_REQUEST, $this);
417 $this->addEvent("ADD", $x, $x->toEventString());
419 // note setFrom might handle this before hand...!??!
420 if (!empty($_FILES) && method_exists($x, 'onUpload')) {
424 $r = DB_DataObject::factory($x->tableName());
426 $this->loadMap($r, $_columns);
430 $rooar = method_exists($r, 'toRooArray');
431 //print_r(var_dump($rooar)); exit;
432 return $rooar ? $r->toRooArray() : $r->toArray();
436 function update($x, $req)
438 if (method_exists($x, 'checkPerm') && !$x->checkPerm('E', $this->authUser, $_REQUEST)) {
439 $this->jerr("PERMISSION DENIED");
442 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
447 // this lot is generic.. needs moving
448 if (method_exists($x, 'setFromRoo')) {
449 $res = $x->setFromRoo($req, $this);
450 if (is_string($res)) {
456 $this->addEvent("EDIT", $x, $x->toEventString());
462 if (isset($cols['modified'])) {
463 $x->modified = date('Y-m-d H:i:s');
465 if (isset($cols['modified_dt'])) {
466 $x->modified_dt = date('Y-m-d H:i:s');
468 if (isset($cols['modified_by'])) {
469 $x->modified_by = $this->authUser->id;
472 if (isset($cols['updated'])) {
473 $x->updated = date('Y-m-d H:i:s');
475 if (isset($cols['updated_dt'])) {
476 $x->updated_dt = date('Y-m-d H:i:s');
478 if (isset($cols['updated_by'])) {
479 $x->updated_by = $this->authUser->id;
484 if (method_exists($x, 'onUpdate')) {
485 $x->onUpdate($old, $req, $this);
488 $r = DB_DataObject::factory($x->tableName());
490 $this->loadMap($r, $_columns);
493 $rooar = method_exists($r, 'toRooArray');
494 //print_r(var_dump($rooar)); exit;
495 return $rooar ? $r->toRooArray() : $r->toArray();
501 function loadMap($do, $filter=false)
503 //DB_DataObject::debugLevel(1);
508 $mods = explode(',',$this->appModules);
510 $ff = HTML_FlexyFramework::geT();
511 //$db->databaseName();
513 //$ff->DB_DataObject['ini_'. $db->database()];
514 //echo '<PRE>';print_r($do->links());exit;
517 if (in_array('Builder', $mods) ) {
519 foreach(in_array('Builder', $mods) ? scandir($this->rootDir.'/Pman') : $mods as $m) {
521 if (!strlen($m) || $m[0] == '.' || !is_dir($this->rootDir."/Pman/$m")) {
524 $ini = $this->rootDir."/Pman/$m/DataObjects/pman.links.ini";
525 if (!file_exists($ini)) {
528 $conf = array_merge($conf, parse_ini_file($ini,true));
529 if (!isset($conf[$do->tableName()])) {
532 $map = $conf[$do->tableName()];
542 $tabdef = $do->table();
543 if (isset($tabdef['passwd'])) {
544 // prevent exposure of passwords..
545 unset($tabdef['passwd']);
548 $xx = array_keys($tabdef);
549 $do->selectAdd(); // we need thsi as normally it's only cleared by an empty selectAs call.
555 if (in_array($c, $filter)) {
559 $do->selectAs($cols);
573 foreach($map as $ocl=>$info) {
575 list($tab,$col) = explode(':', $info);
576 // what about multiple joins on the same table!!!
577 $xx = DB_DataObject::factory($tab);
578 if (!is_a($xx, 'DB_DataObject')) {
581 // this is borked ... for multiple jions..
582 $do->joinAdd($xx, 'LEFT', 'join_'.$ocl.'_'. $col, $ocl);
583 $tabdef = $xx->table();
584 $table = $xx->tableName();
585 if (isset($tabdef['passwd'])) {
587 unset($tabdef['passwd']);
590 $xx = array_keys($tabdef);
597 $tn = sprintf($ocl.'_%s', $c);
598 if (in_array($tn, $filter)) {
602 $do->selectAs($cols, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
604 $do->selectAs($xx, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
612 $this->cols[] = sprintf($ocl.'_%s', $k);
617 //DB_DataObject::debugLevel(1);
622 function setFilters($x, $q)
624 // if a column is type int, and we get ',' -> the it should be come an inc clause..
625 // DB_DataObject::debugLevel(1);
626 if (method_exists($x, 'applyFilters')) {
627 // DB_DataObject::debugLevel(1);
628 $x->applyFilters($q, $this->authUser);
630 $q_filtered = array();
632 foreach($q as $key=>$val) {
634 if (is_array($val)) {
637 if ($key[0] == '!' && in_array(substr($key, 1), $this->cols)) {
639 $x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
640 (is_numeric($val) ? $val : "'". $x->escape($val) . "'")
650 // Events and remarks
651 case 'on_id': // where TF is this used...
652 if (!empty($q['query']['original'])) {
653 // DB_DataObject::debugLevel(1);
654 $o = (int) $q['query']['original'];
656 $x->whereAdd("(on_id = $oid OR
657 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o )
667 $q_filtered[$key] = $val;
673 $x->setFrom($q_filtered);
675 // nice generic -- let's get rid of it.. where is it used!!!!
678 if (!empty($q['query']['name'])) {
679 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
682 // - projectdirectory staff list - persn queuy