Skip to content
Snippets Groups Projects
Commit bbc063ec authored by Peter Rotich's avatar Peter Rotich
Browse files

Support organization export

parent 7d0576e1
Branches
Tags
No related merge requests found
......@@ -149,6 +149,57 @@ class Export {
return false;
}
static function saveOrganizations($sql, $filename, $how='csv') {
$exclude = array('name');
$form = OrganizationForm::getDefaultForm();
$fields = $form->getExportableFields($exclude);
// Field selection callback
$fname = function ($f) {
return 'cdata.`'.$f->getSelectName().'` AS __field_'.$f->get('id');
};
$sql = substr_replace($sql,
','.implode(',', array_map($fname, $fields)).' ',
strpos($sql, 'FROM '), 0);
$sql = substr_replace($sql,
'LEFT JOIN ('.$form->getCrossTabQuery($form->type, '_org_id', $exclude).') cdata
ON (cdata._org_id = org.id) ',
strpos($sql, 'WHERE '), 0);
$cdata = array_combine(array_keys($fields),
array_values(array_map(
function ($f) { return $f->get('label'); }, $fields)));
ob_start();
echo self::dumpQuery($sql,
array(
'name' => 'Name',
'account_manager' => 'Account Manager',
'users' => 'Users'
) + $cdata,
$how,
array('modify' => function(&$record, $keys) use ($fields) {
foreach ($fields as $k=>$f) {
if ($f && ($i = array_search($k, $keys)) !== false) {
$record[$i] = $f->export($f->to_php($record[$i]));
}
}
return $record;
})
);
$stuff = ob_get_contents();
ob_end_clean();
if ($stuff)
Http::download($filename, "text/$how", $stuff);
return false;
}
}
class ResultSetExporter {
......
......@@ -3,9 +3,15 @@ if(!defined('OSTSCPINC') || !$thisstaff) die('Access Denied');
$qstr='';
$select = 'SELECT org.* ';
$from = 'FROM '.ORGANIZATION_TABLE.' org ';
$select = 'SELECT org.*
,COALESCE(team.name,
IF(staff.staff_id, CONCAT_WS(" ", staff.firstname, staff.lastname), NULL)
) as account_manager ';
$from = 'FROM '.ORGANIZATION_TABLE.' org '
.'LEFT JOIN '.STAFF_TABLE.' staff ON (
LEFT(org.manager, 1) = "s" AND staff.staff_id = SUBSTR(org.manager, 2)) '
.'LEFT JOIN '.TEAM_TABLE.' team ON (
LEFT(org.manager, 1) = "t" AND team.team_id = SUBSTR(org.manager, 2)) ';
$where = ' WHERE 1 ';
......@@ -61,6 +67,8 @@ $from .= ' LEFT JOIN '.USER_TABLE.' user ON (user.org_id = org.id) ';
$query="$select $from $where GROUP BY org.id ORDER BY $order_by LIMIT ".$pageNav->getStart().",".$pageNav->getLimit();
//echo $query;
$qhash = md5($query);
$_SESSION['orgs_qs_'.$qhash] = $query;
?>
<h2>Organizations</h2>
<div style="width:700px; float:left;">
......@@ -125,7 +133,10 @@ else
</table>
<?php
if($res && $num): //Show options..
echo '<div>&nbsp;Page:'.$pageNav->getPageLinks().'&nbsp;</div>';
echo sprintf('<div>&nbsp;Page: %s &nbsp; <a class="no-pjax"
href="orgs.php?a=export&qh=%s">Export</a></div>',
$pageNav->getPageLinks(),
$qhash);
endif;
?>
</form>
......
......@@ -30,7 +30,19 @@ if ($_POST) {
$msg = "Successfully imported $status clients";
else
$errors['err'] = $status;
break;
default:
$errors['err'] = 'Unknown action';
}
} elseif ($_REQUEST['a'] == 'export') {
require_once(INCLUDE_DIR.'class.export.php');
$ts = strftime('%Y%m%d');
if (!($token=$_REQUEST['qh']))
$errors['err'] = 'Query token required';
elseif (!($query=$_SESSION['orgs_qs_'.$token]))
$errors['err'] = 'Query token not found';
elseif (!Export::saveOrganizations($query, "organizations-$ts.csv", 'csv'))
$errors['err'] = 'Internal error: Unable to export results';
}
$page = $org? 'org-view.inc.php' : 'orgs.inc.php';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment