Skip to content

Commit

Permalink
Merge pull request #42 from creative-commoners/pulls/1.0/persistent-s…
Browse files Browse the repository at this point in the history
…ubmission-data

API Make submission items store persistent data instead of dynamic relationship
  • Loading branch information
sachajudd committed Jun 8, 2017
2 parents b78fb6e + 2bdcf7b commit 9af9b23
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 55 deletions.
18 changes: 10 additions & 8 deletions code/classes/DMSDocumentCart.php
Expand Up @@ -262,15 +262,17 @@ public function saveSubmission(Form $form)
$submission = DMSDocumentCartSubmission::create();
$form->saveInto($submission);
$return = $submission->write();
$this->getItems()->each(function ($row) use ($submission) {
$values = array(
'Quantity' => $row->getQuantity(),
'DocumentID' => $row->getDocument()->ID,
);
$submissionItem = DMSDocumentCartSubmissionItem::create($values);
$this->getItems()->each(function ($item) use ($submission) {
/** @var DMSDocument $document */
$document = $item->getDocument();
$submissionItem = DMSDocumentCartSubmissionItem::create(array(
'OriginalID' => $document->ID,
'Quantity' => $item->getQuantity(),
'Title' => $document->getTitle(),
'Filename' => $document->getFilenameWithoutID()
));
$submission->Items()->add($submissionItem);

$row->getDocument()->incrementPrintRequest();
$document->incrementPrintRequest();
});

return $return;
Expand Down
9 changes: 5 additions & 4 deletions code/controllers/DMSCheckoutController.php
Expand Up @@ -65,8 +65,9 @@ public function DMSDocumentRequestForm()
$fields->replaceField('DeliveryAddressLine2', TextField::create('DeliveryAddressLine2', ''));
$fields->replaceField('DeliveryAddressCountry', CountryDropdownField::create(
'DeliveryAddressCountry',
_t(__CLASS__ . '.RECEIVER_COUNTRY', 'Country')
_t('DMSCheckoutController.RECEIVER_COUNTRY', 'Country')
)->setValue('NZ'));
$fields->removeByName('CreatedAt');

$requiredFields = array(
'ReceiverName',
Expand All @@ -85,7 +86,7 @@ public function DMSDocumentRequestForm()
$actions = FieldList::create(
FormAction::create(
'doRequestSend',
_t(__CLASS__ . '.SEND_ACTION', 'Send your request')
_t('DMSCheckoutController.SEND_ACTION', 'Send your request')
)
);

Expand Down Expand Up @@ -123,7 +124,7 @@ public function send()
$email = Email::create(
$from,
$emailAddress,
_t(__CLASS__ . '.EMAIL_SUBJECT', 'Request for Printed Publications')
_t('DMSCheckoutController.EMAIL_SUBJECT', 'Request for Printed Publications')
);

if ($bcc = $this->getConfirmationBcc()) {
Expand All @@ -134,7 +135,7 @@ public function send()
$body = sprintf(
'<p>%s</p>',
_t(
__CLASS__ . '.EMAIL_BODY',
'DMSCheckoutController.EMAIL_BODY',
'A request for printed publications has been submitted with the following details:'
)
);
Expand Down
35 changes: 27 additions & 8 deletions code/models/DMSDocumentCartSubmission.php
Expand Up @@ -10,6 +10,7 @@ class DMSDocumentCartSubmission extends DataObject
'DeliveryAddressLine2' => 'Varchar(200)',
'DeliveryAddressCountry' => 'Varchar(50)',
'DeliveryAddressPostCode' => 'Varchar(20)',
'CreatedAt' => 'Datetime',
);

private static $has_many = array(
Expand All @@ -20,7 +21,8 @@ class DMSDocumentCartSubmission extends DataObject
'ReceiverName' => 'Receiver Name',
'ReceiverPhone' => 'Receiver Phone',
'ReceiverEmail' => 'Receiver Email',
'Items.Count' => 'No. Items'
'Items.Count' => 'No. Items',
'CreatedAt.Nice' => 'Created At'
);

private static $singular_name = 'Cart Submission';
Expand All @@ -34,15 +36,32 @@ class DMSDocumentCartSubmission extends DataObject
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
// PHP 5.3 support
$self = $this;
$this->beforeUpdateCMSFields(function (FieldList $fields) use ($self) {
$fields->addFieldToTab(
'Root.Main',
$fields->fieldByName('Root.Main.CreatedAt')->performReadonlyTransformation()
);

$gridField = GridField::create('Items', null, $this->Items(), $config = new GridFieldConfig_RecordEditor);
$fields->addFieldToTab('Root.Items', $gridField);
$gridField = GridField::create('Items', null, $self->Items(), $config = new GridFieldConfig_RecordEditor);
$fields->addFieldToTab('Root.Items', $gridField);

foreach (array('GridFieldAddExistingAutocompleter', 'GridFieldAddNewButton') as $component) {
$config->removeComponentsByType($component);
}
foreach (array('GridFieldAddExistingAutocompleter', 'GridFieldAddNewButton') as $component) {
$config->removeComponentsByType($component);
}
});
return parent::getCMSFields();
}

return $fields;
/**
* Set the created at datetime if it hasn't been set already
*/
public function onBeforeWrite()
{
if (!$this->CreatedAt) {
$this->CreatedAt = SS_Datetime::now();
}
return parent::onBeforeWrite();
}
}
27 changes: 19 additions & 8 deletions code/models/DMSDocumentCartSubmissionItem.php
Expand Up @@ -3,26 +3,37 @@
class DMSDocumentCartSubmissionItem extends DataObject
{
private static $db = array(
'OriginalID' => 'Int',
'Title' => 'Varchar(255)',
'Filename' => 'Varchar(255)',
'Quantity' => 'Int',
);

private static $has_one = array(
'Document' => 'DMSDocument',
'DMSDocumentCartSubmission' => 'DMSDocumentCartSubmission',
);

private static $summary_fields = array(
'Document.getTitle' => 'Document',
'Quantity' => 'Quantity'
);
private static $summary_fields = array('Title', 'Filename', 'Quantity');

private static $singular_name = 'Submission Item';
private static $plural_name = 'Submission Items';

public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('DMSDocumentCartSubmissionID');
return $fields;
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->removeByName('DMSDocumentCartSubmissionID');

$fields->addFieldToTab(
'Root.Main',
$fields->fieldByName('Root.Main.OriginalID')
->performReadonlyTransformation()
->setDescription(_t(
'DMSDocumentCartSubmissionItem.ORIGINALIDHELPTIP',
'Note: The original document may have been modified or removed since this request was made.'
)),
'Title'
);
});
return parent::getCMSFields();
}
}
2 changes: 2 additions & 0 deletions lang/en.yml
Expand Up @@ -20,3 +20,5 @@ en:
EMAIL_SUBJECT: Request for Printed Publications
RECEIVER_COUNTRY: Country
SEND_ACTION: Send your request
DMSDocumentCartSubmissionItem:
ORIGINALIDHELPTIP: 'Note: The original document may have been modified or removed since this request was made.'
10 changes: 5 additions & 5 deletions templates/includes/DocumentCart_email.ss
Expand Up @@ -4,33 +4,33 @@
<thead>
<tr>
<th colspan="2">
<%t DMSCart.DELIVERY_INFORMATION "Delivery Information" %>
<%t DMSDocumentCartSubmission.DELIVERY_INFORMATION "Delivery Information" %>
</th>
</tr>
</thead>
<tbody>
<% with $ReceiverInfoNice %>
<tr>
<td>
<%t DMSCart.RECEIVER_NAME "Name" %>
<%t DMSDocumentCartSubmission.RECEIVER_NAME "Name" %>
</td>
<td>$ReceiverName.XML</td>
</tr>
<tr>
<td>
<%t DMSCart.RECEIVER_PHONE "Phone" %>
<%t DMSDocumentCartSubmission.RECEIVER_PHONE "Phone" %>
</td>
<td>$ReceiverPhone.XML</td>
</tr>
<tr>
<td>
<%t DMSCart.RECEIVER_EMAIL "Email" %>
<%t DMSDocumentCartSubmission.RECEIVER_EMAIL "Email" %>
</td>
<td>$ReceiverEmail.XML</td>
</tr>
<tr>
<td>
<%t DMSCart.RECEIVER_ADDRESS "Shipping Address" %>
<%t DMSDocumentCartSubmission.RECEIVER_ADDRESS "Shipping Address" %>
</td>
<td>
$DeliveryAddressLine1.XML<br/>
Expand Down
30 changes: 8 additions & 22 deletions templates/includes/RequestItems_email.ss
@@ -1,39 +1,25 @@
<table title="<%t DMSCart.TABLE_TITLE "Requested documents" %>" style="text-align:left; width:100%;">
<table title="<%t DMSDocumentCartSubmissionItem.TABLE_TITLE "Requested documents" %>" style="text-align:left; width:100%;">
<thead>
<tr>
<th>
<%t DMSCart.TITLE "Title" %>
<%t DMSDocumentCartSubmissionItem.TITLE "Title" %>
</th>
<th>
<%t DMSCart.FILE_NAME "File name" %>
<%t DMSDocumentCartSubmissionItem.FILE_NAME "File name" %>
</th>
<th>
<%t DMSCart.TOTAL_COPIES "# of copies" %>
</th>
<th>
<%t DMSCart.TYPE "Type" %>
</th>
<th>
<%t DMSCart.SIZE "Size" %>
</th>
<th>
<%t DMSCart.URL "URL" %>
<%t DMSDocumentCartSubmissionItem.TOTAL_COPIES "# of copies" %>
</th>
</tr>
</thead>
<tbody>
<% loop $Items %>
<tr>
<% with $Document %>
<td>$Title</td>
<td>$FilenameWithoutID</td>
<td>$Up.Quantity</td>
<td>$Extension.UpperCase</td>
<td>$Size</td>
<td>
<a href="$Link"><%t DMSCart.VIEW_DOCUMENT "View Document" %></a>
</td>
<% with $getDocument %>
<td>$Title</td>
<td>$FilenameWithoutID</td>
<% end_with %>
<td>$Quantity</td>
</tr>
<% end_loop %>
</tbody>
Expand Down
11 changes: 11 additions & 0 deletions tests/models/DMSDocumentCartSubmissionItemTest.php
Expand Up @@ -10,4 +10,15 @@ public function testNoParentIdField()
$fields = DMSDocumentCartSubmissionItem::create()->getCMSFields();
$this->assertNull($fields->fieldByName('DMSDocumentCartSubmissionID'));
}

/**
* The "original ID" field points to the initial ID of the document that was ordered at the time it was placed.
* This can easily change, so test that we have a helptip to say this. It should also be read only.
*/
public function testOriginalIdFieldHasHelptipAndIsReadonly()
{
$field = DMSDocumentCartSubmissionItem::create()->getCMSFields()->fieldByName('Root.Main.OriginalID');
$this->assertInstanceOf('ReadonlyField', $field);
$this->assertContains('The original document may have been modified', $field->getDescription());
}
}
25 changes: 25 additions & 0 deletions tests/models/DMSDocumentCartSubmissionTest.php
Expand Up @@ -2,6 +2,8 @@

class DMSDocumentCartSubmissionTest extends SapphireTest
{
protected $usesDatabase = true;

/**
* Test that the add new and existing GridField components have been removed
*/
Expand All @@ -15,4 +17,27 @@ public function testNoAddNewOrExistingButtons()
$this->assertNull($config->getComponentByType('GridFieldAddExistingAutocompleter'));
$this->assertNull($config->getComponentByType('GridFieldAddNewButton'));
}

/**
* Ensure that a "created at" date is set when a record is created
*/
public function testCreatedAtIsSetOnWrite()
{
$submission = DMSDocumentCartSubmission::create();
$this->assertNull($submission->CreatedAt);

$submission->write();
$this->assertNotNull($submission->CreatedAt);
}

/**
* Ensure that the scaffolded "created at" is readonly in the CMS
*/
public function testCreatedAtIsReadonly()
{
$submission = DMSDocumentCartSubmission::create();
$submission->write();
$fields = $submission->getCMSFields();
$this->assertInstanceOf('DatetimeField_Readonly', $fields->fieldByName('Root.Main.CreatedAt'));
}
}

0 comments on commit 9af9b23

Please sign in to comment.