Simplesitemap.php 3.96 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
  private $sitemap;

  function __construct() {
20
    $this->initialize();
21 22
  }

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 initialize() {
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
    //todo: update for chunked sitemaps
49 50 51 52
    $result = db_query("SELECT id, sitemap_string FROM {simplesitemap}")->fetchAllAssoc('id');
    foreach ($result as $sitemap_id => $sitemap) {
      $this->sitemap[$sitemap_id] = $sitemap->sitemap_string;
    }
53 54 55 56
  }

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

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

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

68 69 70 71
  public function save_settings($settings) {
    $this->save_config('settings', $settings);
  }

72
  private function save_config($key, $value) {
73 74
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
      ->set($key, $value)->save();
75
    $this->initialize();
76 77
  }

78
  public function get_sitemap($sitemap_id = NULL) {
79 80 81
    if (empty($this->sitemap)) {
      $this->generate_sitemap();
    }
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

    if (is_null($sitemap_id) || !isset($this->sitemap[$sitemap_id])) {

      // Return sitemap index, if there are multiple sitemap chunks.
      if (count($this->sitemap) > 1) {
        return $this->get_sitemap_index();
      }

      // Return sitemap if there is only one chunk.
      else {
        return $this->sitemap[0];
      }
    }

    // Return specific sitemap chunk.
    else {
      return $this->sitemap[$sitemap_id];
    }
100 101
  }

102
  public function generate_sitemap() {
103 104 105
    $generator = new SitemapGenerator();
    $generator->set_custom_links($this->config->get('custom'));
    $generator->set_entity_types($this->config->get('entity_types'));
106 107
    $settings = $this->get_settings();
    $this->sitemap = $generator->generate_sitemap($settings['max_links']);
108
    $this->save_sitemap();
109 110
    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')));
111 112 113
  }

  private function save_sitemap() {
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

    db_truncate('simplesitemap')->execute();
    $values = array();
    foreach($this->sitemap as $sitemap_id => $sitemap_string) {
      $values[] = array(
        'id' => $sitemap_id,
        'sitemap_string' => $sitemap_string,
        //todo: add 'changed' info in new column?
      );
    }

    $query = db_insert('simplesitemap')->fields(array('id', 'sitemap_string'));
    foreach ($values as $record) {
      $query->values($record);
    }
    $query->execute();
  }

  private function get_sitemap_index() {
    $generator = new SitemapGenerator();
    return $generator->generate_sitemap_index($this->sitemap);
Pawel G's avatar
Pawel G committed
135 136
  }

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

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

  public function get_settings() {
    return $this->config->get('settings');
  }
148
}