Project

General

Profile

Download (4.57 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
  $block_info = ext_links_block_info();
116
  db_update("block")
117
    ->fields([
118
      'pages' => $block_info['0']['pages'],
119
      'visibility' => 1
120
    ])
121
    ->condition('module', 'ext_links')
122
    ->execute();
123
  $tasks_performed[] = "ext_links block configuration updated.";
124
  return join("; \n", $tasks_performed);
125
}
126

    
127

    
128
/**
129
 * Migrates old ext link templates from drupal vars to the ext_link table.
130
 */
131
function migrate_old_ext_links(){
132
  $links_templates = ext_links_templates();
133
  foreach ($links_templates as $ext_link){
134
    $ext_link = _ext_links_merge_old_vars($ext_link);
135
    ext_links_save($ext_link);
136
  }
137
  return $links_templates;
138
}
139

    
140
/**
141
 * Updates the ext_links.link column with new urls from the default templates.
142
 */
143
function ext_links_template_links_update(){
144
  $template_default_arrays = ext_links_template_defaults();
145
  foreach ($template_default_arrays as $template_array) {
146
    $return = db_merge('ext_links')
147
      ->key(['id' => $template_array['id']])
148
      ->fields([
149
        'link' => $template_array['link'],
150
        'title' => $template_array['title'],
151
      ])
152
      ->execute();
153
  }
154
}
155

    
156

    
157
/**
158
 * Merge the old settings stored in drupal vars into the ext_link template object
159
 * @param $ext_link object
160
 *
161
 * @return object
162
 *  The ext link template
163
 */
164
function _ext_links_merge_old_vars($ext_link){
165
  $ext_link_id = $ext_link->id;
166
  $ext_link->status = variable_get("ext_links_${ext_link_id}_check", $ext_link->status);
167
  $ext_link->link =  variable_get("ext_links_${ext_link_id}_link", $ext_link->link);
168
  $ext_link->title = variable_get("ext_links_${ext_link_id}_text", $ext_link->title);
169
  return $ext_link;
170
}
(5-5/7)