php7 fixes
[Pman.Builder] / Code.php
1 <?php
2
3 require_once 'Pman.php';
4
5 class Pman_Builder_Code extends Pman
6 {
7     function getAuth()
8     {
9         return true; // everyone?
10     }
11     function get($code, $opts = array()) 
12     {
13         
14         
15         
16        //var_dump($_REQUEST);
17         if (!empty($_REQUEST['_recover'])) {
18             $this->recover($code);
19         }
20        //DB_DataObject::debugLevel(1);
21         $b = DB_DataObject::factory('builder');
22       
23         if (!empty($_REQUEST['id'])) {
24             if (!$b->get($_REQUEST['id'])) {
25                 die("Invalid URL");
26             }
27         }
28         if (!empty($code)) {
29             if (!$b->get('module', $code)) {
30                 die("Invalid URL");
31             }
32         }
33         if (!$b->id) {
34             die("Invalid URL");
35         }
36         
37         
38         $json = json_decode($b->json);
39         //echo '<PRE>';print_r($b->json);exit;
40         $out  = $this->toJSFile($json, $b->json);
41      
42         if (!empty($code)) {
43             header('Content-type: application/javascript');
44             echo $out;
45             exit;
46         }
47         
48         echo "<PRE>";
49         //print_r($json);
50         echo htmlspecialchars($out);
51         
52        
53         
54         exit; 
55         
56     }
57     
58     
59     function recover($code)
60     {
61         if (!$this->getAuthUser()) {
62             $this->jerr("invalid");
63         }
64         // fixme!!!!
65         
66         $fn = $this->rootDir .'/Pman/' . $code.'.js';
67         
68         list($app, $mod) = explode('/', $code);
69         
70         if (!file_exists($fn)) {
71             $this->jerr("file does not exist.:" . $fn);
72         }
73         $data = file_get_contents($fn);
74         preg_match("#/*\n--SOURCE--\n([^\*]+)#m", $data,$matches);
75        // echo "<PRE>";print_R($matches);
76         $base = $matches[1];
77         $str = bzdecompress( base64_decode($base));
78         $js = json_decode($str);
79         //echo "<PRE>";print_R($str);
80          $b = DB_DataObject::factory('builder');
81         $b->app = $app;
82         $b->module = $mod;
83         $b->btype = 'FORM';
84         $b->name= $js->name;
85         if (!$b->find(true)) {
86             $b->insert();
87         }
88         echo "<PRE>" . htmlspecialchars( print_r($b, true));
89         $b->json = $str;
90         $b->update();
91         $this->jok("DONE");
92         exit;
93         
94         
95         
96         
97     }
98     
99     
100     function mungePropsObj($obj)
101     {
102         if (isset($obj->items)) {
103             $items = array();
104             
105             // look for *prop...
106             
107             foreach($obj->items as $o) {
108                 if (!empty($o->{'*prop'})) {
109                     $pn = $o->{'*prop'};
110                     unset($o->{'*prop'});
111                     $this->mungePropsObj($o);
112                     
113                    
114                     
115                     $obj->$pn = $o;
116                     if (isset($o->xtype) &&  $o->xtype == 'Array') {
117                         if (empty($o->items)) {
118                             unset($obj->$pn);
119                             continue;
120                         }
121                         $obj->$pn = $o->items;
122                     }
123                     
124                     
125                     continue;
126                 }
127                 
128                 // otherwise is a standard item..
129                 $this->mungePropsObj($o);
130                 
131                  //if ($o->xtype == 'GridField') {
132                  //       continue;
133                  //   }
134                 $items[] = $o;
135             }
136             
137             $obj->items = $items;
138             if (empty($items)) {
139                 unset($obj->items);
140             }
141         }
142         if (!empty($obj->xtype) && ($obj->xtype[0] == '*')) {
143             unset($obj->xtype);
144         }
145         
146         
147     }
148     
149     function jsHead()
150     {
151         return array(
152             "//<script type=\"text/javascript\">",
153             "//",
154             "// Version " . date("Ymd-His"),
155             "//",
156             "// Auto generated file - do not edit directly",
157             "// created by Application Builder (Web version)",
158             ""
159         );
160        
161     }
162     function toJSFileDialog($obj, $json=false)
163     {
164         $out = implode("\n", $this->jsHead());
165        
166         $module = $obj->{'|module'};
167         $app = $obj->app;
168         $out .= implode("\n", array(
169             "$module = {",
170             "",
171             "    dialog : false,",
172             "    callback:  false,",
173             "",   
174             "    show : function(data, cb)",
175             "    {",
176             "        if (!this.dialog) {",
177             "            this.create();",
178             "        }",
179             "",
180             "        this.callback = cb;",
181             "        this.data = data;",
182             "        this.dialog.show();",
183             "        if (this.form) {",
184             "           this.form.reset();",
185             "           this.form.setValues(data);",
186             "           this.form.fireEvent('actioncomplete', this.form,  { type: 'setdata', data: data });",
187             "        }",
188             "",   
189             "    },",
190             "",
191             "    create : function()",
192             "    {",
193             "        var _this = this;",
194             "        this.dialog = Roo.factory("
195         ));
196         $this->mungePropsObj($obj->items[0]);
197         if (isset($region)) {
198             $obj->items[0]->region = $region;
199         }
200        
201         $out .= $this->toJSObj($obj->items[0], 3); // should be first child really...
202         
203         $out .= ");\n";
204         
205         if (isset($obj->items[0]->layout)) {
206             $out .= implode("\n", array(
207                 "       this.layout = this.dialog.layout;",
208                 ""
209             ));
210            
211         }
212                 
213         $out .=  implode("\n", array(
214             "    }",
215             "};",
216             "",
217         ));
218         $out.=$this->jsonToComment($json);
219         
220         
221       
222         return $out;
223     
224         
225        
226     }
227     function toJSFileLayout($obj, $json=false)
228     {
229         $out = implode("\n", $this->jsHead());
230        
231         $module = $obj->{'|module'};
232         $app = $obj->app;
233         
234         
235               
236            // initially we can get this from $ar
237            
238            
239        
240         $perm = !empty($obj->perm) ? $obj->perm : '';
241         $permtype = !empty($obj->type) ?  $obj->type : 'S';
242         $v = sprintf('%03d', (int) isset($obj->modkey) ? $obj->modkey : 0);
243         $modkey = $v.'-' . strtolower(str_replace('.', '_', $module));
244         if (isset($obj->region)) {
245                 
246             
247             $region = $obj->region;
248             $parent = $obj->{'|parent'};
249             $name = $obj->name;
250             $app_perm = strpos($perm, '.') > 0 ? $perm: "$app.$perm";
251             
252             
253             // I know this is dumb.. but otherwise indenting doesnt really work.
254             $out .= implode("\n", array(
255                 "",
256                 "",
257                 "",
258                 "// register the module first",
259                 "Pman.on('beforeload', function()",
260                 "{",
261                 ""
262             ));
263             
264             if  (!empty($perm)) {
265                 $out .= implode("\n", array(
266                
267                     "   if (Pman.Login.authUser.company_id_comptype != 'OWNER') {",
268                     "        return;",
269                     "    }",
270                     "",
271                     "    if (!Pman.hasPerm('$app_perm', '$permtype')) {",
272                     "        return;",
273                     "    }"
274                 ));
275             }
276             $dis = empty($obj->disabled) ? 'false' : 'true';
277             if (!empty($obj->{'|disabled'})) {
278                 $dis = implode("\n      ", explode("\n", $obj->{'|disabled'}));
279             }
280             $out .= implode("\n", array(
281                 "    var disabled = $dis;",
282                 "    if (disabled) {",
283                 "        return; ",
284                 "    }",
285                 "    Pman.register({",
286                 "        modKey : '$modkey',",
287                 "        module : $module,",
288                 "        region : '$region',",
289                 "        parent : $parent,",
290                 "        name : \"$name\"",
291                 "    });",
292                 "});",
293                 ""
294             ));
295         }
296         $baseprop = 'panel';
297         
298         
299         
300         
301         
302         $out .= implode("\n", array(
303             "$module = new Roo.util.Observable({",
304             "",
305             "    panel : false,",
306             "    disabled : false,",
307             "    parentLayout:  false,",
308             "",
309             "    add : function(parentLayout, region)",
310             "    {",
311             "",
312             "        var _this = this;", // standard avaialbe..
313             "        this.parentLayout = parentLayout;",
314             "",
315             "        this.panel = parentLayout.addxtype("
316         ));
317         
318         $this->mungePropsObj($obj->items[0]);
319         if (isset($region)) {
320             $obj->items[0]->region = $region;
321         }
322        
323         $out .= $this->toJSObj($obj->items[0], 3); // should be first child really...
324         
325         $out .= ");\n";
326         
327         if (isset($obj->items[0]->layout)) {
328             $out .= implode("\n", array(
329                 "       this.layout = this.panel.layout;",
330                 ""
331             ));
332            
333         }
334                 
335         $out .=  implode("\n", array(
336             "    }",
337             "});",
338             "",
339         ));
340         $out.=$this->jsonToComment($json);
341         
342       
343         return $out;
344     }
345     
346             
347             
348
349     function jsonToComment($json)
350     {
351         if ($json === false) {
352             return '';
353         } 
354         // not sure about \ either...
355         return implode("\n", array(
356                 "/*",
357                 "--SOURCE--" ,
358                 wordwrap(str_replace('*/', "*\n/",base64_encode(bzcompress($json))), 80, "\n", true) ,
359                 "*/",
360                 ""
361             ));
362                   
363         
364     }
365          
366     
367     
368     function toJSFile($obj, $json=false)
369     {
370         
371         
372         if (empty($obj->{'|module'}) || (!empty($obj->disabled) && strlen($obj->disabled) == 1)) {
373            return '';
374         }
375         if ($obj->items[0]->xtype != 'LayoutDialog') {
376            return $this->toJSFileLayout($obj,$json);
377         }
378         return $this->toJSFileDialog($obj,$json);
379         
380     }
381     
382     
383     
384     
385     
386     
387     
388     
389     /**
390      * simple js dumper..
391      * 
392      * 
393      */
394     function toJSObj($obj, $in)
395     {
396         
397         $out =  '';
398         $ix = str_repeat("    ", $in);
399         $ix1 = str_repeat("    ", $in-1);
400         foreach((array)$obj as $k=>$v) {
401             if ($k[0] == '+') {
402                 continue;
403             }
404             if (is_object($v)) {
405                 $as_ar = (array)$v;
406                 if (empty($as_ar)) {
407                     continue;
408                 }
409             }
410             $out .= strlen($out) ? ",\n" : '';
411             switch(true) {
412                 case is_object($v):
413                   
414                     $out .= $ix . "$k : " . $this->toJSObj($v, $in+1);
415                     continue 2;
416                 
417                 case is_array($v):
418                     $out .= $ix . "$k : " . $this->toJSAr($v, $in+1);
419                     continue 2;
420             
421                 case is_string($v) && ($k[0] == '|'):
422                     $out .= $ix . substr($k,1). ": " . implode("\n" . $ix, explode("\n", $v));
423                     continue 2;
424            
425                 default :
426                     $out .= $ix . "$k : " . $this->toJSPrim($v, isset($obj->xtype) ? $obj->xtype : '', $k);
427                     continue 2;
428             }
429              
430             // property is string/bool/num... 
431             // strings can be '|' raw...
432             
433             
434         }
435         return  "{\n" . $out . "\n" . $ix1 . "}";
436         
437     }
438     function toJSPrim($v, $xtype, $k)
439     {
440         if (is_bool($v)) {
441             return  ($v ? 'true' : 'false');
442         }
443         if (is_numeric($v)) {
444             return $v;    
445         }
446         if (!is_string($v)) {
447             return  "'??? not string? " . gettype($v) ."'";
448         }
449         return $this->toString($xtype, $k, $v);
450         
451         //return json_encode($v);
452     }
453     
454     function toJSAr($ar, $in)
455     {
456         $isobjar = false;
457         foreach($ar as $o) {
458             if (!is_object($o) && !is_array($o)) {
459                 continue;
460             }
461             $isobjar = true;
462         }
463         if (!$isobjar) {
464             $xa = array();
465             foreach($ar as $o) {
466                 $xa = $this->toJSPrim($o);
467             }
468             return '['. implode(", ", $xa) .']';
469         }
470         // array of parts...
471         $out =  '';
472         $ix = str_repeat("    ", $in);
473         foreach($ar as $v) {
474             $out .= strlen($out) ? ",\n" : '';
475             switch(true) {
476                 case is_object($v):
477                     $out .= $ix . $this->toJSObj($v, $in+1);
478                     continue 2;
479                 
480                 case is_array($v):
481                     $out .= $ix .  $this->toJSAr($v, $in+1);
482                     continue 2;
483             
484                // case is_string($v) && ($k[0] == '|'): // can not have raw in an array?
485                //     $out .= $ix . . $v;
486                 //    continue;
487            
488                 default :
489                     $out .=  $this->toJSPrim($v); // we can not fix these...!!!!
490                     continue 2;  
491             }
492              
493         }
494         return "[\n" . $out . "\n" . str_repeat("    ", $in-1) . "]";
495         
496     }
497     
498     function toString($xtype, $prop, $str)
499     {
500              
501         
502         if (in_array($prop , array(
503             'fieldLabel',
504             'qtip',
505             'emptyText',
506             'loadingText',
507             'title',
508             'legend',
509             'text',
510             'displayMsg',
511             'emptyMsg',
512             
513             
514         ))) {
515             return json_encode($str);
516         }
517         $str = json_encode($str);
518         $str= substr($str,1,-1);
519         return "'". str_replace("'", "\\'", $str) . "'";
520         
521         
522     }
523 }