check what is being written to our log files.
[Pman.Core] / Export / Roo.php
1 <?php
2
3 require_once 'Pman/Roo.php'
4 ;
5 class Pman_Core_Export_Roo extends Pman_Roo {
6     
7      static $cli_desc = "Export a roo query to a files  use like url Core/Export/Roo/Core_enum  (CSV)"; 
8     
9     static $cli_opts = array(
10         'file' => array(
11             'desc' => 'File to export to. (absolute path)',
12             'short' => 'f',
13             'min' => 1,
14             'max' => 1,
15             
16         ),
17         'query' => array(
18             'desc' => 'argument.. eg. --query sort=id&direction=ASC&parent_id=1  ',
19             'short' => 'q',
20             'default' => '',
21             'min' => 0,
22             'max' => 1,
23             
24         ),
25         'user' => array(
26             'desc' => 'user to log in as..',
27             'short' => 'u',
28             'default' => '',
29             'min' => 1,
30             'max' => 1,
31             
32         ),
33         
34     );
35     
36     function getAuth()
37     {
38         $ff = HTML_FlexyFramework::get();
39         
40         if (!$ff->cli) {
41             die("cli only");
42         }
43        
44         
45     } 
46     
47     
48     function post($v)
49     {
50         die("should not happen!");
51     }
52     
53    
54     function get($tab, $opts = Array())
55     {
56   
57         if ($opts['file'][0] != '/') {
58             $this->jerr("file must be an absolute path ");
59         }
60         
61         $args = array();
62         if (!empty($opts['query'])) {
63             parse_str($opts['query'], $args);
64         }
65         
66         $this->authUser = DB_DAtaObject::Factory('Person');
67         if (!$this->authUser->get('email', $opts['user'])) {
68             $this->jerr("user count not be found: " . $opts['user']);
69             
70         }
71         
72          
73         //$this->init(); // from pman.
74         //DB_DataObject::debuglevel(1);
75         //HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
76         
77    
78         
79         // debugging...
80         
81         if (!empty($args['_debug'])) {
82             DB_DAtaObject::debuglevel((int)$args['_debug']);
83         }
84         
85          
86         
87         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
88    
89         $tab = array_shift(explode('/', $tab));
90         $x = $this->dataObject($tab);
91         
92         $_columns = !empty($args['_columns']) ? explode(',', $args['_columns']) : false;
93         
94         
95         
96         // sets map and countWhat
97         $this->loadMap($x, array(
98                     'columns' => $_columns,
99                     'distinct' => empty($args['_distinct']) ? false:  $args['_distinct'],
100                     'exclude' => empty($args['_exclude_columns']) ? false:  explode(',', $args['_exclude_columns'])
101             ));
102         
103         
104         $this->setFilters($x,$args);
105         
106          
107         $total = $x->count($this->countWhat);
108         // sorting..
109       //   
110         //var_dump($total);exit;
111         $this->applySort($x);
112         
113         
114  
115         $x->limit(
116             empty($args['start']) ? 0 : (int)$args['start'],
117             min(empty($args['limit']) ? 25 : (int)$args['limit'], 10000000) // we can handle alot at the command line...
118         );
119         
120         $queryObj = clone($x);
121         //DB_DataObject::debuglevel(1);
122         
123         
124         $res = $x->find();
125        
126         if (false === $res) {
127             $this->jerr($x->_lastError->toString());
128             
129         }
130         
131         
132         
133         $ret = array();
134        
135         $rooar = method_exists($x, 'toRooArray');
136         $_columnsf = $_columns  ? array_flip($_columns) : false;
137         while ($x->fetch()) {
138             //print_R($x);exit;
139             $add = $rooar  ? $x->toRooArray($args) : $x->toArray();
140             if ($add === false) {
141                 continue;
142             }
143             $ret[] =  !$_columns ? $add : array_intersect_key($add, $_columnsf);
144         }
145         
146         
147         
148         $extra = false;
149         if (method_exists($queryObj ,'postListExtra')) {
150             $extra = $queryObj->postListExtra($args, $this);
151         }
152         
153         
154         // filter results, and add any data that is needed...
155         if (method_exists($x,'postListFilter')) {
156             $ret = $x->postListFilter($ret, $this->authUser, $args);
157         }
158         
159         $args['csvCols'] = isset($args['csvCols']) ? $args['csvCols'] : '*';
160         $args['csvTitles'] = isset($args['csvTitles']) ? $args['csvTitles'] : '*';
161             
162         
163         $this->toCsv($ret, $args['csvCols'], $args['csvTitles'],  $opts['file']  );
164         $this->jerr("we should not get here...");
165         
166         
167         
168         
169     }
170     function checkDebug($req = false)
171     {
172         
173     
174         
175     }
176     // min requirement is just to list csvCols ...
177     
178     function toCsv($data, $cols, $titles, $filename, $addDate = true)
179     {
180           
181         $this->sessionState(0); // turn off sessions  - no locking..
182
183         $fh = fopen($filename,'w');
184                     
185         
186         foreach($data as $x) {
187             //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
188             $line = array();
189             if ($titles== '*') {
190                 $titles = $cols== '*' ? array_keys($x) : $cols;
191             }
192             if ($cols== '*') {
193                 $cols= array_keys($x);
194             }
195             if ($titles !== false) {
196                 fputcsv($fh,$titles);
197                 
198                 
199                 //fputcsv($fh, $titles);
200                 $titles = false;
201             }
202             $ar = array();
203             foreach($cols as $c) {
204                 $ar[] = $x[$c];
205             }
206             fputcsv($fh,$ar);
207             
208             
209         }
210         
211         fclose($fh);
212         $this->jok("Wrote file : " . $filename);
213         exit;
214     
215         
216         
217     }
218     
219     
220 }