View source
<?php
function weight_permission() {
return array(
'assign node weight' => array(
'title' => t('Assign node weights'),
'description' => t('Allow modification of node weights.'),
),
);
}
function weight_menu_alter(&$items) {
$enabled = FALSE;
$settings = _weight_get_settings();
foreach ($settings as $setting) {
if ($setting['enabled']) {
$enabled = TRUE;
break;
}
}
if ($enabled && variable_get('weight_frontpage', 0)) {
$items['node']['page callback'] = 'weight_page_default';
}
}
function weight_page_default() {
$select = db_select('node', 'n');
$select
->join('weight_weights', 'w', 'w.entity_id = n.nid');
$select
->fields('n', array(
'nid',
))
->condition('n.promote', 1)
->condition('n.status', 1)
->orderBy('n.sticky', 'DESC')
->orderBy('w.weight', 'ASC')
->orderBy('n.created', 'DESC')
->addTag('node_access');
$select = $select
->extend('PagerDefault');
$select
->limit(variable_get('default_nodes_main', 10));
$nids = $select
->execute()
->fetchCol();
if (!empty($nids)) {
$nodes = node_load_multiple($nids);
$build = node_view_multiple($nodes);
drupal_add_feed('rss.xml', variable_get('site_name', 'Drupal') . ' ' . t('RSS'));
$build['pager'] = array(
'#theme' => 'pager',
'#weight' => 5,
);
drupal_set_title('');
}
else {
drupal_set_title(t('Welcome to @site-name', array(
'@site-name' => variable_get('site_name', 'Drupal'),
)), PASS_THROUGH);
$default_message = '<p>' . t('No front page content has been created yet.') . '</p>';
$default_links = array();
if (_node_add_access()) {
$default_links[] = l(t('Add new content'), 'node/add');
}
if (!empty($default_links)) {
$default_message .= theme('item_list', array(
'items' => $default_links,
));
}
$build['default_message'] = array(
'#markup' => $default_message,
'#prefix' => '<div id="first-time">',
'#suffix' => '</div>',
);
}
return $build;
}
function weight_entity_property_info_alter(&$info) {
$info['node']['properties']['weight'] = array(
'type' => 'integer',
'label' => t('Weight'),
'description' => t('Provides weight scores on nodes'),
'getter callback' => 'weight_weight_property_get',
'setter callback' => 'weight_weight_property_set',
);
}
function weight_weight_property_get($node) {
return _weight_get_weight($node);
}
function weight_weight_property_set($node, $name, $value) {
$node->weight_weight = $value;
}
function weight_form_system_site_information_settings_alter(&$form, &$form_state) {
$form['front_page']['weight_frontpage'] = array(
'#type' => 'checkbox',
'#title' => t('Use weighted front page?'),
'#description' => t('Selecting this option will emulate the default front page,
but the nodes will be sorted by weight. Only nodes of content types that are
weight enabled will be shown.'),
'#return_value' => 1,
'#default_value' => variable_get('weight_frontpage', 0),
'#weight' => 5,
);
$form['front_page']['site_frontpage']['#weight'] = 10;
$form['front_page']['site_frontpage']['#states'] = array(
'visible' => array(
':input[name="weight_frontpage"]' => array(
'checked' => FALSE,
),
),
);
$form['#submit'][] = 'weight_site_information_form_submit';
}
function weight_site_information_form_submit($form, &$form_state) {
cache_clear_all();
}
function weight_form_node_admin_content_alter(&$form, &$form_state) {
if (user_access('assign node weight')) {
$weight_types = _weight_get_types();
$add_column = FALSE;
if (!empty($form['admin']['nodes']['#options'])) {
$nodes = $form['admin']['nodes']['#options'];
}
elseif (!empty($form['admin']['nodes']['#rows'])) {
$nodes = $form['admin']['nodes']['#rows'];
}
else {
$nodes = array();
}
foreach ($nodes as $nid => $node) {
$node = node_load($nid);
if (in_array($node->type, $weight_types)) {
$settings = _weight_get_settings($node->type);
if (!$settings['menu_weight']) {
$options = _weight_get_options($settings['range']);
$form['weight'][$nid] = array(
'#type' => 'select',
'#options' => $options,
'#default_value' => isset($node->weight_weight) ? $node->weight_weight : $settings['default'],
'#ajax' => array(
'callback' => '_weight_set_ajax',
),
);
$add_column = TRUE;
}
else {
$form['weight'][$nid] = array(
'#value' => '',
);
}
}
else {
$form['weight'][$nid] = array(
'#value' => '',
);
}
}
$form['weight']['#add_column'] = $add_column;
$form['#after_build'][] = 'weight_node_admin';
}
}
function _weight_set_ajax($form, $form_state) {
$nid = $form_state['input']['_triggering_element_name'];
$weight = $form_state['values'][$nid];
$node = node_load($nid);
$node->weight_weight = $weight;
_weight_set_weight($node);
drupal_alter('weight_set_ajax', $node);
}
function weight_node_admin($form_element, &$form_state) {
if ($form_element['weight']['#add_column']) {
$form_element['admin']['nodes']['#header']['weight']['data'] = t('Weight');
if (!empty($form_element['admin']['nodes']['#options'])) {
foreach ($form_element['admin']['nodes']['#options'] as $nid => $node) {
$form_element['admin']['nodes']['#options'][$nid]['weight']['data'] = $form_element['weight'][$nid];
}
}
elseif (!empty($form_element['admin']['nodes']['#rows'])) {
foreach ($form_element['admin']['nodes']['#rows'] as $nid => $node) {
$form_element['admin']['nodes']['#rows'][$nid]['weight']['data'] = $form_element['weight'][$nid];
}
}
unset($form_element['weight']);
}
return $form_element;
}
function weight_form_node_type_form_alter(&$form, &$form_state) {
if (!empty($form_state['values']['weight_range'])) {
$range = $form_state['values']['weight_range'];
}
else {
if ($form['#node_type']->type) {
if (isset($settings['range'])) {
$range = $settings['range'];
}
}
if (!isset($range)) {
$range = 20;
}
}
$form['weight_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Weight Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#access' => user_access('assign node weight'),
);
$form['weight_settings']['weight_enabled'] = array(
'#type' => 'radios',
'#title' => t('Enable'),
'#description' => t('Enable weight for this content type?'),
'#options' => array(
t('No'),
t('Yes'),
),
'#default_value' => isset($settings['enabled']) ? $settings['enabled'] : 0,
);
$form['weight_settings']['weight_range'] = array(
'#type' => 'radios',
'#title' => t('Weight Range'),
'#description' => t('This will be the range available when selecting a weight.'),
'#options' => array(
10 => '± 10',
20 => '± 20',
50 => '± 50',
100 => '± 100',
),
'#default_value' => $range,
'#ajax' => array(
'callback' => 'weight_range_callback',
'wrapper' => 'weight-default',
'method' => 'replace',
),
);
$form['weight_settings']['weight_menu'] = array(
'#type' => 'radios',
'#title' => t('Use Menu Weight'),
'#description' => t('Use the menu weight of the node, if it has one.'),
'#options' => array(
t('No'),
t('Yes'),
),
'#default_value' => isset($settings['menu_weight']) ? $settings['menu_weight'] : 0,
);
$form['weight_settings']['weight_default'] = array(
'#type' => 'select',
'#title' => t('Default Weight'),
'#description' => t('This weight will be applied to nodes if none is selected.'),
'#options' => _weight_get_options($range),
'#default_value' => isset($settings['default']) ? $settings['default'] : 0,
'#prefix' => '<div id="weight-default">',
'#suffix' => '</div>',
);
$form['weight_settings']['sync_translations'] = array(
'#type' => 'radios',
'#title' => t('Sync translations'),
'#description' => t('When changing the weight of a node, also update it\'s translations.'),
'#options' => array(
t('No'),
t('Yes'),
),
'#default_value' => isset($settings['sync_translations']) ? $settings['sync_translations'] : 0,
);
$form['weight_settings']['clear_all'] = array(
'#type' => 'checkbox',
'#title' => t('Clear all weights'),
'#description' => t('If selected, all weights for nodes of this type will be cleared.'),
);
$form['additional_settings']['#attached']['js'][] = drupal_get_path('module', 'weight') . '/weight.js';
$form['#submit'][] = 'weight_node_type_form_submit';
}
function weight_range_callback($form, $form_state) {
return $form['weight_settings']['weight_default'];
}
function weight_node_type_form_submit($form, &$form_state) {
$values = $form_state['values'];
$default = $values['weight_menu'] ? 'menu' : $values['weight_default'];
$exists = db_query('SELECT 1 FROM {weight_settings} WHERE type=:type', array(
':type' => $values['type'],
))
->fetchField();
if ($exists) {
db_update('weight_settings')
->fields(array(
'weight_enabled' => $values['weight_enabled'],
'weight_range' => $values['weight_range'],
'menu_weight' => $values['weight_menu'],
'weight_default' => $values['weight_default'],
'sync_translations' => $values['sync_translations'],
))
->condition('type', $values['type'])
->execute();
}
else {
$query = db_insert('weight_settings')
->fields(array(
'type' => $values['type'],
'weight_enabled' => $values['weight_enabled'],
'weight_range' => $values['weight_range'],
'menu_weight' => $values['weight_menu'],
'weight_default' => $values['weight_default'],
'sync_translations' => $values['sync_translations'],
))
->execute();
}
if (!$exists) {
_weight_set_defaults($default, $values['type']);
}
if ($values['clear_all']) {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', $values['type'])
->addMetaData('account', user_load(1));
$result = $query
->execute();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
db_delete('weight_weights')
->condition('entity_type', 'node')
->condition('entity_id', $nids, 'IN')
->execute();
}
_weight_set_defaults($default, $values['type']);
}
}
function weight_form_views_form_alter(&$form, &$form_state, $form_id) {
$view = $form_state['build_info']['args'][0];
if (array_key_exists('weight', $view->field) && empty($view->result)) {
unset($form['actions']);
}
}
function weight_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
if ($entity_type == 'node') {
$settings = _weight_get_settings($entity->type);
if (isset($settings['enabled']) && $settings['enabled'] && !$settings['menu_weight']) {
$form['weight'] = array(
'#type' => 'fieldset',
'#title' => t('Weight'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#access' => user_access('assign node weight'),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'weight') . '/weight.js',
),
),
);
$form['weight']['weight_weight'] = array(
'#type' => 'select',
'#title' => t('Weight'),
'#options' => _weight_get_options($settings['range']),
'#default_value' => isset($entity->weight_weight) ? $entity->weight_weight : $settings['default'],
);
}
}
}
function weight_node_load($nodes, $type) {
$types = _weight_get_types();
foreach ($nodes as $node) {
if (in_array($node->type, $types)) {
$node->weight_weight = _weight_get_weight($node);
}
}
}
function weight_node_insert($node) {
if (isset($node->weight_weight) && is_numeric($node->weight_weight)) {
_weight_set_weight($node);
}
}
function weight_node_update($node) {
if (isset($node->weight_weight) && is_numeric($node->weight_weight)) {
_weight_set_weight($node);
}
}
function weight_menu_link_insert($link) {
_weight_set_weight_from_menu_link($link);
}
function weight_menu_link_update($link) {
_weight_set_weight_from_menu_link($link);
}
function weight_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
if ($entity_type == 'node') {
$settings = _weight_get_settings($bundle_name);
if (!empty($settings) && $settings['enabled']) {
$targets['weight'] = array(
'name' => t('Weight'),
'callback' => 'weight_feeds_set_target',
'description' => t('The weight of the node.'),
);
}
}
}
function weight_feeds_set_target($source, $entity, $target, $values) {
if (!is_array($values)) {
$values = array(
$values,
);
}
$weight = NULL;
foreach ($values as $value) {
if (is_numeric($value)) {
$weight = $value;
break;
}
}
$entity->weight_weight = $weight;
}
function weight_features_api() {
return array(
'weight' => array(
'name' => 'Weight',
'file' => drupal_get_path('module', 'weight') . '/weight.features.inc',
'default_hook' => 'weight_features_default_settings',
'feature_source' => TRUE,
),
);
}
function weight_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'weight') . '/views',
);
}
function weight_views_submit($form, &$form_state) {
$values = $form_state['values'];
$nodes = entity_load('node', $values['nids']);
foreach ($values['nids'] as $key => $nid) {
$node = $nodes[$nid];
$node->weight_weight = $values['weight'][$key][$nid];
_weight_set_weight($node);
}
drupal_set_message(t('Weights saved successfully.'));
}
function weight_preprocess_views_view_table(&$variables) {
if (!array_key_exists('weight', $variables['fields']) || !user_access('assign node weight')) {
return;
}
$vars['classes_array'][] = 'draggable';
foreach ($variables['rows'] as $key => $row) {
$variables['row_classes'][$key][] = 'draggable';
}
$table_id = 'weight-table-' . $variables['id'];
$variables['attributes_array']['id'] = $table_id;
drupal_add_tabledrag($table_id, 'order', 'sibling', 'weight-weight');
}
function weight_apachesolr_index_document_build_node(ApacheSolrDocument $document, $node, $env_id) {
if (isset($node->weight_weight)) {
$document->is_weight = (int) $node->weight_weight;
}
else {
$settings = _weight_get_settings($node->type);
$document->is_weight = (int) $settings['default'];
}
}
function weight_apachesolr_query_prepare($query) {
$query
->setAvailableSort('is_weight', array(
'title' => t('Weight'),
'default' => 'asc',
));
}
function weight_settings_save($settings) {
foreach ($settings as $type => $setting) {
db_merge('weight_settings')
->key(array(
'type' => $type,
))
->fields(array(
'type' => $type,
'weight_enabled' => $setting['enabled'],
'weight_range' => $setting['range'],
'menu_weight' => $setting['menu_weight'],
'weight_default' => $setting['default'],
'sync_translations' => $setting['sync_translations'],
))
->execute();
}
}
function _weight_set_weight($node) {
$weight = $node->weight_weight;
$settings = _weight_get_settings($node->type);
if (isset($settings['menu_weight']) && $settings['menu_weight']) {
$weight = db_select('menu_links', 'ml');
$weight
->fields('ml', array(
'weight',
))
->condition('link_path', 'node/' . $node->nid)
->condition('module', 'menu');
$weight = $weight
->execute()
->fetchField();
if (!$weight) {
$weight = $settings['default'];
}
}
if (module_exists('translation') && $node->nid != $node->tnid) {
if ($settings['sync_translations'] && $node->tnid != 0) {
$weight = _weight_get_weight($node->tnid);
}
}
if ($node->nid && is_numeric($weight)) {
db_merge('weight_weights')
->key(array(
'entity_id' => $node->nid,
))
->fields(array(
'entity_id' => $node->nid,
'entity_type' => 'node',
'weight' => $weight,
))
->updateFields(array(
'weight' => $weight,
))
->execute();
entity_get_controller('node')
->resetCache(array(
$node->nid,
));
if (module_exists('search_api')) {
search_api_track_item_change('node', array(
$node->nid,
));
}
}
}
function _weight_get_weight($node) {
if (is_numeric($node)) {
$nid = $node;
}
elseif (module_exists('translation') && $node->nid != $node->tnid) {
$settings = _weight_get_settings($node->type);
if (empty($settings)) {
return FALSE;
}
if ($settings['sync_translations'] && $node->tnid != 0) {
$nid = $node->tnid;
}
else {
$nid = $node->nid;
}
}
else {
$nid = $node->nid;
}
$weight = db_query("SELECT weight FROM {weight_weights} WHERE entity_id=:id AND entity_type=:type", array(
':id' => $nid,
':type' => 'node',
))
->fetchField();
return $weight;
}
function _weight_set_weight_from_menu_link($link) {
if (user_access('assign node weight')) {
$node = _weight_get_node_from_menu_link($link);
$weight_types = _weight_get_types();
if (is_object($node) && in_array($node->type, $weight_types)) {
$settings = _weight_get_settings($node->type);
if ($settings['menu_weight']) {
_weight_set_weight($node);
}
}
}
}
function _weight_get_node_from_menu_link($link) {
$node = FALSE;
if (is_array($link) && strpos($link['link_path'], 'node/') === 0) {
$nid = drupal_substr($link['link_path'], 5);
if (is_numeric($nid)) {
$node = node_load($nid);
}
}
return $node;
}
function _weight_get_settings($type = NULL) {
$settings =& drupal_static(__FUNCTION__);
$query = 'SELECT * FROM {weight_settings}';
if (!isset($settings)) {
$settings = array();
$result = db_query('SELECT * FROM {weight_settings}');
foreach ($result as $row) {
$settings[$row->type] = array(
'enabled' => $row->weight_enabled,
'range' => $row->weight_range,
'menu_weight' => $row->menu_weight,
'default' => $row->weight_default,
'sync_translations' => $row->sync_translations,
);
}
}
if ($type && !isset($settings[$type])) {
return array();
}
return $type ? $settings[$type] : $settings;
}
function _weight_get_types() {
static $types = NULL;
if ($types === NULL) {
$types = db_query('SELECT type FROM {weight_settings} WHERE weight_enabled=1')
->fetchCol();
}
return $types;
}
function _weight_get_options($range) {
$max = $range;
$min = $max * -1;
$options = array();
for ($i = $min; $i <= $max; $i++) {
$options[$i] = $i;
}
return $options;
}
function _weight_set_defaults($default, $type) {
$select = db_select('node', 'n');
$select
->fields('n', array(
'nid',
))
->condition('type', $type);
$results = $select
->execute();
foreach ($results as $result) {
if ($default == 'menu') {
$weight = db_select('menu_links', 'ml');
$weight
->fields('ml', array(
'weight',
))
->condition('link_path', 'node/' . $result->nid);
$weight = $weight
->execute()
->fetchField();
if (!$weight) {
$weight = 0;
}
}
else {
$weight = db_select('weight_weights', 'w');
$weight
->fields('w', array(
'weight',
))
->condition('entity_id', $result->nid);
$weight = $weight
->execute()
->fetchField();
if (!$weight) {
$weight = $default;
}
}
db_merge('weight_weights')
->key(array(
'entity_id' => $result->nid,
))
->fields(array(
'entity_id' => $result->nid,
'entity_type' => 'node',
'weight' => $weight,
))
->updateFields(array(
'weight' => $weight,
))
->execute();
}
}