DataObjects/Builder.php
[Pman.Builder] / DataObjects / Builder_part.php
1 <?php
2 /**
3 CREATE TABLE `builder`.`builder_part` (
4   `id` int(11)  NOT NULL AUTO_INCREMENT,
5   `module_id` int(11)  NOT NULL,
6   `name` varchar(254)  NOT NULL,
7   `json` longtext  NOT NULL,
8   PRIMARY KEY (`id`)
9 )
10 ENGINE = MyISAM;
11 */
12 /**
13  * Table Definition for builder_app
14  */
15 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
16
17
18 class Pman_Builder_DataObjects_Builder_part extends DB_DataObject 
19 {
20     ###START_AUTOCODE
21     /* the code below is auto generated do not remove the above tag */
22
23     public $__table = 'builder_part';                         // table name
24     public $id;                              // int(11)  not_null primary_key auto_increment
25     public $name;                            // string(128)  not_null
26     public $json;                            // blob(-1)  not_null blob
27     public $jsource;                            // blob(-1)  not_null blob
28
29     public $module_id;                           // string(16)  not_null
30     public $updated;                           // string(16)  not_null
31  
32     
33     /* the code above is auto generated do not remove the tag below */
34     ###END_AUTOCODE
35     /*function applySort($au, $sort, $dir, $cols) 
36     {
37         // DB_DAtaObject::debugLevel(1);
38         $this->orderBy( 'builder.app asc, builder.module asc');
39         
40         
41     }
42     */
43     function applyFilters($q, $au)
44     {
45         //DB_DataObject::debugLEvel(1);
46         if (!empty($q['module_id'])) {
47             // we should only do this on a menu calll to sync. really..
48             $m = DB_DataObject::factory('builder_modules');
49             $m->get($q['module_id']);
50             $m->syncParts();
51         }
52        
53     }
54     
55     function module()
56     {
57         
58         $m = DB_DataObject::Factory('builder_modules');
59         $m->get($this->module_id);
60         return $m;
61         
62         
63     }
64     
65     function beforeDelete($deps)
66     {
67         
68         $m  = $this->module();
69         $m->gitCommitDelete($this->name . '.bjs');
70         $m->gitCommitDelete($this->name . '.js');
71        
72         
73     }
74     
75     
76     function onInsert($req,$roo)
77     {
78         // write it to a file... use date time...  - which should hopefully be the same as the
79         //event that was created..
80         $this->writeEventFile($roo,$req);
81         $this->syncTemplate();
82     }
83     function onUpdate($old , $req, $roo)
84     {
85         $this->writeEventFile($roo,$req);
86         $this->syncTemplate();
87         
88     }
89     function  writeEventFile($roo,$req) 
90     {
91         
92         // try and commit it..
93         
94         $m  = $this->module();
95         $m->gitCommit($this->name . '.bjs', $this->bjs());
96         $m->gitCommit($this->name . '.js', $this->jsource);
97         if (!empty($req['html'])) {
98            // $roo->jerr("write html? {$this->name}.html");
99             $m->gitCommit($this->name . '.html', $req['html']);
100         }
101         
102         // writes to session page?
103         $sp = ini_get('session.save_path').'/Builder/'.date('Y/m');
104         if (!file_exists($sp)) {
105             mkdir($sp, 0755, true);
106         }
107         $fn = $sp .'/'. $this->id . '-'. $this->name . '-'. date('d-H-i-s') . '.bjs';
108         require_once 'Services/JSON.php';
109         $json = Services_JSON::stringify(json_decode($this->json), null, 4);
110         
111         file_put_contents($fn , $json);
112     }
113     function bjs()
114     {
115         return $this->json;
116         $data = json_decode($this->json);
117         // munge data into bjs format..
118         unset($data->xtype);
119         $data->title = isset($data->title) ? $data->title : $data->name;
120         
121         
122         require_once 'Services/JSON.php';
123         return Services_JSON::stringify($data,null,4);
124         
125         
126         
127     }
128     
129     function writeCopy($path,$roo)
130     {
131         die("needs fixing");
132         if (!file_exists($path)) {
133             return;
134         }
135         
136        
137         require_once 'Pman/Builder/Code.php';
138         $x = new Pman_Builder_Code();
139         $this->_new_file = false;
140         if (!file_exists($path.'/'. $this->module.'.js')) {
141             $this->_new_file = true;
142         }
143         if (!$this->_new_file && !is_writable($path.'/'. $this->module.'.js')) {
144             $roo->jerr("Can not write to " . $path.'/'. $this->module.'.js');
145             return;
146         }
147         if ($this->_new_file && !is_writable($path)) {
148             $roo->jerr("Can not write to " . $path);
149             return;
150         }
151        
152         file_put_contents($path.'/'. $this->module.'.js', 
153                 $x->toJSFile(json_decode($this->json))) ;
154         return $path.'/'. $this->module.'.js';
155     }
156     
157     
158     function toRooSingleArray()
159     {
160        // echo '<PRE>';print_R($this);exit;
161      
162         $ret = $this->toArray();
163         
164         return $ret;
165     }
166     
167     function toRooArray()
168     {
169         $ret = $this->toArray();
170         
171     
172        
173         return $ret;
174         /*
175         // update return the code.!!!
176         if (isset($_SERVER["REQUEST_METHOD"]) &&  $_SERVER["REQUEST_METHOD"] == 'POST') {
177             require_once 'Pman/Builder/Code.php';
178             $x = new Pman_Builder_Code();
179             $ret['code'] = $x->toJSFile(json_decode($this->json), $this->json);
180         }
181          
182         return $ret;
183         */
184     }
185     function syncTemplate()
186     {
187         // possibly needs to be cleverer = if template is a dialog etc.?
188         
189         $m = $this->module();
190         $t = DB_DataObject::Factory('cms_template');
191         $t->view_name = $m->name;
192         $t->template = $this->name . '.html';
193         $t->lang= 'en';
194         if (!$t->find(true)) {
195             $t->updated = $t->sqlValue('NOW()');
196             $t->insert();
197             return;
198         }
199         $t->updated = $t->sqlValue('NOW()');
200         $t->update();
201         
202     }
203     
204 }