Simplesitemap.php 4.21 KB
Newer Older
1 2 3
<?php
/**
 * @file
Pawel G's avatar
Pawel G committed
4
 * Contains \Drupal\simple_sitemap\Simplesitemap.
5 6
 */

Pawel G's avatar
Pawel G committed
7
namespace Drupal\simple_sitemap;
8

9
use Drupal\Core\Config\ConfigFactoryInterface;
10

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

18
  private $config;
19 20
  private $sitemap;

21 22 23 24 25 26 27 28 29
  /**
   * Simplesitemap constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory from the container.
   */
  function __construct(ConfigFactoryInterface $config_factory) {
    $this->config = $config_factory->get('simple_sitemap.settings');
    $this->sitemap = db_query("SELECT * FROM {simple_sitemap}")->fetchAllAssoc('id');
30 31
  }

32
  /**
33
   * Gets a specific sitemap configuration from the configuration storage.
34
   *
Pawel G's avatar
Pawel G committed
35 36
   * @param string $key
   *  Configuration key, like 'entity_links'.
37 38
   * @return mixed
   *  The requested configuration.
39
   */
40
  public function getConfig($key) {
41
    return $this->config->get($key);
42 43
  }

44
  /**
45
   * Saves a specific sitemap configuration to db.
46
   *
47 48 49 50
   * @param string $key
   *  Configuration key, like 'entity_links'.
   * @param mixed $value
   *  The configuration to be saved.
51
   */
52
  public function saveConfig($key, $value) {
Pawel G's avatar
Pawel G committed
53
    \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
54
      ->set($key, $value)->save();
55 56
  }

57 58 59 60 61 62 63 64 65 66 67
  /**
   * 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.
   */
68
  public function getSitemap($sitemap_id = NULL) {
69 70 71 72
    if (is_null($sitemap_id) || !isset($this->sitemap[$sitemap_id])) {

      // Return sitemap index, if there are multiple sitemap chunks.
      if (count($this->sitemap) > 1) {
73
        return $this->getSitemapIndex();
74 75 76 77
      }

      // Return sitemap if there is only one chunk.
      else {
78 79 80 81
        if (isset($this->sitemap[1])) {
          return $this->sitemap[1]->sitemap_string;
        }
        return FALSE;
82 83 84 85
      }
    }
    // Return specific sitemap chunk.
    else {
86
      return $this->sitemap[$sitemap_id]->sitemap_string;
87
    }
88 89
  }

90 91
  /**
   * Generates the sitemap for all languages and saves it to the db.
92 93 94 95
   *
   * @param string $from
   *  Can be 'form', 'cron', or 'drush'. This decides how to the batch process
   *  is to be run.
96
   */
97
  public function generateSitemap($from = 'form') {
Pawel G's avatar
Pawel G committed
98
    $generator = new SitemapGenerator($from);
99 100 101
    $generator->setCustomLinks($this->getConfig('custom'));
    $generator->setEntityTypes($this->getConfig('entity_types'));
    $generator->startBatch();
102 103
  }

104
  /**
105 106 107 108
   * Generates and returns the sitemap index as string.
   *
   * @return string
   *  The sitemap index.
109
   */
110
  private function getSitemapIndex() {
111
    $generator = new SitemapGenerator();
112
    return $generator->generateSitemapIndex($this->sitemap);
Pawel G's avatar
Pawel G committed
113 114
  }

115
  /**
116 117 118 119 120 121 122
   * 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.
123
   */
124 125
  public function getSetting($name) {
    $settings = $this->getConfig('settings');
126
    return isset($settings[$name]) ? $settings[$name] : FALSE;
127
  }
128

129
  /**
130 131 132 133 134 135
   * Saves a specific sitemap setting to db.
   *
   * @param $name
   *  Setting name, like 'max_links'.
   * @param $setting
   *  The setting to be saved.
136
   */
137 138
  public function saveSetting($name, $setting) {
    $settings = $this->getConfig('settings');
139
    $settings[$name] = $setting;
140
    $this->saveConfig('settings', $settings);
141
  }
142

Pawel G's avatar
Pawel G committed
143 144
  /**
   * Returns a 'time ago' string of last timestamp generation.
145
   *
Pawel G's avatar
Pawel G committed
146 147 148
   * @return mixed
   *  Formatted timestamp of last sitemap generation, otherwise FALSE.
   */
149
  public function getGeneratedAgo() {
150
    if (isset($this->sitemap[1]->sitemap_created)) {
151
      return \Drupal::service('date.formatter')
152
        ->formatInterval(REQUEST_TIME - $this->sitemap[1]->sitemap_created);
153 154 155
    }
    return FALSE;
  }
Pawel G's avatar
Pawel G committed
156

157
  public static function getDefaultLangId() {
Pawel G's avatar
Pawel G committed
158 159
    return \Drupal::languageManager()->getDefaultLanguage()->getId();
  }
160
}