<?php

namespace App\Services;

use App\Core\Database;

class HomepageReplicationService
{

public static function replicate(
int $sourceConference,
int $targetConference
): bool
{

$db=
Database::getInstance();

$db
->beginTransaction();

try{

$sections=
$db
->prepare("

SELECT *

FROM conference_home_sections

WHERE conference_id=?

");

$sections
->execute([

$sourceConference

]);

$data=
$sections
->fetchAll();

$db
->prepare("

DELETE

FROM conference_home_sections

WHERE conference_id=?

")

->execute([

$targetConference

]);

foreach(
$data
as $row
){

unset(
$row[
'id'
]
);

$row[
'conference_id'
]=
$targetConference;

$cols=
array_keys(
$row
);

$sql=

"

INSERT INTO
conference_home_sections(

"

.

implode(
',',
$cols
)

.

")

VALUES(

"

.

implode(
',',
array_fill(
0,
count(
$cols
),
'?'
)

)

.

")

";

$db
->prepare(
$sql
)

->execute(

array_values(
$row
)

);

}

$db
->commit();

return true;

}catch(
\Exception $e
){

$db
->rollBack();

return false;

}

}

}
