* - postListExtra - add extra column data on the results (like new messages etc.)
* -postListFilter($data, $authUser, $request) return $data - add extra data to an object
*
- * - toRooSingleArray() // single fetch, add data..
+ * - toRooSingleArray($authUser) // single fetch, add data..
* - toRooArray($request) /// toArray if you need to return different data.. for a list fetch.
*
*
* - beforeDelete() -- return false for fail and set DO->err;
- * - onUpdate($old, $request,$roo) - after update
+ * - onUpdate($old, $request,$roo) - after update // return value ignored
* - onInsert($request,$roo) - after insert
* - onUpload($roo)
* - setFromRoo($ar) - values from post (deal with dates etc.) - return true|error string.
* GET method Roo/TABLENAME.php
* -- defaults to listing data. with args.
*
+ * !colname=.... => colname != ....
+ * colname[0]=... colname[1]=... => colname IN (.....) ** only supports main table at present..
+ *
* other opts:
* _post = simulate a post with debuggin on.
* lookup = array( k=>v) single fetch based on a key/value pair
* _id = single fetch based on id.
* _delete = delete a list of ids element. (seperated by ,);
* _columns = comma seperated list of columns.
+ * _distinct = a distinct column lookup.
+ * _requestMeta = default behaviour of Roo stores.. on first query..
+ *
* csvCols = return data as csv
+ *
* csvTitles = return data as csv
*
- * sort = sort column
- * dir = sort direction
+ * sort = sort column (',' comma delimited)
+ * dir = sort direction ?? in future comma delimited...
* start = limit start
* limit = limit number
*
function get($tab)
{
// $this->jerr("Not authenticated", array('authFailure' => true));
- //DB_DataObject::debuglevel(1);
+ // DB_DataObject::debuglevel(1);
// debugging...
if (!empty($_GET['_post'])) {
}
if (isset($_REQUEST['_delete'])) {
// do we really delete stuff!?!?!?
-
-
- $clean = create_function('$v', 'return (int)$v;');
-
- $bits = array_map($clean, explode(',', $_REQUEST['_delete']));
- $x->whereAdd('id IN ('. implode(',', $bits) .')');
- $x->find();
- $errs = array();
- while ($x->fetch()) {
- $xx = clone($x);
-
- if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser)) {
- $this->jerr("PERMISSION DENIED");
- }
-
- $this->addEvent("DELETE", $x, $x->toEventString());
- if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
- $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
- continue;
- }
- $xx->delete();
- }
- if ($errs) {
- $this->jerr(implode("\n<BR>", $errs));
- }
- $this->jok("Deleted");
-
- }
+ return $this->delete($x,$_REQUEST);
+ }
+
+
+
+
if (isset($_REQUEST['_toggleActive'])) {
// do we really delete stuff!?!?!?
if (!$this->hasPerm("Core.Staff", 'E')) {
// build join if req.
-
- $total = $x->count();
+ $countWhat = false;
+ if (!empty($_REQUEST['_distinct'])) {
+ $cols = $x->table();
+ // print_r($cols);
+
+ if (isset($cols[$_REQUEST['_distinct']])) {
+ $countWhat = 'distinct ' . $_REQUEST['_distinct'];
+ $x->selectAdd();
+ $x->selectAdd('distinct('.$_REQUEST['_distinct'].')');
+ $_columns = array( $_REQUEST['_distinct'] );
+ } else {
+ $this->jerr('invalid distinct');
+ }
+
+ }
+ $total = $x->count($countWhat);
// sorting..
// DB_DataObject::debugLevel(1);
- $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
- $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
-
- $sorted = false;
- if (method_exists($x, 'applySort')) {
- $sorted = $x->applySort($this->authUser, $sort, $dir, $this->cols);
- }
- if ($sorted === false) {
-
- $cols = $x->table();
- // echo '<PRE>';print_r(array($sort, $this->cols));
- // other sorts???
- // $otherSorts = array('person_id_name');
-
- if (strlen($sort) && isset($cols[$sort]) ) {
- $sort = $x->tableName() .'.'.$sort . ' ' . $dir ;
- $x->orderBy($sort );
- } else if (in_array($sort, $this->cols)) {
- $sort = $sort . ' ' . $dir ;
- $x->orderBy($sort );
- }// else other formatas?
- //if ( in_array($sort, $otherSorts)) {
- // $x->orderBy($sort . ' ' . $dir);
- ////}
- }
+ $this->applySort($x);
$ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
}
-
+ if (!empty($_REQUEST['_requestMeta']) && count($ret)) {
+ $meta = $this->meta($x, $ret);
+ if ($meta) {
+ $extra['metaData'] = $meta;
+ }
+ }
// echo "<PRE>"; print_r($ret);
$this->jdata($ret,$total, $extra );
+ }
+ /**
+ * applySort
+ *
+ * apply REQUEST[sort] and [dir]
+ * sort may be an array of columsn..
+ *
+ * @arg DB_DataObject $x
+ *
+ */
+ function applySort($x)
+ {
+
+ // Db_DataObject::debugLevel(1);
+ $sort = empty($_REQUEST['sort']) ? '' : $_REQUEST['sort'];
+ $dir = (empty($_REQUEST['dir']) || strtoupper($_REQUEST['dir']) == 'ASC' ? 'ASC' : 'DESC');
+
+
+
+ $sorted = false;
+ if (method_exists($x, 'applySort')) {
+ $sorted = $x->applySort($this->authUser, $sort, $dir, array_keys($this->cols));
+ }
+ if ($sorted === false) {
+
+ $cols = $x->table();
+ $sort_ar = explode(',', $sort);
+ $sort_str = array();
+
+ foreach($sort_ar as $sort) {
+
+ if (strlen($sort) && isset($cols[$sort]) ) {
+ $sort_str[] = $x->tableName() .'.'.$sort . ' ' . $dir ;
+
+ } else if (in_array($sort, array_keys($this->cols))) {
+ $sort_str[] = $sort . ' ' . $dir ;
+ }
+ }
+
+ if ($sort_str) {
+ $x->orderBy(implode(', ', $sort_str ));
+ }
+ }
}
/**
* POST method Roo/TABLENAME.php
if (!empty($_REQUEST['_get'])) {
return $this->get($tab);
}
+
$_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
$tab = str_replace('/', '',$tab); // basic protection??
if (empty($keys) ) {
$this->jerr('no key');
}
+
+ // delete should be here...
+ if (isset($_REQUEST['_delete'])) {
+ // do we really delete stuff!?!?!?
+ return $this->delete($x,$_REQUEST);
+ }
+
+
$old = false;
if (!empty($_REQUEST['_ids'])) {
}
$this->jok($this->update($x, $_REQUEST));
} else {
+
+ if (empty($_POST)) {
+ $this->jerr("No data recieved for inserting");
+ }
+
$this->jok($this->insert($x, $_REQUEST));
}
}
$r = DB_DataObject::factory($x->tableName());
- $r->id = $x->id;
+ $pk = $x->keys();
+ // let's assume it has a key!!!
+ $pk = $pk[0];
+ $r->$pk = $x->$pk;
$this->loadMap($r, $_columns);
$r->limit(1);
$r->find(true);
$rooar = method_exists($r, 'toRooArray');
//print_r(var_dump($rooar)); exit;
- return $rooar ? $r->toRooArray() : $r->toArray();
+ return $rooar ? $r->toRooArray($_REQUEST) : $r->toArray();
}
}
$r = DB_DataObject::factory($x->tableName());
- $r->id = $x->id;
+ $pk = $x->keys();
+ // let's assume it has a key!!!
+ $pk = $pk[0];
+ $r->$pk = $x->$pk;
$this->loadMap($r, $_columns);
$r->limit(1);
$r->find(true);
$rooar = method_exists($r, 'toRooArray');
//print_r(var_dump($rooar)); exit;
- return $rooar ? $r->toRooArray() : $r->toArray();
+ return $rooar ? $r->toRooArray($_REQUEST) : $r->toArray();
}
+ function delete($x, $req)
+ {
+ // do we really delete stuff!?!?!?
+ if (empty($req['_delete'])) {
+ $this->jerr("Delete Requested with no value");
+
+ }
+ // build a list of tables to queriy for dependant data..
+ $map = $x->links();
+
+ $affects = array();
+
+ $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database];
+ foreach($all_links as $tbl => $links) {
+ foreach($links as $col => $totbl_col) {
+ $to = explode(':', $totbl_col);
+ if ($to[0] != $x->tableName()) {
+ continue;
+ }
+
+ $affects[$tbl .'.' . $col] = true;
+ }
+ }
+ // collect tables
+
+ // echo '<PRE>';print_r($affects);exit;
+ //DB_Dataobject::debugLevel(1);
+
+
+ $clean = create_function('$v', 'return (int)$v;');
+
+ $bits = array_map($clean, explode(',', $req['_delete']));
+
+ // print_r($bits);exit;
+ $pk = $x->keys();
+ // let's assume it has a key!!!
+ $pk = $pk[0];
+
+ $x->whereAdd($pk .' IN ('. implode(',', $bits) .')');
+ if (!$x->find()) {
+ $this->jerr("Nothing found to delete");
+ }
+ $errs = array();
+ while ($x->fetch()) {
+ $xx = clone($x);
+
+
+ foreach($affects as $k=> $true) {
+ $ka = explode('.', $k);
+ $chk = DB_DataObject::factory($ka[0]);
+ if (!is_a($chk,'DB_DataObject')) {
+ $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
+ }
+ $chk->{$ka[1]} = $xx->$pk;
+ if ($chk->count()) {
+ $this->jerr('Delete Dependant records first ('. $ka[0]. ':' . $ka[1] .'='.$xx->$pk.')');
+ }
+ }
+
+
+
+ if (method_exists($x, 'checkPerm') && !$x->checkPerm('D', $this->authUser)) {
+ $this->jerr("PERMISSION DENIED");
+ }
+
+ $this->addEvent("DELETE", $x, $x->toEventString());
+ if ( method_exists($xx, 'beforeDelete') && ($xx->beforeDelete() === false)) {
+ $errs[] = "Delete failed ({$xx->id})\n". (isset($xx->err) ? $xx->err : '');
+ continue;
+ }
+ $xx->delete();
+ }
+ if ($errs) {
+ $this->jerr(implode("\n<BR>", $errs));
+ }
+ $this->jok("Deleted");
+
+ }
+
+
var $cols = array();
}
+ $this->cols = array();
+ foreach($xx as $k) {
+ $this->cols[$k] = $do->tableName(). '.' . $k;
+ }
- $this->cols = $xx;
-
} else {
$do->selectAs($xx, $ocl.'_%s', 'join_'.$ocl.'_'. $col);
}
-
-
-
foreach($xx as $k) {
- $this->cols[] = sprintf($ocl.'_%s', $k);
+ $this->cols[sprintf($ocl.'_%s', $k)] = $tab.'.'.$k;
}
}
+ /**
+ * generate the meta data neede by queries.
+ *
+ */
+ function meta($x, $data)
+ {
+ // this is not going to work on queries where the data does not match the database def..
+ // for unknown columns we send them as stirngs..
+ $lost = 0;
+ $cols = array_keys($data[0]);
+
+
+
+
+ //echo '<PRE>';print_r($this->cols); exit;
+ $options = &PEAR::getStaticProperty('DB_DataObject','options');
+ $reader = $options["ini_{$x->_database}"] .'.reader';
+ if (!file_exists( $reader )) {
+ return;
+ }
+
+ $rdata = unserialize(file_get_contents($reader));
+
+ // echo '<PRE>';print_r($rdata);exit;
+
+ $meta = array();
+ foreach($cols as $c ) {
+ if (!isset($this->cols[$c]) || !isset($rdata[$this->cols[$c]]) || !is_array($rdata[$this->cols[$c]])) {
+ $meta[] = $c;
+ continue;
+ }
+ $add = $rdata[$this->cols[$c]];
+ $add['name'] = $c;
+ $meta[] = $add;
+ }
+ return array(
+ 'totalProperty' => 'total',
+ 'successProperty' => 'success',
+ 'root' => 'data',
+ 'id' => 'id',
+ 'fields' => $meta
+ );
+
+
+ }
+
function setFilters($x, $q)
{
// if a column is type int, and we get ',' -> the it should be come an inc clause..
foreach($q as $key=>$val) {
- if (is_array($val)) {
+ if (is_array($val) ) {
+
+ if (!in_array( $key, array_keys($this->cols))) {
+ continue;
+ }
+
+ // support a[0] a[1] ..... => whereAddIn(
+ $ar = array();
+ $quote = false;
+ foreach($val as $k=>$v) {
+ if (!is_numeric($k)) {
+ $ar = array();
+ break;
+ }
+ if (!is_numeric($v) || !is_long($v)) {
+ $quote = true;
+ }
+ $ar[] = $v;
+
+ }
+ if (count($ar)) {
+ $x->whereAddIn($x->tableName(). '.'.$key,$ar, $quote ? 'string' : 'int');
+ }
+
continue;
}
- if ($key[0] == '!' && in_array(substr($key, 1), $this->cols)) {
+
+
+
+ if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
$x->whereAdd( $x->tableName() .'.' .substr($key, 1) . ' != ' .
(is_numeric($val) ? $val : "'". $x->escape($val) . "'")