View source
<?php
define('JEDITABLE_DIR', drupal_get_path('module', 'jeditable'));
define('JEDITABLE_DIR_PLUGIN', function_exists('libraries_get_path') ? libraries_get_path('jeditable') : JEDITABLE_DIR);
function jeditable_menu() {
$items['jeditable/ajax/save'] = array(
'title' => 'Save field',
'page callback' => '_jeditable_ajax_save',
'access arguments' => array(
'use jeditable',
),
'type' => MENU_CALLBACK,
);
$items['jeditable/ajax/load'] = array(
'title' => 'Load field',
'page callback' => '_jeditable_ajax_load',
'page arguments' => array(
3,
4,
5,
),
'access arguments' => array(
'use jeditable',
),
'type' => MENU_CALLBACK,
);
$items['admin/settings/jeditable'] = array(
'title' => 'jEditable',
'description' => 'Configure the jEditable module.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jeditable_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function jeditable_admin_settings() {
$form['jeditable_create_new_revisions'] = array(
'#type' => 'checkbox',
'#title' => t('Create Node Revisions'),
'#default_value' => variable_get('jeditable_create_new_revisions', 0),
'#description' => t('If enabled, each time a field is changed a new node revision will be generated. This will generate a very full revision table if jeditable is used extensively, so use with caution'),
);
return system_settings_form($form);
}
function jeditable_perm() {
return array(
'use jeditable',
);
}
function jeditable_init() {
if (user_access('use jeditable')) {
drupal_add_js(JEDITABLE_DIR_PLUGIN . '/jquery.jeditable.mini.js', 'module');
drupal_add_js(JEDITABLE_DIR . '/drupal_jeditable.js', 'module');
drupal_add_css(JEDITABLE_DIR . '/jeditable.css', 'theme');
}
}
function jeditable_field_formatter_info() {
return array(
'jeditable_textfield' => array(
'label' => t('jEditable Textfield'),
'field types' => array(
'text',
'number_integer',
'number_decimal',
'number_float',
),
'multiple values' => CONTENT_HANDLE_MODULE,
),
'jeditable_textarea' => array(
'label' => t('jEditable Textarea'),
'field types' => array(
'text',
),
'multiple values' => CONTENT_HANDLE_MODULE,
),
'jeditable_nodereference' => array(
'label' => t('jEditable Nodereference'),
'field types' => array(
'nodereference',
),
'multiple values' => CONTENT_HANDLE_MODULE,
),
'jeditable_datetime' => array(
'label' => t('jEditable Datetime picker'),
'field types' => array(
'datetime',
),
'multiple values' => CONTENT_HANDLE_MODULE,
),
);
}
function jeditable_theme() {
return array(
'jeditable_formatter_jeditable_textfield' => array(
'arguments' => array(
'element' => NULL,
),
),
'jeditable_formatter_jeditable_textarea' => array(
'arguments' => array(
'element' => NULL,
),
),
'jeditable_formatter_jeditable_datetime' => array(
'arguments' => array(
'element' => NULL,
),
),
'jeditable_formatter_jeditable_nodereference' => array(
'arguments' => array(
'element' => NULL,
),
),
'jeditable_workflow' => array(
'arguments' => array(
'node' => NULL,
),
),
);
}
function theme_jeditable_formatter_jeditable_textfield($element) {
$id = $element['#node']->nid;
$field = $element['#field_name'];
return '<span id="cck-' . $id . '-' . $field . '" class="jeditable-textfield">' . $element[0]['#item']['value'] . '</span>';
}
function theme_jeditable_formatter_jeditable_textarea($element) {
$id = $element['#node']->nid;
$field = $element['#field_name'];
return '<span id="cck-' . $id . '-' . $field . '" class="jeditable-textarea">' . $element[0]['#item']['value'] . '</span>';
}
function theme_jeditable_formatter_jeditable_nodereference($element) {
$id = $element['#node']->nid;
$field = $element['#field_name'];
$node = node_load($element[0]['#item']['nid']);
return '<span id="cck-' . $id . '-' . $field . '" class="jeditable-select">' . $node->title . '</span>';
}
function theme_jeditable_formatter_jeditable_datetime($element) {
$id = $element['#node']->nid;
$field = $element['#field_name'];
return '<span id="cck-' . $id . '-' . $field . '" class="jeditable-textfield edit-datetime">' . $element[0]['#item']['value'] . '</span>';
}
function theme_jeditable_workflow($node) {
$id = $node->nid;
$field = $node->_workflow ? $node->_workflow : $node->workflow;
$state = workflow_get_state_name($field);
return '<span id="workflow-' . $id . '-' . $field . '" class="jeditable-select">' . $state . '</span>';
}
function _jeditable_ajax_save() {
$array = explode('-', $_POST['id']);
list($type, $id, $field_name) = $array;
$value = check_plain($_POST['value']);
switch ($type) {
case 'node':
$node = node_load($id);
if (!node_access('update', $node)) {
$value = 'access denied';
}
else {
$node->{$field_name} = $value;
$node->revision = variable_get('jeditable_create_new_revisions', false);
node_save($node);
}
break;
case 'cck':
$node = node_load($id);
if (!node_access('update', $node)) {
$value = 'access denied';
}
else {
$field = content_fields($field_name, $node->type);
switch ($field['type']) {
case 'nodereference':
$value = _jeditable_save_nodereference_field($node, $field, $value);
break;
case 'userreference':
$value = _jeditable_save_userreference_field($node, $field, $value);
break;
case 'datetime':
$value = _jeditable_save_date_field($node, $field, $value);
break;
default:
$value = _jeditable_save_other_field($node, $field, $value);
break;
}
$node->revision = variable_get('jeditable_create_new_revisions', false);
}
break;
case 'user':
$user = user_load(array(
'uid' => $id,
));
$user->{$field_name} = $value;
user_save($user);
break;
case 'workflow':
$node = node_load($id);
$value = _jeditable_workflow_save($node, $value);
break;
}
print $value;
exit;
}
function _jeditable_save_nodereference_field($node, $field, $value) {
module_load_include('inc', 'node', 'node.pages');
$field_name = $field['field_name'];
$form_state = array();
$form_state['values'][$field_name]['nid']['nid'] = $value;
$form_state['values']['op'] = t('Save');
$form_id = $node->type . '_node_form';
drupal_execute($form_id, &$form_state, (object) $node);
drupal_get_messages('status');
$node = node_load(array(
"nid" => $node->nid,
));
if (isset($node->{$field_name})) {
$value = $node->{$field_name}[0]['nid'];
}
else {
$value = NULL;
}
$referenced_node = node_load($value);
$value = $referenced_node->title;
return $value;
}
function _jeditable_save_userreference_field($node, $field, $value) {
module_load_include('inc', 'node', 'node.pages');
$field_name = $field['field_name'];
$form_state = array();
$form_state['values'][$field_name]['uid']['uid'] = $value;
$form_state['values']['op'] = t('Save');
$form_id = $node->type . '_node_form';
drupal_execute($form_id, &$form_state, (object) $node);
drupal_get_messages('status');
$node = node_load(array(
"nid" => $node->nid,
));
if (isset($node->{$field_name})) {
$value = $node->{$field_name}[0]['uid'];
}
else {
$value = NULL;
}
$referenced_user = user_load(array(
'uid' => $value,
));
$value = $referenced_user->name;
return $value;
}
function _jeditable_save_date_field($node, $field, $value) {
module_load_include('inc', 'node', 'node.pages');
$field_name = $field['field_name'];
$form_state = array();
$form_state['values']['op'] = t('Save');
switch ($field['widget']['type']) {
case 'date_select':
$date = date_parse($value);
$form_state['values'][$field_name][0]['value']['year'] = $date['year'];
$form_state['values'][$field_name][0]['value']['month'] = $date['month'];
$form_state['values'][$field_name][0]['value']['day'] = $date['day'];
$form_state['values'][$field_name][0]['value']['hour'] = $date['hour'];
$form_state['values'][$field_name][0]['value']['minute'] = $date['minute'];
break;
case 'date_popup':
case 'date_text':
$form_state['values'][$field_name][0]['value']['date'] = $value;
break;
}
$form_id = $node->type . '_node_form';
drupal_execute($form_id, &$form_state, (object) $node);
drupal_get_messages('status');
$node = node_load(array(
"nid" => $node->nid,
));
if (isset($node->{$field_name})) {
$value = $node->{$field_name}[0]['value'];
$value = content_format($field_name, array(
'value' => $value,
));
}
else {
$value = NULL;
}
$value = strip_tags($value);
return $value;
}
function _jeditable_save_other_field($node, $field, $value) {
module_load_include('inc', 'node', 'node.pages');
$field_name = $field['field_name'];
switch ($field['widget']['type']) {
case 'optionwidgets_buttons':
case 'optionwidgets_select':
$defaults = array_values(optionwidgets_options($field));
$value = $defaults[$value];
break;
}
$form_state = array();
$form_state['values'][$field_name][0]['value'] = $value;
$form_state['values']['op'] = t('Save');
$form_id = $node->type . '_node_form';
drupal_execute($form_id, &$form_state, (object) $node);
drupal_get_messages('status');
$node = node_load(array(
"nid" => $node->nid,
));
if (isset($node->{$field_name})) {
$value = $node->{$field_name}[0]['value'];
$value = content_format($field_name, array(
'value' => $value,
));
}
else {
$value = NULL;
}
$value = strip_tags($value);
return $value;
}
function _jeditable_ajax_load() {
$array = explode('-', $_GET['id']);
list($type, $id, $field_name) = $array;
switch ($type) {
case 'node':
$node = node_load($id);
$value = $node->{$field};
$value = 'Y';
$defaults = array(
'E' => 'Letter E',
'M' => 'Letter M',
'Y' => 'Letter Y',
);
$defaults['selected'] = $value;
$defaults = json_encode($defaults);
break;
case 'cck':
$node = node_load($id);
$field = content_fields($field_name, $node->type);
switch ($field['type']) {
case 'nodereference':
$defaults = _jeditable_get_node_reference_field_values($field);
$defaults['selected'] = $node->{$field_name}[0]['nid'];
$defaults = json_encode($defaults);
break;
case 'userreference':
$defaults = _jeditable_get_user_reference_field_values($field);
$defaults['selected'] = $node->{$field_name}[0]['uid'];
$defaults = json_encode($defaults);
break;
case 'datetime':
$date = new DateTime($node->{$field_name}[0]['value']);
$date_format = date_limit_format(date_input_format(NULL, $field), $field['granularity']);
$defaults = date_format_date($date, 'custom', $date_format);
break;
case 'number_decimal':
case 'number_integer':
case 'text':
switch ($field['widget']['type']) {
case 'optionwidgets_buttons':
case 'optionwidgets_select':
$defaults = array_values(optionwidgets_options($field));
$defaults['selected'] = $node->{$field_name}[0]['value'];
$defaults = json_encode($defaults);
break;
default:
$defaults = $node->{$field_name}[0]['value'];
break;
}
break;
default:
$defaults = $node->{$field_name}[0]['value'];
break;
}
break;
case 'workflow':
$node = node_load($id);
$defaults = json_encode(_jeditable_workflow_load($node, $field_name));
}
print $defaults;
exit;
}
function _jeditable_get_node_reference_field_values($field) {
$values = _nodereference_potential_references($field);
$defaults = array();
foreach ($values as $key => $value) {
$defaults[$key] = $value['rendered'];
}
return $defaults;
}
function _jeditable_get_user_reference_field_values($field) {
$values = _userreference_potential_references($field);
$defaults = array();
foreach ($values as $key => $value) {
$defaults[$key] = $value['rendered'];
}
return $defaults;
}
function _jeditable_workflow_load($node, $sid) {
$defaults = workflow_field_choices($node);
$defaults['selected'] = $sid;
return $defaults;
}
function _jeditable_workflow_save($node, $sid) {
if ($sid == $node->_workflow) {
return workflow_get_state_name($sid);
}
else {
$new_sid = workflow_execute_transition($node, $sid, 'set using jeditable at ' . referer_uri());
}
if (empty($new_sid)) {
return "access denied";
}
return workflow_get_state_name($new_sid);
}
function jeditable_views_api() {
return array(
'api' => 2,
'path' => JEDITABLE_DIR . '/views',
);
}