Rename the plugin's vendor/namespace from experiential/Program to mt3s/MT3SMarketingProgram (composer: mt3s/mautic-marketing-program-bundle): - ProgramBundle -> MT3SMarketingProgramBundle; extension, integration, config support, permissions, events and twig extension renamed likewise - Initiative* -> Program*: controller (InitiativeController -> ProgramController), model, entities, repositories, form types and views (new _tabs/_task_table/_tab_table_filter partials, details page) - Settings config form theme -> _config_mt3smarketingprogramconfig_widget; routes and translations re-prefixed (mt3s_marketing_program_*) - Assets: icon.svg -> mt3s-mp-icon.svg; program.js -> mt3s-mp-program.js (OnLoad hooks renamed mt3sMpProgram*) - Add Command/RefreshStatsCommand; Migrations/Version_0_17_0.php renames the tables to mt3s_mp_program_*
135 lines
4.7 KiB
PHP
135 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MauticPlugin\MT3SMarketingProgramBundle\Command;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Program;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramAllocationItem;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramAudience;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramDescriptionItem;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramObjective;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\ProgramStakeholder;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\Task;
|
|
use MauticPlugin\MT3SMarketingProgramBundle\Entity\TaskStatus;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
#[AsCommand(
|
|
name: 'mt3s:marketing-program:refresh-stats',
|
|
description: 'Recomputes and prints the per-program statistics (tasks, objectives, insights, allocations, stakeholders, audiences) from the tables.'
|
|
)]
|
|
class RefreshStatsCommand extends Command
|
|
{
|
|
public function __construct(private readonly EntityManagerInterface $entityManager)
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->addOption('program', null, InputOption::VALUE_REQUIRED, 'Limit the report to one program ID.');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$io = new SymfonyStyle($input, $output);
|
|
$programId = $input->getOption('program');
|
|
|
|
$programs = $this->entityManager->getRepository(Program::class)->findBy(
|
|
null !== $programId ? ['id' => (int) $programId] : []
|
|
);
|
|
|
|
if ([] === $programs) {
|
|
$io->warning(null !== $programId ? sprintf('No program with the ID %s.', $programId) : 'No programs.');
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
$counts = [
|
|
'tasks' => $this->countByProgram(Task::class),
|
|
'objectives' => $this->countByProgram(ProgramObjective::class),
|
|
'insights' => $this->countByProgram(ProgramDescriptionItem::class),
|
|
'allocations' => $this->countByProgram(ProgramAllocationItem::class),
|
|
'stakeholders' => $this->countByProgram(ProgramStakeholder::class),
|
|
'audiences' => $this->countByProgram(ProgramAudience::class),
|
|
];
|
|
|
|
$done = $this->doneByProgram();
|
|
|
|
$rows = [];
|
|
foreach ($programs as $program) {
|
|
$id = (string) $program->getId();
|
|
$rows[] = [
|
|
$id,
|
|
$program->getName(),
|
|
sprintf('%d / %d', $done[$id] ?? 0, $counts['tasks'][$id] ?? 0),
|
|
$counts['objectives'][$id] ?? 0,
|
|
$counts['insights'][$id] ?? 0,
|
|
$counts['allocations'][$id] ?? 0,
|
|
$counts['stakeholders'][$id] ?? 0,
|
|
$counts['audiences'][$id] ?? 0,
|
|
];
|
|
}
|
|
|
|
$io->title('MT3S Marketing Program statistics');
|
|
$io->table(
|
|
['ID', 'Name', 'Done / tasks', 'Objectives', 'Insights', 'Allocations', 'Stakeholders', 'Audiences'],
|
|
$rows
|
|
);
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* Rows of the given entity class grouped by their program: [program id => count].
|
|
*
|
|
* @param class-string $entityClass
|
|
*
|
|
* @return array<string, int>
|
|
*/
|
|
private function countByProgram(string $entityClass): array
|
|
{
|
|
$rows = $this->entityManager->getRepository($entityClass)
|
|
->createQueryBuilder('c')
|
|
->select('IDENTITY(c.program) AS program_id, COUNT(c.id) AS total')
|
|
->groupBy('program_id')
|
|
->getQuery()
|
|
->getArrayResult();
|
|
|
|
$counts = [];
|
|
foreach ($rows as $row) {
|
|
$counts[(string) $row['program_id']] = (int) $row['total'];
|
|
}
|
|
|
|
return $counts;
|
|
}
|
|
|
|
/**
|
|
* Done tasks per program: [program id => done count].
|
|
*
|
|
* @return array<string, int>
|
|
*/
|
|
private function doneByProgram(): array
|
|
{
|
|
$rows = $this->entityManager->getRepository(Task::class)
|
|
->createQueryBuilder('t')
|
|
->select('IDENTITY(t.program) AS program_id, SUM(CASE WHEN t.status = :done THEN 1 ELSE 0 END) AS done')
|
|
->setParameter('done', TaskStatus::Done)
|
|
->groupBy('program_id')
|
|
->getQuery()
|
|
->getArrayResult();
|
|
|
|
$done = [];
|
|
foreach ($rows as $row) {
|
|
$done[(string) $row['program_id']] = (int) $row['done'];
|
|
}
|
|
|
|
return $done;
|
|
}
|
|
} |