<?php
// $Id: editview.module,v 1.7.2.3 2010/11/11 15:06:58 frodo Exp $

/**
 * @file
 * Plug-in to make an editable node view.
 */

/**
 * @addtogroup editview
 * @{
 */

/**
 * Implementation of hook_views_api().
 */
function editview_views_api() {
  return array(
    'api' => 2.0,
    'path' => drupal_get_path('module', 'editview') .'/includes',
  );
}

/**
 * Implementation of hook_help().
 */
function editview_help($path, $arg) {
  switch ($path) {
    case 'admin/help#editview':
      return t('<p>Editview is a Views plug-in that allows you to create views in which nodes are editable and new nodes can be created. Editview works with or without JavaScript enabled.</p>').
             t('<h3>Usage</h3>').
             t('Editable node views are created just like any other type of view, with a few caveats:</p>').
             '<ol>'.
             t('<li>At this moment, editview can only be used on node views (<em>View Type</em> must be set to <em>Node</em> when creating the view).</li>').
	     t('<li>If you want to be able to add new nodes in the view, set the <em>style</em> to <em>Editview</em> in <em>Basic settings</em>.').
	     '<ul>'.
             t('<li>You must now indicate what type of node will be added through the new node form and where it will appear.</li>').
             t('<li>Make sure all required fields are included in the view, or new nodes will fail validation.</li>').
	     t('<li>It may be a good idea to provide a <em>Sort criteria</em> of <em>Node: Post date</em> so that when adding new nodes, they appear where users expect them to appear.</li>').
             '</ul></li>'.
	     t('<li>If you do not want to add new nodes, select any other style that allows you to set a row style and set the <em>row style</em> to <em>Editview</em>. Alternatively, you can now select not to display the new node form when using the Edtiview style.</li>').
             '</ol>'.
             t('<h3>Cookbook</h3>').
             t('<p>A few example views follow. If you find another useful way to use an Editview, write a quick description and it will be added it here.</p>').
             t('<h4>Make a view that allows you to edit and create pages on your site</h4>').
             t('<p>This Editview will show an editable table of page nodes. You can edit their title and body fields, save your changes, delete pages (with confirmation), and create new pages which are then added to the view.</p>').
             t('<p>Steps:</p>').
             t('<ol>').
             t('<li>Create a new node view, giving it a name and setting its <em>View type</em> to <em>node</em></li>').
	     t('<li>In the default display, add <em>Node: title</em> and <em>Node: body</em> fields</li>').
	     t('<li>In the default display, add a sort criterium on <em>Node: Post date</em> and set it to descending</li>').
	     t('<li>In the default display, add a filter on <em>Node: type</em> being equal to <em>Page</em>.</li>').
	     t('<li>Add a <em>Page</em> display</li>').
	     t('<li>For this page, set within <em>Page Settings</em> the <em>Path</em> to something easy to remember</li>').
	     t('<li>For this page, set within <em>Basic Settings</em> the <em>Style</em> to <em>Editview</em>. Remember to use the Override button. Set <em>Node Type</em> to <em>Page</em>.</li>').
	     t('<li>For this page, set within <em>Basic Settings</em> the <em>Access</em> to something sensible. Remember to use the Override button.</li>').
             t('<li>Save the view and visit its url.</li>').
             t('</ol>').
             t('<h4>View, edit, and add <em>child</em> nodes on a <em>parent</em> node page</h4>').
             t('<p>This view uses cck, views, and editview to add the ability to see and edit a table of <em>child</em> nodes on a <em>parent</em> node page. We will relate children to parents using a cck node reference field to the child node type.</p>').
             t('<p>Steps:</p>').
             t('<ol>').
             t('<li>Create the parent and child node types. In this example we will make <em>organisation</em> the parent type, and <em>employee</em> the child type:').
             t('<ol>').
             t('<li>Create <em>organisation</em> as a cck node type, giving it whatever fields you like.</li>').
             t('<li>Create <em>employee</em> as a ckk node type. One of its fields should be a node reference which you should limit to only be able to access nodes of type <em>organisation</em>. Make sure the node reference is a select list, not an auto-complete text field. We will be relying on not having to enter the reference manually.</li>').
	     t('<li>For the node reference field, specify the following code for its default value:').
	     t('<pre>if (arg(0) == \'node\' && is_numeric(arg(1))) {
  $organisation = node_load(arg(1));
  if ($organisation-&gt;type == \'organisation\') {
    return array(0 => array(\'nid\' =&gt; $organisation-&gt;nid));
  }
}
return array(array());</pre>').
             t('<li>At this point you may want to create a few test organisations and assign each one a few employees.</li>').
	     t('</ol></li>').
             t('<li>Create a new node view, giving it a name and setting its <em>View type</em> to <em>node</em>:</li>').
	     '<ol>'.
	     t('<li>In the default display, add all employee fields except the node reference fields </li>').
	     t('<li>In the default display, add a filter on <em>Node: type</em> being equal to <em>employee</em>.</li>').
	     t('<li>In the default display, add an argument on the node reference field. Select <em>Provide default argument</em> and use <em>Node ID from URL</em>.</li>').
	     t('<li>Add a <em>Block</em> display and set its name to <em>View Block</em> and its title to <em>Employees</em>.</li>').
	     t('<li>Add another <em>Block</em> display and set its name to <em>Edit Block</em> and its title to <en>Employees</em>.</li>').
	     t('<li>For the edit block set within <em>Basic Settings</em> the <em>Style</em> to <em>Editview</em>. Remember to use the Override button. Set <em>Node Type</em> to <em>Employee</em>.</li>').
	     t('<li>For both blocks, set within <em>Basic Settings</em> the <em>Access</em> to something sensible. Remember to use the Override button.</li>').
	     '</ol>'.
	     t('<li>Go to the blocks admin page and enable both blocks:').
	     t('<ol><li>Select a region for the view block. Configure it and select <em>Show if the following PHP code returns TRUE</em> with the following code:').
	     '<pre>&lt;?php
  if (arg(0) == \'node\' && is_numeric(arg(1)) && arg(2) == \'\ {
    $node = node_load(arg(1));
    if ($node-&gt;type == \'organisation\') {
      return true;
    }
  }
  return false;
  ?&gt;</pre>'.
	     t('This will only enable the block if you are viewing an organisation.</li>').
	     t('<li>Select a region for the edit block. Configure it and select <em>Show if the following PHP code returns TRUE</em> with the following code:').
	     '<pre>&lt;?php
  if (arg(0) == \'node\' && is_numeric(arg(1)) && arg(2) == \'edit\') {
    $node = node_load(arg(1));
    if ($node-&gt;type == \'organisation\') {
      return true;
    }
  }
  return false;
?&gt;</pre>'.
	     t('This will only enable the block if you are editing an organisation.</li></ol></li></ol>').
             t('<p>Now when you go to an organisation node, you will see a table listing all of that organisation\'s employees, and when you click on its <em>edit</em> tab, the list of employees will become editable as well.</p>');
  }
}


/**
 * Implementation of hook_menu().
 */
function editview_menu() {
  $items['editview_js'] = array(
    'page callback' => 'editview_js',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
    'file' => 'includes/editview.views.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function editview_theme() {
  return array(
    'editview_node_form' => array(
      'arguments' => array('form' => NULL),
      'file' => 'editview.theme.inc',
      'path' => drupal_get_path('module', 'editview') .'/theme',
    ),
  );
}

/**
 * Implementation of hook_forms().
 */
function editview_forms($form_id, $args) {
  $forms = array();
  if (!strncmp($form_id, 'editview_node_form_', 19)) {
    $forms[$form_id] = array(
      'callback' => 'editview_node_form',
    );
  }
  return $forms;
}

/**
 * Proxy for the editview_form_after_build function. The include file will
 * in some cases not be loaded.
 */
function editview_form_after_build_proxy($form, &$form_state) {
  module_load_include('inc', 'editview', 'includes/editview.views');
  return editview_form_after_build($form, $form_state);
}


/**
 * @} End of "addtogroup editview".
 */


