Skip to content

Instantly share code, notes, and snippets.

@freshface
Created May 10, 2019 14:57
Show Gist options
  • Save freshface/292f7917fc90808f46dc1c7fbe91c8e8 to your computer and use it in GitHub Desktop.
Save freshface/292f7917fc90808f46dc1c7fbe91c8e8 to your computer and use it in GitHub Desktop.
<?php
namespace Statamic\Addons\Calculate;
use Statamic\Extend\Tags;
use Statamic\API\Collection;
use Statamic\API\Entry;
use Statamic\API\YAML;
class CalculateTags extends Tags
{
/**
* The {{ calculate }} tag
*
* @return string|array
*/
public function index()
{
$id = (string) $this->getParam('id');
$send = (string) $this->getParam('send');
$file = realpath('.') . '/site/storage/forms/prijsaanvraag/' . $id . '.yaml';
if(!file_exists($file)){
return ['to404' => true];
}
$content = file_get_contents($file);
$data = YAML::parse($content);
$bedrijf = $data['bedrijf'];
$emailAddress = $data['email'];
$branding = (array) @$data['branding'];
$webdesign = (array )@$data['webdesign'];
$content_creatie = (array) @$data['content_creatie'];
$marketing = (array) @$data['marketing'];
$all = array_merge( $branding , $webdesign ,$content_creatie, $marketing);
$vanaf = 0;
$tot = 0;
$entries = Entry::whereCollection('prijsaanvraag');
foreach($entries as $entry){
$data = $entry->data();
if(in_array($data['title'], $all))
{
$vanaf += (int) @$data ['prijs_vanaf'];
$tot += (int) @$data['prijs_tot'];
}
}
$pricing = ['vanaf' => $vanaf, 'tot' => $tot];
$vars = [
'bedrijf' => $bedrijf,
'email' => $emailAddress,
'pricing' => $pricing,
'branding' => $branding,
'webdesign' => $webdesign,
'marketing' => $marketing,
'content_creatie' => $content_creatie,
'to404' => false
];
if($send)
{
$email = \Statamic\API\Email::create();
$email->to($emailAddress)->subject('Uw prijsaanvraag van Figure8')->with($vars)->template('prijsaanvraag');
$email->send();
}
return $vars;
}
/**
* The {{ calculate:example }} tag
*
* @return string|array
*/
public function example()
{
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment