3 trait Pman_Core_RooGetTrait {
5 * GET method Roo/TABLENAME.php
7 * Generally for SELECT or Single SELECT
10 * _id=value single fetch based on primary id.
11 * can be '0' if you want to fetch a set of defaults
12 * Use in conjuntion with toRooSingleArray()
14 * lookup[key]=value single fetch based on a single key value lookup.
15 * multiple key/value can be used. eg. ontable+onid..
16 * _columns what to return.
20 * - all tables are always autojoined.
24 * _columns=a,b,c,d comma seperated list of columns.
25 * _columns_exclude=a,b,c,d comma seperated list of columns.
26 * _distinct=name a distinct column lookup. you also have to use _columns with this.
29 * colname = ... => colname = ....
30 * !colname=.... => colname != ....
31 * !colname[0]=... !colname[1]=... => colname NOT IN (.....) ** only supports main table at present..
32 * colname[0]=... colname[1]=... => colname IN (.....) ** only supports main table at present..
35 * sort=name what to sort.
36 * sort=a,b,d can support multiple columns
37 * dir=ASC what direction
38 * _multisort ={...} JSON encoded { sort : { row : direction }, order : [ row, row, row ] }
42 * limit=25 limit number
46 * csvCols[0] csvCols[1].... = .... column titles for CSV output
47 * csvTitles[0], csvTitles[1] .... = columns to use for CSV output
50 * _toggleActive !:!:!:! - this hsould not really be here..
51 * query[add_blank] - add a line in with an empty option... - not really needed???
52 * _delete = delete a list of ids element. (depricated.. this will be removed...)
55 * _post =1 = simulate a post with debuggin on.
56 * _debug_post << This is prefered, as _post may overlap with accouting posts..
58 * _debug = turn on DB_dataobject deubbing, must be admin at present..
61 * CALLS methods on dataobjects if they exist
64 * checkPerm('S' , $authuser)
65 * - can we list the stuff
66 * - return false to disallow...
67 * applySort($au, $sortcol, $direction, $array_of_columns, $multisort)
68 * -- does not support multisort at present..
69 * applyFilters($_REQUEST, $authUser, $roo)
70 * -- apply any query filters on data. and hide stuff not to be seen.
71 * -- can exit by calling $roo->jerr()
72 * postListExtra($_REQUEST) : array(extra_name => data)
73 * - add extra column data on the results (like new messages etc.)
74 * postListFilter($data, $authUser, $request) return $data
75 * - add extra data to an object
78 * toRooSingleArray($authUser, $request) : array
79 * - called on single fetch only, add or maniuplate returned array data.
80 * - is also called when _id=0 is used (for fetching a default set.)
81 * toRooArray($request) : array
82 * - called if singleArray is unavailable on single fetch.
83 * - always tried for mutiple results.
84 * toArray() - the default method if none of the others are found.
87 * - standard DataObject feature - causes all results to show all
91 * _extra_cols -- if set, then filtering by column etc. will use them.
99 HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
101 if ( $this->checkDebugPost()) {
103 return $this->post($tab);
108 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
110 $tab = array_shift(explode('/', $tab));
112 $x = $this->dataObject($tab);
114 $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
116 if (isset( $_REQUEST['lookup'] ) && is_array($_REQUEST['lookup'] )) { // single fetch based on key/value pairs
117 $this->selectSingle($x, $_REQUEST['lookup'],$_REQUEST);
122 // single fetch (use '0' to fetch an empty object..)
123 if (isset($_REQUEST['_id']) && is_numeric($_REQUEST['_id'])) {
125 $this->selectSingle($x, $_REQUEST['_id'],$_REQUEST);
132 if (isset($_REQUEST['_delete'])) {
133 $this->jerr("DELETE by GET has been removed - update the code to use POST");
138 $this->jerr('no key');
141 $this->key = $keys[0];
144 // do we really delete stuff!?!?!?
145 return $this->delete($x,$_REQUEST);
152 if (isset($_REQUEST['_toggleActive'])) {
153 // do we really delete stuff!?!?!?
154 if (!$this->hasPerm("Core.Staff", 'E')) {
155 $this->jerr("PERMISSION DENIED (ta)");
157 $clean = create_function('$v', 'return (int)$v;');
158 $bits = array_map($clean, explode(',', $_REQUEST['_toggleActive']));
159 if (in_array($this->authUser->id, $bits) && $this->authUser->active) {
160 $this->jerr("you can not disable yourself");
162 $x->query('UPDATE Person SET active = !active WHERE id IN (' .implode(',', $bits).')');
163 $this->addEvent("USERTOGGLE", false, implode(',', $bits));
164 $this->jok("Updated");
167 //DB_DataObject::debugLevel(1);
170 // sets map and countWhat
171 $this->loadMap($x, array(
172 'columns' => $_columns,
173 'distinct' => empty($_REQUEST['_distinct']) ? false: $_REQUEST['_distinct'],
174 'exclude' => empty($_REQUEST['_exclude_columns']) ? false: explode(',', $_REQUEST['_exclude_columns'])
178 $this->setFilters($x,$_REQUEST);
180 if (!$this->checkPerm($x,'S', $_REQUEST)) {
181 $this->jerr("PERMISSION DENIED (g)");
185 // build join if req.
186 //DB_DataObject::debugLevel(1);
187 // var_dump($this->countWhat);
188 $total = $x->count($this->countWhat);
191 //var_dump($total);exit;
192 $this->applySort($x);
196 if (!empty($_REQUEST['_distinct']) && $total < 400) {
203 empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
204 min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 10000)
207 $queryObj = clone($x);
208 //DB_DataObject::debuglevel(1);
210 $this->sessionState(0);
212 $this->sessionState(1);
214 if (false === $res) {
215 $this->jerr($x->_lastError->toString());
223 // ---------------- THESE ARE DEPRICATED.. they should be moved to the model...
226 if (!empty($_REQUEST['query']['add_blank'])) {
227 $ret[] = array( 'id' => 0, 'name' => '----');
231 $rooar = method_exists($x, 'toRooArray');
232 $_columnsf = $_columns ? array_flip($_columns) : false;
233 while ($x->fetch()) {
235 $add = $rooar ? $x->toRooArray($_REQUEST) : $x->toArray();
236 if ($add === false) {
239 $ret[] = !$_columns ? $add : array_intersect_key($add, $_columnsf);
243 $ret = array_slice($ret,
244 empty($_REQUEST['start']) ? 0 : (int)$_REQUEST['start'],
245 min(empty($_REQUEST['limit']) ? 25 : (int)$_REQUEST['limit'], 10000)
252 if (method_exists($queryObj ,'postListExtra')) {
253 $extra = $queryObj->postListExtra($_REQUEST, $this);
257 // filter results, and add any data that is needed...
258 if (method_exists($x,'postListFilter')) {
259 $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
264 if (!empty($_REQUEST['csvCols']) && !empty($_REQUEST['csvTitles']) ) {
267 $this->toCsv($ret, $_REQUEST['csvCols'], $_REQUEST['csvTitles'],
268 empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename']
276 //if ($x->tableName() == 'Documents_Tracking') {
277 // $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
282 if (!empty($_REQUEST['_requestMeta']) && count($ret)) {
283 $meta = $this->meta($x, $ret);
285 $extra['metaData'] = $meta;
288 // this make take some time...
289 $this->sessionState(0);
290 // echo "<PRE>"; print_r($ret);
291 $this->jdata($ret, max(count($ret), $total), $extra );
296 function checkDebugPost()
298 return (!empty($_GET['_post']) || !empty($_GET['_debug_post'])) &&
300 method_exists($this->authUser,'groups') &&
301 in_array('Administrators', $this->authUser->groups('name'));
305 function selectSingle($x, $id, $req=false)
307 $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
309 if (!is_array($id) && empty($id)) {
311 if (method_exists($x, 'toRooSingleArray')) {
312 $this->jok($x->toRooSingleArray($this->getAuthUser(), $req));
315 if (method_exists($x, 'toRooArray')) {
316 $this->jok($x->toRooArray($req));
319 $this->jok($x->toArray());
323 $this->loadMap($x, array(
324 'columns' => $_columns,
327 if ($req !== false) {
328 $this->setFilters($x, $req);
333 $x->setFrom($req['lookup'] );
335 if (!$x->find(true)) {
336 if (!empty($id['_id'])) {
338 $this->jok($x->toArray());
343 } else if (!$x->get($id)) {
344 $this->jerr("selectSingle: no such record ($id)");
347 // ignore perms if comming from update/insert - as it's already done...
348 if ($req !== false && !$this->checkPerm($x,'S')) {
349 $this->jerr("PERMISSION DENIED - si");
351 // different symantics on all these calls??
352 if (method_exists($x, 'toRooSingleArray')) {
353 $this->jok($x->toRooSingleArray($this->authUser, $req));
355 if (method_exists($x, 'toRooArray')) {
356 $this->jok($x->toRooArray($req));
359 $this->jok($x->toArray());
364 function setFilters($x, $q)
366 if (method_exists($x, 'applyFilters')) {
367 // DB_DataObject::debugLevel(1);
368 if (false === $x->applyFilters($q, $this->getAuthUser(), $this)) {
372 $q_filtered = array();
375 // var_dump($keys);exit;
376 foreach($q as $key=>$val) {
378 if (in_array($key,$keys) && !is_array($val)) {
383 // handles name[]=fred&name[]=brian => name in ('fred', 'brian').
384 // value is an array..
385 if (is_array($val) ) {
389 if ($key[0] == '!') {
391 $key = substr($key,1);
394 if (!in_array( $key, array_keys($this->cols))) {
398 // support a[0] a[1] ..... => whereAddIn(
401 foreach($val as $k=>$v) {
402 if (!is_numeric($k)) {
406 // FIXME: note this is not typesafe for anything other than mysql..
408 if (!is_numeric($v) || !is_long($v)) {
417 $x->whereAddIn($pref . (
418 isset($this->colsJname[$key]) ?
419 $this->colsJname[$key] :
420 ($x->tableName(). '.'.$key)),
421 $ar, $quote ? 'string' : 'int');
428 // handles !name=fred => name not equal fred.
429 if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
431 $key = substr($key, 1) ;
434 isset($this->colsJname[$key]) ?
435 $this->colsJname[$key] :
436 $x->tableName(). '.'.$key ) . ' != ' .
437 (is_numeric($val) ? $val : "'". $x->escape($val) . "'")
445 // Events and remarks -- fixme - move to events/remarsk...
446 case 'on_id': // where TF is this used...
447 if (!empty($q['query']['original'])) {
448 // DB_DataObject::debugLevel(1);
449 $o = (int) $q['query']['original'];
451 $x->whereAdd("(on_id = $oid OR
452 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o )
461 if (strlen($val) && $key[0] != '_') {
462 $q_filtered[$key] = $val;
465 // subjoined columns = check the values.
466 // note this is not typesafe for anything other than mysql..
468 if (isset($this->colsJname[$key])) {
470 if (!is_numeric($val) || !is_long($val)) {
473 $x->whereAdd( "{$this->colsJname[$key]} = " . ($quote ? "'". $x->escape($val) ."'" : $val));
481 if (!empty($q_filtered)) {
482 $x->setFrom($q_filtered);
485 if (!empty($q['query']['name'])) {
486 if (in_array( 'name', array_keys($x->table()))) {
487 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");