<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/
require_once __DIR__ . '/../../src/server/config/boot_global.php';
require_once __DIR__ . '/../../src/server/config/boot_local.php';

function patch_AddPublicIdToTeams()
{
    // Set DB configuration for live teamdata access
    Francis_Utils_Config::set( 'db.tbteamdata.host', 'XXXXXX' );
    Francis_Utils_Config::set( 'db.tbteamdata.name', 'd01c3281' );
    Francis_Utils_Config::set( 'db.tbteamdata.user', 'd01c3281' );
    Francis_Utils_Config::set( 'db.tbteamdata.pass', 'XXXXXXX' );

    $db = TB_Shared_Db_TeamData::get();
    $sql = 'SELECT * FROM team WHERE public_id IS NULL';
    $stmt = $db->query( $sql );
    $res = $stmt->fetchAll();

    foreach( $res as $row )
    {
        if ( is_array( $row ) && isset( $row[ 'id' ] ) )
        {
            $db->beginTransaction();
            $entTeam = TB_Shared_Ent_TeamData_Team::get( $row[ 'id'  ] );
            if ( $entTeam instanceof  TB_Shared_Ent_TeamData_Team )
            {
                $entTeam->public_id = TB_Server_Utils_Helper::getToken(32);
                $entTeam->save();
            }
            $db->commit();
        }
    }

    echo "DONE...";
}

patch_AddPublicIdToTeams();
