Last active
August 29, 2015 14:18
-
-
Save heristop/7d3efa80e709a014f42f to your computer and use it in GitHub Desktop.
[SonataAdmin] Create a KPI block with progress bar #AdminLTE #propel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Propel Configuration | |
propel: | |
# ... | |
build_properties: | |
# ... | |
propel.behavior.kpi_comparator.class: 'src.Talad.Bundle.StatsBundle.Behavior.KpiComparatorBehavior' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<database name="default" namespace="Talad\Bundle\CustomerBundle\Model" defaultIdMethod="native"> | |
<table name="customer" phpName="Customer" idMethod="native"> | |
<!-- customer columns --> | |
<column name="creation_date" phpName="CreationDate" type="TIMESTAMP" required="false"/> | |
<behavior name="kpi_comparator" /> | |
</table> | |
</database> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends sonata_block.templates.block_base %} | |
{% block block %} | |
<div class="panel-body"> | |
<div class="col-md-6"> | |
<div class="info-box bg-aqua"> | |
<span class="info-box-icon"><i class="fa fa-shopping-cart"></i></span> | |
<div class="info-box-content"> | |
<span class="info-box-text">New Orders</span> | |
<span class="info-box-number">{{ results['order']['count'] }}</span> | |
<div class="progress"> | |
<div style="width: {{ results['order']['percent'] }}%" class="progress-bar"></div> | |
</div> | |
<span class="progress-description"> | |
{{ results['order']['percent'] }}% <i class="fa fa-chevron-{% if results['order']['percent'] >= 0 %}up{% else %}down{% endif %}"></i> in {{ results['order']['day'] }} days | |
</span> | |
</div><!-- /.info-box-content --> | |
</div><!-- /.info-box --> | |
</div> | |
<div class="col-md-6"> | |
<div class="info-box bg-red"> | |
<span class="info-box-icon"><i class="fa fa-users"></i></span> | |
<div class="info-box-content"> | |
<span class="info-box-text">New Customers</span> | |
<span class="info-box-number">{{ results['customer']['count'] }}</span> | |
<div class="progress"> | |
<div style="width: {{ results['customer']['percent'] }}%" class="progress-bar"></div> | |
</div> | |
<span class="progress-description"> | |
{{ results['customer']['percent'] }}% <i class="fa fa-chevron-{% if results['customer']['percent'] >= 0 %}up{% else %}down{% endif %}"></i> in {{ results['customer']['day'] }} days | |
</span> | |
</div><!-- /.info-box-content --> | |
</div><!-- /.info-box --> | |
</div> | |
</div> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of TaladStatsBundle. | |
* | |
*/ | |
class KpiComparatorBehavior extends Behavior | |
{ | |
public function queryMethods($builder) | |
{ | |
$script = ''; | |
$className = $builder->getStubObjectBuilder()->getClassname(); | |
$objectName = strtolower($className); | |
$peerName = $builder->getStubPeerBuilder()->getClassname(); | |
$builder->declareClassFromBuilder($builder->getStubObjectBuilder()); | |
$script = $this->renderTemplate('queryRecentlyCreated', array()); | |
return $script; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file is part of TaladStatsBundle. | |
* | |
*/ | |
namespace Talad\Bundle\StatsBundle\Block\Service; | |
use Sonata\AdminBundle\Form\FormMapper; | |
use Sonata\CoreBundle\Validator\ErrorElement; | |
use Sonata\BlockBundle\Block\BaseBlockService; | |
use Sonata\BlockBundle\Block\BlockContextInterface; | |
use Sonata\BlockBundle\Model\BlockInterface; | |
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
/** | |
* Class KpiComparatorBlockService. | |
*/ | |
class KpiComparatorBlockService extends BaseBlockService | |
{ | |
protected $day = 10; | |
/** | |
* @param string $name | |
* @param EngineInterface $templating | |
*/ | |
public function __construct($name, EngineInterface $templating) | |
{ | |
parent::__construct($name, $templating); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function execute(BlockContextInterface $blockContext, Response $response = null) | |
{ | |
$compareFor = function($queryClass, $day) { | |
$recentlyCreated = $queryClass::create() | |
->recentlyCreated($day) | |
->count(); | |
$recentlyCreatedBefore = $queryClass::create() | |
->recentlyCreatedBefore($day) | |
->count(); | |
if ($recentlyCreated == 0) { | |
$percent = 0; | |
} else { | |
$percent = round((1 - ($recentlyCreatedBefore / $recentlyCreated)) * 100); | |
} | |
return [ | |
'count' => $recentlyCreated, | |
'previousCount' => $recentlyCreatedBefore, | |
'day' => $day, | |
'percent' => $percent | |
]; | |
}; | |
$results['order'] = $compareFor( | |
'\Talad\Bundle\OrderBundle\Model\OrderHeaderQuery', | |
$this->day | |
); | |
$results['customer'] = $compareFor( | |
'\Talad\Bundle\CustomerBundle\Model\CustomerQuery', | |
$this->day | |
); | |
return $this->renderPrivateResponse($blockContext->getTemplate(), array( | |
'context' => $blockContext, | |
'settings' => $blockContext->getSettings(), | |
'block' => $blockContext->getBlock(), | |
'results' => $results | |
), $response); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function validateBlock(ErrorElement $errorElement, BlockInterface $block) | |
{ | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildEditForm(FormMapper $formMapper, BlockInterface $block) | |
{ | |
$formMapper->add('settings', 'sonata_type_immutable_array', array()); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'KPI'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function setDefaultSettings(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'mode' => 'public', | |
'title' => 'KPI', | |
'template' => 'TaladStatsBundle:Block:kpi_comparator.html.twig', | |
)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<database name="default" namespace="Talad\Bundle\OrderBundle\Model" defaultIdMethod="native"> | |
<table name="order_header" phpName="OrderHeader" idMethod="native"> | |
<!-- order columns --> | |
<column name="creation_date" phpName="CreationDate" type="TIMESTAMP" required="false"/> | |
<behavior name="kpi_comparator" /> | |
</table> | |
</database> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param integer $day | |
*/ | |
public function recentlyCreated($day = 10) | |
{ | |
$date = new \DateTime("now"); | |
$date->modify("-{$day} day"); | |
return $this->filterByCreationDate(array('min' => $date->format('Y-m-d H:i:s'))); | |
} | |
/** | |
* @param integer $day | |
*/ | |
public function recentlyCreatedBefore($day = 10) | |
{ | |
$dateEnd = new \DateTime("now"); | |
$dateEnd->modify("-{$day} day"); | |
$day *= 2; | |
$dateBegin = new \DateTime("now"); | |
$dateBegin->modify("-{$day} day"); | |
return $this->filterByCreationDate(array( | |
'min' => $dateBegin->format('Y-m-d H:i:s'), | |
'max' => $dateEnd->format('Y-m-d H:i:s') | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment