<?php

use Acms\Services\Unit\GroupUnitClassMigrationRepository;

class ACMS_POST_Fix_GroupUnit extends ACMS_POST_Fix
{
    function post()
    {
        if (!sessionWithAdministration()) {
            return $this->Post;
        }
        $Fix = $this->extract('fix', new ACMS_Validator());
        $Fix->setMethod('unit_group_class_target', 'required');
        $Fix->setMethod('unit_group_class_target_blog', 'in', ['descendant']);

        if ($this->Post->isValidAll()) {
            @set_time_limit(0);

            $target = $Fix->get('unit_group_class_target');
            $value  = $Fix->get('unit_group_class_fix');
            $includeChildBlogs = $Fix->get('unit_group_class_target_blog') === 'descendant';

            $repository = new GroupUnitClassMigrationRepository();
            foreach ($this->targetBlog($includeChildBlogs) as $blogId) {
                $repository->rename($blogId, $target, $value);
            }

            $this->Post->set('message', 'success');

            AcmsLogger::info('データ修正ツールで、グループユニットのクラスを変更しました「' . $target . '」->「' . $value . '」');
        }

        return $this->Post;
    }
}
