|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @file |
| 4 | + * Patternkit wrapper/abstraction for panels. |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Implements hook_ctools_plugin_directory(). |
| 9 | + */ |
| 10 | +function pkplugins_ctools_plugin_directory($owner, $plugin) { |
| 11 | + if ($owner == 'ctools') { |
| 12 | + return 'plugins/' . $plugin; |
| 13 | + } |
| 14 | + |
| 15 | + return NULL; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Implements hook_flush_caches(). |
| 20 | + */ |
| 21 | +function pkplugins_flush_caches() { |
| 22 | + return array('cache_pkplugins'); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Implements hook_admin_menu_cache_info(). |
| 27 | + */ |
| 28 | +function pkplugins_admin_menu_cache_info() { |
| 29 | + $caches['pkplugins'] = array( |
| 30 | + 'title' => t('Flush Patternkit caches'), |
| 31 | + 'callback' => 'pkplugins_cache_clear_all', |
| 32 | + ); |
| 33 | + |
| 34 | + return $caches; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Callback for hook_admin_menu_cache_info() to clear all of the caches. |
| 39 | + */ |
| 40 | +function pkplugins_cache_clear_all() { |
| 41 | + foreach (pkplugins_flush_caches() as $cache_bin) { |
| 42 | + cache_clear_all('*', $cache_bin, TRUE); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Flush the pkplugins metadata cache. |
| 48 | + */ |
| 49 | +function pkplugins_flush_metadata_cache() { |
| 50 | + cache_clear_all('pkplugins_pk_metadata', 'cache', FALSE); |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Utility function to get all Patternkit module metadata. |
| 55 | + * |
| 56 | + * @param null $subtype |
| 57 | + * If specified, return only metadata for this subtype. |
| 58 | + * @param bool $reset |
| 59 | + * Optionally force the meta data to be reloaded. |
| 60 | + * |
| 61 | + * @return array|stdClass |
| 62 | + * Array of metadata objects found or object if specific module requested. |
| 63 | + */ |
| 64 | +function _pkplugins_get_metadata($subtype = NULL, $reset = FALSE) { |
| 65 | + $cached_metadata = &drupal_static(__FUNCTION__); |
| 66 | + |
| 67 | + // If the static cache doesn't exist, or we've called with reset, rebuild. |
| 68 | + if (!isset($cached_metadata) || $reset) { |
| 69 | + $pkplugins_cache_enabled = variable_get('pkplugins_cache_enabled', TRUE); |
| 70 | + // If cache is enabled, attempt to load from cache. |
| 71 | + if ($pkplugins_cache_enabled && ($cache = cache_get('pkplugins_pk_metadata'))) { |
| 72 | + $cached_metadata = $cache->data; |
| 73 | + } |
| 74 | + else { |
| 75 | + $patternkit_host = variable_get('pkplugins_pk_host', 'http://10.254.254.254:8080'); |
| 76 | + |
| 77 | + $patterns = drupal_http_request( |
| 78 | + $patternkit_host . '/api/patterns', |
| 79 | + array( |
| 80 | + 'headers' => array('Content-Type' => 'application/json'), |
| 81 | + 'timeout' => 10 |
| 82 | + ) |
| 83 | + ); |
| 84 | + if (!empty($patterns) && empty($patterns->error) && $patterns->code == 200) { |
| 85 | + $cached_metadata = (array)json_decode($patterns->data); |
| 86 | + |
| 87 | + // Cache the data so that we don't have to build it again. |
| 88 | + // (if cache enabled, otherwise just a slow, redundant memcache set). |
| 89 | + if ($pkplugins_cache_enabled == TRUE) { |
| 90 | + cache_set('pkplugins_pk_metadata', $cached_metadata, 'cache', CACHE_PERMANENT); |
| 91 | + } |
| 92 | + } else { |
| 93 | + _pkplugins_show_error( |
| 94 | + 'Patternkit failed to load metadata from service (%service_uri): %error', |
| 95 | + array( |
| 96 | + '%service_uri' => $patternkit_host . '/api/patterns', |
| 97 | + '%error' => !empty($patterns->error) ? $patterns->error : $patterns->code |
| 98 | + ) |
| 99 | + ); |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // If we are requesting data for a specific module type, return just |
| 106 | + // that data. |
| 107 | + if (!is_null($subtype) && strtolower($subtype) != 'none') { |
| 108 | + $lookup = substr($subtype, 3); |
| 109 | + if (!empty($cached_metadata[strtolower($lookup)])) { |
| 110 | + return $cached_metadata[strtolower($lookup)]; |
| 111 | + } |
| 112 | + else { |
| 113 | + _pkplugins_show_error( |
| 114 | + 'Patternkit module does not appear to exist (%module), verify module info/usage.', |
| 115 | + array('%module' => $lookup) |
| 116 | + ); |
| 117 | + |
| 118 | + return NULL; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $cached_metadata; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Utility function to display pkplugins error messages. |
| 127 | + * |
| 128 | + * Checks if permissions and configuration allow showing error, then displays. |
| 129 | + * |
| 130 | + * @param string $msg |
| 131 | + * The message to display (with t vars as appropriate). |
| 132 | + * @param array $vars |
| 133 | + * Optional array of replacement text appropriate for use in t function. |
| 134 | + * @param string $status |
| 135 | + * Status for the error message. Passed to drupal_set_message. |
| 136 | + */ |
| 137 | +function _pkplugins_show_error($msg, $vars = array(), $status = 'error') { |
| 138 | + if (variable_get('pkplugins_show_errors', FALSE) && user_is_logged_in()) { |
| 139 | + drupal_set_message(t($msg, $vars), $status); |
| 140 | + } |
| 141 | + if (variable_get('pkplugins_log_errors', FALSE)) { |
| 142 | + watchdog('pkplugins', $msg, $vars, WATCHDOG_ERROR); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | + |
0 commit comments