summaryrefslogtreecommitdiffstats
path: root/wysiwyg.install
blob: d287c3176c31b0ce316e0083704bd6d9efe0d964 (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
<?php

/**
 * Implementation of hook_install().
 */
function wysiwyg_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {wysiwyg} (
        format int NOT NULL default '0',
        editor varchar(128) NOT NULL default '',
        settings text,
        PRIMARY KEY (format)
      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;

    case 'pgsql':
      db_query("CREATE TABLE {wysiwyg} (
        format int NOT NULL default '0',
        editor varchar(128) NOT NULL default '',
        settings text,
        PRIMARY KEY (format)
      )");
      break;
  }
}

/**
 * Implementation of hook_uninstall()
 */
function wysiwyg_uninstall() {
  db_query('DROP TABLE {wysiwyg}');
}

/**
 * Implementation of hook_enable().
 */
function wysiwyg_enable() {
  // Disable conflicting, obsolete editor integration modules whenever this
  // module is enabled. This is crude, but the only way to ensure no conflicts.
  module_disable(array(
    'ckeditor',
    'editarea',
    'editonpro',
    'editor',
    'fckeditor',
    'freerte',
    'htmlarea',
    'htmlbox',
    'jwysiwyg',
    'markitup',
    'nicedit',
    'openwysiwyg',
    'pegoeditor',
    'quicktext',
    'tinymce',
    'tinymce_autoconf',
    'tinytinymce',
    'whizzywig',
    'widgeditor',
    'wymeditor',
    'xstandard',
    'yui_editor',
  ));
}

/**
 * Retrieve a list of input formats to associate profiles to.
 */
function _wysiwyg_install_get_formats() {
  $formats = array();
  $result = db_query("SELECT format, name FROM {filter_formats}");
  while ($format = db_fetch_object($result)) {
    // Build a list of all formats.
    $formats[$format->format] = $format->name;
    // Fetch filters.
    $result2 = db_query("SELECT module, delta FROM {filters} WHERE format = %d", $format->format);
    while ($filter = db_fetch_object($result2)) {
      // If PHP filter is enabled, remove this format.
      if ($filter->module == 'filter' && $filter->delta == 1) {
        unset($formats[$format->format]);
        break;
      }
    }
  }
  return $formats;
}

/**
 * Associate Wysiwyg profiles with input formats.
 *
 * Since there was no association yet, we can only assume that there is one
 * profile only, and that profile must be duplicated and assigned to all input
 * formats (except PHP code format).  Also, input formats already have
 * titles/names, so Wysiwyg profiles do not need an own.
 *
 * Because input formats are already granted to certain user roles only, we can
 * remove our custom Wysiwyg profile permissions.  A 1:1 relationship between
 * input formats and permissions makes plugin_count obsolete, too.
 *
 * Since the resulting table is completely different, a new schema is installed.
 */
function wysiwyg_update_5001() {
  $ret = array();
  if (db_table_exists('wysiwyg')) {
    return $ret;
  }
  // Install new schema.
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {wysiwyg} (
        format int NOT NULL default '0',
        editor varchar(128) NOT NULL default '',
        settings text,
        PRIMARY KEY (format)
      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;

    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {wysiwyg} (
        format int NOT NULL default '0',
        editor varchar(128) NOT NULL default '',
        settings text,
        PRIMARY KEY (format)
      )");
      break;
  }

  // Fetch all input formats.
  $formats = _wysiwyg_install_get_formats();

  // Fetch all profiles.
  $result = db_query("SELECT name, settings FROM {wysiwyg_profile}");
  while ($profile = db_fetch_object($result)) {
    $profile->settings = unserialize($profile->settings);
    // Extract editor name from profile settings.
    $profile->editor = $profile->settings['editor'];
    // Clean-up.
    unset($profile->settings['editor']);
    unset($profile->settings['old_name']);
    unset($profile->settings['name']);
    unset($profile->settings['rids']);
    // Sorry.  There Can Be Only One. ;)
    break;
  }

  if ($profile) {
    // Rebuild profiles and associate with input formats.
    foreach ($formats as $format => $name) {
      // Insert profiles.
      // We can't use update_sql() here because of curly braces in serialized
      // array.
      db_query("INSERT INTO {wysiwyg} (format, editor, settings) VALUES (%d, '%s', '%s')", $format, $profile->editor, serialize($profile->settings));
      $ret[] = array(
        'success' => TRUE,
        'query' => strtr('Wysiwyg profile %profile converted and associated with input format %format.', array('%profile' => check_plain($profile->name), '%format' => check_plain($name))),
      );
    }
  }

  // Drop obsolete tables {wysiwyg_profile} and {wysiwyg_role}.
  $ret[] = update_sql("DROP TABLE {wysiwyg_profile}");
  $ret[] = update_sql("DROP TABLE {wysiwyg_role}");

  return $ret;
}

/**
 * Clear JS/CSS caches to ensure that clients load fresh copies.
 */
function wysiwyg_update_5200() {
  $ret = array();
  drupal_clear_css_cache();

  // Rebuild the menu to remove old admin/settings/wysiwyg/profile item.
  menu_rebuild();

  // Flush content caches.
  cache_clear_all();

  $ret[] = array(
    'success' => TRUE,
    'query' => 'Caches have been flushed.',
  );
  return $ret;
}

/**
 * Update enabled font plugin buttons to default plugin in TinyMCE profiles.
 */
function wysiwyg_update_5201() {
  $ret = array();
  $results = db_query("SELECT format, settings FROM {wysiwyg} WHERE editor = 'tinymce'");

  while ($profile = db_fetch_object($results)) {
    $settings = unserialize($profile->settings);
    // Move enabled 'font' buttons into 'default' plugin buttons.
    $changed = FALSE;
    foreach (array('formatselect', 'fontselect', 'fontsizeselect', 'styleselect') as $button) {
      if (isset($settings['buttons']['font'][$button])) {
        $settings['buttons']['default'][$button] = $settings['buttons']['font'][$button];
        unset($settings['buttons']['font'][$button]);
        $changed = TRUE;
      }
    }
    if ($changed) {
      // We can't use update_sql() here because of curly braces in serialized
      // array.
      db_query("UPDATE {wysiwyg} SET settings='%s' WHERE format = '%s'", array(serialize($settings), $profile->format));
    }
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => 'TinyMCE profiles have been updated.',
 );
  return $ret;
}