Skip to content

Commit

Permalink
adds export functionality to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed May 18, 2010
1 parent 56cd6bc commit 16a047b
Show file tree
Hide file tree
Showing 299 changed files with 230,405 additions and 0 deletions.
5 changes: 5 additions & 0 deletions apps/backend/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ sf_guard_user:
users:
url: /users
param: { module: users, action: index }

statistics:
url: /
param: { module: statistics, action: index }

export:
url: /export.:sf_format
param: { module: statistics, action: export }

# default rules
homepage:
Expand Down
31 changes: 31 additions & 0 deletions apps/backend/modules/statistics/actions/actions.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,35 @@ public function executeIndex(sfWebRequest $request)
$this->fulfilled_have_stuff = Doctrine::getTable('StuffResource')->getList('have', 1);
$this->fulfilled_need_stuff = Doctrine::getTable('StuffResource')->getList('need', 1);
}

public function executeExport($request)
{
$exportManager = sfExportManager::create($class);

$sheets = array(
'StuffResource' => array('have', 'need'),
'InfoResource' => array('have', 'need'),
'TimeResource' => array('need'));

foreach ($sheets as $class => $types)
{
$fields = Doctrine::getTable($class)->getColumnNames();
$fields = array_combine($fields, $fields);

foreach ($fields as $key => $value)
{
$fields[$key] = sfInflector::humanize($value);
}

foreach ($types as $type)
{
$results = Doctrine::getTable($class)->createQuery()->where('transaction_type = ?', $type)->execute();
$exportManager->exportCollectionSheet($results, $fields, sprintf('%s-%s', $class, $type));
}
}

$exportManager->output(sprintf('%s-Export', $class));

return sfView::NONE;
}
}
1 change: 1 addition & 0 deletions config/ProjectConfiguration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function setup()
'sfGoogleAnalyticsPlugin',
'sfLucenePlugin',
'sfTaskExtraPlugin',
'sfPhpExcelPlugin'
));
}

Expand Down
8 changes: 8 additions & 0 deletions config/phpexcel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
meta:
creator: Donate-Nashville
title: Donate Nashville Export
subject: Donate Nashville Export
description: Donate Nashville Export
keywords: Donate Nashville Export
category: Donate Nashville Export
151 changes: 151 additions & 0 deletions lib/api/sfExportManager.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

/**
*
*/
class sfExportManager
{
protected $_xls,
$_sheets = 0;

public function __construct($class = null, $title = null, $multisheet = false)
{
if ($class && !class_exists($class))
{
throw new sfException("Invalid class: $class. Class does not exist");
}

if (is_null($title))
{
$title = $this->getExportTitle();
}

$this->_xls = new sfPhpExcel();
$this->_xls->getProperties()->setTitle($title);
$this->_xls->getProperties()->setSubject($title);
$this->_xls->getProperties()->setDescription($title);
}

public static function create($class, $title = null, $multisheet = false)
{
$managerClass = sprintf('sfExportManager%s', sfInflector::camelize($class));
if (class_exists($managerClass))
{
return new $managerClass($class, $title, $multisheet);
}

return new self($class, $title, $multisheet);
}

public function initialize($params = array())
{
}

public function getExportTitle()
{
return 'Data Export';
}

public function filterColumns($fields, $ids = array())
{
return $fields;
}

public function exportField($object, $field)
{
return $this->exportObjectRowFieldDefault($object, $field);
}

public function exportObjectRowFieldDefault($object, $field)
{
return $object[$field];
}

/**
* exportCollectionSheet
*
* default sheet export for collection
*
* @param string $object
* @param string $fields
* @return void
* @author Brent Shaffer
*/
public function exportCollectionSheet($collection, $fields, $title = null)
{
if($this->_sheets > 0)
{
$workSheet = $this->_xls->createSheet();
}
else
{
$workSheet = $this->_xls->getActiveSheet();
}

$this->_sheets++;

$workSheet->setTitle($title ? $title : $this->getExportTitle());

// Initialize coordinate counters
$row = 1;
$col = 0;

foreach ($fields as $field => $label)
{
$workSheet->setCellValueByColumnAndRow($col, $row, $label);
$workSheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($col))->setAutoSize(true);
$col++;
}
$row++;

foreach ($collection as $record)
{
$col = 0;
foreach ($fields as $field => $label)
{
$workSheet->setCellValueByColumnAndRow($col, $row, $this->exportField($record, $field));
$col++;
}
$row++;
}
}

public function output($filename = 'export', $format = null)
{
if(is_null($format)) sfConfig::get('sf_environment') == 'test' ? $format = 'HTML' : $format = 'Excel5';

switch(strtolower($format))
{
case 'excel2007':
$content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$ext = 'xlsx';
break;
case 'csv':
$content_type = 'application/vnd.ms-excel';
$ext = 'csv';
break;
case 'html':
$content_type = 'text/html';
$ext = 'html';
break;
case 'pdf':
$content_type = 'application/pdf';
$ext = 'pdf';
break;
default:
$content_type = 'application/vnd.ms-excel';
$ext = 'xls';
}

// redirect output to client browser
if($format != 'HTML')
{
header('Content-Type: ' . $content_type);
header('Content-Disposition: attachment;filename="' . $filename . "." . $ext . '"');
header('Cache-Control: max-age=0');
}

$xlsWriter = PHPExcel_IOFactory::createWriter($this->_xls, $format);
$xlsWriter->save('php://output');
}
}
7 changes: 7 additions & 0 deletions plugins/sfPhpExcelPlugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2008,2009 Bertrand Zuchuat

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit 16a047b

Please sign in to comment.