<?php
declare(strict_types=1);
namespace App\Admin\Block;
use Doctrine\ODM\MongoDB\DocumentManager;
use Sonata\BlockBundle\Block\BaseBlockService;
use Symfony\Component\Templating\EngineInterface;
class BaseStatisticBlock extends BaseBlockService
{
/**
* @var DocumentManager
*/
protected $dm;
/**
* StatisticService constructor.
* @param $name
* @param EngineInterface $templating
* @param DocumentManager $dm
*/
public function __construct($name, EngineInterface $templating, DocumentManager $dm)
{
parent::__construct($name, $templating);
$this->dm = $dm;
}
/**
* Return all days for given month.
* @param int $month
* @param int $year
* @return array
*/
protected function monthCreator(int $month, int $year)
{
$list = [];
for($d = 1; $d <= 31; $d++) {
$time = mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time) == $month) {
$list[] = date('j M', $time);
}
}
return $list;
}
/**
* @param $year
* @param $month
* @param $day
* @return array
* @throws \Exception
*/
protected function weeksCreator($year, $month, $day)
{
$first_date = $year . "-" . $month . "-" . $day;
$last_date = date("Y-m-t", strtotime($first_date));
$first_week = date("W", strtotime($first_date));
$last_week = date("W", strtotime($last_date));
if ($last_week == '01') {
$last_week = $first_week + 4;
}
$list = [];
for($w = $first_week; $w <= $last_week; $w++) {
$dto = new \DateTime();
$dto->setISODate((int) $year, (int) $w);
$weekStart = $dto->format('d M');
$dto->modify('+6 days');
$weekEnd = $dto->format('d M');
$list[] = $weekStart . " - " . $weekEnd;
}
return $list;
}
}