All pastes #119376 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-08-06 20:33 UTC
rendered paste body
<?php
// $Id: anyedit.module,v 0.1alpha $

/**
 * @file
 * adds on the fly editing functionality with JQuery.
 */

/**
 * Implementation of hook_help().
 */
function anyedit_help($section) {
  switch ($section) {
    case 'admin/help#anyedit':
      return t('help content needed');
    case 'admin/modules#description':
      return t('adds on the fly editing functionality with JQuery.');
  }
}

/**
 * Implementation of hook_perm().
 */
function anyedit_perm() {
  return array('access anyedit');
}

/**
 * Implementation of hook_menu().
 */
function anyedit_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    //  main edit callback
    $items[] = array('path' => 'anyedit/edit',
    'title' => t('anyedit edit'),
    'access' => user_access('access anyedit'),
    'type' => MENU_CALLBACK,
    'callback' => 'anyedit_edit');

    //  callback for node properties
    $items[] = array('path' => 'anyedit/node',
    'title' => t('anyedit select node'),
    'access' => user_access('access anyedit'),
    'type' => MENU_CALLBACK,
    'callback' => 'anyedit_select_node');
  }

  return $items;
}

/**
 * Implementation of hook_settings().
 */
function anyedit_settings() {
  $form['messages'] = array('#type' => 'fieldset', '#title' => t('Messages'), '#collapsible' => true);
  $form['messages']['anyedit_updated_msg'] = array(
  '#type' => 'textfield',
  '#title' => t('Updated Message'),
  '#default_value' => variable_get('anyedit_updated_msg', 'updated'),
  '#size' => 40,
  '#maxlength' => 200,
  '#required' => true,
  );

  $form['messages']['anyedit_error_msg'] = array('#type' => 'textfield',
  '#title' => t('Error Message'),
  '#description' => t('%response is a placeholder for what the server will return back as a more specific error response'),
  '#default_value' => variable_get('anyedit_error_msg', 'error updating (%response)'),
  '#size' => 40,
  '#maxlength' => 200,
  '#required' => true,
  );

  $form['node_types_options'] = array('#type' => 'fieldset', '#title' => t('Node Types'), '#collapsible' => true);
  $form['node_types_options']['anyedit_node_types'] = array('#type' => 'checkboxes',
  '#title' => NULL,
  '#default_value' => variable_get('anyedit_node_types', node_get_types()),
  '#options' => node_get_types(),
  //'#prefix' => '<pre>'. print_r(node_get_types(), true) .'</pre>'
  );

  if (module_exist('tinymce') && user_access('administer tinymce')) {
    $form['tinymce'] = array('#type' => 'fieldset', '#title' => t('TinyMCE'), '#collapsible' => true);
    $form['tinymce']['anyedit_tinymce'] = array('#type' => 'checkbox',
    '#title' => t('enable TinyMCE on textarea editing'),
    '#default_value' => variable_get('anyedit_tinymce', true)
    );
  }

  return $form;
}

/**
 * Implementation of hook_nodeapi().
 */
function anyedit_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  static $anyedit_include;

  if ($node && $op == 'view' && user_access('access anyedit')) {
    if (!$anyedit_include) {
      $available_nodes = variable_get('anyedit_node_types', node_get_types());
      foreach ($available_nodes as $available_node => $node_type) {
        if ($node_type == $node->type) {
          _anyedit_jquery();
          _anyedit();
          _anyedit_js('anyedit.nodes.js');
          $anyedit_include = true;
          break;
        }
      }
    }

    // oh so sneaky
    if ($page) {
      $node->body .= '<div class="nid">'. $node->nid .'</div>'. "\n";
    } else if ($teaser) {
      $node->teaser .= '<div class="nid">'. $node->nid .'</div>'. "\n";
    }
  }
}

/**
 * grabbing node content
 */
function anyedit_select_node() {
  $nid = $_POST['nid'];
  $property = $_POST['property'];
  if (is_numeric($nid) && $property) {
    $node = node_load(array('nid' => $nid));
    print $node->$property;
  }

  exit();
}

/**
 * main edit callback
 */
function anyedit_edit() {
  global $user;

  // if this is a node edit we do node permissions check
  if ($_POST['nid']) {
    $node = node_load(array('nid' => $_POST['nid']));
    if (!anyedit_node_access($node)) return;
    
    $node->$_POST['property'] = $_POST['value'];
    node_save($node);

    print $node->$_POST['property'];
  } else if ($_POST['db_table']) {

    if (db_affected_rows() > 0) {
      print variable_get('anyedit_updated_msg', 'updated');
    } else {
      //  TODO: $response needs to be a more specific error message ?
      print t(variable_get('anyedit_error_msg', 'error updating (%response)'), array('%response' => $response));
    }
  }

  exit();
}

/**
 * handle node permissions
 */
function anyedit_node_access($node) {
  global $user;
  if ((user_access('edit own '. $node->type) && ($user->uid == $node->uid)) || user_access('administer '. $node->type)) {
    return true;
  }
}

/**
 *  helper functions
 */
function _anyedit_js($filename) {
  drupal_add_js(drupal_get_path('module', 'anyedit') .'/js/'. $filename);
}

function _anyedit_jquery($f = 'jquery-svn.js') {
  _anyedit_js($f);
}

function _anyedit() {
  static $included;
  $configs = array();
  
  if (module_exist('tinymce') && user_access('access tinymce') && variable_get('anyedit_tinymce', true)) {
    $configs['tinymce'] = 'true';
  }

  if (!$included) {
    // we need a base_path for the js side of things so we don't ever break paths
    $head = '<script type="text/javascript"> 
      var base_path = "'. base_path() .'";
      var anyedit_configs = Array();'. "\n";
    
    foreach ($configs as $config_name => $config_value) {
     $head .= 'anyedit_configs["'. $config_name .'"] = "'. $config_value .'";'. "\n";
    }
    
    $head .= '</script>'. "\n";
    
    drupal_set_html_head($head);

    // include our version if jquery until jquery is in core
    _anyedit_jquery();

    // use my simple wrapper function of drupal_add_js
    _anyedit_js('anyedit.js');

    // include a stylesheet
    theme('anyedit_styles');

    $included = true;
  }
}

/**
 *  theme functions
 */

function theme_anyedit_styles() {
  drupal_set_html_head(theme('stylesheet_import', base_path() . drupal_get_path('module', 'anyedit') .'/anyedit.css'));
}