<?php
$tmp = '../storage/app/';
$conn = new mysqli('localhost', 'will_refer', 'will#123qwe!', 'will_refer_2');
//$conn = new mysqli('localhost', 'root', '', 'bu_1');
$conn->set_charset('utf8');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$result = $conn->query('SELECT id, file_path FROM relation_article_photos WHERE file_path like "%public/sai%"');
if($result->num_rows > 0) {
    echo 'Count Article Photos: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = str_replace('public/sai', 'public/company_2/sai', $row->file_path);
        $sql = 'UPDATE relation_article_photos SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;
        echo $new_file_path.'- Done<hr>';
        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}
//Articles
/*$result = $conn->query('SELECT building_id, floor_file_path, sheet_file_path2, sheet_file_path3, company_id FROM articles WHERE floor_file_path like "%public/floor%" OR sheet_file_path2 like "%public/sheet%" OR sheet_file_path3 like "%public/sheet%"');
if($result->num_rows > 0) {
    echo 'Count Articles: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_floor_file_path = $row->floor_file_path;
        if($row->floor_file_path != '' AND strpos($row->floor_file_path, "company_2") === false){
            $new_floor_file_path = str_replace('public/floor', 'public/company_2/floor', $row->floor_file_path);
            if(file_exists($tmp.$row->floor_file_path)){
                copy($tmp.$row->floor_file_path, $tmp.$new_floor_file_path);
                echo $new_floor_file_path.'- Done<hr>';
            }else{
                echo 'Error<hr>';
            }
        }

        $new_sheet_file_path2 = $row->sheet_file_path2;
        if($row->sheet_file_path2 != '' AND strpos($row->sheet_file_path2, "company_2") === false){
            $new_sheet_file_path2 = str_replace('public/sheet', 'public/company_2/sheet', $row->sheet_file_path2);
            if(file_exists($tmp.$row->sheet_file_path2)){
                copy($tmp.$row->sheet_file_path2, $tmp.$new_sheet_file_path2);
            }
        }

        $new_sheet_file_path3 = $row->sheet_file_path3;
        if($row->sheet_file_path3 != '' AND strpos($row->sheet_file_path3, "company_2") === false){
            $new_sheet_file_path3 = str_replace('public/sheet', 'public/company_2/sheet', $row->sheet_file_path3);
            if(file_exists($tmp.$row->sheet_file_path3)){
                copy($tmp.$row->sheet_file_path3, $tmp.$new_sheet_file_path3);
            }
        }

        $sql = 'UPDATE articles SET floor_file_path = "'.$new_floor_file_path.'", sheet_file_path2 = "'.$new_sheet_file_path2.'", sheet_file_path3 = "'.$new_sheet_file_path3.'" WHERE building_id = '.$row->building_id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

    }
}
$result = $conn->query('SELECT building_id, floor_file_path, sheet_file_path2, sheet_file_path3, company_id FROM articles WHERE floor_file_path like "%public/sai%"');
if($result->num_rows > 0) {
    echo 'Count Articles: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_floor_file_path = $row->floor_file_path;
        if($row->floor_file_path != '' AND strpos($row->floor_file_path, "company_2") === false){
            $new_floor_file_path = str_replace('public/sai', 'public/company_2/sai', $row->floor_file_path);
        }

        $sql = 'UPDATE articles SET floor_file_path = "'.$new_floor_file_path.'" WHERE building_id = '.$row->building_id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

    }
}

$result = $conn->query('SELECT id, file_path FROM facilities WHERE file_path like "%public/facility%"');
if($result->num_rows > 0) {
    echo 'Count Facility: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/facility', 'public/company_2/facility', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo $new_file_path.'- Done<hr>';
            }else{
                echo 'Error<hr>';
            }
        }
        $sql = 'UPDATE facilities SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

$result = $conn->query('SELECT id, file_path FROM news WHERE file_path like "%public/news%"');
if($result->num_rows > 0) {
    echo 'Count News: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/news', 'public/company_2/news', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo 'Done !<hr>';
            }
        }
        $sql = 'UPDATE news SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

//reforms
$result = $conn->query('SELECT id, before_file_path, after_file_path FROM reforms WHERE before_file_path like "%public/sheet%" OR after_file_path like "%public/sheet%"');
if($result->num_rows > 0) {
    echo 'Count Reforms: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_before_file_path = $row->before_file_path;
        if($row->before_file_path != '' AND strpos($row->before_file_path, "company_2") === false){
            $new_before_file_path = str_replace('public/sheet', 'public/company_2/sheet', $row->before_file_path);
            if(file_exists($tmp.$row->before_file_path)){
                copy($tmp.$row->before_file_path, $tmp.$new_before_file_path);
            }
        }

        $new_after_file_path = $row->after_file_path;
        if($row->after_file_path != '' AND strpos($row->after_file_path, "company_2") === false){
            $new_after_file_path = str_replace('public/sheet', 'public/company_2/sheet', $row->after_file_path);
            if(file_exists($tmp.$row->after_file_path)){
                copy($tmp.$row->after_file_path, $tmp.$new_after_file_path);
                echo $new_after_file_path.'<hr>';
            }
        }

        $sql = 'UPDATE reforms SET before_file_path = "'.$new_before_file_path.'", after_file_path = "'.$new_after_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

    }
}

$result = $conn->query('SELECT id, file_path FROM relation_article_photos WHERE file_path like "%public/photo%"');
if($result->num_rows > 0) {
    echo 'Count Article Photos: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/photo', 'public/company_2/photo', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo $new_file_path.'<hr>';
            }
        }
        $sql = 'UPDATE relation_article_photos SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

$result = $conn->query('SELECT id, file_path FROM user_infos WHERE file_path like "%public/staff%"');
if($result->num_rows > 0) {
    echo 'Count User Info: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/staff', 'public/company_2/staff', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo $new_file_path.'<hr>';
            }
        }
        $sql = 'UPDATE user_infos SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

$result = $conn->query('SELECT id, file_path FROM relation_store_photos WHERE file_path like "%public/store%"');
if($result->num_rows > 0) {
    echo 'Count Store: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/store', 'public/company_2/store', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo $new_file_path.'<hr>';
            }
        }
        $sql = 'UPDATE relation_store_photos SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}
*/
$result = $conn->query('SELECT id, file_path FROM relation_reform_photos WHERE file_path like "%public/reform%"');
if($result->num_rows > 0) {
    echo 'Count Reform Photo: '.$result->num_rows.'<br>';
    while($row = mysqli_fetch_object($result)) {
        $new_file_path = $row->file_path;
        if($row->file_path != '' AND strpos($row->file_path, "company_2") === false){
            $new_file_path = str_replace('public/reform', 'public/company_2/reform', $row->file_path);
            if(file_exists($tmp.$row->file_path)){
                copy($tmp.$row->file_path, $tmp.$new_file_path);
                echo $new_file_path.'<hr>';
            }
        }
        $sql = 'UPDATE relation_reform_photos SET file_path = "'.$new_file_path.'" WHERE id = '.$row->id;

        if ($conn->query($sql) === FALSE){
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}
?>
