DataObjects/Builder.php
[Pman.Builder] / DataObjects / Builder.php
1 <?php
2 /**
3  * Table Definition for builder
4  */
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
6
7 class Pman_Builder_DataObjects_Builder extends DB_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'builder';                         // table name
13     public $id;                              // int(11)  not_null primary_key auto_increment
14     public $name;                            // string(128)  not_null
15     public $json;                            // blob(-1)  not_null blob
16     public $btype;                           // string(16)  not_null
17     public $app;                             // string(64)  not_null
18     public $module;                          // string(128)  not_null
19
20     
21     /* the code above is auto generated do not remove the tag below */
22     ###END_AUTOCODE
23     function applySort($au, $sort, $dir, $cols) 
24     {
25         // DB_DAtaObject::debugLevel(1);
26         $this->orderBy( 'builder.app asc, builder.module asc');
27         
28         
29     }
30
31     
32     function onInsert($req,$roo)
33     {
34         if (!empty($req['gitpath']) && !empty($this->module)) {
35             $this->writeCopy($req['gitpath'],$roo);
36         }
37     }
38     function onUpdate($old , $req, $roo)
39     {
40         if (!empty($req['gitpath']) && !empty($this->module)) {
41             $file = $this->writeCopy($req['gitpath'], $roo);
42             $this->gitCommit($file );
43         }
44         
45         
46     }
47     function gitCommit($file )
48     {
49         $n = basename($file);
50         $dir = dirname($file);
51         chdir($dir);
52         if ($this->_new_file) {
53             $cmd = "git add " . escapeshellarg($n);
54             `$cmd`;
55         }
56         
57         $cmd = "git commit -m 'commit by builder' " . escapeshellarg($n);
58         `$cmd`;
59         $cmd = "git push";
60         `$cmd`;
61     }
62     
63     
64     function writeCopy($path,$roo)
65     {
66        
67         if (!file_exists($path)) {
68             return;
69         }
70         
71        
72         require_once 'Pman/Builder/Code.php';
73         $x = new Pman_Builder_Code();
74         $this->_new_file = false;
75         if (!file_exists($path.'/'. $this->module.'.js')) {
76             $this->_new_file = true;
77         }
78         if (!$this->_new_file && !is_writable($path.'/'. $this->module.'.js')) {
79             $roo->jerr("Can not write to " . $path.'/'. $this->module.'.js');
80             return;
81         }
82         if ($this->_new_file && !is_writable($path)) {
83             $roo->jerr("Can not write to " . $path);
84             return;
85         }
86        
87         file_put_contents($path.'/'. $this->module.'.js', 
88                 $x->toJSFile(json_decode($this->json))) ;
89         return $path.'/'. $this->module.'.js';
90     }
91     function toRooArray()
92     {
93         $ret = $this->toArray();
94         // update return the code.!!!
95         if (isset($_SERVER["REQUEST_METHOD"]) &&  $_SERVER["REQUEST_METHOD"] == 'POST') {
96             require_once 'Pman/Builder/Code.php';
97             $x = new Pman_Builder_Code();
98             $ret['code'] = $x->toJSFile(json_decode($this->json), $this->json);
99         }
100         
101         
102         return $ret;
103     }
104 }