<?php
/********************************************************************************
 * (c)1337 aheadware.com - All rights reserved
 ********************************************************************************/

require_once __DIR__ . '/../../src/server/server/config/boot_global.php';
require_once __DIR__ . '/../../src/server/server/config/boot_local.php';


function patch_replaceCategoryByCategoryIdInAppointment()
{
    /*
    // Set DB configuration for live teamdata access
    Francis_Utils_Config::set( 'db.tbteamdata.host', 'v034011.kasserver.com' );
    Francis_Utils_Config::set( 'db.tbteamdata.name', 'd02e620f' );
    Francis_Utils_Config::set( 'db.tbteamdata.user', 'd02e620f' );
    Francis_Utils_Config::set( 'db.tbteamdata.pass', 'NT7uSbtaebV9Gedq' );
    */

    $sql = 'SELECT * FROM appointment';
    $appointments = TB_Shared_Ent_TeamData_Appointment::findMany( $sql );

    /** @var TB_Shared_Ent_TeamData_Appointment $a */
    foreach( $appointments as $a )
    {
        $team = TB_Shared_Ent_TeamData_Team::get( $a->team_id );
        if ( $team )
        {
            $categoryId = $team->id . '-0';
            foreach( $team->course_categories_js as $category )
            {
                if ( $category[ 'name' ] === $a->category )
                {
                    $categoryId = $category[ 'id' ];
                    break;
                }
            }
            if ( $categoryId )
            {
                //$a->category = NULL;
                $a->category_id = $categoryId;
                $a->save();
            }
            unset( $team );
        }
    }

    echo "DONE...";
}

patch_replaceCategoryByCategoryIdInAppointment();
