-
Notifications
You must be signed in to change notification settings - Fork 0
/
features_direct_save.module
245 lines (233 loc) · 8.76 KB
/
features_direct_save.module
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
/**
* @file
* features_direct_save module
*/
/**
* Implementation of hook_menu().
*
* @return An array of menu items.
*/
function features_direct_save_menu() {
$items = array();
$items['unlink'] = array(
'title' => 'unlink test',
'page callback' => 'drupal_get_form',
'page arguments' => array('features_direct_save_unlink_form'),
'access arguments' => array('access administration pages'),
);
return $items;
}
/**
* Implementation of hook_init().
*/
function features_direct_save_init() {
// $files = drupal_system_listing('\.unlink.inc$', 'modules', 'name', 0);
//dpm($view);
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function features_direct_save_form_features_export_form_alter(&$form, &$form_state) {
//TODO: add backup options.
$form['buttons']['create'] = array(
'#type' => 'submit',
'#value' => t('Auto create feature'),
'#weight' => 10,
'#submit' => array('features_direct_save_export_build_form_submit'),
);
$form['features_direct_save_dest'] = array(
'#title' => t('Custom module path.'),
'#type' => 'textfield',
'#default_value' => $form['#feature']->name ? drupal_get_path('module', $form['#feature']->name) : 'sites/all/modules',
);
if ($form['#feature']) {
$form['buttons']['safe_recreate'] = array(
'#type' => 'submit',
'#value' => t('Safe Auto create feature'),
'#weight' => 10,
'#submit' => array('features_direct_save_form_features_export_form_safe_submit'),
);
$form['#validate'][] = 'features_direct_save_form_features_export_form_validate';
}
}
function features_direct_save_form_features_export_form_safe_submit($form, &$form_state) {
// dpm($form);dpm($form_state);
module_load_include('inc', 'features', 'features.export');
features_include();
if ($form_state['clicked_button']['#id'] == 'edit-safe-recreate') {
$destination = $form_state['values']['features_direct_save_dest'];
$arr = explode('/', $destination);
$arr[sizeof($arr) - 1].= '_unlink';
$module_name = $arr[sizeof($arr) - 1];
$destination = implode('/', $arr) . '/';
$hooks = _features_direct_save_get_hooks();
$new_feature = $form_state['values']['sources'];
$old_feature = $form['#feature']->info['features'];
$stub = array();
foreach ($hooks as $hook) {
if (isset($old_feature[$hook])) {
foreach ($old_feature[$hook] as $value) {
if (!$new_feature[$hook][$value]) {
$stub[$hook][] = $value;
}
}
}
}
if ($stub) {
$code_buffer = '';
$file_content = array();
foreach ($stub as $hook => $value) {
$output = array();
foreach ($value as $component) {
switch ($hook) {
case 'views':
$view = views_get_view($component);
$output[] = $view->export();
$output[] = '$view->save();';
break;
case 'box':
$export = array('features' => array('box' => array($component => $component)));
$component_hooks = features_export_render_hooks($export, $module_name, TRUE);
$output[] = str_replace(' return $export;', '', $component_hooks['box']['default_box']);
$output[] = 'drupal_write_record(' . "'box'" . ', $box);';
break;
}
}
$code_buffer .= implode("\n", $output);
$output = array();
$output[] = "/**";
$output[] = " * Unlink {$module_name}_unlink_{$hook}().";
$output[] = " */";
$output[] = "function {$module_name}_unlink_{$hook}() {";
$output[] = $code_buffer;
$output[] = "}";
$file_content[0] .= implode("\n", $output);
}
$file_content = "<?php\n\n". implode("\n\n", $file_content) ."\n";
if (!file_exists($destination)) {
mkdir($destination, 0777, TRUE);
}
$error = '';
if (file_put_contents("{$destination}/$module_name.unlink.inc", $file_content) === FALSE) {
$error = TRUE;
}
if ($error) {
drupal_set_message(t('One or more files cannot be writing to the file system this is probably a permission issue. @see http://drupal.org/project/features_direct_save for more info'));
}
else {
features_direct_save_export_build_form_submit($form, $form_state);
drupal_set_message(t('The feature was safely and successfully created.'));
}
}
}
}
/**
* submit function for the features_export_form, write the file to disk.
*/
function features_direct_save_export_build_form_submit($form, &$form_state) {
module_load_include('inc', 'features', 'features.export');
features_include();
// Assemble the combined component list.
$stub = array();
$components = array_keys(features_get_components());
foreach ($components as $component) {
// User-selected components take precedence.
if (!empty($form_state['values']['sources'][$component])) {
$stub[$component] = features_dom_decode_options(array_filter($form_state['values']['sources'][$component]));
}
// Only fallback to an existing feature's values if there are no export options for the component.
elseif (!empty($form['#feature']->info['features'][$component])) {
$stub[$component] = $form['#feature']->info['features'][$component];
}
}
// Generate populated feature.
$module_name = $form_state['values']['module_name'];
$export = features_populate($stub, $form_state['values']['sources']['dependencies'], $module_name);
// Directly copy the following attributes.
$attr = array('name', 'description');
foreach ($attr as $key) {
$export[$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : NULL;
}
// If either update status-related keys are provided, add a project key
// corresponding to the module name.
if (!empty($form_state['values']['version']) || !empty($form_state['values']['project_status_url'])) {
$export['project'] = $form_state['values']['module_name'];
}
if (!empty($form_state['values']['version'])) {
$export['version'] = $form_state['values']['version'];
}
if (!empty($form_state['values']['project_status_url'])) {
$export['project status url'] = $form_state['values']['project_status_url'];
}
// Generate download.
$directory = '';
$destination = $form_state['values']['features_direct_save_dest'];
if ($destination) {
$directory .= $destination . '/';
}
//TODO: find a better term then arg(3) to find if we are on create page or recreat.
if (arg(3) == 'create') {
$directory .= $module_name;
}
if ($files = features_export_render($export, $module_name, TRUE)) {
//If the directory dont exists create it.
if (!file_exists($directory)) {
mkdir($directory, 0777, TRUE);
}
else {
//If the directory exists delete all the files before recreation, to clean the dead files in case some elements removed from the feature.
foreach(glob($directory . '*.*') as $v){
unlink($v);
}
}
$error = false;
foreach ($files as $extension => $file_contents) {
if (!in_array($extension, array('module', 'info'))) {
$extension .= '.inc';
}
if (file_put_contents("{$directory}/{$module_name}.$extension", $file_contents) === FALSE) {
$error = true;
}
}
if ($error) {
drupal_set_message(t('One or more files cannot be writing to the file system this is probably a permission issue. @see http://drupal.org/project/features_direct_save for more info'));
}
else {
drupal_set_message(t('The feature was successfully created.'));
}
}
drupal_flush_all_caches();
}
function features_direct_save_unlink_form($form_state) {
$files = drupal_system_listing('\.unlink.inc$', 'modules', 'name', 0);
$form = array();
$options = array();
foreach ($files as $value) {
$options[$value->name] = str_replace('_unlink.unlink', '', $value->name);
}
$form['unlink_options'] = array('#type' => 'checkboxes', '#options' => $options);
$form['submit'] = array('#type' => 'submit', '#value' => 'submit', '#name' => 'submit');
return $form;
}
function features_direct_save_unlink_form_submit($form, &$form_state) {
$hooks = _features_direct_save_get_hooks();
$files = drupal_system_listing('\.unlink.inc$', 'modules', 'name', 0);
foreach ($form_state['values']['unlink_options'] as $key => $value) {
if ($value) {
include_once $files[$key]->filename;
views_include('view');
//ft_unlink_unlink_views
foreach ($hooks as $hook) {
$function_name = str_replace('.', '_', $key) . "_$hook";
if (function_exists($function_name)) {
call_user_func_array($function_name, array());
drupal_set_message("$hook $function_name");
}
}
}
}
}
function _features_direct_save_get_hooks() {
return array('box', 'views');
}