3 require_once 'Pman.php';
5 class Pman_Builder_Code extends Pman
9 return true; // everyone?
11 function get($code, $opts = array())
16 //var_dump($_REQUEST);
17 if (!empty($_REQUEST['_recover'])) {
18 $this->recover($code);
20 //DB_DataObject::debugLevel(1);
21 $b = DB_DataObject::factory('builder');
23 if (!empty($_REQUEST['id'])) {
24 if (!$b->get($_REQUEST['id'])) {
29 if (!$b->get('module', $code)) {
38 $json = json_decode($b->json);
39 //echo '<PRE>';print_r($b->json);exit;
40 $out = $this->toJSFile($json, $b->json);
43 header('Content-type: application/javascript');
50 echo htmlspecialchars($out);
59 function recover($code)
61 if (!$this->getAuthUser()) {
62 $this->jerr("invalid");
66 $fn = $this->rootDir .'/Pman/' . $code.'.js';
68 list($app, $mod) = explode('/', $code);
70 if (!file_exists($fn)) {
71 $this->jerr("file does not exist.:" . $fn);
73 $data = file_get_contents($fn);
74 preg_match("#/*\n--SOURCE--\n([^\*]+)#m", $data,$matches);
75 // echo "<PRE>";print_R($matches);
77 $str = bzdecompress( base64_decode($base));
78 $js = json_decode($str);
79 //echo "<PRE>";print_R($str);
80 $b = DB_DataObject::factory('builder');
85 if (!$b->find(true)) {
88 echo "<PRE>" . htmlspecialchars( print_r($b, true));
100 function mungePropsObj($obj)
102 if (isset($obj->items)) {
107 foreach($obj->items as $o) {
108 if (!empty($o->{'*prop'})) {
110 unset($o->{'*prop'});
111 $this->mungePropsObj($o);
116 if (isset($o->xtype) && $o->xtype == 'Array') {
117 if (empty($o->items)) {
121 $obj->$pn = $o->items;
128 // otherwise is a standard item..
129 $this->mungePropsObj($o);
131 //if ($o->xtype == 'GridField') {
137 $obj->items = $items;
142 if (!empty($obj->xtype) && ($obj->xtype[0] == '*')) {
152 "//<script type=\"text/javascript\">",
154 "// Version " . date("Ymd-His"),
156 "// Auto generated file - do not edit directly",
157 "// created by Application Builder (Web version)",
162 function toJSFileDialog($obj, $json=false)
164 $out = implode("\n", $this->jsHead());
166 $module = $obj->{'|module'};
168 $out .= implode("\n", array(
174 " show : function(data, cb)",
176 " if (!this.dialog) {",
180 " this.callback = cb;",
181 " this.data = data;",
182 " this.dialog.show();",
184 " this.form.reset();",
185 " this.form.setValues(data);",
186 " this.form.fireEvent('actioncomplete', this.form, { type: 'setdata', data: data });",
191 " create : function()",
193 " var _this = this;",
194 " this.dialog = Roo.factory("
196 $this->mungePropsObj($obj->items[0]);
197 if (isset($region)) {
198 $obj->items[0]->region = $region;
201 $out .= $this->toJSObj($obj->items[0], 3); // should be first child really...
205 if (isset($obj->items[0]->layout)) {
206 $out .= implode("\n", array(
207 " this.layout = this.dialog.layout;",
213 $out .= implode("\n", array(
218 $out.=$this->jsonToComment($json);
227 function toJSFileLayout($obj, $json=false)
229 $out = implode("\n", $this->jsHead());
231 $module = $obj->{'|module'};
236 // initially we can get this from $ar
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)) {
247 $region = $obj->region;
248 $parent = $obj->{'|parent'};
250 $app_perm = strpos($perm, '.') > 0 ? $perm: "$app.$perm";
253 // I know this is dumb.. but otherwise indenting doesnt really work.
254 $out .= implode("\n", array(
258 "// register the module first",
259 "Pman.on('beforeload', function()",
265 $out .= implode("\n", array(
267 " if (Pman.Login.authUser.company_id_comptype != 'OWNER') {",
271 " if (!Pman.hasPerm('$app_perm', '$permtype')) {",
276 $dis = empty($obj->disabled) ? 'false' : 'true';
277 if (!empty($obj->{'|disabled'})) {
278 $dis = implode("\n ", explode("\n", $obj->{'|disabled'}));
280 $out .= implode("\n", array(
281 " var disabled = $dis;",
286 " modKey : '$modkey',",
287 " module : $module,",
288 " region : '$region',",
289 " parent : $parent,",
302 $out .= implode("\n", array(
303 "$module = new Roo.util.Observable({",
306 " disabled : false,",
307 " parentLayout: false,",
309 " add : function(parentLayout, region)",
312 " var _this = this;", // standard avaialbe..
313 " this.parentLayout = parentLayout;",
315 " this.panel = parentLayout.addxtype("
318 $this->mungePropsObj($obj->items[0]);
319 if (isset($region)) {
320 $obj->items[0]->region = $region;
323 $out .= $this->toJSObj($obj->items[0], 3); // should be first child really...
327 if (isset($obj->items[0]->layout)) {
328 $out .= implode("\n", array(
329 " this.layout = this.panel.layout;",
335 $out .= implode("\n", array(
340 $out.=$this->jsonToComment($json);
349 function jsonToComment($json)
351 if ($json === false) {
354 // not sure about \ either...
355 return implode("\n", array(
358 wordwrap(str_replace('*/', "*\n/",base64_encode(bzcompress($json))), 80, "\n", true) ,
368 function toJSFile($obj, $json=false)
372 if (empty($obj->{'|module'}) || (!empty($obj->disabled) && strlen($obj->disabled) == 1)) {
375 if ($obj->items[0]->xtype != 'LayoutDialog') {
376 return $this->toJSFileLayout($obj,$json);
378 return $this->toJSFileDialog($obj,$json);
394 function toJSObj($obj, $in)
398 $ix = str_repeat(" ", $in);
399 $ix1 = str_repeat(" ", $in-1);
400 foreach((array)$obj as $k=>$v) {
410 $out .= strlen($out) ? ",\n" : '';
414 $out .= $ix . "$k : " . $this->toJSObj($v, $in+1);
418 $out .= $ix . "$k : " . $this->toJSAr($v, $in+1);
421 case is_string($v) && ($k[0] == '|'):
422 $out .= $ix . substr($k,1). ": " . implode("\n" . $ix, explode("\n", $v));
426 $out .= $ix . "$k : " . $this->toJSPrim($v, isset($obj->xtype) ? $obj->xtype : '', $k);
430 // property is string/bool/num...
431 // strings can be '|' raw...
435 return "{\n" . $out . "\n" . $ix1 . "}";
438 function toJSPrim($v, $xtype, $k)
441 return ($v ? 'true' : 'false');
443 if (is_numeric($v)) {
446 if (!is_string($v)) {
447 return "'??? not string? " . gettype($v) ."'";
449 return $this->toString($xtype, $k, $v);
451 //return json_encode($v);
454 function toJSAr($ar, $in)
458 if (!is_object($o) && !is_array($o)) {
466 $xa = $this->toJSPrim($o);
468 return '['. implode(", ", $xa) .']';
472 $ix = str_repeat(" ", $in);
474 $out .= strlen($out) ? ",\n" : '';
477 $out .= $ix . $this->toJSObj($v, $in+1);
481 $out .= $ix . $this->toJSAr($v, $in+1);
484 // case is_string($v) && ($k[0] == '|'): // can not have raw in an array?
485 // $out .= $ix . . $v;
489 $out .= $this->toJSPrim($v); // we can not fix these...!!!!
494 return "[\n" . $out . "\n" . str_repeat(" ", $in-1) . "]";
498 function toString($xtype, $prop, $str)
502 if (in_array($prop , array(
515 return json_encode($str);
517 $str = json_encode($str);
518 $str= substr($str,1,-1);
519 return "'". str_replace("'", "\\'", $str) . "'";