3 require_once 'Pman/Roo.php'
5 class Pman_Core_Export_Roo extends Pman_Roo {
7 static $cli_desc = "Export a roo query to a files use like url Core/Export/Roo/Core_enum (CSV)";
9 static $cli_opts = array(
11 'desc' => 'File to export to. (absolute path)',
18 'desc' => 'argument.. eg. --query sort=id&direction=ASC&parent_id=1 ',
26 'desc' => 'user to log in as..',
38 $ff = HTML_FlexyFramework::get();
50 die("should not happen!");
54 function get($tab, $opts = Array())
57 if ($opts['file'][0] != '/') {
58 $this->jerr("file must be an absolute path ");
62 if (!empty($opts['query'])) {
63 parse_str($opts['query'], $args);
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']);
73 //$this->init(); // from pman.
74 //DB_DataObject::debuglevel(1);
75 //HTML_FlexyFramework::get()->generateDataobjectsCache($this->isDev);
81 if (!empty($args['_debug'])) {
82 DB_DAtaObject::debuglevel((int)$args['_debug']);
87 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
89 $tab = array_shift(explode('/', $tab));
90 $x = $this->dataObject($tab);
92 $_columns = !empty($args['_columns']) ? explode(',', $args['_columns']) : false;
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'])
104 $this->setFilters($x,$args);
107 $total = $x->count($this->countWhat);
110 //var_dump($total);exit;
111 $this->applySort($x);
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...
120 $queryObj = clone($x);
121 //DB_DataObject::debuglevel(1);
126 if (false === $res) {
127 $this->jerr($x->_lastError->toString());
135 $rooar = method_exists($x, 'toRooArray');
136 $_columnsf = $_columns ? array_flip($_columns) : false;
137 while ($x->fetch()) {
139 $add = $rooar ? $x->toRooArray($args) : $x->toArray();
140 if ($add === false) {
143 $ret[] = !$_columns ? $add : array_intersect_key($add, $_columnsf);
149 if (method_exists($queryObj ,'postListExtra')) {
150 $extra = $queryObj->postListExtra($args, $this);
154 // filter results, and add any data that is needed...
155 if (method_exists($x,'postListFilter')) {
156 $ret = $x->postListFilter($ret, $this->authUser, $args);
159 $args['csvCols'] = isset($args['csvCols']) ? $args['csvCols'] : '*';
160 $args['csvTitles'] = isset($args['csvTitles']) ? $args['csvTitles'] : '*';
163 $this->toCsv($ret, $args['csvCols'], $args['csvTitles'], $opts['file'] );
164 $this->jerr("we should not get here...");
170 function checkDebug($req = false)
176 // min requirement is just to list csvCols ...
178 function toCsv($data, $cols, $titles, $filename, $addDate = true)
181 $this->sessionState(0); // turn off sessions - no locking..
183 $fh = fopen($filename,'w');
186 foreach($data as $x) {
187 //echo "<PRE>"; print_r(array($_REQUEST['csvCols'], $x->toArray())); exit;
190 $titles = $cols== '*' ? array_keys($x) : $cols;
193 $cols= array_keys($x);
195 if ($titles !== false) {
196 fputcsv($fh,$titles);
199 //fputcsv($fh, $titles);
203 foreach($cols as $c) {
212 $this->jok("Wrote file : " . $filename);