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
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
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;
|
|
|
|
|
2010-04-08 23:18:01 +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;
|
|
|
|
|
2010-04-08 23:18:01 +02:00
|
|
|
foreach(DB::query("SELECT COUNT(*) AS \"CountRows\" FROM \"SubmittedForm\" WHERE \"ParentID\" = $formId") as $r) $totalCount = $r['CountRows'];
|
2009-07-02 23:18:16 +02:00
|
|
|
|
|
|
|
$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;
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
return $this->generateExport($SQL_ID);
|
2009-04-15 06:23:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
}
|
2009-12-07 03:04:20 +01:00
|
|
|
}
|