/[drupal]/contributions/modules/autosave/autosave.module
ViewVC logotype

Contents of /contributions/modules/autosave/autosave.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.9 - (show annotations) (download) (as text)
Fri Nov 6 00:41:16 2009 UTC (2 weeks, 6 days ago) by ptalindstrom
Branch: MAIN
CVS Tags: DRUPAL-6--2-5, HEAD
Changes since 1.8: +11 -0 lines
File MIME type: text/x-php
- add stealth mode
1 <?php
2 // $Id: autosave.module,v 1.7 2009/10/31 20:59:08 ptalindstrom Exp $
3
4 /**
5 * @file
6 * Does background saves of node being edited.
7 */
8
9 define('AUTOSAVE_PATH', drupal_get_path('module', 'autosave'));
10
11 /**
12 * Implementation of hook_help().
13 */
14 function autosave_help($path, $arg) {
15 $output = '';
16 switch ($path) {
17 case 'admin/help#autosave':
18 $output = '<p>'. t('The autosave module automatically saves a form after a period of time.') .'</p>';
19 break;
20 }
21 return $output;
22 }
23
24 /**
25 * Implementation of hook_menu().
26 */
27 function autosave_menu() {
28 $items['autosave/handler'] = array(
29 'title' => 'Autosave save',
30 'page callback' => 'autosave_save',
31 'access callback' => TRUE,
32 'type' => MENU_CALLBACK,
33 );
34
35 $items['admin/settings/autosave'] = array(
36 'title' => 'Autosave',
37 'description' => 'Configure autosave settings.',
38 'page callback' => 'drupal_get_form',
39 'page arguments' => array('autosave_admin_settings'),
40 'access arguments' => array('administer nodes'),
41 );
42 return $items;
43 }
44
45 /**
46 * Menu callback; return the autosave module settings form.
47 */
48 function autosave_admin_settings() {
49 if (!file_exists(AUTOSAVE_PATH .'/jquery.field.js')) {
50 drupal_set_message(t('Unable to find the jQuery Field Plugin in !path. Please <a href="http://plugins.jquery.com/files/jquery.field.js_4.txt">download jquery.field.js</a>
51 and place it into !path.', array('!path' => $path)), 'error');
52 }
53
54 $form['autosave_period'] = array(
55 '#type' => 'textfield',
56 '#title' => t('Autosave after this amount seconds has passed'),
57 '#default_value' => variable_get('autosave_period', 10),
58 );
59
60 $form['autosave_hidden'] = array(
61 '#prefix' => '<div class="form-item"><label for="edit-autosave-hidden">'. t('Stealth Mode') . '</label>',
62 '#type' => 'checkbox',
63 '#title' => t('Run in stealth mode'),
64 '#description' => t('If this check box is selected no popup will appear notifying user that the form has been autosaved.'),
65 '#default_value' => variable_get('autosave_hidden', 0),
66 '#suffix' => "</div>",
67 );
68
69 return system_settings_form($form);
70 }
71
72 /**
73 * Implementation of hook_form_alter() for node_type_form
74 */
75 function autosave_form_node_type_form_alter(&$form, $form_state) {
76 $form['workflow']['autosave'] = array(
77 '#type' => 'checkbox',
78 '#title' => t('Enable Autosave to add/edit forms for this node type'),
79 '#default_value' => variable_get('autosave_'. $form['#node_type']->type, 0),
80 '#description' => t('Check this box to enable Autosave for this node type.')
81 );
82 }
83
84 /**
85 * Implementation of hook_form_alter().
86 */
87 function autosave_form_alter(&$form, &$form_state, $form_id) {
88 global $user;
89 $path = $_GET['q'];
90
91 if (stristr($form_id, '_node_form') && arg(0) != 'admin') {
92
93 // check if this content_type has the autosave function enabled and make sure it's a node edit or add form
94 if ((variable_get('autosave_'. $form['type']['#value'], 0))) {
95 drupal_add_js(AUTOSAVE_PATH .'/autosave.js');
96 drupal_add_js(AUTOSAVE_PATH .'/jquery.field.js');
97 drupal_add_css(AUTOSAVE_PATH .'/autosave.css');
98
99 // if WYSIWYG module is enabled; lets let JS know this
100 if (module_exists('wysiwyg')) $settings['autosave']['wysiwyg'] = 1;
101 else $settings['autosave']['wysiwyg'] = 0;
102
103 $settings['autosave']['url'] = url('autosave/handler');
104 $settings['autosave']['period'] = variable_get('autosave_period', 10);
105 $settings['autosave']['q'] = $path;
106 $settings['autosave']['hidden'] = variable_get('autosave_hidden', 0);
107
108 // If an autosaved version of the form exists, make it available via javascript.
109 if ($autosaved_form = autosave_get_autosaved_form($form_id, $path, $user->uid)) {
110 //$autosaved_form_id = $form['type']['#value'] ? $form['type']['#value'] .'_node_form' : 'node_form';
111 $settings['autosave'] = array_merge($settings['autosave'], array(
112 'serialized' => unserialize($autosaved_form['serialized']),
113 'saved_date' => format_date($autosaved_form['timestamp'], 'medium'),
114 ));
115 }
116 drupal_add_js($settings, 'setting');
117 }
118 }
119 }
120
121
122 /**
123 * Menu callback; autosaves the node.
124 */
125 function autosave_save() {
126 global $user;
127
128 $path = $_POST['q'];
129 $form_id = $_POST['form_id'];
130 // Not all variables need to be serialized.
131 // - for Drupal 6 version need to remove op and form_build_id
132 unset($_POST['q'], $_POST['op'], $_POST['form_build_id']);
133 $serialized = serialize($_POST);
134
135 // check if node has just been saved - if it has then it's because AS ajax fired off as user was submitting
136 // if it had just been submitted - no need to AS now
137 // - easy to figure out if we are submitting an edit to existing node
138 // - little harder if we have just added a node
139 $path_args = explode("/", $path);
140 // update case
141 if (is_numeric($path_args[1])) {
142 $submitted = node_load($path_args[1]);
143 }
144 else {
145 // add case
146 $submitted->changed = db_result(db_query("SELECT created FROM {node} WHERE uid = %d and type = '%s' ORDER BY created DESC LIMIT 1", $user->uid, str_replace("-", "_", $path_args[2])));
147 }
148
149 if (!$submitted || (time() - $submitted->changed) > 10) {
150 // Currently, each user can have only one autosave form at a particular path.
151 db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $user->uid);
152 db_query("INSERT INTO {autosaved_forms} (form_id, path, uid, timestamp, serialized) VALUES ('%s', '%s', %d, %d, '%s')", $form_id, $path, $user->uid, time(), $serialized);
153 }
154
155 exit();
156 }
157
158 /**
159 * Get the autosaved form at a particular path for a user.
160 *
161 * @param string $form_id
162 * The form_id of the form.
163 * @param string $path
164 * The the internal Drupal path where the form is located
165 * @param string $uid
166 * Drupal UID of the user
167 * @return
168 * An array containing the serialized values of the autosaved form and the timestamp of when the form was autosaved.
169 */
170 function autosave_get_autosaved_form($form_id, $path, $uid) {
171 $result = db_query("SELECT form_id, serialized, timestamp FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $uid);
172
173 while ($data = db_fetch_object($result)) {
174 $form['serialized'] = $data->serialized;
175 $form['timestamp'] = $data->timestamp;
176 }
177 return $form;
178 }
179
180 /**
181 * Implementation of hook_nodeapi().
182 *
183 * Delete autosave table entry on successful submit (add or update) of node
184 *
185 */
186 function autosave_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
187 if ($op == 'presave') {
188 // we remove ALL edits for that page (not just the users) to avoid:
189 // - user1 asaves but doesnt submit
190 // - user2 edits same node and submits
191 // - user1 comes back to edit -> user1 SHOULD lose edits since user2 has precedence
192 db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s'", $node->form_id, $_GET['q']);
193 }
194 }

  ViewVC Help
Powered by ViewVC 1.1.2