View source
<?php
function context_node_perm() {
return array(
'set context on nodes',
);
}
function context_node_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form') {
$form['context'] = array(
'#type' => 'fieldset',
'#title' => t("Allowed node contexts"),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 10,
);
$form['context']['context_node'] = array(
'#type' => 'checkboxes',
'#title' => t('Select allowed contents'),
'#options' => _context_node_get_contexts(),
'#description' => t('Select all contexts that will be available for this content type'),
);
if (variable_get('context_node_' . $form['#node_type']->type, '') != NULL) {
$form['context']['context_node']['#default_value'] = variable_get('context_node_' . $form['#node_type']->type, '');
}
$default = variable_get('context_node_default_' . $form['#node_type']->type, FALSE);
$form['context']['context_node_default'] = array(
'#type' => 'radios',
'#title' => t('Select the default context'),
'#default_value' => isset($default) ? $default : "none",
'#options' => _context_node_get_default_contexts(),
'#description' => t('Select the default context. If you select "Disabled" this functionality will be disabled for this content type. If you select "Default" the functionality will be enabled but no context will be enabled by default'),
);
}
if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id) {
$node = $form['#node'];
$option = variable_get("context_node_default_" . $node->type, '');
if ($option == "none") {
return;
}
if (!empty($option)) {
$form['context_node'] = array(
'#type' => 'fieldset',
'#title' => t('Context'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('set context on nodes'),
'#weight' => 130,
);
$options = _context_node_get_contexts_node_type($node->type);
$default = variable_get("context_node_default_" . $node->type, '');
$form['context_node']['context'] = array(
'#type' => 'radios',
'#title' => t('Context'),
'#description' => t('Select a context from the list to change the layout and configuration of this !type', array(
'!type' => $node->type,
)),
'#default_value' => isset($node->context) ? $node->context : $default,
'#options' => $options,
'#access' => user_access('set context on nodes'),
'#submit' => array(
'context_node_form_submit',
),
);
}
}
}
function context_node_form_submit($form, &$form_state) {
$form_state['node']->context = $form_state['values']['context'];
$form_state['rebuild'] = TRUE;
}
function _context_node_get_contexts() {
$contexts = context_enabled_contexts();
ksort($contexts);
$con = array();
foreach ($contexts as $context) {
$con[$context->name] = $context->name;
$cons[] = $con;
}
return $con;
}
function _context_node_get_default_contexts() {
$contexts = context_enabled_contexts();
ksort($contexts);
$con = array();
$con["none"] = "Disabled";
$con["default"] = "Default";
foreach ($contexts as $context) {
$con[$context->name] = $context->name;
$cons[] = $con;
}
return $con;
}
function _context_node_get_contexts_node_type($type) {
$contexts = variable_get("context_node_" . $type, '');
$con = array();
$con["default"] = "Default";
foreach ($contexts as $context) {
$con[$context] = $context;
$cons[] = $con;
}
return $con;
}
function context_node_nodeapi(&$node, $op, $teaser, $page) {
$option = variable_get("context_node_default_" . $node->type, '');
if ($option == "none" || empty($option)) {
return;
}
switch ($op) {
case 'load':
$node->context = db_result(db_query('SELECT context FROM {context_node} WHERE vid = %d', $node->vid));
return $node->context;
break;
case 'insert':
db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
break;
case 'update':
if ($node->revision) {
db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
}
elseif (_context_node_check_for_context($node)) {
db_query("UPDATE {context_node} SET context = '%s' WHERE vid = %d", $node->context, $node->vid);
}
else {
db_query("INSERT INTO {context_node} (nid, vid, context) VALUES (%d, %d, '%s')", $node->nid, $node->vid, $node->context);
}
break;
case 'delete':
db_query('DELETE FROM {context_node} WHERE nid = %d', $node->nid);
break;
case 'delete revision':
db_query('DELETE FROM {node_example} WHERE vid = %d', $node->vid);
break;
case 'view':
$name = $node->context;
if ($name == "none" || empty($name) || $name == "default") {
return;
}
$context = context_load($name);
context_set('context', "context_node", $context);
break;
}
}
function _context_node_check_for_context($node) {
$result = db_result(db_query("SELECT nid FROM {context_node} WHERE nid = %d", $node->nid));
if ($result) {
return TRUE;
}
else {
return FALSE;
}
}
function context_node_content_extra_fields($type_name) {
$fields['context_node'] = array(
'label' => t('Context Node'),
'description' => t('Select a context'),
'weight' => 0,
);
return $fields;
}