You are here

function organigrams_form_organigrams_item in Organigrams 7

Create or edit form for an organigrams item.

Parameters

array $form: The form being used to edit the node.

array $form_state: The form state array.

array $edit: The object to edit.

mixed $organigram: The parent organigram entity.

Return value

mixed Renderable array containing a form.

1 string reference to 'organigrams_form_organigrams_item'
organigrams_menu in ./organigrams.module
Implements hook_menu().

File

./organigrams_item.admin.inc, line 112
Defines the user forms and pages for managing organigrams items.

Code

function organigrams_form_organigrams_item($form, &$form_state, $edit = array(), $organigram = NULL) {

  // During initial form build, add the organigrams item 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['organigrams_item'])) {

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

    // If the organigram argument is not set and the organigrams item has an
    // oid.
    if (!isset($organigram) && isset($organigrams_item->oid)) {

      // Load the organigram.
      $organigram = organigrams_load($organigrams_item->oid);
    }

    // Create default values array.
    $defaults = array(
      'iid' => NULL,
      'oid' => isset($organigram) ? $organigram->oid : NULL,
      'name' => '',
      'description' => '',
      'organigrams_machine_name' => isset($organigram) ? $organigram->machine_name : NULL,
      'position' => 'u',
      'url' => '',
      'border_color' => '',
      'border_color_hover' => '',
      'background_color' => '',
      'background_color_hover' => '',
      'font_color' => '',
      'font_color_hover' => '',
      'bold_border' => 0,
      'image_url' => '',
      'image_alignment' => '',
      'parent' => 0,
      'weight' => 0,
    );

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

      // If the property is not set then add the default value.
      if (!isset($organigrams_item->{$key})) {
        $organigrams_item->{$key} = $value;
      }
    }

    // Save the organigrams item to the form state.
    $form_state['organigrams_item'] = $organigrams_item;
  }
  else {

    // Retrieve the organigrams item from the form state.
    $organigrams_item = $form_state['organigrams_item'];
  }

  // Retrieve parent for the current organigrams item.
  $parent = organigrams_item_get_parent($organigrams_item->iid);

  // Build the form.
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('The content of the organigram item. Use <b>|</b> for line breaks.'),
    '#default_value' => $organigrams_item->name,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#weight' => -5,
  );
  $form['organigrams_machine_name'] = array(
    '#type' => 'value',
    '#value' => isset($organigrams_item->organigrams_machine_name) ? $organigrams_item->organigrams_machine_name : $organigram->machine_name,
  );

  // Create default options array and merge with suitable parents.
  $parent_relation_options = array(
    0 => t('<root>'),
  ) + organigrams_item_get_suitable_parents_options($organigrams_item->iid, $organigram->oid);
  $form['parent'] = array(
    '#type' => 'select',
    '#title' => t('Parent item'),
    '#description' => t('The parent item defines the hierarchical place within the organigram for the current item. An organigram item which is connected to the "<root>" item will be at the top of the organigram.'),
    '#options' => $parent_relation_options,
    '#default_value' => isset($parent) ? $parent->iid : NULL,
    '#weight' => -4,
  );
  $form['position'] = array(
    '#type' => 'select',
    '#title' => t('Position'),
    '#description' => t('The position of the organigram item (will be ignored on root items).'),
    '#options' => array(
      'u' => t('Below the parent'),
      'l' => t('Staff function positioned on the left'),
      'r' => t('Staff function positioned on the right'),
    ),
    '#default_value' => $organigrams_item->position,
    '#weight' => -3,
  );
  $form['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight'),
    '#description' => t('organigram items are displayed in ascending order by weight.'),
    '#delta' => count($parent_relation_options),
    '#default_value' => $organigrams_item->weight,
    '#weight' => -2,
  );

  // Options.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Options'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#weight' => -1,
  );
  $form['options']['url'] = array(
    '#type' => 'textfield',
    '#title' => t('URL'),
    '#description' => t('Add a link to this URL to the organigram item.<br />If the URL starts with <b>http://</b> or <b>https://</b>, it will be opened in a new window.'),
    '#default_value' => $organigrams_item->url,
  );
  $form['options']['border_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Border color'),
    '#description' => t('The border color of the organigram item.'),
    '#default_value' => $organigrams_item->border_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['border_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Border color hover'),
    '#description' => t('The border color of the organigram item on hover if it has a URL.'),
    '#default_value' => $organigrams_item->border_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['background_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Background color'),
    '#description' => t('The background color of the organigram item.'),
    '#default_value' => $organigrams_item->background_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['background_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Background color hover'),
    '#description' => t('The background color of the organigram item on hover if it has a URL.'),
    '#default_value' => $organigrams_item->background_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['font_color'] = array(
    '#type' => 'textfield',
    '#title' => t('Font color'),
    '#description' => t('The font color of the organigram item.'),
    '#default_value' => $organigrams_item->font_color,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['font_color_hover'] = array(
    '#type' => 'textfield',
    '#title' => t('Font color hover'),
    '#description' => t('The font color of the organigram item on hover if it has a URL.'),
    '#default_value' => $organigrams_item->font_color_hover,
    '#attributes' => array(
      'class' => array(
        'colorpicker',
      ),
    ),
  );
  $form['options']['bold_border'] = array(
    '#type' => 'checkbox',
    '#title' => t('Bold border'),
    '#description' => t('Emphasize the organigram item with an extra border.'),
    '#default_value' => $organigrams_item->bold_border,
  );

  // @TODO: Use managed file here.
  $form['options']['image_url'] = array(
    '#type' => 'textfield',
    '#title' => t('Image URL'),
    '#description' => t('The image URL of an image to add to the organigram item.'),
    '#default_value' => $organigrams_item->image_url,
  );
  $form['options']['image_alignment'] = array(
    '#type' => 'select',
    '#title' => t('Image alignment'),
    '#description' => t('The alignment of the image in the organigram item.'),
    '#options' => array(
      'lt' => t('Topleft'),
      'ct' => t('Topcenter'),
      'rt' => t('Topright'),
      'lm' => t('Centerleft'),
      'cm' => t('Center'),
      'rm' => t('Centerright'),
      'lb' => t('Bottomleft'),
      'cb' => t('Bottomcenter'),
      'rb' => t('Bottomright'),
    ),
    '#default_value' => $organigrams_item->image_alignment,
  );
  $form['oid'] = array(
    '#type' => 'value',
    '#value' => $organigram->oid,
  );
  $form['iid'] = array(
    '#type' => 'value',
    '#value' => $organigrams_item->iid,
  );

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

  // Redirect the this page.
  $form_state['redirect'] = $_GET['q'];

  // 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';
  }

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