*
* - applySort($au, $sortcol, $direction, $array_of_columns, $multisort) -- does not support multisort at present..
* - applyFilters($_REQUEST, $authUser, $roo) -- apply any query filters on data. and hide stuff not to be seen. (RETURN false to prevent default filters.)
- * - postListExtra($_REQUEST) : array(extra_name => data) - add extra column data on the results (like new messages etc.)
+ * - postListExtra($_REQUEST, $roo, $array_data) : array(extra_name => data) - add extra column data on the results (like new messages etc.)
* - postListFilter($data, $authUser, $request) return $data - add extra data to an object
*
* - toRooSingleArray($authUser, $request) // single fetch, add data..
* ... call $roo->jerr() on failure...
*
* BEFORE
- * - beforeDelete($dependants_array, $roo) Argument is an array of un-find/fetched dependant items.
+ * - beforeDelete($dependants_array, $roo, $request) Argument is an array of un-find/fetched dependant items.
* - jerr() will stop insert.. (Prefered)
* - return false for fail and set DO->err;
* - beforeUpdate($old, $request,$roo) - after update - jerr() will stop insert..
* - beforeInsert($request,$roo) - before insert - jerr() will stop insert..
*
* AFTER
- * - onUpdate($old, $request,$roo) - after update // return value ignored
- * - onInsert($request,$roo) - after insert
+ * - onUpdate($old, $request,$roo, $event) - after update // return value ignored
+ * - onInsert($request,$roo, $event) - after insert
* - onDelete($req, $roo) - after delete
* - onUpload($roo)
- *
-
- *
+ *
* - toEventString (for logging - this is generically prefixed to all database operations.)
*/
class Pman_Roo extends Pman
{
- /**
+
+ /* EVENTUALLY - move this stuff here..
+ * use Pman_Core_RooTrait,
+ Pman_Core_RooGetTrait,
+ Pman_Core_RooPostTrait,
+ Pman_Core_RooJsonOutputTrait;
+ */
+ /**
* if set to an array (when extending this, then you can restrict which tables are available
*/
var $validTables = false;
var $debugEnabled = true; // disable this for public versions of this code.
+ var $do = false; // the dataobject being worked on..
+
+
+ var $countWhat; // set in loadMap
+ var $colsJname;
+ var $old;
+
function getAuth()
{
parent::getAuth(); // load company!
$au = $this->getAuthUser();
if (!$au) {
- $this->jerr("Not authenticated", array('authFailure' => true));
+ $this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
}
if (!$au->pid() ) { // not set up yet..
- $this->jerr("Not authenticated", array('authFailure' => true));
+ $this->jerror("LOGIN-NOAUTH", "Not authenticated", array('authFailure' => true));
}
return true;
}
/**
- * GET method Roo/TABLENAME.php
+ * GET method Roo/TABLENAME
*
* Generally for SELECT or Single SELECT
*
* lookup[key]=value single fetch based on a single key value lookup.
* multiple key/value can be used. eg. ontable+onid..
* _columns what to return.
+ *
+ * _no_count skip the default count query.
+ * use the number of records in the query result as the total
*
*
* JOINS:
* applyFilters($_REQUEST, $authUser, $roo)
* -- apply any query filters on data. and hide stuff not to be seen.
* -- can exit by calling $roo->jerr()
- * postListExtra($_REQUEST) : array(extra_name => data)
- * - add extra column data on the results (like new messages etc.)
+ * postListExtra($_REQUEST, $roo, $array_data) : array(extra_name => data)
+ * - add extra column to the jdata return key/value info.
* postListFilter($data, $authUser, $request) return $data
- * - add extra data to an object
+ * - modify the returned array of data (either add stuff to the data, or remove lines etc)
*
*
* toRooSingleArray($authUser, $request) : array
* toRooArray($request) : array
* - called if singleArray is unavailable on single fetch.
* - always tried for mutiple results.
- * toArray() - the default method if none of the others are found.
+ * toArray() - the default method if none of the others are found.
+ * toSimpleExcelColumn($col) - see toCSV - this enables special columns to be created for exported data.
*
* autoJoin($request)
* - standard DataObject feature - causes all results to show all
$this->init(); // from pman.
//DB_DataObject::debuglevel(1);
HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev && !empty($_REQUEST['isDev']));
-
-
-
+
// debugging...
-
-
-
+
if ( $this->checkDebugPost()) {
$tab = array_shift($tt);
$x = $this->dataObject($tab);
- $_columns = !empty($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
+ $_columns = !empty($_REQUEST['_columns']) && is_string($_REQUEST['_columns']) ? explode(',', $_REQUEST['_columns']) : false;
+
if (isset( $_REQUEST['lookup'] ) && is_array($_REQUEST['lookup'] )) { // single fetch based on key/value pairs
$this->selectSingle($x, $_REQUEST['lookup'],$_REQUEST);
// sets map and countWhat
$this->loadMap($x, array(
- 'columns' => $_columns,
- 'distinct' => empty($_REQUEST['_distinct']) ? false: $_REQUEST['_distinct'],
- 'exclude' => empty($_REQUEST['_exclude_columns']) ? false: explode(',', $_REQUEST['_exclude_columns'])
- ));
+ 'columns' => $_columns,
+ 'distinct' => empty($_REQUEST['_distinct']) ? false: $_REQUEST['_distinct'],
+ 'exclude' => empty($_REQUEST['_exclude_columns']) ? false: explode(',', $_REQUEST['_exclude_columns'])
+ ));
$this->setFilters($x,$_REQUEST);
if (!$this->checkPerm($x,'S', $_REQUEST)) {
- $this->jerr("PERMISSION DENIED (g)");
+ $this->jerror("NOTICE-NOPERM", "PERMISSION DENIED (g)");
}
//print_r($x);
// build join if req.
//DB_DataObject::debugLevel(1);
- // var_dump($this->countWhat);
- $total = $x->count($this->countWhat);
+ // count with multiple joins and no conditions can be quite slow - so if there are no conditions - just remove the joins from the count.
+ $xx = clone($x);
+ $old_where = $x->whereAdd();
+ if (empty($old_where )) {
+ $xx->_join = '';
+ } else {
+ $x->whereAdd($old_where);
+ $xx=clone($x);
+
+ }
+ $total = false;
+ if (!isset($_REQUEST['_no_count'])) {
+ $total = $xx->count($this->countWhat);
+ }
+ if (isset($xx->_real_total)) { // this is used when we subquery the search.
+ $total = $xx->_real_total;
+ }
// sorting..
//
- //var_dump($total);exit;
+ // var_dump($total);exit;
$this->applySort($x);
$fake_limit = false;
- if (!empty($_REQUEST['_distinct']) && $total < 400) {
+ if (!empty($_REQUEST['_distinct']) && $total !== false && $total < 400) {
$fake_limit = true;
}
$this->sessionState(0);
$res = $x->find();
$this->sessionState(1);
-
+
if (false === $res) {
$this->jerr($x->_lastError->toString());
if (!empty($_REQUEST['query']['add_blank'])) {
$ret[] = array( 'id' => 0, 'name' => '----');
- $total+=1;
+ $total === false ? false : $total+1;
}
$rooar = method_exists($x, 'toRooArray');
$extra = false;
if (method_exists($queryObj ,'postListExtra')) {
- $extra = $queryObj->postListExtra($_REQUEST, $this);
+ $extra = $queryObj->postListExtra($_REQUEST, $this, $ret);
}
// filter results, and add any data that is needed...
if (method_exists($x,'postListFilter')) {
- $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST);
+ $ret = $x->postListFilter($ret, $this->authUser, $_REQUEST, $this);
}
$this->toCsv($ret, $_REQUEST['csvCols'], $_REQUEST['csvTitles'],
- empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename']
- );
+ empty($_REQUEST['csvFilename']) ? '' : $_REQUEST['csvFilename'] );
}
- //die("DONE?");
-
- //if ($x->tableName() == 'Documents_Tracking') {
- // $ret = $this->replaceSubject(&$ret, 'doc_id_subject');
- // }
-
if (!empty($_REQUEST['_requestMeta']) && count($ret)) {
$meta = $this->meta($x, $ret);
if ($meta) {
+ $extra = $extra ? $extra: array();
$extra['metaData'] = $meta;
}
}
// this make take some time...
$this->sessionState(0);
// echo "<PRE>"; print_r($ret);
- $this->jdata($ret, max(count($ret), $total), $extra );
+ $this->jdata($ret, max(count($ret), $total === false ? 0 : $total), $extra );
}
function toCsv($data, $cols, $titles, $filename, $addDate = true)
{
-
+ // die('here');
$this->sessionState(0); // turn off sessions - no locking..
require_once 'Pman/Core/SimpleExcel.php';
$rooar = method_exists($data, 'toRooArray');
while($data->fetch()) {
$x = $rooar ? $data->toRooArray($q) : $data->toArray();
-
-
+
if ($cols == '*') { /// did we get cols sent to us?
$cols = array_keys($x);
}
+
+ if(!is_array($cols)) {
+ $cols = explode(',', $cols);
+ }
if ($titles !== false) {
if ($titles== '*') {
$titles= array_keys($x);
}
+ if(!is_array($titles)) {
+ $titles = explode(',', $titles);
+ }
foreach($cols as $i=>$col) {
- $se_config['cols'][] = array(
+ $add = array(
'header'=> isset($titles[$i]) ? $titles[$i] : $col,
'dataIndex'=> $col,
'width'=> 100,
// 'color' => 'yellow', // set color for the cell which is a header element
// 'fillBlank' => 'gray', // set
);
- $se = new Pman_Core_SimpleExcel(array(), $se_config);
+ //die('here');
+ if (method_exists($data, 'toSimpleExcelColumn')) {
+ $add = $data->toSimpleExcelColumn($add);
+ }
+
+ $se_config['cols'][] = $add;
+
}
+ $se = new Pman_Core_SimpleExcel(array(), $se_config);
$titles = false;
}
}
-
- foreach($data as $x) {
+ foreach($data as $x) {
//echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
$line = array();
- if ($titles== '*') {
- $titles= array_keys($x);
- }
+
if ($cols== '*') {
$cols= array_keys($x);
}
+
+ if(!is_array($cols)) {
+ $cols = explode(',', $cols);
+ }
+
if ($titles !== false) {
+ if ($titles== '*') {
+ $titles= array_keys($x);
+ }
+
+ if(!is_array($titles)) {
+ $titles = explode(',', $titles);
+ }
+
foreach($cols as $i=>$col) {
- $se_config['cols'][] = array(
+ $add = array(
'header'=> isset($titles[$i]) ? $titles[$i] : $col,
'dataIndex'=> $col,
'width'=> 100,
// 'color' => 'yellow', // set color for the cell which is a header element
// 'fillBlank' => 'gray', // set
);
- $se = new Pman_Core_SimpleExcel(array(),$se_config);
+ //die('here');
+ if (!empty($this->do) && method_exists($this->do, 'toSimpleExcelColumn')) {
+ $add = $this->do->toSimpleExcelColumn($add);
+ }
+
+ $se_config['cols'][] = $add;
+
}
-
+ $se = new Pman_Core_SimpleExcel(array(),$se_config);
+
//fputcsv($fh, $titles);
$titles = false;
}
-
-
$se->addLine($se_config['workbook'], $x);
}
+
if(!$se){
$this->jerr('no data found');
}
+
$se->send($fn .'.xls');
exit;
-
-
}
* - return false for fail and set DO->err;
*
* beforeUpdate($old, $request,$roo)
- * - after update - jerr() will stop insert..
+ * - before update - jerr() will stop insert..
* beforeInsert($request,$roo)
* - before insert - jerr() will stop insert..
*
}
+
+
$old = false;
// not sure if this is a good idea here...
-
+
if (!empty($_REQUEST['_ids'])) {
$ids = explode(',',$_REQUEST['_ids']);
$x->whereAddIn($this->key, $ids, 'int');
{
// Db_DataObject::debugLevel(1);
- $sort = empty($_REQUEST['sort']) ? $sort : $_REQUEST['sort'];
- $dir = empty($_REQUEST['dir']) ? $dir : $_REQUEST['dir'];
+ $sort = empty($_REQUEST['sort']) || !is_string($_REQUEST['sort']) ? $sort : $_REQUEST['sort'];
+ $dir = empty($_REQUEST['dir']) || !is_string($_REQUEST['dir']) ? $dir : $_REQUEST['dir'];
$dir = $dir == 'ASC' ? 'ASC' : 'DESC';
- $ms = empty($_REQUEST['_multisort']) ? false : $_REQUEST['_multisort'];
+ $ms = empty($_REQUEST['_multisort']) || !is_string($_REQUEST['_multisort']) ? false : $_REQUEST['_multisort'];
//var_Dump($ms);exit;
$sorted = false;
if (method_exists($x, 'applySort')) {
$ms ? json_decode($ms) : false
);
}
+
if ($ms !== false) {
return $this->multiSort($x);
}
-
+
if ($sorted === false) {
-
$cols = $x->tableColumns();
$excols = array_keys($this->cols);
} else if (method_exists($x, 'applyFilters')) {
// always call apply filters even after update/insert...
// however arguments are not passed.
- $x->applyFilters(array(), $this->authUser, $this);
+ $x->applyFilters(array('_is_update_request' => true), $this->authUser, $this);
}
// DB_DataObject::DebugLevel(1);
if (method_exists($x, 'toRooArray')) {
$this->jok($x->toRooArray($req));
}
-
$this->jok($x->toArray());
function insert($x, $req, $with_perm_check = true)
{
- if (method_exists($x, 'setFromRoo')) {
+ if (method_exists($x, 'setFromRoo')) {
$res = $x->setFromRoo($req, $this);
if (is_string($res)) {
- $this->jerr($res);
+ $this->jerror("NOTICE-INSERT", $res);
}
} else {
$x->setFrom($req);
}
-
+
if ( $with_perm_check && !$this->checkPerm($x,'A', $req)) {
- $this->jerr("PERMISSION DENIED (i)");
+ $this->jerr("PERMISSION DENIED {$x->tableName()}:checkPerm(A)");
}
$cols = $x->tableColumns();
if ($res === false) {
$this->jerr($x->_lastError->toString());
}
+ $ev = $this->addEvent("ADD", $x);
if (method_exists($x, 'onInsert')) {
- $x->onInsert($_REQUEST, $this);
+
+ $x->onInsert($_REQUEST, $this, $ev);
}
- $ev = $this->addEvent("ADD", $x);
+
if ($ev) {
$ev->audit($x);
}
-
+
// note setFrom might handle this before hand...!??!
if (!empty($_FILES) && method_exists($x, 'onUpload')) {
$x->onUpload($this, $_REQUEST);
if ($res === false) {
$this->jerr($x->_lastError->toString());
}
-
+ $ev = $this->addEvent("EDIT", $x);
+
if (method_exists($x, 'onUpdate')) {
- $x->onUpdate($old, $req, $this);
+ $x->onUpdate($old, $req, $this, $ev);
}
- $ev = $this->addEvent("EDIT", $x);
if ($ev) {
$ev->audit($x, $old);
}
*/
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();
$x->whereAdd($this->key .' IN ('. implode(',', $bits) .')');
if (!$x->find()) {
- $this->jerr("Nothing found to delete");
+ $this->jerror("NOTICE-DELETE","Nothing found to delete");
}
$errs = array();
while ($x->fetch()) {
}
}
+
$has_beforeDelete = method_exists($xx, 'beforeDelete');
// before delte = allows us to trash dependancies if needed..
$match_total = 0;
if ( $has_beforeDelete ) {
- if ($xx->beforeDelete($match_ar, $this) === false) {
+ if ($xx->beforeDelete($match_ar, $this, $_REQUEST) === false) {
$errs[] = "Delete failed ({$xx->id})\n".
(isset($xx->err) ? $xx->err : '');
continue;
if (method_exists($chk, 'toEventString')) {
$desc .= ' : ' . $o[0]->toEventString();
}
- $this->jerr("Delete Dependant records ($match_total found), " .
+ $this->jerror("NOTICE-DELETE-DEP", "Delete Dependant records ($match_total found), " .
"first is ( $desc )");
}
function loadMap($do, $cfg =array()) //$onlycolumns=false, $distinct = false)
{
-
- //DB_DataObject::debugLevel(5);
+ //DB_DataObject::debugLevel(5);
$onlycolumns = !empty($cfg['columns']) ? $cfg['columns'] : false;
$distinct = !empty($cfg['distinct']) ? $cfg['distinct'] : false;
$excludecolumns = !empty($cfg['exclude']) ? $cfg['exclude'] : array();
$q_filtered = array();
$keys = $x->keys();
- // var_dump($keys);exit;
+
foreach($q as $key=>$val) {
if (in_array($key,$keys) && !is_array($val)) {
}
-
+
switch($key) {
$x->whereAdd("(on_id = $oid OR
on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o )
)");
- continue;
+ continue 2;
}
$x->on_id = $val;
// the aobve rule for !strlen non-joined cols should apply to joined ones.
if (!strlen($val)) {
- continue;
+ continue 2;
}
}
- continue;
+ continue 2;
}
}
if (!empty($q_filtered)) {
}
$tab = str_replace('/', '',$tab); // basic protection??
- $pm = HTML_FlexyFramework::get()->Pman;
+ $ff = HTML_FlexyFramework::get();
+ $pm = isset($ff->Pman) ? $ff->Pman : array();
if (isset($pm['roo_alias'])) {
$map = array_flip($pm['roo_alias']);
if (!is_a($x, 'DB_DataObject') && !is_a($x, 'PDO_DataObject')) {
$this->jerr('invalid url - no dataobject');
}
-
+ $this->do = $x;
return $x;
}