| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
*
|
| 6 |
* Provides the basic plugin definition used by plugins.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Find all topichub plugins defined in all modules. A plugin is defined
|
| 11 |
* by 'plugin_name' => array(options)
|
| 12 |
*
|
| 13 |
* Plugin name should be unique across all plugins. Otherwise the last one
|
| 14 |
* will win and override the previous.
|
| 15 |
*
|
| 16 |
* Options for plugin arrays are:
|
| 17 |
* - title: Required. The title/name of the plugin
|
| 18 |
* - description: Optional. A description that may be used somewhere.
|
| 19 |
* - handler: Required. The class that extends topichub_plugin.
|
| 20 |
* - path: Optional. The path to the file containing the handler class.
|
| 21 |
* Defaults to plugins/<em>plugin_name</em>
|
| 22 |
* - file: Options. The filename for the plugin handler.
|
| 23 |
* Defaults to <em>handlername</em>.inc
|
| 24 |
*/
|
| 25 |
function topichubs_discover_plugins() {
|
| 26 |
static $cache;
|
| 27 |
|
| 28 |
// Get plugins from all modules.
|
| 29 |
foreach (module_implements('topichubs_plugins') as $module) {
|
| 30 |
$function = $module . '_topichubs_plugins';
|
| 31 |
$result = $function();
|
| 32 |
if (!is_array($result)) {
|
| 33 |
continue;
|
| 34 |
}
|
| 35 |
|
| 36 |
$module_dir = drupal_get_path('module', $module);
|
| 37 |
foreach ($result as $plugin => $def) {
|
| 38 |
$def['module'] = $module;
|
| 39 |
|
| 40 |
if (!isset($def['file'])) {
|
| 41 |
$def['file'] = $def['module'] . '.topichubs.inc';
|
| 42 |
}
|
| 43 |
if (!isset($def['theme path'])) {
|
| 44 |
$def['theme path'] = $module_dir;
|
| 45 |
}
|
| 46 |
// merge the new data in
|
| 47 |
$cache[$plugin] = $def;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
return $cache;
|
| 51 |
}
|
| 52 |
|
| 53 |
/**
|
| 54 |
* Look up plugin implementation class by plugin name (human readable one).
|
| 55 |
**/
|
| 56 |
function topichubs_lookup_plugin_by_name($name) {
|
| 57 |
static $plugins_map;
|
| 58 |
|
| 59 |
if (!empty($plugins_map)) {
|
| 60 |
if (!empty($plugins_map[$name])) {
|
| 61 |
return $plugins_map[$name];
|
| 62 |
} else {
|
| 63 |
return NULL;
|
| 64 |
}
|
| 65 |
}
|
| 66 |
|
| 67 |
$plugins = topichubs_discover_plugins();
|
| 68 |
$p_map = array();
|
| 69 |
if (is_array($plugins)) {
|
| 70 |
foreach ($plugins as $key => $plugin) {
|
| 71 |
$impl = new StdClass();
|
| 72 |
$impl->type = $key;
|
| 73 |
$impl->def = $plugin;
|
| 74 |
|
| 75 |
$p_map[$plugin['title']] = $impl;
|
| 76 |
}
|
| 77 |
$plugins_map = $p_map;
|
| 78 |
if (!empty($p_map[$name])) {
|
| 79 |
return $p_map[$name];
|
| 80 |
} else {
|
| 81 |
return NULL;
|
| 82 |
}
|
| 83 |
} else {
|
| 84 |
return NULL;
|
| 85 |
}
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Instantiate an instance of the handler class for the provided plugin definition.
|
| 90 |
*
|
| 91 |
* @param $definition
|
| 92 |
* A plugin definition as specified in hook_topichubs_plugins
|
| 93 |
*
|
| 94 |
* @see hook_topichubs_plugins
|
| 95 |
* @see topichubs_topichubs_plugins
|
| 96 |
*/
|
| 97 |
function _topichubs_new_handler_class($definition) {
|
| 98 |
$module_dir = drupal_get_path('module', $definition['module']);
|
| 99 |
$filename = $module_dir;
|
| 100 |
if(isset($definition['path'])) {
|
| 101 |
$filename .= '/' . $definition['path'];
|
| 102 |
}
|
| 103 |
$filename .= '/' . $definition['file'];
|
| 104 |
|
| 105 |
if (file_exists($filename)) {
|
| 106 |
require_once $filename;
|
| 107 |
if(class_exists($definition['handler'])) {
|
| 108 |
return new $definition['handler'];
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
return NULL;
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Base class to provide a common interface for all plugins.
|
| 117 |
*/
|
| 118 |
class topichub_plugin {
|
| 119 |
|
| 120 |
var $type;
|
| 121 |
var $conf;
|
| 122 |
var $node;
|
| 123 |
var $topichub;
|
| 124 |
var $settings;
|
| 125 |
|
| 126 |
/**
|
| 127 |
* Initialize the plugin with the topichub node and any settings previously saved.
|
| 128 |
*/
|
| 129 |
function init($type, $conf, &$node, $settings) {
|
| 130 |
$this->type = $type;
|
| 131 |
$this->conf = $conf;
|
| 132 |
$this->node = $node;
|
| 133 |
$this->topichub = $node->topichub;
|
| 134 |
$this->settings = $settings;
|
| 135 |
}
|
| 136 |
|
| 137 |
/**
|
| 138 |
* Provide a form for configuring default settings.
|
| 139 |
*/
|
| 140 |
function settings_form(&$form, &$form_state) { }
|
| 141 |
|
| 142 |
/**
|
| 143 |
* Validate the default settings.
|
| 144 |
*/
|
| 145 |
function settings_validate($form, &$form_state) { }
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Provide a form for setting per topichub options.
|
| 149 |
*/
|
| 150 |
function options_form(&$form, &$form_state) { }
|
| 151 |
|
| 152 |
/**
|
| 153 |
* Validate the options_form.
|
| 154 |
*/
|
| 155 |
function options_validate($form, &$form_state) { }
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Execute the plugin.
|
| 159 |
*
|
| 160 |
* @return
|
| 161 |
* The results of this topichub plugin.
|
| 162 |
*/
|
| 163 |
function execute() { return array(); }
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Set a form error, make sure the right path is set so the proper field is highlighted.
|
| 167 |
*/
|
| 168 |
function form_error($key, $msg) {
|
| 169 |
form_set_error("topichub][config][$this->type][$key]", $msg);
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Get a value form the plugin settings
|
| 174 |
*
|
| 175 |
* @param $key
|
| 176 |
* The setting key
|
| 177 |
* @param $default
|
| 178 |
* The value to use if the setting
|
| 179 |
*/
|
| 180 |
function get_setting($key, $default, $allow_empty = FALSE) {
|
| 181 |
if($allow_empty) {
|
| 182 |
return $this->settings && isset($this->settings[$key]) ? $this->settings[$key] : $default;
|
| 183 |
}
|
| 184 |
|
| 185 |
return $this->settings && !empty($this->settings[$key]) ? $this->settings[$key] : $default;
|
| 186 |
}
|
| 187 |
|
| 188 |
function get_global_default_types() {
|
| 189 |
return variable_get('topic_hub_plugin_type_default' , array());
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Get the content type settings.
|
| 194 |
*/
|
| 195 |
function get_types_setting() {
|
| 196 |
$types = $this->get_setting('types', array());
|
| 197 |
return array_filter(array_values($types));
|
| 198 |
}
|
| 199 |
|
| 200 |
/**
|
| 201 |
* Build a plugin form.
|
| 202 |
*
|
| 203 |
* @param $form_type
|
| 204 |
* The type of plugin form to build (corresponds to a plugin function like options_form, etc)
|
| 205 |
* @param $form
|
| 206 |
* The form to add to
|
| 207 |
* @param $form_state
|
| 208 |
* The current form state
|
| 209 |
*/
|
| 210 |
function build_form($form_type, &$form, &$form_state) {
|
| 211 |
$form_method = "{$form_type}_form";
|
| 212 |
if(method_exists($this, $form_method)) {
|
| 213 |
$plugin_form = array();
|
| 214 |
$this->$form_method($plugin_form, $form_state);
|
| 215 |
if(!empty($plugin_form)) {
|
| 216 |
$form[$this->type] = array(
|
| 217 |
'#type' => 'markup',
|
| 218 |
'#title' => t($this->conf['title']),
|
| 219 |
);
|
| 220 |
$form[$this->type] = array_merge($form[$this->type], $plugin_form);
|
| 221 |
}
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Add a field to specify desired node types.
|
| 227 |
*/
|
| 228 |
function add_types_field(&$form, &$form_state) {
|
| 229 |
$types = node_get_types();
|
| 230 |
foreach ($types as $type => $info) {
|
| 231 |
$options[$type] = $info->name;
|
| 232 |
}
|
| 233 |
|
| 234 |
$form['types'] = array(
|
| 235 |
'#type' => 'checkboxes',
|
| 236 |
'#title' => t('Node Types to Include'),
|
| 237 |
'#description' => t('Choose which node types are eligible for inclusion in the Locations Mentioned map.'),
|
| 238 |
'#options' => $options,
|
| 239 |
'#default_value' => $this->settings['types'] ? $this->settings['types'] : $this->get_global_default_types(),
|
| 240 |
);
|
| 241 |
}
|
| 242 |
|
| 243 |
/**
|
| 244 |
* Add a field to specify a node count.
|
| 245 |
*/
|
| 246 |
function add_count_field(&$form, &$form_state) {
|
| 247 |
$form['count'] = array(
|
| 248 |
'#type' => 'textfield',
|
| 249 |
'#title' => t('Number of nodes to return'),
|
| 250 |
'#description' => t('Select the maximum number of nodes to be displayed.'),
|
| 251 |
'#size' => 4,
|
| 252 |
'#maxlength' => 4,
|
| 253 |
'#default_value' => $this->settings['count'] ? $this->settings['count'] : 10,
|
| 254 |
);
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Base class for plugins that use Views.
|
| 260 |
*/
|
| 261 |
class topichub_views_plugin extends topichub_plugin {
|
| 262 |
|
| 263 |
var $view;
|
| 264 |
|
| 265 |
function init($type, $conf, &$node, $settings) {
|
| 266 |
parent::init($type, $conf, $node, $settings);
|
| 267 |
$view_name = $this->get_view_name();
|
| 268 |
$this->view = views_get_view($view_name);
|
| 269 |
}
|
| 270 |
|
| 271 |
/**
|
| 272 |
* Get the name of the view this plugin uses.
|
| 273 |
*
|
| 274 |
* @return
|
| 275 |
* The view name for this plugin
|
| 276 |
*/
|
| 277 |
function get_view_name() {}
|
| 278 |
|
| 279 |
/**
|
| 280 |
* Add Content Type filter to the View
|
| 281 |
*
|
| 282 |
* @param $types
|
| 283 |
* The content types
|
| 284 |
* @param $display
|
| 285 |
* The view display on which to add the filter
|
| 286 |
*/
|
| 287 |
function add_content_type_filter($types, $display = 'block_1') {
|
| 288 |
if(!empty($types)) {
|
| 289 |
$this->view->add_item($display, 'filter', 'node', 'type', array('value' => $types));
|
| 290 |
}
|
| 291 |
}
|
| 292 |
|
| 293 |
/**
|
| 294 |
* Add per page item count to the view.
|
| 295 |
*
|
| 296 |
* @param $count
|
| 297 |
* The number of items to display
|
| 298 |
* @param $display
|
| 299 |
* The view display on which to add the count
|
| 300 |
*/
|
| 301 |
function set_items_per_page($count, $display = 'block_1') {
|
| 302 |
$this->view->set_display($display);
|
| 303 |
$view_display = $this->view->display[$display];
|
| 304 |
$view_display->handler->set_option('items_per_page', $count);
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
* Add Topic Hub Expression filter to the View
|
| 309 |
*
|
| 310 |
* @param $display
|
| 311 |
* The view display on which to add the filter
|
| 312 |
*/
|
| 313 |
function add_topichub_filter($display = 'block_1') {
|
| 314 |
if($this->node->topichub->conditions) {
|
| 315 |
$this->view->add_item($display, 'filter', 'topichub', 'nid', array('value' => $this->node->nid));
|
| 316 |
}
|
| 317 |
}
|
| 318 |
|
| 319 |
/**
|
| 320 |
* Run the view.
|
| 321 |
*
|
| 322 |
* @param $display
|
| 323 |
* The view display to run
|
| 324 |
* @return
|
| 325 |
* An array containing the raw results and the formatted results
|
| 326 |
*/
|
| 327 |
function execute_view($display = 'block_1') {
|
| 328 |
$content = $this->view->preview($display, array($this->node->nid));
|
| 329 |
$hub_data = array(
|
| 330 |
'#values' => $view->result,
|
| 331 |
'#view' => $content,
|
| 332 |
);
|
| 333 |
return $hub_data;
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
| 337 |
/**
|
| 338 |
* Base class for plugins that create their own SQL.
|
| 339 |
* Provide some convenience functions where possible for common operations.
|
| 340 |
*/
|
| 341 |
class topichub_sql_plugin extends topichub_plugin {
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Add Content Type filter to the View
|
| 345 |
*
|
| 346 |
* @param $types
|
| 347 |
* The content types
|
| 348 |
* @param $display
|
| 349 |
* The view display on which to add the filter
|
| 350 |
*/
|
| 351 |
function get_term_where($node_alias = 'n') {
|
| 352 |
$joins = array();
|
| 353 |
$wheres = array();
|
| 354 |
$args = array();
|
| 355 |
|
| 356 |
foreach($this->topichub->conditions as $tids) {
|
| 357 |
$where_terms = array();
|
| 358 |
foreach($tids as $tid) {
|
| 359 |
$alias = 'term_node' . count($joins);
|
| 360 |
$joins[] = " JOIN {term_node} $alias ON {$node_alias}.nid = $alias.nid";
|
| 361 |
$where_terms[] = "$alias.tid = %d";
|
| 362 |
$args[] = $tid;
|
| 363 |
}
|
| 364 |
$wheres[] = '(' . implode(' AND ', $where_terms) . ')';
|
| 365 |
}
|
| 366 |
$where_clause = '(' . implode(' OR ', $wheres) . ')';
|
| 367 |
|
| 368 |
return array(
|
| 369 |
'joins' => $joins,
|
| 370 |
'where' => $where_clause,
|
| 371 |
'args' => $args,
|
| 372 |
);
|
| 373 |
}
|
| 374 |
|
| 375 |
/**
|
| 376 |
* Add Content Type filter to the query
|
| 377 |
*
|
| 378 |
* @param $types
|
| 379 |
* The content types
|
| 380 |
* @param $node_alias
|
| 381 |
* The the alias used for the node table
|
| 382 |
*/
|
| 383 |
function get_content_type_where($types, $node_alias = 'n') {
|
| 384 |
$where = '1=1';
|
| 385 |
$args = $types;
|
| 386 |
|
| 387 |
if(!empty($types)) {
|
| 388 |
$where = " {$node_alias}.type IN (" . db_placeholders($types, 'varchar') . ")";
|
| 389 |
}
|
| 390 |
|
| 391 |
return array(
|
| 392 |
'where' => $where,
|
| 393 |
'args' => $args,
|
| 394 |
);
|
| 395 |
}
|
| 396 |
|
| 397 |
|
| 398 |
}
|