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

gbyte.co's avatar
gbyte.co committed
7
namespace Drupal\simple_sitemap;
8

9
use Drupal\Core\Config\ConfigFactoryInterface;
10
use Drupal\Core\Entity\ContentEntityTypeInterface;
11

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

19
  private $config;
20 21
  private $sitemap;

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');
30
    $this->sitemap = \Drupal::service('database')->query("SELECT * FROM {simple_sitemap}")->fetchAllAssoc('id');
31 32
  }

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

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

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

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

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

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

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

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

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

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

158
  public static function getDefaultLangId() {
gbyte.co's avatar
gbyte.co committed
159 160
    return \Drupal::languageManager()->getDefaultLanguage()->getId();
  }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

  /**
   * Returns objects of entity types that can be indexed by the sitemap.
   *
   * @return array
   *  Objects of entity types that can be indexed by the sitemap.
   */
  public static function getSitemapEntityTypes() {
    $entity_types = \Drupal::entityTypeManager()->getDefinitions();

    foreach ($entity_types as $entity_type_id => $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface || !method_exists($entity_type, 'getBundleEntityType')) {
        unset($entity_types[$entity_type_id]);
        continue;
      }
    }
    return $entity_types;
  }
179 180 181 182 183 184 185 186 187 188 189 190 191 192

  public static function entityTypeIsAtomic($entity_type_id) { //todo: make it work with entity object as well
    if ($entity_type_id == 'menu_link_content') // Menu fix.
      return FALSE;
    $sitemap_entity_types = self::getSitemapEntityTypes();
    if (isset($sitemap_entity_types[$entity_type_id])) {
      $entity_type = $sitemap_entity_types[$entity_type_id];
      if (empty($entity_type->getBundleEntityType())) {
        return TRUE;
      }
      return FALSE;
    }
    return FALSE; //todo: throw exception
  }
193
}