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);
193 $clean = create_function('$v', 'return (int)$v;');
195 $bits = array_map($clean, explode(',', $req['_delete']));
197 // print_r($bits);exit;
199 // let's assume it has a key!!!
202 $x->whereAdd($this->key .' IN ('. implode(',', $bits) .')');
204 $this->jerr("Nothing found to delete");
207 while ($x->fetch()) {
213 if (!$this->checkPerm($x,'D') ) {
214 $this->jerr("PERMISSION DENIED (d)");
218 foreach($affects as $k=> $true) {
219 $ka = explode('.', $k);
221 $chk = DB_DataObject::factory($ka[0]);
222 if (!is_a($chk,'DB_DataObject')) {
223 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
225 // print_r(array($chk->tablename() , $ka[1] , $xx->tablename() , $this->key ));
226 $chk->{$ka[1]} = $xx->{$this->key};
228 if (count($chk->keys())) {
229 $matches = $chk->count();
231 //DB_DataObject::DebugLevel(1);
232 $matches = $chk->count($ka[1]);
236 $chk->_match_key = $ka[1];
237 $match_ar[] = clone($chk);
242 $has_beforeDelete = method_exists($xx, 'beforeDelete');
243 // before delte = allows us to trash dependancies if needed..
246 if ( $has_beforeDelete ) {
247 if ($xx->beforeDelete($match_ar, $this, $req) === false) {
248 $errs[] = "Delete failed ({$xx->id})\n".
249 (isset($xx->err) ? $xx->err : '');
255 foreach($affects as $k=> $true) {
256 $ka = explode('.', $k);
257 $chk = DB_DataObject::factory($ka[0]);
258 if (!is_a($chk,'DB_DataObject')) {
259 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
261 $chk->{$ka[1]} = $xx->{$this->key};
262 $matches = $chk->count();
263 $match_total += $matches;
265 $chk->_match_key = $ka[1];
266 $match_ar[] = clone($chk);
273 if (!empty($match_ar)) {
276 $o = $chk->fetchAll();
277 $key = isset($chk->_match_key) ?$chk->_match_key : '?unknown column?';
278 $desc = $chk->tableName(). '.' . $key .'='.$xx->{$this->key} ;
279 if (method_exists($chk, 'toEventString')) {
280 $desc .= ' : ' . $o[0]->toEventString();
283 $this->jerr("Delete Dependant records ($match_total found), " .
284 "first is ( $desc )");
288 $this->logDeleteEvent($x);
292 if (method_exists($xx,'onDelete')) {
293 $xx->onDelete($req, $this);
299 $this->jerr(implode("\n<BR>", $errs));
301 $this->jok("Deleted");
305 function logDeleteEvent($object)
308 DB_DataObject::Factory('Events')->logDeletedRecord($object);
310 $this->addEvent("DELETE", $object);
316 function update($x, $req, $with_perm_check = true)
318 if ( $with_perm_check && !$this->checkPerm($x,'E', $req) ) {
319 $this->jerr("PERMISSION DENIED - No Edit permissions on this element");
323 // only done if we recieve a lock_id.
324 // we are very trusing here.. that someone has not messed around with locks..
325 // the object might want to check in their checkPerm - if locking is essential..
326 $lock = $this->updateLock($x,$req);
330 // this lot is generic.. needs moving
331 if (method_exists($x, 'setFromRoo')) {
332 $res = $x->setFromRoo($req, $this);
333 if (is_string($res)) {
342 //echo '<PRE>';print_r($old);print_r($x);exit;
347 if (isset($cols['modified'])) {
348 $x->modified = date('Y-m-d H:i:s');
350 if (isset($cols['modified_dt'])) {
351 $x->modified_dt = date('Y-m-d H:i:s');
353 if (isset($cols['modified_by']) && $this->authUser) {
354 $x->modified_by = $this->authUser->id;
357 if (isset($cols['updated'])) {
358 $x->updated = date('Y-m-d H:i:s');
360 if (isset($cols['updated_dt'])) {
361 $x->updated_dt = date('Y-m-d H:i:s');
363 if (isset($cols['updated_by']) && $this->authUser) {
364 $x->updated_by = $this->authUser->id;
367 if (method_exists($x, 'beforeUpdate')) {
368 $x->beforeUpdate($old, $req, $this);
371 if ($with_perm_check && !empty($_FILES) && method_exists($x, 'onUpload')) {
372 $x->onUpload($this, $_REQUEST);
375 //DB_DataObject::DebugLevel(1);
376 $res = $x->update($old);
377 if ($res === false) {
378 $this->jerr($x->_lastError->toString());
381 if (method_exists($x, 'onUpdate')) {
382 $x->onUpdate($old, $req, $this);
384 $ev = $this->addEvent("EDIT", $x);
386 $ev->audit($x, $old);
390 return $this->selectSingle(
391 DB_DataObject::factory($x->tableName()),
397 function updateLock($x, $req )
399 $this->permitError = true; // allow it to fail without dieing
401 $lock = DB_DataObjecT::factory('core_locking');
402 $this->permitError = false;
403 if (is_a($lock,'DB_DataObject') && $this->authUser) {
405 $lock->on_id = $x->{$this->key};
406 $lock->on_table= strtolower($x->tableName());
407 if (!empty($_REQUEST['_lock_id'])) {
408 $lock->whereAdd('id != ' . ((int)$_REQUEST['_lock_id']));
410 $lock->whereAdd('person_id !=' . $this->authUser->id);
414 $exp = date('Y-m-d', strtotime('NOW - 1 WEEK'));
415 $llc->whereAdd("created < '$exp'");
418 while($llc->fetch()) {
425 if ($lock->find(true)) {
426 // it's locked by someone else..
427 $p = $lock->person();
430 $this->jerr( "Record was locked by " . $p->name . " at " .$lock->created.
431 " - Please confirm you wish to save"
432 , array('needs_confirm' => true));
436 // check the users lock.. - no point.. ??? - if there are no other locks and it's not the users, then they can
437 // edit it anyways...
439 // can we find the user's lock.
440 $lock = DB_DataObjecT::factory('core_locking');
441 $lock->on_id = $x->{$this->key};
442 $lock->on_table= strtolower($x->tableName());
443 $lock->person_id = $this->authUser->id;
444 $lock->orderBy('created DESC');
449 isset($x->modified_dt) &&
450 strtotime($x->modified_dt) > strtotime($lock->created) &&
451 empty($req['_submit_confirmed']) &&
452 $x->modified_by != $this->authUser->id
455 $p = DB_DataObject::factory('core_person');
456 $p->get($x->modified_by);
457 $this->jerr($p->name . " saved the record since you started editing,\nDo you really want to update it?", array('needs_confirm' => true));
466 function insert($x, $req, $with_perm_check = true)
468 if (method_exists($x, 'setFromRoo')) {
469 $res = $x->setFromRoo($req, $this);
470 if (is_string($res)) {
477 if ( $with_perm_check && !$this->checkPerm($x,'A', $req)) {
478 $this->jerr("PERMISSION DENIED (i)");
482 if (isset($cols['created'])) {
483 $x->created = date('Y-m-d H:i:s');
485 if (isset($cols['created_dt'])) {
486 $x->created_dt = date('Y-m-d H:i:s');
488 if (isset($cols['created_by'])) {
489 $x->created_by = $this->authUser->id;
493 if (isset($cols['modified'])) {
494 $x->modified = date('Y-m-d H:i:s');
496 if (isset($cols['modified_dt'])) {
497 $x->modified_dt = date('Y-m-d H:i:s');
499 if (isset($cols['modified_by'])) {
500 $x->modified_by = $this->authUser->id;
503 if (isset($cols['updated'])) {
504 $x->updated = date('Y-m-d H:i:s');
506 if (isset($cols['updated_dt'])) {
507 $x->updated_dt = date('Y-m-d H:i:s');
509 if (isset($cols['updated_by'])) {
510 $x->updated_by = $this->authUser->id;
513 if (method_exists($x, 'beforeInsert')) {
514 $x->beforeInsert($_REQUEST, $this);
518 if ($res === false) {
519 $this->jerr($x->_lastError->toString());
521 if (method_exists($x, 'onInsert')) {
522 $x->onInsert($_REQUEST, $this);
524 $ev = $this->addEvent("ADD", $x);
529 // note setFrom might handle this before hand...!??!
530 if (!empty($_FILES) && method_exists($x, 'onUpload')) {
531 $x->onUpload($this, $_REQUEST);
534 return $this->selectSingle(
535 DB_DataObject::factory($x->tableName()),