summaryrefslogtreecommitdiffstats
path: root/modules/media_wysiwyg/media_wysiwyg.install
blob: ae26856622a0407a1e4b720468607c35f1cee3c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
86
87
88
89
90
91
92
93
94
95
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php

/**
 * @file
 * Install, update and uninstall functions for the Media WYSIWYG module.
 */

/**
 * Implements hook_schema().
 */
function media_wysiwyg_schema() {
  $schema['media_restrict_wysiwyg'] = array(
    'description' => 'Stores media displays restricted in wysiwyg.',
    'fields' => array(
      'type' => array(
        'description' => 'The machine name of the file type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'display' => array(
        'description' => 'The restricted display.',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
    ),
  );
  $schema['media_view_mode_wysiwyg'] = array(
    'description' => 'Maps WYSIWYG view modes to file types.',
    'fields' => array(
      'type' => array(
        'description' => 'The machine name of the file type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'view_mode' => array(
        'description' => 'WYSIWYG view mode mapped to this file type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),

    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function media_wysiwyg_install() {
  media_wysiwyg_update_7204();

  // Start off with the alignment feature enabled.
  variable_set('media_wysiwyg_alignment', TRUE);
}

/**
 * Implements hook_uninstall().
 */
function media_wysiwyg_uninstall() {
  // Remove variables.
  variable_del('media_wysiwyg_wysiwyg_title');
  variable_del('media_wysiwyg_wysiwyg_icon_title');
  variable_del('media_wysiwyg_wysiwyg_default_view_mode');
  variable_del('media_wysiwyg_wysiwyg_upload_directory');
  variable_del('media_wysiwyg_wysiwyg_allowed_types');
  variable_del('media_wysiwyg_wysiwyg_allowed_attributes');
  variable_del('media_wysiwyg_wysiwyg_browser_plugins');
  variable_del('media_wysiwyg_wysiwyg_override_field_types');
  variable_del('media_wysiwyg_use_link_text_for_filename');
  variable_del('media_wysiwyg_alignment');
  variable_del('media_wysiwyg_external_link');
  variable_del('media_wysiwyg_remove_media_class');
}

/**
 * Implements hook_update_dependencies().
 */
function media_wysiwyg_update_dependencies() {
  // Ensure the "access media browser" permission is granted to users before
  // using it to grant the "use media wysiwyg" permission.
  $dependencies['media_wysiwyg'][7201] = array(
    'media' => 7226,
  );

  return $dependencies;
}

/**
 * Grant existing user access to new media wysiwyg permission.
 */
function media_wysiwyg_update_7201() {
  $roles = user_roles(TRUE, 'access media browser');
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array('use media wysiwyg'));
  }

  return t('Use Media WYSIWYG permission was granted to: @roles.', array(
    '@roles' => check_plain(implode(', ', $roles)),
  ));
}

/**
 * Use the legacy file entity rendering method for existing sites.
 *
 * Existing sites can change this setting at admin/config/media/browser.
 */
function media_wysiwyg_update_7202() {
  variable_set('media_wysiwyg_default_render', 'field_attach');
}

/**
 * Move integration with the stand-alone CKEditor module into the Media CKEditor module.
 */
function media_wysiwyg_update_7203() {
  $output = '';

  if (module_exists('ckeditor')) {
    $output .= t('CKEditor integration has been moved to the Media CKEditor module.');
    $output .= t('If you are using the stand-alone CKEditor module in combination with the CKEditor plugin provided by Media WYSIWYG then you must download and enable the <a href="@url">Media CKEditor</a> module.', array('@url' => 'https://www.drupal.org/project/media_ckeditor'));
  }

  return $output;
}

/**
 * Whitelists certain fields for WYSIWYG overriding.
 */
function media_wysiwyg_update_7204() {
  $instances = field_read_instances(array('entity_type' => 'file'));
  $updated = array();
  $set_to_default = array();
  foreach ($instances as $instance) {
    $field_info = field_info_field($instance['field_name']);
    $allowed_field_types = variable_get('media_wysiwyg_wysiwyg_override_field_types', array('text', 'text_long'));
    if (in_array($field_info['type'], $allowed_field_types)) {
      if (!isset($instance['settings']['wysiwyg_override'])) {
        $instance['settings']['wysiwyg_override'] = 1;
        field_update_instance($instance);
        $set_to_default[] = $instance['field_name'];
      }
    }
    elseif (isset($instance['settings']['wysiwyg_override'])) {
      unset($instance['settings']['wysiwyg_override']);
      field_update_instance($instance);
      $updated[] = $instance['field_name'];
    }
  }
  if (count($updated) || count($set_to_default)) {
    $updated_string = implode(', ', $updated);
    $default_string = implode(', ', $set_to_default);
    return t("Updated the following field instances: @updated_string so they can't be overridden when the file is inserted in the WYSIWYG. Updated the following fields @default_string so that they continue to show up when a file is inserted.", array(
      '@updated_string' => $updated_string,
      '@default_string' => $default_string,
    ));
  }
}

/**
 * Install {media_restrict_wysiwyg} and {media_view_mode_wysiwyg}.
 *
 * Remove variables media_wysiwyg_view_mode_%.
 *
 * Uninstall media_wysiwyg_view_mode module.
 */
function media_wysiwyg_update_7205() {
  $schema = media_wysiwyg_schema();

  // Create the new configuration tables.
  if (!db_table_exists('media_restrict_wysiwyg')) {
    db_create_table('media_restrict_wysiwyg', $schema['media_restrict_wysiwyg']);
    db_create_table('media_view_mode_wysiwyg', $schema['media_view_mode_wysiwyg']);
  }

  // Migrate the configuration from the old variables into the new DB tables.
  $types = file_type_load_all(TRUE);
  foreach ($types as $type) {
    $enabled = variable_get("media_wysiwyg_view_mode_" . $type->type . "_file_wysiwyg_restricted_view_modes_status", FALSE);
    if ($enabled) {
      $wysiwyg_restricted_view_modes = variable_get("media_wysiwyg_view_mode_" . $type->type . "_file_wysiwyg_restricted_view_modes", array());
      foreach ($wysiwyg_restricted_view_modes as $wysiwyg_restricted_view_mode) {
        db_insert('media_restrict_wysiwyg')
          ->fields(array(
            'type' => $type->type,
            'display' => $wysiwyg_restricted_view_mode,
          ))
          ->execute();
      }
    }

    $enabled = variable_get("media_wysiwyg_view_mode_" . $type->type . "_wysiwyg_view_mode_status", FALSE);
    if ($enabled) {
      $file_wysiwyg_view_mode = variable_get("media_wysiwyg_view_mode_" . $type->type . "_file_wysiwyg_view_mode", 'wysiwyg');
      db_insert('media_view_mode_wysiwyg')
        ->fields(array(
          'type' => $type->type,
          'view_mode' => $file_wysiwyg_view_mode,
        ))
        ->execute();
    }
  }

  // Remove old configuration variables.
  db_delete('variable')->condition('name', "media_wysiwyg_view_mode_%", "LIKE")->execute();

  // Disable and uninstall View Mode module.Since the view mode module is
  // deleted, this copies from  module_disable() and drupal_uninstall_modules().
  // Disable first.
  $module = 'media_wysiwyg_view_mode';
  db_update('system')
    ->fields(array('status' => 0))
    ->condition('type', 'module')
    ->condition('name', $module)
    ->execute();
  system_list_reset();
  module_list(TRUE);
  module_implements('', FALSE, TRUE);
  entity_info_cache_clear();
  // Invoke hook_modules_disabled before disabling modules,
  // so we can still call module hooks to get information.
  module_invoke_all('modules_disabled', array($module));
  // Update the registry to remove the newly-disabled module.
  registry_update();
  _system_update_bootstrap_status();
  // Update the theme registry to remove the newly-disabled module.
  drupal_theme_rebuild();

  // Now uninstall.
  drupal_uninstall_schema($module);
  drupal_set_installed_schema_version($module, SCHEMA_UNINSTALLED);

  module_invoke_all('modules_uninstalled', array($module));
}

/**
 * Notify upgraders that there's optional media alignment functionality that needs to be enabled.
 */
function media_wysiwyg_update_7206() {
  $message = t('If you would like to be able to align your embedded media (left, right, or center), go to /admin/config/media/browser and check "Provide alignment option when embedding media", and save the settings.');
  drupal_set_message($message, 'warning', TRUE);
}

/**
 * Notify upgraders that there's optional media linking functionality that needs to be enabled.
 */
function media_wysiwyg_update_7207() {
  $message = t('If you would like to be able to link images to a page, go to /admin/config/media/browser, check "Provide the ability to link media to pages", and save the settings.');
  drupal_set_message($message, 'warning', TRUE);
}