View source
<?php
function context_node_permission() {
return array(
'set context on nodes' => array(
'title' => t('Set context on nodes'),
'description' => t('Allow the user to set a context on each node.'),
),
);
}
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,
'#group' => 'additional_settings',
'#weight' => 50,
);
$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'),
);
}
}
function context_node_form_node_form_alter(&$form, &$form_state, $form_id) {
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'),
'#group' => 'additional_settings',
'#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_type($type) {
$contexts = variable_get("context_node_" . $type);
$con = array();
foreach ($contexts as $context) {
$con[$context] = $context;
$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_node_load($nodes, $types) {
foreach ($nodes as $node) {
$vids[] = $node->vid;
}
$result = db_select('context_node', 'c')
->fields('c', array(
'nid',
'vid',
'context',
))
->where('c.vid IN (:vids)', array(
':vids' => $vids,
))
->execute();
foreach ($result as $record) {
$nodes[$record->nid]->context = $record->context;
}
}
function context_node_node_insert($node) {
$option = variable_get("context_node_default_" . $node->type, '');
if ($option == "none" || empty($option)) {
return;
}
else {
if (empty($node->context)) {
$node->context = $option;
}
db_insert('context_node')
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'context' => $node->context,
))
->execute();
}
}
function context_node_node_update($node) {
$option = variable_get("context_node_default_" . $node->type, '');
if ($option == "none" || empty($option)) {
return;
}
else {
if (empty($node->context)) {
$node->context = $option;
}
if (!empty($node->revision)) {
db_insert('context_node')
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'context' => $node->context,
))
->execute();
}
elseif (_context_node_check_for_context($node)) {
db_update('context_node')
->fields(array(
'context' => $node->context,
))
->condition('vid', $node->vid)
->execute();
}
else {
db_insert('context_node')
->fields(array(
'nid' => $node->nid,
'vid' => $node->vid,
'context' => $node->context,
))
->execute();
}
}
}
function context_node_node_delete($node) {
db_delete('context_node')
->condition('nid', $node->nid)
->execute();
}
function context_node_node_view($node, $view_mode, $langcode) {
$option = variable_get("context_node_default_" . $node->type, '');
if ($option == "none" || empty($option)) {
return;
}
else {
if (isset($node->context)) {
if ($node->context == "none" || empty($node->context) || $node->context == "default") {
return;
}
$context = context_load($node->context);
context_set('context', "context_node", $context);
}
}
}
function _context_node_check_for_context($node) {
$result = db_query("SELECT nid FROM {context_node} WHERE nid = :nid", array(
':nid' => $node->nid,
))
->fetchObject();
if ($result) {
return TRUE;
}
else {
return FALSE;
}
}