Commit ae319dd7 authored by alexpott's avatar alexpott

Issue #2225283 by Primsi, martin107, slashrsm | fgm: Make Book storage independent.

parent e6681d4e
......@@ -6,7 +6,7 @@ services:
- { name: breadcrumb_builder, priority: 701 }
book.manager:
class: Drupal\book\BookManager
arguments: ['@database', '@entity.manager', '@string_translation', '@config.factory']
arguments: ['@entity.manager', '@string_translation', '@config.factory', '@book.outline_storage']
tags:
- { name: backend_overridable }
book.outline:
......@@ -15,7 +15,9 @@ services:
book.export:
class: Drupal\book\BookExport
arguments: ['@entity.manager', '@book.manager']
book.outline_storage:
class: Drupal\book\BookOutlineStorage
arguments: ['@database']
access_check.book.removable:
class: Drupal\book\Access\BookNodeIsRemovableAccessCheck
arguments: ['@book.manager']
......
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\book\BookOutlineStorage.
*/
namespace Drupal\book;
use Drupal\Core\Database\Connection;
/**
* Defines a storage class for books outline.
*/
class BookOutlineStorage implements BookOutlineStorageInterface {
/**
* Database Service Object.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a BookOutlineStorage object.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public function getBooks() {
return $this->connection->query("SELECT DISTINCT(bid) FROM {book}")->fetchCol();
}
/**
* {@inheritdoc}
*/
public function loadMultiple($nids) {
$query = $this->connection->select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC));
$query->fields('b');
$query->condition('b.nid', $nids);
$query->addTag('node_access');
$query->addMetaData('base_table', 'book');
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function getChildRelativeDepth($book_link, $max_depth) {
$query = $this->connection->select('book');
$query->addField('book', 'depth');
$query->condition('bid', $book_link['bid']);
$query->orderBy('depth', 'DESC');
$query->range(0, 1);
$i = 1;
$p = 'p1';
while ($i <= $max_depth && $book_link[$p]) {
$query->condition($p, $book_link[$p]);
$p = 'p' . ++$i;
}
return $query->execute()->fetchField();
}
/**
* {@inheritdoc}
*/
public function delete($nid) {
return $this->connection->delete('book')
->condition('nid', $nid)
->execute();
}
/**
* {@inheritdoc}
*/
public function loadBookChildren($pid) {
return $this->connection
->query("SELECT * FROM {book} WHERE pid = :pid", array(':pid' => $pid))
->fetchAllAssoc('nid', \PDO::FETCH_ASSOC);
}
/**
* {@inheritdoc}
*/
public function getBookMenuTree($bid, $parameters, $min_depth, $max_depth) {
$query = $this->connection->select('book');
$query->fields('book');
for ($i = 1; $i <= $max_depth; $i++) {
$query->orderBy('p' . $i, 'ASC');
}
$query->condition('bid', $bid);
if (!empty($parameters['expanded'])) {
$query->condition('pid', $parameters['expanded'], 'IN');
}
if ($min_depth != 1) {
$query->condition('depth', $min_depth, '>=');
}
if (isset($parameters['max_depth'])) {
$query->condition('depth', $parameters['max_depth'], '<=');
}
// Add custom query conditions, if any were passed.
if (isset($parameters['conditions'])) {
foreach ($parameters['conditions'] as $column => $value) {
$query->condition($column, $value);
}
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function insert($link, $parents) {
return $this->connection
->insert('book')
->fields(array(
'nid' => $link['nid'],
'bid' => $link['bid'],
'pid' => $link['pid'],
'weight' => $link['weight'],
) + $parents
)
->execute();
}
/**
* {@inheritdoc}
*/
public function update($nid, $fields) {
return $this->connection
->update('book')
->fields($fields)
->condition('nid', $nid)
->execute();
}
/**
* {@inheritdoc}
*/
public function updateMovedChildren($bid, $original, $expressions, $shift) {
$query = $this->connection->update('book');
$query->fields(array('bid' => $bid));
foreach ($expressions as $expression) {
$query->expression($expression[0], $expression[1], $expression[2]);
}
$query->expression('depth', 'depth + :depth', array(':depth' => $shift));
$query->condition('bid', $original['bid']);
$p = 'p1';
for ($i = 1; !empty($original[$p]); $p = 'p' . ++$i) {
$query->condition($p, $original[$p]);
}
return $query->execute();
}
/**
* {@inheritdoc}
*/
public function countOriginalLinkChildren($original) {
return $this->connection->select('book', 'b')
->condition('bid', $original['bid'])
->condition('pid', $original['pid'])
->condition('nid', $original['nid'], '<>')
->countQuery()
->execute()->fetchField();
}
/**
* {@inheritdoc}
*/
public function getBookSubtree($link, $max_depth) {
$query = db_select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC));
$query->fields('b');
$query->condition('b.bid', $link['bid']);
for ($i = 1; $i <= $max_depth && $link["p$i"]; ++$i) {
$query->condition("p$i", $link["p$i"]);
}
for ($i = 1; $i <= $max_depth; ++$i) {
$query->orderBy("p$i");
}
return $query->execute();
}
}
<?php
/**
* @file
* Contains \Drupal\book\BookOutlineStorageInterface.
*/
namespace Drupal\book;
/**
* Defines a common interface for book outline storage classes.
*/
interface BookOutlineStorageInterface {
/**
* Gets books (the highest positioned book links).
*
* @return array
* An array of book IDs.
*/
public function getBooks();
/**
* Loads books.
*
* @param array $nids
* An array of node IDs.
*
* @return array
* Array of loaded book items.
*/
public function loadMultiple($nids);
/**
* Gets child relative depth.
*
* @param array $book_link
* The book link.
*
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return int
* The depth of the searched book.
*/
public function getChildRelativeDepth($book_link, $max_depth);
/**
* Deletes a book entry.
*
* @param int $nid
* Deletes a book entry.
*
* @return mixed
* Number of deleted book entries.
*/
public function delete($nid);
/**
* Loads book's children using it's parent ID.
*
* @param int $pid
* The book's parent ID.
*
* @return array
* Array of loaded book items.
*/
public function loadBookChildren($pid);
/**
* Builds tree data used for the menu tree.
*
* @param int $bid
* The ID of the book that we are building the tree for.
* @param array $parameters
* An associative array of build parameters. For info about individual
* parameters see BookManager::bookTreeBuild().
* @param int $min_depth
* The minimum depth of book links in the resulting tree.
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return array
* Array of loaded book links.
*/
public function getBookMenuTree($bid, $parameters, $min_depth, $max_depth);
/**
* Inserts a book link.
*
* @param array $link
* The link array to be inserted in the database.
* @param array $parents
* The array of parent ids for the link to be inserted.
*
* @return mixed
* The last insert ID of the query, if one exists.
*/
public function insert($link, $parents);
/**
* Updates book reference for links that were moved between books.
*
* @param int $nid
* The nid of the book entry to be updated.
* @param array $fields
* The array of fields to be updated.
*
* @return mixed
* The number of rows matched by the update query.
*/
public function update($nid, $fields);
/**
* Update the book ID of the book link that it's being moved.
*
* @param int $bid
* The ID of the book whose children we move.
* @param array $original
* The original parent of the book link.
* @param array $expressions
* Array of expressions to be added to the query.
* @param int $shift
* The difference in depth between the old and the new position of the
* element being moved.
*
* @return mixed
* The number of rows matched by the update query.
*/
public function updateMovedChildren($bid, $original, $expressions, $shift);
/**
* Count the number of original link children.
*
* @param array $original
* The book link array.
*
* @return int
* Number of children.
*/
public function countOriginalLinkChildren($original);
/**
* Get book subtree.
*
* @param array $link
* A fully loaded book link.
* @param int $max_depth
* The maximum supported depth of the book tree.
*
* @return array
* Array of unordered subtree book items.
*/
public function getBookSubtree($link, $max_depth);
}
......@@ -149,11 +149,9 @@ public function build() {
}
elseif ($current_bid) {
// Only display this block when the user is browsing a book.
$select = db_select('node', 'n')
->fields('n', array('nid'))
->condition('n.nid', $node->book['bid'])
->addTag('node_access');
$nid = $select->execute()->fetchField();
$query = \Drupal::entityQuery('node');
$nid = $query->condition('nid', $node->book['bid'], '=')->execute();
// Only show the block if the user has view access for the top-level node.
if ($nid) {
$tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
......
......@@ -16,13 +16,6 @@
*/
class BookManagerTest extends UnitTestCase {
/**
* The mocked database connection.
*
* @var \Drupal\Core\Database\Connection|\PHPUnit_Framework_MockObject_MockObject
*/
protected $connection;
/**
* The mocked entity manager.
*
......@@ -51,17 +44,22 @@ class BookManagerTest extends UnitTestCase {
*/
protected $bookManager;
/**
* Book outline storage.
*
* @var \Drupal\book\BookOutlineStorageInterface
*/
protected $bookOutlineStorage;
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->getMock();
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$this->translation = $this->getStringTranslationStub();
$this->configFactory = $this->getConfigFactoryStub(array());
$this->bookManager = new BookManager($this->connection, $this->entityManager, $this->translation, $this->configFactory);
$this->bookOutlineStorage = $this->getMock('Drupal\book\BookOutlineStorageInterface');
$this->bookManager = new BookManager($this->entityManager, $this->translation, $this->configFactory, $this->bookOutlineStorage);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment