Implemented discarding of products

This commit is contained in:
Erik Thuning 2018-09-06 17:28:47 +02:00
parent 162c89065a
commit 8c2de26af1
4 changed files with 45 additions and 2 deletions

@ -205,6 +205,16 @@
</table>
</form>
¤¤ discard_button ¤¤
<form>
<input type="hidden"
name="id"
value="¤id¤" />
<button onClick="JavaScript:discardProduct(event)">
Skrota artikel
</button>
</form>
¤¤ info_item ¤¤
<tr>
<td>

@ -265,7 +265,7 @@ class Product {
}
public function get_discardtime($format = true) {
if($format) {
if($this->discardtime && $format) {
return gmdate('Y-m-d', $this->discardtime);
}
return $this->discardtime;

@ -422,7 +422,8 @@ class ProductPage extends Page {
}
$tags = '';
foreach($this->product->get_tags() as $tag) {
$tags .= replace(array('tag' => ucfirst($tag)), $this->fragments['tag']);
$tags .= replace(array('tag' => ucfirst($tag)),
$this->fragments['tag']);
}
$out = replace(array('id' => $this->product->get_id(),
'name' => $this->product->get_name(),
@ -431,6 +432,8 @@ class ProductPage extends Page {
'tags' => $tags,
'info' => $info),
$this->fragments['product_details']);
$out .= replace(array('id' => $this->product->get_id()),
$this->fragments['discard_button']);
$out .= replace(array('title' => 'Lånehistorik'),
$this->fragments['subtitle']);
$loan_table = 'Inga lån att visa.';
@ -734,6 +737,8 @@ class Ajax extends Responder {
case 'suggest':
$out = $this->suggest();
break;
case 'discardproduct':
$out = $this->discard_product();
}
print($out->toJson());
}
@ -935,6 +940,16 @@ class Ajax extends Responder {
return new Failure('Invalid type.');
}
}
private function discard_product() {
$product = new Product($_POST['id']);
if(!$product->get_discardtime()) {
$product->discard();
return new Success('Artikeln skrotad.');
} else {
return new Failure('Artikeln är redan skrotad.');
}
}
}
class Result {

@ -340,3 +340,21 @@ function calendar(event) {
cal.show()
}
}
function discardProduct(event) {
event.preventDefault()
if(!window.confirm(
'Är du säker på att du vill skrota artikeln? \n'
+ 'Den kommer fortsättningsvis kunna ses på Historik-sidan.')) {
return
}
var form = event.currentTarget.parentNode
var render = function(result) {
if(result.type == 'success') {
window.location.href = '?page=products'
} else {
showResult(result)
}
}
ajaxRequest('discardproduct', dataListFromForm(form), render)
}