TrackManiaControl/application/core/Database/MigrationHelper.php

74 lines
1.8 KiB
PHP
Raw Normal View History

2014-04-27 21:31:55 +02:00
<?php
namespace ManiaControl\Database;
2014-05-02 17:40:47 +02:00
2014-04-27 21:31:55 +02:00
use ManiaControl\ManiaControl;
2014-05-02 17:40:47 +02:00
use ManiaControl\Settings\SettingManager;
2014-04-27 21:31:55 +02:00
/**
* Database Migration Assistant
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
2014-04-27 21:31:55 +02:00
*/
class MigrationHelper {
/*
* Private Properties
*/
private $maniaControl = null;
/**
* Construct Migration Helper
2014-05-02 17:40:47 +02:00
*
2014-04-27 21:31:55 +02:00
* @param ManiaControl $maniaControl
*/
public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl;
}
/**
* Transfer the Settings of the given Class to a new One
*
* @param mixed $sourceClass
* @param mixed $targetClass
* @return bool
*/
public function transferSettings($sourceClass, $targetClass) {
$sourceClass = $this->getClass($sourceClass);
$targetClass = $this->getClass($targetClass);
2014-05-02 17:40:47 +02:00
2014-04-27 21:31:55 +02:00
$mysqli = $this->maniaControl->database->mysqli;
2014-05-02 17:40:47 +02:00
$query = "INSERT INTO `" . SettingManager::TABLE_SETTINGS . "` (`class`, `setting`, `type`, `value`, `default`)
2014-04-27 21:31:55 +02:00
SELECT ?, `setting`, `type`, `value`, `default` FROM `" . SettingManager::TABLE_SETTINGS . "` WHERE `class` = ?;";
$statement = $mysqli->prepare($query);
if ($mysqli->error) {
trigger_error($mysqli->error);
return false;
}
$statement->bind_param('ss', $targetClass, $sourceClass);
if ($statement->error) {
trigger_error($statement->error);
$statement->close();
return false;
}
$success = $statement->execute();
$statement->close();
return $success;
}
/**
* Get the Class of the given Object
*
* @param mixed $class
* @return string
*/
private function getClass($class) {
if (is_object($class)) {
return get_class($class);
}
2014-05-02 17:40:47 +02:00
return (string)$class;
2014-04-27 21:31:55 +02:00
}
}