Skip to content

Instantly share code, notes, and snippets.

@iamrobert
Created December 21, 2021 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iamrobert/0a19a6f43db017cff368ea244922052d to your computer and use it in GitHub Desktop.
Save iamrobert/0a19a6f43db017cff368ea244922052d to your computer and use it in GitHub Desktop.
Sponsoship.php
//EXTERNAL SITE URL - LIVE URL FROM FLEXIContent
$sponsor_url = 'https://packsanctuary.neticrm.tw/en/civicrm/contribute/transact?reset=1&id=31';
$dogCachePath = JPATH_CACHE;
$mode = 0755;
// Create the subfolder called dogs in cache folder
if (!JFolder::create($dogCachePath . "/dogs", $mode))
{
// Throw error message and stop script.
echo 'DOG CACHE FOLDER CREATION FAILED';
}
$sponsor_block = '';
$node = '';
if($sponsor_url != '') {
//GET VALUE FROM PAGE
// $query = array(
// 'limit' => 20,
// );
//CREATE CACHE FILE
$cacheFileName = $dogCachePath . '/dogs/json.dog-'.$item->id.'.txt';
//CREATE CACHE UPDATE
$cacheUpdateMin = 360;
$results = array();
// Check if the file exists and file timestamp against current time.
if (!file_exists($cacheFileName) || filemtime($cacheFileName) + ($cacheUpdateMin * 60) < time()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_URL, $sponsor_url. '?' . http_build_query($query));
curl_setopt($ch, CURLOPT_URL, $sponsor_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_PROXY, '');
$data = curl_exec($ch);
curl_close($ch);
// Decode the EXTERNAL Website
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($data);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//*[starts-with(@class,"progress-amount-raised")]/span');
//IF NODE DOESN'T EQUAL 0 -> don't execute
if($nodes->length != 0) {
$node = $nodes->item(0)->textContent;
}
// Write to the cache file.
$results = file_put_contents($cacheFileName, $node);
}
else {
// Load the results directly from cache.
$results = file_get_contents($cacheFileName);
if ($results != false) {
$node = $results;
}
}
if($node != NULL) {
$raw_number = filter_var($node, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
$total_amount = filter_var($sponsorship_monthly_total, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND);
if($total_amount != 0 ) {
$pie = round(($raw_number / $total_amount) * 100);
$sponsor_block .='<div class="grid">';
$sponsor_block .='<div class="col-12 text-center text-width mb-p">';
$sponsor_block .='<div class="pie" data-pie=\'{ "percent": '.$pie.' }\'></div>';
$sponsor_block .='<div class="pie-info">';
$sponsor_block .='<p class="amount"><strong>'.JText::_( 'MONEY_NT' ).$node.JText::_( 'MONEY_MONTH' ).'</strong></p>';
$sponsor_block .='<p><strong>Sponsorship Limit:</strong> '.JText::_( 'MONEY_NT' ).$sponsorship_monthly_total.JText::_( 'MONEY_MONTH' ).'</p>';
$sponsor_block .='</div>';
$sponsor_block .=' <p><a href="'.$sponsor_url.'" class="button tr-y--50 st-donate">'.JText::_( 'SPONSOR_ME_BUTTON' ).'</a></p>';
$sponsor_block .='</div>';
$sponsor_block .='</div>';
}
}
//END $sponsor_url != ''
}
echo $sponsor_block;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment