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

namespace Drupal\simplesitemap;

/**
 * Simplesitemap class.
11 12
 *
 * Main module class.
13 14 15
 */
class Simplesitemap {

16
  private $config;
17 18 19
  private $sitemap;

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

23 24 25 26 27 28
  /**
   * Returns an the form entity object.
   *
   * @param object $form_state
   * @return object $entity or FALSE if non-existent.
   */
29
  public static function get_form_entity($form_state) {
30 31
    if (!is_null($form_state->getFormObject())
      && method_exists($form_state->getFormObject(), 'getEntity')) {
32 33 34 35 36 37
      $entity = $form_state->getFormObject()->getEntity();
      return $entity;
    }
    return FALSE;
  }

38
  private function initialize() {
39 40 41 42 43 44
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

  // Get sitemap from database.
  private function get_sitemap_from_db() {
45
    $this->sitemap = db_query("SELECT * FROM {simplesitemap}")->fetchAllAssoc('id');
46
  }
47 48 49
  /**
   * Gets sitemap settings from configuration storage.
   */
50
  private function get_config_from_db() {
51 52 53
    $this->config = \Drupal::config('simplesitemap.settings');
  }

54 55 56 57 58
  /**
   * Saves entity type sitemap settings to db.
   *
   * @param array $entity_types
   */
59 60 61 62
  public function save_entity_types($entity_types) {
    $this->save_config('entity_types', $entity_types);
  }

63 64 65 66 67
  /**
   * Saves the sitemap custom links settings to db.
   *
   * @param array $custom_links
   */
68 69 70 71
  public function save_custom_links($custom_links) {
    $this->save_config('custom', $custom_links);
  }

72 73 74 75 76
  /**
   * Saves other sitemap settings to db.
   *
   * @param array $settings
   */
77 78 79 80
  public function save_settings($settings) {
    $this->save_config('settings', $settings);
  }

81
  private function save_config($key, $value) {
82 83
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
      ->set($key, $value)->save();
84
    $this->initialize();
85 86
  }

87 88 89 90 91 92 93 94 95 96 97
  /**
   * Returns the whole sitemap, a requested sitemap chunk,
   * or the sitemap index file.
   *
   * @param int $sitemap_id
   *
   * @return string $sitemap
   *  If no sitemap id provided, either a sitemap index is returned, or the
   *  whole sitemap, if the amount of links does not exceed the max links setting.
   *  If a sitemap id is provided, a sitemap chunk is returned.
   */
98
  public function get_sitemap($sitemap_id = NULL) {
99 100 101
    if (empty($this->sitemap)) {
      $this->generate_sitemap();
    }
102 103 104 105 106 107 108 109 110 111

    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 {
112
        return $this->sitemap[0]->sitemap_string;
113 114 115 116 117
      }
    }

    // Return specific sitemap chunk.
    else {
118
      return $this->sitemap[$sitemap_id]->sitemap_string;
119
    }
120 121
  }

122 123 124
  /**
   * Generates the sitemap for all languages and saves it to the db.
   */
125
  public function generate_sitemap() {
126 127 128
    $generator = new SitemapGenerator();
    $generator->set_custom_links($this->config->get('custom'));
    $generator->set_entity_types($this->config->get('entity_types'));
129 130
    $settings = $this->get_settings();
    $this->sitemap = $generator->generate_sitemap($settings['max_links']);
131
    $this->save_sitemap();
132 133
    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')));
134 135
  }

136 137 138
  /**
   * Saves the sitemap to the db.
   */
139
  private function save_sitemap() {
140 141
    db_truncate('simplesitemap')->execute();
    $values = array();
142
    foreach($this->sitemap as $chunk_id => $chunk_data) {
143
      $values[] = array(
144 145
        'id' => $chunk_id,
        'sitemap_string' => $chunk_data->sitemap_string,
146
        'sitemap_created' =>  $chunk_data->sitemap_created,
147 148 149
      );
    }

150
    $query = db_insert('simplesitemap')
151
      ->fields(array('id', 'sitemap_string', 'sitemap_created'));
152 153 154 155 156 157
    foreach ($values as $record) {
      $query->values($record);
    }
    $query->execute();
  }

158 159 160
  /**
   * Gets the sitemap index.
   */
161 162 163
  private function get_sitemap_index() {
    $generator = new SitemapGenerator();
    return $generator->generate_sitemap_index($this->sitemap);
Pawel G's avatar
Pawel G committed
164 165
  }

166 167 168
  /**
   * Gets the sitemap entity type settings.
   */
169 170
  public function get_entity_types() {
    return $this->config->get('entity_types');
171
  }
172

173 174 175
  /**
   * Gets the sitemap custom links settings.
   */
176 177 178
  public function get_custom_links() {
    return $this->config->get('custom');
  }
179

180 181 182
  /**
   * Gets other sitemap settings.
   */
183 184 185
  public function get_settings() {
    return $this->config->get('settings');
  }
186
}