summaryrefslogtreecommitdiffstats
path: root/modules/deploy_managed_ui/deploy_managed_ui.module
blob: 014b5b61f362da4fdd13eb8ecfaf6e025f26dbf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

/**
 * @file
 * Deploy Managed UI module functions.
 */

/**
 * Implements hook_permission().
 */
function deploy_managed_ui_permission() {
  return array(
    'access managed aggregator ui' => array(
      'title' => t('Access Managed Aggregator UI'),
      'description' => t('Add items to the managed deployment aggregator when editing entities.'),
    ),
  );
}

/**
 * Implements hook form_alter().
 */
function deploy_managed_ui_form_alter(&$form, &$form_state, $form_id) {

  if (!empty($form['#node_edit_form'])) {
    deploy_managed_ui_form_elements($form, 'deploy_managed_ui_node_form_submit', TRUE);
  }

  // For the rest we only support edit forms following the Entity API convention.
  if (!empty($form['#entity_type'])) {
    $entity_type = $form['#entity_type'];
    if (isset($form[$entity_type]) && is_object($form[$entity_type]['#value'])) {
      deploy_managed_ui_form_elements($form, 'deploy_managed_ui_entity_form_submit');
    }
  }
}

/**
 * Generates form elements for hook_form_alter() implementations to call.
 */
function deploy_managed_ui_form_elements(&$form, $submit_handler, $vertical_tabs = FALSE) {

  $weight = 999;
  if ($vertical_tabs) {
    $weight = -999;
  }

  if (!user_access('access managed aggregator ui')) {
    return;
  }

  $plans = deploy_manager_plan_get_options();
  if (!count($plans)) {
    return;
  }

  $form['deploy_managed_ui'] = array(
    '#type' => 'fieldset',
    '#title' => t('Deployment plans'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
/*
    // Skipping fancy JS for now.
    '#attached' => array(
      'js' => array(
        'vertical-tabs' => drupal_get_path('module', 'deploy_managed_ui') . '/deploy_managed_ui.js',
      ),
    ),
*/
    '#tree' => TRUE, 
    '#weight' => $weight,
  );

  $form['deploy_managed_ui']['text'] = array(
    '#markup' => t('Select the plans to include these chanages in.'),
  );

  $form['deploy_managed_ui']['plans'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Plans'),
    '#options' => $plans,
  );

  if (!$vertical_tabs) {
    $form['actions']['#weight'] = ($weight + 1);
  }

  $form['#submit'][] = $submit_handler;
}

/**
 * Submit handler for node forms supporting managed UI.
 */
function deploy_managed_ui_node_form_submit(&$form, &$form_state) {
  $form_state['node']->___deploy_plans = array();
  foreach ($form_state['values']['deploy_managed_ui']['plans'] as $plan => $checked) {
    if ($checked) {
      $form_state['node']->___deploy_plans[$plan] = $plan;
    }
  }
}

/**
 * Submit handler for entity forms supporting managed UI.
 */
function deploy_managed_ui_entity_form_submit(&$form, &$form_state) {
  $entity_type = $form['#entity_type'];
  $entity = $form_state[$entity_type];
  $form_state[$entity_type]->___deploy_plans = array();
  foreach ($form_state['values']['deploy_managed_ui']['plans'] as $plan => $checked) {
    if ($checked) {
      $form_state[$entity_type]->___deploy_plans[$plan] = $plan;
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function deploy_managed_ui_entity_insert($entity, $type) {
  _deploy_managed_ui_entity_save($entity, $type);
}

/**
 * Implements hook_entity_update().
 */
function deploy_managed_ui_entity_update($entity, $type) {
  _deploy_managed_ui_entity_save($entity, $type);
}

/**
 * Handled deployment plans attached to entities being saved.
 *
 * @param object
 *   $entity The entity being saved.
 * @param string
 *   $entity_type The type of entity being saved.
 */
function _deploy_managed_ui_entity_save($entity, $type) {
  // Ignore all entities which don't have plans attached.
  if (empty($entity->___deploy_plans)) {
    return;
  }

  foreach ($entity->___deploy_plans as $plan) {
    deploy_manager_add_to_plan($plan, $type, $entity);
  }
}