/[drupal]/contributions/modules/birthdays/birthdays.sync.inc
ViewVC logotype

Contents of /contributions/modules/birthdays/birthdays.sync.inc

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


Revision 1.2 - (show annotations) (download) (as text)
Tue Oct 14 07:26:24 2008 UTC (13 months, 1 week ago) by maartenvg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +3 -3 lines
File MIME type: text/x-php
#320168: Untranslatable strings
1 <?php
2 // $Id: birthdays.sync.inc,v 1.1 2008/10/04 10:29:02 maartenvg Exp $
3 /**
4 * @file
5 * Because the Birthdays module uses 2 tables for the birthdays, they have
6 * to be kept in sync with each other. Normally that will be no problem, but
7 * in some cases there is the need to manually synchronize the two tables.
8 * These cases are for example:
9 * - The initial installation on a site which already used the Profile module
10 * to collect data.
11 * - Temporarily disabling the Birthdays module while keeping the profile
12 * field active.
13 * - Completely reinstalling the Profile module.
14 * - Crashed or changed database tables.
15 */
16
17 /**
18 * Creates the synchronization form.
19 */
20 function birthdays_sync_form() {
21 global $_birthdays_field;
22
23 // We can't synchronize if the field hasn't been set yet.
24 if (!isset($_birthdays_field)) {
25 drupal_set_message(t('No date field has been selected as birthdays field. Please visit the <a href="@birthdays-settings">birthdays settings page</a>.', array('@birthdays-settings' => url('admin/settings/birthdays'))), 'warning');
26 return array();
27 }
28 $form['profile_fieldset'] = array('#type' => 'fieldset');
29
30 $form['profile_fieldset']['description'] = array(
31 '#type' => 'item',
32 '#title' => t('Profile to Birthdays'),
33 '#value' => t("Fill the Birthdays module's table with data from the Profile module's table. This is needed when you install the Birthdays module, but already collected birthdays with the Profile module (e.g. for age verification, to show in the profile or after uninstalling the Birthdays module)."),
34 );
35
36 $form['profile_fieldset']['submit'] = array(
37 '#type' => 'submit',
38 '#value' => t('Copy Profile data to Birthdays module'),
39 '#submit' => array('birthdays_sync_profiles_to_birthdays'),
40 );
41
42 $form['birthdays_fieldset'] = array('#type' => 'fieldset');
43
44 $form['birthdays_fieldset']['description'] = array(
45 '#type' => 'item',
46 '#title' => t('Birthdays to Profile'),
47 '#value' => t("Fill the Profile module's table with data from the Birthdays module's table. You can use this if you completely reinstalled the Profile module while leaving the Birthdays module alone, or to copy the old data collected by a version of the Birthdays module which didn't use the Profile module (pre-5.x)."),
48 );
49
50 $form['birthdays_fieldset']['submit'] = array(
51 '#type' => 'submit',
52 '#value' => t('Copy Birthdays data to Profile module'),
53 '#submit' => array('birthdays_sync_birthdays_to_profiles'),
54 );
55
56 return $form;
57 }
58
59 /**
60 * Profile to Birthdays submit handler.
61 */
62 function birthdays_sync_profiles_to_birthdays($form, &$form_state) {
63 birthdays_set_batch(t('Copy Profile data to Birthdays module'), 'batch_birthdays_sync_p2b');
64 }
65
66 /**
67 * Birthdays to Profile submit handler.
68 */
69 function birthdays_sync_birthdays_to_profiles($form, &$form_state) {
70 birthdays_set_batch(t('Copy Birthdays data to Profile module.'), 'batch_birthdays_sync_b2p');
71 }
72
73 /**
74 * Set the batch for either synchronisation processes.
75 */
76 function birthdays_set_batch($title, $operation_callback) {
77 global $_birthdays_field;
78
79 $batch = array(
80 'title' => $title,
81 'operations' => array(
82 array($operation_callback, array($_birthdays_field)),
83 ),
84 'finished' => 'batch_birthdays_sync_finished',
85 'init_message' => t('Synchronization is starting.'),
86 'progress_message' => t('Synchronizing'),
87 'file' => drupal_get_path('module', 'birthdays'). '/birthdays.sync.inc',
88 );
89
90 // Set the batch. It will start immediately, because it is called from a
91 // submit handler.
92 batch_set($batch);
93 }
94
95 /**
96 * Batch finished synchronizing. Display an apropriate message.
97 */
98 function batch_birthdays_sync_finished($success, $results, $operations) {
99 if($success) {
100 drupal_set_message(t('Birthdays have been synchronized.'));
101 }
102 else {
103 drupal_set_message(t('The birthdays have not been synchronized due to an error.'));
104 }
105 }
106
107 /**
108 * Profile to Birthdays batch copying process.
109 */
110 function batch_birthdays_sync_p2b($field, &$context) {
111 // First run, set up the sandbox.
112 if (empty($context['sandbox'])) {
113 $context['sandbox']['progress'] = 0;
114 $context['sandbox']['current_user'] = 0;
115 // Count how many users are going to be processed.
116 $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT uid) FROM {users} WHERE uid > 0'));
117 }
118
119 // I'm not sure how many a slow server can process within the batch execution
120 // limit, but 25 seems not too much. Faster machines won't notice the limit
121 // thanks to the Batch API.
122 $limit = 25;
123
124 // Get next batch of users.
125 $result = db_query_range('SELECT uid FROM {users} WHERE uid > %d ORDER BY uid ASC', $context['sandbox']['current_user'], 0, $limit);
126
127 // For each user id.
128 while ($uid = db_fetch_object($result)) {
129 // Get the user object.
130 $account = user_load(array('uid' => $uid->uid));
131 // If the date of birth was set
132 if ($account->{$field->name}) {
133 // Save it, so birthdays_user() will be called and all necessary actions
134 // will be performed.
135 user_save($account, array($field->name => $account->{$field->name}));
136 }
137
138 // Updating the sandbox.
139 $context['sandbox']['progress']++;
140 $context['sandbox']['current_user'] = $account->uid;
141 }
142
143 // Update the message.
144 $context['message'] = t('Synchronized @current of @total users.', array('@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));
145
146 // If not yet finished, calculate the proportion of items that are.
147 if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
148 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
149 }
150 }
151
152 /**
153 * Birthdays to Profile batch copying process
154 */
155 function batch_birthdays_sync_b2p($field, &$context) {
156 // First run, set up the sandbox.
157 if (empty($context['sandbox'])) {
158 $context['sandbox']['progress'] = 0;
159 $context['sandbox']['current_user'] = 0;
160 $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(uid) FROM {dob}'));
161 }
162
163 // I'm not sure how many a slow server can process within the batch execution
164 // limit, but 25 seems not too much. Faster machines won't notice the limit
165 // thanks to the Batch API.
166 $limit = 25;
167
168 // Different syntax for MySQL and Postgresql when getting theday, month and
169 // year parts of the date of birth. Only get the next batch of birthdays.
170 switch ($GLOBALS['db_type']) {
171 case 'mysql':
172 case 'mysqli':
173 $result = db_query_range("SELECT uid, MONTH(birthday) AS month, YEAR(birthday) AS year, DAYOFMONTH(birthday) AS day FROM {dob} WHERE uid > %d ORDER BY uid ASC", $context['sandbox']['current_user'], 0, $limit);
174 break;
175 case 'pgsql':
176 $result = db_query_range("SELECT uid, date_part('month',birthday) AS month, date_part('year',birthday) AS year, date_part('day', birthday) AS day FROM {dob} WHERE uid > %d ORDER BY uid ASC", $context['sandbox']['current_user'], 0, $limit);
177 break;
178 }
179
180 // For each date of birth.
181 while ($birthday = db_fetch_object($result)) {
182 // Get the user object.
183 $account = user_load(array('uid' => $birthday->uid));
184 // Make the date of birth array.
185 $dob = array(
186 'day' => $birthday->day,
187 'month' => $birthday->month,
188 'year' => $birthday->year
189 );
190 // And save the user.
191 user_save($account, array($field->name => $dob), $field->category);
192
193 // Updating the sandbox.
194 $context['sandbox']['progress']++;
195 $context['sandbox']['current_user'] = $account->uid;
196 }
197
198 // Update the message.
199 $context['message'] = t('Synchronized @current of @total birthdays.', array('@current' => $context['sandbox']['progress'], '@total' => $context['sandbox']['max']));
200
201 // If not yet finished, calculate the proportion of items that are.
202 if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
203 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
204 }
205 }

  ViewVC Help
Powered by ViewVC 1.1.2