File: /var/www/html/wp-content/plugins/bsx/inc/cass-tabs.php
<?php
add_shortcode('cass-boxplot', 'cass_boxplot');
function cass_boxplot() {
global $wpdb;
$cassId = get_queried_object_id();
$ccmp = getCassCamps($cassId);
if(count($ccmp) <= 1) {
return '<p style="color: #ffcc33;font-size: 0.9rem;">Two campaigns necessary for this view. This Cassandra has posted '.count($ccmp).'.';
}
$cmp_ids = array_column($ccmp,"ID");
$bpdat = bsx_rest_get("box",['cid'=>implode(",",$cmp_ids)]);
$apx = [];
foreach($bpdat as $cat) {
$apx[] = array(
'x'=>$cat['_row'],
'y'=>array(
round(100*(float)$cat['min'],2),
round(100*(float)$cat['q1'],2),
round(100*(float)$cat['median'],2),
round(100*(float)$cat['q3'],2),
round(100*(float)$cat['max'],2)
)
);
}
enqueue_apex();
$chart_id = 'chart-box-plot' . uniqid();
ob_start();
?>
<div id=<?php echo esc_attr($chart_id); ?> style="width: 100%;"></div>
<script>
document.addEventListener("DOMContentLoaded", function() {
var options = {
annotations:{
yaxis: [{
y:0
}]
},
chart: {
id: '<?php echo esc_js($chart_id); ?>',
type: "boxPlot",
height: 400,
},
plotOptions: {
boxPlot: {
colors: {
upper: '#ffcc33',
lower: '#8e7d18'
}
}
},
yaxis: {
min: (min) => Math.min(-5, Math.floor(min / 5) * 5),
max: (max) => Math.max(10, Math.ceil(max / 5) * 5),
tickAmount: 6,
labels: {
formatter: (val) => `${Math.round(val)}%`
}
},
tooltip:{
theme: 'dark'
},
series: [{
data: <?php echo wp_json_encode($apx); ?>
}]
};
var chart = new ApexCharts(
document.querySelector("#<?php echo esc_js($chart_id); ?>"),
options
);
chart.render();
});
</script>
<?php
return ob_get_clean();
}
// ###########################################################################################
add_shortcode('cass-compare', 'cass_compare');
function cass_compare() {
global $wpdb;
$cassId = get_queried_object_id();
$ccmp = getCassCamps($cassId);
if(count($ccmp) <= 1) {
return '<p style="color: #ffcc33;font-size: 0.9rem;">Two campaigns necessary for this view. This Cassandra has posted '.count($ccmp).'.';
}
$cmp_ids = array_column($ccmp, "ID");
$bpdat = bsx_rest_get('cmp', ['cid'=>implode(",",$cmp_ids)]);
$series = array_transpose($bpdat);
$cat = array_pop($series);
// Finde die maximale Länge (sollte gleich der Kategorie-Länge sein)
$max_length = count($cat);
// Formatiere die Daten und fülle mit null auf
$formatted_series = [];
foreach($series as $name => $data) {
$cat2 = [];
$percentage_data = [];
for($i = 0; $i < $max_length; $i++) {
$cat2[] = 'day '.$i;
if(isset($data[$i])) {
$percentage_data[] = round($data[$i] * 100, 2);
} else {
$percentage_data[] = null; // Fehlende Werte als null
}
}
$fseries[] = [
'name' => $name,
'data' => $percentage_data
];
};
enqueue_apex();
$chart_id = 'chart-cass-line-plot-' . uniqid();
ob_start();
?>
<div id="<?php echo esc_attr($chart_id); ?>" style="width: 100%;"></div>
<script>
document.addEventListener("DOMContentLoaded", function() {
console.log('Script started');
var categories = <?php echo wp_json_encode(array_values($cat2)); ?>;
var series = <?php echo wp_json_encode($fseries); ?>;
console.log('Categories:', categories);
console.log('Series:', series);
console.log('Is categories array?', Array.isArray(categories));
console.log('Is series array?', Array.isArray(series));
if (series.length > 0) {
console.log('First series:', series[0]);
console.log('Is first series.data array?', Array.isArray(series[0].data));
}
var options = {
colors:['#ffcc33', '#ebaecb', '#8e7d18', '#868188', '#a5a2a3'],
chart: {
id: '<?php echo esc_js($chart_id); ?>',
height: 400,
type: "line"
},
xaxis: {
tickAmount: 10,
stepSize: 1,
categories: <?php echo wp_json_encode($cat2); ?>
},
tooltip: {
theme: 'dark'
},
stroke: {
curve: 'smooth'
},
series: <?php echo wp_json_encode($fseries); ?>
};
console.log('Options:', options);
var chart = new ApexCharts(
document.querySelector("#<?php echo esc_js($chart_id); ?>"),
options
);
chart.render().then(function() {
console.log('Chart rendered successfully');
}).catch(function(err) {
console.error('Chart render error:', err);
});
});
</script>
<?php
return ob_get_clean();
}
/**
* Cassandra Kampagnenliste für Kadence Tab
* Füge diesen Code in functions.php ein oder als Custom HTML Block
*/
function render_inline_campaigns() {
global $tz;
$id = get_queried_object_id();
$camps = get_posts(array(
'post_type'=>'campaign',
'posts_per_page'=> -1,
'meta_key' => 'publish_date',
'orderby' => 'meta_value',
'order' => 'DESC'
));
$cmps = [];
foreach($camps as $camp) {
$cass = get_field("cassandra",$camp->ID);
if($cass[0]->ID != $id) continue;
$cmps[] = $camp;
}
if(count($cmps) == 0) {
echo '<p style="color: #888;">This Cassandra has not posted a report yet';
return;
}
ob_start();
?>
<div class="ccin-container">
<?php
foreach($cmps as $camp) {
$status = getStatus($camp->ID);
if(!in_array($status, ["Active", "Closed","Locked"])) continue;
$kpis=getKpis($camp->ID);
$company = get_field("company",$camp->ID);
$legal_name = get_field('legal_name', $company[0]->ID);
$featured_image = get_the_post_thumbnail_url($company[0]->ID, 'medium');
$first_date = $kpis->pdt;
$last_date = bsx_last_pub_date($camp->ID);
if($last_date==$first_date) $last_date = '';
$type = $kpis->ls == -1 ? 'short':'long';
$perf = $kpis->perf;
$perf_class = $perf < 0 ? 'a-red':'a-green';
$link = get_permalink($camp->ID);
$status_class = $status === 'Active' ? 'status-active' : 'status-closed';
$perf_value = $perf ? sprintf("% 6.2f %%", 100 * $perf) : 'N/A';
$link = get_permalink($camp->ID);
?>
<a href="<?php echo $link; ?>">
<div class="ccin-row">
<div class="ccin-rowpart ccin-info-first">
<?php if ($featured_image) { ?>
<div class="ccin-image">
<img src="<?php echo esc_url($featured_image); ?>" alt="<?php echo esc_attr($legal_name);?>">
</div>
<?php } ?>
</div>
<div class="ccin-rowpart ccin-info-2nd">
<div class="ccin-name"><?php echo esc_html($legal_name); ?></div>
</div>
<div class="ccin-rowpart ccin-info-3rd">
<div class="ccin-meta-top"><?php echo esc_html($first_date); ?></div>
<div class="ccin-meta-bottom"><?php echo esc_html($last_date); ?></div>
</div>
<div class="ccin-rowpart ccin-info-4th">
<div class="ccin-status <?php echo $status_class; ?>"><?php echo $status; ?></div>
</div>
<div class="ccin-rowpart ccin-info-5th">
<div class="type-badge"><?php echo esc_html($type); ?></div>
</div>
<div class="ccin-rowpart ccin-info-6th">
<div class="ccin-performance <?php echo $perf_class; ?>"><?php echo $perf_value; ?></div>
</div>
</div>
</a>
<?php } ?>
</div>
<?php
return ob_get_clean();
}
add_shortcode('inline-campaigns','render_inline_campaigns');
function inline_camp_enqueue (){
// -------------------------------------------------------------------------------------------
wp_enqueue_style(
'inline-campaign',
BSX_PLUGIN_URL . 'ass/css/inline-campaign.css',
array(),
'2.0.1'
);
}
add_action('wp_enqueue_scripts','inline_camp_enqueue',30);