<?php

/*
 * This file is part of the Access to Memory (AtoM) software.
 *
 * Access to Memory (AtoM) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Access to Memory (AtoM) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Access to Memory (AtoM).  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Information Object - editMods.
 *
 * @author     Peter Van Garderen <peter@artefactual.com>
 */
class sfModsPluginEditAction extends InformationObjectEditAction
{
    // Arrays not allowed in class constants
    public static $NAMES = [
        'accessConditions',
        'identifier',
        'language',
        'subjectAccessPoints',
        'placeAccessPoints',
        'nameAccessPoints',
        'title',
        'type',
        'repository',
        'scopeAndContent',
        'displayStandard',
        'displayStandardUpdateDescendants',
    ];

    protected function earlyExecute()
    {
        parent::earlyExecute();

        $this->mods = new sfModsPlugin($this->resource);

        $title = $this->context->i18n->__('Add new resource');
        if (isset($this->getRoute()->resource)) {
            if (1 > strlen($title = $this->resource)) {
                $title = $this->context->i18n->__('Untitled');
            }

            $title = $this->context->i18n->__('Edit %1%', ['%1%' => $title]);
        }

        $this->response->setTitle("{$title} - {$this->response->getTitle()}");

        $this->eventComponent = new InformationObjectEventComponent($this->context, 'informationobject', 'event');
        $this->eventComponent->resource = $this->resource;
        $this->eventComponent->execute($this->request);

        $this->eventComponent->form->getWidgetSchema()->type->setHelp($this->context->i18n->__('Select the type of activity that established the relation between the authority record and the resource.'));
    }

    protected function addField($name)
    {
        switch ($name) {
            case 'type':
                $criteria = new Criteria();
                $this->resource->addObjectTermRelationsRelatedByObjectIdCriteria($criteria);
                QubitObjectTermRelation::addJoinTermCriteria($criteria);
                $criteria->add(QubitTerm::TAXONOMY_ID, QubitTaxonomy::MODS_RESOURCE_TYPE_ID);

                $value = [];
                foreach ($this->relations = QubitObjectTermRelation::get($criteria) as $item) {
                    $value[] = $this->context->routing->generate(null, [$item->term, 'module' => 'term']);
                }

                $this->form->setDefault('type', $value);
                $this->form->setValidator('type', new sfValidatorPass());

                $choices = [];
                foreach (QubitTaxonomy::getTermsById(QubitTaxonomy::MODS_RESOURCE_TYPE_ID) as $item) {
                    $choices[$this->context->routing->generate(null, [$item, 'module' => 'term'])] = $item;
                }

                $this->form->setWidget('type', new sfWidgetFormSelect(['choices' => $choices, 'multiple' => true]));

                break;

            default:
                return parent::addField($name);
        }
    }

    protected function processField($field)
    {
        switch ($field->getName()) {
            case 'type':
                $value = $filtered = [];
                foreach ($this->form->getValue('type') as $item) {
                    $params = $this->context->routing->parse(Qubit::pathInfo($item));
                    $resource = $params['_sf_route']->resource;
                    $value[$resource->id] = $filtered[$resource->id] = $resource;
                }

                foreach ($this->relations as $item) {
                    if (isset($value[$item->term->id])) {
                        unset($filtered[$item->term->id]);
                    } else {
                        $item->delete();
                    }
                }

                foreach ($filtered as $item) {
                    $relation = new QubitObjectTermRelation();
                    $relation->term = $item;

                    $this->resource->objectTermRelationsRelatedByobjectId[] = $relation;
                }

                break;

            default:
                return parent::processField($field);
        }
    }

    protected function processForm()
    {
        $this->resource->sourceStandard = 'MODS version 3.3';

        $this->eventComponent->processForm();

        return parent::processForm();
    }
}
