Lecture 2 minutes
Since the Sugar 7.7.1.0 version, SugarCRM introduces a Sugar CLI tool based on Symfony Console. This Sugar CLI tool is under beta version at this moment (August 2016) and can be changed in the future.
We will see in this article how to use this new Sugar CLI to add a command which provides some statistics from the Email Manager Queue. We want to display how many emails by campaign by module are waiting to be send.
The first step is to define the Command itself
To perform this operation we implement
1 |
Sugarcrm\Sugarcrm\Console\CommandRegistry\Mode\InstanceModeInterface |
because our command must be executed only on installed Sugar instance. The only require thing to do that is to describe the command name, the description and the help on the configure()
method and to implement our logic in the execute()
method. We are using a SugarQuery
to retrieve the data and to display the result through a table. We can imagine to externalize this part and use it in an API and unit test it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
//File: custom/Synolia/EmailMan/Console/Command/ListQueueCommand.php namespace Synolia\EmailMan\Console\Command; use Sugarcrm\Sugarcrm\Console\CommandRegistry\Mode\InstanceModeInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\TableSeparator; /** * * Email Manager Queue statistics * */ class ListQueueCommand extends Command implements InstanceModeInterface { /** * {inheritdoc} */ protected function configure() { $this ->setName('synolia:emailman:queue') ->setDescription('Show Email Manager Queue statistics') ->setHelp('This command displays statistics from Email Manager Queue.') ; } /** * {inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $result = $this->getSugarQuery()->execute(); $nbEmailsToSent = 0; $table = new Table($output); $table->setHeaders(array('Campaign', 'Module', 'Count')); foreach ($result as $row) { $table->addRow(array($row['name'], $row['related_type'], $row['record_count'])); $nbEmailsToSent += $row['record_count']; } $table->addRow(new TableSeparator()); $table->addRow(array('# Emails to send', '', $nbEmailsToSent)); $table->render(); } /** * @return \SugarQuery * @throws \SugarQueryException */ protected function getSugarQuery() { $sq = new \SugarQuery(); $sq->from(\BeanFactory::newBean('EmailMan')) ->joinTable('campaigns', array( 'alias' => 'campaigns', 'joinType' => 'LEFT', 'linkingTable' => true) )->on()->equalsField('campaigns.id', 'emailman.campaign_id'); $sq->select(array('campaigns.name', 'emailman.related_type'))->setCountQuery(); $sq->groupBy('campaigns.name') ->groupBy('emailman.related_type'); return $sq; } } |
Declare the Command
Now we need to set this command available by using the application/Ext/Console
framework:
1 2 3 |
//File: custom/Extension/application/Ext/Console/SynoliaEmailManListQueueCommand.php Sugarcrm\Sugarcrm\Console\CommandRegistry\CommandRegistry::getInstance() ->addCommand(new Synolia\EmailMan\Console\Command\ListQueueCommand()); |
Add our namespace
To use our own namespace we can follow one of the way describe in our previous article by using the application/Ext/Utils
framework:
1 2 |
//File: custom/Extension/application/Ext/Utils/SynoliaEmailManConsoleCommandNamespace.php SugarAutoLoader::addNamespace('Synolia\\EmailMan\\Console\\Command', 'custom/Synolia/EmailMan/Console/Command', 'psr4'); |
Perform a Quick Repair and Rebuild et voilà !
Thanks to Jelle Vink about his presentation of this new Sugar CLI tools at the Uncon 2016
You can find more information about Sugar CLI on the Sugar Developer Guide