View source
<?php
define('SLICKGRID_CALLBACK_PATH', 'slickgrid/callback');
function slickgrid_menu() {
$items[SLICKGRID_CALLBACK_PATH . '/%'] = array(
'page callback' => 'slickgrid_callback',
'page arguments' => array(
2,
),
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
'file' => 'includes/slickgrid.callbacks.inc',
);
return $items;
}
function slickgrid_views_api() {
$path = drupal_get_path('module', 'slickgrid');
return array(
'api' => '3',
'path' => $path . '/includes',
'template path' => $path . '/theme',
);
}
function slickgrid_theme() {
$path = drupal_get_path('module', 'slickgrid');
return array(
'slickgrid' => array(
'arguments' => array(
'view' => array(),
),
),
'slickgrid_controls' => array(
'arguments' => array(
'view' => array(),
),
),
'slickgrid_tabs' => array(
'arguments' => array(
'view' => array(),
),
),
'slickgrid_views_plugin_table' => array(
'render element' => 'form',
'path' => $path . '/theme',
'file' => 'theme.inc',
),
);
}
function slickgrid_library() {
$path = libraries_get_path('slickgrid');
$libraries['slickgrid'] = array(
'title' => 'Slickgrid',
'website' => 'https://github.com/mleibman/SlickGrid/',
'version' => 'Master',
'js' => array(
$path . '/lib/firebugx.js' => array(),
$path . '/lib/jquery.event.drag-2.0.min.js' => array(),
$path . '/slick.core.js' => array(),
$path . '/slick.dataview.js' => array(),
$path . '/slick.grid.js' => array(),
$path . '/plugins/slick.checkboxselectcolumn.js' => array(),
$path . '/plugins/slick.rowselectionmodel.js' => array(),
$path . '/controls/slick.columnpicker.js' => array(),
),
'css' => array(
$path . '/slick.grid.css' => array(),
$path . '/controls/slick.columnpicker.css' => array(),
),
'dependencies' => array(
array(
'system',
'ui.sortable',
),
array(
'system',
'ui.resizable',
),
array(
'system',
'effects.highlight',
),
),
);
return $libraries;
}
function slickgrid_views_pre_view(&$view, $display_id) {
global $user;
if (isset($_POST['display_id'])) {
$view->slickgrid_settings = slickgrid_get_settings(array(
'uid' => $user->uid,
'view_name' => $view->name,
'display_id' => $_POST['display_id'],
));
}
if (isset($view->slickgrid_settings['hidden_columns'])) {
foreach ($view->slickgrid_settings['hidden_columns'] as $hidden_column) {
$view
->set_item($display_id, 'field', $hidden_column, null);
}
}
if (isset($_POST['export_selected_rows']) && isset($_POST['entity_ids'])) {
foreach ($view
->get_items('argument') as $id => $arg) {
$view
->set_item($display_id, 'argument', $id, NULL);
}
$options = array(
'table' => $view->base_table,
'field' => $view->base_field,
'break_phrase' => 1,
);
$view
->add_item($display_id, 'argument', $view->base_table, $view->base_field, $options);
$view
->set_arguments(array(
implode('+', $_POST['entity_ids']),
));
}
$plugins = array();
if ($view->display_handler
->get_option('style_plugin') == 'slickgrid') {
$style_options = $view->display_handler
->get_option('style_options');
if (isset($style_options['columns'])) {
foreach ($style_options['columns'] as $field_id => $column) {
foreach (array_keys(slickgrid_get_plugin_types()) as $plugin_type) {
if (!empty($column[$plugin_type])) {
if (!isset($plugins[$plugin_type])) {
$plugins[$plugin_type] = slickgrid_get_plugins($plugin_type);
}
if (isset($plugins[$plugin_type][$column[$plugin_type]]['hooks']['views_pre_view'])) {
require_once DRUPAL_ROOT . '/' . $plugins[$plugin_type][$column[$plugin_type]]['path'] . '/' . $plugins[$plugin_type][$column[$plugin_type]]['file'];
$func = $plugins[$plugin_type][$column[$plugin_type]]['hooks']['views_pre_view'];
$func($view, $field_id, $display_id);
}
}
}
}
}
}
}
function slickgrid_user_delete($account) {
slickgrid_delete_settings(array(
'uid' => $account->uid,
));
}
function slickgrid_entity_info_alter(&$entity_info) {
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', 'node') . "/node.pages.inc";
foreach ($entity_info['node']['bundles'] as $type => $bundle) {
$entity_info['node']['bundles'][$type]['add'] = array(
'callback' => $type . '_node_form',
'file' => $file,
);
}
}
function slickgrid_ctools_plugin_api($owner, $api) {
if ($owner == 'slickgrid') {
return array(
'version' => 1,
);
}
}
function slickgrid_ctools_plugin_directory($module, $plugin) {
if ($module == 'slickgrid') {
return 'plugins/' . $plugin . 's';
}
}
function slickgrid_ctools_plugin_type() {
return array(
'filter' => array(
'cache' => TRUE,
'title' => t("Filter"),
),
'editor' => array(
'cache' => TRUE,
'title' => t("Editor"),
),
'formatter' => array(
'cache' => TRUE,
'title' => t("Formatter"),
),
'validator' => array(
'cache' => TRUE,
'title' => t("Validator"),
),
);
}
function slickgrid_get_plugins($plugin_type) {
ctools_include('plugins');
$plugins = ctools_get_plugins('slickgrid', $plugin_type);
return $plugins;
}
function slickgrid_get_plugin_types() {
return array_map(create_function('$type', 'return $type["title"];'), slickgrid_ctools_plugin_type());
}
function slickgrid_get_plugin_options_for_field($plugin_type, $field_type) {
$options = array();
$plugins = slickgrid_get_plugins($plugin_type);
foreach ($plugins as $type => $plugin) {
if (!isset($plugin['field_types'])) {
$options[$type] = $plugin['title'];
}
elseif (in_array($field_type, $plugin['field_types'])) {
$options[$type] = $plugin['title'];
}
}
if (count($options)) {
$options = array_merge(array(
'' => '<none>',
), $options);
}
return $options;
}
function slickgrid_plugin_load_class($type, $id, $class_name) {
ctools_include('plugins');
$plugin_definition = ctools_get_plugins('slickgrid', $type, $id);
require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . "/{$class_name}.class.php";
$class = ctools_plugin_get_class($plugin_definition, $class_name);
return $class;
}
function slickgrid_set_settings($uid, $view_name, $display_id, $settings) {
$record = new stdClass();
$record->uid = $uid;
$record->view_name = $view_name;
$record->display_id = $display_id;
if ($record->settings = slickgrid_get_settings(array(
'uid' => $uid,
'view_name' => $view_name,
'display_id' => $display_id,
))) {
$update = array(
'uid',
'view_name',
'display_id',
);
}
else {
$update = array();
}
foreach ($settings as $setting => $value) {
$record->settings[$setting] = $value;
}
$record->settings = serialize($record->settings);
drupal_write_record('slickgrid', $record, $update);
}
function slickgrid_get_settings($conditions = array(), $setting = null) {
$query = db_select('slickgrid', 'sg');
foreach ($conditions as $field => $condition) {
$query
->condition($field, $condition);
}
$query
->fields('sg', array(
'settings',
));
$result = $query
->execute();
$settings = unserialize($result
->fetchField());
if ($setting) {
return $settings[$setting];
}
else {
return $settings;
}
}
function slickgrid_delete_settings($conditions = array()) {
if (count($conditions)) {
$query = db_delete('slickgrid');
foreach ($conditions as $field => $condition) {
$query
->condition($field, $condition);
}
return $query
->execute();
}
return false;
}
function slickgrid_add_modal() {
ctools_include('ajax');
ctools_include('modal');
ctools_modal_add_js();
$style_settings = array(
'ctools-modal-slickgrid-fixed' => array(
'modalSize' => array(
'type' => 'fixed',
'width' => 550,
'height' => 300,
'addWidth' => 20,
'addHeight' => 15,
),
'modalOptions' => array(
'opacity' => 0.5,
'background-color' => '#000',
),
'animation' => 'fadeIn',
'modalTheme' => 'SlickgridModal',
'throbber' => theme('image', array(
'path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'),
'alt' => t('Loading...'),
'title' => t('Loading'),
)),
),
'ctools-modal-slickgrid-scale' => array(
'modalSize' => array(
'type' => 'scale',
),
'modalOptions' => array(
'opacity' => 0.5,
'background-color' => '#000',
),
'animation' => 'fadeIn',
'modalTheme' => 'SlickgridModal',
'throbber' => theme('image', array(
'path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'),
'alt' => t('Loading...'),
'title' => t('Loading'),
)),
),
);
drupal_add_js($style_settings, 'setting');
}
function slickgrid_encode_columns($columns) {
$encoded_columns = drupal_json_encode($columns);
foreach (slickgrid_get_plugin_types() as $type => $plugin) {
$encoded_columns = preg_replace('/("' . $type . '":)"([a-z]*)"/i', '$1$2', $encoded_columns);
}
$encoded_columns = preg_replace('/("width":)"([0-9]*)"/i', '$1$2', $encoded_columns);
return $encoded_columns;
}
function slickgrid_get_view($view_name, $display_id, $entity_ids = array()) {
$view = views_get_view($view_name);
$view
->set_display($display_id);
foreach ($view
->get_items('argument') as $id => $arg) {
$view
->set_item($display_id, 'argument', $id, NULL);
}
if (count($entity_ids)) {
$options = array(
'table' => $view->base_table,
'field' => $view->base_field,
'break_phrase' => 1,
);
$view
->add_item($display_id, 'argument', $view->base_table, $view->base_field, $options);
$view
->set_arguments(array(
implode('+', $entity_ids),
));
}
$view
->pre_execute();
$view
->execute();
$view
->render();
return $view;
}
function slickgrid_get_entity_keys($entity_type = null, $keys_to_ignore = array(
'label',
)) {
$entity_keys = array();
if ($entity_type) {
$entities_info = array(
entity_get_info($entity_type),
);
}
else {
$entities_info = entity_get_info();
}
foreach ($entities_info as $entity_info) {
foreach ($keys_to_ignore as $key_to_ignore) {
unset($entity_info['entity keys'][$key_to_ignore]);
}
$entity_keys += array_values($entity_info['entity keys']);
}
return $entity_keys;
}
function slickgrid_get_fields_of_type($type, $entity_type = null) {
$query = db_select('field_config', 'fc');
$query
->fields('fc', array(
'field_name',
));
$query
->condition('type', $type, '=');
if (!is_null($entity_type)) {
$query
->join('field_config_instance', 'fci', 'fci.field_id = fc.field_id');
$query
->condition('entity_type', $entity_type);
}
$result = $query
->execute();
return $result
->fetchAllKeyed();
}
function slickgrid_editor_form($form, &$form_state) {
$editor = $form_state['editor'];
foreach ($editor->entities as $entity) {
list($id, $vid, $bundle_name) = entity_extract_ids($editor->entity_type, $entity);
if (!($instance = field_info_instance($editor->entity_type, $editor->field_id, $bundle_name))) {
$editor
->set_error($id, t('Field doesn\'t exist'), 'form');
return;
}
elseif ($instance['required']) {
$required = true;
}
if (!count($form)) {
$form['#parents'] = array();
$form = _field_invoke_default('form', $editor->entity_type, $entity, $form, $form_state, array(
'field_id' => $editor->field_id,
'field_name' => $editor->field_id,
));
}
}
if ($required) {
$langcode = $form[$editor->field_id]['#language'];
$form[$editor->field_id][$langcode][0]['#required'] = $required;
}
foreach (array(
'field_name',
'field_id',
'view',
'display_id',
'plugin',
'revision',
'entity_type',
'entity_ids',
) as $element_name) {
if (is_array($form_state['values'][$element_name])) {
foreach ($form_state['values'][$element_name] as $element_value) {
$form[$element_name][] = array(
'#type' => 'hidden',
'#value' => $element_value,
'#parents' => array(
$element_name,
'',
),
);
}
}
else {
$form[$element_name] = array(
'#type' => 'hidden',
'#value' => $form_state['values'][$element_name],
);
}
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function slickgrid_editor_form_validate($form, &$form_state) {
$editor =& $form_state['editor'];
foreach ($editor->entities as $entity) {
list($id, $vid, $bundle_name) = entity_extract_ids($editor->entity_type, $entity);
if ($instance = field_info_instance($editor->entity_type, $editor->field_id, $bundle_name)) {
_field_invoke_default('extract_form_values', $editor->entity_type, $entity, $form, $form_state);
try {
field_attach_validate($editor->entity_type, $entity);
continue;
} catch (FieldValidationException $e) {
foreach ($e->errors as $field_name => $field_errors) {
$error .= $field_errors;
}
}
}
else {
$error = t('Field doesn\'t exist');
}
$editor
->set_error($id, $error);
form_set_error($editor->field_id, t('Validation error'));
}
}
function slickgrid_editor_form_submit($form, &$form_state) {
$editor =& $form_state['editor'];
foreach ($editor->entities as $entity) {
list($id, $vid, $bundle_name) = entity_extract_ids($editor->entity_type, $entity);
if (!($instance = field_info_instance($editor->entity_type, $editor->field_id, $bundle_name))) {
$editor
->set_error($id, t('Field does not exist for this bundle'), 'submit');
continue;
}
entity_form_submit_build_entity($editor->entity_type, $entity, $form, $form_state);
try {
entity_save($editor->entity_type, $entity);
$editor->updated[$id] = array(
'vid' => $vid,
);
} catch (Exception $e) {
$editor
->set_error($id, t('Error trying to update entity'), 'submit');
}
}
}