Skip to content

Commit

Permalink
Merge pull request #33 from creative-commoners/issue/11
Browse files Browse the repository at this point in the history
NEW update button added to checkout process
  • Loading branch information
sachajudd committed Jun 1, 2017
2 parents e99b85f + 4b1384d commit 87a4b0c
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 98 deletions.
49 changes: 49 additions & 0 deletions code/classes/DMSDocumentCart.php
Expand Up @@ -12,6 +12,13 @@ class DMSDocumentCart extends ViewableData
*/
protected $backend;

/**
* Variable to control whether a cart is being updated or not
*
* @var bool
*/
private $viewOnly = false;

/**
* Instantiate a cart backend either by that provided, or a session default
*
Expand Down Expand Up @@ -247,4 +254,46 @@ public function saveSubmission(Form $form)

return $return;
}

/**
* Returns true if the cart is being updated. False otherwise
* @return bool
*/
public function isViewOnly()
{
return $this->viewOnly;
}

/**
* Sets the updating flag
*
* @param bool $viewOnly
* @return DMSDocumentCart
*/
public function setViewOnly($viewOnly)
{
$this->viewOnly = (bool) $viewOnly;
return $this;
}

/**
* Displays a view-only table of the cart items.
*
* @return HTMLText
*/
public function getSummary()
{
return $this->renderWith('DMSDocumentCartSummary');
}

/**
* Utility method to link to the current controllers action
*
* @param string $action
* @return string
*/
public function getLink($action = null)
{
return DMSDocumentCartController::create()->Link($action);
}
}
33 changes: 7 additions & 26 deletions code/controllers/DMSCheckoutController.php
Expand Up @@ -6,7 +6,7 @@ class DMSCheckoutController extends ContentController
'DMSDocumentRequestForm',
'index',
'complete',
'send'
'send',
);

/**
Expand Down Expand Up @@ -34,17 +34,18 @@ public function init()

public function index()
{
$this->getCart()->setBackUrl(Director::absoluteBaseURL().$this->Link());
$form = $this->DMSDocumentRequestForm();
return $this
->customise(array(
'Form' => $form,
'Form' => $form,
'Title' => _t(__CLASS__ . '.CHECKOUT_TITLE', 'Checkout')
))
->renderWith(array('Page', 'DMSDocumentRequestForm'));
}

/**
* Gets and displays an editable list of items within the cart, as well as a contact form with entry
* Gets and displays a list of items within the cart, as well as a contact form with entry
* fields for the recipients information.
*
* To extend use the following from within an Extension subclass:
Expand Down Expand Up @@ -149,15 +150,14 @@ public function send()
* Totals requested are updated, delivery details added, email sent for fulfillment
* and print request totals updated.
*
* @param array $data
* @param Form $form
* @param array $data
* @param Form $form
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse
*/
public function doRequestSend($data, Form $form, SS_HTTPRequest $request)
{
$this->updateCartItems($data);
$this->updateCartReceiverInfo($data);
$this->send();
$this->getCart()->saveSubmission($form);
Expand All @@ -174,7 +174,7 @@ public function doRequestSend($data, Form $form, SS_HTTPRequest $request)
public function complete()
{
$data = array(
'Title' => _t(__CLASS__ . '.COMPLETE_THANKS', 'Thanks!'),
'Title' => _t(__CLASS__ . '.COMPLETE_THANKS', 'Thanks!'),
'Content' => _t(
__CLASS__ . '.COMPLETE_MESSAGE',
'Thank you. You will receive a confirmation email shortly.'
Expand All @@ -196,25 +196,6 @@ public function getCart()
return singleton('DMSDocumentCart');
}

/**
* Updates the document quantities just before the request is sent.
*
* @param array $data
*/
public function updateCartItems($data)
{
if (!empty($data['ItemQuantity'])) {
foreach ($data['ItemQuantity'] as $itemID => $quantity) {
// Only update if quantity has changed
$item = $this->getCart()->getItem($itemID);
if ($item->getQuantity() == $quantity) {
continue;
}
$this->getCart()->updateItemQuantity($itemID, $quantity - 1);
}
}
}

/**
* Updates the cart receiver info just before the request is sent.
*
Expand Down
116 changes: 112 additions & 4 deletions code/controllers/DMSDocumentCartController.php
@@ -1,18 +1,24 @@
<?php

class DMSDocumentCartController extends Controller
class DMSDocumentCartController extends ContentController
{
private static $url_handlers = array(
'$Action//$ID' => 'handleAction',
);

private static $allowed_actions = array(
'DocumentCartForm',
'DMSCartEditForm',
'add',
'deduct',
'remove',
'view'
);

public function init()
{
parent::init();
Requirements::css(DMS_CART_DIR . '/css/dms-cart.css');
}
/**
* See {@link DMSDocumentCart::getItems()}
*
Expand Down Expand Up @@ -183,13 +189,115 @@ protected function validateAddRequest($quantity, DMSDocument $document)
if ($document->getHasQuantityLimit() && $quantity > $document->getMaximumQuantity()) {
$result->error(_t(
__CLASS__ . '.ERROR_QUANTITY_EXCEEDED',
'You can\'t add {quantity} of this document',
array('quantity' => $quantity)
'You can\'t add {quantity} of \'{title}\'',
array('quantity' => $quantity, 'title' => $document->getTitle())
));
}

$this->extend('updateValidateAddRequest', $result, $quantity, $document);

return $result;
}

/**
* Updates the document quantities just before the request is sent.
*
* @param array $data
* @param Form $form
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse
*/
public function updateCartItems($data, Form $form, SS_HTTPRequest $request)
{
if (!empty($data['ItemQuantity'])) {
foreach ($data['ItemQuantity'] as $itemID => $quantity) {
if (!is_numeric($quantity) || $quantity < 0) {
continue;
}
// Only update if quantity has changed
$item = $this->getCart()->getItem($itemID);
if ($item->getQuantity() == $quantity) {
continue;
}
// No validate item
$validate = $this->validateAddRequest($quantity, $item->getDocument());
if ($validate->valid()) {
// Removes, then adds a item new item.
$this->getCart()->removeItem($item);
$this->getCart()->addItem($item->setQuantity($quantity));
} else {
$form->sessionMessage($validate->starredList(), 'bad');
return $this->redirectBack();
}
}
}

return $this->redirect($this->getCart()->getBackUrl());
}

/**
* Presents an interface for user to update the cart quantities
*
* @param SS_HTTPRequest $request
* @return ViewableData_Customised
*/
public function view(SS_HTTPRequest $request)
{
$this->getCart()->setViewOnly(true);
$form = $this->DMSCartEditForm();
return $this
->customise(
array(
'Form' => $form,
'Title' => _t(__CLASS__ . '.UPDATE_TITLE', 'Updating cart items')
)
);
}

/**
* Gets and displays an editable list of items within the cart.
*
* To extend use the following from within an Extension subclass:
*
* <code>
* public function updateDMSCartEditForm($form)
* {
* // Do something here
* }
* </code>
*
* @return Form
*/
public function DMSCartEditForm()
{
$actions = FieldList::create(
FormAction::create(
'updateCartItems',
_t(__CLASS__ . '.SAVE_BUTTON', 'Save changes')
)
);
$form = Form::create(
$this,
'DMSCartEditForm',
FieldList::create(),
$actions
);
$form->setTemplate('DMSDocumentRequestForm');
$this->extend('updateDMSCartEditForm', $form);
return $form;
}

/**
* Ensure that links for this controller use the customised route
*
* @param string $action
* @return string
*/
public function Link($action = null)
{
if ($url = array_search(__CLASS__, (array)Config::inst()->get('Director', 'rules'))) {
return $this->join_links($url, $action);
}
}
}
4 changes: 4 additions & 0 deletions css/dms-cart.css
Expand Up @@ -7,3 +7,7 @@
border: none;
vertical-align: middle;
}

.Actions a.action:hover {
border-bottom: inherit;
}
2 changes: 1 addition & 1 deletion lang/en.yml
Expand Up @@ -9,7 +9,7 @@ en:
REQUESTED_ITEMS: Requested documents
DMSDocumentCartController:
ERROR_NOT_ALLOWED: You are not allowed to add this document
ERROR_QUANTITY_EXCEEDED: You can't add {quantity} of this document
ERROR_QUANTITY_EXCEEDED: You can't add {quantity} of '{title}'
DMSDocumentCartExtension:
PRINT_REQUEST_COUNT: 'Print request count:'
DMSCheckoutController:
Expand Down
54 changes: 54 additions & 0 deletions templates/DMSDocumentCartSummary.ss
@@ -0,0 +1,54 @@
<% if $IncludeFormTag %>
<form $AttributesHTML>
<% end_if %>
<table title="<%t DMSDocumentCart.REQUESTED_ITEMS "Requested documents" %>">
<tbody>
<% loop $Items %>
<tr>
<% with $Document %>
<td>
<div class="checkout-page-thumbnail">
<% if $CoverImage %>
<img class="thumbnail" src="$CoverImage.FitMax(32,32).Link"/>
<% else %>
<img class="thumbnail" src="$Icon($Extension)"/>
<% end_if %>
</div>
</td>
<td>$Title</td>

<% if $Up.Up.isViewOnly %>
<td>
<label for="ItemQuantity[{$ID}]"
class="visuallyhidden"><%t DMSDocumentCart.ITEM_QUANTITY "Item Quantity" %></label>
<input min="1" type='text' id="ItemQuantity[{$ID}]" name="ItemQuantity[{$ID}]"
value="$Up.Quantity"/>
</td>
<td><a class="docCart-remove-link" href="$getActionLink('remove')"
title="Remove item"><%t DMSDocumentCart.REMOVE_ITEM "X" %></a></td>
<% else %>
<td>
$Up.Quantity
</td>
<% end_if %>

<% end_with %>
</tr>
<% end_loop %>
</tbody>
</table>
<div class="Actions">
<% if $isViewOnly %>
<% if $Actions %>
<% loop $Actions %>
$Field
<% end_loop %>
<% end_if %>
<% else %>
<a class="action" href="{$getLink('view')}"><%t DMSDocumentCart.UPDATE_CART "Update cart" %></a>
<div class="clear"></div>
<% end_if %>
</div>
<% if $IncludeFormTag %>
</form>
<% end_if %>

0 comments on commit 87a4b0c

Please sign in to comment.