Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

php - How can I get the clients information that access an exact page?

So, I have this code, which is from a plug-in that I want to create:

<?php

declare(strict_types = 1);

use UbntUcrmPluginSdkServiceUcrmApi;
use UbntUcrmPluginSdkServiceUcrmSecurity;
use UbntUcrmPluginSdkSecurityPermissionNames;

chdir(__DIR__);

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/main.php';

$fullAddress = isset($_GET['fullAddress']) ? $_GET['fullAddress'] : '';
$firstName = isset($_GET['firstName']) ? $_GET['firstName'] : '';
$lastName = isset($_GET['lastName']) ? $_GET['lastName'] : '';

// GET collection of clients
$clients = UCRMAPIAccess::doRequest('clients') ?: [];
//echo sprintf('Found %d clients.', count($clients)) . PHP_EOL;

// GET the client data that access the 'Contract' page
foreach($clients as $client) {
    $response = UCRMAPIAccess::doRequest(
        sprintf('clients/%d', $client['id']),
        'GET',
        [
            'fullAddress' => $fullAddress,
            'firstName' => $firstName,
            'lastName' => $lastName,
        ]
    );

    if($response !== null) {
        echo sprintf('The following details are for client number: <b>%d</b>', $client['id']);
        echo '<ul>';
                echo sprintf('<li>%d</li>', $client['id']);
                echo sprintf('<li>%s</li>', $client['fullAddress']);
                echo sprintf('<li>%s</li>', $client['firstName'] . ' ' . $client['lastName']);
        echo '</ul>';
    } else {
        echo sprintf('There was an error retrieving data from client: %d.', $client['id']) . PHP_EOL;
    }
}

echo 'Done.' . PHP_EOL;

For now, if I run this code and I click on the plug-in's page I get this:

image

What I am trying to is: I want to recieve only the client's information that access the page.

So, I am logged as a client (each client have access to that page named Contract) and when I click on Contract page I want to get my client information, not of all clients.

I've tried to modify :

$clients = UCRMAPIAccess::doRequest('clients') ?: []; > UCRMAPIAccess::doRequest(sprintf('clients/%d', $client['id']) and remove the foreach, but then I got an error that $client is not defined.

question from:https://stackoverflow.com/questions/65842830/how-can-i-get-the-clients-information-that-access-an-exact-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since the $clients variable is an array that contains a list of all the clients that have accessed that page, as you're getting the collection here:

$clients = UCRMAPIAccess::doRequest('clients') ?: [];

And I believe you're trying to get the one client that just accessed the page, since you said:

"when I click on Contract page I want to get my client information, not of all clients"

Then I believe you can filter the $clients array for a single client that matches an id, ONLY if you know the id of the client you're looking for.

For example:

$clients = UCRMAPIAccess::doRequest('clients') ?: [];

$you = (object) array('firstName' => 'foo', 'lastName' => 'bar', 'id' => 1);

$meInClients = array_filter($clients, function($k) {
    // filter by key
}, ARRAY_FILTER_USE_KEY)

$clients[meInClients] 

But in this case it seems your program doesn't know what user to look for, so a filter or search might not work here. What you want to do is get the last user who JUST visited that page, naturally that user would be the last in the array.

So:

$clients = UCRMAPIAccess::doRequest('clients') ?: [];

$whoJustVisitedPage = $clients[count($clients) - 1]

So this might work.

UPDATE:

When you access the page as John Doe and go to that page, ( the $clientsWhoVisitedPage array now becomes $clientsWhoVisitedPage + YOU (currently authenticated user a.k.a the last person who viewed the page) What you can do in this case is first to filter the $client array so it doesn't contain the currently authenticated user. Then get the last user in the new array.

Like so:

$me = $security->getUser();

$clientsExceptMe = array_filter($clients, function($client, $x) {

    return $client->id != $me->id;

}, ARRAY_FILTER_USE_BOTH));

$lastPageVisitor = $clientsExceptMe[count($clientsExceptMe) - 1]

lastPageVisitor should now give you the last person that visited that page that isn't the currently authenticated user.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...