<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once 'include/dir_inc.php';
require_once dirname(__DIR__) . '/modules/AnalyticReporting/profiles/FunctionWrapper.php';

function post_install()
{
	//install table for user management
	global $db;
	if (!$db->tableExists('so_users')) {

		$fieldDefs = array(
			'id' => array (
			  'name' => 'id',
			  'vname' => 'LBL_ID',
			  'type' => 'id',
			  'required' => true,
			  'reportable' => true,
			),
			'deleted' => array (
				'name' => 'deleted',
				'vname' => 'LBL_DELETED',
				'type' => 'bool',
				'default' => '0',
				'reportable' => false,
				'comment' => 'Record deletion indicator',
			),
			'shortname' => array (
				'name' => 'shortname',
				'vname' => 'LBL_SHORTNAME',
				'type' => 'varchar',
				'len' => 255,
			),
			'user_id' => array (
				'name' => 'user_id',
				'rname' => 'user_name',
				'module' => 'Users',
				'id_name' => 'user_id',
				'vname' => 'LBL_USER_ID',
				'type' => 'relate',
				'isnull' => 'false',
				'dbType' => 'id',
				'reportable' => true,
				'massupdate' => false,
			),
		);
		
		$indices = array(
			'id' => array (
				'name' => 'so_userspk',
				'type' => 'primary',
				'fields' => array (
					0 => 'id',
				),
			),
			'shortname' => array (
				'name' => 'shortname',
				'type' => 'index',
				'fields' => array (
					0 => 'shortname',
				),
			),
		);
		$db->createTableParams('so_users',$fieldDefs,$indices);
	}

	// Quick Repair & Rebuild
	$module = array('All Modules');
	$selected_actions = array('clearAll');
	require_once 'modules/Administration/QuickRepairAndRebuild.php';
	$randc = new RepairAndClear();
	$randc->repairAndClearAll($selected_actions, $module, false, false);

	$arScheduler = BeanFactory::getBean('Schedulers');
	$arScheduler->retrieve_by_string_fields(array('job' => 'function::sendScheduledReports','deleted' => '0' ));
	if(empty($arScheduler->id)){
		$arScheduler->name = 'Sapiens.BI Send Scheduled Reports';
		$arScheduler->job = 'function::sendScheduledReports';
		$arScheduler->date_time_start = '2005-01-01 00:00:00';
		$arScheduler->job_interval = '*::*::*::*::*';
		$arScheduler->status = 'Active';
		$arScheduler->catch_up = '1';
		$arScheduler->save();
	}

	$arJobs = BeanFactory::getBean('Schedulers');
	$arJobs->retrieve_by_string_fields(array('job' => 'function::executeARJobs','deleted' => '0' ));
	if(empty($arJobs->id)){
		$arJobs->name = 'Sapiens.BI Jobs';
		$arJobs->job = 'function::executeARJobs';
		$arJobs->date_time_start = '2005-01-01 00:00:00';
		$arJobs->job_interval = '*::*::*::*::*';
		$arJobs->status = 'Active';
		$arJobs->catch_up = '1';
		$arJobs->save();
	}

	$arScheduler = BeanFactory::getBean('Schedulers');
	$arScheduler->retrieve_by_string_fields(array('job' => 'function::sendScheduledDashboards','deleted' => '0' ));
	if(empty($arScheduler->id)){
		$arScheduler->name = 'Sapiens.BI Send Scheduled Dashboards';
		$arScheduler->job = 'function::sendScheduledDashboards';
		$arScheduler->date_time_start = '2005-01-01 00:00:00';
		$arScheduler->job_interval = '*::*::*::*::*';
		$arScheduler->status = 'Active';
		$arScheduler->catch_up = '1';
		$arScheduler->save();
	}

    // Suitecrm 8.x legacy view support
    global $suitecrm_version;
    if(!empty($suitecrm_version) && version_compare($suitecrm_version, '8.0.0') >= 0) {
        addSuiteLegacyRouting();
    }
}

/**
 * Copy ../extensions/AnalyticReporting/config/module_routing.php
 * to  ../../extensions/AnalyticReporting/config/module_routing.php
 *
 * @return void
 */
function addSuiteLegacyRouting()
{
    // Retrieve source file
    $moduleName = "AnalyticReporting";
    $filePath = dirname(__DIR__)."/extensions/".$moduleName."/config/module_routing.php";
    $contents = FunctionWrapper::_file_get_contents($filePath);

    // Create directory and copy file to destination
    $dirPath = "../../extensions/".$moduleName."/config";
    FunctionWrapper::_mkdir($dirPath, 0755, true);
    if(FunctionWrapper::_is_dir($dirPath)) {
        $filePath = $dirPath. "/module_routing.php";
        FunctionWrapper::_file_put_contents($filePath, $contents);
        echo "Enabled SuiteCRM 8.x legacy routing. Please run <b>/bin/console cache:clear</b><br />\n";
    } else {
        echo "<b>Failed to create directory ({$dirPath}) for SuiteCRM 8.x legacy routing. Please apply patch manually.</b><br />\n";
    }
}