/[drupal]/contributions/modules/simplenews/simplenews.install
ViewVC logotype

Contents of /contributions/modules/simplenews/simplenews.install

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


Revision 1.36 - (show annotations) (download) (as text)
Sat Oct 24 16:30:09 2009 UTC (4 weeks, 5 days ago) by sutharsan
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--2
Changes since 1.35: +2 -2 lines
File MIME type: text/x-php
#515274 by Dmitriy.trt: Fixed Make Vocabulary required at install. Fixed Losing content type settings when saving Vocabulary form.
1 <?php
2 // $Id: simplenews.install,v 1.35 2009/09/30 14:10:20 sutharsan Exp $
3
4 /**
5 * @file
6 * Simplenews installation.
7 */
8
9 /**
10 * Implementation of hook_schema().
11 */
12 function simplenews_schema() {
13 $schema['simplenews_subscriptions'] = array(
14 'description' => 'Subscribers to {simplenews_newsletters}.',
15 'fields' => array(
16 'snid' => array(
17 'description' => 'Primary key: Unique subscription ID.',
18 'type' => 'serial',
19 'not null' => TRUE,
20 ),
21 'activated' => array(
22 'description' => 'Boolean indicating the status of the subscription.',
23 'type' => 'int',
24 'size' => 'tiny',
25 'not null' => TRUE,
26 'default' => 0,
27 ),
28 'mail' => array(
29 'description' => 'The subscription email address.',
30 'type' => 'varchar',
31 'length' => 64,
32 'not null' => TRUE,
33 'default' => '',
34 ),
35 'uid' => array(
36 'description' => 'The {users}.uid that has the same email address.',
37 'type' => 'int',
38 'not null' => TRUE,
39 'default' => 0,
40 ),
41 'language' => array(
42 'type' => 'varchar',
43 'length' => 12,
44 'not null' => TRUE,
45 'default' => '',
46 'description' => 'Anonymous subscriber preferred language. Empty for authenticated users.',
47 ),
48 ),
49 'indexes' => array(
50 'mail' => array('mail'),
51 'uid' => array('uid'),
52 ),
53 'primary key' => array('snid'),
54 );
55
56 $schema['simplenews_newsletters'] = array(
57 'description' => 'Simplenews newsletter data.',
58 'fields' => array(
59 'nid' => array(
60 'description' => '{node} that is used as newsletter.',
61 'type' => 'int',
62 'not null' => TRUE,
63 'default' => 0,
64 ),
65 'vid' => array(
66 'description' => 'The {node}.vid that identifies the version used as newsletter.',
67 'type' => 'int',
68 'not null' => TRUE,
69 'default' => 0,
70 ),
71 'tid' => array(
72 'description' => 'The {term_data}.tid (= newsletter series) this issue belongs to.',
73 'type' => 'int',
74 'not null' => TRUE,
75 'default' => 0,
76 ),
77 's_status' => array(
78 'description' => 'Send status of the newsletter issue (0 = not send; 1 = pending; 2 = send). ',
79 'type' => 'int',
80 'size' => 'tiny',
81 'not null' => TRUE,
82 'default' => 0,
83 ),
84 's_format' => array(
85 'description' => 'Format of the newsletter (plain or html).',
86 'type' => 'varchar',
87 'length' => 8,
88 'not null' => TRUE,
89 'default' => '',
90 ),
91 'priority' => array(
92 'description' => 'Email priority according to RFC 2156 and RFC 5231 (0 = none; 1 = highest; 2 = high; 3 = normal; 4 = low; 5 = lowest).',
93 'type' => 'int',
94 'size' => 'tiny',
95 'not null' => TRUE,
96 'default' => 0,
97 ),
98 'receipt' => array(
99 'description' => 'Boolean indicating request for email receipt confirmation according to RFC 2822.',
100 'type' => 'int',
101 'size' => 'tiny',
102 'not null' => TRUE,
103 'default' => 0,
104 ),
105 ),
106 'primary key' => array('nid'),
107 );
108
109 $schema['simplenews_snid_tid'] = array(
110 'description' => 'Newsletter series subscription data.',
111 'fields' => array(
112 'snid' => array(
113 'description' => 'The {simplenews_subscriptions}.snid who is subscribed.',
114 'type' => 'int',
115 'not null' => TRUE,
116 'default' => 0,
117 ),
118 'tid' => array(
119 'description' => 'The newsletter series ({term_data}.tid) the subscriber is subscribed to.',
120 'type' => 'int',
121 'not null' => TRUE,
122 'default' => 0,
123 ),
124 'status' => array(
125 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
126 'type' => 'int',
127 'size' => 'small',
128 'not null' => TRUE,
129 'default' => 1
130 ),
131 'timestamp' => array(
132 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
133 'type' => 'int',
134 'unsigned' => TRUE,
135 'not null' => TRUE,
136 'default' => 0,
137 ),
138 'source' => array(
139 'description' => 'The source via which the user is (un)subscription.',
140 'type' => 'varchar',
141 'length' => 24,
142 'not null' => TRUE,
143 'default' => '',
144 ),
145 ),
146 'primary key' => array('snid', 'tid'),
147 );
148 $schema['simplenews_mail_spool'] = array(
149 'description' => 'Spool for temporary storage of newsletter emails.',
150 'fields' => array(
151 'msid' => array(
152 'description' => 'The primary identifier for a mail spool record.',
153 'type' => 'serial',
154 'unsigned' => TRUE,
155 'not null' => TRUE,
156 ),
157 'mail' => array(
158 'description' => 'The formatted email address of mail message receipient.',
159 'type' => 'varchar',
160 'length' => 255,
161 'not null' => TRUE,
162 'default' => '',
163 ),
164 'nid' => array(
165 'description' => 'The {node}.nid of this newsletter.',
166 'type' => 'int',
167 'not null' => TRUE,
168 'default' => 0,
169 ),
170 'vid' => array(
171 'description' => 'The {node}.vid of this newsletter.',
172 'type' => 'int',
173 'not null' => TRUE,
174 'default' => 0,
175 ),
176 'tid' => array(
177 'description' => 'The {term_data}.tid this newsletter issue belongs to.',
178 'type' => 'int',
179 'not null' => TRUE,
180 'default' => 0,
181 ),
182 'status' => array(
183 'description' => 'The send status of the email (0 = hold, 1 = pending, 2 = send).',
184 'type' => 'int',
185 'unsigned' => TRUE,
186 'not null' => TRUE,
187 'default' => 0,
188 'size' => 'tiny',
189 ),
190 'timestamp' => array(
191 'description' => 'The time status was set or changed.',
192 'type' => 'int',
193 'unsigned' => TRUE,
194 'not null' => TRUE,
195 'default' => 0,
196 ),
197 ),
198 'primary key' => array('msid'),
199 'indexes' => array('tid' => array('tid'), 'status' => array('status')),
200 );
201 return $schema;
202 }
203
204 /**
205 * Implementation of hook_install().
206 */
207 function simplenews_install() {
208 if (drupal_install_schema('simplenews')) {
209 drupal_set_message(t('Simplenews installation instructions are available on the <a href="!simplenews_help">Simplenews help page</a>.',
210 array('!simplenews_help' => url('admin/help/simplenews'))));
211 }
212 else {
213 drupal_set_message(t('The installation of Simplenews was not successful.'), 'error');
214 }
215
216 _simplenews_install_nodetype();
217 variable_set('simplenews_content_types', array('simplenews' => 'simplenews'));
218
219 _simplenews_install_vocabulary();
220 }
221
222 /**
223 * Implementation of hook_uninstall().
224 */
225 function simplenews_uninstall() {
226 drupal_uninstall_schema('simplenews');
227 db_query("DELETE FROM {variable} WHERE name LIKE 'simplenews_%%'");
228 }
229
230 /**
231 * Create simplenews node type.
232 */
233 function _simplenews_install_nodetype() {
234 // Create a newsletter type. If exists, modify it.
235 if ($type = node_get_types('type', 'simplenews')) {
236 $type->module = 'node';
237 $type->locked = FALSE;
238 $type->custom = TRUE;
239 node_type_save($type);
240 }
241 else {
242 $info = array(
243 'type' => 'simplenews',
244 'name' => t('Newsletter issue'),
245 'module' => 'node',
246 'description' => t('A newsletter issue to be sent to subscribed email addresses.'),
247 'locked' => FALSE,
248 'custom' => TRUE,
249 );
250 $info = _node_type_set_defaults($info);
251 node_type_save((object)$info);
252 }
253 }
254
255 /**
256 * Create simplenews vocabulary and initial term.
257 */
258 function _simplenews_install_vocabulary() {
259 // Create the simplenews vocabulary. If it exists, set it as the simplenews_vid.
260 if ($vocabulary = db_fetch_array(db_query("SELECT * FROM {vocabulary} WHERE name = '%s'", t('Newsletter')))) {
261 $vocabulary['nodes'] = variable_get('simplenews_content_types', array('simplenews' => 'simplenews'));
262 }
263 else {
264 $vocabulary = array(
265 'name' => t('Newsletter'),
266 'multiple' => '0',
267 'required' => '1',
268 'hierarchy' => '0',
269 'relations' => '0',
270 'module' => 'simplenews',
271 'nodes' => variable_get('simplenews_content_types', array('simplenews' => 'simplenews')),
272 );
273 }
274 taxonomy_save_vocabulary($vocabulary);
275 variable_set('simplenews_vid', $vocabulary['vid']);
276
277 // Check to see if at least 1 term exists, else create one
278 $tid = db_result(db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vocabulary['vid']));
279 if (!$tid) {
280 $form_values = array(
281 'name' => t('@site_name newsletter', array('@site_name' => variable_get('site_name', 'Drupal'))),
282 'vid' => $vocabulary['vid'],
283 'weight' => 0,
284 );
285 switch (taxonomy_save_term($form_values)) {
286 case SAVED_UPDATED:
287 drupal_set_message(t('Updated term %name.', array('%name' => $form_values['name'])));
288 break;
289 case SAVED_DELETED:
290 drupal_set_message(t('Deleted term %name.', array('%name' => $form_values['name'])));
291 break;
292 }
293 }
294 }
295
296 /**
297 * Rename sn_* tables to simplenews_* to avoid namespace conflicts.
298 */
299 function simplenews_update_2() {
300 $ret = array();
301 db_rename_table($ret, 'sn_snid_tid', 'simplenews_snid_tid');
302 db_rename_table($ret, 'sn_newsletters', 'simplenews_newsletters');
303 db_rename_table($ret, 'sn_subscriptions', 'simplenews_subscriptions');
304 return $ret;
305 }
306
307 /**
308 * Add index to simplenews_subscriptions.
309 */
310 function simplenews_update_5000() {
311 $ret = array();
312 db_add_index($ret, 'simplenews_subscriptions', 'mail', array('mail'));
313 return $ret;
314 }
315
316 /**
317 * Addition of node version to simplenews_newsletters in order to record the
318 * node version which is being send.
319 */
320 function simplenews_update_5001() {
321 $ret = array();
322 db_add_field($ret, 'simplenews_newsletters', 'vid', array('type' => 'int', 'type' => 'int', 'not null' => TRUE, 'default' => 0));
323 return $ret;
324 }
325
326 /**
327 * Data conversion of block delta.
328 * Field type conversions: newsletter priority.
329 * Field name change in simplenews_subscriptions table.
330 */
331 function simplenews_update_6000() {
332 $ret = array();
333
334 // Convert the block delta: remove 'newsletter-' prefix from the delta.
335 $result = db_query("SELECT module, delta FROM {blocks} WHERE module = 'simplenews' AND delta LIKE 'newsletter-%'");
336 while ($data = db_fetch_object($result)) {
337 $delta = strtr($data->delta, array('newsletter-' => ''));
338 $ret[] = update_sql("UPDATE {blocks} SET delta = '%s' WHERE module = 'simplenews' AND delta = 'newsletter-%s'", $delta, $delta);
339 }
340 $result = db_query("SELECT module, delta FROM {blocks_roles} WHERE module = 'simplenews' AND delta LIKE 'newsletter-%'");
341 while ($data = db_fetch_object($result)) {
342 $delta = strtr($data->delta, array('newsletter-' => ''));
343 $ret[] = update_sql("UPDATE {blocks_roles} SET delta = '%s' WHERE module = 'simplenews' AND delta = 'newsletter-%s'", $delta, $delta);
344 }
345
346 // Convert newsletter priority: change field type int to string
347 db_change_field($ret, 'simplenews_newsletters', 'priority', 'priority', array(
348 'type' => 'varchar',
349 'length' => 8,
350 'not null' => TRUE,
351 'default' => '',
352 )
353 );
354 // Convert subscription field name: change a_status to activated
355 db_change_field($ret, 'simplenews_subscriptions', 'a_status', 'activated', array(
356 'type' => 'int',
357 'size' => 'tiny',
358 'not null' => TRUE,
359 'default' => 0,
360 )
361 );
362 // Convert subscription field name: change s_status to is_send
363 db_change_field($ret, 'simplenews_subscriptions', 's_status', 'is_send', array(
364 'type' => 'int',
365 'size' => 'tiny',
366 'not null' => TRUE,
367 'default' => 0,
368 )
369 );
370 return $ret;
371 }
372
373 /**
374 * Addition of simplenews_mail_cache table.
375 * Related to this new table: the removal of simplenews_subscriptions is_send
376 * status.
377 * Correction of simplenews_newsletters priority field type.
378 */
379 function simplenews_update_6001() {
380 $schema['simplenews_mail_cache'] = array(
381 'description' => '',
382 'fields' => array(
383 'mcid' => array(
384 'description' => 'The primary identifier for a mail cache record.',
385 'type' => 'serial',
386 'unsigned' => TRUE,
387 'not null' => TRUE,
388 ),
389 'tid' => array(
390 'description' => 'The {term_data}.tid this newsletter issue belongs to.',
391 'type' => 'int',
392 'not null' => TRUE,
393 'default' => 0,
394 ),
395 'subject' => array(
396 'description' => 'The subject of this mail message.',
397 'type' => 'varchar',
398 'length' => 255,
399 'not null' => TRUE,
400 'default' => '',
401 ),
402 'mail' => array(
403 'description' => 'The formatted email address of mail message receipient.',
404 'type' => 'varchar',
405 'length' => 255,
406 'not null' => TRUE,
407 'default' => '',
408 ),
409 'status' => array(
410 'description' => 'The send status of the email (0 = hold, 1 = pending, 2 = send).',
411 'type' => 'int',
412 'unsigned' => TRUE,
413 'not null' => TRUE,
414 'default' => 0,
415 'size' => 'tiny',
416 ),
417 'timestamp' => array(
418 'description' => 'The time status was set or changed.',
419 'type' => 'int',
420 'unsigned' => TRUE,
421 'not null' => TRUE,
422 'default' => 0,
423 ),
424 'message' => array(
425 'description' => 'The mail message array.',
426 'type' => 'blob',
427 'not null' => FALSE,
428 'size' => 'big',
429 ),
430 ),
431 'primary key' => array('mcid'),
432 'indexes' => array('tid' => array('tid'), 'status' => array('status')),
433 );
434 $ret = array();
435 // New table to buffer mail messages during sending
436 db_create_table($ret, 'simplenews_mail_cache', $schema['simplenews_mail_cache']);
437
438 // Remove is_send field. No longer required by the introduction of simplenews_mail_cache table
439 db_drop_field($ret, 'simplenews_subscriptions', 'is_send');
440
441 // Convert newsletter priority: change field type string back to int
442 db_change_field($ret, 'simplenews_newsletters', 'priority', 'priority', array(
443 'type' => 'int',
444 'size' => 'tiny',
445 'not null' => TRUE,
446 'default' => 0,
447 )
448 );
449 return $ret;
450 }
451
452 /**
453 * Addition of node version to simplenews_newsletters in order to record the
454 * node version which is being send.
455 * Addition of nid to simplenews_mail_cache to be able to check the newsletter
456 * send status.
457 */
458 function simplenews_update_6002() {
459 $ret = array();
460 db_add_field($ret, 'simplenews_mail_cache', 'nid', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
461 variable_del('simplenews_time');
462 return $ret;
463 }
464
465 /**
466 * Addition of index to uid in simplenews_subscriptions.
467 */
468 function simplenews_update_6003() {
469 $ret = array();
470 db_add_index($ret, 'simplenews_subscriptions', 'uid', array('uid'));
471 return $ret;
472 }
473
474 /**
475 * Add spool table and remove cache table.
476 */
477 function simplenews_update_6004() {
478 $schema['simplenews_mail_spool'] = array(
479 'description' => 'Spool for temporary storage of newsletter emails.',
480 'fields' => array(
481 'msid' => array(
482 'description' => 'The primary identifier for a mail spool record.',
483 'type' => 'serial',
484 'unsigned' => TRUE,
485 'not null' => TRUE,
486 ),
487 'mail' => array(
488 'description' => 'The formatted email address of mail message receipient.',
489 'type' => 'varchar',
490 'length' => 255,
491 'not null' => TRUE,
492 'default' => '',
493 ),
494 'nid' => array(
495 'description' => 'The {node}.nid of this newsletter.',
496 'type' => 'int',
497 'not null' => TRUE,
498 'default' => 0,
499 ),
500 'vid' => array(
501 'description' => 'The {node}.vid of this newsletter.',
502 'type' => 'int',
503 'not null' => TRUE,
504 'default' => 0,
505 ),
506 'tid' => array(
507 'description' => 'The {term_data}.tid this newsletter issue belongs to.',
508 'type' => 'int',
509 'not null' => TRUE,
510 'default' => 0,
511 ),
512 'status' => array(
513 'description' => 'The send status of the email (0 = hold, 1 = pending, 2 = send).',
514 'type' => 'int',
515 'unsigned' => TRUE,
516 'not null' => TRUE,
517 'default' => 0,
518 'size' => 'tiny',
519 ),
520 'timestamp' => array(
521 'description' => 'The time status was set or changed.',
522 'type' => 'int',
523 'unsigned' => TRUE,
524 'not null' => TRUE,
525 'default' => 0,
526 ),
527 ),
528 'primary key' => array('msid'),
529 'indexes' => array('tid' => array('tid'), 'status' => array('status')),
530 );
531 $ret = array();
532 // New table to buffer mail messages during sending
533 db_create_table($ret, 'simplenews_mail_spool', $schema['simplenews_mail_spool']);
534 db_drop_table($ret, 'simplenews_mail_cache');
535 return $ret;
536 }
537
538 /**
539 * Add language field to subscription table and set language of existing subscribers.
540 */
541 function simplenews_update_6005() {
542 $ret = array();
543 db_add_field($ret, 'simplenews_subscriptions', 'language', array(
544 'type' => 'varchar',
545 'length' => 12,
546 'not null' => TRUE,
547 'default' => '',
548 'description' => 'Subscriber preferred language.',
549 )
550 );
551
552 // Set preferred language for all current none anonymous subscribers.
553 $result = db_query('SELECT s.snid, u.language FROM {simplenews_subscriptions} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.uid > %d', 0);
554 while ($subscriber = db_fetch_object($result)) {
555 $ret[] = update_sql("UPDATE {simplenews_subscriptions} SET language = '%s' WHERE snid = %d", $subscriber->language, $subscriber->snid);
556 }
557 return $ret;
558 }
559
560 /**
561 * Make the simplenews content type a custom type.
562 */
563 function simplenews_update_6006() {
564 $ret = array();
565
566 // Convert existing node type or re-create it.
567 // If _node_types_build() if called before update, the simplenews
568 // node type gets deleted because simplenews_node_info() no longer exists.
569 // In that case we re-create the node type.
570 if ($type = node_get_types('type', 'simplenews')) {
571 $type->module = 'node';
572 $type->locked = FALSE;
573 $type->custom = TRUE;
574 node_type_save($type);
575 }
576 else {
577 _simplenews_install_nodetype();
578 }
579
580 return $ret;
581 }
582
583 /**
584 * Rename old permissions.
585 */
586 function simplenews_update_6007() {
587 $ret = array();
588 $result = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
589 while ($role = db_fetch_object($result)) {
590 $patterns = array('/create newsletter/', '/edit own newsletter/', '/edit any newsletter/', '/delete own newsletter/', '/delete any newsletter/');
591 $replacements = array('create simplenews content', 'edit own simplenews content', 'edit any simplenews content' ,'delete own simplenews content', 'delete any simplenews content');
592 $renamed_permission = preg_replace($patterns, $replacements, $role->perm);
593 if ($renamed_permission != $role->perm) {
594 $ret[] = update_sql("UPDATE {permission} SET perm = '$renamed_permission' WHERE rid = $role->rid");
595 }
596 }
597 return $ret;
598 }
599
600 /**
601 * Make vocabulary required.
602 */
603 function simplenews_update_6008() {
604 $vocabulary = (array)taxonomy_vocabulary_load(variable_get('simplenews_vid', ''));
605 $vocabulary['required'] = TRUE;
606 taxonomy_save_vocabulary($vocabulary);
607 return array();
608 }
609
610 /**
611 * Add (un)subscription data to table simplenews_snid_tid.
612 */
613 function simplenews_update_6100() {
614 $ret = array();
615 db_add_field($ret, 'simplenews_snid_tid', 'status', array(
616 'description' => 'A flag indicating whether the user is subscribed (1) or unsubscribed (0).',
617 'type' => 'int',
618 'size' => 'small',
619 'not null' => TRUE,
620 'default' => 1
621 )
622 );
623 db_add_field($ret, 'simplenews_snid_tid', 'timestamp', array(
624 'description' => 'UNIX timestamp of when the user is (un)subscribed.',
625 'type' => 'int',
626 'unsigned' => TRUE,
627 'not null' => TRUE,
628 'default' => 0,
629 )
630 );
631 db_add_field($ret, 'simplenews_snid_tid', 'source', array(
632 'description' => 'The source via which the user is (un)subscription.',
633 'type' => 'varchar',
634 'length' => 24,
635 'not null' => TRUE,
636 'default' => '',
637 )
638 );
639 return $ret;
640 }
641
642 /**
643 * Convert Simplenews custom tokens to Token tokens.
644 */
645 function simplenews_update_6101() {
646 $ret = array();
647
648 $old = array(
649 '!site',
650 '!mailto',
651 '!date',
652 '!login_uri',
653 '!uri',
654 '!confirm_subscribe_url',
655 '!confirm_unsubscribe_url',
656 '!newsletter_url',
657 '!newsletter_name',
658 );
659 $new = array(
660 '[site-name]',
661 '[user-mail]',
662 '[site-date]',
663 '[site-url]/user',
664 '[site-url]',
665 '[simplenews-subscribe-url]',
666 '[simplenews-unsubscribe-url]',
667 '[simplenews-newsletter-url]',
668 '[simplenews-newsletters-name]',
669 );
670 $variables = array(
671 'simplenews_confirm_subscribe_subject',
672 'simplenews_confirm_subscribe_unsubscribed',
673 'simplenews_confirm_subscribe_subscribed',
674 'simplenews_confirm_unsubscribe_subscribed',
675 'simplenews_confirm_unsubscribe_unsubscribed',
676 );
677 foreach ($variables as $variable) {
678 if ($text = variable_get($variable, FALSE)) {
679 $text = str_replace($old, $new, $text);
680 variable_set($variable, $text);
681 }
682 }
683 drupal_set_message(t('Simplenews custom tokens have been deprecated. An attempt was made to replace custom tokens in the confirmation messages. Check the messages at <a href="!url">Simplenews subscription settings</a>. Manually replace Simplenews tokens in (unsent) newsletter issues.', array('!url' => url('admin/settings/simplenews/subscription'))));
684 return $ret;
685 }

  ViewVC Help
Powered by ViewVC 1.1.2