Skip to content
Snippets Groups Projects
Commit 553773f0 authored by sluc23's avatar sluc23
Browse files

campaign tree graph

parent 2425deca
No related branches found
Tags 5.2
1 merge request!4campaign tree graph
......@@ -34,6 +34,22 @@ class CRM_CampaignManager_BAO_CampaignTree extends CRM_CampaignManager_DAO_Campa
$path . $campaign['id'] . self::SEPARATOR
);
}
}
/**
* Returns ancestors id
*
* @param string $path
* @param bool $fullPath
*
* @return array
*/
public static function getAncestors($campaignId, $path, $fullPath = TRUE) {
$pathArray = array_filter(explode(\CRM_CampaignManager_BAO_CampaignTree::SEPARATOR, $path), 'strlen');
if (!$fullPath) {
$pathArray = array_slice($pathArray, array_search($campaignId, $pathArray) - 1);
}
return $pathArray;
}
......
......@@ -59,7 +59,7 @@ class CRM_CampaignManager_Form_Campaign_View extends CRM_Core_Form {
$this->assign('action', CRM_Core_Action::VIEW);
$this->assign('campaign', $campaign);
// get KPIs
// KPIs
$allKPIs = \Civi\CampaignManager\Utils\ClassScanner::scanClasses('Civi\CampaignManager\KPI', 'Civi\CampaignManager\KPI\AbstractKPI', 'kpi');
$campaignKpis = \Civi\Api4\CampaignKPI::get()
->addSelect('name', 'title', 'campaign_kpi_value.value', 'campaign_kpi_value.value_parent', 'campaign_kpi_value.last_modified_date')
......@@ -94,9 +94,21 @@ class CRM_CampaignManager_Form_Campaign_View extends CRM_Core_Form {
];
}
}
$this->assign('kpis', $kpis);
// Tree Chart
$campaignTree = \Civi\Api4\CampaignTree::getFamily(TRUE)
->setCampaignId($campaignId)
->addSelect('campaign_id', 'path', 'depth')
->addChain('campaign',
\Civi\Api4\Campaign::get(TRUE)
->addSelect('title')
->addWhere('id', '=', '$campaign_id'),
0
)
->execute();
$mermaid = \Civi\CampaignManager\Utils\Mermaid::drawTree($campaignTree, $campaignId);
$this->assign('mermaid', $mermaid);
}
/**
......
......@@ -102,8 +102,8 @@ class Calculate extends \Civi\Api4\Generic\AbstractAction {
// add to parent totals, propagate upwards
if (isset($campaignTree['campaign_id.parent_id'])) {
// only propagates upward until campaign selected
if ($depth > $treeLimits['min']) {
$ancestors = $this->getAncestors($campaignTree['path'], FALSE);
if ($depth >= $treeLimits['min']) {
$ancestors = CampaignTree::getAncestors($this->campaignId, $campaignTree['path'], FALSE);
foreach ($ancestors as $ancestor) {
$campaignKPIValues[$ancestor]['value_parent'] += $value;
}
......
......@@ -82,7 +82,7 @@ class CalculateAll extends \Civi\Api4\Generic\AbstractAction {
if ($this->parentValue) {
$campaignKPIValues[$kpi['id']][$campaign['id']]['value_parent'] += $value;
if (isset($campaign['parent_id'])) {
$ancestors = $this->getAncestors($campaign['campaign_tree.path']);
$ancestors = \CRM_CampaignManager_BAO_CampaignTree::getAncestors($this->campaignId, $campaign['campaign_tree.path']);
// Loop by tree branch to update parent values
foreach ($ancestors as $ancestor_id) {
$campaignKPIValues[$kpi['id']][$ancestor_id]['value_parent'] += $value;
......
<?php
namespace Civi\Api4\Action\CampaignTree;
use CRM_CampaignManager_BAO_CampaignTree as CampaignTreeBAO;
/**
* CampaignTree.GetFamily action.
*
* @see \Civi\Api4\Generic\BasicGetAction
*
* @package Civi\Api4\Action\CampaignTree
*/
class GetFamily extends \Civi\Api4\Generic\BasicGetAction {
/**
* ID of campaign
*
* @var int
* @required
*/
protected $campaignId;
/**
* Get Ancestors
*
* @var bool
*/
protected $getAncestors = TRUE;
/**
* Get Descendants
*
* @var bool
*/
protected $getDescendants = TRUE;
/**
* Get Siblings
*
* @var bool
*/
protected $getSiblings = TRUE;
/**
* Override getRecords.
*
* @see BasicGetAction::getRecords
*
* @return array
*/
protected function getRecords() {
$campaignPath = \Civi\Api4\CampaignTree::get(TRUE)
->addSelect('path')
->addWhere('campaign_id', '=', $this->campaignId)
->execute()
->first()['path'];
$campaignTreeApi = \Civi\Api4\CampaignTree::get();
$campaignTreeApi->addSelect('campaign_id.parent_id', 'id', 'campaign_id', 'path', 'depth');
$campaignTreeApi->addOrderBy('depth', 'ASC');
$clauses = [
['campaign_id', '=', $this->campaignId],
];
if ($this->getAncestors) {
$ancestors = CampaignTreeBAO::getAncestors($this->campaignId, $campaignPath, TRUE);
if (!empty($ancestors)) {
$clauses[] = ['campaign_id', 'IN', $ancestors];
}
}
if ($this->getDescendants) {
$regexp = '^' . $campaignPath . $this->campaignId . CampaignTreeBAO::SEPARATOR . '([0-9].)*';
$clauses[] = ['path', 'REGEXP', $regexp];
}
if ($this->getSiblings) {
// only get siblings if it's not top parent category
if ($campaignPath != CampaignTreeBAO::SEPARATOR) {
$clauses[] = ['path', '=', $campaignPath];
}
}
$campaignTreeApi->addClause('OR', $clauses);
$campaignTree = (array) $campaignTreeApi->execute()->indexBy('id');
return $campaignTree;
}
}
......@@ -8,21 +8,4 @@ namespace Civi\Api4\Traits;
*/
trait CampaignKPITrait {
/**
* Returns ancestors id
*
* @param string $path
* @param bool $fullPath
*
* @return array
*/
private function getAncestors($path, $fullPath = TRUE) {
$pathArray = array_filter(explode(\CRM_CampaignManager_BAO_CampaignTree::SEPARATOR, $path), 'strlen');
if (!$fullPath) {
$pathArray = array_slice($pathArray, array_search($this->campaignId, $pathArray) - 1);
}
return $pathArray;
}
}
<?php
namespace Civi\CampaignManager\Utils;
use CRM_CampaignManager_BAO_CampaignTree as CampaignTreeBAO;
class Mermaid {
/**
* Returns Mermaid schema for campaign tree
*/
public static function drawTree($campaignTree, $currentCampaignId) {
// Create mermaid.
$mermaid = [
'flowchart TD',
];
foreach ($campaignTree as $campaign) {
$mermaid[] = "id{$campaign['campaign_id']}[{$campaign['campaign']['title']}]";
}
foreach ($campaignTree as $campaign) {
$parentId = CampaignTreeBAO::getAncestors($campaign['campaign_id'], $campaign['path'], FALSE);
$parentId = $parentId[0] ?? '';
if (!empty($parentId)) {
$mermaid[] = "id{$parentId} --> id{$campaign['campaign_id']}";
}
}
foreach ($campaignTree as $campaign) {
if ($campaign['campaign_id'] != $currentCampaignId) {
$url = \CRM_Utils_System::url('civicrm/campaign/view', 'id=' . $campaign['campaign_id'], TRUE);
$mermaid[] = "click id{$campaign['campaign_id']} href \"{$url}\" _blank";
}
}
$mermaidStyle = [
"classDef default font-family:inherit,font-size: 14px",
"style id{$currentCampaignId} fill:#c0c0c0,stroke:#333,stroke-width:4px",
];
return implode("\n ", array_merge($mermaid, $mermaidStyle));
}
}
......@@ -9,8 +9,8 @@ This extension is not compatible with original extension `de.systopia.campaign`.
### Requirements
* PHP v8.0+
* CiviCRM 5.63+
* PHP v8.1+
* CiviCRM 5.69+
* Extension `de.systopia.campaign` must be disabled before installation.
## Usage
......
......@@ -74,7 +74,7 @@ function campaignmanager_civicrm_buildForm($formName, &$form) {
}
$campaigns = $campaignAPI->execute()->indexBy('id')->column('title');
$form->addElement('select', 'parent_id', E::ts('Parent ID'),
$form->addElement('select', 'parent_id', E::ts('Parent Campaign'),
['' => E::ts('- select Parent -')] + $campaigns,
['class' => 'crm-select2']
);
......
......@@ -159,10 +159,11 @@
</tr>
</table>
</div>
<div class="col-lg-7 kpi-wrap">
<h3>{ts}Key Performance Indicators{/ts}</h3>
{* KPIS table *}
<table class="row-highlight display kpi-table">
<div class="col-lg-7">
<div class="kpi-wrap">
<h3>{ts}Key Performance Indicators{/ts}</h3>
{* KPIS table *}
<table class="row-highlight display kpi-table">
<thead class="sticky">
<tr class="columnheader">
<th style="text-align:center;">{ts}KPI{/ts}</th>
......@@ -203,7 +204,25 @@
</tr>
{/foreach}
</tbody>
</table>
</table>
</div>
{if $mermaid}
<div class="">
<h3>{ts}Campaign Tree Chart{/ts}</h3>
{* Tree graph *}
<pre id="treechart" class="mermaid" style="display:none;text-align: center;">{$mermaid}</pre>
<script type=module >
{literal}
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10.0.2/dist/mermaid.esm.min.mjs'
mermaid.initialize({
startOnLoad:true
});
document.getElementById('treechart').style.display = 'block';
{/literal}
</script>
</div>
{/if}
</div>
</div>
<div class="row">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment