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 dataObject($tab)
72     {
73         if (is_array($this->validTables) &&  !in_array($tab, $this->validTables)) {
74             $this->jerr("Invalid url - not listed in validTables");
75         }
76         
77         $tab = str_replace('/', '',$tab); // basic protection??
78         
79         $x = DB_DataObject::factory($tab);
80         
81         if (!is_a($x, 'DB_DataObject')) {
82             $this->jerr('invalid url - no dataobject');
83         }
84     
85         return $x;
86     }
87     
88     function selectSingle($x, $id, $req=false)
89     {
90         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
91
92         if (!is_array($id) && empty($id)) {
93             
94             if (method_exists($x, 'toRooSingleArray')) {
95                 $this->jok($x->toRooSingleArray($this->getAuthUser(), $req));
96             }
97             
98             if (method_exists($x, 'toRooArray')) {
99                 $this->jok($x->toRooArray($req));
100             }
101             
102             $this->jok($x->toArray());
103         }
104        
105         
106         $this->loadMap($x, array(
107                     'columns' => $_columns,
108                      
109             ));
110         if ($req !== false) { 
111             $this->setFilters($x, $req);
112         }
113         
114         // DB_DataObject::DebugLevel(1);
115         if (is_array($id)) {
116             // lookup...
117             $x->setFrom($req['lookup'] );
118             $x->limit(1);
119             if (!$x->find(true)) {
120                 if (!empty($id['_id'])) {
121                     // standardize this?
122                     $this->jok($x->toArray());
123                 }
124                 $this->jok(false);
125             }
126             
127         } else if (!$x->get($id)) {
128             $this->jerr("selectSingle: no such record ($id)");
129         }
130         
131         // ignore perms if comming from update/insert - as it's already done...
132         if ($req !== false && !$this->checkPerm($x,'S'))  {
133             $this->jerr("PERMISSION DENIED - si");
134         }
135         // different symantics on all these calls??
136         if (method_exists($x, 'toRooSingleArray')) {
137             $this->jok($x->toRooSingleArray($this->authUser, $req));
138         }
139         if (method_exists($x, 'toRooArray')) {
140             $this->jok($x->toRooArray($req));
141         }
142         
143         $this->jok($x->toArray());
144         
145         
146     }
147     
148     /*
149      * From Pman.php
150      */
151     
152     static $permitError = false;
153     
154     function onPearError($err)
155     {
156         static $reported = false;
157         if ($reported) {
158             return;
159         }
160         
161         if (Pman::$permitError) {
162              
163             return;
164             
165         }
166         
167         $reported = true;
168         $out = $err->toString();
169         
170         $ret = array();
171         $n = 0;
172         
173         foreach($err->backtrace as $b) {
174             $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' .   @$b['class'] . '::' . @$b['function'];
175             if ($n > 20) {
176                 break;
177             }
178             $n++;
179         }
180         //convert the huge backtrace into something that is readable..
181         $out .= "\n" . implode("\n",  $ret);
182      
183         print_R($out);exit;
184         
185         $this->jerr($out);
186         
187     }
188     
189     function addEvent($act, $obj = false, $remarks = '') 
190     {
191         if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
192             return;
193         }
194         
195         $au = $this->getAuthUser();
196        
197         $e = DB_DataObject::factory('Events');
198         $e->init($act,$obj,$remarks); 
199          
200         $e->event_when = date('Y-m-d H:i:s');
201         
202         $eid = $e->insert();
203         
204         // fixme - this should be in onInsert..
205         $wa = DB_DataObject::factory('core_watch');
206         if (method_exists($wa,'notifyEvent')) {
207             $wa->notifyEvent($e); // trigger any actions..
208         }
209         
210         $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
211         
212         return $e;
213         
214     }
215     
216     function getAuthUser()
217     {
218         die('Get auth user is not implement.');
219     }
220 }