Simplesitemap.php 6.96 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
use Drupal\Core\Config\ConfigFactoryInterface;
9

10 11
use Drupal\Core\Cache\Cache;

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 30
  /**
   * 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');
31 32
  }

33
  /**
34 35
   * Gets the entity_type_id and bundle_name of the form object if available and only
   * if the sitemap supports this entity type through an existing plugin.
36 37
   *
   * @param object $form_state
38
   * @param string $form_id
39
   *
40 41
   * @return array containing the entity_type_id and the bundle_name of the
   *  form object or FALSE if none found or not supported by an existing plugin.
42
   */
43 44
  public static function get_sitemap_form_entity_data($form_state, $form_id) {

Pawel G's avatar
Pawel G committed
45 46
    // Get all simple_sitemap plugins.
    $manager = \Drupal::service('plugin.manager.simple_sitemap');
47 48
    $plugins = $manager->getDefinitions();

Pawel G's avatar
Pawel G committed
49
    // Go through simple_sitemap plugins and check if one of them declares usage
50
    // of this particular form. If that's the case, get the entity type id of the
51 52 53 54 55 56 57 58 59 60 61 62
    // plugin definition and assume the bundle to be of the same name as the
    // entity type id.
    foreach($plugins as $plugin) {
      if (isset($plugin['form_id']) && $plugin['form_id'] === $form_id) {
        return array(
          'entity_type_id' => $plugin['id'],
          'bundle_name' => $plugin['id'],
        );
      }
    }

    // Else get entity type id and bundle name from the form if available and only
Pawel G's avatar
Pawel G committed
63
    // if a simple_sitemap plugin of the same entity type exists.
64 65 66 67 68 69 70 71 72 73 74 75 76 77
    $form_entity = self::get_form_entity($form_state);
    if ($form_entity !== FALSE) {
      $form_entity_type_id = $form_entity->getEntityTypeId();
      if (isset($plugins[$form_entity_type_id])) {
        if (!isset($plugins[$form_entity_type_id]['form_id'])
          || $plugins[$form_entity_type_id]['form_id'] === $form_id) {
          return array(
            'entity_type_id' => $form_entity_type_id,
            'bundle_name' => $form_entity->Id(),
          );
        }
      }
    }

Pawel G's avatar
Pawel G committed
78
    // If both methods of getting simple_sitemap entity data for this form
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // failed, return FALSE.
    return FALSE;
  }

  /**
   * Gets the object entity of the form if available.
   *
   * @param object $form_state
   *
   * @return object $entity or FALSE if non-existent or if form operation is
   *  'delete'.
   */
  private static function get_form_entity($form_state) {
    $form_object = $form_state->getFormObject();
    if (!is_null($form_object)
      && method_exists($form_state->getFormObject(), 'getEntity')
      && $form_object->getOperation() !== 'delete') {
96 97 98 99 100 101
      $entity = $form_state->getFormObject()->getEntity();
      return $entity;
    }
    return FALSE;
  }

102 103 104
  /**
   * Gets sitemap from db.
   */
105
  private function get_sitemap_from_db() {
Pawel G's avatar
Pawel G committed
106
    $this->sitemap = db_query("SELECT * FROM {simple_sitemap}")->fetchAllAssoc('id');
107
  }
108

109
  /**
110
   * Gets sitemap settings from the configuration storage.
111
   */
112
  private function get_config_from_db() {
Pawel G's avatar
Pawel G committed
113
    $this->config = \Drupal::config('simple_sitemap.settings');
114 115
  }

116
  /**
117
   * Gets a specific sitemap configuration from the configuration storage.
118 119 120
   *
   * @return mixed
   *  The requested configuration.
121
   */
122 123
  public function get_config($key) {
    return $this->config->get($key);
124 125
  }

126
  /**
127
   * Saves a specific sitemap configuration to db.
128
   *
129 130 131 132
   * @param string $key
   *  Configuration key, like 'entity_links'.
   * @param mixed $value
   *  The configuration to be saved.
133
   */
134
  public function save_config($key, $value) {
Pawel G's avatar
Pawel G committed
135
    \Drupal::service('config.factory')->getEditable('simple_sitemap.settings')
136
      ->set($key, $value)->save();
137
    $this->initialize();
138 139
  }

140 141 142 143 144 145 146 147 148 149 150
  /**
   * 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.
   */
151 152 153 154 155 156 157 158 159 160
  public function get_sitemap($sitemap_id = NULL) {
    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 {
161 162 163 164
        if (isset($this->sitemap[1])) {
          return $this->sitemap[1]->sitemap_string;
        }
        return FALSE;
165 166 167 168
      }
    }
    // Return specific sitemap chunk.
    else {
169
      return $this->sitemap[$sitemap_id]->sitemap_string;
170
    }
171 172
  }

173 174 175
  /**
   * Generates the sitemap for all languages and saves it to the db.
   */
Pawel G's avatar
Pawel G committed
176
  public function generate_sitemap($from = 'form') {
177
    Cache::invalidateTags(array('simple_sitemap'));
Pawel G's avatar
Pawel G committed
178 179
    db_truncate('simple_sitemap')->execute();
    $generator = new SitemapGenerator($from);
180 181
    $generator->set_custom_links($this->get_config('custom'));
    $generator->set_entity_types($this->get_config('entity_types'));
Pawel G's avatar
Pawel G committed
182
    $generator->start_batch();
183 184
  }

185
  /**
186 187 188 189
   * Generates and returns the sitemap index as string.
   *
   * @return string
   *  The sitemap index.
190
   */
191 192 193
  private function get_sitemap_index() {
    $generator = new SitemapGenerator();
    return $generator->generate_sitemap_index($this->sitemap);
Pawel G's avatar
Pawel G committed
194 195
  }

196
  /**
197 198 199 200 201 202 203
   * 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.
204
   */
205 206 207
  public function get_setting($name) {
    $settings = $this->get_config('settings');
    return isset($settings[$name]) ? $settings[$name] : FALSE;
208
  }
209

210
  /**
211 212 213 214 215 216
   * Saves a specific sitemap setting to db.
   *
   * @param $name
   *  Setting name, like 'max_links'.
   * @param $setting
   *  The setting to be saved.
217
   */
218 219 220 221
  public function save_setting($name, $setting) {
    $settings = $this->get_config('settings');
    $settings[$name] = $setting;
    $this->save_config('settings', $settings);
222
  }
223

Pawel G's avatar
Pawel G committed
224 225
  /**
   * Returns a 'time ago' string of last timestamp generation.
226
   *
Pawel G's avatar
Pawel G committed
227 228 229
   * @return mixed
   *  Formatted timestamp of last sitemap generation, otherwise FALSE.
   */
230
  public function get_generated_ago() {
231
    if (isset($this->sitemap[1]->sitemap_created)) {
232
      return \Drupal::service('date.formatter')
233
        ->formatInterval(REQUEST_TIME - $this->sitemap[1]->sitemap_created);
234 235 236
    }
    return FALSE;
  }
Pawel G's avatar
Pawel G committed
237 238 239 240

  public static function get_default_lang_id() {
    return \Drupal::languageManager()->getDefaultLanguage()->getId();
  }
241
}