Project

General

Profile

Download (5.01 KB) Statistics
| Branch: | Tag: | Revision:
1 2a3a2a4f Andreas Kohlbecker
<?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 6325712d Andreas Kohlbecker
        'length' => 2048,
31 2a3a2a4f Andreas Kohlbecker
        'not null' => TRUE,
32
        'description' => 'The link url template',
33
      ],
34 0e38f1bf Andreas Kohlbecker
      'category' => [
35
        'type' => 'varchar',
36
        'length' => 255,
37
        'description' => 'The link category',
38
      ],
39 2a3a2a4f Andreas Kohlbecker
      '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 0e38f1bf Andreas Kohlbecker
}
60
61 a14257fa Andreas Kohlbecker
/**
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 019d69fa Andreas Kohlbecker
  variable_set('ext_links_appearance_grouped', variable_get('ext_links_options', 0) != 1 ? 1 : 0 );
72
  variable_del('ext_links_options');
73 a14257fa Andreas Kohlbecker
}
74
75 0e38f1bf Andreas Kohlbecker
/**
76
 * Implements hook_uninstall().
77
 */
78 5c5f760a Andreas Kohlbecker
function ext_links_uninstall() {
79 0e38f1bf Andreas Kohlbecker
  db_drop_table('ext_links');
80 c03b5f5d Andreas Kohlbecker
}
81
82 5c5f760a Andreas Kohlbecker
/**
83
 * Update function to migrate previous versions
84
 */
85
function ext_links_update_7001(){
86 6325712d Andreas Kohlbecker
    $connection_info = Database::parseConnectionInfo();
87
    $db_prefix = $connection_info['default']['default']['prefix'];
88 5c5f760a Andreas Kohlbecker
    foreach(ext_links_schema() as $table_name => $schema){
89 6325712d Andreas Kohlbecker
90
      if(!db_table_exists($db_prefix . $table_name)){
91
        db_create_table($db_prefix . $table_name, $schema);
92 5c5f760a Andreas Kohlbecker
      }
93
    }
94
    ext_links_install();
95 6325712d Andreas Kohlbecker
    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 abb2570a Andreas Kohlbecker
103 6325712d Andreas Kohlbecker
  $tasks_performed = [];
104 abb2570a Andreas Kohlbecker
  module_enable(['cdm_tokens'], TRUE);
105
  $tasks_performed[] = "cdm_tokens module enabled.";
106 6325712d Andreas Kohlbecker
  $column_glue_exists = db_query("SHOW COLUMNS FROM {ext_links} LIKE 'glue';")->rowCount();
107
108
  if($column_glue_exists){
109
    db_query("ALTER TABLE {ext_links} DROP COLUMN glue;");
110
    $tasks_performed[] = "ext_links table: Column glue removed.";
111
  }
112
113
  db_query("ALTER TABLE {ext_links} MODIFY COLUMN link varchar(2048) NOT NULL COMMENT 'The link url template';");
114
  $tasks_performed[] = "ext_links.link modified to varchar 2048.";
115
  ext_links_template_links_update();
116
  $tasks_performed[] = "ext_link template links updated.";
117
118 0501ab2c Andreas Kohlbecker
  $block_info = ext_links_block_info();
119
  db_update("block")
120
    ->fields([
121
      'pages' => $block_info['0']['pages'],
122
      'visibility' => 1
123
    ])
124
    ->condition('module', 'ext_links')
125
    ->execute();
126
  $tasks_performed[] = "ext_links block configuration updated.";
127 6325712d Andreas Kohlbecker
  return join("; \n", $tasks_performed);
128 5c5f760a Andreas Kohlbecker
}
129
130 2c63e9ba Andreas Kohlbecker
/**
131
 * - remove linkg to ePIC
132
 */
133
function ext_links_update_7003() {
134
135
  $tasks_performed = [];
136
  $links_templates = ext_links_templates();
137
  if(array_key_exists('epic', $links_templates)) {
138
    db_delete('ext_links')->condition('id', 'epic')->execute();
139
    $tasks_performed[] = "ext_link for ePIC removed";
140
  }
141
  return join("; \n", $tasks_performed);
142
}
143 6325712d Andreas Kohlbecker
144 c03b5f5d Andreas Kohlbecker
/**
145 a14257fa Andreas Kohlbecker
 * Migrates old ext link templates from drupal vars to the ext_link table.
146 c03b5f5d Andreas Kohlbecker
 */
147
function migrate_old_ext_links(){
148 a14257fa Andreas Kohlbecker
  $links_templates = ext_links_templates();
149
  foreach ($links_templates as $ext_link){
150
    $ext_link = _ext_links_merge_old_vars($ext_link);
151
    ext_links_save($ext_link);
152
  }
153
  return $links_templates;
154 c03b5f5d Andreas Kohlbecker
}
155
156 6325712d Andreas Kohlbecker
/**
157
 * Updates the ext_links.link column with new urls from the default templates.
158
 */
159
function ext_links_template_links_update(){
160
  $template_default_arrays = ext_links_template_defaults();
161
  foreach ($template_default_arrays as $template_array) {
162
    $return = db_merge('ext_links')
163
      ->key(['id' => $template_array['id']])
164
      ->fields([
165
        'link' => $template_array['link'],
166 46aa1f81 Andreas Kohlbecker
        'title' => $template_array['title'],
167 6325712d Andreas Kohlbecker
      ])
168
      ->execute();
169
  }
170
}
171
172
173 c03b5f5d Andreas Kohlbecker
/**
174
 * Merge the old settings stored in drupal vars into the ext_link template object
175
 * @param $ext_link object
176
 *
177
 * @return object
178
 *  The ext link template
179
 */
180
function _ext_links_merge_old_vars($ext_link){
181
  $ext_link_id = $ext_link->id;
182 fc1b00e2 Andreas Kohlbecker
  $ext_link->status = variable_get("ext_links_${ext_link_id}_check", $ext_link->status);
183 a14257fa Andreas Kohlbecker
  $ext_link->link =  variable_get("ext_links_${ext_link_id}_link", $ext_link->link);
184
  $ext_link->title = variable_get("ext_links_${ext_link_id}_text", $ext_link->title);
185 c03b5f5d Andreas Kohlbecker
  return $ext_link;
186 2a3a2a4f Andreas Kohlbecker
}