4 * Implements hook_permission().
6 function node_save_state_permission() {
8 'node save state' => array(
9 'title' => t('Node Save State'),
10 'description' => t(''),
16 * Implements hook_form_FORM_ID_alter().
18 function node_save_state_form_alter(&$form, &$form_state, $form_id) {
20 $reset = variable_get('node_save_state_reset', FALSE
);
22 //add checkbox that enables 'save node state' for a given content type
23 if ($form['#form_id'] == 'node_type_form') {
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());
30 $existing = ($node_save_state_enable[$type]['existing'] == 1) ?
1 : 0;
31 $new = ($node_save_state_enable[$type]['new'] == 1) ?
1 : 0;
33 $form['node_save_state_settings'] = array(
34 '#type' => 'fieldset',
35 '#title' => 'Node Save State settings',
36 '#collapsible' => TRUE
,
38 '#group' => 'additional_settings',
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',
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',
56 if (isset($form['nid'])) {
60 $nid = $form['nid']['#value'];
61 $type = $form['type']['#value'];
64 if (!isset($nid)) $nid=0;
66 $form_state['values']['type'] = $type;
67 $form_state['values']['nid'] = $nid;
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);
73 if (!$reset && $enable) {
75 $result = db_select('node_save_state', 'n')
77 ->condition('nid', $nid, '=')
78 ->condition('type', $type, '=')
79 ->condition('uid', $uid, '=')
82 if ($row = $result->fetchAssoc()) {
84 drupal_set_message(t('Values are being restored from the previous state.'));
86 $serialized = $row['serialized'];
87 $serialized_fields = array();
88 $serialized_fields = unserialize($serialized);
90 foreach ($serialized_fields as
$key => $value) {
92 if (isset($form[$key]["und"][0]['value'])) {
94 foreach ($value as
$index => $default_value) {
96 if (!isset($form[$key]["und"][$index])) {
98 $form[$key]["und"][$index] = $form[$key]["und"][$index-1];
99 $form[$key]["und"][$index]['#weight']--;
102 $form[$key]["und"][$index]['value']['#default_value'] = $default_value;
105 elseif (isset($form[$key]['und'][0]['email'])) {
107 foreach ($value as
$index => $default_value)
108 $form[$key]["und"][$index]['email']['#default_value'] = $default_value;
110 elseif (isset($form[$key]["und"][0])) {
112 if (isset($value[0]['country_codes']) && isset($value[0]['number'])) {
114 foreach ($value as
$phone_index => $phone_value)
115 $form[$key]["und"][$phone_index]['#default_value'] = $phone_value;
117 elseif (is_array($value[0])) {
119 foreach ($value as
$collection_index => $collection_values) {
121 if (is_numeric($collection_index)) {
123 if (!isset($form[$key]["und"][$collection_index])) {
125 $form[$key]["und"][$collection_index] = $form[$key]["und"][$collection_index-1];
126 $form[$key]["und"][$collection_index]['#weight']--;
129 foreach ($collection_values as
$field_name => $field_values) {
131 foreach ($field_values as
$field_index => $default_value) {
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])) {
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']--;
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;
153 $form[$key]["und"][0]['#default_value'] = $value;
156 elseif (isset($form[$key]["und"])) {
157 $form[$key]["und"]['#default_value'] = $value;
159 elseif (isset($form[$key])) {
160 $form[$key]['#default_value'] = $value;
164 if (isset($form['body']["und"][0]['summary']) && isset($serialized_fields['summary']))
165 $form['body']["und"][0]['summary']['#default_value'] = $serialized_fields['summary'];
167 $form['reset_state'] = array(
168 '#access' => user_access('node save state'),
170 '#value' => t('Reset State'),
172 '#submit' => array('node_reset_state_form_submit'),
179 $form['save_state'] = array(
180 '#access' => user_access('node save state'),
182 '#value' => t('Save Node State'),
184 '#submit' => array('node_save_state_form_submit'),
190 $reset = variable_set('node_save_state_reset', FALSE
);
194 function node_save_state_form_submit($form, &$form_state) {
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');
203 if (!isset($nid)) $nid=0;
205 foreach ($form_state['values'] as
$key => $value) {
207 if ($key == "title") {
209 $serialized_fields[$key] = $value;
211 elseif ($key == 'body') {
213 $serialized_fields[$key] = $value['und'][0]['value'];
214 $serialized_fields['summary'] = $value['und'][0]['summary'];
216 elseif (substr($key, 0, 6) == 'field_') {
218 foreach ($value['und'] as
$index => $cur_value) {
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']);
224 if (!empty($field_collection_array)) {
226 foreach ($field_collection_array as
$field_name => $field_value) {
228 $serialized_fields[$key][$index][$field_name] = $field_value;
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);
245 drupal_get_messages('status', TRUE
);
246 //drupal_set_message('Test: ' . json_encode($serialized_fields));
248 $serialized_fields = serialize($serialized_fields);
250 $result = db_select('node_save_state', 'n')
252 ->condition('nid', $nid, '=')
253 ->condition('type', $type, '=')
254 ->condition('uid', $uid, '=')
257 if ($result->rowCount() == 0) {
259 db_insert('node_save_state')
264 'serialized' => $serialized_fields,
265 'inserted_time' => $time,
271 db_update('node_save_state')
273 'serialized' => $serialized_fields,
276 ->condition('nid', $nid, '=')
277 ->condition('type', $type, '=')
278 ->condition('uid', $uid, '=')
285 function _node_save_state_get_collection_values($fields_array) {
287 if (!is_object($fields_array) && !is_array($fields_array)) return NULL
;
288 $field_collection_array = array();
290 foreach ($fields_array as
$key => $value) {
292 if (substr($key, 0, 6) == 'field_' && $key != 'field_name') {
294 $value_array = $value['und'];
295 foreach ($value_array as
$index => $cur_value) {
297 if (isset($value_array[$index]['number']) && isset($value_array[$index]['country_codes']))
298 $field_collection_array[$key][$index] = $value_array[$index];
300 $field_collection_array[$key][$index] = array_shift($value_array[$index]);
305 return $field_collection_array;
308 function node_reset_state_form_submit($form, &$form_state) {
310 variable_set('node_save_state_reset', TRUE
);
311 drupal_get_messages('status', TRUE
);
312 //drupal_set_message('Test reset');
316 function node_save_state_content_type_submit($form, &$form_state) {
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);