5 use Drupal\Core\Database\Connection;
8 * Defines a storage class for books outline.
10 class BookOutlineStorage implements BookOutlineStorageInterface {
13 * Database Service Object.
15 * @var \Drupal\Core\Database\Connection
17 protected $connection;
20 * Constructs a BookOutlineStorage object.
22 public function __construct(Connection $connection) {
23 $this->connection = $connection;
29 public function getBooks() {
30 return $this->connection->query("SELECT DISTINCT(bid) FROM {book}")->fetchCol();
36 public function hasBooks() {
37 return (bool) $this->connection
38 ->query('SELECT count(bid) FROM {book}')
45 public function loadMultiple($nids, $access = TRUE) {
46 $query = $this->connection->select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
48 $query->condition('b.nid', $nids, 'IN');
51 $query->addTag('node_access');
52 $query->addMetaData('base_table', 'book');
55 return $query->execute();
61 public function getChildRelativeDepth($book_link, $max_depth) {
62 $query = $this->connection->select('book');
63 $query->addField('book', 'depth');
64 $query->condition('bid', $book_link['bid']);
65 $query->orderBy('depth', 'DESC');
70 while ($i <= $max_depth && $book_link[$p]) {
71 $query->condition($p, $book_link[$p]);
75 return $query->execute()->fetchField();
81 public function delete($nid) {
82 return $this->connection->delete('book')
83 ->condition('nid', $nid)
90 public function loadBookChildren($pid) {
91 return $this->connection
92 ->query("SELECT * FROM {book} WHERE pid = :pid", [':pid' => $pid])
93 ->fetchAllAssoc('nid', \PDO::FETCH_ASSOC);
99 public function getBookMenuTree($bid, $parameters, $min_depth, $max_depth) {
100 $query = $this->connection->select('book');
101 $query->fields('book');
102 for ($i = 1; $i <= $max_depth; $i++) {
103 $query->orderBy('p' . $i, 'ASC');
105 $query->condition('bid', $bid);
106 if (!empty($parameters['expanded'])) {
107 $query->condition('pid', $parameters['expanded'], 'IN');
109 if ($min_depth != 1) {
110 $query->condition('depth', $min_depth, '>=');
112 if (isset($parameters['max_depth'])) {
113 $query->condition('depth', $parameters['max_depth'], '<=');
115 // Add custom query conditions, if any were passed.
116 if (isset($parameters['conditions'])) {
117 foreach ($parameters['conditions'] as $column => $value) {
118 $query->condition($column, $value);
122 return $query->execute();
128 public function insert($link, $parents) {
129 return $this->connection
132 'nid' => $link['nid'],
133 'bid' => $link['bid'],
134 'pid' => $link['pid'],
135 'weight' => $link['weight'],
144 public function update($nid, $fields) {
145 return $this->connection
148 ->condition('nid', $nid)
155 public function updateMovedChildren($bid, $original, $expressions, $shift) {
156 $query = $this->connection->update('book');
157 $query->fields(['bid' => $bid]);
159 foreach ($expressions as $expression) {
160 $query->expression($expression[0], $expression[1], $expression[2]);
163 $query->expression('depth', 'depth + :depth', [':depth' => $shift]);
164 $query->condition('bid', $original['bid']);
166 for ($i = 1; !empty($original[$p]); $p = 'p' . ++$i) {
167 $query->condition($p, $original[$p]);
170 return $query->execute();
176 public function countOriginalLinkChildren($original) {
177 return $this->connection->select('book', 'b')
178 ->condition('bid', $original['bid'])
179 ->condition('pid', $original['pid'])
180 ->condition('nid', $original['nid'], '<>')
182 ->execute()->fetchField();
188 public function getBookSubtree($link, $max_depth) {
189 $query = db_select('book', 'b', ['fetch' => \PDO::FETCH_ASSOC]);
191 $query->condition('b.bid', $link['bid']);
193 for ($i = 1; $i <= $max_depth && $link["p$i"]; ++$i) {
194 $query->condition("p$i", $link["p$i"]);
196 for ($i = 1; $i <= $max_depth; ++$i) {
197 $query->orderBy("p$i");
199 return $query->execute();