<?php
/*
 * Admin Page Framework v3.9.1 by Michael Uno
 * Compiled with Admin Page Framework Compiler <https://github.com/michaeluno/admin-page-framework-compiler>
 * <https://en.michaeluno.jp/admin-page-framework>
 * Copyright (c) 2013-2022, Michael Uno; Licensed under MIT <https://opensource.org/licenses/MIT>
 */

class AdminPageFramework_Form_Model___FormatFieldsets extends AdminPageFramework_Form_Base {
    public $aSectionsets = array();
    public $aFieldsets = array();
    public $sStructureType = '';
    public $sCapability = '';
    public $aCallbacks = array( 'fieldset_before_output' => null, 'fieldset_after_formatting' => null, );
    public $aSavedData = array();
    public $oCallerForm;
    public function __construct()
    {
        $_aParameters = func_get_args() + array( $this->aFieldsets, $this->aSectionsets, $this->sStructureType, $this->aSavedData, $this->sCapability, $this->aCallbacks, $this->oCallerForm, );
        $this->aFieldsets = $_aParameters[ 0 ];
        $this->aSectionsets = $_aParameters[ 1 ];
        $this->sStructureType = $_aParameters[ 2 ];
        $this->aSavedData = $_aParameters[ 3 ];
        $this->sCapability = $_aParameters[ 4 ];
        $this->aCallbacks = $_aParameters[ 5 ];
        $this->oCallerForm = $_aParameters[ 6 ];
    }
    public function get()
    {
        $this->aFieldsets = $this->___getFieldsetsFormatted($this->aFieldsets, $this->aSectionsets, $this->sCapability);
        return $this->___getDynamicElementsAddedToFieldsets();
    }
    private function ___getDynamicElementsAddedToFieldsets()
    {
        $_oDynamicElements = new AdminPageFramework_Form_Model___FormatDynamicElements($this->aSectionsets, $this->aFieldsets, $this->aSavedData);
        return $_oDynamicElements->get();
    }
    private function ___getFieldsetsFormatted(array $aFieldsets, array $aSectionsets, $sCapability)
    {
        $aFieldsets = $this->callBack($this->aCallbacks[ 'fieldsets_before_formatting' ], array( $aFieldsets, $aSectionsets ));
        $_aNewFieldsets = array();
        foreach ($aFieldsets as $_sSectionPath => $_aItems) {
            if (! isset($aSectionsets[ $_sSectionPath ])) {
                continue;
            }
            $_aNewFieldsets[ $_sSectionPath ] = $this->___getItemsFormatted($_sSectionPath, $_aItems, $this->getElement($aSectionsets, array( $_sSectionPath, 'capability', ), $sCapability), $aSectionsets);
        }
        $this->___sortFieldsBySectionsOrder($_aNewFieldsets, $aSectionsets);
        return $_aNewFieldsets;
    }
    private function ___getItemsFormatted($sSectionPath, $aItems, $sCapability, $aSectionsets)
    {
        $_abSectionRepeatable = $this->getElement($aSectionsets, array( $sSectionPath, 'repeatable' ), false);
        if ($this->___isSubSections($aItems, $_abSectionRepeatable)) {
            return $this->___getSubSectionsFormatted($aItems, $sCapability, $aSectionsets, $_abSectionRepeatable);
        }
        return $this->___getNormalFieldsetsFormatted($aItems, $sCapability, $aSectionsets, $_abSectionRepeatable);
    }
    private function ___getNormalFieldsetsFormatted($aItems, $sCapability, $aSectionsets, $_abSectionRepeatable)
    {
        $_aNewItems = array();
        foreach ($aItems as $_sFieldID => $_aFieldset) {
            $_aFieldset = $this->___getFieldsetFormatted($_aFieldset, $aSectionsets, $sCapability, count($_aNewItems), null, $_abSectionRepeatable, $this->oCallerForm);
            if (empty($_aFieldset)) {
                continue;
            }
            $_aNewItems[ $_aFieldset[ 'field_id' ] ] = $_aFieldset;
        }
        uasort($_aNewItems, array( $this, 'sortArrayByKey' ));
        return $_aNewItems;
    }
    private function ___isSubSections($aItems, $_abSectionRepeatable)
    {
        if (! empty($_abSectionRepeatable)) {
            return true;
        }
        return ( boolean ) count($this->getIntegerKeyElements($aItems));
    }
    private function ___getSubSectionsFormatted($aItems, $sCapability, $aSectionsets, $_abSectionRepeatable)
    {
        $_aNewFieldset = array();
        foreach ($this->numerizeElements($aItems) as $_iSubSectionIndex => $_aFieldsets) {
            foreach ($_aFieldsets as $_aFieldset) {
                $_iCountElement = count($this->getElementAsArray($_aNewFieldset, $_iSubSectionIndex));
                $_aFieldset = $this->___getFieldsetFormatted($_aFieldset, $aSectionsets, $sCapability, $_iCountElement, $_iSubSectionIndex, $_abSectionRepeatable, $this->oCallerForm);
                if (empty($_aFieldset)) {
                    continue;
                }
                $_aNewFieldset[ $_iSubSectionIndex ][ $_aFieldset['field_id'] ] = $_aFieldset;
            }
            uasort($_aNewFieldset[ $_iSubSectionIndex ], array( $this, 'sortArrayByKey' ));
        }
        return $_aNewFieldset;
    }
    private function ___sortFieldsBySectionsOrder(array &$aFieldsets, array $aSectionsets)
    {
        if (empty($aSectionsets) || empty($aFieldsets)) {
            return;
        }
        $_aSortedFields = array();
        foreach ($aSectionsets as $_sSectionPath => $_aSecitonset) {
            if (isset($aFieldsets[ $_sSectionPath ])) {
                $_aSortedFields[ $_sSectionPath ] = $aFieldsets[ $_sSectionPath ];
            }
        }
        $aFieldsets = $_aSortedFields;
    }
    private function ___getFieldsetFormatted($aFieldset, $aSectionsets, $sCapability, $iCountOfElements, $iSubSectionIndex, $bIsSectionRepeatable, $oCallerObject)
    {
        if (! isset($aFieldset[ 'field_id' ])) {
            return;
        }
        $_oFieldsetFormatter = new AdminPageFramework_Form_Model___Format_Fieldset($aFieldset, $this->sStructureType, $sCapability, $iCountOfElements, $iSubSectionIndex, $bIsSectionRepeatable, $oCallerObject);
        $_aFieldset = $this->callBack($this->aCallbacks[ 'fieldset_before_output' ], array( $_oFieldsetFormatter->get(), $aSectionsets ));
        $_aFieldset = $this->callBack($this->aCallbacks[ 'fieldset_after_formatting' ], array( $_aFieldset, $aSectionsets ));
        return $_aFieldset;
    }
}
