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);
126 // not sure if this is a good idea here...
128 if (!empty($_REQUEST['_ids'])) {
129 $ids = explode(',',$_REQUEST['_ids']);
130 $x->whereAddIn($this->key, $ids, 'int');
131 $ar = $x->fetchAll();
134 $this->update($x, $_REQUEST);
138 $this->jok("UPDATED");
143 if (!empty($_REQUEST[$this->key])) {
145 if (!$x->get($this->key, $_REQUEST[$this->key])) {
146 $this->jerr("Invalid request");
148 $this->jok($this->update($x, $_REQUEST));
152 $this->jerr("No data recieved for inserting");
155 $this->jok($this->insert($x, $_REQUEST));
163 function delete($x, $req)
165 // do we really delete stuff!?!?!?
166 if (empty($req['_delete'])) {
167 $this->jerr("Delete Requested with no value");
169 // build a list of tables to queriy for dependant data..
174 $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$x->_database];
175 foreach($all_links as $tbl => $links) {
176 foreach($links as $col => $totbl_col) {
177 $to = explode(':', $totbl_col);
178 if ($to[0] != $x->tableName()) {
182 $affects[$tbl .'.' . $col] = true;
187 // echo '<PRE>';print_r($affects);exit;
188 // DB_Dataobject::debugLevel(1);
191 $clean = create_function('$v', 'return (int)$v;');
193 $bits = array_map($clean, explode(',', $req['_delete']));
195 // print_r($bits);exit;
197 // let's assume it has a key!!!
200 $x->whereAdd($this->key .' IN ('. implode(',', $bits) .')');
202 $this->jerr("Nothing found to delete");
205 while ($x->fetch()) {
211 if (!$this->checkPerm($x,'D') ) {
212 $this->jerr("PERMISSION DENIED (d)");
216 foreach($affects as $k=> $true) {
217 $ka = explode('.', $k);
219 $chk = DB_DataObject::factory($ka[0]);
220 if (!is_a($chk,'DB_DataObject')) {
221 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
223 // print_r(array($chk->tablename() , $ka[1] , $xx->tablename() , $this->key ));
224 $chk->{$ka[1]} = $xx->{$this->key};
226 if (count($chk->keys())) {
227 $matches = $chk->count();
229 //DB_DataObject::DebugLevel(1);
230 $matches = $chk->count($ka[1]);
234 $chk->_match_key = $ka[1];
235 $match_ar[] = clone($chk);
240 $has_beforeDelete = method_exists($xx, 'beforeDelete');
241 // before delte = allows us to trash dependancies if needed..
244 if ( $has_beforeDelete ) {
245 if ($xx->beforeDelete($match_ar, $this) === false) {
246 $errs[] = "Delete failed ({$xx->id})\n".
247 (isset($xx->err) ? $xx->err : '');
253 foreach($affects as $k=> $true) {
254 $ka = explode('.', $k);
255 $chk = DB_DataObject::factory($ka[0]);
256 if (!is_a($chk,'DB_DataObject')) {
257 $this->jerr('Unable to load referenced table, check the links config: ' .$ka[0]);
259 $chk->{$ka[1]} = $xx->{$this->key};
260 $matches = $chk->count();
261 $match_total += $matches;
263 $chk->_match_key = $ka[1];
264 $match_ar[] = clone($chk);
271 if (!empty($match_ar)) {
274 $o = $chk->fetchAll();
275 $key = isset($chk->_match_key) ?$chk->_match_key : '?unknown column?';
276 $desc = $chk->tableName(). '.' . $key .'='.$xx->{$this->key} ;
277 if (method_exists($chk, 'toEventString')) {
278 $desc .= ' : ' . $o[0]->toEventString();
281 $this->jerr("Delete Dependant records ($match_total found), " .
282 "first is ( $desc )");
286 DB_DataObject::Factory('Events')->logDeletedRecord($x);
288 $this->addEvent("DELETE", $x);
292 if (method_exists($xx,'onDelete')) {
293 $xx->onDelete($req, $this);
299 $this->jerr(implode("\n<BR>", $errs));
301 $this->jok("Deleted");