3 trait Pman_Core_RooPostTrait {
6 * POST method Roo/TABLENAME
7 * -- creates, updates, or deletes data.
10 * if the primary key is empty, this happens
11 * will automatically set these to current date and authUser->id
12 * created, created_by, created_dt
13 * updated, update_by, updated_dt
14 * modified, modified_by, modified_dt
16 * will return a GET request SINGLE SELECT (and accepts same)
19 * _delete=1,2,3 delete a set of data.
21 * if the primary key value is set, then update occurs.
22 * will automatically set these to current date and authUser->id
23 * updated, update_by, updated_dt
24 * modified, modified_by, modified_dt
28 * _delete=1,2,3 causes a delete to occur.
29 * _ids=1,2,3,4 causes update to occur on all primary ids.
32 * = same as single SELECT GET request..
37 * _debug=1 forces debug
38 * _get=1 - causes a get request to occur when doing a POST..
42 * these methods on dataobjects if they exist
44 * checkPerm('E' / 'D' , $authuser)
45 * - can we list the stuff
46 * - return false to disallow...
48 * toRooSingleArray($authUser, $request) : array
49 * - called on single fetch only, add or maniuplate returned array data.
50 * toRooArray($request) : array
51 * - Called if toSingleArray does not exist.
52 * - if you need to return different data than toArray..
55 * (for logging - this is generically prefixed to all database operations.)
59 * called when $_FILES is not empty
62 * setFromRoo($ar, $roo)
63 * - alternative to setFrom() which is called if this method does not exist
64 * - values from post (deal with dates etc.) - return true|error string.
65 * - call $roo->jerr() on failure...
67 * CALLS BEFORE change occurs:
69 * beforeDelete($dependants_array, $roo)
70 * Argument is an array of un-find/fetched dependant items.
71 * - jerr() will stop insert.. (Prefered)
72 * - return false for fail and set DO->err;
74 * beforeUpdate($old, $request,$roo)
75 * - after update - jerr() will stop insert..
76 * beforeInsert($request,$roo)
77 * - before insert - jerr() will stop insert..
80 * CALLS AFTER change occured
82 * onUpdate($old, $request,$roo)
83 * - after update // return value ignored
85 * onInsert($request,$roo)
88 * onDelete($request, $roo) - after delete
92 function post($tab) // update / insert (?? delete??)
94 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
98 if (!empty($_REQUEST['_get'])) {
99 return $this->get($tab);
104 $x = $this->dataObject($tab);
106 $this->transObj = clone($x);
108 $this->transObj->query('BEGIN');
109 // find the key and use that to get the thing..
112 $this->jerr('no key');
115 $this->key = $keys[0];
117 // delete should be here...
118 if (isset($_REQUEST['_delete'])) {
119 // do we really delete stuff!?!?!?
120 return $this->delete($x,$_REQUEST);
125 // not sure if this is a good idea here...
127 if (!empty($_REQUEST['_ids'])) {
128 $ids = explode(',',$_REQUEST['_ids']);
129 $x->whereAddIn($this->key, $ids, 'int');
130 $ar = $x->fetchAll();
133 $this->update($x, $_REQUEST);
137 $this->jok("UPDATED");
141 print_r($_POST);exit;
142 if (!empty($_REQUEST[$this->key])) {
144 if (!$x->get($this->key, $_REQUEST[$this->key])) {
145 $this->jerr("Invalid request");
147 $this->jok($this->update($x, $_REQUEST));
151 $this->jerr("No data recieved for inserting");
154 $this->jok($this->insert($x, $_REQUEST));
162 function delete($x, $req)
164 // do we really delete stuff!?!?!?
165 if (empty($req['_delete'])) {
166 $this->jerr("Delete Requested with no value");
168 // build a list of tables to queriy for dependant data..
173 $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database];
174 foreach($all_links as $tbl => $links) {
175 foreach($links as $col => $totbl_col) {
176 $to = explode(':', $totbl_col);
177 if ($to[0] != $x->tableName()) {
181 $affects[$tbl .'.' . $col] = true;
186 // echo '<PRE>';print_r($affects);exit;
187 // DB_Dataobject::debugLevel(1);
190 $clean = create_function('$v', 'return (int)$v;');
192 $bits = array_map($clean, explode(',', $req['_delete']));
194 // print_r($bits);exit;
196 // let's assume it has a key!!!
199 $x->whereAdd($this->key .' IN ('. implode(',', $bits) .')');
201 $this->jerr("Nothing found to delete");
204 while ($x->fetch()) {
210 if (!$this->checkPerm($x,'D') ) {
211 $this->jerr("PERMISSION DENIED (d)");
215 foreach($affects as $k=> $true) {
216 $ka = explode('.', $k);
218 $chk = DB_DataObject::factory($ka[0]);
219 if (!is_a($chk,'DB_DataObject')) {
220 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
222 // print_r(array($chk->tablename() , $ka[1] , $xx->tablename() , $this->key ));
223 $chk->{$ka[1]} = $xx->{$this->key};
225 if (count($chk->keys())) {
226 $matches = $chk->count();
228 //DB_DataObject::DebugLevel(1);
229 $matches = $chk->count($ka[1]);
233 $chk->_match_key = $ka[1];
234 $match_ar[] = clone($chk);
239 $has_beforeDelete = method_exists($xx, 'beforeDelete');
240 // before delte = allows us to trash dependancies if needed..
243 if ( $has_beforeDelete ) {
244 if ($xx->beforeDelete($match_ar, $this) === false) {
245 $errs[] = "Delete failed ({$xx->id})\n".
246 (isset($xx->err) ? $xx->err : '');
252 foreach($affects as $k=> $true) {
253 $ka = explode('.', $k);
254 $chk = DB_DataObject::factory($ka[0]);
255 if (!is_a($chk,'DB_DataObject')) {
256 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
258 $chk->{$ka[1]} = $xx->{$this->key};
259 $matches = $chk->count();
260 $match_total += $matches;
262 $chk->_match_key = $ka[1];
263 $match_ar[] = clone($chk);
270 if (!empty($match_ar)) {
273 $o = $chk->fetchAll();
274 $key = isset($chk->_match_key) ?$chk->_match_key : '?unknown column?';
275 $desc = $chk->tableName(). '.' . $key .'='.$xx->{$this->key} ;
276 if (method_exists($chk, 'toEventString')) {
277 $desc .= ' : ' . $o[0]->toEventString();
280 $this->jerr("Delete Dependant records ($match_total found), " .
281 "first is ( $desc )");
285 DB_DataObject::Factory('Events')->logDeletedRecord($x);
287 $this->addEvent("DELETE", $x);
291 if (method_exists($xx,'onDelete')) {
292 $xx->onDelete($req, $this);
298 $this->jerr(implode("\n<BR>", $errs));
300 $this->jok("Deleted");
304 function update($x, $req, $with_perm_check = true)
306 if ( $with_perm_check && !$this->checkPerm($x,'E', $req) ) {
307 $this->jerr("PERMISSION DENIED - No Edit permissions on this element");
311 // only done if we recieve a lock_id.
312 // we are very trusing here.. that someone has not messed around with locks..
313 // the object might want to check in their checkPerm - if locking is essential..
314 $lock = $this->updateLock($x,$req);
318 // this lot is generic.. needs moving
319 if (method_exists($x, 'setFromRoo')) {
320 $res = $x->setFromRoo($req, $this);
321 if (is_string($res)) {
330 //echo '<PRE>';print_r($old);print_r($x);exit;
335 if (isset($cols['modified'])) {
336 $x->modified = date('Y-m-d H:i:s');
338 if (isset($cols['modified_dt'])) {
339 $x->modified_dt = date('Y-m-d H:i:s');
341 if (isset($cols['modified_by']) && $this->authUser) {
342 $x->modified_by = $this->authUser->id;
345 if (isset($cols['updated'])) {
346 $x->updated = date('Y-m-d H:i:s');
348 if (isset($cols['updated_dt'])) {
349 $x->updated_dt = date('Y-m-d H:i:s');
351 if (isset($cols['updated_by']) && $this->authUser) {
352 $x->updated_by = $this->authUser->id;
355 if (method_exists($x, 'beforeUpdate')) {
356 $x->beforeUpdate($old, $req, $this);
359 if ($with_perm_check && !empty($_FILES) && method_exists($x, 'onUpload')) {
360 $x->onUpload($this, $_REQUEST);
363 //DB_DataObject::DebugLevel(1);
364 $res = $x->update($old);
365 if ($res === false) {
366 $this->jerr($x->_lastError->toString());
369 if (method_exists($x, 'onUpdate')) {
370 $x->onUpdate($old, $req, $this);
372 $ev = $this->addEvent("EDIT", $x);
374 $ev->audit($x, $old);
378 return $this->selectSingle(
379 DB_DataObject::factory($x->tableName()),
385 function updateLock($x, $req )
387 $this->permitError = true; // allow it to fail without dieing
389 $lock = DB_DataObjecT::factory('Core_locking');
390 $this->permitError = false;
391 if (is_a($lock,'DB_DataObject') && $this->authUser) {
393 $lock->on_id = $x->{$this->key};
394 $lock->on_table= strtolower($x->tableName());
395 if (!empty($_REQUEST['_lock_id'])) {
396 $lock->whereAdd('id != ' . ((int)$_REQUEST['_lock_id']));
398 $lock->whereAdd('person_id !=' . $this->authUser->id);
402 $exp = date('Y-m-d', strtotime('NOW - 1 WEEK'));
403 $llc->whereAdd("created < '$exp'");
406 while($llc->fetch()) {
413 if ($lock->find(true)) {
414 // it's locked by someone else..
415 $p = $lock->person();
418 $this->jerr( "Record was locked by " . $p->name . " at " .$lock->created.
419 " - Please confirm you wish to save"
420 , array('needs_confirm' => true));
424 // check the users lock.. - no point.. ??? - if there are no other locks and it's not the users, then they can
425 // edit it anyways...
427 // can we find the user's lock.
428 $lock = DB_DataObjecT::factory('Core_locking');
429 $lock->on_id = $x->{$this->key};
430 $lock->on_table= strtolower($x->tableName());
431 $lock->person_id = $this->authUser->id;
432 $lock->orderBy('created DESC');
437 isset($x->modified_dt) &&
438 strtotime($x->modified_dt) > strtotime($lock->created) &&
439 empty($req['_submit_confirmed']) &&
440 $x->modified_by != $this->authUser->id
443 $p = DB_DataObject::factory('Person');
444 $p->get($x->modified_by);
445 $this->jerr($p->name . " saved the record since you started editing,\nDo you really want to update it?", array('needs_confirm' => true));
454 function insert($x, $req, $with_perm_check = true)
456 if (method_exists($x, 'setFromRoo')) {
457 $res = $x->setFromRoo($req, $this);
458 if (is_string($res)) {
465 if ( $with_perm_check && !$this->checkPerm($x,'A', $req)) {
466 $this->jerr("PERMISSION DENIED (i)");
470 if (isset($cols['created'])) {
471 $x->created = date('Y-m-d H:i:s');
473 if (isset($cols['created_dt'])) {
474 $x->created_dt = date('Y-m-d H:i:s');
476 if (isset($cols['created_by'])) {
477 $x->created_by = $this->authUser->id;
481 if (isset($cols['modified'])) {
482 $x->modified = date('Y-m-d H:i:s');
484 if (isset($cols['modified_dt'])) {
485 $x->modified_dt = date('Y-m-d H:i:s');
487 if (isset($cols['modified_by'])) {
488 $x->modified_by = $this->authUser->id;
491 if (isset($cols['updated'])) {
492 $x->updated = date('Y-m-d H:i:s');
494 if (isset($cols['updated_dt'])) {
495 $x->updated_dt = date('Y-m-d H:i:s');
497 if (isset($cols['updated_by'])) {
498 $x->updated_by = $this->authUser->id;
501 if (method_exists($x, 'beforeInsert')) {
502 $x->beforeInsert($_REQUEST, $this);
506 if ($res === false) {
507 $this->jerr($x->_lastError->toString());
509 if (method_exists($x, 'onInsert')) {
510 $x->onInsert($_REQUEST, $this);
512 $ev = $this->addEvent("ADD", $x);
517 // note setFrom might handle this before hand...!??!
518 if (!empty($_FILES) && method_exists($x, 'onUpload')) {
519 $x->onUpload($this, $_REQUEST);
522 return $this->selectSingle(
523 DB_DataObject::factory($x->tableName()),