df4c0e9e194e56cb2e84935d714fa9a5a3923d66
[yaffs-website] / web / core / modules / block / src / Plugin / migrate / source / Block.php
1 <?php
2
3 namespace Drupal\block\Plugin\migrate\source;
4
5 use Drupal\migrate\Row;
6 use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
7
8 /**
9  * Drupal block source from database.
10  *
11  * @MigrateSource(
12  *   id = "block",
13  *   source_provider = "block"
14  * )
15  */
16 class Block extends DrupalSqlBase {
17
18   /**
19    * The default theme name.
20    *
21    * @var string
22    */
23   protected $defaultTheme;
24
25   /**
26    * The admin theme name.
27    *
28    * @var string
29    */
30   protected $adminTheme;
31
32   /**
33    * Table containing block configuration.
34    *
35    * @var string
36    */
37   protected $blockTable;
38
39   /**
40    * Table mapping blocks to user roles.
41    *
42    * @var string
43    */
44   protected $blockRoleTable;
45
46   /**
47    * Table listing user roles.
48    *
49    * @var string
50    */
51   protected $userRoleTable;
52
53   /**
54    * {@inheritdoc}
55    */
56   public function query() {
57     if ($this->getModuleSchemaVersion('system') >= 7000) {
58       $this->blockTable = 'block';
59       $this->blockRoleTable = 'block_role';
60     }
61     else {
62       $this->blockTable = 'blocks';
63       $this->blockRoleTable = 'blocks_roles';
64     }
65     // Drupal 6 & 7 both use the same name for the user roles table.
66     $this->userRoleTable = 'role';
67
68     return $this->select($this->blockTable, 'b')->fields('b');
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function initializeIterator() {
75     $this->defaultTheme = $this->variableGet('theme_default', 'Garland');
76     $this->adminTheme = $this->variableGet('admin_theme', NULL);
77     return parent::initializeIterator();
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function fields() {
84     return [
85       'bid' => $this->t('The block numeric identifier.'),
86       'module' => $this->t('The module providing the block.'),
87       'delta' => $this->t('The block\'s delta.'),
88       'theme' => $this->t('Which theme the block is placed in.'),
89       'status' => $this->t('Whether or not the block is enabled.'),
90       'weight' => $this->t('Weight of the block for ordering within regions.'),
91       'region' => $this->t('Region the block is placed in.'),
92       'visibility' => $this->t('Visibility expression.'),
93       'pages' => $this->t('Pages list.'),
94       'title' => $this->t('Block title.'),
95       'cache' => $this->t('Cache rule.'),
96     ];
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function getIds() {
103     $ids['module']['type'] = 'string';
104     $ids['delta']['type'] = 'string';
105     $ids['theme']['type'] = 'string';
106     return $ids;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function prepareRow(Row $row) {
113     $row->setSourceProperty('default_theme', $this->defaultTheme);
114     $row->setSourceProperty('admin_theme', $this->adminTheme);
115
116     $module = $row->getSourceProperty('module');
117     $delta = $row->getSourceProperty('delta');
118
119     $query = $this->select($this->blockRoleTable, 'br')
120       ->fields('br', ['rid'])
121       ->condition('module', $module)
122       ->condition('delta', $delta);
123     $query->join($this->userRoleTable, 'ur', 'br.rid = ur.rid');
124     $roles = $query->execute()
125       ->fetchCol();
126     $row->setSourceProperty('roles', $roles);
127
128     $settings = [];
129     switch ($module) {
130       case 'aggregator':
131         list($type, $id) = explode('-', $delta);
132         if ($type == 'feed') {
133           $item_count = $this->select('aggregator_feed', 'af')
134             ->fields('af', ['block'])
135             ->condition('fid', $id)
136             ->execute()
137             ->fetchField();
138         }
139         else {
140           $item_count = $this->select('aggregator_category', 'ac')
141             ->fields('ac', ['block'])
142             ->condition('cid', $id)
143             ->execute()
144             ->fetchField();
145         }
146         $settings['aggregator']['item_count'] = $item_count;
147         break;
148       case 'book':
149         $settings['book']['block_mode'] = $this->variableGet('book_block_mode', 'all pages');
150         break;
151       case 'forum':
152         $settings['forum']['block_num'] = $this->variableGet('forum_block_num_' . $delta, 5);
153         break;
154       case 'statistics':
155         foreach (['statistics_block_top_day_num', 'statistics_block_top_all_num', 'statistics_block_top_last_num'] as $name) {
156           $settings['statistics'][$name] = $this->variableGet($name, 0);
157         }
158         break;
159       case 'user':
160         switch ($delta) {
161           case 2:
162           case 'new':
163             $settings['user']['block_whois_new_count'] = $this->variableGet('user_block_whois_new_count', 5);
164             break;
165           case 3:
166           case 'online':
167             $settings['user']['block_seconds_online'] = $this->variableGet('user_block_seconds_online', 900);
168             $settings['user']['max_list_count'] = $this->variableGet('user_block_max_list_count', 10);
169             break;
170         }
171         break;
172     }
173     $row->setSourceProperty('settings', $settings);
174     return parent::prepareRow($row);
175   }
176
177 }