/[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.8 - (show annotations) (download) (as text)
Wed Nov 4 06:23:09 2009 UTC (3 weeks, 4 days ago) by ptalindstrom
Branch: MAIN
CVS Tags: DRUPAL-6--2-4, DRUPAL-6--2-3
Changes since 1.7: +5 -1 lines
File MIME type: text/x-php
- remove dependency on wysiwyg module
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 return system_settings_form($form);
60 }
61
62 /**
63 * Implementation of hook_form_alter() for node_type_form
64 */
65 function autosave_form_node_type_form_alter(&$form, $form_state) {
66 $form['workflow']['autosave'] = array(
67 '#type' => 'checkbox',
68 '#title' => t('Enable Autosave to add/edit forms for this node type'),
69 '#default_value' => variable_get('autosave_'. $form['#node_type']->type, 0),
70 '#description' => t('Check this box to enable Autosave for this node type.')
71 );
72 }
73
74 /**
75 * Implementation of hook_form_alter().
76 */
77 function autosave_form_alter(&$form, &$form_state, $form_id) {
78 global $user;
79 $path = $_GET['q'];
80
81 if (stristr($form_id, '_node_form') && arg(0) != 'admin') {
82
83 // check if this content_type has the autosave function enabled and make sure it's a node edit or add form
84 if ((variable_get('autosave_'. $form['type']['#value'], 0))) {
85 drupal_add_js(AUTOSAVE_PATH .'/autosave.js');
86 drupal_add_js(AUTOSAVE_PATH .'/jquery.field.js');
87 drupal_add_css(AUTOSAVE_PATH .'/autosave.css');
88
89 // if WYSIWYG module is enabled; lets let JS know this
90 if (module_exists('wysiwyg')) $settings['autosave']['wysiwyg'] = 1;
91 else $settings['autosave']['wysiwyg'] = 0;
92
93 $settings['autosave']['url'] = url('autosave/handler');
94 $settings['autosave']['period'] = variable_get('autosave_period', 10);
95 $settings['autosave']['q'] = $path;
96
97 // If an autosaved version of the form exists, make it available via javascript.
98 if ($autosaved_form = autosave_get_autosaved_form($form_id, $path, $user->uid)) {
99 //$autosaved_form_id = $form['type']['#value'] ? $form['type']['#value'] .'_node_form' : 'node_form';
100 $settings['autosave'] = array_merge($settings['autosave'], array(
101 'serialized' => unserialize($autosaved_form['serialized']),
102 'saved_date' => format_date($autosaved_form['timestamp'], 'medium'),
103 ));
104 }
105 drupal_add_js($settings, 'setting');
106 }
107 }
108 }
109
110
111 /**
112 * Menu callback; autosaves the node.
113 */
114 function autosave_save() {
115 global $user;
116
117 $path = $_POST['q'];
118 $form_id = $_POST['form_id'];
119 // Not all variables need to be serialized.
120 // - for Drupal 6 version need to remove op and form_build_id
121 unset($_POST['q'], $_POST['op'], $_POST['form_build_id']);
122 $serialized = serialize($_POST);
123
124 // check if node has just been saved - if it has then it's because AS ajax fired off as user was submitting
125 // if it had just been submitted - no need to AS now
126 // - easy to figure out if we are submitting an edit to existing node
127 // - little harder if we have just added a node
128 $path_args = explode("/", $path);
129 // update case
130 if (is_numeric($path_args[1])) {
131 $submitted = node_load($path_args[1]);
132 }
133 else {
134 // add case
135 $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])));
136 }
137
138 if (!$submitted || (time() - $submitted->changed) > 10) {
139 // Currently, each user can have only one autosave form at a particular path.
140 db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s' AND uid = %d", $form_id, $path, $user->uid);
141 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);
142 }
143
144 exit();
145 }
146
147 /**
148 * Get the autosaved form at a particular path for a user.
149 *
150 * @param string $form_id
151 * The form_id of the form.
152 * @param string $path
153 * The the internal Drupal path where the form is located
154 * @param string $uid
155 * Drupal UID of the user
156 * @return
157 * An array containing the serialized values of the autosaved form and the timestamp of when the form was autosaved.
158 */
159 function autosave_get_autosaved_form($form_id, $path, $uid) {
160 $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);
161
162 while ($data = db_fetch_object($result)) {
163 $form['serialized'] = $data->serialized;
164 $form['timestamp'] = $data->timestamp;
165 }
166 return $form;
167 }
168
169 /**
170 * Implementation of hook_nodeapi().
171 *
172 * Delete autosave table entry on successful submit (add or update) of node
173 *
174 */
175 function autosave_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
176 if ($op == 'presave') {
177 // we remove ALL edits for that page (not just the users) to avoid:
178 // - user1 asaves but doesnt submit
179 // - user2 edits same node and submits
180 // - user1 comes back to edit -> user1 SHOULD lose edits since user2 has precedence
181 db_query("DELETE FROM {autosaved_forms} WHERE form_id = '%s' AND path = '%s'", $node->form_id, $_GET['q']);
182 }
183 }

  ViewVC Help
Powered by ViewVC 1.1.2