diff --git a/jeditable.module b/jeditable.module index 0f2811c..5e802f2 100644 --- a/jeditable.module +++ b/jeditable.module @@ -1,11 +1,11 @@ array(3, 4, 5), 'access arguments' => array('use jeditable'), ); + $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; } - -/** - * Implementation of hook_perm() - */ -function jeditable_perm() { - return array('use jeditable'); +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); } /** - * Implementation of hook_init(). + * Implements hook_permission(). */ -function jeditable_init() { - if(user_access('use jeditable')) { - drupal_add_js(drupal_get_path('module', 'jeditable') .'/jquery.jeditable.mini.js', 'module'); - drupal_add_js(drupal_get_path('module', 'jeditable') .'/drupal_jeditable.js', 'module'); - drupal_add_css(drupal_get_path('module', 'jeditable') .'/jeditable.css', 'theme'); - } +function jeditable_permission() { + return array( + 'use jeditable' => array( + 'title' => t('Use jEditable'), + 'description' => t('Use jEditable to edit fields in place.'), + ), + ); } /** - * Implementation of hook_field_formatter_info(),. + * Implements hook_field_formatter_info(),. */ 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, + 'field types' => array('text_long'), ), '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, ), ); } /** - * Implementation of hook_theme(). + * Implements hook_field_formatter_view(). + */ +function jeditable_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { + $path = drupal_get_path('module', 'jeditable'); + $elements = array(); + foreach ($items as $delta => $item) { + $field_delta=$display['views_field']->options['delta_offset']+$delta; + $elements[$delta] = array( + '#markup' => theme('jeditable_formatter_'. $display['type'], array('element' => $item, 'field' => $instance, 'entity' => $entity, 'entity_type' => $entity_type, 'delta' => $field_delta )), + ); + + if (user_access('use jeditable')) { + $elements[$delta]['#attached'] = array( + 'js' => array( + $path . '/jquery.jeditable.mini.js', + $path . '/drupal_jeditable.js', + ), + 'css' => array( + $path . '/jeditable.css', + ), + ); + } + } + return $elements; +} + +/** + * Implements hook_theme(). */ function jeditable_theme() { return array( 'jeditable_formatter_jeditable_textfield' => array( - 'arguments' => array('element' => NULL), + 'arguments' => array('element' => NULL, 'field' => NULL, 'entity' => NULL, 'entity_type' => NULL, 'delta' => NULL), ), 'jeditable_formatter_jeditable_textarea' => array( - 'arguments' => array('element' => NULL), + 'arguments' => array('element' => NULL, 'field' => NULL, 'entity' => NULL, 'entity_type' => NULL, 'delta' => NULL), ), 'jeditable_formatter_jeditable_datetime' => array( - 'arguments' => array('element' => NULL), + 'arguments' => array('element' => NULL, 'field' => NULL, 'entity' => NULL, 'entity_type' => NULL, 'delta' => NULL), ), 'jeditable_formatter_jeditable_nodereference' => array( - 'arguments' => array('element' => NULL), + 'arguments' => array('element' => NULL, 'field' => NULL, 'entity' => NULL, 'entity_type' => NULL, 'delta' => NULL), ), 'jeditable_workflow' => array( 'arguments' => array('node' => NULL), @@ -93,25 +128,73 @@ function jeditable_theme() { } /** - * Theme a CCK text field as a jeditable textfield. + * Returns the id fo + +/** + * Theme a text field as a jeditable textfield. * * @ingroup themeable */ -function theme_jeditable_formatter_jeditable_textfield($element) { - $id = $element['#node']->nid; - $field = $element['#field_name']; - return ''. $element[0]['#item']['value'] .''; +function theme_jeditable_formatter_jeditable_textfield($variables) { + $element = $variables['element']; + $field = $variables['field']; + $entity = $variables['entity']; + $entity_type = $variables['entity_type']; + + switch ($entity_type) { + case 'node': + // Check user's access to editing this node. + if (!node_access('update', $entity)) { + return $element['value']; + } + + $id = $entity->nid; + break; + + case 'user': + // Check user's access to editing this user. + if (!user_edit_access($entity)) { + return $element['value']; + } + $id = $entity->uid; + break; + } + if($entity_type == 'node') $entity_type='field'; + + return '' . $element['value'] . ''; } /** - * Theme a CCK text field as a jeditable textarea. + * Theme a textarea field as a jeditable textarea. * * @ingroup themeable */ -function theme_jeditable_formatter_jeditable_textarea($element) { - $id = $element['#node']->nid; - $field = $element['#field_name']; - return ''. $element[0]['#item']['value'] .''; +function theme_jeditable_formatter_jeditable_textarea($variables) { + $element = $variables['element']; + $field = $variables['field']; + $entity = $variables['entity']; + $entity_type = $variables['entity_type']; + + switch ($entity_type) { + case 'node': + // Check user's access to editing this node. + if (!node_access('update', $entity)) { + return $element['value']; + } + + $id = $entity->nid; + break; + + case 'user': + // Check user's access to editing this user. + if (!user_edit_access($entity)) { + return $element['value']; + } + $id = $entity->uid; + break; + } + + return '' . $element['value'] . ''; } /** @@ -123,7 +206,7 @@ function theme_jeditable_formatter_jeditable_nodereference($element) { $id = $element['#node']->nid; $field = $element['#field_name']; $node = node_load($element[0]['#item']['nid']); - return ''. $node->title .''; + return '' . $node->title . ''; } /** @@ -134,7 +217,7 @@ function theme_jeditable_formatter_jeditable_nodereference($element) { function theme_jeditable_formatter_jeditable_datetime($element) { $id = $element['#node']->nid; $field = $element['#field_name']; - return ''. $element[0]['#item']['value'] .''; + return '' . $element[0]['#item']['value'] . ''; } /** @@ -152,7 +235,7 @@ function theme_jeditable_workflow($node) { $field = $node->_workflow ? $node->_workflow : $node->workflow; // named differently depending on how far the node has loaded $state = workflow_get_state_name($field); - return ''. $state .''; + return '' . $state . ''; } /** @@ -161,7 +244,7 @@ function theme_jeditable_workflow($node) { function _jeditable_ajax_save() { // Retrieve the values needed from the post to this page $array = explode('-', $_POST['id']); - list($type, $id, $field_name) = $array; + list($type, $id, $field_name, $delta) = $array; $value = check_plain($_POST['value']); switch($type) { @@ -170,38 +253,47 @@ function _jeditable_ajax_save() { if(!node_access('update', $node)) { // check to see that current user has update permissions on the node $value = 'access denied'; // this is the value that will be returned, but no updates made } else { - $node->{$field_name} = $value; + $node->{$field_name}[$node->language][0] = $value; + $node->revision = variable_get('jeditable_create_new_revisions', false); node_save($node); } break; - case 'cck': + + case 'user': + $user = user_load($id); + if(!user_edit_access($user)) { // check to see that current user has update permissions on the user + $value = 'access denied'; // this is the value that will be returned, but no updates made + } else { + $user->{$field_name}[$user->language][0]['value'] = $value; + user_save($user); + } + break; + + case 'field': $node = node_load($id); + $delta=intval($delta); if(!node_access('update', $node)) { // check to see that current user has update permissions on the node $value = 'access denied'; // this is the value that will be returned, but no updates made } else { - $field = content_fields($field_name, $node->type); - + $field=field_info_field($field_name); + $lang=isset($node->{$field_name}['und'])?'und':$node->language; // assign nid if nodereference, format date if date, otherwise just assign value if($field['type'] == 'nodereference') { - $node->{$field_name}[0]['nid'] = $value; + $node->{$field_name}[$lang][$delta]['nid'] = $value; $referenced = node_load($value); $value = $referenced->title; } else if($field['type'] == 'datetime') { $unixtime = strtotime($value); $value = date('o-m-d H:i:s', $unixtime); - $node->{$field_name}[0]['value'] = $value; + $node->{$field_name}[$lang][$delta]['value'] = $value; } else { - $node->{$field_name}[0]['value'] = $value; + $node->{$field_name}[$lang][$delta]['value'] = $value; } + $node->revision = variable_get('jeditable_create_new_revisions', false); node_save($node); } break; - case 'user': - /** should be implemented if user reference field is implemented **/ - $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); @@ -302,7 +394,7 @@ function _jeditable_workflow_save($node, $sid) { return workflow_get_state_name($sid); } else { // here's where we do the actual transition. It will fail if user does not have appropriate permissions. - $new_sid = workflow_execute_transition($node, $sid, 'set using jeditable at '. request_uri()); + $new_sid = workflow_execute_transition($node, $sid, 'set using jeditable at ' . $_SERVER['HTTP_REFERER']); } if(empty($new_sid)) { @@ -312,4 +404,4 @@ function _jeditable_workflow_save($node, $sid) { // finally, this is the intended outcome and we can return the changed state's name return workflow_get_state_name($new_sid); -} \ No newline at end of file +}