node save state enable checkbox
[sandbox/caseyc/1844110.git] / node_save_state / node_save_state.module
1 <?php
2
3 /**
4 * Implements hook_permission().
5 */
6 function node_save_state_permission() {
7 return array(
8 'node save state' => array(
9 'title' => t('Node Save State'),
10 'description' => t(''),
11 ),
12 );
13 }
14
15 /**
16 * Implements hook_form_FORM_ID_alter().
17 */
18 function node_save_state_form_alter(&$form, &$form_state, $form_id) {
19
20 $reset = variable_get('node_save_state_reset', FALSE);
21
22 //add checkbox that enables 'save node state' for a given content type
23 if ($form['#form_id'] == 'node_type_form') {
24
25 $form['#submit'][] = 'node_save_state_content_type_submit';
26 $type = $form['type']['#default_value'];
27 $form_state['values']['type'] = $type;
28 $node_save_state_enable = variable_get('node_save_state_enable', array());
29
30 $existing = ($node_save_state_enable[$type]['existing'] == 1) ? 1 : 0;
31 $new = ($node_save_state_enable[$type]['new'] == 1) ? 1 : 0;
32
33 $form['node_save_state_settings'] = array(
34 '#type' => 'fieldset',
35 '#title' => 'Node Save State settings',
36 '#collapsible' => TRUE,
37 '#collapsed' => TRUE,
38 '#group' => 'additional_settings',
39 );
40
41 $form['node_save_state_settings']['new_node'] = array(
42 '#type' => 'checkbox',
43 '#title' => 'New node',
44 '#default_value' => $new,
45 '#description' => 'Enable node save state option for creating a new node',
46 );
47
48 $form['node_save_state_settings']['existing_node'] = array(
49 '#type' => 'checkbox',
50 '#title' => 'Existing nodes',
51 '#default_value' => $existing,
52 '#description' => 'Enable node save state option for existing nodes',
53 );
54
55 }
56 if (isset($form['nid'])) {
57
58 global $user;
59
60 $nid = $form['nid']['#value'];
61 $type = $form['type']['#value'];
62 $uid = $user->uid;
63
64 if (!isset($nid)) $nid=0;
65
66 $form_state['values']['type'] = $type;
67 $form_state['values']['nid'] = $nid;
68
69 $node_save_state_enable = variable_get('node_save_state_enable', array());
70 $enable = ($nid == 0) ? $node_save_state_enable[$type]['new'] : $node_save_state_enable[$type]['existing'];
71 krumo($node_save_state_enable);
72 krumo($enable);
73 if (!$reset && $enable) {
74
75 $result = db_select('node_save_state', 'n')
76 ->fields('n')
77 ->condition('nid', $nid, '=')
78 ->condition('type', $type, '=')
79 ->condition('uid', $uid, '=')
80 ->execute();
81
82 if ($row = $result->fetchAssoc()) {
83
84 drupal_set_message(t('Values are being restored from the previous state.'));
85
86 $serialized = $row['serialized'];
87 $serialized_fields = array();
88 $serialized_fields = unserialize($serialized);
89
90 foreach ($serialized_fields as $key => $value) {
91
92 if (isset($form[$key]["und"][0]['value'])) {
93
94 foreach ($value as $index => $default_value) {
95
96 if (!isset($form[$key]["und"][$index])) {
97
98 $form[$key]["und"][$index] = $form[$key]["und"][$index-1];
99 $form[$key]["und"][$index]['#weight']--;
100 }
101
102 $form[$key]["und"][$index]['value']['#default_value'] = $default_value;
103 }
104 }
105 elseif (isset($form[$key]['und'][0]['email'])) {
106
107 foreach ($value as $index => $default_value)
108 $form[$key]["und"][$index]['email']['#default_value'] = $default_value;
109 }
110 elseif (isset($form[$key]["und"][0])) {
111
112 if (isset($value[0]['country_codes']) && isset($value[0]['number'])) {
113
114 foreach ($value as $phone_index => $phone_value)
115 $form[$key]["und"][$phone_index]['#default_value'] = $phone_value;
116 }
117 elseif (is_array($value[0])) {
118
119 foreach ($value as $collection_index => $collection_values) {
120
121 if (is_numeric($collection_index)) {
122
123 if (!isset($form[$key]["und"][$collection_index])) {
124
125 $form[$key]["und"][$collection_index] = $form[$key]["und"][$collection_index-1];
126 $form[$key]["und"][$collection_index]['#weight']--;
127 }
128
129 foreach ($collection_values as $field_name => $field_values) {
130
131 foreach ($field_values as $field_index => $default_value) {
132
133 if (isset($form[$key]["und"][$collection_index][$field_name]["und"][$field_index-1])
134 && !isset($form[$key]["und"][$collection_index][$field_name]["und"][$field_index])) {
135
136 $form[$key]["und"][$collection_index][$field_name]["und"][$field_index] = $form[$key]["und"][$collection_index][$field_name]["und"][$field_index-1];
137 $form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['#weight']--;
138 }
139
140 if (isset($form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['email']))
141 $form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['email']['#default_value'] = $default_value;
142 elseif (isset($form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['value']))
143 $form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['value']['#default_value'] = $default_value;
144 elseif (isset($form[$key]["und"][$collection_index][$field_name]["und"][$field_index]))
145 $form[$key]["und"][$collection_index][$field_name]["und"][$field_index]['#default_value'] = $default_value;
146 }
147 }
148 }
149 }
150 }
151 else {
152
153 $form[$key]["und"][0]['#default_value'] = $value;
154 }
155 }
156 elseif (isset($form[$key]["und"])) {
157 $form[$key]["und"]['#default_value'] = $value;
158 }
159 elseif (isset($form[$key])) {
160 $form[$key]['#default_value'] = $value;
161 }
162 }
163
164 if (isset($form['body']["und"][0]['summary']) && isset($serialized_fields['summary']))
165 $form['body']["und"][0]['summary']['#default_value'] = $serialized_fields['summary'];
166
167 $form['reset_state'] = array(
168 '#access' => user_access('node save state'),
169 '#type' => 'submit',
170 '#value' => t('Reset State'),
171 '#weight' => 101,
172 '#submit' => array('node_reset_state_form_submit'),
173 );
174 }
175 }
176
177 if ($enable) {
178
179 $form['save_state'] = array(
180 '#access' => user_access('node save state'),
181 '#type' => 'submit',
182 '#value' => t('Save Node State'),
183 '#weight' => 100,
184 '#submit' => array('node_save_state_form_submit'),
185 );
186 }
187
188 }
189
190 $reset = variable_set('node_save_state_reset', FALSE);
191
192 }
193
194 function node_save_state_form_submit($form, &$form_state) {
195
196 global $user;
197 $serialized_fields= array();
198 $nid = $form_state['values']['nid'];
199 $type = $form_state['values']['type'];
200 $time = date('Y-m-d H:i:s');
201 $uid = $user->uid;
202
203 if (!isset($nid)) $nid=0;
204
205 foreach ($form_state['values'] as $key => $value) {
206
207 if ($key == "title") {
208
209 $serialized_fields[$key] = $value;
210 }
211 elseif ($key == 'body') {
212
213 $serialized_fields[$key] = $value['und'][0]['value'];
214 $serialized_fields['summary'] = $value['und'][0]['summary'];
215 }
216 elseif (substr($key, 0, 6) == 'field_') {
217
218 foreach ($value['und'] as $index => $cur_value) {
219
220 $field_collection_array = array();
221 if (isset($value['und'][$index]['entity']))
222 $field_collection_array = _node_save_state_get_collection_values($value['und'][$index]['entity']);
223
224 if (!empty($field_collection_array)) {
225
226 foreach ($field_collection_array as $field_name => $field_value) {
227
228 $serialized_fields[$key][$index][$field_name] = $field_value;
229 }
230 }
231 else {
232
233 $value_array = $value['und'][$index];
234 if (isset($value_array['number']) && isset($value_array['country_codes']))
235 $serialized_fields[$key][$index] = $value_array;
236 elseif (isset($value_array['name']))
237 $serialized_fields[$key][$index] = $value_array['name'];
238 elseif (is_array($value_array))
239 $serialized_fields[$key][$index] = array_shift($value_array);
240 }
241 }
242 }
243 }
244
245 drupal_get_messages('status', TRUE);
246 //drupal_set_message('Test: ' . json_encode($serialized_fields));
247
248 $serialized_fields = serialize($serialized_fields);
249
250 $result = db_select('node_save_state', 'n')
251 ->fields('n')
252 ->condition('nid', $nid, '=')
253 ->condition('type', $type, '=')
254 ->condition('uid', $uid, '=')
255 ->execute();
256
257 if ($result->rowCount() == 0) {
258
259 db_insert('node_save_state')
260 ->fields(array(
261 'nid' => $nid,
262 'uid' => $uid,
263 'type' => $type,
264 'serialized' => $serialized_fields,
265 'inserted_time' => $time,
266 ))
267 ->execute();
268 }
269 else {
270
271 db_update('node_save_state')
272 ->fields(array(
273 'serialized' => $serialized_fields,
274 'updated' => $time,
275 ))
276 ->condition('nid', $nid, '=')
277 ->condition('type', $type, '=')
278 ->condition('uid', $uid, '=')
279 ->execute();
280
281 }
282
283 }
284
285 function _node_save_state_get_collection_values($fields_array) {
286
287 if (!is_object($fields_array) && !is_array($fields_array)) return NULL;
288 $field_collection_array = array();
289
290 foreach ($fields_array as $key => $value) {
291
292 if (substr($key, 0, 6) == 'field_' && $key != 'field_name') {
293
294 $value_array = $value['und'];
295 foreach ($value_array as $index => $cur_value) {
296
297 if (isset($value_array[$index]['number']) && isset($value_array[$index]['country_codes']))
298 $field_collection_array[$key][$index] = $value_array[$index];
299 else
300 $field_collection_array[$key][$index] = array_shift($value_array[$index]);
301 }
302 }
303 }
304
305 return $field_collection_array;
306 }
307
308 function node_reset_state_form_submit($form, &$form_state) {
309
310 variable_set('node_save_state_reset', TRUE);
311 drupal_get_messages('status', TRUE);
312 //drupal_set_message('Test reset');
313
314 }
315
316 function node_save_state_content_type_submit($form, &$form_state) {
317
318 $type = $form_state['values']['type'];
319 $new = $form_state['values']['new_node'];
320 $exisiting = $form_state['values']['existing_node'];
321 $node_save_state_enable = variable_get('node_save_state_enable', array());
322 $node_save_state_enable[$type]['new'] = $new;
323 $node_save_state_enable[$type]['existing'] = $exisiting;
324 $node_save_state_enable = variable_set('node_save_state_enable', $node_save_state_enable);
325 }