Simplesitemap.php 6.69 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 10
use Drupal\Core\Cache\Cache;

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

18
  private $config;
19 20 21
  private $sitemap;

  function __construct() {
22
    $this->initialize();
23 24
  }

25 26 27 28 29
  private function initialize() {
    $this->get_config_from_db();
    $this->get_sitemap_from_db();
  }

30
  /**
31 32
   * 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.
33 34
   *
   * @param object $form_state
35
   * @param string $form_id
36
   *
37 38
   * @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.
39
   */
40 41
  public static function get_sitemap_form_entity_data($form_state, $form_id) {

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

Pawel G's avatar
Pawel G committed
46
    // Go through simple_sitemap plugins and check if one of them declares usage
47
    // of this particular form. If that's the case, get the entity type id of the
48 49 50 51 52 53 54 55 56 57 58 59
    // 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
60
    // if a simple_sitemap plugin of the same entity type exists.
61 62 63 64 65 66 67 68 69 70 71 72 73 74
    $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
75
    // If both methods of getting simple_sitemap entity data for this form
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    // 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') {
93 94 95 96 97 98
      $entity = $form_state->getFormObject()->getEntity();
      return $entity;
    }
    return FALSE;
  }

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

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

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

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

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

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

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

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

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

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

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