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

Contents of /contributions/modules/relatedcontent/relatedcontent.install

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


Revision 1.7 - (show annotations) (download) (as text)
Thu Jan 10 14:51:27 2008 UTC (22 months, 2 weeks ago) by tbarregren
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +0 -0 lines
File MIME type: text/x-php
Fixing minor erros in the documentation. Refactoring.
1 <?php
2
3 /* $Id: relatedcontent.install,v 1.3.2.2 2008/01/09 21:41:48 tbarregren Exp $
4 *
5 * Copyright (C) 2007-2008 Thomas Barregren.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22
23 /**
24 * @file
25 * Installer script for the RelatedContent – a Drupal module that allows
26 * privileged users to add "related content" to the beginning or end of any
27 * node.
28 *
29 * Author:
30 * Thomas Barregren at Webbredaktören <http://drupal.org/user/16678>.
31 */
32
33
34 /******************************************************************************
35 * MYSQL
36 ******************************************************************************/
37
38 /**
39 * Install.
40 */
41 function relatedcontent_mysql_install() {
42
43 // Create the table relatedcontent.
44 $sql[] = <<<EOT
45 CREATE TABLE if not exists {relatedcontent} (
46 nid int NOT NULL,
47 include_nid int NOT NULL,
48 ordinal_number int NOT NULL,
49 PRIMARY KEY (nid, include_nid)
50 ) /*!40100 DEFAULT CHARACTER SET utf8 */;
51 EOT;
52
53 return $sql;
54
55 }
56
57 /**
58 * Uninstall.
59 */
60 function relatedcontent_mysql_uninstall() {
61
62 // Drop the table relatedcontent.
63 $sql[] = <<<EOT
64 DROP TABLE {relatedcontent};
65 EOT;
66
67 return $sql;
68
69 }
70
71
72 /**
73 * Update 1.
74 */
75 function relatedcontent_mysql_update_1() {
76
77 $sql[] = <<<EOT
78 ALTER TABLE {relatedcontent} ADD COLUMN ordinal_number int NOT NULL;
79 EOT;
80
81 $sql[] = <<<EOT
82 UPDATE {relatedcontent} AS a INNER JOIN (SELECT include_nid FROM {relatedcontent}) AS b ON a.include_nid = b.include_nid SET a.ordinal_number = b.include_nid;
83 EOT;
84
85 return $sql;
86
87 }
88
89
90 /******************************************************************************
91 * INSTALL
92 ******************************************************************************/
93
94 /**
95 * Implements hook_install().
96 */
97 function relatedcontent_install() {
98 _relatedcontent_install_op('install');
99 }
100
101
102 /******************************************************************************
103 * UNINSTALL
104 ******************************************************************************/
105
106 /**
107 * Implements hook_uninstall().
108 */
109 function relatedcontent_uninstall() {
110 _relatedcontent_install_op('uninstall');
111 _relatedcontent_install_variable_delete();
112 }
113
114
115 /******************************************************************************
116 * UPDATES
117 ******************************************************************************/
118
119 /**
120 * Implements hook_update_1().
121 */
122 function relatedcontent_update_1() {
123
124 // Add the column ordinal_number to the table relatedcontent. Previous to
125 // this update, include_nid was used implicit as ordinal_number. Therefore,
126 // for each row, copy the value of include_nid to ordinal_number.
127 $status = _relatedcontent_install_op('update_1');
128
129 // Build a view name to view id lookup table.
130 $result = db_query('SELECT vid, name FROM {view_view}');
131 while ($row = db_fetch_object($result)) {
132 $views[$row->name] = $row->vid;
133 }
134
135 // Update some variables for each content type.
136 $result = db_query('SELECT type FROM {node_type}');
137 while ($row = db_fetch_object($result)) {
138
139 // Update the variable 'view' to hold the views' id instead of name.
140 $view = variable_get("relatedcontent_view_$row->type", '');
141 $has_view = $view && $views[$view];
142 if ($has_view) {
143 $view_id = serialize($views[$view]);
144 $sql = "UPDATE {variable} SET value = '%s' WHERE name = '%s'";
145 $status[] = _relatedcontent_install_db_query($sql, $view_id, "relatedcontent_view_$row->type");
146 }
147
148 // When upgrading from 1.0 or 1.1, the variable 'enabled' is implicit. In
149 // version 1.5 it must be explicit.
150 if (variable_get("relatedcontent_enabled_$row->type", null) === null && $has_view) {
151 $enabled = serialize(true);
152 $sql = "INSERT INTO {variable} (name, value) VALUES ('%s', '%s')";
153 $status[] = _relatedcontent_install_db_query($sql, "relatedcontent_enabled_$row->type", $enabled);
154 }
155
156 // Replace the variable 'after' with the variable 'placing'.
157 if (($after = variable_get("relatedcontent_output_after_$row->type", 0)) !== 0) {
158 $placing = serialize($after ? 'end' : 'beginning');
159 $sql = "UPDATE {variable} SET name = '%s', value = '%s' WHERE name = '%s'";
160 $status[] = _relatedcontent_install_db_query($sql, "relatedcontent_output_placing_$row->type", $placing, "relatedcontent_output_after_$row->type");
161 }
162
163 }
164
165 return $status;
166
167 }
168
169
170 /******************************************************************************
171 * HELPERS
172 ******************************************************************************/
173
174 /**
175 * Execute the operation $op.
176 */
177 function _relatedcontent_install_op($op, $prefix = 'relatedcontent') {
178 global $db_type;
179 $db = $db_type === 'mysqli' ? 'mysql' : $db_type;
180 $function = $prefix .'_'. $db .'_'. $op;
181 if (function_exists($function)) {
182 $queries = $function();
183 foreach ($queries as $query) {
184 $status[] = _relatedcontent_install_db_query($query);
185 }
186 }
187 else {
188 $msg = t('RelatedContent install script doesn\'t support %op for %db.', array('%op' => $op, '%db' => $db));
189 $status[] = array('success' => false, 'query' => $msg);
190 }
191 return $status;
192 }
193
194 /**
195 * Execute a database query. Based on db_query() and update_sql().
196 */
197 function _relatedcontent_install_db_query($query) {
198
199 // Get the arguments.
200 $args = func_get_args();
201 array_shift($args);
202 if (isset($args[0]) and is_array($args[0])) {
203 $args = $args[0];
204 }
205
206 // Build the query.
207 $query = db_prefix_tables($query);
208 _db_query_callback($args, true);
209 $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
210
211 // Do the database query, and return the status.
212 $result = _db_query($query);
213 return array('success' => $result !== false, 'query' => check_plain($query));
214
215 }
216
217 /**
218 * Remove all persisted variables that beginns with $prefix.
219 */
220 function _relatedcontent_install_variable_delete($prefix = 'relatedcontent') {
221 global $conf;
222 $prefix .= '_%';
223 $result = db_query("SELECT name FROM {variable} WHERE name LIKE '%s'", $prefix);
224 while ($row = db_fetch_array($result)) {
225 unset($conf[$row['name']]);
226 }
227 $result = $result && db_query("DELETE FROM {variable} WHERE name LIKE '%s'", $prefix);
228 cache_clear_all('variables', 'cache');
229 }

  ViewVC Help
Powered by ViewVC 1.1.2