|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Provides install, update, and uninstall functions. |
| 6 | + * |
| 7 | + * @author Jim Berry ("solotandem", http://drupal.org/user/240748) |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements hook_uninstall(). |
| 12 | + */ |
| 13 | +function google_tag_uninstall() { |
| 14 | + db_delete('variable') |
| 15 | + ->condition('name', db_like('google_tag_') . '%', 'LIKE') |
| 16 | + ->execute(); |
| 17 | + drupal_clear_js_cache(); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Implements hook_requirements(). |
| 22 | + */ |
| 23 | +function google_tag_requirements($phase) { |
| 24 | + $t = get_t(); |
| 25 | + $requirements = array(); |
| 26 | + if ($phase == 'runtime') { |
| 27 | + if (!preg_match('/^GTM-\w{4,}$/', variable_get('google_tag_container_id', ''))) { |
| 28 | + // Google Tag Manager container ID has not been set. |
| 29 | + $args = array('@path' => '/admin/config/system/google_tag'); |
| 30 | + $description = $t('Configure this integration module on its <a href="@path">settings page</a>.', $args); |
| 31 | + $requirements['google_tag'] = array( |
| 32 | + 'title' => $t('Google Tag Manager'), |
| 33 | + 'description' => $description, |
| 34 | + 'severity' => REQUIREMENT_WARNING, |
| 35 | + 'value' => $t('Not configured'), |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + if ($phase == 'runtime' || $phase == 'install') { |
| 40 | + // Adapted from system_requirements(). |
| 41 | + $directory = variable_get('file_public_path', conf_path() . '/files') . '/js'; |
| 42 | + if (!is_dir($directory) || !is_writable($directory)) { |
| 43 | + file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); |
| 44 | + } |
| 45 | + $is_writable = is_writable($directory); |
| 46 | + $is_directory = is_dir($directory); |
| 47 | + if (!$is_writable || !$is_directory) { |
| 48 | + // The snippet directory does not exist or is not writable. |
| 49 | + if (!$is_directory) { |
| 50 | + $error = $t('The directory %directory does not exist.', array('%directory' => $directory)); |
| 51 | + $description = $t('An automated attempt to create the directory failed, possibly due to a permissions problem. Create the directory and make it writable.'); |
| 52 | + $value = $t('Does not exist'); |
| 53 | + } |
| 54 | + else { |
| 55 | + $error = $t('The directory %directory is not writable.', array('%directory' => $directory)); |
| 56 | + $description = $t('An automated attempt to make the directory writable failed, possibly due to a permissions problem. Make the directory writable.'); |
| 57 | + $value = $t('Not writable'); |
| 58 | + } |
| 59 | + if ($phase == 'install') { |
| 60 | + $description .= $t(' For more information, see INSTALL.txt or the <a href=":handbook_url">online handbook</a>.', array(':handbook_url' => 'https://www.drupal.org/server-permissions')); |
| 61 | + $value = ''; |
| 62 | + } |
| 63 | + $requirements['google_tag_snippet_directory'] = array( |
| 64 | + 'title' => $t('Google Tag Manager snippet directory'), |
| 65 | + 'description' => "$error $description", |
| 66 | + 'severity' => REQUIREMENT_ERROR, |
| 67 | + 'value' => $value, |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + return $requirements; |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Convert values in role_list variable from rid to role name. |
| 76 | + */ |
| 77 | +function google_tag_update_7101(&$sandbox) { |
| 78 | + $roles = user_roles(); |
| 79 | + $old_values = variable_get('google_tag_role_list', array()); |
| 80 | + $new_values = array(); |
| 81 | + foreach ($old_values as $rid => $old_value) { |
| 82 | + $role_name = $roles[$rid]; |
| 83 | + $new_values[$role_name] = $old_value; |
| 84 | + } |
| 85 | + variable_set('google_tag_role_list', $new_values); |
| 86 | + return t('Converted @count role IDs to role names in google_tag_role_list variable', array('@count' => count($old_values))); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Create directory for snippet files, if not present. |
| 91 | + */ |
| 92 | +function google_tag_update_7102(&$sandbox) { |
| 93 | + $result = _google_tag_snippet_directory_prepare(); |
| 94 | + return t('The directory exists (or was created) and is writable: @result', array('@result' => $result ? 1 : 0)); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Creates directory for snippet files, if not present. |
| 99 | + * |
| 100 | + * @return bool |
| 101 | + * Whether the directory exists (or was created) and is writable. |
| 102 | + */ |
| 103 | +function _google_tag_snippet_directory_prepare() { |
| 104 | + // Create directory if not present. |
| 105 | + $directory = 'public://js'; |
| 106 | + $result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); |
| 107 | + if (!$result) { |
| 108 | + drupal_set_message(t('An error occurred creating a directory for snippet files. Please try again or contact the site administrator if it persists.')); |
| 109 | + } |
| 110 | + return $result; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Convert toggle settings from integer to string. |
| 115 | + */ |
| 116 | +function google_tag_update_7103(&$sandbox) { |
| 117 | + $types = array('path', 'role', 'status'); |
| 118 | + $count = 0; |
| 119 | + |
| 120 | + if (module_exists('variable_realm') && module_exists('variable_store')) { |
| 121 | + // i18n_variable module depends on variable_realm, variable_store |
| 122 | + // In the course of variable_realm_set() calls will be made to: |
| 123 | + // - cache_clear_all('variables', 'cache_bootstrap'); |
| 124 | + // - cache_clear_all('variable:' . $realm . ':' . $key, 'cache_bootstrap'); |
| 125 | + // which will clear all relevant cache tables except for cache_variable. |
| 126 | + // Clear the latter at end. |
| 127 | + $realms = variable_realm_list(); |
| 128 | + foreach ($realms as $realm_name => $realm_title) { |
| 129 | + $keys = variable_realm_keys($realm_name); |
| 130 | + foreach ($keys as $key_name => $key_title) { |
| 131 | + foreach ($types as $type) { |
| 132 | + $name = "google_tag_{$type}_toggle"; |
| 133 | + $value = variable_realm_get($realm_name, $key_name, $name); |
| 134 | + if (!is_null($value)) { |
| 135 | + $new_value = $value ? GOOGLE_TAG_INCLUDE_LISTED : GOOGLE_TAG_EXCLUDE_LISTED; |
| 136 | + variable_realm_set($realm_name, $key_name, $name, $new_value, FALSE); |
| 137 | + $count++; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + variable_cache_clear(); |
| 143 | + } |
| 144 | + else { |
| 145 | + global $conf; |
| 146 | + foreach ($types as $type) { |
| 147 | + $name = "google_tag_{$type}_toggle"; |
| 148 | + if (isset($conf[$name])) { |
| 149 | + $new_value = $conf[$name] ? GOOGLE_TAG_INCLUDE_LISTED : GOOGLE_TAG_EXCLUDE_LISTED; |
| 150 | + variable_set($name, $new_value); |
| 151 | + $count++; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return t('Converted @count toggle settings', ['@count' => $count]); |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Rename 'compact_tag' setting to 'compact_snippet'. |
| 161 | + */ |
| 162 | +function google_tag_update_7104(&$sandbox) { |
| 163 | + $count = 0; |
| 164 | + $tables = array('variable'); |
| 165 | + if (module_exists('variable_realm') && module_exists('variable_store')) { |
| 166 | + $tables[] = 'variable_store'; |
| 167 | + $args = array( |
| 168 | + ':old' => 's:22:"google_tag_compact_tag"', |
| 169 | + ':new' => 's:26:"google_tag_compact_snippet"', |
| 170 | + ); |
| 171 | + $count += db_update('variable') |
| 172 | + ->expression('value', 'REPLACE(value, :old, :new)', $args) |
| 173 | + ->condition('name', 'variable_realm_list_language') |
| 174 | + ->execute(); |
| 175 | + } |
| 176 | + |
| 177 | + foreach ($tables as $table) { |
| 178 | + $count += db_update($table) |
| 179 | + ->fields(array('name' => 'google_tag_compact_snippet')) |
| 180 | + ->condition('name', 'google_tag_compact_tag') |
| 181 | + ->execute(); |
| 182 | + } |
| 183 | + |
| 184 | + cache_clear_all('variables', 'cache_bootstrap'); |
| 185 | + cache_clear_all('variable:', 'cache_bootstrap', TRUE); |
| 186 | + if (module_exists('variable')) { |
| 187 | + variable_cache_clear(); |
| 188 | + } |
| 189 | + |
| 190 | + return t('Converted @count occurrences of setting', ['@count' => $count]); |
| 191 | +} |
0 commit comments