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

Diff of /contributions/modules/refcolab/refcolab.module

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

revision 1.1, Fri Jun 13 00:42:46 2008 UTC revision 1.2, Tue Jun 17 03:58:10 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: refcolab.module,v 1.1 2008/06/13 00:42:46 herc Exp $
3    
4  /**  /**
5   * @file   * @file
6   * Define primarily Drupal hooks and API functions of the RefColab package.   * Define primarily Drupal hooks and API functions of the RefColab package.
7   *   *
8   * This is the main module file for RefColab modules package.   * This is the main module file for RefColab modules package.
9   *   *
10   */   */
11    
12    /*
13     * The number of default creators fields.
14     */
15    define('REFCOLAB_NUM_OF_CREATORS', 3);
16    
17    
18  /**  /**
19   * Implementation of hook_perm().   * Implementation of hook_perm().
20   */   */
# Line 19  function refcolab_perm() { Line 25  function refcolab_perm() {
25  /**  /**
26   * Implementation of hook_menu().   * Implementation of hook_menu().
27   */   */
 /*  
28  function refcolab_menu() {  function refcolab_menu() {
29      $items['refcolab/js/more-authors'] = array(
30        'page callback' => 'refcolab_js_more_authors',
31        'file' => 'includes/ahah.inc',
32        'access arguments' => array('create references'),
33        'type' => MENU_CALLBACK,
34      );
35    
36    return $items;    return $items;
37  }  }
38  */  
39    
40  /**  /**
41   * Implementation of hook_theme(). Register theming functions.   * Implementation of hook_theme(). Register theming functions.
# Line 58  function refcolab_access($op, $node = NU Line 70  function refcolab_access($op, $node = NU
70    global $user;    global $user;
71    switch ($op) {    switch ($op) {
72      case 'create':      case 'create':
73        return user_access('create references');        return user_access('create references');
74    }    }
75  }  }
76    
# Line 67  function refcolab_access($op, $node = NU Line 79  function refcolab_access($op, $node = NU
79   */   */
80  function refcolab_form_reference_node_form_alter($form, &$form_state) {  function refcolab_form_reference_node_form_alter($form, &$form_state) {
81    $node = $form['#node'];    $node = $form['#node'];
82    
83    if (isset($node->reference_type)) {    if (isset($node->reference_type)) {
84    }    }
85    else {    else {
86      // If the reference type has not been selected yet,      // If the reference type has not been selected yet,
87      // then unset the submit and preview buttons and add a      // then unset the submit and preview buttons and add a
88      // next button with a custom validation handler.      // next button with a custom validation handler.
89      unset($form['buttons']['submit'], $form['buttons']['preview']);      unset($form['buttons']['submit'], $form['buttons']['preview']);
90      $form['buttons']['next'] = array(      $form['buttons']['next'] = array(
91        '#type' => 'submit',        '#type' => 'submit',
92        '#value' => t('Next'),        '#value' => t('Next'),
# Line 93  function refcolab_form(&$node, &$form_st Line 105  function refcolab_form(&$node, &$form_st
105    if (isset($node->reference)) {    if (isset($node->reference)) {
106      $node->reference = (object)($node->reference);      $node->reference = (object)($node->reference);
107    }    }
108    
109    if (isset($node->nid) && $node->nid || isset($node->reference_type) || isset($form_state['storage']['reference_type'])) {    if (isset($node->nid) && $node->nid || isset($node->reference_type) || isset($form_state['storage']['reference_type'])) {
110      if (!isset($node->reference_type)) {      if (!isset($node->reference_type)) {
111        $node->reference_type = $form_state['storage']['reference_type'];        $node->reference_type = $form_state['storage']['reference_type'];
112        $node->reference->type = $node->reference_type;        $node->reference->type = $node->reference_type;
113      }      }
114      $form += refcolab_reference_form($node, $form_state);      $form += refcolab_reference_form($node, $form_state);
115    }    }
116    else {    else {
117      $options = _refcolab_reference_type_options(refcolab_get_reference_types());      $options = _refcolab_reference_type_options(refcolab_get_reference_types());
118      $form['reference_type'] = array(      $form['reference_type'] = array(
119        '#type' => 'select',        '#type' => 'select',
120        '#title' => t('Select reference type'),        '#title' => t('Select reference type'),
121        '#options' => $options,        '#options' => $options,
122        '#default_value' => key($options),        '#default_value' => key($options),
123      );      );
124    }    }
125    
126    $form['#submit'][] = 'refcolab_form_submit';    $form['#submit'][] = 'refcolab_form_submit';
127    return $form;    return $form;
128  }  }
129    
130  function refcolab_select_reference_type_validate($form, &$form_state) {  function refcolab_select_reference_type_validate($form, &$form_state) {
131    if (isset($form_state['values']['reference_type']) && !form_get_errors()) {    if (isset($form_state['values']['reference_type']) && !form_get_errors()) {
132      $form_state['rebuild'] = TRUE;      $form_state['rebuild'] = TRUE;
133      $form_state['storage']['reference_type'] = $form_state['values']['reference_type'];      $form_state['storage']['reference_type'] = $form_state['values']['reference_type'];
134    }    }
135  }  }
136    
137  function refcolab_form_submit($form, &$form_state) {  function refcolab_form_submit($form, &$form_state) {
138    unset($form_state['storage']['reference_type']);    // Unset used form state storage entries.
139      unset($form_state['storage']['reference_type']);
140      // Clean up creators fields that have the name value empty.
141      foreach($form_state['values']['reference']['creators'] as $delta => $creator) {
142        if (strlen(trim($creator['value'])) == 0) {
143          unset($form_state['values']['reference']['creators'][$delta]);
144        }
145      }
146  }  }
147    
148  /**  /**
# Line 131  function refcolab_form_submit($form, &$f Line 151  function refcolab_form_submit($form, &$f
151   * @param $node   * @param $node
152   * @param $form_state   * @param $form_state
153   */   */
   
154  function refcolab_reference_form($node, &$form_state) {  function refcolab_reference_form($node, &$form_state) {
155    $form['reference'] = array(    $form['reference'] = array(
156      '#tree' => TRUE,      '#tree' => TRUE,
# Line 142  function refcolab_reference_form($node, Line 161  function refcolab_reference_form($node,
161      '#required' => TRUE,      '#required' => TRUE,
162      '#default_value' => $node->title,      '#default_value' => $node->title,
163      '#maxlength' => 255,      '#maxlength' => 255,
164    );    );
165    
166      $form['reference'] += _refcolab_creators_widget($node, $form_state);
167    
168    $fields = refcolab_reference_type_fields($node->reference_type);    $fields = refcolab_reference_type_fields($node->reference_type);
169    foreach($fields as $field_name => $field) {    foreach($fields as $field_name => $field) {
# Line 150  function refcolab_reference_form($node, Line 171  function refcolab_reference_form($node,
171      $form['reference'][$field->field_name]['#title'] = $field->field_title;      $form['reference'][$field->field_name]['#title'] = $field->field_title;
172      $form['reference'][$field->field_name]['#default_value'] = isset($node->reference->$field_name) ? $node->reference->$field_name  : '';      $form['reference'][$field->field_name]['#default_value'] = isset($node->reference->$field_name) ? $node->reference->$field_name  : '';
173    }    }
174    
175    // Make the following fields available to use any of them.    // Make the following fields available to use any of them.
176    $form['reference']['type'] = array('#type' => 'value', '#value' => $node->reference_type);    $form['reference']['type'] = array('#type' => 'value', '#value' => $node->reference_type);
177      $form['reference']['type'] = array('#type' => 'hidden', '#value' => $node->reference_type);
178    $form['reference_type'] = array('#type' => 'value', '#value' => $node->reference_type);    $form['reference_type'] = array('#type' => 'value', '#value' => $node->reference_type);
179    
180    return $form;    return $form;
181  }  }
182    
183  /**  /**
184   * implementation of hook_load().   * Generate the creators input widget.
185     */
186    function _refcolab_creators_widget(&$node, $form_state) {
187      $form['creators'] = array(
188        '#type' => 'fieldset',
189        '#tree' => TRUE,
190        '#title' => t('Authors'),
191        '#collapsible' => TRUE,
192        '#collapsed' => FALSE,
193        '#prefix' => '<div id="refcolab-reference-creators">',
194        '#suffix' => '</div>',
195      );
196    
197      if (isset($node->nid)&& $node->nid) {
198        // Add the current fields to the form.
199        foreach($node->reference->creators as $creator) {
200          $form['creators'][] = _refcolab_creator_element($node->reference_type, $creator);
201        }
202      }
203      else {
204        $num_of_fields = max(REFCOLAB_NUM_OF_CREATORS, isset($form_state['creators_count']) ? $form_state['creators_count'] : 0);
205        // Add empty creator fields to the form.
206        for($i = 0; $i < $num_of_fields; $i++) {
207          $form['creators'][] = _refcolab_creator_element($node->reference_type);
208        }
209      }
210    
211      $form['creators']['refcolab_more_creators'] = array(
212        '#type' => 'submit',
213        '#value' => t('More'),
214        '#description' => t("If there exists more authors, click here to add more."),
215        '#submit' => array('refcolab_more_creators_submit'),
216        '#ahah' => array(
217          'path' => 'refcolab/js/more-authors',
218          'wrapper' => 'refcolab-reference-creators',
219          'method' => 'replace',
220          'effect' => 'fade'
221        ),
222        '#weight' => 10,
223      );
224    
225      return $form;
226    }
227    
228    function _refcolab_creator_element($type_name, $creator = NULL) {
229      static $creators;
230      if (!isset($creators)) {
231        foreach(refcolab_reference_type_creators($type_name) as $item) {
232          $creators[$item->creator] = $item->creator_title;
233        }
234      }
235      $form['creator'] = array(
236        '#type' => 'select',
237        '#options' => $creators,
238        '#default_value' => isset($creator->creator) ? $creator->creator : '',
239      );
240      $form['value'] = array(
241        '#type' => 'textfield',
242        '#title' => t('Name'),
243        '#default_value' => isset($creator->value) ? $creator->value : '',
244      );
245      return $form;
246    }
247    
248    /**
249     * Submit handler to add more creators fiedls to a form.
250     * This handler is used when javascript is not available. It makes changes to
251     * the form state and the entire form is rebuilt during the page reload.
252     */
253    function refcolab_more_creators_submit($form, &$form_state) {
254      // Set the form to rebuild and run submit handlers.
255      node_form_submit_build_node($form, $form_state);
256      // Make the changes we want to the form state.
257      if ($form_state['values']['reference']['creators']['refcolab_more_creators']) {
258        $form_state['creators_count'] = count($form_state['values']['reference']['creators']);
259      }
260    }
261    
262    
263    /**
264     * Implementation of hook_load().
265   */   */
266  function refcolab_load(&$node) {  function refcolab_load(&$node) {
267    $node->reference_type = db_result(db_query("SELECT reference_type FROM {refcolab_references} WHERE nid = %d AND vid = %d", $node->nid, $node->vid));    $node->reference_type = db_result(db_query("SELECT reference_type FROM {refcolab_references} WHERE nid = %d AND vid = %d", $node->nid, $node->vid));
268      // Load type specific values.
269    $node->reference = db_fetch_object(db_query("SELECT * FROM {refcolab_type_%s} WHERE nid = %d AND vid = %d", $node->reference_type, $node->nid, $node->vid));    $node->reference = db_fetch_object(db_query("SELECT * FROM {refcolab_type_%s} WHERE nid = %d AND vid = %d", $node->reference_type, $node->nid, $node->vid));
270      // Load creators data.
271      $node->reference->creators = array();
272      $result = db_query("SELECT creator, value FROM {refcolab_references_creators} WHERE nid = %d AND vid = %d ORDER BY weight", $node->nid, $node->vid);
273      while ($item = db_fetch_object($result)) {
274        $node->reference->creators[] = $item;
275      }
276    }
277    
278    /**
279     * Implementation of hook_prepare().
280     */
281    function refcolab_prepare(&$node) {
282      if (!isset($node->reference)) {
283        $reference = array(
284          'creators' => array(),
285        );
286        $node->reference = (object)$reference;
287      }
288  }  }
289    
290  /**  /**
# Line 182  function refcolab_update($node) { Line 305  function refcolab_update($node) {
305   * Implementation of hook_delete().   * Implementation of hook_delete().
306   */   */
307  function refcolab_delete($node) {  function refcolab_delete($node) {
308    db_query("DELETE FROM {refcolab_references} WHERE nid = %d AND vid = %d", $node->nid, $node->vid);    // Delete the reference entry.
309    db_query("DELETE FROM {refcolab_type_%s} WHERE nid = %d AND vid = %d", $node->reference_type, $node->nid, $node->vid);    db_query("DELETE FROM {refcolab_references} WHERE nid = %d", $node->nid);
310      // Delete reference type specific data.
311      db_query("DELETE FROM {refcolab_type_%s} WHERE nid = %d", $node->reference_type, $node->nid);
312      // Delete creators.
313      db_query("DELETE FROM {refcolab_references_creators} WHERE nid = %d", $node->nid);
314  }  }
315    
316    
317    /**
318     * Implementation of hook_view().
319     */
320    function refcolab_view(&$node, $teaser = false, $page = false) {
321      $node->content['body']['#value'] = theme('biblio_tabular', $node, $base, $teaser);
322      return $node;
323    }
324    
325    
326  /**  /**
327   * Save a reference node.   * Save a reference node.
328   *   *
329   * @param $node   * @param $node
330   *   The node reference to be saved.   *   The node reference to be saved.
331   * @param boolean $insert   * @param boolean $insert
332   *   Whether to perform an insert or an update. TRUE to insert (default)   *   Whether to perform an insert or an update. TRUE to insert (default)
333   *   FALSE to update.   *   FALSE to update.
334   */   */
335  function refcolab_save_reference($node, $insert = TRUE) {  function refcolab_save_reference($node, $insert = TRUE) {
336    $node->reference['nid'] = $node->nid;    $node->reference['nid'] = $node->nid;
337    $node->reference['vid'] = $node->vid;    $node->reference['vid'] = $node->vid;
338    if ($insert) {    if ($insert) {
339      drupal_write_record('refcolab_references', $node);      drupal_write_record('refcolab_references', $node);
340      drupal_write_record('refcolab_type_'. $node->reference_type, $node->reference);      drupal_write_record('refcolab_type_'. $node->reference_type, $node->reference);
341        refcolab_save_creators($node);
342    }    }
343    else {    else {
344      drupal_write_record('refcolab_type_'. $node->reference_type, $node->reference, 'vid');      drupal_write_record('refcolab_type_'. $node->reference_type, $node->reference, 'vid');
345        refcolab_save_creators($node);
346      }
347    
348    }
349    
350    function refcolab_save_creators($node) {
351      // First delete all the current entries and insert the existing again.
352      db_query("DELETE FROM {refcolab_references_creators} WHERE nid = %d AND vid = %d",  $node->nid, $node->vid);
353      foreach($node->reference['creators'] as $delta => $creator) {
354        if (is_array($creator)) {
355          $creator += array('nid' => $node->nid, 'vid' => $node->vid, 'delta' => $delta, 'weight' => $delta);
356          drupal_write_record('refcolab_references_creators', $creator);
357        }
358    }    }
359  }  }
360    
361  /**  /**
362   * Load the fields available to the given reference type.   * Load the fields available to the given reference type.
363   *   *
364   * @param $rtid   * @param $type_name
365   *   The reference type ID.   *   The reference type name.
366   */   */
367  function refcolab_reference_type_fields($type_name) {  function refcolab_reference_type_fields($type_name) {
368    // @TODO: cache fields statically by type.    // @TODO: cache fields statically by type.
369    $result = db_query(    $result = db_query(
370      "SELECT mf.* FROM {refcolab_meta_types_fields} mtf ".      "SELECT mf.* FROM {refcolab_meta_types_fields} mtf ".
371      "INNER JOIN {refcolab_meta_fields} mf ON mtf.field_name = mf.field_name ".      "INNER JOIN {refcolab_meta_fields} mf ON mtf.field_name = mf.field_name ".
372      "WHERE mtf.type_name = '%s' ORDER BY mtf.weight",      "WHERE mtf.type_name = '%s' ORDER BY mtf.weight",
373      $type_name      $type_name
374    );    );
375    $fields = array();    $fields = array();
376    while ($item = db_fetch_object($result)) {    while ($item = db_fetch_object($result)) {
377      $item->widget = unserialize($item->widget);      $item->widget = unserialize($item->widget);
378      $fields[$item->field_name] = $item;      $fields[$item->field_name] = $item;
379      }
380      return $fields;
381    }
382    
383    /**
384     * Load the creator fields available to the given reference type.
385     *
386     * @param $type_name
387     *   The reference type name.
388     */
389    function refcolab_reference_type_creators($type_name) {
390      // @TODO: cache fields statically by type.
391      $result = db_query("SELECT mc.* FROM {refcolab_meta_types_creators} mtc ".
392        "INNER JOIN {refcolab_meta_creators} mc ON mtc.creator = mc.creator ".
393        "WHERE type_name = '%s' ORDER BY weight",
394        $type_name
395      );
396      $fields = array();
397      while ($item = db_fetch_object($result)) {
398        $fields[$item->creator] = $item;
399    }    }
400    return $fields;    return $fields;
401  }  }
402    
403  /**  /**
404   * Load reference types.   * Load reference types.
405   *   *
406   * @param boolean $reset   * @param boolean $reset
407   *   Whether to reset the statically cached array of loaded objects.   *   Whether to reset the statically cached array of loaded objects.
408   * @return   * @return
409   *   An array of reference types objects.   *   An array of reference types objects.
410   */   */
411  function refcolab_get_reference_types($reset = FALSE) {  function refcolab_get_reference_types($reset = FALSE) {
412    static $ref_types;    static $ref_types;
413    if (!isset($ref_types) || $reset) {    if (!isset($ref_types) || $reset) {
414      $ref_types = array();      $ref_types = array();
415      $result = db_query("SELECT * FROM {refcolab_meta_types}");      $result = db_query("SELECT * FROM {refcolab_meta_types}");
416      while ($item = db_fetch_object($result)) {      while ($item = db_fetch_object($result)) {
417        $ref_types[$item->type_name] = $item;        $ref_types[$item->type_name] = $item;
418      }      }
419    }    }
420    return $ref_types;    return $ref_types;
# Line 251  function refcolab_get_reference_types($r Line 422  function refcolab_get_reference_types($r
422    
423  function _refcolab_reference_type_options($ref_types) {  function _refcolab_reference_type_options($ref_types) {
424    foreach($ref_types as $type_name => $type) {    foreach($ref_types as $type_name => $type) {
425      $options[$type_name] = t($type->type_title);      $options[$type_name] = t($type->type_title);
426    }    }
427    return $options;    return $options;
428  }  }
# Line 260  function _refcolab_reference_type_option Line 431  function _refcolab_reference_type_option
431   * Helper function to load include files.   * Helper function to load include files.
432   *   *
433   * @param $files   * @param $files
434   *   Array of files to be loaded. if there is only one file   *   Array of files to be loaded. if there is only one file
435   *   it can be passed as string.   *   it can be passed as string.
436   */   */
437  function _refcolab_load_includes($files = array()) {  function _refcolab_load_includes($files = array()) {
438    if (is_array($files)) {    if (is_array($files)) {
439      foreach($files as $file) {      foreach($files as $file) {
440        require_once './'. drupal_get_path('module', 'refcolab') .'/includes/'. $file;        require_once './'. drupal_get_path('module', 'refcolab') .'/includes/'. $file;
441      }      }
442    }    }
443    else {    else {
444      require_once './'. drupal_get_path('module', 'refcolab') .'/includes/'. $file;      require_once './'. drupal_get_path('module', 'refcolab') .'/includes/'. $file;
445    }    }
446  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2