Skip to content

Commit

Permalink
Merge pull request #1002 from inbaz/f-shipajax
Browse files Browse the repository at this point in the history
F shipajax
  • Loading branch information
BigAndini authored Jul 18, 2018
2 parents ee0e09e + fb9f5f5 commit cb02ca3
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 120 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ vendor/bin
vendor/*
bin/*
data/etickets/*
mailoutput/*
mailoutput/*
sxp-bin/
logs/
htdocs
data/log/
85 changes: 42 additions & 43 deletions module/OnsiteReg/src/OnsiteReg/Controller/PackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function detailAction() {
));
}

public function shipAction() {
public function shipAjaxAction() {
if(!$this->getRequest()->isPost()) {
return $this->redirect()->toRoute('onsite/search');
return $this->notFoundAction();
}

$entityManager = $this->getServiceLocator()
Expand All @@ -89,60 +89,59 @@ public function shipAction() {

$postData = $this->getRequest()->getPost();

$itemId = $postData->itemId;
$matchedItems = $package->getAllItems()->filter(function($item) use ($itemId){ return $item->getId() === (int)$itemId; });
if($matchedItems->count() !== 1) {
return $this->notFoundAction();
}
$item = $matchedItems->first();

$itemViewModel = new ViewModel();
$itemViewModel->setTemplate('partial/package-detail-item');
$itemViewModel->setVariable('item', $item);
$itemViewModel->setTerminal('true');

$form = new Form\ConfirmItems();
$form->setData($postData);

$toDetailRedirect = $this->redirect()->toRoute('onsite/package', ['action' => 'detail', 'id' => $package->getId()]);

if($form->isValid()) {
$itemIds = $postData->items;

foreach($itemIds as $itemId) {
$matchedItems = $package->getAllItems()->filter(function($item) use ($itemId){ return $item->getId() === (int)$itemId; });
if($matchedItems->count() !== 1) {
$this->flashMessenger()->addErrorMessage('No item with id ' . $itemId . ' was not found in the package!');
return $toDetailRedirect;
}

$item = $matchedItems->first();

if($item->getShipped()) {
$this->flashMessenger()->addErrorMessage('The item ' . $item->getName() . ' was changed since it was last displayed. Please try again!');
return $toDetailRedirect;
}

#if($item->getStatus()->getValue() !== 'paid') {
if(!$item->getStatus()->getValid()) {
$this->flashMessenger()->addErrorMessage('The item ' . $item->getName() . ' cannot be set to shipped because it is not paid.');
return $toDetailRedirect;
}

$item->setShipped(true);
$item->setShippedDate(new \DateTime());
$entityManager->persist($item);

$log = new \ErsBase\Entity\Log();
$log->setUser($this->zfcUserAuthentication()->getIdentity());
$log->setData('SHIPPED Item ' . $item->getName() . ' of package ' . $package->getCode()->getValue() . '.');
$entityManager->persist($log);

# TODO: create log entry for this.
error_log('set item ' . $item->getId() . ' of package ' . $package->getId() . ' to shipped');
if($item->getShipped()) {
$itemViewModel->setVariable('error', 'This item has already been shipped!');
error_log('attempted duplicate shipping of item ' . $item->getId() . ' in package ' . $package->getId() . '!');
return $itemViewModel;
}

#if($item->getStatus()->getValue() !== 'paid') {
if(!$item->getStatus()->getValid()) {
$itemViewModel->setVariable('error', 'The item cannot be set to shipped because it is not paid.');
error_log('attempted shipping with invalid status of item ' . $item->getId() . ' in package ' . $package->getId() . '!');
return $itemViewModel;
}

$item->setShipped(true);
$item->setShippedDate(new \DateTime());
$entityManager->persist($item);

$log = new \ErsBase\Entity\Log();
$log->setUser($this->zfcUserAuthentication()->getIdentity());
$log->setData('SHIPPED Item ' . $item->getName() . ' of package ' . $package->getCode()->getValue() . '.');
$entityManager->persist($log);
$entityManager->flush();

$this->flashMessenger()->addSuccessMessage('The items were successfully marked as shipped!');

# TODO: create log entry for this.
error_log('set item ' . $item->getId() . ' of package ' . $package->getId() . ' to shipped');
}
else {
$logger = $this->getServiceLocator()->get('Logger');
$logger->warn($form->getMessages());

foreach(call_user_func_array('array_merge', $form->getMessages()) as $error) {
$this->flashMessenger()->addErrorMessage($error);
}
$errors = call_user_func_array('array_merge', $form->getMessages());
$itemViewModel->setVariable('error',
implode("\n", $errors)
. "\nPlease refresh the page and try again." );
}

return $toDetailRedirect;
return $itemViewModel;
}

public function undoItemAction() {
Expand Down
6 changes: 5 additions & 1 deletion module/OnsiteReg/src/OnsiteReg/Form/ConfirmItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public function __construct()

$this->add(array(
'name' => 'csrf',
'type' => 'Zend\Form\Element\Csrf',
'type' => 'Zend\Form\Element\Csrf',
'options' => array(
// Increase timeout to 30 minutes.
'csrf_options' => array('timeout' => 1800),
),
));

$this->add(array(
Expand Down
46 changes: 9 additions & 37 deletions module/OnsiteReg/view/onsite-reg/package/detail.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
$title = "Package " . $package->getCode()->getValue();
$this->headTitle($package->getParticipant()->getFullName());

$form->setAttribute('action', $this->url('onsite/package', array('action' => 'ship', 'id' => $package->getId())));
$form->setAttribute('action', $this->url('onsite/package', array('action' => 'ship-ajax', 'id' => $package->getId())));
$form->prepare();

$packageStatus = $package->getStatusWithShipped();
Expand Down Expand Up @@ -111,54 +111,26 @@ echo $this->flashMessenger()


<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Unshipped items</h3></div>
<div class="panel-body">
<?php if(empty($unshippedItems)): ?>
<span class="gray">No unshipped items for this package.</span>
<?php else: ?>
<?php echo $this->form()->openTag($form); ?>

<ul class="list-unstyled">
<?php
foreach ($unshippedItems as $item):
echo $this->partial('partial/package-detail-item.phtml', ['item' => $item]);
endforeach;
?>
</ul>

<?php echo $this->formHidden($form->get('id')); ?>
<?php echo $this->formHidden($form->get('csrf')); ?>
<div class="text-right">
<button type="button" class="btn btn-lg btn-default select-all-button">Select all</button>
<?php
$form->get('submit')->setAttribute('disabled', 'disabled');
$form->get('submit')->setAttribute('class', 'btn btn-lg btn-danger confirm-items-button');
echo $this->formSubmit($form->get('submit'));

?>
</div>

<?php echo $this->form()->closeTag(); ?>
<?php endif; ?>
</div>
</div>

<?php if(!empty($shippedItems)): ?>
<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Shipped items</h3></div>
<div class="panel-heading"><h3 class="panel-title">Items</h3></div>
<div class="panel-body">
<?php echo $this->form()->openTag($form); ?>

<ul class="list-unstyled">
<?php
foreach ($unshippedItems as $item):
echo $this->partial('partial/package-detail-item.phtml', ['item' => $item]);
endforeach;
foreach ($shippedItems as $item):
echo $this->partial('partial/package-detail-item.phtml', ['item' => $item]);
endforeach;
?>
</ul>

<?php echo $this->formHidden($form->get('id')); ?>
<?php echo $this->formHidden($form->get('csrf')); ?>
<?php echo $this->form()->closeTag(); ?>
</div>
</div>
<?php endif; ?>

<div class="panel panel-default">
<div class="panel-heading"><h3 class="panel-title">Order</h3></div>
Expand Down
24 changes: 20 additions & 4 deletions module/OnsiteReg/view/partial/package-detail-item.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,16 @@ $itemOK = $item->getStatus()->getValid();
</div>

<?php else: ?>
<div class="col-xs-4 text-<?php echo ($itemOK ? 'success' : 'warning'); ?>">
<div class="col-xs-5 text-<?php echo ($itemOK ? 'success' : 'warning'); ?>">
<div class="floatRight">
<button type="button"
class="btn btn-lg item-ship-button <?php echo ($itemOK ? 'btn-success' : 'btn-danger'); ?>"
data-itemid="<?php echo $item->getId(); ?>"
<?php echo ($itemOK ? '' : 'disabled'); ?>>
Confirm
</button>
</div>

<p class="h4">
<i class="fa <?php echo ($itemOK ? 'fa-check' : 'fa-warning'); ?>"></i>
<?php echo $this->escapeHtml($status); ?>
Expand All @@ -71,9 +80,16 @@ $itemOK = $item->getStatus()->getValid();
endif;
?>
</div>
<div class="col-xs-1">
<input type="checkbox" class="checkbox item-ship-checkbox" name="items[]" value="<?php echo $item->getId(); ?>" <?php echo ($itemOK ? '' : ' disabled') ?>>
</div>
<?php endif; ?>
</div>

<?php if(isset($error)): ?>
<div class="row">
<div class="col-xs-12">
<h3 class="text-danger text-right">
<?php echo nl2br($this->escapeHtml($error)) ?>
</h3>
</div>
</div>
<?php endif; ?>
</label></li>
2 changes: 2 additions & 0 deletions public/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ div.product-info-field {
border-radius: 5px;
padding: 10px 15px;
margin-bottom: 20px;
-webkit-transition: background-color 0.8s;
transition: background-color 0.8s;
}

label.payment-type {
Expand Down
73 changes: 39 additions & 34 deletions public/js/custom_onsite.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,19 @@ jQuery(function($) {

// package detail controls
var $confirmAgegroupCheckbox = $('#confirm-agegroup-checkbox');
var $selectAllButton = $('.select-all-button');
var $confirmButton = $('.confirm-items-button');
var $itemCheckboxes = $('.item-ship-checkbox').not(':disabled');
var $itemShipButtons = $('.item-ship-button').not(':disabled');

function updateConfirmButton() {
var anythingSelected = $itemCheckboxes.is(':checked');
function updateShipButtons() {
var agegroupConfirmed = ($confirmAgegroupCheckbox.length === 0 || $confirmAgegroupCheckbox.prop('checked'));
if(!anythingSelected || !agegroupConfirmed) {
$confirmButton.prop('disabled', true);
$confirmButton.prop('class', 'btn btn-lg btn-danger confirm-items-button');
if(agegroupConfirmed) {
$itemShipButtons.prop('disabled', false);
$itemShipButtons.removeClass('btn-danger');
$itemShipButtons.addClass('btn-success');
} else {
$confirmButton.prop('disabled', false);
$confirmButton.prop('class', 'btn btn-lg btn-success confirm-items-button');
$itemShipButtons.prop('disabled', true);
$itemShipButtons.removeClass('btn-success');
$itemShipButtons.addClass('btn-danger');
}

}

$confirmAgegroupCheckbox.change(function() {
Expand All @@ -101,35 +99,42 @@ jQuery(function($) {
.addClass(replaceClasses[i][newClass]);
}

updateConfirmButton();
});

$selectAllButton.click(function() {
var newState = ($selectAllButton.text() === 'Select all');
$itemCheckboxes.prop('checked', newState).change();
updateShipButtons();
});
$confirmAgegroupCheckbox.change();

$itemCheckboxes.change(function() {
var $container = $(this).closest('li');
if($(this).prop('checked')) {
$container.addClass('light-green-bg');
} else {
$container.removeClass('light-green-bg');
}
$itemShipButtons.click(function() {
var $button = $(this);
var $container = $button.closest('li');

updateConfirmButton();
$button.prop('disabled', true);
$container.addClass('bg-info');

if($itemCheckboxes.not(':checked').length === 0) {
$selectAllButton.text('Select none');
} else {
$selectAllButton.text('Select all');
}
var $form = $(this).closest('form');
var url = $form.prop('action');
$.post(url, {
id: $form[0].id.value,
csrf: $form[0].csrf.value,
itemId: $button.data('itemid'),
})
.done(function(data) {
var $newItem = $(data);
var $newPackage = $newItem.find(".package");
var highlightClass = $newPackage.find(".text-danger").length ? 'bg-danger' : 'bg-success';
$newPackage.addClass(highlightClass);

$container.replaceWith($newItem);
setTimeout(function(){
$newPackage.removeClass(highlightClass);
}, 500);
})
.fail(function(e) {
alert('Failed to ship item: Network error!');
console.error(e);
$button.prop('disabled', false);
});
});

if($itemCheckboxes.length === 0) {
$selectAllButton.prop('disabled', true);
}

if(!$('.package-detail-view').length) {
$('.onsite-search-box').focus();
}
Expand Down

0 comments on commit cb02ca3

Please sign in to comment.