| 1 |
<?php
|
| 2 |
/*
|
| 3 |
Copyright (C) 2008 by Phase2 Technology.
|
| 4 |
Author(s): Frank Febbraro, Irakli Nadareishvili
|
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify
|
| 7 |
it under the terms of the GNU General Public License.
|
| 8 |
This program is distributed in the hope that it will be useful,
|
| 9 |
but WITHOUT ANY WARRANTY. See the LICENSE.txt file for more details.
|
| 10 |
|
| 11 |
$Id: topichubs.module,v 1.1 2009/03/03 23:53:11 febbraro Exp $
|
| 12 |
*/
|
| 13 |
/**
|
| 14 |
* @file
|
| 15 |
*/
|
| 16 |
|
| 17 |
define ('TOPICHUB_NO_RENDER_PLUGIN', 'TOPICHUB_NO_RENDER_PLUGIN');
|
| 18 |
|
| 19 |
module_load_include('inc', 'topichubs', 'topichubs.plugins');
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Determine the rendering plugin of topichubs. Returns FALSE if none
|
| 23 |
* of the modules define a plugin;
|
| 24 |
*/
|
| 25 |
function topichubs_render_plugin() {
|
| 26 |
static $pluginName;
|
| 27 |
|
| 28 |
if (empty($pluginName)) {
|
| 29 |
// Only one render plugin can be used at a time, so we are fetching the first one.
|
| 30 |
$plugins = module_implements('topichubs_render_plugin');
|
| 31 |
if (is_array($plugins) && sizeof($plugins)>0) {
|
| 32 |
$pluginName = $plugins[0];
|
| 33 |
}
|
| 34 |
else {
|
| 35 |
$pluginName = TOPICHUB_NO_RENDER_PLUGIN;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
return ($pluginName!=TOPICHUB_NO_RENDER_PLUGIN) ? $pluginName : FALSE;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* We need to make sure pass-by-reference works, so we can't func_get_args
|
| 44 |
**/
|
| 45 |
function topichubs_render_invoke($hook, &$arg0=null, &$arg1=null, &$arg2=null, &$arg3=null) {
|
| 46 |
$render_plugin = topichubs_render_plugin();
|
| 47 |
if (!empty($render_plugin)) {
|
| 48 |
|
| 49 |
$module = $render_plugin;
|
| 50 |
|
| 51 |
$function = $module .'_topichubs_render_'. $hook;
|
| 52 |
|
| 53 |
$result = $function($arg0, $arg1, $arg2, $arg3);
|
| 54 |
return $result;
|
| 55 |
} else {
|
| 56 |
return "#!#TH_RENDER_NO_IMPL#!#"; //None of the render implementation modules are enabled
|
| 57 |
}
|
| 58 |
}
|
| 59 |
|
| 60 |
/**
|
| 61 |
* Declare the views version we support (and provide views)
|
| 62 |
*/
|
| 63 |
function topichubs_views_api() {
|
| 64 |
return array(
|
| 65 |
'api' => 2.0,
|
| 66 |
'path' => drupal_get_path('module', 'topichubs') .'/views',
|
| 67 |
);
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* Implementation of hook_perm().
|
| 72 |
*/
|
| 73 |
function topichubs_perm() {
|
| 74 |
return array(
|
| 75 |
'administer topichubs',
|
| 76 |
);
|
| 77 |
}
|
| 78 |
|
| 79 |
/**
|
| 80 |
* Implementation of hook_menu().
|
| 81 |
*/
|
| 82 |
function topichubs_menu() {
|
| 83 |
$items = array();
|
| 84 |
$items['admin/content/topichubs'] = array(
|
| 85 |
'title' => 'Topic Hubs',
|
| 86 |
'description' => 'Topic Hubs',
|
| 87 |
'page callback' => 'topichubs_admin_overview',
|
| 88 |
'access arguments' => array('administer topichubs'),
|
| 89 |
'file' => 'topichubs.admin.inc',
|
| 90 |
);
|
| 91 |
$items['admin/content/topichubs/list'] = array(
|
| 92 |
'title' => 'List',
|
| 93 |
'description' => 'List all site Topic Hubs',
|
| 94 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 95 |
'weight' => 10,
|
| 96 |
);
|
| 97 |
$items['admin/content/topichubs/hot'] = array(
|
| 98 |
'title' => 'Hot Topics',
|
| 99 |
'description' => 'What Topics are Hot.',
|
| 100 |
'page callback' => 'topichubs_hot_topics_page',
|
| 101 |
'access arguments' => array('administer topichubs'),
|
| 102 |
'file' => 'topichubs.admin.inc',
|
| 103 |
'type' => MENU_LOCAL_TASK,
|
| 104 |
'weight' => 15,
|
| 105 |
);
|
| 106 |
$items['admin/content/topichubs/settings'] = array(
|
| 107 |
'title' => t('Settings'),
|
| 108 |
'description' => 'Configuration for Topic Hubs.',
|
| 109 |
'page callback' => 'drupal_get_form',
|
| 110 |
'page arguments' => array('topichubs_settings'),
|
| 111 |
'access arguments' => array('administer topichubs'),
|
| 112 |
'file' => 'topichubs.admin.inc',
|
| 113 |
'type' => MENU_LOCAL_TASK,
|
| 114 |
'weight' => 20,
|
| 115 |
);
|
| 116 |
if (_get_panels_version() >= 6300 && !variable_get('topichubs_panel_update_run', 0)) {
|
| 117 |
$items['admin/content/topichubs/update'] = array(
|
| 118 |
'title' => t('Update'),
|
| 119 |
'description' => 'Update existing Topic Hubs from Panels 2 to Panels 3.',
|
| 120 |
'page callback' => 'topichubs_update_page',
|
| 121 |
'access arguments' => array('administer topichubs'),
|
| 122 |
'file' => 'topichubs.admin.inc',
|
| 123 |
'type' => MENU_LOCAL_TASK,
|
| 124 |
'weight' => 25,
|
| 125 |
);
|
| 126 |
}
|
| 127 |
|
| 128 |
$items['node/%node/topichub'] = array(
|
| 129 |
'title' => 'Topic Hub Configuration',
|
| 130 |
'page callback' => 'drupal_get_form',
|
| 131 |
'page arguments' => array('topichubs_config_form', 1),
|
| 132 |
'access callback' => 'topichubs_access',
|
| 133 |
'access arguments' => array(1),
|
| 134 |
'weight' => 2,
|
| 135 |
'type' => MENU_LOCAL_TASK
|
| 136 |
);
|
| 137 |
|
| 138 |
$items_render = topichubs_render_invoke('menu');
|
| 139 |
|
| 140 |
if(is_array($items_render)) {
|
| 141 |
$items = array_merge($items, $items_render);
|
| 142 |
}
|
| 143 |
|
| 144 |
return $items;
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Implementation of hook_access().
|
| 149 |
*/
|
| 150 |
function topichubs_access($node) {
|
| 151 |
return user_access('administer topichubs') && $node->type == 'topichub';
|
| 152 |
}
|
| 153 |
|
| 154 |
/**
|
| 155 |
* Implementation of hook_theme().
|
| 156 |
*/
|
| 157 |
function topichubs_theme() {
|
| 158 |
$themes = array();
|
| 159 |
|
| 160 |
$themes['topichubs_config_form'] = array(
|
| 161 |
'arguments' => array('form' => array()),
|
| 162 |
);
|
| 163 |
|
| 164 |
$plugins = topichubs_discover_plugins();
|
| 165 |
foreach ($plugins as $type => $definition) {
|
| 166 |
$themes["topichubs_{$type}"] = array(
|
| 167 |
'pattern' => "topichubs_{$type}__",
|
| 168 |
'arguments' => array('topichub' => NULL, 'items' => array()),
|
| 169 |
'path' => $definition['theme path'],
|
| 170 |
'template' => "topichubs-{$type}",
|
| 171 |
);
|
| 172 |
}
|
| 173 |
return $themes;
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Implementation of hook_theme_registry_alter().
|
| 178 |
*
|
| 179 |
* This is so that it will look for the node-topichub.tpl.php file here too.
|
| 180 |
*/
|
| 181 |
function topichubs_theme_registry_alter(&$theme_registry) {
|
| 182 |
$modulepath = drupal_get_path('module', 'topichubs');
|
| 183 |
array_unshift($theme_registry['node']['theme paths'], $modulepath);
|
| 184 |
}
|
| 185 |
|
| 186 |
/**
|
| 187 |
* Implementation of hook_preprocess_node().
|
| 188 |
*
|
| 189 |
* This is so that it will look for the node-topichub-[nid].tpl.php
|
| 190 |
*/
|
| 191 |
function topichubs_preprocess_node(&$variables) {
|
| 192 |
$node = $variables['node'];
|
| 193 |
if($node->type == 'topichub') {
|
| 194 |
$variables['template_files'][] = 'node-' . $node->type . '-' . $node->nid;
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* Implementation of hook_nodeapi().
|
| 200 |
*/
|
| 201 |
function topichubs_nodeapi(&$node, $op, $param3 = NULL, $param4 = NULL) {
|
| 202 |
if ($node->type == 'topichub') {
|
| 203 |
switch ($op) {
|
| 204 |
case 'prepare':
|
| 205 |
topichubs_render_invoke('load', $node);
|
| 206 |
// covers add coming from hot topic page.
|
| 207 |
if (arg(1) == 'add' && isset($_GET['tid'])) {
|
| 208 |
$tid = $_GET['tid'];
|
| 209 |
$term = taxonomy_get_term($tid);
|
| 210 |
$node->title = "{$term->name} Topic Hub";
|
| 211 |
$node->topichub_tid = $tid;
|
| 212 |
}
|
| 213 |
break;
|
| 214 |
case 'load':
|
| 215 |
topichub_load($node);
|
| 216 |
topichubs_render_invoke('load', $node);
|
| 217 |
break;
|
| 218 |
case 'validate':
|
| 219 |
topichubs_render_invoke('validate', $node);
|
| 220 |
break;
|
| 221 |
case 'insert':
|
| 222 |
if(property_exists($node, 'topichub_tid')) {
|
| 223 |
$node->topichub->conditions = array(array($node->topichub_tid));
|
| 224 |
topichub_insert($node);
|
| 225 |
}
|
| 226 |
// Since calling topichub_insert is conditional, we have to call this outside that function
|
| 227 |
// This does a redirect too, so cal it last
|
| 228 |
topichubs_render_invoke('insert', $node);
|
| 229 |
break;
|
| 230 |
case 'update':
|
| 231 |
topichubs_render_invoke('update', $node);
|
| 232 |
break;
|
| 233 |
case 'delete':
|
| 234 |
topichubs_render_invoke('delete', $node);
|
| 235 |
topichub_delete($node->nid);
|
| 236 |
break;
|
| 237 |
case 'view':
|
| 238 |
if(topichub_is_initialized($node)) {
|
| 239 |
$res = topichubs_render_invoke('view', $node);
|
| 240 |
if ($res == '#!#TH_RENDER_NO_IMPL#!#') { //default, TPL_based implementationvai
|
| 241 |
$path = drupal_get_path('module', 'topichubs');
|
| 242 |
drupal_add_css("$path/topichubs.css");
|
| 243 |
topichub_view($node);
|
| 244 |
}
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
drupal_set_message(t('Topic Hub not initialized. Please !config.', array('!config' => l('configure an expression', "node/{$node->nid}/topichub"))));
|
| 248 |
}
|
| 249 |
break;
|
| 250 |
}
|
| 251 |
|
| 252 |
}
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Implementation of hook_taxonomy().
|
| 257 |
*
|
| 258 |
* Make sure deleted terms are removed from existing expressions
|
| 259 |
*/
|
| 260 |
function topichubs_taxonomy($op, $type, $data) {
|
| 261 |
if($op == 'delete' && $type == 'term') {
|
| 262 |
topichub_term_delete($data['tid']);
|
| 263 |
}
|
| 264 |
}
|
| 265 |
|
| 266 |
/**
|
| 267 |
* Implementation of hook_form_alter().
|
| 268 |
*
|
| 269 |
* If we are creating a Topic Hub form a Taxonomy Term, set the value for save processing
|
| 270 |
*/
|
| 271 |
function topichubs_form_alter(&$form, $form_state, $form_id) {
|
| 272 |
if($form_id == 'topichub_node_form'){
|
| 273 |
$node = $form['#node'];
|
| 274 |
if($node->topichub_tid) {
|
| 275 |
$form['topichub_tid'] = array(
|
| 276 |
'#type' => 'hidden',
|
| 277 |
'#value' => $node->topichub_tid,
|
| 278 |
);
|
| 279 |
}
|
| 280 |
|
| 281 |
topichubs_render_invoke('form', $node, $form);
|
| 282 |
}
|
| 283 |
}
|
| 284 |
|
| 285 |
/**
|
| 286 |
* Determine if a topichub has any conditions setup.
|
| 287 |
*/
|
| 288 |
function topichub_is_initialized($node) {
|
| 289 |
if(is_numeric($node)) {
|
| 290 |
$node = node_load($node);
|
| 291 |
}
|
| 292 |
else if (is_object($node) && is_numeric($node->nid)) {
|
| 293 |
return !empty($node->topichub->conditions);
|
| 294 |
}
|
| 295 |
|
| 296 |
return FALSE;
|
| 297 |
}
|
| 298 |
|
| 299 |
|
| 300 |
/**
|
| 301 |
* Lookup topichub data from the DB
|
| 302 |
*
|
| 303 |
* @param $node
|
| 304 |
* The node to load with configuration. It will populate an object property of <em>topichub</em>
|
| 305 |
* with the taxonomy expression and plugin configuration.
|
| 306 |
*/
|
| 307 |
function topichub_load(&$node) {
|
| 308 |
$result = db_query('SELECT * FROM {topichub} WHERE nid = %d', $node->nid);
|
| 309 |
if ($topichub = db_fetch_object($result)) {
|
| 310 |
$node->topichub = $topichub;
|
| 311 |
$node->topichub->config = unserialize($topichub->config);
|
| 312 |
$node->topichub->conditions = topichub_load_conditions($node->nid);
|
| 313 |
}
|
| 314 |
}
|
| 315 |
|
| 316 |
/**
|
| 317 |
* Lookup topichub condition from the DB
|
| 318 |
*
|
| 319 |
* @param $nid
|
| 320 |
* The node id to load the conditions.
|
| 321 |
*/
|
| 322 |
function topichub_load_conditions($nid) {
|
| 323 |
$result = db_query('SELECT * FROM {topichub_condition} WHERE nid = %d ORDER BY `condition` ASC, tcid ASC', $nid);
|
| 324 |
$conditions = array();
|
| 325 |
while($cond = db_fetch_object($result)) {
|
| 326 |
$conditions[$cond->condition][] = $cond->tid;
|
| 327 |
}
|
| 328 |
return $conditions;
|
| 329 |
}
|
| 330 |
|
| 331 |
/**
|
| 332 |
* Load all topichub data from providers.
|
| 333 |
*
|
| 334 |
* @param $node
|
| 335 |
* The node to load with data. It should have an object property of <em>topichub</em> which
|
| 336 |
* will get populated with a <em>data</em> array.
|
| 337 |
*/
|
| 338 |
function topichub_view(&$node) {
|
| 339 |
$plugins = topichubs_discover_plugins();
|
| 340 |
foreach ($plugins as $type => $def) {
|
| 341 |
$impl = _topichubs_new_handler_class($def);
|
| 342 |
if($impl) {
|
| 343 |
$impl->init($type, $def, $node, $node->topichub->config[$type]);
|
| 344 |
$node->topichub->data[$type] = $impl->execute();
|
| 345 |
}
|
| 346 |
}
|
| 347 |
|
| 348 |
return $node;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Insert topichub data.
|
| 353 |
*
|
| 354 |
* @param $node
|
| 355 |
* The node to persist. It should have an object property of <em>topichub</em>
|
| 356 |
*/
|
| 357 |
function topichub_insert($node) {
|
| 358 |
db_query("INSERT INTO {topichub} (nid, config) VALUES (%d, %b)", $node->nid, serialize($node->topichub->config));
|
| 359 |
topichub_insert_conditions($node->nid, $node->topichub->conditions);
|
| 360 |
}
|
| 361 |
|
| 362 |
/**
|
| 363 |
* Update topichub data.
|
| 364 |
*
|
| 365 |
* @param $node
|
| 366 |
* The node to persist. It should have an object property of <em>topichub</em>
|
| 367 |
*/
|
| 368 |
function topichub_update($node) {
|
| 369 |
db_query("UPDATE {topichub} SET config = %b WHERE nid = %d", serialize($node->topichub->config), $node->nid);
|
| 370 |
topichub_insert_conditions($node->nid, $node->topichub->conditions);
|
| 371 |
}
|
| 372 |
|
| 373 |
/**
|
| 374 |
* Insert condition data for the topichub expression.
|
| 375 |
*
|
| 376 |
* @param $nid
|
| 377 |
* Node ID
|
| 378 |
* @param $conditions
|
| 379 |
* Array of conditions and term IDs for each condition
|
| 380 |
*/
|
| 381 |
function topichub_insert_conditions($nid, $conditions) {
|
| 382 |
db_query("DELETE FROM {topichub_condition} WHERE nid = %d", $nid);
|
| 383 |
foreach($conditions as $key => $tids) {
|
| 384 |
foreach($tids as $tid) {
|
| 385 |
db_query("INSERT INTO {topichub_condition} (nid, `condition`, tid) VALUES (%d, %d, %d)", $nid, $key, $tid);
|
| 386 |
}
|
| 387 |
}
|
| 388 |
}
|
| 389 |
|
| 390 |
/**
|
| 391 |
* Remove topichub data.
|
| 392 |
*
|
| 393 |
* @param $nid
|
| 394 |
* The node to delete
|
| 395 |
*/
|
| 396 |
function topichub_delete($nid) {
|
| 397 |
db_query("DELETE FROM {topichub} WHERE nid = %d", $nid);
|
| 398 |
db_query("DELETE FROM {topichub_condition} WHERE nid = %d", $nid);
|
| 399 |
}
|
| 400 |
|
| 401 |
/**
|
| 402 |
* Topichub remove term from conditions. We also need to load the conditions and reorder them
|
| 403 |
* b/c the first condition might have only 1 term and if we remove it conditions woudl start ordering @ 1.
|
| 404 |
*
|
| 405 |
* @param $nid
|
| 406 |
* The node to delete
|
| 407 |
*/
|
| 408 |
function topichub_term_delete($tid) {
|
| 409 |
$result = db_query("SELECT DISTINCT(nid) FROM {topichub_condition} WHERE tid = %d", $tid);
|
| 410 |
db_query("DELETE FROM {topichub_condition} WHERE tid = %d", $tid);
|
| 411 |
while($cond = db_fetch_object($result)) {
|
| 412 |
$conditions = topichub_load_conditions($cond->nid);
|
| 413 |
$conditions = array_merge($conditions);
|
| 414 |
topichub_insert_conditions($cond->nid, $conditions);
|
| 415 |
}
|
| 416 |
}
|
| 417 |
|
| 418 |
/**
|
| 419 |
* Form for configuring a Topic Hub.
|
| 420 |
* This form allows for the building of a taxonomy
|
| 421 |
* expression (in conjunction with topichubs.js). It also provides the configuration
|
| 422 |
* options for each of the enabled Topic Hub plugins.
|
| 423 |
*/
|
| 424 |
function topichubs_config_form(&$form_state, $node) {
|
| 425 |
$path = drupal_get_path('module', 'topichubs');
|
| 426 |
drupal_add_css("$path/topichubs-admin.css");
|
| 427 |
drupal_add_js("$path/topichubs.js");
|
| 428 |
|
| 429 |
// Add image as an array so the JS can always use [0]. Somethign wierd was happening where
|
| 430 |
// the multistep form was setting this value twice and causign the JS to barf when adding
|
| 431 |
// a term to the UI. This way it is always an array and even if there are many, [0] works.
|
| 432 |
drupal_add_js(array(
|
| 433 |
'topichubs' => array(
|
| 434 |
'delete_img' => array(theme('image', "$path/images/delete.gif")),
|
| 435 |
)
|
| 436 |
), 'setting');
|
| 437 |
|
| 438 |
topichub_load($node);
|
| 439 |
|
| 440 |
$form = array();
|
| 441 |
$form['#node'] = $node;
|
| 442 |
$form['nid'] = array(
|
| 443 |
'#type' => 'hidden',
|
| 444 |
'#value' => $node->nid,
|
| 445 |
);
|
| 446 |
|
| 447 |
_topichub_plugin_expression_form($form, $form_state, $node);
|
| 448 |
_topichub_plugin_config_form($form, $form_state, $node);
|
| 449 |
|
| 450 |
$form['#tree'] = TRUE;
|
| 451 |
$form['submit'] = array(
|
| 452 |
'#type' => 'submit',
|
| 453 |
'#value' => t('Save'),
|
| 454 |
);
|
| 455 |
|
| 456 |
return $form;
|
| 457 |
}
|
| 458 |
|
| 459 |
/**
|
| 460 |
* Render the Topic Hub Expression Builder form.
|
| 461 |
*/
|
| 462 |
function _topichub_plugin_expression_form(&$form, &$form_state, $node) {
|
| 463 |
$conditions = $form_state['storage']['conditions'];
|
| 464 |
if(!isset($conditions)) {
|
| 465 |
$conditions = array();
|
| 466 |
if(is_array($node->topichub->conditions)) {
|
| 467 |
foreach($node->topichub->conditions as $cond => $tids) {
|
| 468 |
$conditions[$cond] = implode(',', $tids);
|
| 469 |
}
|
| 470 |
}
|
| 471 |
}
|
| 472 |
|
| 473 |
$form['expression']['help'] = array(
|
| 474 |
'#type' => 'markup',
|
| 475 |
'#value' => t('Content meeting any of the following conditions will be included in this Topic Hub. <b>Adding multiple terms</b> will make a specific condition more stringent. <b>Adding conditions</b> will broaden the context of your Topic Hub by increasing the likelyhood of matching. Be careful, large expressions can have a significant performance penalty.'),
|
| 476 |
);
|
| 477 |
|
| 478 |
$form['expression']['buttons']['add_condition'] = array(
|
| 479 |
'#type' => 'submit',
|
| 480 |
'#value' => t('New Condition'),
|
| 481 |
'#submit' => array('topichubs_config_form_add_condition'),
|
| 482 |
'#skip_validate' => TRUE,
|
| 483 |
);
|
| 484 |
|
| 485 |
$vocab_options = array('' => '<select>');
|
| 486 |
$options = array();
|
| 487 |
$vocabs = taxonomy_get_vocabularies();
|
| 488 |
foreach ($vocabs as $vocab) {
|
| 489 |
$tree = taxonomy_get_tree($vocab->vid);
|
| 490 |
if ($tree) {
|
| 491 |
$vocab_options[$vocab->vid] = $vocab->name;
|
| 492 |
foreach ($tree as $term) {
|
| 493 |
$options[$vocab->vid][$term->tid] = $term->name;
|
| 494 |
}
|
| 495 |
}
|
| 496 |
}
|
| 497 |
|
| 498 |
$form['expression']['taxonomy'] = array(
|
| 499 |
'#type' => 'fieldset',
|
| 500 |
'#title' => t('Add a Term to an existing Condition'),
|
| 501 |
'#collapsible' => TRUE,
|
| 502 |
'#collapsed' => FALSE,
|
| 503 |
);
|
| 504 |
|
| 505 |
$form['expression']['taxonomy']['pre'] = array(
|
| 506 |
'#value' => '<div id="condition-builder" class="clear-block">',
|
| 507 |
);
|
| 508 |
|
| 509 |
$form['expression']['taxonomy']['vocab'] = array(
|
| 510 |
'#type' => 'select',
|
| 511 |
'#title' => t('Choose a Vocabulary'),
|
| 512 |
'#value' => '',
|
| 513 |
'#options' => $vocab_options,
|
| 514 |
'#attributes' => array('id' => 'vocabulary-selector'),
|
| 515 |
);
|
| 516 |
|
| 517 |
foreach($options as $vid => $terms) {
|
| 518 |
$form['expression']['taxonomy']['term'][$vid] = array(
|
| 519 |
'#type' => 'select',
|
| 520 |
'#title' => t('@vocab Terms', array('@vocab' => $vocab_options[$vid])),
|
| 521 |
'#options' => $terms,
|
| 522 |
'#attributes' => array('id' => "term-{$vid}-selector"),
|
| 523 |
'#prefix' => "<div id='terms-{$vid}-selector-wrapper' class='topichub-term-selector'>",
|
| 524 |
'#suffix' => '</div>',
|
| 525 |
);
|
| 526 |
}
|
| 527 |
|
| 528 |
$condition_options = array('' => '<select>');
|
| 529 |
$conditionKeys = array_keys($conditions);
|
| 530 |
foreach ($conditionKeys as $key) {
|
| 531 |
$condition_options[$key] = t("Condition @num", array('@num' => ($key+1)));
|
| 532 |
}
|
| 533 |
$form['expression']['taxonomy']['condition'] = array(
|
| 534 |
'#type' => 'select',
|
| 535 |
'#title' => t('Condition'),
|
| 536 |
'#value' => '',
|
| 537 |
'#attributes' => array('id' => "condition-selector"),
|
| 538 |
'#options' => $condition_options,
|
| 539 |
);
|
| 540 |
$form['expression']['taxonomy']['add'] = array(
|
| 541 |
'#value' => '<a id="topichub-add-term" href="javascript:void(0);">' . t('Add Term') . '</a>',
|
| 542 |
'#prefix' => "<div id='add-term'>",
|
| 543 |
'#suffix' => '</div>',
|
| 544 |
);
|
| 545 |
|
| 546 |
$form['expression']['taxonomy']['post'] = array(
|
| 547 |
'#value' => '</div>',
|
| 548 |
);
|
| 549 |
|
| 550 |
// Hide the condition builder if there are no conditions
|
| 551 |
if(count($conditions) == 0) {
|
| 552 |
$form['expression']['taxonomy']['#attributes'] = array('style' => "display:none;");
|
| 553 |
}
|
| 554 |
|
| 555 |
foreach ($conditions as $conditionKey => $tids) {
|
| 556 |
$form['expression']['condition'][$conditionKey] = array(
|
| 557 |
'#type' => 'fieldset',
|
| 558 |
'#title' => t("Condition @num", array('@num' => ($conditionKey + 1))),
|
| 559 |
'#collapsible' => FALSE,
|
| 560 |
'#collapsed' => FALSE,
|
| 561 |
'#tree' => TRUE,
|
| 562 |
'#attributes' => array('id' => "condition-wrapper-{$conditionKey}"),
|
| 563 |
'#prefix' => '<div class="clear-block">',
|
| 564 |
'#suffix' => '</div>',
|
| 565 |
);
|
| 566 |
if($conditionKey + 1 < count($conditions)) {
|
| 567 |
$form['expression']['condition'][$conditionKey]['#suffix'] .= '<div class="operator">' . t('OR') . '</div>';
|
| 568 |
}
|
| 569 |
$form['expression']['condition'][$conditionKey]['remove'] = array(
|
| 570 |
'#type' => 'submit',
|
| 571 |
'#value' => t('Remove Condition @num', array('@num' => ($conditionKey + 1))),
|
| 572 |
'#submit' => array('topichubs_config_form_remove_condition'),
|
| 573 |
'#prefix' => '<div class="remove-condition">',
|
| 574 |
'#suffix' => '</div>',
|
| 575 |
'#skip_validate' => TRUE,
|
| 576 |
'#condition' => $conditionKey,
|
| 577 |
);
|
| 578 |
$path = drupal_get_path('module', 'topichubs');
|
| 579 |
$delete_img = theme('image', "$path/images/delete.gif");
|
| 580 |
$tids_array = empty($tids) ? array() : explode(',', $tids);
|
| 581 |
foreach($tids_array as $index => $tid) {
|
| 582 |
$term = taxonomy_get_term($tid);
|
| 583 |
$vocab = taxonomy_vocabulary_load($term->vid);
|
| 584 |
$operator = ($index > 0) ? '<span class="operator">' . t('AND') . '</span>' : '';
|
| 585 |
$form['expression']['condition'][$conditionKey][] = array(
|
| 586 |
'#value' => "<span class='vocab-name'>" . $vocab->name . "</span>: <span class='term-name'>" . $term->name . '</span>',
|
| 587 |
'#prefix' => '<span class="condition-term">' . $operator,
|
| 588 |
'#suffix' => "<a class='topichub-term-remove' href='javascript:void(0);' title='Remove' tid='{$tid}' condition='{$conditionKey}'>{$delete_img}</a></span>",
|
| 589 |
);
|
| 590 |
}
|
| 591 |
// FAPI does not allow for overriding ID of hidden via #attributes
|
| 592 |
$form['expression']['conditions'][$conditionKey] = array(
|
| 593 |
'#type' => 'hidden',
|
| 594 |
'#default_value' => $tids,
|
| 595 |
'#id' => "conditions-{$conditionKey}",
|
| 596 |
);
|
| 597 |
}
|
| 598 |
|
| 599 |
}
|
| 600 |
|
| 601 |
/**
|
| 602 |
* Render the Topic Hub Plugin configuration form.
|
| 603 |
*/
|
| 604 |
function _topichub_plugin_config_form(&$form, &$form_state, $node) {
|
| 605 |
|
| 606 |
$config = $form_state['storage']['config'];
|
| 607 |
if(!isset($config)) {
|
| 608 |
$config = $node->topichub->config;
|
| 609 |
}
|
| 610 |
|
| 611 |
$plugins = topichubs_discover_plugins();
|
| 612 |
foreach ($plugins as $type => $def) {
|
| 613 |
$impl = _topichubs_new_handler_class($def);
|
| 614 |
if($impl) {
|
| 615 |
$impl->init($type, $def, $node, $config[$type]);
|
| 616 |
$impl->build_form('options', $form['config'], $form_state);
|
| 617 |
}
|
| 618 |
}
|
| 619 |
}
|
| 620 |
|
| 621 |
/**
|
| 622 |
* Validate handler for the Topic Hub configuration.
|
| 623 |
*/
|
| 624 |
function topichubs_config_form_validate($form, &$form_state) {
|
| 625 |
|
| 626 |
if(!$form_state['clicked_button']['#skip_validate']) {
|
| 627 |
$node = $form['#node'];
|
| 628 |
|
| 629 |
$conditions = $form_state['values']['expression']['conditions'];
|
| 630 |
if(isset($conditions)) {
|
| 631 |
foreach($conditions as $key => $terms) {
|
| 632 |
if(empty($terms)) {
|
| 633 |
form_set_error("condition-$key", t('Condition @num cannot be empty.', array('@num' => ($key + 1))));
|
| 634 |
}
|
| 635 |
}
|
| 636 |
}
|
| 637 |
|
| 638 |
$config = $form_state['values']['config'];
|
| 639 |
$plugins = topichubs_discover_plugins();
|
| 640 |
foreach ($plugins as $type => $def) {
|
| 641 |
$impl = _topichubs_new_handler_class($def);
|
| 642 |
if($impl) {
|
| 643 |
$impl->init($type, $def, $node, $config[$type]);
|
| 644 |
$impl->options_validate($node, $form);
|
| 645 |
}
|
| 646 |
}
|
| 647 |
}
|
| 648 |
}
|
| 649 |
|
| 650 |
|
| 651 |
/**
|
| 652 |
* Submit handler for the Topic Hub configuration.
|
| 653 |
*/
|
| 654 |
function topichubs_config_form_submit($form, &$form_state) {
|
| 655 |
$values = $form_state['values'];
|
| 656 |
|
| 657 |
$node = new stdClass;
|
| 658 |
$node->nid = $values['nid'];
|
| 659 |
topichub_load($node);
|
| 660 |
|
| 661 |
$save_function = 'topichub_update';
|
| 662 |
if(!$node->topichub) {
|
| 663 |
$node->topichub = new stdClass;
|
| 664 |
$save_function = 'topichub_insert';
|
| 665 |
}
|
| 666 |
|
| 667 |
$conditions = $values['expression']['conditions'];
|
| 668 |
foreach($conditions as $key => $tids) {
|
| 669 |
$conditions[$key] = empty($tids) ? array() : explode(',', $tids);
|
| 670 |
}
|
| 671 |
|
| 672 |
$node->topichub->config = $values['config'];
|
| 673 |
$node->topichub->conditions = $conditions;
|
| 674 |
$save_function($node);
|
| 675 |
|
| 676 |
unset($form_state['storage']); // Otherwise redirect won't work, it will just rebuild
|
| 677 |
$form_state['redirect'] = "node/{$node->nid}";
|
| 678 |
}
|
| 679 |
|
| 680 |
/**
|
| 681 |
* Handle the Add Condition button. Adds a new condition and saves them away for form rebuild
|
| 682 |
*/
|
| 683 |
function topichubs_config_form_add_condition($form, &$form_state) {
|
| 684 |
|
| 685 |
$conditions = $form_state['values']['expression']['conditions'];
|
| 686 |
if(!isset($conditions)) {
|
| 687 |
$conditions = array();
|
| 688 |
}
|
| 689 |
array_push($conditions, '');
|
| 690 |
$form_state['storage']['conditions'] = $conditions;
|
| 691 |
$form_state['storage']['config'] = $form_state['values']['config'];
|
| 692 |
$form_state['rebuild'] = TRUE;
|
| 693 |
}
|
| 694 |
|
| 695 |
/**
|
| 696 |
* Handle the Remove Condition button. Removes and existing condition and saves them away for form rebuild
|
| 697 |
*/
|
| 698 |
function topichubs_config_form_remove_condition($form, &$form_state) {
|
| 699 |
$removeKey = $form_state['clicked_button']['#condition'];
|
| 700 |
$conditions = $form_state['values']['expression']['conditions'];
|
| 701 |
if(isset($conditions)) {
|
| 702 |
unset($conditions[$removeKey]);
|
| 703 |
$conditions = array_merge($conditions); //reorder keys
|
| 704 |
}
|
| 705 |
$form_state['storage']['conditions'] = $conditions;
|
| 706 |
$form_state['storage']['config'] = $form_state['values']['config'];
|
| 707 |
$form_state['rebuild'] = TRUE;
|
| 708 |
}
|
| 709 |
|
| 710 |
/**
|
| 711 |
* Lookup installed version of panels
|
| 712 |
* @return int
|
| 713 |
* version
|
| 714 |
*/
|
| 715 |
function _get_panels_version() {
|
| 716 |
return db_result(db_query("SELECT schema_version from {system} where name = 'panels'"));
|
| 717 |
}
|
| 718 |
|
| 719 |
|
| 720 |
/**
|
| 721 |
* Theme the Topic Hub Config form.
|
| 722 |
*/
|
| 723 |
function theme_topichubs_config_form($form) {
|
| 724 |
$output = '<div id="topichub-config">';
|
| 725 |
$output .= '<h3>' . t('Topic Hub Expression') . '</h3>';
|
| 726 |
$output .= drupal_render($form['expression']['help']);
|
| 727 |
$output .= "<div>";
|
| 728 |
$output .= drupal_render($form['expression']['buttons']);
|
| 729 |
$output .= "</div>";
|
| 730 |
$output .= drupal_render($form['expression']['taxonomy']);
|
| 731 |
$output .= drupal_render($form['expression']['condition']);
|
| 732 |
$output .= '<h3>' . t('Plugin Configuration') . '</h3>';
|
| 733 |
$config = &$form['config'];
|
| 734 |
foreach(element_children($config) as $index => $type) {
|
| 735 |
$labels[] .= "<div id='plugin-{$type}' class='plugin-label'>" . $config[$type]['#title'] . '</div>';
|
| 736 |
$settings[] .= "<div id='plugin-{$type}-form' class='plugin-settings'>" . drupal_render($config[$type]) . '</div>';
|
| 737 |
}
|
| 738 |
$labels[] .= '<div class="plugin-label-bottom"> </div>';
|
| 739 |
|
| 740 |
$header = array(
|
| 741 |
t('Plugin'),
|
| 742 |
t('Configuration'),
|
| 743 |
);
|
| 744 |
$rows = array(
|
| 745 |
array(
|
| 746 |
array(
|
| 747 |
'data' => implode($labels),
|
| 748 |
'class' => 'topichub-plugin-labels'
|
| 749 |
),
|
| 750 |
array(
|
| 751 |
'data' => implode($settings),
|
| 752 |
'class' => 'topichub-plugin-settings'
|
| 753 |
),
|
| 754 |
),
|
| 755 |
);
|
| 756 |
$output .= theme('table', $header, $rows, array('class' => 'topichub-plugin-config'));
|
| 757 |
|
| 758 |
$output .= drupal_render($form);
|
| 759 |
$output .= '</div>';
|
| 760 |
return $output;
|
| 761 |
}
|