BookEventSubscriber.php 4.01 KB
Newer Older
Samuel Mortenson's avatar
Samuel Mortenson committed
1
2
<?php

3
namespace Drupal\tome_sync\EventSubscriber;
Samuel Mortenson's avatar
Samuel Mortenson committed
4
5
6
7

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Site\Settings;
8
use Drupal\tome_sync\Event\TomeSyncEvents;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
use Drupal\Core\File\FileSystemInterface;
Samuel Mortenson's avatar
Samuel Mortenson committed
11
12

/**
13
 * Event subscriber that keep book outlines in sync with content changes.
Samuel Mortenson's avatar
Samuel Mortenson committed
14
15
16
 *
 * The book module API isn't used for this because it seems incomplete and is
 * really difficult to work with.
17
18
 *
 * @internal
Samuel Mortenson's avatar
Samuel Mortenson committed
19
 */
20
class BookEventSubscriber implements EventSubscriberInterface {
Samuel Mortenson's avatar
Samuel Mortenson committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

36
37
38
39
40
41
42
  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

Samuel Mortenson's avatar
Samuel Mortenson committed
43
  /**
44
   * Creates a BookEventSubscriber object.
Samuel Mortenson's avatar
Samuel Mortenson committed
45
46
47
48
49
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
50
51
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
Samuel Mortenson's avatar
Samuel Mortenson committed
52
   */
53
  public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager, FileSystemInterface $file_system) {
Samuel Mortenson's avatar
Samuel Mortenson committed
54
55
    $this->connection = $connection;
    $this->entityTypeManager = $entity_type_manager;
56
    $this->fileSystem = $file_system;
Samuel Mortenson's avatar
Samuel Mortenson committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  }

  /**
   * Exports all book outlines.
   */
  public function exportBookOutlines() {
    $storage = $this->entityTypeManager->getStorage('node');
    $results = $this->connection->select('book')
      ->fields('book')
      ->execute()
      ->fetchAll();
    $id_map = [];
    $rows = [];
    foreach ($results as $result) {
      $row = [];
      foreach ($result as $key => $value) {
        if (!$value || in_array($key, ['has_children', 'weight', 'depth'], TRUE)) {
          $row[$key] = $value;
        }
        else {
          if (!isset($id_map[$value]) && $node = $storage->load($value)) {
            $id_map[$value] = $node->uuid();
          }
          $row[$key] = $id_map[$value];
        }
      }
      $rows[] = $row;
    }
    $directory = $this->getExportDirectory();
86
    $this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
Samuel Mortenson's avatar
Samuel Mortenson committed
87
88
89
90
91
92
93
    file_put_contents("$directory/book_outlines.json", json_encode($rows, JSON_PRETTY_PRINT));
  }

  /**
   * Imports all book outlines.
   */
  public function importBookOutlines() {
94
95
    $this->connection->truncate('book')->execute();

Samuel Mortenson's avatar
Samuel Mortenson committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    $directory = $this->getExportDirectory();
    $storage = $this->entityTypeManager->getStorage('node');
    if (!file_exists("$directory/book_outlines.json")) {
      return;
    }
    $rows = json_decode(file_get_contents("$directory/book_outlines.json"), TRUE);
    $id_map = [];
    foreach ($rows as $row) {
      foreach ($row as $key => $value) {
        if (!$value || in_array($key, ['has_children', 'weight', 'depth'], TRUE)) {
          $row[$key] = $value;
        }
        else {
          if (!isset($id_map[$value]) && $results = $storage->loadByProperties(['uuid' => $value])) {
            $node = reset($results);
            $id_map[$value] = $node->id();
          }
          $row[$key] = $id_map[$value];
        }
      }
      $this->connection->insert('book')
        ->fields($row)
        ->execute();
    }
  }

  /**
   * Gets the export directory for book outlines.
   *
   * @return string
   *   The book export directory.
   */
  protected function getExportDirectory() {
    return Settings::get('tome_book_outline_directory', '../extra');
  }

132
133
134
135
  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
136
137
138
    $events[TomeSyncEvents::IMPORT_ALL][] = ['importBookOutlines'];
    $events[TomeSyncEvents::EXPORT_CONTENT][] = ['exportBookOutlines'];
    $events[TomeSyncEvents::EXPORT_ALL][] = ['exportBookOutlines'];
139
140
141
    return $events;
  }

Samuel Mortenson's avatar
Samuel Mortenson committed
142
}