function _prev_next_add in Previous/Next API 7.2
Same name and namespace in other branches
- 6 prev_next.module \_prev_next_add()
- 7 prev_next.module \_prev_next_add()
Create or update the prev_next records.
Parameters
string $nid: The current node id.
4 calls to _prev_next_add()
- prev_next_cron in ./
prev_next.module - Implements hook_cron().
- prev_next_node_insert in ./
prev_next.module - Implements hook_node_insert().
- _prev_next_modify in ./
prev_next.module - Module function to update the prev_next records.
- _prev_next_modify_pointing_nodes in ./
prev_next.module - Updates other nodes pointing to a particular node.
File
- ./
prev_next.module, line 118 - The previous next module indexes the previous and next nodes based upon user-selectable criteria and stores this index in the database for faster retrieval later.
Code
function _prev_next_add($nid) {
$node_type = db_query_range("SELECT type\n FROM {node}\n WHERE nid = :nid", 0, 1, array(
':nid' => $nid,
))
->fetchField();
$search_criteria = variable_get(PREV_NEXT_NODE_TYPE . $node_type . '_indexing_criteria', PREV_NEXT_INDEXING_CRITERIA_DEFAULT);
$join = _prev_next_category_sql($node_type);
if (strlen($join) > 0) {
// search category for current node
$category = variable_get(PREV_NEXT_NODE_TYPE . $node_type . '_category_criteria', 'none');
$category_id = db_query("SELECT " . $category . "_tid FROM {field_data_" . $category . "} WHERE entity_id = :nid", array(
':nid' => $nid,
))
->fetchField();
if (empty($category_id)) {
$cond = '';
}
else {
$cond = " AND " . $category . "_tid = " . $category_id . " ";
$cond .= _prev_next_node_types_sql($node_type);
}
}
else {
$cond = _prev_next_node_types_sql($node_type);
}
if ($search_criteria != 'nid') {
$criteria_value = db_query_range("SELECT {$search_criteria}\n FROM {node}\n WHERE nid = :nid", 0, 1, array(
':nid' => $nid,
))
->fetchField();
$next_nid = db_query_range("SELECT nid FROM {node}\n {$join}\n WHERE (({$search_criteria} = :value AND nid > :nid)\n OR {$search_criteria} > :value)\n AND status = 1\n {$cond} ORDER BY\n {$search_criteria} ASC,nid ASC", 0, 1, array(
':value' => $criteria_value,
':nid' => $nid,
))
->fetchField();
$prev_nid = db_query_range("SELECT nid FROM {node}\n {$join}\n WHERE (({$search_criteria} = :value\n AND nid < :nid)\n OR {$search_criteria} < :value)\n AND status = 1\n {$cond}\n ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, array(
':value' => $criteria_value,
':nid' => $nid,
))
->fetchField();
}
else {
$next_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE nid > :nid\n AND status = 1 {$cond} ORDER BY nid ASC", 0, 1, array(
':nid' => $nid,
))
->fetchField();
$prev_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE nid < :nid\n AND status = 1\n {$cond} ORDER BY nid DESC", 0, 1, array(
':nid' => $nid,
))
->fetchField();
}
// Update the node-level data
$exists = (bool) db_query_range('SELECT 1
FROM {prev_next_node}
WHERE nid = :nid', 0, 1, array(
':nid' => $nid,
))
->fetchField();
if (!empty($exists)) {
db_update('prev_next_node')
->fields(array(
'prev_nid' => $prev_nid ? $prev_nid : 0,
'next_nid' => $next_nid ? $next_nid : 0,
'changed' => REQUEST_TIME,
))
->condition('nid', $nid)
->execute();
}
else {
$id = db_insert('prev_next_node')
->fields(array(
'prev_nid' => $prev_nid ? $prev_nid : 0,
'next_nid' => $next_nid ? $next_nid : 0,
'changed' => REQUEST_TIME,
'nid' => $nid,
))
->execute();
}
// Update the other nodes pointing to this node
foreach (node_type_get_types() as $type => $name) {
if (variable_get(PREV_NEXT_NODE_TYPE . $type, 0)) {
$search_criteria = variable_get(PREV_NEXT_NODE_TYPE . $type . '_indexing_criteria', PREV_NEXT_INDEXING_CRITERIA_DEFAULT);
$join = _prev_next_category_sql($node_type);
if (strlen($join) > 0) {
// search category for current node
$category = variable_get(PREV_NEXT_NODE_TYPE . $node_type . '_category_criteria', 'none');
$category_id = db_query("SELECT " . $category . "_tid FROM {field_data_" . $category . "} WHERE entity_id = :nid", array(
':nid' => $nid,
))
->fetchField();
if (empty($category_id)) {
$cond = '';
watchdog('prev_next', 'Node ' . $nid . ' missing taxonomy id for field_data_' . $category, WATCHDOG_ERROR);
}
else {
$cond = " AND " . $category . "_tid = " . $category_id . " ";
$cond .= _prev_next_node_types_sql($node_type);
}
}
else {
$cond = _prev_next_node_types_sql($node_type);
}
if ($search_criteria != 'nid') {
$criteria_value = db_query_range("SELECT {$search_criteria}\n FROM {node}\n WHERE nid = :nid", 0, 1, array(
':nid' => $nid,
))
->fetchField();
$prev_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE (({$search_criteria} = :value\n AND nid < :nid)\n OR {$search_criteria} < :value)\n AND status = 1\n {$cond} ORDER BY {$search_criteria} DESC,nid DESC", 0, 1, array(
':value' => $criteria_value,
':nid' => $nid,
))
->fetchField();
$next_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE (({$search_criteria} = :value\n AND nid > :nid)\n OR {$search_criteria} > :value)\n AND status = 1\n {$cond} ORDER BY\n {$search_criteria} ASC,nid ASC", 0, 1, array(
':value' => $criteria_value,
':nid' => $nid,
))
->fetchField();
}
else {
$prev_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE nid < :nid\n AND status = 1\n {$cond}\n ORDER BY nid DESC", 0, 1, array(
':nid' => $nid,
))
->fetchField();
$next_nid = db_query_range("SELECT nid\n FROM {node}\n {$join}\n WHERE nid > :nid\n AND status = 1\n {$cond}\n ORDER BY nid ASC", 0, 1, array(
':nid' => $nid,
))
->fetchField();
}
}
if ($next_nid) {
db_update('prev_next_node')
->fields(array(
'next_nid' => $next_nid ? $next_nid : 0,
))
->condition('nid', $nid)
->execute();
}
if ($prev_nid) {
db_update('prev_next_node')
->fields(array(
'prev_nid' => $prev_nid ? $prev_nid : 0,
))
->condition('nid', $nid)
->execute();
}
}
}