<commit_before><?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/>.
 */

class QubitMetadataRoute extends sfRoute
{
  public static

    $METADATA_PLUGINS = array(
      'isaar' => 'sfIsaarPlugin',
      'eac'   => 'sfEacPlugin',
      'ead'   => 'sfEadPlugin',
      'isad'  => 'sfIsadPlugin',
      'dc'    => 'sfDcPlugin',
      'skos'  => 'sfSkosPlugin',
      'rad'   => 'sfRadPlugin',
      'mods'  => 'sfModsPlugin',
      'isdf'  => 'sfIsdfPlugin'),

    $DEFAULT_MODULES = array(
      'informationobject' => false,
      'actor'             => 'sfIsaarPlugin',
      'repository'        => 'sfIsdiahPlugin',
      'function'          => 'sfIsdfPlugin');

  /**
   * Returns an array of parameters if the $url matches this route by looking up
   * the slug in the database, otherwise returns FALSE. These parameters are
   * modified according to the type of resource in order to route the request to
   * its corresponding Symfony module.
   *
   * Case 1: e.g. uri "/peanut-12345" (QubitInformationObject)
   *     -> Add module 'sfIsadPlugin' since that is the module that corresponds
   *        with that description (it looks at the record settings and the
   *        default application template).
   * Case 2: e.g. uri "/repository/add"
   *     -> Replace module "repository" with "sfIsdiahPlugin". The relation
   *        is descrbied in self::$DEFAULT_MODULES
   *
   * @see sfRoute
   */
  public function matchesUrl($url, $context = array())
  {
    // Delegate basic matching to sfRoute
    if (false === $parameters = parent::matchesUrl($url, $context))
    {
      return false;
    }

    // Rewrite action add/copy to edit
    if (in_array($parameters['action'], array('add', 'copy')))
    {
      $parameters['action'] = 'edit';
    }

    // At this point, it's likely that we are dealing with a permalink so let's
    // hit the database to see whether the resource exists or not.
    if (isset($parameters['slug']))
    {
      $criteria = new Criteria;
      $criteria->add(QubitSlug::SLUG, $parameters['slug']);
      $criteria->addJoin(QubitSlug::OBJECT_ID, QubitObject::ID);

      if (null === $this->resource = QubitObject::get($criteria)->__get(0))
      {
        return false;
      }

      // Find the Symfony module to be used based in the object class.
      // In some cases, the metadata template passed as part of the URL will be
      // considered. Additionally, QubitInformationObject will be analyzed
      // differently, since every object may have a specific metadata template
      // assigned.
      switch (true)
      {
        case $this->resource instanceof QubitRepository:
          $parameters['module'] = 'sfIsdiahPlugin';

          break;

        case $this->resource instanceof QubitDonor:
          $parameters['module'] = 'donor';

          break;

        case $this->resource instanceof QubitRightsHolder:
          $parameters['module'] = 'rightsholder';

          break;

        case $this->resource instanceof QubitUser:
          $parameters['module'] = 'user';

          break;

        case $this->resource instanceof QubitActor:
          $parameters['module'] = $this->getActionParameter(array('isaar', 'eac'), $this->getDefaultTemplate('actor'), $parameters);

          break;

        case $this->resource instanceof QubitFunction:
          $parameters['module'] = 'sfIsdfPlugin';

          break;

        case $this->resource instanceof QubitDigitalObject:
          $parameters['module'] = 'digitalobject';

          break;

        case $this->resource instanceof QubitInformationObject:
          $default = $this->getDefaultTemplate('informationobject');

          // See if there is a template assigned in the database for this record
          $sql = 'SELECT code
            FROM information_object JOIN term ON information_object.display_standard_id = term.id
            WHERE information_object.id = ? AND taxonomy_id = ?';

          if (false !== $defaultSetting = QubitPdo::fetchColumn($sql, array($this->resource->id, QubitTaxonomy::INFORMATION_OBJECT_TEMPLATE_ID)))
          {
            $default = $defaultSetting;
          }

          $parameters['module'] = $this->getActionParameter(array('isad', 'dc', 'mods', 'rad', 'ead'), $default, $parameters);

          break;

        case $this->resource instanceof QubitAccession:
          $parameters['module'] = 'accession';

          break;

        case $this->resource instanceof QubitDeaccession:
          $parameters['module'] = 'deaccession';

          break;

        case $this->resource instanceof QubitTerm:
          $parameters['module'] = 'term';

          break;

        case $this->resource instanceof QubitTaxonomy:
          $parameters['module'] = 'taxonomy';

          break;

        case $this->resource instanceof QubitStaticPage:
          $parameters['module'] = 'staticpage';

          break;

        default:

          return false;
      }
    }

    // Given the parent module (e.g. informationobject), find the best metadata
    // to be used
    else if (isset($parameters['module']))
    {
      switch ($parameters['module'])
      {
        case 'informationobject':
          if (false !== $code = $this->getDefaultTemplate($parameters['module']))
          {
            $parameters['module'] = self::$METADATA_PLUGINS[$code];<commit_msg>Route physical objects, fixes #5454<commit_after><?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/>.
 */

class QubitMetadataRoute extends sfRoute
{
  public static

    $METADATA_PLUGINS = array(
      'isaar' => 'sfIsaarPlugin',
      'eac'   => 'sfEacPlugin',
      'ead'   => 'sfEadPlugin',
      'isad'  => 'sfIsadPlugin',
      'dc'    => 'sfDcPlugin',
      'skos'  => 'sfSkosPlugin',
      'rad'   => 'sfRadPlugin',
      'mods'  => 'sfModsPlugin',
      'isdf'  => 'sfIsdfPlugin'),

    $DEFAULT_MODULES = array(
      'informationobject' => false,
      'actor'             => 'sfIsaarPlugin',
      'repository'        => 'sfIsdiahPlugin',
      'function'          => 'sfIsdfPlugin');

  /**
   * Returns an array of parameters if the $url matches this route by looking up
   * the slug in the database, otherwise returns FALSE. These parameters are
   * modified according to the type of resource in order to route the request to
   * its corresponding Symfony module.
   *
   * Case 1: e.g. uri "/peanut-12345" (QubitInformationObject)
   *     -> Add module 'sfIsadPlugin' since that is the module that corresponds
   *        with that description (it looks at the record settings and the
   *        default application template).
   * Case 2: e.g. uri "/repository/add"
   *     -> Replace module "repository" with "sfIsdiahPlugin". The relation
   *        is descrbied in self::$DEFAULT_MODULES
   *
   * @see sfRoute
   */
  public function matchesUrl($url, $context = array())
  {
    // Delegate basic matching to sfRoute
    if (false === $parameters = parent::matchesUrl($url, $context))
    {
      return false;
    }

    // Rewrite action add/copy to edit
    if (in_array($parameters['action'], array('add', 'copy')))
    {
      $parameters['action'] = 'edit';
    }

    // At this point, it's likely that we are dealing with a permalink so let's
    // hit the database to see whether the resource exists or not.
    if (isset($parameters['slug']))
    {
      $criteria = new Criteria;
      $criteria->add(QubitSlug::SLUG, $parameters['slug']);
      $criteria->addJoin(QubitSlug::OBJECT_ID, QubitObject::ID);

      if (null === $this->resource = QubitObject::get($criteria)->__get(0))
      {
        return false;
      }

      // Find the Symfony module to be used based in the object class.
      // In some cases, the metadata template passed as part of the URL will be
      // considered. Additionally, QubitInformationObject will be analyzed
      // differently, since every object may have a specific metadata template
      // assigned.
      switch (true)
      {
        case $this->resource instanceof QubitRepository:
          $parameters['module'] = 'sfIsdiahPlugin';

          break;

        case $this->resource instanceof QubitDonor:
          $parameters['module'] = 'donor';

          break;

        case $this->resource instanceof QubitRightsHolder:
          $parameters['module'] = 'rightsholder';

          break;

        case $this->resource instanceof QubitUser:
          $parameters['module'] = 'user';

          break;

        case $this->resource instanceof QubitActor:
          $parameters['module'] = $this->getActionParameter(array('isaar', 'eac'), $this->getDefaultTemplate('actor'), $parameters);

          break;

        case $this->resource instanceof QubitFunction:
          $parameters['module'] = 'sfIsdfPlugin';

          break;

        case $this->resource instanceof QubitDigitalObject:
          $parameters['module'] = 'digitalobject';

          break;

        case $this->resource instanceof QubitInformationObject:
          $default = $this->getDefaultTemplate('informationobject');

          // See if there is a template assigned in the database for this record
          $sql = 'SELECT code
            FROM information_object JOIN term ON information_object.display_standard_id = term.id
            WHERE information_object.id = ? AND taxonomy_id = ?';

          if (false !== $defaultSetting = QubitPdo::fetchColumn($sql, array($this->resource->id, QubitTaxonomy::INFORMATION_OBJECT_TEMPLATE_ID)))
          {
            $default = $defaultSetting;
          }

          $parameters['module'] = $this->getActionParameter(array('isad', 'dc', 'mods', 'rad', 'ead'), $default, $parameters);

          break;

        case $this->resource instanceof QubitAccession:
          $parameters['module'] = 'accession';

          break;

        case $this->resource instanceof QubitDeaccession:
          $parameters['module'] = 'deaccession';

          break;

        case $this->resource instanceof QubitTerm:
          $parameters['module'] = 'term';

          break;

        case $this->resource instanceof QubitTaxonomy:
          $parameters['module'] = 'taxonomy';

          break;

        case $this->resource instanceof QubitStaticPage:
          $parameters['module'] = 'staticpage';

          break;

        case $this->resource instanceof QubitPhysicalObject:
          $parameters['module'] = 'physicalobject';

          break;

        default:

          return false;
      }
    }

    // Given the parent module (e.g. informationobject), find the best metadata
    // to be used
    else if (isset($parameters['module']))
    {
      switch ($parameters['module'])
      {
        case 'informationobject':
          if (false !== $code = $this->getDefaultTemplate($parameters['module']))
          {
            $parameters['module'] = self::$METADATA_PLUGINS[$code];
{'fraction_non_alphanumeric': 0.11529680365296803, 'fraction_numerical': 0.0017938682322243965, 'mean_word_length': 2.5799766491535316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7137035', 'n_tokens_mistral': 3431, 'n_tokens_neox': 3321, 'n_words': 1058}