project. I am using the latest version of the csv helper found here:
http://ifunk.net/cakephp/helpers/csv.php.txt. I have this code as ../
app/views/helpers/csv.php. I also have added Router::parseExtensions
('json', 'xml', 'csv'); to my routes.php file.
In my ../app/controllers/repairorders_controller.php I have this
action defined:
function exportrepairorders() {
Configure::write('debug', '0');
$this->layout = 'ajax'; // use the Ajax layout
$data = array(); // hold the data from the database
/* Set up the "conditions" array to retrieve selected Repairorder
data */
$params = array(
'order' => array(
'Repairorder.servicedate DESC',
'Repairorder.distributor_id',
'Repairorder.dealer_id'
)
);
$paramsCount = array();
/* Set up for an export request, if requested */
$exportRequest = ($this->RequestHandler->ext == 'csv' ||
$this->RequestHandler->ext == 'xml' ||
($this->RequestHandler->ext == 'json' &&
!$this->RequestHandler->isAjax())
);
if ($exportRequest) {
if ($this->RequestHandler->ext == 'csv') {
$this->helpers[] = 'Csv';
}
if ($this->RequestHandler->ext == 'xml') {
// Do nothing, no helper needed
}
}
/* Retrieve count of selected Repairorder and associated data and
then the data */
$count = $this->Repairorder->find('count', $paramsCount);
$this->Repairorder->recursive = 0;
$data = $this->Repairorder->find('all', $params);
// Debugger::dump($data);
$this->set('total', $count); // send total to the view
$this->set('repairorders', $data); // send repairorders to the view
}
I have a corresponding view established in ../app/views/repairorders/
csv/exportrepairorders.ctp which looks like this:
<?php
Configure::write('debug', 0);
if (!$data['success']) {
$line ='Error occurred. ';
if (array_key_exists('msg', $data)) {
$line .= $data['msg'];
}
if (array_key_exists('errors', $data)) {
$line .= print_r($data['errors'], true);
}
$csv->addRow(array($line));
echo $csv->render('Repair Order Export ' . date('Y-m-d h:i') .
'.csv');
return;
}
if (!array_key_exists('results', $data) || !count($data['results']))
{
$csv->addRow('No data found.');
echo $csv->render('Repair Order Export ' . date('Y-m-d h:i') .
'.csv');
return;
}
$csv->addRow(array(
'Distributor Name',
'Dealer Name',
'Advisor Name',
'Date of Service',
'Repair Order Number',
'VIN',
'Year',
'Make',
'Model',
'Odometer',
'Kms/Miles',
'Basic LOF?',
'Premier LOF?',
'Performance LOF?',
'Transmission?',
'Brakes?',
'Front Diff?',
'Rear Diff?',
'Transaxle?',
'Power Steering?',
'Transfer Case?',
'Date Created'
)
);
foreach ($data['results'] as $result) {
$csv->addRow(
array(
$result['Distributor']['name'],
$result['Dealer']['name'],
$result['Advisor']['name'],
$result['Repairorder']['servicedate'],
$result['Repairorder']['number'],
$result['Repairorder']['vin'],
$result['Repairorder']['year'],
$result['Repairorder']['make'],
$result['Repairorder']['model'],
$result['Repairorder']['odometer'],
$result['Repairorder']['odometer_type'],
intval($result['Repairorder']['basic_lof']) ? 'Yes' : 'No',
intval($result['Repairorder']['premier_lof']) ? 'Yes' : 'No',
intval($result['Repairorder']['performance_lof']) ? 'Yes' : 'No',
intval($result['Repairorder']['transmission']) ? 'Yes' : 'No',
intval($result['Repairorder']['brakes']) ? 'Yes' : 'No',
intval($result['Repairorder']['front_diff']) ? 'Yes' : 'No',
intval($result['Repairorder']['rear_diff']) ? 'Yes' : 'No',
intval($result['Repairorder']['transaxle']) ? 'Yes' : 'No',
intval($result['Repairorder']['power_steering']) ? 'Yes' : 'No',
intval($result['Repairorder']['transfer_case']) ? 'Yes' : 'No',
$result['Repairorder']['created']
)
);
}
echo $csv->render('Repair Order Export ' . date('Y-m-d h:i') .
'.csv');
?>
My action is being called from an ExtJS grid button. The URL that is
being generated looks like 'http://' + host + '/repairorders/
exportrepairorders.csv'.
When I execute the button from the grid, I receive a 404 that the page
cannot be found. Does this mean that my routes.php entry is not being
executed? What am I missing here? Do I have the correct set up with
the view file being in a /csv/ sub-directory?
Thank you in advance for your kind assistance.
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
No comments:
Post a Comment