silverstripe-userforms/code/model/submissions/SubmittedForm.php

148 lines
2.9 KiB
PHP
Raw Normal View History

<?php
/**
* Contents of an UserDefinedForm submission
*
* @package userforms
*/
class SubmittedForm extends DataObject {
2015-09-11 00:20:06 +02:00
private static $has_one = array(
"SubmittedBy" => "Member",
"Parent" => "UserDefinedForm",
);
2015-09-11 00:20:06 +02:00
private static $has_many = array(
"Values" => "SubmittedFormField"
);
private static $summary_fields = array(
'ID',
'Created'
);
2015-09-11 00:20:06 +02:00
/**
* Returns the value of a relation or, in the case of this form, the value
* of a given child {@link SubmittedFormField}
2015-09-11 00:20:06 +02:00
*
* @param string
2015-09-11 00:20:06 +02:00
*
* @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() {
2015-09-11 00:20:06 +02:00
$self = $this;
2015-09-11 00:20:06 +02:00
$this->beforeUpdateCMSFields(function($fields) use ($self) {
$fields->removeByName('Values');
$fields->dataFieldByName('SubmittedByID')->setDisabled(true);
2015-09-11 00:20:06 +02:00
$values = new GridField(
2015-09-11 00:20:06 +02:00
'Values',
'SubmittedFormField',
$self->Values()->sort('Created', 'ASC')
);
2015-09-11 00:20:06 +02:00
$config = new GridFieldConfig();
$config->addComponent(new GridFieldDataColumns());
$config->addComponent(new GridFieldExportButton());
$config->addComponent(new GridFieldPrintButton());
$values->setConfig($config);
2015-09-11 00:20:06 +02:00
$fields->addFieldToTab('Root.Main', $values);
});
2015-09-11 00:20:06 +02:00
$fields = parent::getCMSFields();
2015-09-11 00:20:06 +02:00
return $fields;
}
/**
* @param Member
*
* @return boolean
*/
public function canCreate($member = null) {
$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);
if($extended !== null) {
return $extended;
}
return $this->Parent()->canView();
}
/**
* @param Member
*
* @return boolean
*/
public function canEdit($member = null) {
$extended = $this->extendedCan(__FUNCTION__, $member);
if($extended !== null) {
return $extended;
}
return $this->Parent()->canEdit();
}
/**
* @param Member
*
* @return boolean
*/
public function canDelete($member = null) {
$extended = $this->extendedCan(__FUNCTION__, $member);
if($extended !== null) {
return $extended;
}
return $this->Parent()->canDelete();
}
/**
* Before we delete this form make sure we delete all the
* field values so that we don't leave old data round
*
* @return void
*/
protected function onBeforeDelete() {
if($this->Values()) {
foreach($this->Values() as $value) {
$value->delete();
}
}
2015-09-11 00:20:06 +02:00
parent::onBeforeDelete();
}
}