2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\Submission;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldPrintButton;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\ReadonlyField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-10-09 04:05:41 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm;
|
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class SubmittedForm extends DataObject
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_one = [
|
|
|
|
'SubmittedBy' => Member::class,
|
2017-10-09 04:05:41 +02:00
|
|
|
'Parent' => DataObject::class,
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $has_many = [
|
|
|
|
'Values' => SubmittedFormField::class
|
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-15 05:30:59 +02:00
|
|
|
private static $cascade_deletes = [
|
|
|
|
'Values',
|
|
|
|
];
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $summary_fields = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'ID',
|
|
|
|
'Created'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'SubmittedForm';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-10-09 04:05:41 +02:00
|
|
|
public function requireDefaultRecords()
|
|
|
|
{
|
|
|
|
parent::requireDefaultRecords();
|
|
|
|
|
|
|
|
// make sure to migrate the class across (prior to v5.x)
|
|
|
|
DB::query("UPDATE SubmittedForm SET ParentClass = 'Page' WHERE ParentClass IS NULL");
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* Returns the value of a relation or, in the case of this form, the value
|
|
|
|
* of a given child {@link SubmittedFormField}
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function relField($fieldName)
|
|
|
|
{
|
|
|
|
// default case
|
|
|
|
if ($value = parent::relField($fieldName)) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check values for a form field with the matching name.
|
|
|
|
$formField = SubmittedFormField::get()->filter(array(
|
|
|
|
'ParentID' => $this->ID,
|
|
|
|
'Name' => $fieldName
|
|
|
|
))->first();
|
|
|
|
|
|
|
|
if ($formField) {
|
|
|
|
return $formField->getFormattedValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
2016-07-21 07:53:59 +02:00
|
|
|
$fields->removeByName('Values');
|
2017-06-17 02:34:15 +02:00
|
|
|
|
|
|
|
//check to ensure there is a Member to extract an Email from else null value
|
2017-08-11 02:37:03 +02:00
|
|
|
if ($this->SubmittedBy() && $this->SubmittedBy()->exists()) {
|
2017-08-11 01:33:06 +02:00
|
|
|
$submitter = $this->SubmittedBy()->Email;
|
2017-06-17 02:34:15 +02:00
|
|
|
} else {
|
|
|
|
$submitter = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//replace scaffolded field with readonly submitter
|
|
|
|
$fields->replaceField(
|
|
|
|
'SubmittedByID',
|
|
|
|
ReadonlyField::create(
|
|
|
|
'Submitter',
|
|
|
|
'Submitter',
|
|
|
|
$submitter
|
|
|
|
)
|
|
|
|
);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-07-16 08:39:03 +02:00
|
|
|
$values = GridField::create(
|
2016-07-21 07:53:59 +02:00
|
|
|
'Values',
|
2017-08-09 01:55:09 +02:00
|
|
|
SubmittedFormField::class,
|
2017-08-11 01:33:06 +02:00
|
|
|
$this->Values()->sort('Created', 'ASC')
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
|
2017-06-27 01:03:32 +02:00
|
|
|
$exportColumns = array(
|
|
|
|
'Title' => 'Title',
|
|
|
|
'ExportValue' => 'Value'
|
|
|
|
);
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
$config = new GridFieldConfig();
|
|
|
|
$config->addComponent(new GridFieldDataColumns());
|
2017-06-27 01:03:32 +02:00
|
|
|
$config->addComponent(new GridFieldExportButton('after', $exportColumns));
|
2016-07-21 07:53:59 +02:00
|
|
|
$config->addComponent(new GridFieldPrintButton());
|
|
|
|
$values->setConfig($config);
|
|
|
|
|
|
|
|
$fields->addFieldToTab('Root.Main', $values);
|
|
|
|
});
|
|
|
|
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-08-11 02:36:28 +02:00
|
|
|
public function canCreate($member = null, $context = [])
|
2016-07-21 07:53:59 +02:00
|
|
|
{
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
|
|
|
return $this->Parent()->canCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canView($member = null)
|
|
|
|
{
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2017-10-15 23:13:14 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2017-10-15 23:13:14 +02:00
|
|
|
|
|
|
|
if ($this->Parent()) {
|
|
|
|
return $this->Parent()->canView($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::canView($member);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null)
|
|
|
|
{
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2017-10-15 23:13:14 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2017-10-15 23:13:14 +02:00
|
|
|
|
|
|
|
if ($this->Parent()) {
|
|
|
|
return $this->Parent()->canEdit($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::canEdit($member);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Member
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canDelete($member = null)
|
|
|
|
{
|
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member);
|
2017-10-15 23:13:14 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($extended !== null) {
|
|
|
|
return $extended;
|
|
|
|
}
|
2017-10-15 23:13:14 +02:00
|
|
|
|
|
|
|
if ($this->Parent()) {
|
|
|
|
return $this->Parent()->canDelete($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::canDelete($member);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-15 23:13:14 +02:00
|
|
|
* Before we delete this form make sure we delete all the field values so
|
|
|
|
* that we don't leave old data round.
|
2016-07-21 07:53:59 +02:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function onBeforeDelete()
|
|
|
|
{
|
|
|
|
if ($this->Values()) {
|
|
|
|
foreach ($this->Values() as $value) {
|
|
|
|
$value->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::onBeforeDelete();
|
|
|
|
}
|
2013-05-01 00:49:57 +02:00
|
|
|
}
|