RooTrait.php
[Pman.Core] / RooTrait.php
1 <?php
2
3 trait Pman_Core_RooTrait {
4     
5     var $validTables = false; 
6     
7     var $key;
8     
9     var $transObj = false;
10     
11     var $debugEnabled = true;
12     
13     function init() 
14     {
15         if (isset($this->_hasInit)) {
16             return;
17         }
18         
19         $this->_hasInit = true;
20         
21         $boot = HTML_FlexyFramework::get();
22         
23         $this->appName= $boot->appName;
24         $this->appNameShort= $boot->appNameShort;
25         $this->appModules= $boot->enable;
26         $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
27         $this->appDisable = $boot->disable;
28         $this->appDisabled = explode(',', $boot->disable);
29         $this->version = $boot->version; 
30         $this->uiConfig = empty($boot->Pman['uiConfig']) ? false : $boot->Pman['uiConfig']; 
31         
32         if (!empty($ff->Pman['local_autoauth']) && 
33             ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
34             ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') 
35         ) {
36             $this->isDev = true;
37         }
38         
39     }
40     
41     function checkDebug($req = false)
42     {
43         $req =  $req === false  ? $_REQUEST : $req;
44         if (isset($req['_debug']) 
45                 && 
46                 $this->authUser
47                 &&
48                 (
49                     (
50                         method_exists($this->authUser,'canDebug')
51                         &&
52                         $this->authUser->canDebug()
53                     )
54                 ||
55                     (  
56                     
57                         method_exists($this->authUser,'groups') 
58                         &&
59                         is_a($this->authUser, 'Pman_Core_DataObjects_Person')
60                         &&
61                         in_array('Administrators', $this->authUser->groups('name'))
62                     )
63                 )
64                 
65             ){
66             DB_DAtaObject::debuglevel((int)$req['_debug']);
67         }
68         
69     }
70     
71     function checkDebugPost()
72     {
73         return (!empty($_GET['_post']) || !empty($_GET['_debug_post'])) && 
74                     $this->authUser && 
75                     method_exists($this->authUser,'groups') &&
76                     in_array('Administrators', $this->authUser->groups('name')); 
77         
78     }
79     
80     function dataObject($tab)
81     {
82         if (is_array($this->validTables) &&  !in_array($tab, $this->validTables)) {
83             $this->jerr("Invalid url - not listed in validTables");
84         }
85         
86         $tab = str_replace('/', '',$tab); // basic protection??
87         
88         $x = DB_DataObject::factory($tab);
89         
90         if (!is_a($x, 'DB_DataObject')) {
91             $this->jerr('invalid url - no dataobject');
92         }
93     
94         return $x;
95     }
96     
97     function selectSingle($x, $id, $req=false)
98     {
99         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
100
101         //var_dump(array(!is_array($id) , empty($id)));
102         if (!is_array($id) && empty($id)) {
103             
104             if (method_exists($x, 'toRooSingleArray')) {
105                 $this->jok($x->toRooSingleArray($this->authUser, $req));
106             }
107             
108             if (method_exists($x, 'toRooArray')) {
109                 $this->jok($x->toRooArray($req));
110             }
111             
112             $this->jok($x->toArray());
113         }
114        
115         
116         $this->loadMap($x, array(
117                     'columns' => $_columns,
118                      
119             ));
120         if ($req !== false) { 
121             $this->setFilters($x, $req);
122         }
123         
124         // DB_DataObject::DebugLevel(1);
125         if (is_array($id)) {
126             // lookup...
127             $x->setFrom($req['lookup'] );
128             $x->limit(1);
129             if (!$x->find(true)) {
130                 if (!empty($id['_id'])) {
131                     // standardize this?
132                     $this->jok($x->toArray());
133                 }
134                 $this->jok(false);
135             }
136             
137         } else if (!$x->get($id)) {
138             $this->jerr("selectSingle: no such record ($id)");
139         }
140         
141         // ignore perms if comming from update/insert - as it's already done...
142         if ($req !== false && !$this->checkPerm($x,'S'))  {
143             $this->jerr("PERMISSION DENIED - si");
144         }
145         // different symantics on all these calls??
146         if (method_exists($x, 'toRooSingleArray')) {
147             $this->jok($x->toRooSingleArray($this->authUser, $req));
148         }
149         if (method_exists($x, 'toRooArray')) {
150             $this->jok($x->toRooArray($req));
151         }
152         
153         $this->jok($x->toArray());
154         
155         
156     }
157     
158     /*
159      * From Pman.php
160      */
161     
162     static $permitError = false;
163     
164     function onPearError($err)
165     {
166         static $reported = false;
167         if ($reported) {
168             return;
169         }
170         
171         if (Pman::$permitError) {
172              
173             return;
174             
175         }
176         
177         $reported = true;
178         $out = $err->toString();
179         
180         $ret = array();
181         $n = 0;
182         
183         foreach($err->backtrace as $b) {
184             $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' .   @$b['class'] . '::' . @$b['function'];
185             if ($n > 20) {
186                 break;
187             }
188             $n++;
189         }
190         //convert the huge backtrace into something that is readable..
191         $out .= "\n" . implode("\n",  $ret);
192      
193         print_R($out);exit;
194         
195         $this->jerr($out);
196         
197     }
198     
199     function addEvent($act, $obj = false, $remarks = '') 
200     {
201         if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
202             return;
203         }
204         
205         $au = $this->getAuthUser();
206        
207         $e = DB_DataObject::factory('Events');
208         $e->init($act,$obj,$remarks); 
209          
210         $e->event_when = date('Y-m-d H:i:s');
211         
212         $eid = $e->insert();
213         
214         // fixme - this should be in onInsert..
215         $wa = DB_DataObject::factory('core_watch');
216         if (method_exists($wa,'notifyEvent')) {
217             $wa->notifyEvent($e); // trigger any actions..
218         }
219         
220         $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
221         
222         return $e;
223         
224     }
225     
226     function getAuthUser()
227     {
228         die('Get auth user is not implement.');
229     }
230 }