81b29a0625a304ef836d38cdbb12d3abde2112c2
[Pman.Core] / RooPostTrait.php
1 <?php
2
3 trait Pman_Core_RooPostTrait {
4     
5     /**
6      * POST method   Roo/TABLENAME  
7      * -- creates, updates, or deletes data.
8      *
9      * INSERT
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
15      *        
16      *   will return a GET request SINGLE SELECT (and accepts same)
17      *    
18      * DELETE
19      *    _delete=1,2,3     delete a set of data.
20      * UPDATE
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
25      *        
26      *
27      * Params:
28      *   _delete=1,2,3   causes a delete to occur.
29      *   _ids=1,2,3,4    causes update to occur on all primary ids.
30      *  
31      *  RETURNS
32      *     = same as single SELECT GET request..
33      *
34      *
35      *
36      * DEBUGGING
37      *   _debug=1    forces debug
38      *   _get=1 - causes a get request to occur when doing a POST..
39      *
40      *
41      * CALLS
42      *   these methods on dataobjects if they exist
43      * 
44      *   checkPerm('E' / 'D' , $authuser)
45      *                      - can we list the stuff
46      *                      - return false to disallow...
47    
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..
53      *
54      *   toEventString()
55      *                  (for logging - this is generically prefixed to all database operations.)
56      *
57      *  
58      *   onUpload($roo)
59      *                  called when $_FILES is not empty
60      *
61      *                  
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...
66      *
67      * CALLS BEFORE change occurs:
68      *  
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;
73      *                      
74      *      beforeUpdate($old, $request,$roo)
75      *                      - after update - jerr() will stop insert..
76      *      beforeInsert($request,$roo)
77      *                      - before insert - jerr() will stop insert..
78      *
79      *
80      * CALLS AFTER change occured
81      * 
82      *      onUpdate($old, $request,$roo)
83      *               - after update // return value ignored
84      *
85      *      onInsert($request,$roo)
86      *                  - after insert
87      * 
88      *      onDelete($request, $roo) - after delete
89      * 
90      */                     
91      
92     function post($tab) // update / insert (?? delete??)
93     {
94         // -- why was this put in? - Roo is not related to Core.All ?
95         //if (!$this->hasPerm("Core.All", 'E'))  {
96         //        $this->jerr("PERMISSION DENIED (e)");
97         //}
98         
99         
100         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
101     
102         //DB_DataObject::debugLevel(1);
103         $this->checkDebug();
104         
105         if (!empty($_REQUEST['_get'])) {
106             return $this->get($tab);
107         }
108         
109         $this->init(); // for pman.
110          
111         $x = $this->dataObject($tab);
112         
113         $this->transObj = clone($x);
114         
115         $this->transObj->query('BEGIN');
116         // find the key and use that to get the thing..
117         $keys = $x->keys();
118         if (empty($keys) ) {
119             $this->jerr('no key');
120         }
121         
122         $this->key = $keys[0];
123         
124           // delete should be here...
125         if (isset($_REQUEST['_delete'])) {
126             // do we really delete stuff!?!?!?
127             return $this->delete($x,$_REQUEST);
128         } 
129          
130         
131         $old = false;
132         
133         // not sure if this is a good idea here...
134         
135         if (!empty($_REQUEST['_ids'])) {
136             $ids = explode(',',$_REQUEST['_ids']);
137             $x->whereAddIn($this->key, $ids, 'int');
138             $ar = $x->fetchAll();
139             
140             foreach($ar as $x) {
141                 $this->update($x, $_REQUEST);
142                 
143             }
144             // all done..
145             $this->jok("UPDATED");
146             
147             
148         }
149          
150         if (!empty($_REQUEST[$this->key])) {
151             // it's a create..
152             if (!$x->get($this->key, $_REQUEST[$this->key]))  {
153                 $this->jerr("Invalid request");
154             }
155             $this->jok($this->update($x, $_REQUEST));
156         } else {
157             
158             if (empty($_POST)) {
159                 $this->jerr("No data recieved for inserting");
160             }
161             
162             $this->jok($this->insert($x, $_REQUEST));
163             
164         }
165         
166         
167         
168     }
169 }