3 trait Pman_Core_RooPostTrait {
8 * POST method Roo/TABLENAME
9 * -- creates, updates, or deletes data.
12 * if the primary key is empty, this happens
13 * will automatically set these to current date and authUser->id
14 * created, created_by, created_dt
15 * updated, update_by, updated_dt
16 * modified, modified_by, modified_dt
18 * will return a GET request SINGLE SELECT (and accepts same)
21 * _delete=1,2,3 delete a set of data.
23 * if the primary key value is set, then update occurs.
24 * will automatically set these to current date and authUser->id
25 * updated, update_by, updated_dt
26 * modified, modified_by, modified_dt
30 * _delete=1,2,3 causes a delete to occur.
31 * _ids=1,2,3,4 causes update to occur on all primary ids.
34 * = same as single SELECT GET request..
39 * _debug=1 forces debug
40 * _get=1 - causes a get request to occur when doing a POST..
44 * these methods on dataobjects if they exist
46 * checkPerm('E' / 'D' , $authuser)
47 * - can we list the stuff
48 * - return false to disallow...
50 * toRooSingleArray($authUser, $request) : array
51 * - called on single fetch only, add or maniuplate returned array data.
52 * toRooArray($request) : array
53 * - Called if toSingleArray does not exist.
54 * - if you need to return different data than toArray..
57 * (for logging - this is generically prefixed to all database operations.)
61 * called when $_FILES is not empty
64 * setFromRoo($ar, $roo)
65 * - alternative to setFrom() which is called if this method does not exist
66 * - values from post (deal with dates etc.) - return true|error string.
67 * - call $roo->jerr() on failure...
69 * CALLS BEFORE change occurs:
71 * beforeDelete($dependants_array, $roo, $request)
72 * Argument is an array of un-find/fetched dependant items.
73 * - jerr() will stop insert.. (Prefered)
74 * - return false for fail and set DO->err;
76 * beforeUpdate($old, $request,$roo)
77 * - after update - jerr() will stop insert..
78 * beforeInsert($request,$roo)
79 * - before insert - jerr() will stop insert..
82 * CALLS AFTER change occured
84 * onUpdate($old, $request,$roo)
85 * - after update // return value ignored
87 * onInsert($request,$roo)
90 * onDelete($request, $roo) - after delete
94 function post($tab) // update / insert (?? delete??)
96 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
100 if (!empty($_REQUEST['_get'])) {
101 return $this->get($tab);
106 $x = $this->dataObject($tab);
108 $this->transObj = clone($x);
110 $this->transObj->query('BEGIN');
111 // find the key and use that to get the thing..
114 $this->jerr('no key');
117 $this->key = $keys[0];
119 // delete should be here...
120 if (isset($_REQUEST['_delete'])) {
121 // do we really delete stuff!?!?!?
122 return $this->delete($x,$_REQUEST);
127 // not sure if this is a good idea here...
129 if (!empty($_REQUEST['_ids'])) {
130 $ids = explode(',',$_REQUEST['_ids']);
131 $x->whereAddIn($this->key, $ids, 'int');
132 $ar = $x->fetchAll();
135 $this->update($x, $_REQUEST);
139 $this->jok("UPDATED");
144 if (!empty($_REQUEST[$this->key])) {
146 if (!$x->get($this->key, $_REQUEST[$this->key])) {
147 $this->jerr("Invalid request");
149 $this->jok($this->update($x, $_REQUEST));
153 $this->jerr("No data recieved for inserting");
156 $this->jok($this->insert($x, $_REQUEST));
164 function delete($x, $req)
166 // do we really delete stuff!?!?!?
167 if (empty($req['_delete'])) {
168 $this->jerr("Delete Requested with no value");
170 // build a list of tables to queriy for dependant data..
175 $all_links = $x->databaseLinks();
177 foreach($all_links as $tbl => $links) {
178 foreach($links as $col => $totbl_col) {
179 $to = explode(':', $totbl_col);
180 if ($to[0] != $x->tableName()) {
184 $affects[$tbl .'.' . $col] = true;
189 // echo '<PRE>';print_r($affects);exit;
190 // DB_Dataobject::debugLevel(1);
192 if (function_exists('create_function')) {
193 $clean = create_function('$v', 'return (int)$v;');
195 $clean = function($v) {
200 $bits = array_map($clean, explode(',', $req['_delete']));
202 // print_r($bits);exit;
204 // let's assume it has a key!!!
207 $x->whereAdd($this->key .' IN ('. implode(',', $bits) .')');
209 $this->jerr("Nothing found to delete");
212 while ($x->fetch()) {
218 if (!$this->checkPerm($x,'D') ) {
219 $this->jerr("PERMISSION DENIED (d)");
223 foreach($affects as $k=> $true) {
224 $ka = explode('.', $k);
226 $chk = DB_DataObject::factory($ka[0]);
227 if (!is_a($chk,'DB_DataObject')) {
228 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
230 // print_r(array($chk->tablename() , $ka[1] , $xx->tablename() , $this->key ));
231 $chk->{$ka[1]} = $xx->{$this->key};
233 if (count($chk->keys())) {
234 $matches = $chk->count();
236 //DB_DataObject::DebugLevel(1);
237 $matches = $chk->count($ka[1]);
241 $chk->_match_key = $ka[1];
242 $match_ar[] = clone($chk);
247 $has_beforeDelete = method_exists($xx, 'beforeDelete');
248 // before delte = allows us to trash dependancies if needed..
251 if ( $has_beforeDelete ) {
252 if ($xx->beforeDelete($match_ar, $this, $req) === false) {
253 $errs[] = "Delete failed ({$xx->id})\n".
254 (isset($xx->err) ? $xx->err : '');
260 foreach($affects as $k=> $true) {
261 $ka = explode('.', $k);
262 $chk = DB_DataObject::factory($ka[0]);
263 if (!is_a($chk,'DB_DataObject')) {
264 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
266 $chk->{$ka[1]} = $xx->{$this->key};
267 $matches = $chk->count();
268 $match_total += $matches;
270 $chk->_match_key = $ka[1];
271 $match_ar[] = clone($chk);
278 if (!empty($match_ar)) {
281 $o = $chk->fetchAll();
282 $key = isset($chk->_match_key) ?$chk->_match_key : '?unknown column?';
283 $desc = $chk->tableName(). '.' . $key .'='.$xx->{$this->key} ;
284 if (method_exists($chk, 'toEventString')) {
285 $desc .= ' : ' . $o[0]->toEventString();
288 $this->jerr("Delete Dependant records ($match_total found), " .
289 "first is ( $desc )");
293 $this->logDeleteEvent($x);
297 if (method_exists($xx,'onDelete')) {
298 $xx->onDelete($req, $this);
304 $this->jerr(implode("\n<BR>", $errs));
306 $this->jok("Deleted");
310 function logDeleteEvent($object)
313 DB_DataObject::Factory('Events')->logDeletedRecord($object);
315 $this->addEvent("DELETE", $object);
321 function update($x, $req, $with_perm_check = true)
323 if ( $with_perm_check && !$this->checkPerm($x,'E', $req) ) {
324 $this->jerr("PERMISSION DENIED - No Edit permissions on this element");
328 // only done if we recieve a lock_id.
329 // we are very trusing here.. that someone has not messed around with locks..
330 // the object might want to check in their checkPerm - if locking is essential..
331 $lock = $this->updateLock($x,$req);
335 // this lot is generic.. needs moving
336 if (method_exists($x, 'setFromRoo')) {
337 $res = $x->setFromRoo($req, $this);
338 if (is_string($res)) {
347 //echo '<PRE>';print_r($old);print_r($x);exit;
352 if (isset($cols['modified'])) {
353 $x->modified = date('Y-m-d H:i:s');
355 if (isset($cols['modified_dt'])) {
356 $x->modified_dt = date('Y-m-d H:i:s');
358 if (isset($cols['modified_by']) && $this->authUser) {
359 $x->modified_by = $this->authUser->id;
362 if (isset($cols['updated'])) {
363 $x->updated = date('Y-m-d H:i:s');
365 if (isset($cols['updated_dt'])) {
366 $x->updated_dt = date('Y-m-d H:i:s');
368 if (isset($cols['updated_by']) && $this->authUser) {
369 $x->updated_by = $this->authUser->id;
372 if (method_exists($x, 'beforeUpdate')) {
373 $x->beforeUpdate($old, $req, $this);
376 if ($with_perm_check && !empty($_FILES) && method_exists($x, 'onUpload')) {
377 $x->onUpload($this, $_REQUEST);
380 //DB_DataObject::DebugLevel(1);
381 $res = $x->update($old);
382 if ($res === false) {
383 $this->jerr($x->_lastError->toString());
386 if (method_exists($x, 'onUpdate')) {
387 $x->onUpdate($old, $req, $this);
389 $ev = $this->addEvent("EDIT", $x);
391 $ev->audit($x, $old);
395 return $this->selectSingle(
396 DB_DataObject::factory($x->tableName()),
402 function updateLock($x, $req )
404 $this->permitError = true; // allow it to fail without dieing
406 $lock = DB_DataObjecT::factory('core_locking');
407 $this->permitError = false;
408 if (is_a($lock,'DB_DataObject') && $this->authUser) {
410 $lock->on_id = $x->{$this->key};
411 $lock->on_table= strtolower($x->tableName());
412 if (!empty($_REQUEST['_lock_id'])) {
413 $lock->whereAdd('id != ' . ((int)$_REQUEST['_lock_id']));
415 $lock->whereAdd('person_id !=' . $this->authUser->id);
419 $exp = date('Y-m-d', strtotime('NOW - 1 WEEK'));
420 $llc->whereAdd("created < '$exp'");
423 while($llc->fetch()) {
430 if ($lock->find(true)) {
431 // it's locked by someone else..
432 $p = $lock->person();
435 $this->jerr( "Record was locked by " . $p->name . " at " .$lock->created.
436 " - Please confirm you wish to save"
437 , array('needs_confirm' => true));
441 // check the users lock.. - no point.. ??? - if there are no other locks and it's not the users, then they can
442 // edit it anyways...
444 // can we find the user's lock.
445 $lock = DB_DataObjecT::factory('core_locking');
446 $lock->on_id = $x->{$this->key};
447 $lock->on_table= strtolower($x->tableName());
448 $lock->person_id = $this->authUser->id;
449 $lock->orderBy('created DESC');
454 isset($x->modified_dt) &&
455 strtotime($x->modified_dt) > strtotime($lock->created) &&
456 empty($req['_submit_confirmed']) &&
457 $x->modified_by != $this->authUser->id
460 $p = DB_DataObject::factory('core_person');
461 $p->get($x->modified_by);
462 $this->jerr($p->name . " saved the record since you started editing,\nDo you really want to update it?", array('needs_confirm' => true));
471 function insert($x, $req, $with_perm_check = true)
473 if (method_exists($x, 'setFromRoo')) {
474 $res = $x->setFromRoo($req, $this);
475 if (is_string($res)) {
482 if ( $with_perm_check && !$this->checkPerm($x,'A', $req)) {
483 $this->jerr("PERMISSION DENIED (i)");
487 if (isset($cols['created'])) {
488 $x->created = date('Y-m-d H:i:s');
490 if (isset($cols['created_dt'])) {
491 $x->created_dt = date('Y-m-d H:i:s');
493 if (isset($cols['created_by'])) {
494 $x->created_by = $this->authUser->id;
498 if (isset($cols['modified'])) {
499 $x->modified = date('Y-m-d H:i:s');
501 if (isset($cols['modified_dt'])) {
502 $x->modified_dt = date('Y-m-d H:i:s');
504 if (isset($cols['modified_by'])) {
505 $x->modified_by = $this->authUser->id;
508 if (isset($cols['updated'])) {
509 $x->updated = date('Y-m-d H:i:s');
511 if (isset($cols['updated_dt'])) {
512 $x->updated_dt = date('Y-m-d H:i:s');
514 if (isset($cols['updated_by'])) {
515 $x->updated_by = $this->authUser->id;
518 if (method_exists($x, 'beforeInsert')) {
519 $x->beforeInsert($_REQUEST, $this);
523 if ($res === false) {
524 $this->jerr($x->_lastError->toString());
526 if (method_exists($x, 'onInsert')) {
527 $x->onInsert($_REQUEST, $this);
529 $ev = $this->addEvent("ADD", $x);
534 // note setFrom might handle this before hand...!??!
535 if (!empty($_FILES) && method_exists($x, 'onUpload')) {
536 $x->onUpload($this, $_REQUEST);
539 return $this->selectSingle(
540 DB_DataObject::factory($x->tableName()),