Last active
February 8, 2018 17:24
-
-
Save abdusco/2bb649cd2fc181734a132b0e660f64a2 to your computer and use it in GitHub Desktop.
A simple dashboard module for ProcessWire 3.0+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Abdus on 29.04.2017. | |
*/ | |
window.addEventListener('DOMContentLoaded', function () { | |
let refresh = document.querySelector('#ProcessDashboardRefresh'); | |
let table = document.querySelector('#ProcessDashboardTable'); | |
refresh.addEventListener('click', function (e) { | |
e.preventDefault(); | |
$.getJSON('./latest', { | |
limit: 10 | |
}, function (data) { | |
updateTable(data); | |
}); | |
console.log(this); | |
}); | |
function renderRow(row) { | |
return `<tr> | |
<td>${row.title}</td> | |
<td>${row.date}</td> | |
<td>${row.total}</td> | |
</tr>`; | |
} | |
function updateTable(rows) { | |
table.tBodies[0].innerHTML = rows.map(renderRow) | |
.join(''); | |
} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace ProcessWire; | |
class ProcessDashboard extends Process implements Module | |
{ | |
public static function getModuleInfo() | |
{ | |
return [ | |
'title' => 'Orders Dashboard', | |
'summary' => 'Shows latest orders', | |
'version' => '0.0.1', | |
'author' => 'abdus', | |
'autoload' => true, | |
'page' => [ | |
'name' => 'order-dashboard', | |
'title' => 'Orders', | |
'template' => 'admin' | |
] | |
]; | |
} | |
public function init() | |
{ | |
$scriptUrl = $this->urls->$this . 'assets/dashboard.js'; | |
$this->config->scripts->add($scriptUrl); | |
} | |
public function ___execute() | |
{ | |
/* @var $table MarkupAdminDataTable */ | |
$table = $this->modules->MarkupAdminDataTable; | |
$table->setID($this->className . 'Table'); // "#ProcessDashboardTable" | |
$table->headerRow([ | |
'Product', | |
'Date', | |
'Total' | |
]); | |
// fill the table | |
foreach ($this->getLatest(10) as $order) { | |
$table->row([ | |
$order['title'], | |
$order['date'], | |
$order['total'] | |
]); | |
} | |
// to refresh items | |
$refreshButton = $this->modules->InputfieldSubmit; | |
$refreshButton->name = 'latest'; | |
$refreshButton->id = $this->className . 'Refresh'; // "#ProcessDashboardRefresh" | |
$refreshButton->value = 'latest'; // label of the button | |
return $table->render() . $refreshButton->render(); | |
} | |
public function ___executeLatest() | |
{ | |
// get limit from request, if not provided, it defaults to 10 | |
$limit = $this->input->get->limit ?? 10; | |
return json_encode($this->getRandom($limit)); | |
} | |
protected function getLatest($limit = 5, $start = 0) | |
{ | |
// find last $limit orders, starting from $offset | |
$orders = $this->pages->find("template=order, sort=-created, limit=$limit, start=$start"); | |
return $orders->explode(function ($order) { | |
return [ | |
'title' => $order->title, | |
'date' => date('Y-m-d h:i:s', $order->created), | |
'total' => $order->total | |
]; | |
}); | |
} | |
protected function getRandom($limit = 5) | |
{ | |
$orders = $this->pages->find("template=order, sort=random, limit=$limit"); | |
return $orders->explode(function ($order) { | |
return [ | |
'title' => $order->title, | |
'date' => date('Y-m-d h:i:s', $order->created), | |
'total' => $order->total | |
]; | |
}); | |
} | |
public function install() | |
{ | |
parent::install(); | |
} | |
public function uninstall() | |
{ | |
parent::uninstall(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment