2008-09-29 05:18:23 +02:00
< ? php
/**
* Displays a summary of instances of a form submitted to the website
2009-04-15 00:59:46 +02:00
*
* @ package userforms
2008-09-29 05:18:23 +02:00
*/
class SubmittedFormReportField extends FormField {
function Field () {
Requirements :: css ( SAPPHIRE_DIR . " /css/SubmittedFormReportField.css " );
2009-04-15 06:23:43 +02:00
Requirements :: javascript ( " userforms/javascript/UserForm.js " );
2008-09-29 05:18:23 +02:00
return $this -> renderWith ( " SubmittedFormReportField " );
}
2009-04-15 00:59:46 +02:00
/**
* Return the submissions from the site
*
* @ return ComponentSet
*/
2008-09-29 05:18:23 +02:00
function Submissions () {
2009-07-02 23:18:16 +02:00
$pageStart = isset ( $_REQUEST [ 'start' ]) && is_numeric ( $_REQUEST [ 'start' ]) ? $_REQUEST [ 'start' ] : 0 ;
$pageLength = 10 ;
2009-07-22 05:34:27 +02:00
$items = $this -> form -> getRecord () -> getComponents ( 'Submissions' , null , " Created DESC " , null , " $pageStart , $pageLength " );
2009-07-02 23:18:16 +02:00
$formId = $this -> form -> getRecord () -> ID ;
foreach ( DB :: query ( " SELECT COUNT(*) AS CountRows FROM SubmittedForm WHERE ParentID = $formId " ) as $r ) $totalCount = $r [ 'CountRows' ];
$items -> setPageLimits ( $pageStart , $pageLength , $totalCount );
$items -> NextStart = $pageStart + $pageLength ;
$items -> PrevStart = $pageStart - $pageLength ;
$items -> Start = $pageStart ;
$items -> StartPlusOffset = $pageStart + $pageLength ;
$items -> TotalCount = $totalCount ;
return $items ;
2008-09-29 05:18:23 +02:00
}
2008-09-29 07:33:43 +02:00
2009-07-02 23:18:16 +02:00
function getSubmissions () {
return $this -> customise ( array (
'Submissions' => $this -> Submissions ()
)) -> renderWith ( array ( 'SubmittedFormReportField' ));
}
2009-04-15 00:59:46 +02:00
/**
2009-04-15 06:23:43 +02:00
* ID of this forms record
2009-04-15 00:59:46 +02:00
*
2009-04-15 06:23:43 +02:00
* @ return int
2009-04-15 00:59:46 +02:00
*/
2009-04-15 06:23:43 +02:00
function RecordID () {
return $this -> form -> getRecord () -> ID ;
2008-09-29 07:33:43 +02:00
}
2009-04-15 00:59:46 +02:00
/**
2009-04-15 06:23:43 +02:00
* Export each of the form submissions for this UserDefinedForm
* instance into a CSV file .
2009-04-15 00:59:46 +02:00
*
2009-04-15 06:23:43 +02:00
* In order to run this export function , the user must be
* able to edit the page , so we check canEdit ()
*
* @ return HTTPResponse / bool
2009-04-15 00:59:46 +02:00
*/
2009-04-15 06:23:43 +02:00
public function export () {
2009-04-20 01:22:22 +02:00
$now = Date ( " Y-m-d_h.i.s " );
2009-04-15 06:23:43 +02:00
$fileName = " export- $now .csv " ;
$separator = " , " ;
// Get the UserDefinedForm to export data from the URL
$SQL_ID = ( isset ( $_REQUEST [ 'id' ])) ? Convert :: raw2sql ( $_REQUEST [ 'id' ]) : false ;
if ( $SQL_ID ) {
$udf = DataObject :: get_by_id ( " UserDefinedForm " , $SQL_ID );
if ( $udf ) {
$submissions = $udf -> Submissions ();
if ( $submissions && $submissions -> Count () > 0 ) {
// Get all the submission IDs (so we know what names/titles to get - helps for sites with many UDF's)
$inClause = array ();
foreach ( $submissions as $submission ) {
$inClause [] = $submission -> ID ;
}
// Get the CSV header rows from the database
2009-05-12 03:48:04 +02:00
if ( defined ( 'Database::USE_ANSI_SQL' )) {
2009-08-13 02:19:20 +02:00
$tmp = DB :: query ( " SELECT DISTINCT \" Name \" , \" Title \" FROM \" SubmittedFormField \" LEFT JOIN \" SubmittedForm \" ON \" SubmittedForm \" . \" ID \" = \" SubmittedFormField \" . \" ParentID \" WHERE \" SubmittedFormField \" . \" ParentID \" IN ( " . implode ( ',' , $inClause ) . " ) ORDER BY \" SubmittedFormField \" . \" ID \" " );
2009-05-12 03:48:04 +02:00
} else {
2009-08-13 02:19:20 +02:00
$tmp = DB :: query ( " SELECT DISTINCT `Name`, `Title` FROM `SubmittedFormField` LEFT JOIN `SubmittedForm` ON `SubmittedForm`.`ID` = `SubmittedFormField`.`ParentID` WHERE `SubmittedFormField`.`ParentID` IN ( " . implode ( ',' , $inClause ) . " ) ORDER BY `SubmittedFormField`.`ID` " );
2009-05-12 03:48:04 +02:00
}
2009-04-15 06:23:43 +02:00
// Sort the Names and Titles from the database query into separate keyed arrays
foreach ( $tmp as $array ) {
$csvHeaderNames [] = $array [ 'Name' ];
$csvHeaderTitle [] = $array [ 'Title' ];
}
// For every submission...
$i = 0 ;
foreach ( $submissions as $submission ) {
// Get the rows for this submission (One row = one form field)
$dataRow = $submission -> FieldValues ();
$rows [ $i ] = array ();
// For every row/field, get all the columns
foreach ( $dataRow as $column ) {
// If the Name of this field is in the $csvHeaderNames array, get an array of all the places it exists
if ( $index = array_keys ( $csvHeaderNames , $column -> Name )) {
if ( is_array ( $index )) {
// Set the final output array for each index that we want to insert this value into
foreach ( $index as $idx ) {
$rows [ $i ][ $idx ] = $column -> Value ;
}
}
}
}
$i ++ ;
}
// CSV header row
$csvData = '"' . implode ( '","' , $csvHeaderTitle ) . '"' . " \n " ;
// For every row of data (one form submission = one row)
foreach ( $rows as $row ) {
// Loop over all the names we can use
for ( $i = 0 ; $i < count ( $csvHeaderNames ); $i ++ ) {
2009-07-10 06:51:24 +02:00
if ( ! isset ( $row [ $i ]) || ! $row [ $i ]) $csvData .= '"",' ; // If there is no data for this column, output it as blank instead
else $csvData .= '"' . str_replace ( '"' , '\"' , $row [ $i ]) . '",' ;
2009-04-15 06:23:43 +02:00
}
// Start a new row for each submission
$csvData .= " \n " ;
}
} else {
user_error ( " No submissions to export. " , E_USER_ERROR );
}
HTTPRequest :: send_file ( $csvData , $fileName ) -> output ();
} else {
user_error ( " ' $SQL_ID ' is a valid type, but we can't find a UserDefinedForm in the database that matches the ID. " , E_USER_ERROR );
}
} else {
user_error ( " ' $SQL_ID ' is not a valid UserDefinedForm ID. " , E_USER_ERROR );
}
}
/**
* Delete all the submissions listed in the user defined form
*
* @ return Redirect | Boolean
*/
public function deletesubmissions () {
$SQL_ID = ( isset ( $_REQUEST [ 'id' ])) ? Convert :: raw2sql ( $_REQUEST [ 'id' ]) : false ;
if ( $SQL_ID ) {
$udf = DataObject :: get_by_id ( " UserDefinedForm " , $SQL_ID );
$submissions = $udf -> Submissions ();
if ( $submissions ) {
foreach ( $submissions as $submission ) {
// delete the submission @see $submission->onBeforeDelete() for more info
$submission -> delete ();
}
return ( Director :: is_ajax ()) ? true : Director :: redirectBack ();
}
}
return ( Director :: is_ajax ()) ? false : Director :: redirectBack ();
}
/**
* Delete a given submission from a user defined form
*
* @ return Redirect | Boolean
*/
public function deletesubmission () {
$SQL_ID = ( isset ( $_REQUEST [ 'id' ])) ? Convert :: raw2sql ( $_REQUEST [ 'id' ]) : false ;
if ( $SQL_ID ) {
$submission = DataObject :: get_by_id ( " SubmittedForm " , $SQL_ID );
if ( $submission ) {
$submission -> delete ();
return ( Director :: is_ajax ()) ? true : Director :: redirectBack ();
}
}
return ( Director :: is_ajax ()) ? false : Director :: redirectBack ();
2009-04-15 00:59:46 +02:00
}
2008-09-29 05:18:23 +02:00
}
?>