<?php

namespace App\Reports\Sources\UsersProfilesAccess;


class PopulateInitialImport extends \App\Reports\Sources\PopulateAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Reports\Sources\PopulateAbstract::populate()
	 */
	public function populate() :void
	{

		// INITS //////////////////////////////////////////////////////////////////////////////////
		global $DBTUserProfiles, $DBTUsersProfilesAccess;

		$reportTable = $this->tableManager->getTableName();

		$activeColumns = array_keys( $this->columnsManager->getStdColumns() );

		$stdFilters = $this->filtersManager->getStdFilters();

		// refresh report status
		$this->reportGenerationStatusUpdate('extractMainDataFromDb', 'runningMainSQL', 0);

		// CREATE SQL FOR COPYING USER PROFILE ACCESS /////////////////////////////////////////////////////

		$reportCopyInfo = new \App\Reports\Db\ManageRecords\Insert($reportTable);
		$reportCopyInfo->setMainTable($DBTUsersProfilesAccess,	'profileAccess');
		$reportCopyInfo->addJoinTable($DBTUserProfiles,			'profile',		'profile.id=profileAccess.idProfile');

		$cols = [
			'idProfile'			=> 'profile.id',
			'profileName'		=> 'profile.name',
			'idSection'			=> 'profileAccess.idSection',
			'type'				=> 'profileAccess.type',
			'optionValue'		=> 'profileAccess.optionValue',
		];

		$reportCopyInfo->setStructureColumns($cols);
		$reportCopyInfo->addStructureColumn("id", 		"CONCAT(profile.id, '|', profileAccess.idSection)", 'profile');
		$reportCopyInfo->addStructureColumn("addTime", 	"FROM_UNIXTIME(profile.addTime)", 'profile');

		$reportCopyInfo->setActiveColumns($activeColumns);

		// ADD FILTERS ////////////////////////////////////////////////////////////////////////////////////
		if ($stdFilters['addDate']['start']) {
			$reportCopyInfo->addFilter('profile.addTime', '>=', strtotime($stdFilters['addDate']['start']." 00:00:00"));
		}
		if ($stdFilters['addDate']['end']) {
			$reportCopyInfo->addFilter('profile.addTime', '<=', strtotime($stdFilters['addDate']['end']." 23:59:59"));
		}

		$reportCopyInfo->addFilter('profile.deleted', '=', '0');

		$reportCopyInfo->populate();

	}

}
