/[drupal]/contributions/modules/biblio_normalize/biblio_normalize.module
ViewVC logotype

Contents of /contributions/modules/biblio_normalize/biblio_normalize.module

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


Revision 1.4 - (show annotations) (download) (as text)
Wed Apr 23 19:05:45 2008 UTC (19 months ago) by davidlesieur
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Changes since 1.3: +10 -10 lines
File MIME type: text/x-php
#200933: Renamed a variable that was conflicting with Biblio.
1 <?php
2 // $Id: biblio_normalize.module,v 1.3 2007/11/23 20:40:04 davidlesieur Exp $
3
4 /**
5 * @file
6 * Provides normalized database tables for Biblio's multiple-values fields.
7 */
8
9 /**
10 * Implementation of hook_menu().
11 */
12 function biblio_normalize_menu($may_cache) {
13 $items = array();
14 if ($may_cache) {
15 $items[] = array(
16 'path' => 'admin/settings/biblio/normalize',
17 'title' => t('Field normalization'),
18 'callback' => 'drupal_get_form',
19 'callback arguments' => array('biblio_normalize_settings_form'),
20 'access' => user_access('administer site configuration'),
21 'type' => MENU_LOCAL_TASK,
22 );
23 }
24
25 return $items;
26 }
27
28 /**
29 * Implementation of hook_nodeapi().
30 */
31 function biblio_normalize_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
32 if ($node->type != 'biblio') {
33 return;
34 }
35 switch ($op) {
36 case 'update':
37 biblio_normalize_node_delete($node->nid);
38 biblio_normalize_node_insert($node);
39 break;
40
41 case 'insert':
42 biblio_normalize_node_insert($node);
43 break;
44
45 case 'delete':
46 biblio_normalize_node_delete($node->nid);
47 break;
48 }
49 }
50
51 /**
52 * Menu callback for administration settings.
53 */
54 function biblio_normalize_settings_form() {
55 $settings = variable_get('biblio_normalize_settings', array());
56 $fields = _biblio_normalize_get_fields();
57 $form['message'] = array(
58 '#value' => '<p>'. t("Check any multiple-values fields that you wish to normalize, and specify the delimiter character that's used to separate multiple values.") .' '. t('You may enter any printable character as a delimiter, or use "\n" for line changes.') .'</p>',
59 );
60 $form['biblio_normalize_settings'] = array(
61 '#theme' => 'biblio_normalize_settings',
62 '#tree' => TRUE,
63 );
64 foreach($fields as $field) {
65 // Note: Storing fields in a flat array indexed by a composite key eases
66 // field sorting.
67 $key = _biblio_normalize_field_to_key($field->tid, $field->fid);
68 $form['biblio_normalize_settings'][$key] = array(
69 '#title' => check_plain($field->title),
70 '#weight' => $field->weight,
71 );
72 $form['biblio_normalize_settings'][$key]['type'] = array(
73 '#value' => check_plain($field->typename),
74 );
75 $form['biblio_normalize_settings'][$key]['normalize'] = array(
76 '#title' => t('Normalize'),
77 '#type' => 'checkbox',
78 '#default_value' => isset($settings[$key]['normalize']) ? $settings[$key]['normalize'] : 0,
79 );
80 $form['biblio_normalize_settings'][$key]['delimiter'] = array(
81 '#title' => t('Delimiter'),
82 '#type' => 'textfield',
83 '#maxlength' => 2,
84 '#size' => 2,
85 '#default_value' => isset($settings[$key]['delimiter']) ? $settings[$key]['delimiter'] : ',',
86 '#required' => TRUE,
87 );
88 }
89 if (!isset($form)) {
90 $form['message'] = array(
91 '#value' => '<p>'. t('There are currently no Biblio fields.') .'</p>',
92 );
93 }
94 $form = system_settings_form($form);
95 $form['#submit'] = array(
96 'system_settings_form_submit' => array(),
97 'biblio_normalize_rebuild' => array()
98 );
99 return $form;
100 }
101
102 /**
103 * Returns the delimiter of a field.
104 */
105 function biblio_normalize_get_delimiter($tid, $fid) {
106 $key = _biblio_normalize_field_to_key($tid, $fid);
107 $settings = variable_get('biblio_normalize_settings', array());
108 $delimiter = isset($settings[$key]) ? $settings[$key]['delimiter'] : '';
109 if ($delimiter == '\n') {
110 $delimiter = "\n";
111 }
112 return $delimiter;
113 }
114
115 /**
116 * Returns the normalized values of the specified field in the given node.
117 *
118 * This must be called on normalized fields only
119 * (i.e. biblio_normalize_is_normalized() must be true for the field).
120 */
121 function biblio_normalize_get_normalized_values($node, $field) {
122 $delimiter = biblio_normalize_get_delimiter($field->tid, $field->fid);
123 if (empty($delimiter)) {
124 return array($node->{$field->name});
125 }
126 else {
127 $values = explode($delimiter, $node->{$field->name});
128 foreach ($values as $delta => $value) {
129 $values[$delta] = trim($value);
130 if (empty($values[$delta])) {
131 unset($values[$delta]);
132 }
133 }
134 return $values;
135 }
136 }
137
138 /**
139 * Indicates whether or not a field is normalized.
140 */
141 function biblio_normalize_is_normalized($tid, $fid) {
142 $key = _biblio_normalize_field_to_key($tid, $fid);
143 $settings = variable_get('biblio_normalize_settings', array());
144 return isset($settings[$key]) ? $settings[$key]['normalize'] : '';
145 }
146
147 /**
148 * Deletes the normalized data related to the given Biblio node.
149 */
150 function biblio_normalize_node_delete($nid) {
151 db_query('DELETE FROM {biblio_normalize} WHERE nid = %d', $nid);
152 }
153
154 /**
155 * Inserts the normalized data for the given Biblio node.
156 */
157 function biblio_normalize_node_insert($node) {
158 $normalized_fields = _biblio_normalize_get_normalized_fields();
159 foreach ($normalized_fields as $field) {
160 _biblio_normalize_node_insert_field($node, $field);
161 }
162 }
163
164 /**
165 * Rebuilds the normalized data.
166 */
167 function biblio_normalize_rebuild() {
168 db_query('TRUNCATE {biblio_normalize}');
169 $normalized_fields = _biblio_normalize_get_normalized_fields();
170 foreach ($normalized_fields as $field) {
171 $args = array();
172 if ($field->tid != 0) {
173 $where_type = "b.biblio_type = %d AND";
174 $args[] = $field->tid;
175 }
176 $results = db_query("SELECT b.nid, b.biblio_type, b.". db_escape_string($field->name) ." FROM {biblio} b INNER JOIN {node} n ON n.vid = b.vid WHERE n.type = 'biblio' AND $where_type CHAR_LENGTH(b.". db_escape_string($field->name) .") > 0", $args);
177 while ($node = db_fetch_object($results)) {
178 _biblio_normalize_node_insert_field($node, $field);
179 }
180 }
181 if (isset($args)) {
182 drupal_set_message(t('Updated normalized Biblio data.'));
183 }
184 }
185
186 function theme_biblio_normalize_settings($form) {
187 uasort($form, '_element_sort');
188
189 $output = '';
190 $header = array('', t('Field'), t('Publication type'), t('Delimiter'));
191 $rows = array();
192 foreach (element_children($form) as $key) {
193 unset($form[$key]['normalize']['#title']);
194 unset($form[$key]['delimiter']['#title']);
195 $rows[] = array(
196 drupal_render($form[$key]['normalize']),
197 $form[$key]['#title'],
198 drupal_render($form[$key]['type']),
199 drupal_render($form[$key]['delimiter']),
200 );
201 }
202 $output .= theme('table', $header, $rows);
203 $output .= drupal_render($form);
204 return $output;
205 }
206
207 /**
208 * Retrieves the text fields provided by Biblio (common fields and
209 * type-specific fields), except for 'textarea' fields.
210 *
211 * @return
212 * Array of field objects. A field object has the following attributes: tid
213 * (Biblio type id), fid (Biblio field id), title, name (database column in
214 * the Biblio table), weight. A tid of 0 denotes a field that's common to all
215 * Biblio types.
216 */
217 function _biblio_normalize_get_fields() {
218 $fields = array();
219 // Note: The Biblio module has a visible attribute at the field level, but
220 // doesn't seem to use it at the moment. If that changes, we might have to add
221 // a condition for field visibility.
222 $results = db_query('SELECT 0 AS tid, f.fid, f.title, f.name, f.weight, \'common\' AS typename FROM {biblio_fields} f WHERE f.common = 1 UNION SELECT d.tid, d.fid, d.title, f.name, d.weight, t.name AS typename FROM {biblio_type_details} d INNER JOIN {biblio_types} t ON t.tid = d.tid INNER JOIN {biblio_fields} f ON f.fid = d.fid WHERE t.visible = 1');
223 while ($field = db_fetch_object($results)) {
224 $fields[] = $field;
225 }
226 return $fields;
227 }
228
229 /**
230 * Retrieves only the normalized fields.
231 */
232 function _biblio_normalize_get_normalized_fields() {
233 $fields = array();
234 $settings = variable_get('biblio_normalize_settings', array());
235 if (is_array($settings)) {
236 foreach ($settings as $key => $setting) {
237 if ($setting['normalize']) {
238 list($tid, $fid) = _biblio_normalize_key_to_field($key);
239 if ($tid == 0) {
240 $wheres_common[] = $fid;
241 }
242 else {
243 $wheres_fields[] = "(f.fid = $fid AND d.tid = $tid)";
244 }
245 }
246 }
247 }
248 // Retrieve the common fields.
249 if (isset($wheres_common)) {
250 $in = implode(', ', $wheres_common);
251 // Note: The Biblio module has a visible attribute at the field level, but
252 // doesn't seem to use it at the moment. If that changes, we might have to
253 // add a condition for field visibility.
254 $results = db_query("SELECT 0 AS tid, f.fid, f.name FROM {biblio_fields} f WHERE f.common = 1 AND f.fid IN ($in)");
255 while ($field = db_fetch_object($results)) {
256 $fields[] = $field;
257 }
258 }
259 // Retrieve the type-specific fields.
260 if (isset($wheres_fields)) {
261 $wheres = implode(' OR ', $wheres_fields);
262 // Note: The Biblio module has a visible attribute at the field level, but
263 // doesn't seem to use it at the moment. If that changes, we might have to
264 // add a condition for field visibility.
265 $results = db_query("SELECT d.tid, d.fid, f.name FROM {biblio_type_details} d INNER JOIN {biblio_types} t ON t.tid = d.tid INNER JOIN {biblio_fields} f ON f.fid = d.fid WHERE t.visible = 1 AND $wheres");
266 while ($field = db_fetch_object($results)) {
267 $fields[] = $field;
268 }
269 }
270 return $fields;
271 }
272
273 /**
274 * Normalizes a field from a node, and inserts the normalized data into the
275 * database.
276 */
277 function _biblio_normalize_node_insert_field($node, $field) {
278 if (($field->tid == 0 || $node->biblio_type == $field->tid) && !empty($node->{$field->name})) {
279 $values = biblio_normalize_get_normalized_values($node, $field);
280 foreach ($values as $delta => $value) {
281 db_query("INSERT INTO {biblio_normalize} SET nid = %d, tid = %d, fid = %d, delta = %d, value = '%s'", $node->nid, $node->biblio_type, $field->fid, $delta, $value);
282 }
283 }
284 }
285
286 /**
287 * Returns a composite key with the specified field info.
288 */
289 function _biblio_normalize_field_to_key($tid, $fid) {
290 return $tid .'.'. $fid;
291 }
292
293 /**
294 * Extracts field info from the specified composite key.
295 */
296 function _biblio_normalize_key_to_field($key) {
297 return explode('.', $key, 2);
298 }
299

  ViewVC Help
Powered by ViewVC 1.1.2