You are here

function edit_limit_nodeapi in Edit Limit 6

Implementation of hook_nodeapi().

File

./edit_limit.module, line 164
edit_limit.module Primary module file for Edit Limit. This contains all the hooks needed to run the module.

Code

function edit_limit_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // On update, ignore users with 'bypass edit limit' when updating the edit
  // count. We don't want to penalize other users. Also, we'll skip this if
  // the edit count setting is disabled, for the same reason.
  $allowed_types = variable_get('edit_limit_node_types', array());
  if (!user_access('bypass edit limit') && !empty($allowed_types[$node->type])) {
    if ($op == 'update') {
      if (variable_get('edit_limit_node_count_enabled', FALSE)) {

        // Update the count of edit limits.
        $count_data = db_fetch_array(db_query("SELECT * FROM {edit_limit_node_count} WHERE nid=%d", $node->nid));
        $edit_count = 0;
        $update = array();
        if (!empty($count_data)) {
          $edit_count = $count_data['count'];
          $update = array(
            'nid',
          );
        }
        $edit_count++;
        $data = array(
          'nid' => $node->nid,
          'count' => $edit_count,
        );
        drupal_write_record('edit_limit_node_count', $data, $update);
      }
    }

    // On prepare, compare the number of times a node has been edited to the limit,
    // and set a message for the user to let them know how many edits are left, if any.
    if ($op == 'prepare' && !empty($node->nid)) {
      if (variable_get('edit_limit_node_count_enabled', FALSE)) {
        $limit = variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT);
        $count = edit_limit_node_count($node->nid);

        // If the edit count is getting close to the limit, make this a warning instead of a simple status
        $type = $count + 2 > $limit ? 'warning' : 'status';
        drupal_set_message("This node has been edit {$count} out of {$limit} times, after which it will be locked.", $type);
      }
    }
  }
}