Simplesitemap.php 4.79 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
  private function initialize() {
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

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

44 45 46
  /**
   * Gets sitemap from db.
   */
47
  private function get_sitemap_from_db() {
48
    $this->sitemap = db_query("SELECT * FROM {simplesitemap}")->fetchAllAssoc('id');
49
  }
50

51
  /**
52
   * Gets sitemap settings from the configuration storage.
53
   */
54
  private function get_config_from_db() {
55 56 57
    $this->config = \Drupal::config('simplesitemap.settings');
  }

58
  /**
59
   * Gets a specific sitemap configuration from the configuration storage.
60
   */
61 62
  public function get_config($key) {
    return $this->config->get($key);
63 64
  }

65
  /**
66
   * Saves a specific sitemap configuration to db.
67
   *
68 69 70 71
   * @param string $key
   *  Configuration key, like 'entity_links'.
   * @param mixed $value
   *  The configuration to be saved.
72
   */
73
  public function save_config($key, $value) {
74 75
    \Drupal::service('config.factory')->getEditable('simplesitemap.settings')
      ->set($key, $value)->save();
76
    $this->initialize();
77 78
  }

79 80 81 82 83 84 85 86 87 88 89
  /**
   * 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.
   */
90
  public function get_sitemap($sitemap_id = NULL) {
91 92 93
    if (empty($this->sitemap)) {
      $this->generate_sitemap();
    }
94 95 96 97 98 99 100 101 102 103

    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 {
104
        return $this->sitemap[0]->sitemap_string;
105 106 107 108 109
      }
    }

    // Return specific sitemap chunk.
    else {
110
      return $this->sitemap[$sitemap_id]->sitemap_string;
111
    }
112 113
  }

114 115 116
  /**
   * Generates the sitemap for all languages and saves it to the db.
   */
117
  public function generate_sitemap() {
118
    $generator = new SitemapGenerator();
119 120 121
    $generator->set_custom_links($this->get_config('custom'));
    $generator->set_entity_types($this->get_config('entity_types'));
    $this->sitemap = $generator->generate_sitemap($this->get_setting('max_links'));
122
    $this->save_sitemap();
123 124
    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')));
125 126
  }

127 128 129
  /**
   * Saves the sitemap to the db.
   */
130
  private function save_sitemap() {
131 132
    db_truncate('simplesitemap')->execute();
    $values = array();
133
    foreach($this->sitemap as $chunk_id => $chunk_data) {
134
      $values[] = array(
135 136
        'id' => $chunk_id,
        'sitemap_string' => $chunk_data->sitemap_string,
137
        'sitemap_created' =>  $chunk_data->sitemap_created,
138 139 140
      );
    }

141
    $query = db_insert('simplesitemap')
142
      ->fields(array('id', 'sitemap_string', 'sitemap_created'));
143 144 145 146 147 148
    foreach ($values as $record) {
      $query->values($record);
    }
    $query->execute();
  }

149
  /**
150 151 152 153
   * Generates and returns the sitemap index as string.
   *
   * @return string
   *  The sitemap index.
154
   */
155 156 157
  private function get_sitemap_index() {
    $generator = new SitemapGenerator();
    return $generator->generate_sitemap_index($this->sitemap);
Pawel G's avatar
Pawel G committed
158 159
  }

160
  /**
161 162 163 164 165 166 167
   * Gets a specific sitemap setting.
   *
   * @param string $name
   *  Name of the setting, like 'max_links'.
   *
   * @return mixed
   *  The current setting from db or FALSE if setting does not exist.
168
   */
169 170 171
  public function get_setting($name) {
    $settings = $this->get_config('settings');
    return isset($settings[$name]) ? $settings[$name] : FALSE;
172
  }
173

174
  /**
175 176 177 178 179 180
   * Saves a specific sitemap setting to db.
   *
   * @param $name
   *  Setting name, like 'max_links'.
   * @param $setting
   *  The setting to be saved.
181
   */
182 183 184 185
  public function save_setting($name, $setting) {
    $settings = $this->get_config('settings');
    $settings[$name] = $setting;
    $this->save_config('settings', $settings);
186
  }
187
}