Hi.
I guess I’m out on deep water here with my not-so-great-php-knowledge, but if some one could push me in the right direction I would be much appreciated.
So, what I want to do is this:
Create a custom field type which is a button the admin can click to send a request to an api and get some answers which will create a bunch of subpages and add data to them. Then give the admin some feedback about which pages has been created or updated/already exist.
I have manage to do this by adding the logic to a template and just refresh the page, but now I want to move it to the panel where it belong. This is the code I used i the template:
<?php
require_once kirby()->roots()->assets() . '/apiLib.php';
/**
* Instantiate the api Class with the following parameters:
*
* @param string Access ID.
* @param string Secret Key.
* @param string HTTP transport function. (optional) Options: curl, file or socket. Default: curl
* @param int HTTP connection timeout, in seconds. (optional)
* @param bool $returnObjects (optional) if TRUE, responses are objects, otherwise associative arrays.
*
*/
$dataAPI = new DataApi( 'XXXXXXX', 'XXXXXXXXXX' );
?>
<main class="main grid-container" role="main">
<div class="text">
<h1><?php echo $page->title()->html() ?></h1>
<?php echo $page->text()->kirbytext() ?>
</div>
<?php
$search = $dataFeedrAPI->searchRequest();
$category = $page->category();
$search->addFilter('category LIKE [' . $category . ']');
$search->addFilter('source_id IN 320');
$search->addFilter('merchant_id IN 38788, 61612');
$search->addFilter('brand !EMPTY');
$search->addFilter('description !EMPTY');
$search->addFilter('image !EMPTY');
$search->setLimit(100);
$products = $search->execute();
$return_count = count($products);
print '<h2>'.$return_count . ' of ' . $search->getFoundCount() . ' total products found in the DataFeedr API.</h2>';
foreach($products as $product) {
$productItem = $page->children()->find($product['_id']);
//Check if product already exist
if ( $productItem ) {
//Check if product has been updated by the merchant
if ( $product['time_updated'] != $productItem->time_updated() ) {
// Update the product content
try {
$productItem->update(array(
'title' => $product['name'],
'id' => $product['_id'],
'suid' => $product['suid'],
'sku' => $product['sku'],
'time_updated' => $product['time_updated'],
'description' => $product['description'],
));
echo 'Product ' . $product['name'] . ' has been updated<br / >';
} catch(Exception $e) {
echo $e->getMessage();
}
// end product update
} else {
//The product is up to date
echo 'Product ' . $product['name'] . ' already exist<br / >';
}
} else {
//The product does not jet exist and will be created
try {
$newProduct = $page->children()->create($product['_id'], 'product', array(
'title' => $product['name'],
'id' => $product['_id'],
'suid' => $product['suid'],
'sku' => $product['sku'],
'time_updated' => $product['time_updated'],
'description' => $product['description'],
));// end $newProduct
//Download the product image to server
$imageData = file_get_contents($product['image']);
$imageName = $product['brand'] . '-' . $product['name'];
file_put_contents($newProduct->root() . DS . f::safeName($imageName) . '.jpg', $imageData);
echo 'The new product ' . $product['name'] . ' has been created <br / >';
} catch(Exception $e) {
echo $e->getMessage();
};
} // end else
}// end foreach
?>
Hope some one wants to take this challenge.