fix #8131 - chinese translations
[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     var $appName;
14     var $appNameShort;
15     var $appModules;
16     var $isDev;
17     var $appDisable;
18     var $appDisabled;
19     var $version ;
20     var $uiConfig ;
21       
22     var $cols = array();
23     var $countWhat;
24     var $colsJname;    
25     var $_hasInit;
26     
27     function init() 
28     {
29         if (!empty($this->_hasInit)) {
30             return;
31         }
32         
33         $this->_hasInit = true;
34         
35         $boot = HTML_FlexyFramework::get();
36         
37         $this->appName= $boot->appName;
38         $this->appNameShort= $boot->appNameShort;
39         $this->appModules= $boot->enable;
40         $this->isDev = empty($boot->Pman['isDev']) ? false : $boot->Pman['isDev'];
41         $this->appDisable = $boot->disable;
42         $this->appDisabled = explode(',', $boot->disable);
43         $this->version = $boot->version; 
44         $this->uiConfig = empty($boot->Pman['uiConfig']) ? false : $boot->Pman['uiConfig']; 
45         
46         if (!empty($ff->Pman['local_autoauth']) && 
47             ($_SERVER['SERVER_ADDR'] == '127.0.0.1') &&
48             ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') 
49         ) {
50             $this->isDev = true;
51         }
52         
53     }
54     
55     function checkDebug($req = false)
56     {
57         $req =  $req === false  ? $_REQUEST : $req;
58         if (isset($req['_debug']) 
59                 && 
60                 $this->authUser
61                 &&
62                 (
63                     (
64                         method_exists($this->authUser,'canDebug')
65                         &&
66                         $this->authUser->canDebug()
67                     )
68                 ||
69                     (  
70                     
71                         method_exists($this->authUser,'groups') 
72                         &&
73                         is_a($this->authUser, 'Pman_Core_DataObjects_Person')
74                         &&
75                         in_array('Administrators', $this->authUser->groups('name'))
76                     )
77                 )
78                 
79             ){
80             DB_DAtaObject::debuglevel((int)$req['_debug']);
81         }
82         
83     }
84     
85     function dataObject($tab)
86     {
87         if (is_array($this->validTables) &&  !in_array($tab, $this->validTables)) {
88             $this->jerr("Invalid url - not listed in validTables");
89         }
90         
91         $tab = str_replace('/', '',$tab); // basic protection??
92         
93         $x = DB_DataObject::factory($tab);
94         
95         if (!is_a($x, 'DB_DataObject')) {
96             $this->jerr('invalid url - no dataobject');
97         }
98     
99         return $x;
100     }
101     
102     function selectSingle($x, $id, $req=false)
103     {
104         $_columns = !empty($req['_columns']) ? explode(',', $req['_columns']) : false;
105
106         if (!is_array($id) && empty($id)) {
107             
108             if (method_exists($x, 'toRooSingleArray')) {
109                 $this->jok($x->toRooSingleArray($this->authUser, $req));
110             }
111             
112             if (method_exists($x, 'toRooArray')) {
113                 $this->jok($x->toRooArray($req));
114             }
115             
116             $this->jok($x->toArray());
117         }
118         
119         $this->loadMap($x, array(
120             'columns' => $_columns,
121         ));
122         
123         if ($req !== false) { 
124             $this->setFilters($x, $req);
125         }
126         
127         if (is_array($id)) {
128             // lookup...
129             $x->setFrom($req['lookup'] );
130             $x->limit(1);
131             if (!$x->find(true)) {
132                 if (!empty($id['_id'])) {
133                     // standardize this?
134                     $this->jok($x->toArray());
135                 }
136                 $this->jok(false);
137             }
138             
139         } else if (!$x->get($id)) {
140             $this->jerr("selectSingle: no such record ($id)");
141         }
142         
143         // ignore perms if comming from update/insert - as it's already done...
144         if ($req !== false && !$this->checkPerm($x,'S'))  {
145             $this->jerr("PERMISSION DENIED - si");
146         }
147         // different symantics on all these calls??
148         if (method_exists($x, 'toRooSingleArray')) {
149             $this->jok($x->toRooSingleArray($this->authUser, $req));
150         }
151         if (method_exists($x, 'toRooArray')) {
152             $this->jok($x->toRooArray($req));
153         }
154         
155         $this->jok($x->toArray());
156         
157         
158     }
159     
160     
161     
162     function loadMap($do, $cfg =array())
163     {
164         $onlycolumns    = !empty($cfg['columns']) ? $cfg['columns'] : false;
165         $distinct       = !empty($cfg['distinct']) ? $cfg['distinct'] : false;
166         $excludecolumns = !empty($cfg['exclude']) ? $cfg['exclude'] : array();
167           
168         $excludecolumns[] = 'passwd'; // we never expose passwords
169         
170         $ret = $do->autoJoin(array(
171             'include' => $onlycolumns,
172             'exclude' => $excludecolumns,
173             'distinct' => $distinct
174         ));
175         
176         $this->countWhat = $ret['count'];
177         $this->cols = $ret['cols'];
178         $this->colsJname = $ret['join_names'];
179         
180         return;
181         
182     }
183     
184     function setFilters($x, $q)
185     {
186         if (method_exists($x, 'applyFilters')) {
187            // DB_DataObject::debugLevel(1);
188             if (false === $x->applyFilters($q, $this->authUser, $this)) {
189                 return; 
190             } 
191         }
192         $q_filtered = array();
193         
194         $keys = $x->keys();
195         // var_dump($keys);exit;
196         foreach($q as $key=>$val) {
197             
198             if (in_array($key,$keys) && !is_array($val)) {
199                
200                 $x->$key  = $val;
201             }
202             
203              // handles name[]=fred&name[]=brian => name in ('fred', 'brian').
204             // value is an array..
205             if (is_array($val) ) {
206                 
207                 $pref = '';
208                 
209                 if ($key[0] == '!') {
210                     $pref = '!';
211                     $key = substr($key,1);
212                 }
213                 
214                 if (!in_array( $key,  array_keys($this->cols))) {
215                     continue;
216                 }
217                 
218                 // support a[0] a[1] ..... => whereAddIn(
219                 $ar = array();
220                 $quote = false;
221                 foreach($val as $k=>$v) {
222                     if (!is_numeric($k)) {
223                         $ar = array();
224                         break;
225                     }
226                     // FIXME: note this is not typesafe for anything other than mysql..
227                     
228                     if (!is_numeric($v) || !is_long($v)) {
229                         $quote = true;
230                     }
231                     $ar[] = $v;
232                     
233                 }
234                 if (count($ar)) {
235                     
236                     
237                     $x->whereAddIn($pref . (
238                         isset($this->colsJname[$key]) ? 
239                             $this->colsJname[$key] :
240                             ($x->tableName(). '.'.$key)),
241                         $ar, $quote ? 'string' : 'int');
242                 }
243                 
244                 continue;
245             }
246             
247             
248             // handles !name=fred => name not equal fred.
249             if ($key[0] == '!' && in_array(substr($key, 1), array_keys($this->cols))) {
250                 
251                 $key  = substr($key, 1) ;
252                 
253                 $x->whereAdd(   (
254                         isset($this->colsJname[$key]) ? 
255                             $this->colsJname[$key] :
256                             $x->tableName(). '.'.$key ) . ' != ' .
257                     (is_numeric($val) ? $val : "'".  $x->escape($val) . "'")
258                 );
259                 continue;
260                 
261             }
262
263             switch($key) {
264                     
265                 // Events and remarks -- fixme - move to events/remarsk...
266                 case 'on_id':  // where TF is this used...
267                     if (!empty($q['query']['original'])) {
268                       //  DB_DataObject::debugLevel(1);
269                         $o = (int) $q['query']['original'];
270                         $oid = (int) $val;
271                         $x->whereAdd("(on_id = $oid  OR 
272                                 on_id IN ( SELECT distinct(id) FROM Documents WHERE original = $o ) 
273                             )");
274                         continue 2;
275                                 
276                     }
277                     $x->on_id = $val;
278                 
279                 
280                 default:
281                     if (strlen($val) && $key[0] != '_') {
282                         $q_filtered[$key] = $val;
283                     }
284                     
285                     // subjoined columns = check the values.
286                     // note this is not typesafe for anything other than mysql..
287                     
288                     if (isset($this->colsJname[$key])) {
289                         $quote = false;
290                         if (!is_numeric($val) || !is_long($val)) {
291                             $quote = true;
292                         }
293                         $x->whereAdd( "{$this->colsJname[$key]} = " . ($quote ? "'". $x->escape($val) ."'" : $val));
294                         
295                     }
296                     
297                     
298                     continue 2;
299             }
300         }
301         if (!empty($q_filtered)) {
302             $x->setFrom($q_filtered);
303         }
304         
305         if (!empty($q['query']['name'])) {
306             if (in_array( 'name',  array_keys($x->table()))) {
307                 $x->whereAdd($x->tableName().".name LIKE '". $x->escape($q['query']['name']) . "%'");
308             }
309         }
310         
311     }
312     
313     
314     /*
315      * From Pman.php
316      */
317     
318     static $permitError = false;
319     
320     function onPearError($err)
321     {
322         static $reported = false;
323         if ($reported) {
324             return;
325         }
326         
327         if (self::$permitError) {
328              
329             return;
330             
331         }
332         
333         $reported = true;
334         $out = $err->toString();
335         
336         $ret = array();
337         $n = 0;
338         
339         foreach($err->backtrace as $b) {
340             $ret[] = @$b['file'] . '(' . @$b['line'] . ')@' .   @$b['class'] . '::' . @$b['function'];
341             if ($n > 20) {
342                 break;
343             }
344             $n++;
345         }
346         //convert the huge backtrace into something that is readable..
347         $out .= "\n" . implode("\n",  $ret);
348      
349         print_R($out);exit;
350         
351         $this->jerr($out);
352         
353     }
354     
355     function addEvent($act, $obj = false, $remarks = '') 
356     {
357         if (!empty(HTML_FlexyFramework::get()->Pman['disable_events'])) {
358             return;
359         }
360         
361         $e = DB_DataObject::factory('Events');
362         $e->init($act,$obj,$remarks); 
363          
364         $e->event_when = date('Y-m-d H:i:s');
365         
366         $eid = $e->insert();
367         
368         // fixme - this should be in onInsert..
369         $wa = DB_DataObject::factory('core_watch');
370         if (method_exists($wa,'notifyEvent')) {
371             $wa->notifyEvent($e); // trigger any actions..
372         }
373         
374         $e->onInsert(isset($_REQUEST) ? $_REQUEST : array() , $this);
375         
376         return $e;
377         
378     }
379     
380     function checkPerm($obj, $lvl, $req= null)
381     {
382         if (!method_exists($obj, 'checkPerm')) {
383             return true;
384         }
385         if ($obj->checkPerm($lvl, $this->authUser, $req))  {
386             return true;
387         }
388         
389         return false;
390     }
391     
392     function hasPerm($name, $lvl)  // do we have a permission
393     {
394         static $pcache = array();
395         $au = $this->getAuthUser();
396         return $au && $au->hasPerm($name, $lvl);
397         
398     }
399     
400     function getAuthUser()
401     {
402         die('Get auth user is not implement.');
403     }
404     
405 }