Skip to content

Commit ae463a8

Browse files
author
Jason Smith
committed
Initial code commit
1 parent 69026ae commit ae463a8

File tree

5 files changed

+990
-0
lines changed

5 files changed

+990
-0
lines changed

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "customer-portal/pkplugins",
3+
"authors": [
4+
{
5+
"name": "Red Hat Customer Portal",
6+
"email": "[email protected]"
7+
}
8+
],
9+
"require": {},
10+
"config": {},
11+
"repositories": []
12+
}

pkplugins/pkplugins.info

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name="Patternkit plugin - Red Hat Base"
2+
description="Adds patternkit patterns to panels as content types."
3+
package="Presentation Framework"
4+
core=7.x
5+
version=7.x-1.0

pkplugins/pkplugins.install

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* @file
4+
* Install file for ct_article module.
5+
*/
6+
7+
/**
8+
* Add Schema for cache data.
9+
*/
10+
function pkplugins_schema() {
11+
$schema = array();
12+
13+
// Add the redhat_pkplugins caching table.
14+
$schema['cache_pkplugins'] = drupal_get_schema_unprocessed('system', 'cache');
15+
16+
// // Add the redhat_pkplugins module html artifact table.
17+
// $schema['pkplugins_artifacts'] = array(
18+
// 'description' => 'The artifact table for pkplugins. Provides a failover for origin unavailability.',
19+
// 'fields' => array(
20+
// 'uuid' => array(
21+
// 'type' => 'char',
22+
// 'length' => 36,
23+
// 'not null' => TRUE,
24+
// 'default' => '',
25+
// 'description' => 'The Universally Unique Identifier.',
26+
// ),
27+
// 'content' => array(
28+
// 'description' => 'The artifact of interest.',
29+
// 'type' => 'longtext',
30+
// 'default' => '',
31+
// ),
32+
// 'type' => array(
33+
// 'description' => 'The {node_type} of this node.',
34+
// 'type' => 'varchar',
35+
// 'length' => 32,
36+
// 'not null' => TRUE,
37+
// 'default' => '',
38+
// ),
39+
// 'title' => array(
40+
// 'description' => 'The title of this node, always treated a non-markup plain text.',
41+
// 'type' => 'varchar',
42+
// 'length' => 255,
43+
// 'not null' => TRUE,
44+
// 'default' => '',
45+
// ),
46+
// 'created' => array(
47+
// 'description' => 'The Unix timestamp when the node was created.',
48+
// 'type' => 'int',
49+
// 'not null' => TRUE,
50+
// 'default' => 0,
51+
// ),
52+
// 'changed' => array(
53+
// 'description' => 'The Unix timestamp when the node was most recently saved.',
54+
// 'type' => 'int',
55+
// 'not null' => TRUE,
56+
// 'default' => 0,
57+
// ),
58+
// ),
59+
// 'indexes' => array(
60+
// 'node_changed' => array('changed'),
61+
// 'node_created' => array('created'),
62+
// ),
63+
// 'unique keys' => array(
64+
// 'nid_vid' => array('nid', 'vid'),
65+
// 'vid' => array('vid'),
66+
// ),
67+
// 'primary key' => array('nid'),
68+
// );
69+
70+
return $schema;
71+
}
72+

pkplugins/pkplugins.module

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)