Simplesitemap.php 2.87 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 18 19 20 21 22
  private $sitemap;

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

23
  public static function get_form_entity($form_state) {
24 25
    if (!is_null($form_state->getFormObject())
      && method_exists($form_state->getFormObject(), 'getEntity')) {
26 27 28 29 30 31 32
      $entity = $form_state->getFormObject()->getEntity();
      return $entity;
    }
    return FALSE;
  }

  public static function get_plugin_path($entity_type_name) {
33 34
    $class_path = drupal_get_path('module', 'simplesitemap')
      . '/' . self::SITEMAP_PLUGIN_PATH . '/' . $entity_type_name . '.php';
35 36 37 38 39 40
    if (file_exists($class_path)) {
      return $class_path;
    }
    return FALSE;
  }

41
  private function set_config() {
42 43 44 45 46 47
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

  // Get sitemap from database.
  private function get_sitemap_from_db() {
48 49
    //todo: update for chunked sitemaps
    $result = db_query("SELECT sitemap_string FROM {simplesitemap}")->fetchAll();
50 51 52 53 54
    $this->sitemap = !empty($result[0]->sitemap_string) ? $result[0]->sitemap_string : NULL;
  }

  // Get sitemap settings from configuration storage.
  private function get_config_from_db() {
55 56 57 58 59 60 61
    $this->config = \Drupal::config('simplesitemap.settings');
  }

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

62 63 64 65
  public function save_custom_links($custom_links) {
    $this->save_config('custom', $custom_links);
  }

66
  private function save_config($key, $value) {
67 68
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
      ->set($key, $value)->save();
69 70 71 72 73 74 75 76 77 78
    $this->set_config();
  }

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

79
  public function generate_sitemap() {
80 81 82 83 84
    $generator = new SitemapGenerator();
    $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();
85 86
    drupal_set_message(t("The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.",
      array('@url' => $GLOBALS['base_url'] . '/sitemap.xml')));
87 88 89
  }

  private function save_sitemap() {
90 91 92 93 94 95 96 97
    //todo: update for chunked sitemaps
    db_merge('simplesitemap')
      ->key(array('id' => 1))
      ->fields(array(
        'id' => 1,
        'sitemap_string' => $this->sitemap,
      ))
      ->execute();
Pawel G's avatar
Pawel G committed
98 99
  }

100 101
  public function get_entity_types() {
    return $this->config->get('entity_types');
102
  }
103 104 105 106

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