Simplesitemap.php 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
/**
 * @file
 * Contains \Drupal\simplesitemap\Simplesitemap.
 */

namespace Drupal\simplesitemap;

/**
 * Simplesitemap class.
 */
class Simplesitemap {

14 15 16
  const SITEMAP_PLUGIN_PATH = 'src/LinkGenerators/EntityTypeLinkGenerators';

  private $config;
17
  private $sitemap;
18
  private $language;
19 20 21 22 23 24

  function __construct() {
    $this->set_current_lang();
    $this->set_config();
  }

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  public static function get_form_entity($form_state) {
    if (!is_null($form_state->getFormObject()) && method_exists($form_state->getFormObject(), 'getEntity')) {
      $entity = $form_state->getFormObject()->getEntity();
      return $entity;
    }
    return FALSE;
  }

  public static function get_plugin_path($entity_type_name) {
    $class_path = drupal_get_path('module', 'simplesitemap') . '/' . self::SITEMAP_PLUGIN_PATH . '/' . $entity_type_name . '.php';
    if (file_exists($class_path)) {
      return $class_path;
    }
    return FALSE;
  }

Pawel G's avatar
Pawel G committed
41
  private function set_current_lang($language = NULL) {
42
    $this->language = is_null($language) ? \Drupal::languageManager()->getCurrentLanguage() : $language;
43 44 45
  }

  private function set_config() {
46 47 48 49 50 51 52 53
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

  // Get sitemap from database.
  private function get_sitemap_from_db() {
    $result = db_select('simplesitemap', 's')
      ->fields('s', array('sitemap_string'))
54
      ->condition('language_code', $this->language->getId())
55 56 57 58 59 60
      ->execute()->fetchAll();
    $this->sitemap = !empty($result[0]->sitemap_string) ? $result[0]->sitemap_string : NULL;
  }

  // Get sitemap settings from configuration storage.
  private function get_config_from_db() {
61 62 63 64 65 66 67
    $this->config = \Drupal::config('simplesitemap.settings');
  }

  public function save_entity_types($entity_types) {
    $this->save_config('entity_types', $entity_types);
  }

68 69 70 71
  public function save_custom_links($custom_links) {
    $this->save_config('custom', $custom_links);
  }

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  private function save_config($key, $value) {
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')->set($key, $value)->save();
    $this->set_config();
  }

  public function get_sitemap() {
    if (empty($this->sitemap)) {
      $this->generate_sitemap();
    }
    return $this->sitemap;
  }

  private function generate_sitemap() {
    $generator = new SitemapGenerator();
    $generator->set_sitemap_lang($this->language);
    $generator->set_custom_links($this->config->get('custom'));
    $generator->set_entity_types($this->config->get('entity_types'));
    $this->sitemap = $generator->generate_sitemap();
    $this->save_sitemap();
  }

  public function generate_all_sitemaps() {
    $generator = new SitemapGenerator();
    $generator->set_custom_links($this->config->get('custom'));
    $generator->set_entity_types($this->config->get('entity_types'));
    foreach(\Drupal::languageManager()->getLanguages() as $language) {
      $generator->set_sitemap_lang($language);
      $this->language = $language;
      $this->sitemap = $generator->generate_sitemap();
      $this->save_sitemap();
    }
103
    drupal_set_message(t('XML sitemaps have been regenerated for all languages.'));
104 105 106
  }

  private function save_sitemap() {
107 108 109

    //todo: db_merge not working in D8(?), this is why the following queries are needed:
//    db_merge('simplesitemap')
110
//      ->key(array('language_code', $this->lang))
111
//      ->fields(array(
112
//        'language_code' => $this->lang,
113 114 115 116
//        'sitemap_string' => $this->sitemap,
//      ))
//      ->execute();
    $exists_query = db_select('simplesitemap')
117
      ->condition('language_code', $this->language->getId())
118 119 120 121 122 123 124
      ->countQuery()->execute()->fetchField();

    if ($exists_query > 0) {
      db_update('simplesitemap')
        ->fields(array(
          'sitemap_string' => $this->sitemap,
        ))
125
        ->condition('language_code', $this->language->getId())
126 127 128 129 130
        ->execute();
    }
    else {
      db_insert('simplesitemap')
        ->fields(array(
131
          'language_code' => $this->language->getId(),
132 133 134 135
          'sitemap_string' => $this->sitemap,
        ))
        ->execute();
    }
Pawel G's avatar
Pawel G committed
136 137
  }

138 139
  public function get_entity_types() {
    return $this->config->get('entity_types');
140
  }
141 142 143 144

  public function get_custom_links() {
    return $this->config->get('custom');
  }
145
}