. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Statistics\Google; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Statistics\AbstractGoogle; use Fisharebest\Webtrees\Statistics\Helper\Century; use Fisharebest\Webtrees\Tree; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Query\JoinClause; /** * */ class ChartNoChildrenFamilies extends AbstractGoogle { /** * @var Century */ private $centuryHelper; /** * Constructor. * * @param Tree $tree */ public function __construct(Tree $tree) { parent::__construct($tree); $this->centuryHelper = new Century(); } /** * Returns the related database records. * * @param int $year1 * @param int $year2 * * @return \stdClass[] */ private function queryRecords(int $year1, int $year2): array { $query = DB::table('families') ->selectRaw('ROUND((d_year - 50) / 100) AS century') ->selectRaw('COUNT(*) AS total') ->join('dates', function (JoinClause $join) { $join->on('d_file', '=', 'f_file') ->on('d_gid', '=', 'f_id'); }) ->where('f_file', '=', $this->tree->id()) ->where('f_numchil', '=', 0) ->where('d_fact', '=', 'MARR') ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@']) ->groupBy(['century']) ->orderBy('century'); if ($year1 >= 0 && $year2 >= 0) { $query->whereBetween('d_year', [$year1, $year2]); } return $query->get()->all(); } /** * Create a chart of children with no families. * * @param int $no_child_fam The number of families with no children * @param int $year1 * @param int $year2 * * @return string */ public function chartNoChildrenFamilies( int $no_child_fam, int $year1 = -1, int $year2 = -1 ): string { $data = [ [ I18N::translate('Century'), I18N::translate('Total') ] ]; $total = 0; foreach ($this->queryRecords($year1, $year2) as $record) { $total += $record->total; $data[] = [ $this->centuryHelper->centuryName((int) $record->century), $record->total ]; } $data[] = [ I18N::translateContext('unknown century', 'Unknown'), $no_child_fam - $total ]; return view( 'statistics/other/charts/column', [ 'data' => $data, 'colors' => ['#84beff'], 'chart_title' => I18N::translate('Number of families without children'), 'hAxis_title' => I18N::translate('Century'), 'vAxis_title' => I18N::translate('Total families'), ] ); } }