1<?php
2
3/**
4 * Implements hook_features_export_options().
5 */
6function wysiwyg_features_export_options() {
7  $profiles = array();
8
9  // Get human-readable name from filter module.
10  $formats = filter_formats();
11
12  foreach (array_keys(wysiwyg_profile_load_all()) as $format) {
13    // Text format may vanish without deleting the wysiwyg profile.
14    if (isset($formats[$format])) {
15      $profiles[$format] = $formats[$format]->name;
16    }
17  }
18  return $profiles;
19}
20
21/**
22 * Implements hook_features_export().
23 */
24function wysiwyg_features_export($data, &$export, $module_name = '') {
25  $pipe = array();
26
27  // The wysiwyg_default_formats() hook integration is provided by the
28  // features module so we need to add it as a dependency.
29  $export['dependencies']['features'] = 'features';
30  $export['dependencies']['wysiwyg'] = 'wysiwyg';
31
32  foreach ($data as $name) {
33    if ($profile = wysiwyg_get_profile($name)) {
34      // Add profile to exports.
35      $export['features']['wysiwyg'][$profile->format] = $profile->format;
36
37      // Chain filter format for export.
38      $pipe['filter'][] = $profile->format;
39    }
40  }
41
42  return $pipe;
43}
44
45/**
46 * Implements hook_features_export_render().
47 */
48function wysiwyg_features_export_render($module, $data, $export = NULL) {
49  $code = array();
50  $code[] = '  $profiles = array();';
51  $code[] = '';
52
53  foreach ($data as $name) {
54    if ($profile = wysiwyg_get_profile($name)) {
55      $profile = (array) $profile;
56      $profile_export = features_var_export($profile, '  ');
57      $profile_identifier = features_var_export($profile['format']);
58      $code[] = "  // Exported profile: {$profile['format']}.";
59      $code[] = "  \$profiles[{$profile_identifier}] = {$profile_export};";
60      $code[] = "";
61    }
62  }
63
64  $code[] = '  return $profiles;';
65  $code = implode("\n", $code);
66  return array('wysiwyg_default_profiles' => $code);
67}
68
69/**
70 * Implements hook_features_revert().
71 */
72function wysiwyg_features_revert($module) {
73  return wysiwyg_features_rebuild($module);
74}
75
76/**
77 * Implements hook_features_rebuild().
78 */
79function wysiwyg_features_rebuild($module) {
80  if ($defaults = features_get_default('wysiwyg', $module)) {
81    foreach ($defaults as $profile) {
82      $profile = is_object($profile) ? (array) $profile : $profile;
83      if (empty($profile['settings']['_profile_preferences'])) {
84        if (!empty($profile['preferences'])) {
85          $settings = &$profile['preferences'];
86        }
87        else {
88          // Importing an older profile, move state to its own section.
89          $settings = &$profile['settings'];
90        }
91
92        $preferences = array(
93          'add_to_summaries' => (!empty($settings['add_to_summaries']) ? $settings['add_to_summaries'] : FALSE),
94          'default' => $settings['default'],
95          'show_toggle' => $settings['show_toggle'],
96          'user_choose' => $settings['user_choose'],
97          'version' => (!empty($settings['version']) ? $settings['version'] : NULL),
98        );
99        unset($settings['add_to_summaries'],
100          $settings['default'],
101          $settings['show_toggle'],
102          $settings['user_choose'],
103          $settings['version'],
104          $profile['preferences']
105        );
106
107        if (!empty($settings['library'])) {
108          $preferences['library'] = $settings['library'];
109          unset($settings['library']);
110        }
111
112        $editor = wysiwyg_get_editor($profile['editor']);
113        if (empty($preferences['version']) && !empty($editor['installed'])) {
114          $preferences['version'] = $editor['installed version'];
115        }
116
117        $profile['settings']['_profile_preferences'] = $preferences;
118      }
119
120      db_merge('wysiwyg')
121        ->key(array('format' => $profile['format']))
122        ->fields(array(
123          'editor' => $profile['editor'],
124          'settings' => serialize($profile['settings']),
125        ))
126        ->execute();
127      // Clear the editing caches.
128      if (module_exists('ctools')) {
129        ctools_include('object-cache');
130        ctools_object_cache_clear_all('wysiwyg_profile', 'format' . $profile['format']);
131      }
132      cache_clear_all('wysiwyg_profile:format' . $profile['format'], 'cache');
133    }
134    wysiwyg_profile_cache_clear();
135  }
136}
137