SimplesitemapManager.php 9.89 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\simple_sitemap;

use Drupal\Core\Config\ConfigFactory;
6
use Drupal\Core\Database\Connection;
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager;

/**
 * Class SimplesitemapManager
 * @package Drupal\simple_sitemap
 */
class SimplesitemapManager {

  const DEFAULT_SITEMAP_TYPE = 'default_hreflang';
  const DEFAULT_SITEMAP_GENERATOR = 'default';

  /**
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

28 29 30 31 32
  /**
   * @var \Drupal\Core\Database\Connection
   */
  protected $db;

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager
   */
  protected $sitemapTypeManager;

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager
   */
  protected $urlGeneratorManager;

  /**
   * @var \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager
   */
  protected $sitemapGeneratorManager;

gbyte.co's avatar
gbyte.co committed
48 49 50 51 52
  /**
   * @var \Drupal\simple_sitemap\SimplesitemapSettings
   */
  protected $settings;

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
  /**
   * @var SitemapTypeBase[] $sitemapTypes
   */
  protected $sitemapTypes = [];

  /**
   * @var UrlGeneratorBase[] $urlGenerators
   */
  protected $urlGenerators = [];

  /**
   * @var SitemapGeneratorBase[] $sitemapGenerators
   */
  protected $sitemapGenerators = [];

  /**
   * SimplesitemapManager constructor.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
71
   * @param \Drupal\Core\Database\Connection $database
72 73 74
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapType\SitemapTypeManager $sitemap_type_manager
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorManager $sitemap_generator_manager
gbyte.co's avatar
gbyte.co committed
75
   * @param \Drupal\simple_sitemap\SimplesitemapSettings $settings
76 77 78
   */
  public function __construct(
    ConfigFactory $config_factory,
79
    Connection $database,
80 81
    SitemapTypeManager $sitemap_type_manager,
    UrlGeneratorManager $url_generator_manager,
gbyte.co's avatar
gbyte.co committed
82 83
    SitemapGeneratorManager $sitemap_generator_manager,
    SimplesitemapSettings $settings
84 85
  ) {
    $this->configFactory = $config_factory;
86
    $this->db = $database;
87 88 89
    $this->sitemapTypeManager = $sitemap_type_manager;
    $this->urlGeneratorManager = $url_generator_manager;
    $this->sitemapGeneratorManager = $sitemap_generator_manager;
gbyte.co's avatar
gbyte.co committed
90
    $this->settings = $settings;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  }

  /**
   * @param $sitemap_generator_id
   * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapGeneratorBase
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function getSitemapGenerator($sitemap_generator_id) {
    if (!isset($this->sitemapGenerators[$sitemap_generator_id])) {
      $this->sitemapGenerators[$sitemap_generator_id]
        = $this->sitemapGeneratorManager->createInstance($sitemap_generator_id);
    }

    return $this->sitemapGenerators[$sitemap_generator_id];
  }

  /**
   * @param $url_generator_id
   * @return \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function getUrlGenerator($url_generator_id) {
    if (!isset($this->urlGenerators[$url_generator_id])) {
      $this->urlGenerators[$url_generator_id]
        = $this->urlGeneratorManager->createInstance($url_generator_id);
    }

    return $this->urlGenerators[$url_generator_id];
  }

  /**
   * @return array
   */
  public function getSitemapTypes() {
    if (empty($this->sitemapTypes)) {
      $this->sitemapTypes = $this->sitemapTypeManager->getDefinitions();
    }

    return $this->sitemapTypes;
  }

  /**
   * @param null $sitemap_type
   * @return array
   *
   * @todo translate label
   */
  public function getSitemapVariants($sitemap_type = NULL, $attach_type_info = TRUE) {
    if (NULL === $sitemap_type) {
      $variants = [];
      foreach ($this->configFactory->listAll('simple_sitemap.variants.') as $config_name) {
        $config_name_parts = explode('.', $config_name);
        $saved_variants = $this->configFactory->get($config_name)->get('variants');
        $saved_variants = $attach_type_info ? $this->attachSitemapTypeToVariants($saved_variants, $config_name_parts[2]) : $saved_variants;
        $variants = array_merge($variants, (is_array($saved_variants) ? $saved_variants : []));
      }
    }
    else {
      $variants = $this->configFactory->get("simple_sitemap.variants.$sitemap_type")->get('variants');
      $variants = is_array($variants) ? $variants : [];
      $variants = $attach_type_info ? $this->attachSitemapTypeToVariants($variants, $sitemap_type) : $variants;
    }
153 154
    array_multisort(array_column($variants, "weight"), SORT_ASC, $variants);
    return $variants;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  }

  protected function attachSitemapTypeToVariants(array $variants, $type) {
    return array_map(function($variant) use ($type) { return $variant + ['type' => $type]; }, $variants);
  }

  protected function detachSitemapTypeFromVariants(array $variants) {
    return array_map(function($variant) { unset($variant['type']); return $variant; }, $variants);
  }

  /**
   * @param $name
   * @param $definition
   * @return $this
   *
   * @todo document
   */
  public function addSitemapVariant($name, $definition = []) {
173 174 175 176 177 178 179 180 181 182
    $all_variants = $this->getSitemapVariants();
    if (isset($all_variants[$name])) {
      $old_variant = $all_variants[$name];
      if (!empty($definition['type']) && $old_variant['type'] !== $definition['type']) {
        $this->removeSitemapVariants($name);
        unset($old_variant);
      }
      else {
        unset($old_variant['type']);
      }
183 184
    }

185 186
    if (!isset($old_variant) && empty($definition['label'])) {
      $definition['label'] = (string) $name;
187 188
    }

189 190 191
    if (!isset($old_variant) && empty($definition['type'])) {
      $definition['type'] = self::DEFAULT_SITEMAP_TYPE;
    }
192

193 194
    if (isset($definition['weight'])) {
      $definition['weight'] = (int) $definition['weight'];
195
    }
196 197 198 199 200 201
    elseif (!isset($old_variant)) {
      $definition['weight'] = 0;
    }

    if (isset($old_variant)) {
      $definition = $definition + $old_variant;
202 203
    }

204 205 206 207 208
    $variants = array_merge($this->getSitemapVariants($definition['type'], FALSE), [$name => ['label' => $definition['label'], 'weight' => $definition['weight']]]);
    $this->configFactory->getEditable('simple_sitemap.variants.' . $definition['type'])
      ->set('variants', $variants)
      ->save();

209 210 211
    return $this;
  }

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
  public function removeSitemap($variant_names = NULL) {
    $saved_variants = $this->getSitemapVariants();
    $remove_variants = NULL !== $variant_names && !empty((array) $variant_names)
      ? array_intersect_key($saved_variants, array_flip((array) $variant_names))
      : $saved_variants;

    if (!empty($remove_variants)) {
      $type_definitions = $this->getSitemapTypes();
      foreach ($remove_variants as $variant_name => $variant_definition) {
        $this->getSitemapGenerator($type_definitions[$variant_definition['type']]['sitemapGenerator'])
          ->setSitemapVariant($variant_name)
          ->remove();
      }
    }

    return $this;
  }

230
  public function removeSitemapVariants($variant_names = NULL) {
231
    if (NULL === $variant_names || !empty((array) $variant_names)) {
232 233 234

      // Remove sitemap instances.
      $this->removeSitemap($variant_names);
235

236 237
      if (NULL === $variant_names) {
        // Remove all variants and their bundle settings.
238
        foreach(['variants', 'bundle_settings', 'custom_links'] as $config_name_part) {
239 240 241 242
          foreach ($this->configFactory->listAll("simple_sitemap.$config_name_part.") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }
243
      }
244 245 246 247 248 249 250 251
      else {
        // Remove bundle settings for specific variants.
        foreach ((array) $variant_names as $variant_name) {
          foreach ($this->configFactory->listAll("simple_sitemap.bundle_settings.$variant_name.") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }

252 253 254 255 256 257 258
        // Remove custom links for specific variants.
        foreach ((array) $variant_names as $variant_name) {
          foreach ($this->configFactory->listAll("simple_sitemap.custom_links.$variant_name") as $config_name) {
            $this->configFactory->getEditable($config_name)->delete();
          }
        }

259 260 261 262 263 264 265 266 267 268 269 270
        // Remove specific variants from configuration.
        $remove_variants = [];
        $variants = $this->getSitemapVariants();
        foreach ((array) $variant_names as $variant_name) {
          if (isset($variants[$variant_name])) {
            $remove_variants[$variants[$variant_name]['type']][$variant_name] = $variant_name;
          }
        }
        foreach ($remove_variants as $type => $variants_per_type) {
          $this->configFactory->getEditable("simple_sitemap.variants.$type")
            ->set('variants', array_diff_key($this->getSitemapVariants($type, FALSE), $variants_per_type))
            ->save();
271 272 273
        }
      }

274 275 276 277
      // Remove bundle setting overrides for entities.
      $query = $this->db->delete('simple_sitemap_entity_overrides');
      if (NULL !== $variant_names) {
        $query->condition('type', (array) $variant_names, 'IN');
278
      }
279 280 281 282 283 284
      $query->execute();


      // Remove default variant setting.
      if (NULL === $variant_names
        || in_array($this->settings->getSetting('default_variant', ''), (array) $variant_names)) {
gbyte.co's avatar
gbyte.co committed
285 286
        $this->settings->saveSetting('default_variant', '');
      }
287 288 289 290 291
    }

    return $this;
  }
}