You are here

organigrams.admin.inc in Organigrams 7

Defines the administration forms for managing organigrams.

File

organigrams.admin.inc
View source
<?php

/**
 * @file
 * Defines the administration forms for managing organigrams.
 */

/**
 * List all organigrams form.
 *
 * @param array $form
 *   Contains form elements.
 *
 * @return array
 *   Renderable form array.
 */
function organigrams_form_overview_organigrams($form) {

  // Retrieve all organigrams.
  $organigrams = organigrams_get_organigrams();

  // All form should be rendered as a tree.
  $form['#tree'] = TRUE;

  // Add items container.
  $form['items'] = array();

  // Retrieve organigrams count.
  $organigrams_count = count($organigrams);

  // Iterate through organigrams.
  foreach ($organigrams as $organigram) {

    // Retrieve the form element entry by reference.
    $organigram_element =& $form['items'][$organigram->oid];

    // Initialize the organigram element.
    $organigram_element = array();

    // Add the organigram as a property of the form element.
    $organigram_element['#organigram'] = $organigram;

    // Add organigram name as a markup element.
    $organigram_element['name'] = array(
      '#markup' => check_plain($organigram->name) . '<div class="description">[organigrams:' . $organigram->machine_name . ']</div>',
    );

    // Add organigram weight.
    $organigram_element['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @name', array(
        '@name' => $organigram->name,
      )),
      '#title_display' => 'invisible',
      '#delta' => $organigrams_count,
      '#default_value' => $organigram->weight,
    );

    // Add list, view, add, edit and delete links.
    $organigram_element['list'] = array(
      '#type' => 'link',
      '#title' => t('list items'),
      '#href' => "admin/structure/organigrams/{$organigram->machine_name}",
    );
    $organigram_element['view'] = array(
      '#type' => 'link',
      '#title' => t('view'),
      '#href' => "organigram/{$organigram->machine_name}",
    );
    $organigram_element['edit'] = array(
      '#type' => 'link',
      '#title' => t('edit'),
      '#href' => "admin/structure/organigrams/{$organigram->machine_name}/edit",
    );
    $organigram_element['delete'] = array(
      '#type' => 'link',
      '#title' => t('delete'),
      '#href' => "admin/structure/organigrams/{$organigram->machine_name}/delete",
    );
    $organigram_element['export'] = array(
      '#type' => 'link',
      '#title' => t('export'),
      '#href' => "admin/structure/organigrams/{$organigram->machine_name}/export/full",
    );
  }
  $form['#empty_text'] = t('No organigrams available. <a href="@link">Add organigram</a>.', array(
    '@link' => url('admin/structure/organigrams/add'),
  ));

  // Only make this form include a submit button and weight if more than one
  // organigram exists.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}

/**
 * Create or edit an organigram form.
 *
 * @param array $form
 *   The form being used to edit the node.
 * @param array $form_state
 *   The form state array.
 * @param array $edit
 *   The object to update.
 *
 * @return mixed
 *   Renderable array containing a form.
 */
function organigrams_form_organigrams($form, &$form_state, $edit = array()) {

  // During initial form build, add the entity to the form state for use
  // during form building and processing. During a rebuild, use what is in
  // the form state.
  if (!isset($form_state['organigram'])) {

    // Convert the edit variable to an object.
    $organigram = is_object($edit) ? $edit : (object) $edit;

    // Create array with default values.
    $default = array(
      'name' => '',
      'machine_name' => '',
      'description' => '',
      'status' => 1,
      'canvas_width' => 'auto',
      'canvas_height' => 'auto',
      'center' => 0,
      'fit' => 0,
      'node_width' => 120,
      'node_height' => 30,
      'top_radius' => 0,
      'bottom_radius' => 0,
      'shadow_offset' => 0,
      'horizontal_space' => 20,
      'vertical_space' => 20,
      'horizontal_offset' => 30,
      'line_color' => '',
      'border_color' => '',
      'border_color_hover' => '',
      'background_color' => '',
      'background_color_hover' => '',
      'font_color' => '',
      'font_color_hover' => '',
      'font_name' => '',
      'font_size' => '',
      'line_height' => '',
      'vertical_alignment' => 1,
      'weight' => 0,
    );

    // Iterate through default values.
    foreach ($default as $key => $value) {

      // If the property is missing.
      if (!isset($organigram->{$key})) {

        // Then add the default value.
        $organigram->{$key} = $value;
      }
    }

    // Assign organigram to the form state.
    $form_state['organigram'] = $organigram;
  }
  else {

    // Retrieve the organigram from the form state.
    $organigram = $form_state['organigram'];
  }

  // Add organigram to the form.
  $form['#organigram'] = $form_state['organigram'];

  // Load the extra fields information so set the correct order.
  $extrafields = field_info_extra_fields('organigrams', 'organigrams', 'form');
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('The name of the organigram is used as title.'),
    '#default_value' => $organigram->name,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => isset($extrafields['name']['weight']) ? $extrafields['name']['weight'] : 0,
  );
  $form['machine_name'] = array(
    '#type' => 'machine_name',
    '#default_value' => $organigram->machine_name,
    '#maxlength' => 255,
    '#machine_name' => array(
      'exists' => 'organigrams_machine_name_load',
    ),
  );
  $form['old_machine_name'] = array(
    '#type' => 'value',
    '#value' => $organigram->machine_name,
  );
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#description' => t('The description can used as an explanatory text.'),
    '#default_value' => $organigram->description,
    '#weight' => isset($extrafields['#description']['weight']) ? $extrafields['#description']['weight'] : 1,
  );
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Published'),
    '#description' => t('Unpublished organigrams will not be displayed on the website.<br />Note that users with the permission to edit this organigram will still be able to see this organigram.'),
    '#default_value' => $organigram->status,
    '#weight' => isset($extrafields['status']['weight']) ? $extrafields['status']['weight'] : 2,
  );

  // Canvas settings.
  $form['canvas'] = array(
    '#type' => 'fieldset',
    '#title' => t('organigram settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => isset($extrafields['organigram_settings']['weight']) ? $extrafields['organigram_settings']['weight'] : 3,
  );
  $form['canvas']['canvas_width'] = array(
    '#type' => 'textfield',
    '#title' => t('organigram width'),
    '#description' => t('Specify the width in pixels of the organigram.<br /><br />Other available options:<br /><br /><ul><li><b>auto</b>: Calculate the width by the number of items</li><li><b>parent</b>: Adjust the width to the parent element of the organigram.</li></ul>'),
    '#default_value' => $organigram->canvas_width,
  );
  $form['canvas']['canvas_height'] = array(
    '#type' => 'textfield',
    '#title' => t('organigram height'),
    '#description' => t('Specify the height in pixels of the organigram. The same options as width are available here.'),
    '#default_value' => $organigram->canvas_height,
  );
  $form['canvas']['center'] = array(
    '#type' => 'checkbox',
    '#title' => t('Center on page'),
    '#description' => t('Aligns the organigram on the center of the page.'),
    '#default_value' => $organigram->center,
  );
  $form['canvas']['fit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Fit in given width'),
    '#description' => t('Scale the organigram to fit in the given width.'),
    '#default_value' => $organigram->fit,
  );
  $form['canvas']['line_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Line color'),
    '#description' => t('Specify the connection lines color in this organigram.'),
    '#default_value' => $organigram->line_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );

  // Node settings.
  $form['node'] = array(
    '#type' => 'fieldset',
    '#title' => t('Item settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => isset($extrafields['item_settings']['weight']) ? $extrafields['item_settings']['weight'] : 4,
  );
  $form['node']['node_width'] = array(
    '#type' => 'textfield',
    '#title' => t('Item width'),
    '#description' => t('Specify the width in pixels of the items in this organigram.'),
    '#default_value' => $organigram->node_width,
  );
  $form['node']['node_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Item height'),
    '#description' => t('Specify the height in pixels of the items in this organigram.'),
    '#default_value' => $organigram->node_height,
  );
  $form['node']['top_radius'] = array(
    '#type' => 'textfield',
    '#title' => t('Top radius'),
    '#description' => t('Specify the radius of the top corners in pixels of the items in this organigram.'),
    '#default_value' => $organigram->top_radius,
  );
  $form['node']['bottom_radius'] = array(
    '#type' => 'textfield',
    '#title' => t('Bottom radius'),
    '#description' => t('Specify the radius of the bottom corners in pixels of the items in this organigram.'),
    '#default_value' => $organigram->bottom_radius,
  );
  $form['node']['shadow_offset'] = array(
    '#type' => 'textfield',
    '#title' => t('Shadow offset'),
    '#description' => t('Specify the shadow offset in pixels of the items in this organigram.'),
    '#default_value' => $organigram->shadow_offset,
  );
  $form['node']['horizontal_space'] = array(
    '#type' => 'textfield',
    '#title' => t('Horizontal space'),
    '#description' => t('Specify the horizontal space in pixels between the items in this organigram.'),
    '#default_value' => $organigram->horizontal_space,
  );
  $form['node']['vertical_space'] = array(
    '#type' => 'textfield',
    '#title' => t('Vertical space'),
    '#description' => t('Specify the vertical space in pixels between the items in this organigram.'),
    '#default_value' => $organigram->vertical_space,
  );
  $form['node']['horizontal_offset'] = array(
    '#type' => 'textfield',
    '#title' => t('Horizontal offset'),
    '#description' => t('Specify the horizontal offset in pixels between the l and r items in this organigram.'),
    '#default_value' => $organigram->horizontal_offset,
  );
  $form['node']['border_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Border color'),
    '#description' => t('Specify the border color of the items in this organigram.'),
    '#default_value' => $organigram->border_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['node']['border_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Border color hover'),
    '#description' => t('Specify the border color of the items in this organigram on hover if they have a URL.'),
    '#default_value' => $organigram->border_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['node']['background_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Background color'),
    '#description' => t('Specify the background color of the items in this organigram.'),
    '#default_value' => $organigram->background_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['node']['background_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Background color hover'),
    '#description' => t('Specify the background color of the items in this organigram on hover if they have a URL.'),
    '#default_value' => $organigram->background_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );

  // Font settings.
  $form['font'] = array(
    '#type' => 'fieldset',
    '#title' => t('Font settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => isset($extrafields['font_settings']['weight']) ? $extrafields['font_settings']['weight'] : 5,
  );
  $form['font']['font_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Font color'),
    '#description' => t('Specify the font color of the items in this organigram.'),
    '#default_value' => $organigram->font_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['font']['font_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Font color hover'),
    '#description' => t('Specify the font color of the items in this organigram on hover if they have a URL.'),
    '#default_value' => $organigram->font_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['font']['font_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Font name'),
    '#description' => t('Specify the font name of the items in this organigram.'),
    '#default_value' => $organigram->font_name,
  );
  $form['font']['font_size'] = array(
    '#type' => 'textfield',
    '#title' => t('Font size'),
    '#description' => t('Specify the font size in pixels of the items in this organigram.'),
    '#default_value' => $organigram->font_size,
  );
  $form['font']['line_height'] = array(
    '#type' => 'textfield',
    '#title' => t('Line height'),
    '#description' => t('Specify the line height in pixels for the text in the items in this organigram.'),
    '#default_value' => $organigram->line_height,
  );
  $form['font']['vertical_alignment'] = array(
    '#type' => 'select',
    '#title' => t('Vertical text alignment'),
    '#description' => t('Specify the vertical text alignment of the items in this organigram.'),
    '#options' => array(
      0 => t('Top'),
      1 => t('Center'),
    ),
    '#default_value' => $organigram->vertical_alignment,
  );

  // Add supported actions for this form.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Attach the ColorPicker library if it exists.
  $form['#attached']['libraries_load'][] = array(
    'colorpicker',
  );
  $library = libraries_detect('colorpicker');
  if (!empty($library['installed'])) {
    $form['#attached']['js'][] = drupal_get_path('module', 'organigrams') . '/js/colorpicker.js';
  }

  // Add fields to the form.
  field_attach_form('organigrams', $organigram, $form, $form_state);

  // Return the generated form.
  return $form;
}

/**
 * Confirm organigram deletion form.
 *
 * @param array $form
 *   The form being used to edit the node.
 * @param array $form_state
 *   The form state array.
 * @param object $organigram
 *   The object to delete.
 *
 * @return mixed
 *   Renderable array containing a form.
 */
function organigrams_form_confirm_delete_organigrams($form, &$form_state, $organigram) {

  // Always provide entity id in the same form key as in the entity edit form.
  $form['oid'] = array(
    '#type' => 'value',
    '#value' => $organigram->oid,
  );

  // Add organigram to the form.
  $form['#organigram'] = $organigram;
  $form['type'] = array(
    '#type' => 'value',
    '#value' => 'organigram',
  );
  $form['name'] = array(
    '#type' => 'value',
    '#value' => $organigram->name,
  );
  return confirm_form($form, t('Are you sure you want to delete the organigram %name?', array(
    '%name' => $organigram->name,
  )), 'admin/structure/organigrams', t('Deleting an organigram will delete all the organigram items in it. This action cannot be undone.'), t('Delete'), t('Cancel'));
}

/**
 * Form validation handler for 'organigrams_form_organigrams'.
 */
function organigrams_form_organigrams_validate($form, &$form_state) {

  // During the deletion there is no 'machine_name' key.
  if (isset($form_state['values']['machine_name'])) {

    // Do not allow machine names to conflict with organigrams path arguments.
    $machine_name = $form_state['values']['machine_name'];

    // Invalid machine names which will conflict with the organigrams path
    // arguments.
    $disallowed = array(
      'add',
      'list',
    );

    // Check if the machine name has on of the disallowed values.
    if (in_array($machine_name, $disallowed)) {

      // Raise form error.
      form_set_error('machine_name', t('The machine-readable name cannot be "add" or "list".'));
    }
  }

  // Allow the entity fields to be validated.
  entity_form_field_validate('organigrams', $form, $form_state);
}

/*
 * Form submit handlers.
 */

/**
 * Form submit handler for 'organigrams_form_overview_organigrams'.
 */
function organigrams_form_overview_organigrams_submit($form, &$form_state) {

  // Check if the form state contains any items.
  if (!empty($form_state['values']['items'])) {

    // Iterate through the form state values.
    foreach ($form_state['values']['items'] as $oid => $organigram) {

      // Retrieve the form element by reference.
      $organigram =& $form['items'][$oid]['#organigram'];

      // Retrieve the form state weight.
      $weight = $form_state['values']['items'][$oid]['weight'];

      // Validate the element name and weight difference.
      if ($organigram->weight != $weight) {

        // Save the new weight.
        $organigram->weight = $weight;

        // Save changes to the DB.
        organigrams_save($organigram);
      }
    }
  }
}

/**
 * Form submit handler for 'organigrams_form_organigrams'.
 */
function organigrams_form_organigrams_submit($form, &$form_state) {

  // Retrieve the organigram.
  $organigram = $form_state['organigram'];

  // Submit the organigrams entity.
  entity_form_submit_build_entity('organigrams', $organigram, $form, $form_state);

  // Evaluate the save status.
  switch (organigrams_save($organigram)) {
    case SAVED_NEW:
      drupal_set_message(t('Created new organigram %name.', array(
        '%name' => $organigram->name,
      )));
      watchdog('organigrams', 'Created new organigram %name.', array(
        '%name' => $organigram->name,
      ), WATCHDOG_NOTICE, l(t('edit'), "admin/structure/organigrams/{$organigram->machine_name}/edit"));
      break;
    case SAVED_UPDATED:
      drupal_set_message(t('Updated organigram %name.', array(
        '%name' => $organigram->name,
      )));
      watchdog('organigrams', 'Updated organigram %name.', array(
        '%name' => $organigram->name,
      ), WATCHDOG_NOTICE, l(t('edit'), "admin/structure/organigrams/{$organigram->machine_name}/edit"));
      break;
  }

  // Add organigram information to the form state.
  $form_state['values']['oid'] = $organigram->oid;
  $form_state['oid'] = $organigram->oid;

  // Redirect to the 'admin/structure/organigrams' page.
  $form_state['redirect'] = 'admin/structure/organigrams';
}

/**
 * Form submit handler for 'organigrams_form_confirm_delete_organigrams'.
 */
function organigrams_form_confirm_delete_organigrams_submit($form, &$form_state) {

  // Delete the organigram.
  organigrams_delete($form_state['values']['oid']);
  drupal_set_message(t('Deleted organigram %name', array(
    '%name' => $form_state['values']['name'],
  )));
  watchdog('organigrams', 'Deleted organigram %name', array(
    '%name' => $form_state['values']['name'],
  ), WATCHDOG_NOTICE);
  $form_state['redirect'] = 'admin/structure/organigrams';

  // Clear the page and block caches to avoid stale data.
  cache_clear_all();
}

/**
 * Admin menu callback to export all items from an organigram.
 *
 * @param array $form
 *   The form being used to export the items.
 * @param array $form_state
 *   The form state array.
 * @param array $organigram
 *   The organigram object to export.
 *
 * @return mixed
 *   Renderable array containing a form.
 */
function organigrams_form_export($form, &$form_state, $organigram) {

  // Show not found page if no argument is given.
  if (empty($organigram)) {
    drupal_not_found();
    drupal_exit();
  }

  // Load the organigram if only a machine name is given.
  if (is_string($organigram)) {
    $organigram = organigrams_machine_name_load($organigram);
  }

  // If no organigram is found, show not found page.
  if (!is_object($organigram)) {
    drupal_not_found();
    drupal_exit();
  }

  // Decide which export type should be shown by default.
  $type = '';
  $args = arg();
  if (array_pop($args) == 'full') {
    $type = 'full';
  }

  // Load all items fully loaded from the organigram.
  $tree = organigrams_get_tree($organigram->oid, 0, NULL, TRUE);

  // Encode it.
  $tree_json = json_encode($tree);

  // Encode the organigram data.
  $organigram_json = json_encode(array(
    'organigram' => $organigram,
    'items' => $tree,
  ));
  $form['export_type'] = array(
    '#type' => 'select',
    '#title' => t('What do you want to export?'),
    '#description' => t('Select if you want to export the full organigram or only its items.'),
    '#options' => array(
      t('Full organigram'),
      t('Only the organigram items'),
    ),
    '#default_value' => $type == 'full' ? 0 : 1,
  );

  // Display the data in a textarea.
  $form['organigram_export'] = array(
    '#type' => 'textarea',
    '#title' => t('Organigram export'),
    '#description' => t('Copy the contents of this textarea and paste it in the import form of another organigram.'),
    '#value' => $organigram_json,
    '#states' => array(
      'visible' => array(
        ':input[name=export_type]' => array(
          'value' => 0,
        ),
      ),
    ),
  );

  // Display the data in a textarea.
  $form['organigram_items_export'] = array(
    '#type' => 'textarea',
    '#title' => t('Organigram items export'),
    '#description' => t('Copy the contents of this textarea and paste it in the import form of another organigram.'),
    '#value' => $tree_json,
    '#states' => array(
      'visible' => array(
        ':input[name=export_type]' => array(
          'value' => 1,
        ),
      ),
    ),
  );
  return $form;
}

/**
 * Admin menu callback to import all items into an organigram.
 *
 * @param array $form
 *   The form being used to import the items.
 * @param array $form_state
 *   The form state array.
 * @param array $organigram
 *   The organigram object to import the items in.
 *
 * @return mixed
 *   Renderable array containing a form.
 */
function organigrams_form_import_items($form, &$form_state, $organigram) {

  // Show not found page if no argument is given.
  if (empty($organigram)) {
    drupal_not_found();
    drupal_exit();
  }

  // Load the organigram if only a machine name is given.
  if (is_string($organigram)) {
    $organigram = organigrams_machine_name_load($organigram);
  }

  // If no organigram is found, show not found page.
  if (!is_object($organigram)) {
    drupal_not_found();
    drupal_exit();
  }

  // Add the organigram to the form.
  $form['#organigram'] = $organigram;

  // Load the import field if confirm_import is not yet set.
  if (empty($form_state['confirm_import'])) {

    // A textarea for the import code.
    $form['import'] = array(
      '#type' => 'textarea',
      '#title' => t('Items import'),
      '#description' => t('Paste the contents of an organigram export in this textarea.'),
    );

    // Add actions.
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Start import'),
    );
  }
  else {
    $form['import'] = array(
      '#type' => 'value',
      '#value' => $form_state['values']['import'],
    );
    $form = confirm_form($form, t('Are you sure you want to import items to organigram %name?', array(
      '%name' => $organigram->name,
    )), "admin/structure/organigrams/{$organigram->machine_name}", t('Importing new items in an organigram will delete all existing items. This action cannot be undone.'), t('Import items'), t('Cancel'));
  }
  return $form;
}

/**
 * Validate function for organigrams_form_import_items.
 */
function organigrams_form_import_items_validate($form, &$form_state) {

  // Make sure we have an organigram.
  if (empty($form['#organigram'])) {
    form_set_error('import', t('No organigram found to import to.'));
  }

  // Also check if we have any import data.
  if (empty($form_state['values']['import'])) {
    form_set_error('import', t('No import data found.'));
  }

  // Decode the data.
  $import_data = json_decode($form_state['values']['import']);

  // Check if the data is valid.
  if (empty($import_data)) {
    form_set_error('import', t('No valid import data found.'));
  }
}

/**
 * Submit function for organigrams_form_import_items.
 */
function organigrams_form_import_items_submit($form, &$form_state) {

  // Only add a new key and rebuild the form when the import data is submitted.
  if (empty($form_state['confirm_import'])) {
    $form_state['confirm_import'] = TRUE;
    $form_state['rebuild'] = TRUE;
  }
  else {

    // If the import is confirmed, execute the actual import.
    $status = organigrams_import_items($form['#organigram']->oid, json_decode($form_state['values']['import']));

    // Let the user know if the imported succeeded.
    if ($status === TRUE) {
      drupal_set_message(t('Successfully imported new items.'));

      // Redirect to the item overview.
      drupal_goto('admin/structure/organigrams/' . $form['#organigram']->machine_name);
    }
    else {
      drupal_set_message($status, 'error');
    }
  }
}

/**
 * Admin menu callback to import an organigram.
 *
 * @param array $form
 *   The form being used to import the items.
 * @param array $form_state
 *   The form state array.
 *
 * @return mixed
 *   Renderable array containing a form.
 */
function organigrams_form_import($form, &$form_state) {

  // A textarea for the import code.
  $form['import'] = array(
    '#type' => 'textarea',
    '#title' => t('Organigram import'),
    '#description' => t('Paste the contents of an organigram export in this textarea.'),
  );

  // Add actions.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Start import'),
  );
  return $form;
}

/**
 * Validate function for organigrams_form_import.
 */
function organigrams_form_import_validate($form, &$form_state) {

  // Also check if we have any import data.
  if (empty($form_state['values']['import'])) {
    form_set_error('import', t('No import data found.'));
  }
  $data = json_decode($form_state['values']['import']);
  if (empty($data->organigram)) {
    form_set_error('import', t('No organigram found.'));
  }
}

/**
 * Submit function for organigrams_form_import.
 */
function organigrams_form_import_submit($form, &$form_state) {
  $data = json_decode($form_state['values']['import']);
  $organigram = $data->organigram;

  // Remove the original organigram ID.
  unset($organigram->oid);

  // Get a unique machine name.
  $organigram->machine_name = organigrams_check_machine_name($organigram->machine_name);

  // Save the organigram.
  organigrams_save($organigram);

  // Show an error if the organigram save failed.
  if (empty($organigram->oid)) {
    drupal_set_message(t('Failed importing the organigram. Please make sure you copied the whole export script.'), 'error');
  }
  else {

    // Show a success message if the importing succeeded.
    drupal_set_message(t('Successfully imported organigram %name.', array(
      '%name' => $organigram->name,
    )));

    // Check if there are items to import.
    if (!empty($data->items)) {

      // Import the items.
      $status = organigrams_import_items($organigram->oid, $data->items);

      // Show an additional error message if the items import failed.
      if ($status !== TRUE) {
        drupal_set_message($status, 'error');
      }
    }

    // Redirect to the new organigram.
    drupal_goto('admin/structure/organigrams/' . $organigram->machine_name);
  }
}

Functions

Namesort descending Description
organigrams_form_confirm_delete_organigrams Confirm organigram deletion form.
organigrams_form_confirm_delete_organigrams_submit Form submit handler for 'organigrams_form_confirm_delete_organigrams'.
organigrams_form_export Admin menu callback to export all items from an organigram.
organigrams_form_import Admin menu callback to import an organigram.
organigrams_form_import_items Admin menu callback to import all items into an organigram.
organigrams_form_import_items_submit Submit function for organigrams_form_import_items.
organigrams_form_import_items_validate Validate function for organigrams_form_import_items.
organigrams_form_import_submit Submit function for organigrams_form_import.
organigrams_form_import_validate Validate function for organigrams_form_import.
organigrams_form_organigrams Create or edit an organigram form.
organigrams_form_organigrams_submit Form submit handler for 'organigrams_form_organigrams'.
organigrams_form_organigrams_validate Form validation handler for 'organigrams_form_organigrams'.
organigrams_form_overview_organigrams List all organigrams form.
organigrams_form_overview_organigrams_submit Form submit handler for 'organigrams_form_overview_organigrams'.