Add UI for adding entities to plans
[project/deploy.git] / modules / deploy_managed_ui / deploy_managed_ui.module
1 <?php
2
3 /**
4 * @file
5 * Deploy Managed UI module functions.
6 */
7
8 /**
9 * Implements hook_permission().
10 */
11 function deploy_managed_ui_permission() {
12 return array(
13 'access managed aggregator ui' => array(
14 'title' => t('Access Managed Aggregator UI'),
15 'description' => t('Add items to the managed deployment aggregator when editing entities.'),
16 ),
17 );
18 }
19
20 /**
21 * Implements hook form_alter().
22 */
23 function deploy_managed_ui_form_alter(&$form, &$form_state, $form_id) {
24
25 if (!empty($form['#node_edit_form'])) {
26 deploy_managed_ui_form_elements($form, 'deploy_managed_ui_node_form_submit', TRUE);
27 }
28
29 // For the rest we only support edit forms following the Entity API convention.
30 if (!empty($form['#entity_type'])) {
31 $entity_type = $form['#entity_type'];
32 if (isset($form[$entity_type]) && is_object($form[$entity_type]['#value'])) {
33 deploy_managed_ui_form_elements($form, 'deploy_managed_ui_entity_form_submit');
34 }
35 }
36 }
37
38 /**
39 * Generates form elements for hook_form_alter() implementations to call.
40 */
41 function deploy_managed_ui_form_elements(&$form, $submit_handler, $vertical_tabs = FALSE) {
42
43 $weight = 999;
44 if ($vertical_tabs) {
45 $weight = -999;
46 }
47
48 if (!user_access('access managed aggregator ui')) {
49 return;
50 }
51
52 $plans = deploy_manager_plan_get_options();
53 if (!count($plans)) {
54 return;
55 }
56
57 $form['deploy_managed_ui'] = array(
58 '#type' => 'fieldset',
59 '#title' => t('Deployment plans'),
60 '#collapsible' => TRUE,
61 '#collapsed' => FALSE,
62 '#group' => 'additional_settings',
63 /*
64 // Skipping fancy JS for now.
65 '#attached' => array(
66 'js' => array(
67 'vertical-tabs' => drupal_get_path('module', 'deploy_managed_ui') . '/deploy_managed_ui.js',
68 ),
69 ),
70 */
71 '#tree' => TRUE,
72 '#weight' => $weight,
73 );
74
75 $form['deploy_managed_ui']['text'] = array(
76 '#markup' => t('Select the plans to include these chanages in.'),
77 );
78
79 $form['deploy_managed_ui']['plans'] = array(
80 '#type' => 'checkboxes',
81 '#title' => t('Plans'),
82 '#options' => $plans,
83 );
84
85 if (!$vertical_tabs) {
86 $form['actions']['#weight'] = ($weight + 1);
87 }
88
89 $form['#submit'][] = $submit_handler;
90 }
91
92 /**
93 * Submit handler for node forms supporting managed UI.
94 */
95 function deploy_managed_ui_node_form_submit(&$form, &$form_state) {
96 $form_state['node']->___deploy_plans = array();
97 foreach ($form_state['values']['deploy_managed_ui']['plans'] as $plan => $checked) {
98 if ($checked) {
99 $form_state['node']->___deploy_plans[$plan] = $plan;
100 }
101 }
102 }
103
104 /**
105 * Submit handler for entity forms supporting managed UI.
106 */
107 function deploy_managed_ui_entity_form_submit(&$form, &$form_state) {
108 $entity_type = $form['#entity_type'];
109 $entity = $form_state[$entity_type];
110 $form_state[$entity_type]->___deploy_plans = array();
111 foreach ($form_state['values']['deploy_managed_ui']['plans'] as $plan => $checked) {
112 if ($checked) {
113 $form_state[$entity_type]->___deploy_plans[$plan] = $plan;
114 }
115 }
116 }
117
118 /**
119 * Implements hook_entity_insert().
120 */
121 function deploy_managed_ui_entity_insert($entity, $type) {
122 _deploy_managed_ui_entity_save($entity, $type);
123 }
124
125 /**
126 * Implements hook_entity_update().
127 */
128 function deploy_managed_ui_entity_update($entity, $type) {
129 _deploy_managed_ui_entity_save($entity, $type);
130 }
131
132 /**
133 * Handled deployment plans attached to entities being saved.
134 *
135 * @param object
136 * $entity The entity being saved.
137 * @param string
138 * $entity_type The type of entity being saved.
139 */
140 function _deploy_managed_ui_entity_save($entity, $type) {
141 // Ignore all entities which don't have plans attached.
142 if (empty($entity->___deploy_plans)) {
143 return;
144 }
145
146 foreach ($entity->___deploy_plans as $plan) {
147 deploy_manager_add_to_plan($plan, $type, $entity);
148 }
149 }