View source
<?php
function draggableviews_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'draggableviews') . '/views',
);
}
function _draggableviews_get_field($view) {
foreach ($view->field as $field_name => $field) {
if ($field instanceof draggableviews_handler_field_draggable) {
$field->view = $view;
return $field;
}
}
return FALSE;
}
function draggableviews_views_pre_view(&$view, &$display_id, &$args) {
if (isset($view->preview) && isset($view->display_handler->handlers['field']['draggableviews']) && !user_access('access draggableviews')) {
drupal_set_message(t("Current display has a draggableviews field, however you cannot see it in the preview because you do not have\n '@permission' permission.", array(
'@permission' => 'access draggableviews',
)), 'warning');
}
}
function draggableviews_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'views_form_') === 0) {
$draggableviews = _draggableviews_get_field($form_state['build_info']['args'][0]);
}
if (empty($draggableviews)) {
return;
}
$has_other_views_form_handlers = FALSE;
foreach ($draggableviews->view->field as $field) {
if (property_exists($field, 'views_form_callback') || method_exists($field, 'views_form')) {
if (!$field instanceof draggableviews_handler_field_draggable) {
$has_other_views_form_handlers = TRUE;
}
if ($field instanceof views_bulk_operations_handler_field_operations) {
if ($form_state['step'] == 'views_form_views_form') {
$has_other_views_form_handlers = FALSE;
}
}
}
}
if (!$has_other_views_form_handlers) {
$form['actions']['submit']['#access'] = FALSE;
}
$form['actions']['save-order'] = array(
'#type' => 'submit',
);
if (!isset($form['draggableviews'][0])) {
$form['actions']['save-order']['#access'] = FALSE;
return;
}
if (!user_access('access draggableviews') || count($draggableviews->view->result) < 2) {
$form['actions']['save-order']['#access'] = FALSE;
return;
}
$current_path = current_path();
if (strpos($current_path, 'admin/structure/views/nojs/preview') !== FALSE) {
$form['actions']['message'] = array(
'#markup' => '<div class="messages warning">' . t('It is not possible to save order in Preview mode.') . '</div>',
);
$form['actions']['save-order']['#access'] = FALSE;
return;
}
$options = $draggableviews->view->field['draggableviews']->options['draggableviews'];
$form['actions']['save-order']['#value'] = t($options['save_button_label']);
$form['actions']['save-order']['#submit'][] = 'draggableviews_views_submit';
if ($options['ajax']) {
$form['actions']['save-order']['#ajax'] = array(
'callback' => 'draggableviews_view_draggabletable_form_ajax',
);
$form['actions']['save-order']['#attributes']['class'][] = 'js-draggableviews-save-order';
$form['actions']['save-order']['#attributes']['class'][] = 'element-invisible';
}
$form['#action'] = url(current_path());
if (count($_GET) > 1) {
$get = $_GET;
unset($get['q']);
if (!isset($_GET['destination'])) {
$get['destination'] = current_path() . '?' . drupal_http_build_query($get);
}
$form['#action'] .= '?' . drupal_http_build_query($get);
}
}
function draggableviews_views_submit($form, &$form_state) {
$view = $form_state['build_info']['args'][0];
$form_state['values']['draggableviews'] = $form_state['input']['draggableviews'];
$handler_object = draggableviews_get_handler_class($view->field['draggableviews']->options['draggableviews']['handler']);
$handler_object
->set($form_state);
module_invoke_all('draggableviews_sorted', $view);
if (module_exists('rules')) {
rules_invoke_event('draggableviews_rules_event_sorted', $view->name, $view->current_display);
}
}
function draggableviews_preprocess_views_view_table(&$vars) {
if ($order_view = _draggableviews_load_order_view($vars['view'])) {
if (!empty($order_view->field['draggableviews']->options['draggableviews']['hierarchy_handler'])) {
$hierarchy_handler_object = draggableviews_get_handler_class($order_view->field['draggableviews']->options['draggableviews']['hierarchy_handler'], 'hierarchy_handler');
foreach ($vars['rows'] as $key => $row) {
$first_column = current(array_keys($row));
$field = (object) array(
'view' => $vars['view'],
);
$depth = $hierarchy_handler_object
->get_depth($field, $key);
$vars['rows'][$key][$first_column] = theme('indentation', array(
'size' => $depth,
)) . $vars['rows'][$key][$first_column];
$vars['row_classes'][$key][] = 'depth-' . $depth;
}
}
}
if (!isset($vars['view']->field['draggableviews'])) {
return;
}
if (!user_access('access draggableviews')) {
foreach ($vars['rows'] as &$row) {
unset($row['draggableviews']);
}
unset($vars['header']['draggableviews']);
return;
}
$vars['classes_array'][] = 'draggable';
foreach ($vars['row_classes'] as &$row_classes) {
$row_classes[] = 'draggable';
}
$vars['attributes_array']['id'] = drupal_html_id('draggableviews-table-' . $vars['view']->name . '-' . $vars['view']->current_display);
drupal_add_tabledrag($vars['attributes_array']['id'], 'order', 'sibling', 'draggableviews-weight');
if ($vars['view']->field['draggableviews']->options['draggableviews']['ajax']) {
drupal_add_js(drupal_get_path('module', 'draggableviews') . '/js/draggableviews_table.js');
}
if (!empty($vars['view']->field['draggableviews']->options['draggableviews']['hierarchy_handler'])) {
drupal_add_tabledrag($vars['attributes_array']['id'], 'match', 'parent', 'draggableviews-parent', 'draggableviews-parent', 'draggableviews-id', FALSE);
drupal_add_tabledrag($vars['attributes_array']['id'], 'depth', 'group', 'draggableviews-depth', NULL, NULL, FALSE);
}
}
function draggableviews_preprocess_views_view_list(&$vars) {
if (!isset($vars['view']->field['draggableviews'])) {
return;
}
$class = 'draggableviews-grid-' . $vars['view']->name . '-' . $vars['view']->current_display;
$vars['list_type_prefix'] = str_replace('>', ' class="' . $class . '">', $vars['list_type_prefix']);
drupal_add_library('system', 'ui.sortable');
$js_setting['draggableviews_row_class'][] = $class;
$js_setting['draggableviews_ajax'] = $vars['view']->field['draggableviews']->options['draggableviews']['ajax'];
drupal_add_js($js_setting, 'setting');
drupal_add_js(drupal_get_path('module', 'draggableviews') . '/js/draggableviews_list.js');
drupal_add_css(drupal_get_path('module', 'draggableviews') . '/css/draggableviews_list.css');
}
function draggableviews_permission() {
return array(
'access draggableviews' => array(
'title' => t('Access draggable views'),
'description' => t('Give users the right to sort their views'),
),
);
}
function draggableviews_ctools_plugin_type() {
return array(
'handler' => array(
'use hooks' => FALSE,
),
'hierarchy_handler' => array(
'use hooks' => FALSE,
),
);
}
function draggableviews_ctools_plugin_directory($module, $plugin) {
if ($module == 'draggableviews' && ($plugin == 'handler' || $plugin == 'hierarchy_handler')) {
return 'handlers';
}
}
function draggableviews_contextual_links_view_alter(&$element, $items) {
if (!user_access('access draggableviews')) {
return;
}
$views_ui_element = array();
if (isset($element['#element']['#views_contextual_links_info']['views_ui'])) {
$views_ui_element = $element['#element']['#views_contextual_links_info']['views_ui'];
}
if (empty($views_ui_element) && isset($element['#element']['content']['#views_contextual_links_info']['views_ui'])) {
$views_ui_element = $element['#element']['content']['#views_contextual_links_info']['views_ui'];
}
if (!empty($views_ui_element['view_display_id']) && isset($views_ui_element['view'])) {
$display_id = $views_ui_element['view_display_id'];
$view = $views_ui_element['view'];
$view
->build($display_id);
if ($path = _draggableviews_get_order_path($view, FALSE)) {
if ($router_item = menu_get_item($path)) {
if ($router_item['access']) {
$element['#links']['draggableviews-order'] = array(
'title' => t('Order view'),
'href' => $path,
'query' => array(
'destination' => current_path(),
),
);
}
}
}
}
}
function draggableviews_get_handler_class($handler_name, $handler_type = 'handler') {
$objects =& drupal_static(__FUNCTION__);
if (!isset($objects[$handler_name])) {
ctools_include('plugins');
if ($class = ctools_plugin_load_class('draggableviews', $handler_type, $handler_name, 'handler')) {
$objects[$handler_name] = new $class();
}
}
return $objects[$handler_name];
}
function draggableviews_get_handlers() {
ctools_include('plugins');
$handlers = ctools_get_plugins('draggableviews', 'handler');
$return = array();
foreach ($handlers as $handler_id => $handler) {
$handler_object = draggableviews_get_handler_class($handler_id);
if (in_array('draggableviews_handler', class_parents(get_class($handler_object)))) {
$return[$handler_id] = $handler_object;
}
}
return $return;
}
function draggableviews_get_hierarchy_handlers() {
ctools_include('plugins');
$handlers = ctools_get_plugins('draggableviews', 'hierarchy_handler');
$return = array();
foreach ($handlers as $handler_id => $handler) {
$handler_object = draggableviews_get_handler_class($handler_id);
if (in_array('draggableviews_hierarchy_handler', class_parents(get_class($handler_object)))) {
$return[$handler_id] = $handler_object;
}
}
return $return;
}
function draggableviews_view_draggabletable_form_ajax($form, $form_state) {
$form_element = "form:has(input[name=form_build_id][value='{$form['form_build_id']['#value']}'])";
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_remove("{$form_element} div.tabledrag-changed-warning"),
ajax_command_remove("{$form_element} span.tabledrag-changed"),
ajax_command_remove("{$form_element} div.draggableviews-changed-warning"),
ajax_command_invoke("{$form_element} ul.draggableviews-changed", 'removeClass', array(
'draggableviews-changed',
)),
),
);
}
function _draggableviews_get_draggable_sort($view, $preferred_table_alias = '') {
$default_sort_id = '';
foreach ($view->sort as $id => $sort) {
if ($sort->definition['handler'] == 'draggableviews_handler_sort') {
if (!$preferred_table_alias || $sort->table_alias == $preferred_table_alias) {
return $id;
}
if (!$default_sort_id) {
$default_sort_id = $id;
}
}
}
return $default_sort_id;
}
function _draggableviews_eval_return($code, $arguments, $view) {
return eval($code);
}
function _draggableviews_load_order_view($view, $include_self = TRUE) {
if ($order_view_name_display = _draggableviews_get_order_view_display($view, $include_self)) {
if ($order_view_name_display == 'self' && $include_self) {
$order_view = $view;
}
else {
list($order_view_name, $order_view_display) = explode(':', $order_view_name_display);
if ($order_view_name == $view->name) {
if ($order_view_display == $view->current_display) {
$order_view = $view;
}
else {
$order_view = views_get_view($order_view_name);
if (!isset($order_view)) {
drupal_set_message(t('The order view is unavailable. Try saving or re-saving the view'), 'error');
return;
}
$order_view
->set_display($order_view_display);
$order_view
->init_handlers();
}
}
else {
$order_view = views_get_view($order_view_name);
if ($order_view && $order_view
->access($order_view_display)) {
$order_view
->set_display($order_view_display);
$order_view
->init_handlers();
}
}
}
return $order_view;
}
}
function _draggableviews_get_order_view_display($view, $include_self = TRUE) {
if (!($sort_key = _draggableviews_get_draggable_sort($view))) {
return FALSE;
}
$order_view_display = $view->sort[$sort_key]->options['draggableviews_setting_view'];
if (empty($order_view_display)) {
return FALSE;
}
if (!$include_self) {
if ($order_view_display == 'self' || $order_view_display == $view->name . ':' . $view->current_display) {
return FALSE;
}
}
return $order_view_display;
}
function _draggableviews_get_order_path($view, $include_self = TRUE) {
$path = FALSE;
if ($order_view = _draggableviews_load_order_view($view, $include_self)) {
if (isset($order_view->display[$order_view->current_display]->display_options['path'])) {
$path = $order_view->display[$order_view->current_display]->display_options['path'];
}
}
elseif ($include_self) {
if (isset($view->display[$view->current_display]->display_options['path'])) {
$path = $view->display[$view->current_display]->display_options['path'];
}
}
else {
return FALSE;
}
$args = $view->args;
if (strpos($path, '%') !== FALSE && !empty($args)) {
$new_path_array = array();
foreach (explode('/', $path) as $path_part) {
if (strpos($path_part, '%') !== FALSE) {
$new_path_array[] = !empty($args) ? array_shift($args) : '';
}
else {
$new_path_array[] = $path_part;
}
}
$path = implode('/', $new_path_array);
}
if (!empty($args)) {
$path .= '/' . implode('/', $args);
}
return $path;
}
function _draggableviews_get_views_options($view = NULL) {
if (!empty($view)) {
$view_clone = clone $view;
$view_clone
->set_display($view_clone->current_display);
$view_clone
->init_handlers();
if (isset($view_clone->field['draggableviews'])) {
return $view_clone->name . ':' . $view_clone->current_display;
}
}
$views = views_get_enabled_views();
$options = array();
foreach ($views as $view_name => $view) {
foreach ($view->display as $display_name => $display) {
if ($display_name == 'default') {
continue;
}
$view_clone = clone $view;
$view_clone
->set_display($display_name);
$view_clone
->init_handlers();
if (isset($view_clone->field['draggableviews'])) {
$options[$view_name . ':' . $display_name] = $view->human_name . ' (' . $display->display_title . ')';
}
}
}
return $options;
}
function draggableviews_node_delete($node) {
$nid = $node->nid;
$query = db_select('draggableviews_structure', 'ds')
->condition('entity_id', $nid)
->fields('ds', array(
'view_name',
));
$result = $query
->execute()
->rowCount();
if ($result > 0) {
db_delete('draggableviews_structure')
->condition('entity_id', $nid)
->execute();
}
}