Skip to content

Commit

Permalink
Merge pull request #208
Browse files Browse the repository at this point in the history
Fleet representation change
  • Loading branch information
sveneld authored Dec 8, 2024
2 parents f0b37c5 + 6adc1d9 commit 3b7d583
Show file tree
Hide file tree
Showing 13 changed files with 517 additions and 138 deletions.
2 changes: 1 addition & 1 deletion actions-sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ function note($number,$bikeNum,$message)

function last($number,$bike)
{

//@see \BikeShare\Repository\BikeRepository::findItemLastUsage
global $db, $smsSender, $user;
$userId = $user->findUserIdByNumber($number);
$bikeNum = intval($bike);
Expand Down
70 changes: 0 additions & 70 deletions actions-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,76 +122,6 @@ function removenote($userId, $bikeNum)
response(_('Note for bike') . ' ' . $bikeNum . ' ' . _('deleted') . '.');
}

function last($userId, $bike = 0)
{
global $db;
$bikeNum = intval($bike);
if ($bikeNum) {

$result = $db->query("SELECT note FROM notes WHERE bikeNum='$bikeNum' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$note = _('Bike note:') . ' ' . $note;
}

$historyInfo = '<h3>' . _('Bike') . ' ' . $bikeNum . ' ' . _('history') . ':</h3>' . $note . '<ul style="margin-top: 0.5em">';
$result = $db->query("SELECT userName,parameter,standName,action,time FROM `history` JOIN users ON history.userid=users.userid LEFT JOIN stands ON stands.standid=history.parameter WHERE bikenum=$bikeNum AND (action NOT LIKE '%CREDIT%') ORDER BY time DESC LIMIT 10");
while ($row = $result->fetch_assoc()) {
$time = strtotime($row['time']);
$historyInfo .= '<li>' . date('d/m H:i', $time) . ' - ';
if ($row['standName'] != null) {
$historyInfo .= $row['standName'];
if (strpos($row['parameter'], '|')) {
$revertcode = explode('|', $row['parameter']);
$revertcode = $revertcode[1];
}
if ($row['action'] == 'REVERT') {
$historyInfo .= ' <span class="label label-warning">' . _('Revert') . ' (' . str_pad($revertcode, 4, '0', STR_PAD_LEFT) . ')</span>';
}
} else {
$historyInfo .= $row['userName'] . ' (<span class="label label-default">' . str_pad($row['parameter'], 4, '0', STR_PAD_LEFT) . '</span>)';
}
$historyInfo .= '</li>';
}
$historyInfo .= '</ul>';
} else {
$result = $db->query("SELECT bikeNum FROM bikes WHERE currentUser<>''");
$inuse = $result->num_rows;
$result = $db->query('SELECT bikeNum,userName,standName,users.userId FROM bikes LEFT JOIN users ON bikes.currentUser=users.userId LEFT JOIN stands ON bikes.currentStand=stands.standId ORDER BY bikeNum');
$total = $result->num_rows;
$historyInfo = '<h3>' . _('Current network usage:') . '</h3>';
$historyInfo .= '<h4>' . sprintf(ngettext('%d bicycle', '%d bicycles', $total), $total) . ', ' . $inuse . ' ' . _('in use') . '</h4><ul>';
while ($row = $result->fetch_assoc()) {
$historyInfo .= '<li>' . $row['bikeNum'] . ' - ';
if ($row['standName'] != null) {
$historyInfo .= $row['standName'];
} else {
$historyInfo .= '<span class="bg-warning">' . $row['userName'];
$result2 = $db->query('SELECT time FROM history WHERE bikeNum=' . $row['bikeNum'] . ' AND userId=' . $row['userId'] . " AND action='RENT' ORDER BY time DESC");
$row2 = $result2->fetch_assoc();
$historyInfo .= ': ' . date('d/m H:i', strtotime($row2['time'])) . '</span>';
}
$result2 = $db->query("SELECT note FROM notes WHERE bikeNum='" . $row['bikeNum'] . "' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result2->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$historyInfo .= ' (' . $note . ')';
}

$historyInfo .= '</li>';
}
$historyInfo .= '</ul>';
}
response($historyInfo, 0, '', 0);
}

function userbikes($userId)
{
global $db, $auth;
Expand Down
19 changes: 0 additions & 19 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@
checkbikeno($bikeno); checkstandname($stand);
$rentSystem->returnBike($userid, $bikeno, $stand, $note, TRUE);
break;
case "where":
logrequest($userid,$action);
$auth->refreshSession();
$bikeno=trim($_GET["bikeno"]);
checkbikeno($bikeno);
where($userid,$bikeno);
break;
case "removenote":
logrequest($userid,$action);
$auth->refreshSession();
Expand All @@ -107,18 +100,6 @@
checkbikeno($bikeno);
revert($userid,$bikeno);
break;
case "last":
logrequest($userid,$action);
$auth->refreshSession();
checkprivileges($userid);
if ($_GET["bikeno"])
{
$bikeno=trim($_GET["bikeno"]);
checkbikeno($bikeno);
last($userid,$bikeno);
}
else last($userid);
break;
case "stands": #"operationId": "stand.get",
logrequest($userid,$action);
$auth->refreshSession();
Expand Down
11 changes: 11 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@
->controller([\BikeShare\Controller\SecurityController::class, 'logout']);
$routes->add('reset_password', '/resetPassword')
->controller([\BikeShare\Controller\SecurityController::class, 'resetPassword']);

$routes->add('api_bike_index', '/api/bike')
->methods(['GET'])
->controller([\BikeShare\Controller\Api\BikeController::class, 'index']);
$routes->add('api_bike_item', '/api/bike/{bikeNumber}')
->methods(['GET'])
->controller([\BikeShare\Controller\Api\BikeController::class, 'item']);
$routes->add('api_bike_last_usage', '/api/bikeLastUsage/{bikeNumber}')
->methods(['GET'])
->controller([\BikeShare\Controller\Api\BikeController::class, 'lastUsage']);

};
3 changes: 3 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@
$services->alias(CodeGeneratorInterface::class, CodeGenerator::class);

$services->alias(PhonePurifierInterface::class, PhonePurifier::class);

$services->load('BikeShare\\EventListener\\', '../src/EventListener')
->tag('kernel.event_listener');
};
134 changes: 111 additions & 23 deletions public/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ $(document).ready(function () {
$("#edituser").hide();
$("#where").click(function () {
if (window.ga) ga('send', 'event', 'buttons', 'click', 'admin-where');
where();
bikeInfo($('#bikeNumber').val());
});
$("#revert").click(function () {
$("#fleetconsole").on('click', '.bike-revert', function (event) {
if (window.ga) ga('send', 'event', 'buttons', 'click', 'admin-revert');
revert();
revert($(this).data('bike-number'));
event.preventDefault();
});
$("#last").click(function () {
$('#fleetconsole').on('click', '.bike-last-usage', function (event) {
if (window.ga) ga('send', 'event', 'buttons', 'click', 'admin-last');
last();
last($(this).data('bike-number'));
event.preventDefault();
});
$("#stands").click(function () {
if (window.ga) ga('send', 'event', 'buttons', 'click', 'admin-stands');
Expand Down Expand Up @@ -66,7 +68,7 @@ $(document).ready(function () {
addcredit(10);
return false;
});
last();
bikeInfo();
});

function handleresponse(elementid, jsonobject, display) {
Expand All @@ -76,24 +78,110 @@ function handleresponse(elementid, jsonobject, display) {
}
}

function where() {
if (window.ga) ga('send', 'event', 'bikes', 'where', $('#adminparam').val());
function generateBikeCards(data) {
const $container = $("#fleetconsole");
const $template = $("#bike-card-template");
$container.empty();

$.each(data, function (index, item) {
const $card = $template.clone().removeAttr("id").removeClass("d-none");

const $bikeCard = $card.find(".bike-card");
if (item.userName !== null) {
$bikeCard.addClass("bg-success text-white border-success");
} else if (item.notes !== null) {
$bikeCard.addClass("bg-warning text-dark border-warning");
} else {
$bikeCard.addClass("bg-light text-dark border-light");
}
$card.attr("data-bike-number", item.bikeNum);
$card.find(".bike-last-usage").attr("data-bike-number", item.bikeNum);
$card.find(".bike-revert").attr("data-bike-number", item.bikeNum);

$card.find(".bike-number").text(item.bikeNum);

const $standInfo = $card.find(".stand-info");
const $rentInfo = $card.find(".rent-info");
if (item.userName !== null) {
$standInfo.addClass("d-none");
$rentInfo.removeClass("d-none");
$rentInfo.find(".user-name").text(item.userName);
$rentInfo.find(".rent-time").text(item.rentTime || "Unknown time");
} else {
$standInfo.removeClass("d-none").find('.stand-name').text(item.standName);
$rentInfo.addClass("d-none");
}

if (item.isServiceStand == 1) {
$standInfo.find(".service-stand").removeClass("d-none");
}

const $noteInfo = $card.find(".note-info");
if (item.notes) {
$noteInfo.removeClass("d-none");
$noteInfo.find(".note-text").text(item.notes);
} else {
$noteInfo.addClass("d-none");
}

$container.append($card);
});
}

function bikeInfo(bikeNumber) {
if (window.ga) ga('send', 'event', 'bikes', 'where', bikeNumber);

$.ajax({
url: "command.php?action=where&bikeno=" + $('#adminparam').val()
}).done(function (jsonresponse) {
jsonobject = $.parseJSON(jsonresponse);
handleresponse("fleetconsole", jsonobject);
url: "/api/bike" + (bikeNumber ? "/" + bikeNumber : ""),
method: "GET",
dataType: "json",
success: function(response) {
generateBikeCards(response);
},
error: function(xhr, status, error) {
console.error("Error fetching bike data:", error);
}
});
}

function last() {
if (window.ga) ga('send', 'event', 'bikes', 'last', $('#adminparam').val());
function last(bikeNumber) {
if (window.ga) ga('send', 'event', 'bikes', 'last', bikeNumber);

$.ajax({
url: "command.php?action=last&bikeno=" + $('#adminparam').val()
}).done(function (jsonresponse) {
jsonobject = $.parseJSON(jsonresponse);
handleresponse("fleetconsole", jsonobject);
url: "/api/bikeLastUsage/" + bikeNumber,
method: "GET",
dataType: "json",
success: function(data) {
$container = $("#bikeLastUsage .modal-body");
$container.empty();

if (data.notes !== '') {
const $bikeUsageNotesTemplate = $("#bike-card-last_usage_notes_template");
const $notes = $bikeUsageNotesTemplate.clone().removeClass("d-none");
$notes.find("#note").text(data.notes);
$container.append($notes);
}

$.each(data.history, function (index, item) {
const $template = $("#bike-card-last_usage_template");
const $history = $template.clone().removeClass("d-none");
$history.find("#time").text(item.time);
$history.find("#standName").text(item.standName);
$history.find("#userName").text(item.userName);
$history.find("#parameter").text(item.parameter);
$history.find("#action i").addClass("d-none");
$history.find("#action ." + item.action).removeClass("d-none");

$container.append($history);
});

$('#bikeLastUsage').modal()
},
error: function(xhr, status, error) {
console.error("Error fetching bike data:", error);
}
});

}

function stands() {
Expand Down Expand Up @@ -276,9 +364,9 @@ function sellcoupon(coupon) {
}

function trips() {
if (window.ga) ga('send', 'event', 'bikes', 'trips', $('#adminparam').val());
if (window.ga) ga('send', 'event', 'bikes', 'trips', $('#bikeNumber').val());
$.ajax({
url: "command.php?action=trips&bikeno=" + $('#adminparam').val()
url: "command.php?action=trips&bikeno=" + $('#bikeNumber').val()
}).done(function (jsonresponse) {
jsonobject = $.parseJSON(jsonresponse);
if (jsonobject.error == 1) {
Expand Down Expand Up @@ -311,10 +399,10 @@ function trips() {
});
}

function revert() {
if (window.ga) ga('send', 'event', 'bikes', 'revert', $('#adminparam').val());
function revert(bikeNumber) {
if (window.ga) ga('send', 'event', 'bikes', 'revert', bikeNumber);
$.ajax({
url: "command.php?action=revert&bikeno=" + $('#adminparam').val()
url: "command.php?action=revert&bikeno=" + bikeNumber
}).done(function (jsonresponse) {
jsonobject = $.parseJSON(jsonresponse);
handleresponse("fleetconsole", jsonobject);
Expand Down
5 changes: 1 addition & 4 deletions src/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use BikeShare\Credit\CreditSystemInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AdminController extends AbstractController
Expand All @@ -18,7 +17,6 @@ class AdminController extends AbstractController
* @Route("/admin.php", name="admin_old")
*/
public function index(
Request $request,
Configuration $configuration,
CreditSystemInterface $creditSystem,
LoggerInterface $logger
Expand All @@ -27,8 +25,7 @@ public function index(
$logger->info(
'User tried to access admin page without permission',
[
'user' => $this->getUser()->getNumber(),
'ip' => $request->getClientIp(),
'user' => $this->getUser()->getUserIdentifier(),
]
);

Expand Down
Loading

0 comments on commit 3b7d583

Please sign in to comment.