summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuriy Gerasimov2012-03-01 09:04:48 (GMT)
committer Yuriy Gerasimov2012-03-01 09:04:48 (GMT)
commit53d0ac3776d790d152daa97c238b212b8814afc5 (patch)
treed1692cd0304d268924aec52a90fbc12e58f48376
parent5bcb10c097794efef449bea17c2839cc5f710f2c (diff)
Issue #1353738: Init commit.
-rw-r--r--draggableviews.module54
-rw-r--r--handlers/draggableviews_fieldapi.inc103
-rw-r--r--handlers/draggableviews_native.inc50
-rw-r--r--views/draggableviews_handler_field_draggable.inc65
4 files changed, 239 insertions, 33 deletions
diff --git a/draggableviews.module b/draggableviews.module
index 2289c14..47eb7f3 100644
--- a/draggableviews.module
+++ b/draggableviews.module
@@ -24,37 +24,11 @@ function draggableviews_views_form_submit($form, &$form_state) {
return;
}
$view = $fv['view'];
- $view_name = $view->name;
- $view_display = $view->current_display;
- // View arguments.
- $arguments = $view->args;
- if (isset($view->exposed_input)) {
- $arguments += $view->exposed_input;
- ksort($arguments);
- // Redirect view to the same page with exposed filters set.
- $form_state['redirect'] = array(current_path(), array('query' => $view->exposed_input));
- }
- $args_string = json_encode($arguments);
-
- // Save records to our custom table.
- foreach ($fv['draggableviews'] as $item) {
- // Delete previous order record.
- db_delete('draggableviews_structure')
- ->condition('view_name', $view_name)
- ->condition('view_display', $view_display)
- ->condition('args', $args_string)
- ->condition('entity_id', $item['id'])
- ->execute();
- // Create new order record.
- $record = array(
- 'view_name' => $view_name,
- 'view_display' => $view_display,
- 'args' => $args_string,
- 'entity_id' => $item['id'],
- 'weight' => $item['weight'],
- );
- drupal_write_record('draggableviews_structure', $record);
+ // Get name of getter function of plugin.
+ ctools_include('plugins');
+ if ($setter_function = ctools_plugin_load_function('draggableviews', 'handler', $view->field['draggableviews']->options['draggableviews']['handler'], 'setter callback')) {
+ $setter_function($form_state);
}
// Trigger the event "A view has been sorted"
@@ -156,6 +130,26 @@ function draggableviews_permission() {
}
/**
+ * Implements hook_ctools_plugin_type().
+ */
+function draggableviews_ctools_plugin_type() {
+ return array(
+ 'handler' => array(
+ 'use hooks' => FALSE,
+ ),
+ );
+}
+
+/**
+ * Implements hook_ctools_plugin_directory().
+ */
+function draggableviews_ctools_plugin_directory($module, $plugin) {
+ if (($module == 'draggableviews') && ($plugin == 'handler')) {
+ return 'handlers';
+ }
+}
+
+/**
* Implements hook_contextual_links_view_alter().
*
* Adds "Order view" contextual link.
diff --git a/handlers/draggableviews_fieldapi.inc b/handlers/draggableviews_fieldapi.inc
new file mode 100644
index 0000000..bf783c8
--- /dev/null
+++ b/handlers/draggableviews_fieldapi.inc
@@ -0,0 +1,103 @@
+<?php
+
+$plugin = array(
+ 'label' => 'FieldAPI',
+ 'getter callback' => 'draggableviews_fieldapi_getter',
+ 'setter callback' => 'draggableviews_fieldapi_setter',
+ 'options form callback' => 'draggableviews_fieldapi_options_form',
+ 'option definition callback' => 'draggableviews_fieldapi_definition_form',
+);
+
+/**
+ * Set default value of field option.
+ */
+function draggableviews_fieldapi_definition_form() {
+ return array('field' => '');
+}
+
+/**
+ * Add field options for handler.
+ */
+function draggableviews_fieldapi_options_form($field) {
+ $form = array();
+ $options = array('' => t('- Select -'));
+
+ // Check all the sortings added to a view. Hope there is
+ // better way to determine them. Need to research.
+ foreach ($field->view->display_handler->default_display->display->display_options['sorts'] as $sort_option) {
+ $field_name = $sort_option['field'];
+ // Field should be like "field_name_value".
+ if (strpos($field_name, 'field_') === FALSE || strpos($field_name, '_value') === FALSE) {
+ continue;
+ }
+
+ // Remove "_value" from field name and try to load the field.
+ $field_name = substr($field_name, 0, strlen($field_name) - 6);
+ if ($field_info = field_info_field($field_name)) {
+ if ($field_info['type'] == 'number_integer') {
+ $views_field_data = field_views_field_default_views_data($field_info);
+ $options[$sort_option['table'] . ':' . $sort_option['field']] = filter_xss($views_field_data[$sort_option['table']][$sort_option['field']]['title']);
+ }
+ }
+ }
+
+ $form['field'] = array(
+ '#type' => 'select',
+ '#title' => t('Field'),
+ '#options' => $options,
+ '#default_value' => $field->options['draggableviews']['draggableviews_fieldapi']['field'],
+ '#description' => t('Please select field that contains weight. It should be integer type and already added to sorts of the view.'),
+ );
+ return $form;
+}
+
+function draggableviews_fieldapi_getter($field, $index) {
+ // Get the name of selected field.
+ $field_option = $field->options['draggableviews']['draggableviews_fieldapi']['field'];
+ list($field_table, $field_name) = explode(':', $field_option);
+ // Current row.
+ $row = $field->view->result[$index];
+ // Check whether key has table name and field name in it.
+ foreach ($row as $key => $value) {
+ if (strpos($key, $field_table) !== FALSE && strpos($key, $field_name) !== FALSE) {
+ return $value;
+ }
+ }
+}
+
+function draggableviews_fieldapi_setter(&$form_state) {
+ $fv = $form_state['values'];
+ $view = $fv['view'];
+ $view_name = $view->name;
+ $view_display = $view->current_display;
+ // View arguments.
+ $arguments = $view->args;
+ if (isset($view->exposed_input)) {
+ $arguments += $view->exposed_input;
+ ksort($arguments);
+ // Redirect view to the same page with exposed filters set.
+ $form_state['redirect'] = array(current_path(), array('query' => $view->exposed_input));
+ }
+
+ $base_table = $view->base_table;
+ $entity_info_all = entity_get_info();
+ $entity_type = '';
+ foreach ($entity_info_all as $entity_name_key => $entity_info) {
+ if ($entity_info['base table'] == $view->base_table) {
+ $entity_type = $entity_name_key;
+ break;
+ }
+ }
+
+ $options_field = $view->field['draggableviews']->options['draggableviews']['draggableviews_fieldapi']['field'];
+ list($field_tabe, $field_column) = explode(':', $options_field);
+ // Remove '_value' from column name to get field name.
+ $field_name = substr($field_column, 0, strlen($field_column) - 6);
+
+ // Save the values of the field.
+ foreach ($fv['draggableviews'] as $item) {
+ $entity_wrapper = entity_metadata_wrapper($entity_type, $item['id']);
+ $entity_wrapper->{$field_name}->set($item['weight']);
+ $entity_wrapper->save();
+ }
+}
diff --git a/handlers/draggableviews_native.inc b/handlers/draggableviews_native.inc
new file mode 100644
index 0000000..4d96cef
--- /dev/null
+++ b/handlers/draggableviews_native.inc
@@ -0,0 +1,50 @@
+<?php
+
+$plugin = array(
+ 'label' => 'Native',
+ 'getter callback' => 'draggableviews_native_getter',
+ 'setter callback' => 'draggableviews_native_setter',
+ 'options validation callback' => 'draggableviews_native_options_validate',
+);
+
+function draggableviews_native_getter($field, $index) {
+ $row = $field->view->result[$index];
+ return (isset($row->draggableviews_structure_weight)) ? $row->draggableviews_structure_weight : 0;
+}
+
+function draggableviews_native_setter(&$form_state) {
+ $fv = $form_state['values'];
+ $view = $fv['view'];
+ $view_name = $view->name;
+ $view_display = $view->current_display;
+ // View arguments.
+ $arguments = $view->args;
+ if (isset($view->exposed_input)) {
+ $arguments += $view->exposed_input;
+ ksort($arguments);
+ // Redirect view to the same page with exposed filters set.
+ $form_state['redirect'] = array(current_path(), array('query' => $view->exposed_input));
+ }
+ $args_string = json_encode($arguments);
+
+ // Save records to our custom table.
+ foreach ($fv['draggableviews'] as $item) {
+ // Delete previous order record.
+ db_delete('draggableviews_structure')
+ ->condition('view_name', $view_name)
+ ->condition('view_display', $view_display)
+ ->condition('args', $args_string)
+ ->condition('entity_id', $item['id'])
+ ->execute();
+
+ // Create new order record.
+ $record = array(
+ 'view_name' => $view_name,
+ 'view_display' => $view_display,
+ 'args' => $args_string,
+ 'entity_id' => $item['id'],
+ 'weight' => $item['weight'],
+ );
+ drupal_write_record('draggableviews_structure', $record);
+ }
+} \ No newline at end of file
diff --git a/views/draggableviews_handler_field_draggable.inc b/views/draggableviews_handler_field_draggable.inc
index 1cfaf06..a70982a 100644
--- a/views/draggableviews_handler_field_draggable.inc
+++ b/views/draggableviews_handler_field_draggable.inc
@@ -14,11 +14,21 @@ class draggableviews_handler_field_draggable extends views_handler_field {
$options['draggableviews'] = array(
'contains' => array(
+ 'handler' => array('default' => 'draggableviews_native'),
'save_button_label' => array('default' => 'Save'),
'ajax' => array('default' => FALSE),
),
);
+ // Populate default values of form elements provided by handlers.
+ ctools_include('plugins');
+ $handlers = ctools_get_plugins('draggableviews', 'handler');
+ foreach ($handlers as $handler_id => $handler) {
+ // Add handlers form elements.
+ if ($definition_function = ctools_plugin_load_function('draggableviews', 'handler', $handler_id, 'option definition callback')) {
+ $options['draggableviews']['contains'][$handler_id] = array('default' => $definition_function());
+ }
+ }
return $options;
}
@@ -37,6 +47,44 @@ class draggableviews_handler_field_draggable extends views_handler_field {
'#collapsed' => FALSE,
);
+ $handler_options = array();
+ // Load all plugins type "handler".
+ ctools_include('plugins');
+ $handlers = ctools_get_plugins('draggableviews', 'handler');
+ $handler_forms = array();
+ foreach ($handlers as $handler_id => $handler) {
+ $handler_options[$handler_id] = filter_xss($handler['label']);
+ // Add handlers form elements.
+ if ($form_function = ctools_plugin_load_function('draggableviews', 'handler', $handler_id, 'options form callback')) {
+ $handler_forms[$handler_id] = $form_function($this);
+ }
+ }
+
+ $form['draggableviews']['handler'] = array(
+ '#type' => 'select',
+ '#title' => t('Handler'),
+ '#options' => $handler_options,
+ '#default_value' => $this->options['draggableviews']['handler'],
+ );
+
+ // Add handler's form element as fielset that
+ // is active only if handler selected.
+ foreach ($handler_forms as $handler_id => $handler_form_element) {
+ $form['draggableviews'][$handler_id] = array(
+ '#type' => 'fieldset',
+ '#title' => $form['draggableviews']['handler']['#options'][$handler_id],
+ '#collapsible' => FALSE,
+ '#states' => array(
+ 'visible' => array(
+ 'select[name="options[draggableviews][handler]"]' => array('value' => $handler_id),
+ ),
+ ),
+ );
+ foreach ($handler_form_element as $key => $form_element) {
+ $form['draggableviews'][$handler_id][$key] = $form_element;
+ }
+ }
+
$form['draggableviews']['save_button_label'] = array(
'#type' => 'textfield',
'#title' => t('Custom Save button label'),
@@ -52,6 +100,14 @@ class draggableviews_handler_field_draggable extends views_handler_field {
);
}
+ // TODO add validation callback for plugin to ensure all other settings are in place (sort order for native filter for example).
+ function options_validate(&$form, &$form_state) {
+ // Get name of getter function of plugin.
+// ctools_include('plugins');
+// $validation_function = ctools_plugin_load_function('draggableviews', 'handler', $this->options['draggableviews']['handler'], 'options validation callback');
+// $validation_function($this->view, $form, $form_state);
+ }
+
function render($values) {
if (user_access('access draggableviews')) {
return '<!--form-item-' . $this->options['id'] . '--' . $this->view->row_index . '-->';
@@ -85,13 +141,17 @@ class draggableviews_handler_field_draggable extends views_handler_field {
$form[$this->options['id']][$row_index] = array(
'#tree' => TRUE,
);
+
+ // Get name of getter function of plugin.
+ ctools_include('plugins');
+ $getter_function = ctools_plugin_load_function('draggableviews', 'handler', $this->options['draggableviews']['handler'], 'getter callback');
+
// Weight field selectbox.
$form[$this->options['id']][$row_index]['weight'] = array(
'#type' => 'select',
'#options' => range(-$range, $range),
'#attributes' => array('class' => array('draggableviews-weight')),
- '#default_value' => (isset($row->draggableviews_structure_weight)) ?
- $row->draggableviews_structure_weight : 0,
+ '#default_value' => $getter_function($this, $row_index),
);
// Item to keep id of the entity.
$form[$this->options['id']][$row_index]['id'] = array(
@@ -100,5 +160,4 @@ class draggableviews_handler_field_draggable extends views_handler_field {
);
}
}
-
} \ No newline at end of file