Project

General

Profile

Download (4.26 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update, and uninstall functions for the ext_links module.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function ext_links_schema() {
12
  $schema['ext_links'] = [
13
    'description' => 'Table external link templates.',
14
    'fields' => [
15
      'id' => [
16
        'type' => 'varchar',
17
        'length' => 32,
18
        'not null' => TRUE,
19
        'default' => '',
20
        'description' => 'Name of the ext_link being referenced.',
21
      ],
22
      'title' => [
23
        'type' => 'varchar',
24
        'length' => 255,
25
        'not null' => TRUE,
26
        'description' => 'The link title',
27
      ],
28
      'link' => [
29
        'type' => 'varchar',
30
        'length' => 2048,
31
        'not null' => TRUE,
32
        'description' => 'The link url template',
33
      ],
34
      'category' => [
35
        'type' => 'varchar',
36
        'length' => 255,
37
        'description' => 'The link category',
38
      ],
39
      'weight' => [
40
        'type' => 'int',
41
        'not null' => TRUE,
42
        'default' => 0,
43
        'description' => 'Weight of filter within format.',
44
      ],
45
      'status' => [
46
        'type' => 'int',
47
        'not null' => TRUE,
48
        'default' => 0,
49
        'description' => 'External link enabled status. (1 = enabled, 0 = disabled)',
50
      ],
51
    ],
52
    'primary key' => ['id'],
53
    'indexes' => [
54
      'list' => ['weight', 'id'],
55
    ],
56
  ];
57

    
58
  return $schema;
59
}
60

    
61
/**
62
 * implements hook_install()
63
 */
64
function ext_links_install() {
65
  $links_templates = migrate_old_ext_links();
66
  foreach ($links_templates as $ext_link){
67
    db_delete('variable')
68
      ->condition('name', "ext_links_" . $ext_link->id . '_%', 'LIKE')
69
      ->execute();
70
  }
71
  variable_set('ext_links_appearance_grouped', variable_get('ext_links_options', 0) != 1 ? 1 : 0 );
72
  variable_del('ext_links_options');
73
}
74

    
75
/**
76
 * Implements hook_uninstall().
77
 */
78
function ext_links_uninstall() {
79
  db_drop_table('ext_links');
80
}
81

    
82
/**
83
 * Update function to migrate previous versions
84
 */
85
function ext_links_update_7001(){
86
    $connection_info = Database::parseConnectionInfo();
87
    $db_prefix = $connection_info['default']['default']['prefix'];
88
    foreach(ext_links_schema() as $table_name => $schema){
89

    
90
      if(!db_table_exists($db_prefix . $table_name)){
91
        db_create_table($db_prefix . $table_name, $schema);
92
      }
93
    }
94
    ext_links_install();
95
    return "ext_links table created and old settings migrated.";
96
}
97

    
98
/**
99
 * Update ext_link to cdm_tokens functionality
100
 */
101
function ext_links_update_7002(){
102
  $tasks_performed = [];
103
  $column_glue_exists = db_query("SHOW COLUMNS FROM {ext_links} LIKE 'glue';")->rowCount();
104

    
105
  if($column_glue_exists){
106
    db_query("ALTER TABLE {ext_links} DROP COLUMN glue;");
107
    $tasks_performed[] = "ext_links table: Column glue removed.";
108
  }
109

    
110
  db_query("ALTER TABLE {ext_links} MODIFY COLUMN link varchar(2048) NOT NULL COMMENT 'The link url template';");
111
  $tasks_performed[] = "ext_links.link modified to varchar 2048.";
112
  ext_links_template_links_update();
113
  $tasks_performed[] = "ext_link template links updated.";
114

    
115
  return join("; \n", $tasks_performed);
116
}
117

    
118

    
119
/**
120
 * Migrates old ext link templates from drupal vars to the ext_link table.
121
 */
122
function migrate_old_ext_links(){
123
  $links_templates = ext_links_templates();
124
  foreach ($links_templates as $ext_link){
125
    $ext_link = _ext_links_merge_old_vars($ext_link);
126
    ext_links_save($ext_link);
127
  }
128
  return $links_templates;
129
}
130

    
131
/**
132
 * Updates the ext_links.link column with new urls from the default templates.
133
 */
134
function ext_links_template_links_update(){
135
  $template_default_arrays = ext_links_template_defaults();
136
  foreach ($template_default_arrays as $template_array) {
137
    $return = db_merge('ext_links')
138
      ->key(['id' => $template_array['id']])
139
      ->fields([
140
        'link' => $template_array['link'],
141
      ])
142
      ->execute();
143
  }
144
}
145

    
146

    
147
/**
148
 * Merge the old settings stored in drupal vars into the ext_link template object
149
 * @param $ext_link object
150
 *
151
 * @return object
152
 *  The ext link template
153
 */
154
function _ext_links_merge_old_vars($ext_link){
155
  $ext_link_id = $ext_link->id;
156
  $ext_link->status = variable_get("ext_links_${ext_link_id}_check", $ext_link->status);
157
  $ext_link->link =  variable_get("ext_links_${ext_link_id}_link", $ext_link->link);
158
  $ext_link->title = variable_get("ext_links_${ext_link_id}_text", $ext_link->title);
159
  return $ext_link;
160
}
(5-5/7)