const getAutocompleteLocation = async (sendObject) => {
try {
const response = await fetch ('index.php?option=com_comprofiler&view=pluginclass&plugin=cbautoactions&action=action&actions=296&format=raw', {
method: 'POST',
mode: 'same-origin',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sendObject)
})
const data = await response.json();
console.log(data)
console.log(typeof(data))
console.log(data.success)
if (data.success === true) {
console.log(data.data[0].views, 'views')
const previewLocations = data.data[0].views
const listItems = previewLocations.map(location => {
console.log(location)
return `<li data-location-id=${location.id}>${location.first}</li>`
})
.join('')
ul.innerHTML = listItems
ul.removeAttribute('hidden')
console.log('listItems', listItems)
ul.addEventListener('click', e => {
if (!event.target.matches('li')) return
const li = e.target
const locationName = li.textContent
console.log('locationName', locationName)
const locationId = li.dataset.locationId
console.log('locationId', locationId)
input.value = locationName
input.setAttribute('data-location-id', locationId)
ul.setAttribute('hidden', true)
})
}
} catch(e){
console.log(e)
}
}
Please Log in to join the conversation.
Yes, use $input as noted in the Code parameter description. Example as follows.But has Cb the same functionalities as joomla to get the tasks that are sent by the js file and how ??
if ($receivedData['task'] === 'getAutoCompleteLocationData') {
if ( $input->getCmd( 'task', '' ) === 'getAutoCompleteLocationData' ) {
$query = $receivedData["query"];
$locationId = $receivedData["location_id"];
$query = $input->getString( 'query', '' );
$locationId = $input->getString( 'location_id', '' );
$content = trim(file_get_contents("php://input"));
$receivedData = json_decode($content, true);
Please Log in to join the conversation.
I've no idea why you're using the following even in Joomla.
$content = trim(file_get_contents("php://input"));
$receivedData = json_decode($content, true);
You had access to $app->input to get request data.
<div class="cb_template cb_template_4d95f3"></div>
{"task":"getAutoCompleteLocationData","query":"xxx"}
print_r($input);exit;
Please Log in to join the conversation.
They don't need to know CB or Joomla. It's all documented (and for what isn't they can just ask here how to do X in regards to CB) and has autocomplete. Any PHP dev with an IDE should be able to see that. You'd just need a quality developer experienced with PHP that's capable of learning, but don't expect much from a PHP developer if you're paying them less than $50 an hour.That's written by the dev that I hired for this job.... very difficult to find experts who knows CB... they write joomla only and I have to adapt each time
Wish I could find a good dev team expert in CB and plugins...
Either should work fine. CB Auto Actions will force a JSON header when using JSON output.if I add &format=raw at the end of the autoaction will it output a correct json or do I have to change it for &format=json at the end ?
That's because you're converting the post body to a JSON string in your fetch. That explains why the PHP was so goofy accessing the request data. I've no idea what sendObject is in your JS, but it should either be a FormData object or a JS object. Example as follows.It seems like the js script sends requests looking like
const formData = new FormData();
formData.append( 'task', 'getAutoCompleteLocationData' );
formData.append( 'query', 'xxx' );
const url = new URL( 'index.php?option=com_comprofiler&view=pluginclass&plugin=cbautoactions&action=action&actions=296&format=raw' );
url.searchParams.set( 'task', 'getAutoCompleteLocationData' );
url.searchParams.set( 'query', 'xxx' );
Please Log in to join the conversation.
document.addEventListener('DOMContentLoaded', e => {
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
const typeaheadLocation = document.querySelector('.typeahead__location')
let input
let ul
if (typeaheadLocation) {
input = typeaheadLocation.querySelector('input')
ul = typeaheadLocation.querySelector('ul')
}
const getAutocompleteLocation = async (sendObject) => {
try {
const url = new URL( 'index.php?option=com_comprofiler&view=pluginclass&plugin=cbautoactions&action=action&actions=296&format=raw' );
url.searchParams.set( 'task', 'getAutoCompleteLocationData' );
url.searchParams.set( 'query', inputValue );
const response = await fetch ( url , {
method: 'GET',
mode: 'same-origin',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
})
const data = await response.json();
console.log(data)
console.log(typeof(data))
console.log(data.success)
if (data.success === true) {
console.log(data.data[0].views, 'views')
const previewLocations = data.data[0].views
const listItems = previewLocations.map(location => {
console.log(location)
return `<li data-location-id=${location.id}>${location.first}</li>`
})
.join('')
ul.innerHTML = listItems
ul.removeAttribute('hidden')
console.log('listItems', listItems)
ul.addEventListener('click', e => {
if (!event.target.matches('li')) return
const li = e.target
const locationName = li.textContent
console.log('locationName', locationName)
const locationId = li.dataset.locationId
console.log('locationId', locationId)
input.value = locationName
input.setAttribute('data-location-id', locationId)
ul.setAttribute('hidden', true)
})
}
} catch(e){
console.log(e)
}
}
if (typeaheadLocation) {
input.addEventListener('input', event => {
const input = event.target
const inputValue = input.value.trim()
console.log(inputValue)
const autoLocationObjectFormData = new FormData()
autoLocationObjectFormData.append('task', 'getAutoCompleteLocationData')
autoLocationObjectFormData.append('query', inputValue)
getAutocompleteLocation(autoLocationObjectFormData)
})
}
document.addEventListener('click', e => {
const typeaheadLocation = document.querySelector('.typeahead__location')
if (!typeaheadLocation) return
if (!e.target.closest('.typeahead__location')) {
ul.setAttribute('hidden', true)
}
})
})
if ( $input->get( 'task' ) === 'getAutoCompleteLocationData' ) {
//print_r($input->getString( 'query' ));
$curl = curl_init();
$query = $input->getString( 'query' );
curl_setopt_array($curl, array(
CURLOPT_URL => "https://dev.societeinfo.com/app/rest/api/v2/places.json/autocomplete?query={$query}&key={$api_key}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response_location = curl_exec($curl);
curl_close($curl);
//TODO: Split the company name if the result false
$location = $response_location;
// return json_decode($response_location);
return $location;
print_r( $location );
// Save the result
}
Please Log in to join the conversation.
https://dev.societeinfo.com/app/rest/api/v2/places.json/autocomplete
Please Log in to join the conversation.