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

Diff of /contributions/modules/docapi/docapi.module

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

revision 1.2.2.8, Mon Jul 14 07:07:22 2008 UTC revision 1.2.2.9, Wed Aug 20 02:40:58 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: docapi.module,v 1.2.2.7 2008/07/12 19:24:49 bradfordcp Exp $    // $Id: docapi.module,v 1.2.2.8 2008/07/14 07:07:22 bradfordcp Exp $
3    
4    /**    /**
5     * @file     * @file
# Line 32  Line 32 
32        'title' => 'Document Library',        'title' => 'Document Library',
33        'description' => 'The collection of uploaded documents',        'description' => 'The collection of uploaded documents',
34        'page callback' => 'docapi_library',        'page callback' => 'docapi_library',
35        'page_arguments' => array(),        'page arguments' => array(),
36        'access arguments' => array(),        'access arguments' => array(),
37      );      );
38    
39      // The item to load a document from the library      $items['doc/library/import'] = array(
40      /*$items['node/add/lib'] = array(        'title' => 'Convert Document',
41        'title' => t('Load Library Item'),        'type' => MENU_CALLBACK,
42        'description' => t('Load an item from the library into s specified node type.'),        'description' => 'Converts a document in the library into a node based on mappings.',
43        'access arguments' => array('create node from document'),        'page callback' => 'drupal_get_form',
44      );*/        'page arguments' => array('docapi_library_import_form'),
45          'access arguments' => array(),
46        );
47    
48        $items['doc/library/delete'] = array(
49          'title' => 'Remove Document',
50          'type' => MENU_CALLBACK,
51          'description' => '',
52          'page callback' => 'drupal_get_form',
53          'page arguments' => array('docapi_library_remove_confirm_form'),
54          'access arguments' => array(),
55        );
56    
57        $items['doc/mappings'] = array(
58          'title' => 'Document to Node Mappings',
59          'description' => 'Lists document to node field relationships',
60          'page callback' => 'docapi_mappings_list',
61          'page arguments' => array(),
62          'access arguments' => array(),
63        );
64    
65        $items['doc/mappings/add'] = array(
66          'title' => 'Create new Mapping',
67          'description' => 'Creates a new Map between node and document fields',
68          'page callback' => 'drupal_get_form',
69          'page arguments' => array('docapi_mappings_add'),
70          'access arguments' => array(),
71        );
72    
73    
74        // AHAH callbacks
75        $items['doc/js/add_nodetype_field'] = array(
76          'title' => 'Nodetype Select',
77          'description' => 'Adds the select drop down to select the node type',
78          'page callback' => 'docapi_mappings_add_nodetype',
79          'page arguments' => array(),
80          'access arguments' => array(),
81          'type' => MENU_CALLBACK,
82        );
83        $items['doc/js/add_node_field'] = array(
84          'title' => 'Node Select',
85          'description' => 'Adds the select drop down to select the sample node',
86          'page callback' => 'docapi_mappings_add_node',
87          'page arguments' => array(),
88          'access arguments' => array(),
89          'type' => MENU_CALLBACK,
90        );
91        $items['doc/js/add_mapping_add_node_fields'] = array(
92          'title' => 'Node Fields',
93          'description' => 'Displays a list of fields within the example node to attempt to map to.',
94          'page callback' => 'docapi_mappings_add_node_fields',
95          'page arguments' => array(),
96          'access arguments' => array(),
97          'type' => MENU_CALLBACK,
98        );
99    
100      return $items;      return $items;
101    }    }
# Line 118  Line 172 
172      db_query("DELETE FROM {docapi_plugins} WHERE module = '%s'", $plugin);      db_query("DELETE FROM {docapi_plugins} WHERE module = '%s'", $plugin);
173    }    }
174    
175      function _get_metadata_fields() {
176        $options = array();
177    
178        $result = db_query("SELECT DISTINCT dm.field FROM {docapi_metadata} dm");
179    
180        while ($row = db_fetch_array($result)) {
181          $options[$row['field']] = $row['field'];
182        }
183    
184        return array_merge(array('' => ''), $options);
185      }
186    
187    /**    /**
188     * Retrieve the list of available parsers     * Retrieve the list of available parsers
189     *     *
# Line 207  Line 273 
273     * DocAPI Upload document form     * DocAPI Upload document form
274     */     */
275    function docapi_doc_add_form($form_state) {    function docapi_doc_add_form($form_state) {
     drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>');  
   
276      $form = array();      $form = array();
277      $form['#attributes'] = array('enctype' => "multipart/form-data");      $form['#attributes'] = array('enctype' => "multipart/form-data");
278    
# Line 321  Line 385 
385    
386    /**    /**
387     * DocAPI Upload document form submission     * DocAPI Upload document form submission
388       *
389       * @todo Move the file into the files directory
390     */     */
391    function docapi_doc_add_form_submit($form, &$form_state) {    function docapi_doc_add_form_submit($form, &$form_state) {
     // @TODO: Remove, this is for debugging only.  
     //drupal_set_message('FORM STATE: <pre>'. var_export($form_state, true) .'</pre>');  
   
392      // Begin file processing once it has cleared all of the validation      // Begin file processing once it has cleared all of the validation
393    
394      // Move the file to the its location within the 'docapi_file_path'      // Move the file to the its location within the 'docapi_file_path'
# Line 336  Line 399 
399    
400      // Set the variables      // Set the variables
401      $file = $form_state['values']['docapi_obj_file'];      $file = $form_state['values']['docapi_obj_file'];
402    
403        file_set_status($file, FILE_STATUS_PERMANENT);
404    
405      $module = $form_state['values']['docapi_doc_parser'];      $module = $form_state['values']['docapi_doc_parser'];
406      $plugin_id = db_result(db_query("SELECT plugin_id FROM {docapi_plugins} WHERE module = '%s'", $module));      $plugin_id = db_result(db_query("SELECT plugin_id FROM {docapi_plugins} WHERE module = '%s'", $module));
407    
# Line 350  Line 416 
416      }      }
417    
418      drupal_set_message(t('Sucessfully imported %file', array('%file' => $file->filename)));      drupal_set_message(t('Sucessfully imported %file', array('%file' => $file->filename)));
419    
420        $form_state['redirect'] = 'doc/library';
421    }    }
422    
423    /**    /**
424     * Displays a list of all documents that have been imported     * Displays a list of all documents that have been imported
425     */     */
426    function docapi_library() {    function docapi_library() {
427      $ret = '';      $output = '';
428    
429        $sql = 'SELECT dl.doc_id, dl.fid, f.filename, f.filemime, f.filesize, dp.name
430                  FROM {docapi_library} dl
431                  INNER JOIN {files} f on f.fid = dl.fid
432                  INNER JOIN {docapi_plugins} dp on dl.plugin_id = dp.plugin_id';
433    
434        $result = db_query($sql);
435    
436        $arr_rows = array();
437        while ($row = db_fetch_array($result)) {
438          $arr_row = array();
439    
440          foreach ($row as  $key => $value)
441            $arr_row[] = $value;
442    
443          $arr_row[] = l(t('Import'), 'doc/library/import/'. $arr_row[0]) .' '. l(t('Remove'), 'doc/library/delete/'. $arr_row[0]);
444    
445          $arr_rows[] = $arr_row;
446        }
447    
448        $output .= l('Add', 'node/add/doc') .' a new document.';
449        $output .= theme('table', array("Document ID", "File ID", "Filename", "Mimetype", "Filesize", "Plugin Name", "Actions"), $arr_rows);
450        return $output;
451      }
452    
453      /**
454       * Confirms the removal of a document.
455       *
456       * @param $doc_id
457       *   The document to remove
458       */
459      function docapi_library_remove_confirm_form($form_state, $doc_id = 0) {
460        // Load the filename
461        $filename = '';
462    
463        $form = array();
464    
465        if ($doc_id != 0) {
466          $filename = db_result(db_query("SELECT f.filename FROM {files} f INNER JOIN {docapi_library} dl ON dl.fid = f.fid WHERE dl.doc_id = %d", $doc_id));
467    
468          $form['message'] = array(
469            '#type' => 'markup',
470            '#value' => t('Are you sure you want to remove the document "%file"? <br />', array('%file' => $filename)),
471          );
472          $form['confirm'] = array(
473            '#type' => 'submit',
474            '#value' => t('Delete'),
475          );
476          $form['doc_id'] = array(
477            '#type' => 'value',
478            '#value' => $doc_id,
479          );
480    
481        }
482        else {
483          drupal_set_message(t('An error has occured, invalid document id supplied.'));
484        }
485    
486        return $form;
487      }
488    
489      /**
490       * Removes the document
491       */
492      function docapi_library_remove_confirm_form_submit($form, &$form_state) {
493        $doc_id = $form_state['values']['doc_id'];
494    
495        // Retrieve the fid and filepath
496        $file = db_fetch_array(db_query("SELECT f.fid, f.filepath FROM {files} f INNER JOIN {docapi_library} dl ON dl.fid = f.fid WHERE dl.doc_id = %d", $doc_id));
497    
498       // Remove the file
499       if (file_delete($file['filepath'])) {
500        db_query("DELETE FROM {files} WHERE fid = %d", $file['fid']);
501       }
502    
503       // Remove the document and it's metadata
504       db_query("DELETE FROM {docapi_library} WHERE doc_id = %d", $doc_id);
505       db_query("DELETE FROM {docapi_metadata} WHERE doc_id = %d", $doc_id);
506       db_query("DELETE FROM {docapi_doc_map} WHERE doc_id = %d", $doc_id);
507    
508       $form_state['redirect'] = 'doc/library';
509      }
510    
511      /**
512       * Converts a document to a node.
513       *
514       * @param $doc_id
515       *   The document to convert
516       * @return The form array structure
517       */
518      function docapi_library_import_form($form_state, $doc_id = 0) {
519        $form = array();
520    
521        if ($doc_id != 0) {
522          // Load the document mimetype
523          $sql = "SELECT f.filemime
524                    FROM {files} f
525                    INNER JOIN {docapi_library} dl ON f.fid = dl.fid
526                    WHERE dl.doc_id = %d";
527          $mimetype = db_result(db_query($sql, $doc_id));
528    
529          // Check to verify that metadata has been loaded
530          if (!empty($mimetype)) {
531    
532            // Store the doc_id
533            $form['docapi_convert_doc_id'] = array(
534              '#type' => 'value',
535              '#value' => $doc_id,
536            );
537    
538            // Create a list of mappings for the given mimetype
539            $mappings = array(0 => t('Select a Mapping'));
540    
541            $sql = "SELECT dmm.metadata_map_id, dmm.name
542                      FROM {docapi_metadata_map} dmm
543                      WHERE dmm.mimetype = '%s'";
544            $result = db_query($sql, $mimetype);
545    
546            while ($row = db_fetch_array($result)) {
547              $mappings[(string)$row['metadata_map_id']] = $row['name'];
548            }
549    
550            // Create the select element
551            $form['docapi_convert_map_id'] = array(
552              '#type' => 'select',
553              '#title' => t('Metadata Map'),
554              '#description' => t('Please select the metadata map to use during the import.'),
555              '#options' => $mappings,
556            );
557    
558            // The submit button
559            $form['docapi_convert_submit'] = array(
560              '#type' => 'submit',
561              '#value' => t('Convert'),
562            );
563          }
564          else {
565            drupal_set_message('Information not found for Document ID: '. $doc_id);
566          }
567        }
568    
569        return $form;
570      }
571    
572      /**
573       * Validatation hook for the import document form
574       */
575      function docapi_library_import_form_validate($form, $form_state) {
576        if (empty($form_state['values']['docapi_convert_map_id']))
577          form_set_error('docapi_convert_map_id', t('Invalid map specified, please select again or create a new one.'));
578      }
579    
580      /**
581       * Submit hook for the import document form
582       */
583      function docapi_library_import_form_submit($form, $form_state) {
584        $doc = array();
585        $map = array();
586        $type = '';
587    
588        // Load the document metadata
589        $sql = "SELECT dm.field, dm.meta_value
590                  FROM {docapi_metadata} dm
591                  WHERE dm.doc_id = %d";
592        $result = db_query($sql, $form_state['values']['docapi_convert_doc_id']);
593    
594        while ($row = db_fetch_array($result)) {
595          $doc[$row['field']] = $row['meta_value'];
596        }
597    
598        // Load the field mappings
599        $sql = "SELECT dmm.node_field, dmm.metadata_field
600                  FROM {docapi_metadata_mappings} dmm
601                  WHERE dmm.metadata_map_id = %d";
602        $result = db_query($sql, $form_state['values']['docapi_convert_map_id']);
603    
604        while ($row = db_fetch_array($result)) {
605          $map[$row['node_field']] = $row['metadata_field'];
606        }
607    
608        // Load the nodetype (we overwrite the type set in the mapping here)
609        $sql = "SELECT dmm.nodetype
610                  FROM {docapi_metadata_map} dmm
611                  WHERE dmm.metadata_map_id = %d";
612        $map['type'] = db_result(db_query($sql, $form_state['values']['docapi_convert_map_id']));
613    
614        // Generate the node
615        $node = new stdClass();
616    
617        foreach ($map as $key => $value) {
618          $node->$key = $doc[$value];
619        }
620    
621        node_save($node);
622    
623        drupal_set_message('<pre>'. var_export($node, TRUE) .'</pre>');
624      }
625    
626      /**
627       * Lists all field to node mappings
628       */
629      function docapi_mappings_list() {
630        $output = '';
631    
632        $sql = "SELECT dmm.metadata_map_id, dmm.name, dmm.mimetype, dmm.nodetype
633                  FROM {docapi_metadata_map} dmm";
634        $result = db_query($sql);
635    
636        $arr_rows = array();
637        while ($row = db_fetch_array($result)) {
638          $arr_row = array();
639    
640          $arr_row[] = $row['name'];
641          $arr_row[] = $row['mimetype'];
642          $arr_row[] = $row['nodetype'];
643    
644          $sql = "SELECT dmm.node_field, dmm.metadata_field
645                    FROM {docapi_metadata_mappings} dmm
646                    WHERE dmm.metadata_map_id = %d";
647          $result_detail = db_query($sql, $row['metadata_map_id']);
648    
649          $fields_cell = '<ul>';
650          while ($row_detail = db_fetch_array($result_detail)) {
651            $fields_cell .= '<li>'. ucwords($row_detail['node_field']) .': '. $row_detail['metadata_field'] .'</li>';
652          }
653          $fields_cell .= '</ul>';
654          $arr_row[] = $fields_cell;
655    
656          $arr_rows[] = $arr_row;
657        }
658    
659        $output .= theme('table', array('Mapping', 'Mimetype', 'Node Type', 'Mappings'), $arr_rows);
660    
661        return $output;
662      }
663    
664      /**
665       * Create a mapping between a node and document field
666       */
667      function docapi_mappings_add($form_state, $mimetype = '', $nodetype = '', $nid = 0) {
668        $form = array();
669    
670        // Turn caching on so AHAH doesn't complain
671        $form['#cache'] = TRUE;
672    
673        // Name of the map
674        $form['docapi_mapping_name'] = array(
675          '#type' => 'textfield',
676          '#title' => t('Name'),
677          '#description' => t('The unique name of the map.'),
678          '#required' => true,
679        );
680    
681        // List all mimetypes
682        $options = array('' => '');
683    
684        $sql = "SELECT DISTINCT f.filemime
685                  FROM {files} f
686                  INNER JOIN {docapi_library} dl ON f.fid = dl.fid";
687        $result = db_query($sql);
688        while ($row = db_fetch_array($result)) {
689          $options[$row['filemime']] = $row['filemime'];
690        }
691    
692        $form['docapi_mapping_mimetype'] = array(
693          '#type' => 'select',
694          '#title' => t('Mimetype'),
695          '#description' => t('The mimetype to use when generating the mapping.'),
696          '#options' => $options,
697          '#ahah' => array(
698            'path' => 'doc/js/add_nodetype_field',
699            'wrapper' => 'docapi_mapping_nodetype_wrapper',
700            'method' => 'replace',
701            'effect' => 'fade',
702          ),
703        );
704    
705        // Nodetype select box wrapper
706        $form['docapi_mapping_nodetype_wrapper'] = array(
707          '#type' => 'markup',
708          '#value' => '<div id="docapi_mapping_nodetype_wrapper"></div>',
709          '#ahah' => array(
710            'selector' => 'select.docapi_mapping_node_type',
711            'event' => 'change',
712            'path' => 'doc/js/add_mapping_add_fields',
713            'wrapper' => 'docapi_mapping_node_fields_wrapper',
714            'method' => 'replace',
715            'effect' => 'fade',
716          ),
717        );
718    
719        // Sample node select wrapper
720        $form['docapi_mapping_sample_node_wrapper'] = array(
721          '#type' => 'markup',
722          '#value' => '<div id="docapi_mapping_sample_node_wrapper"></div>',
723        );
724    
725        // Node fields wrapper
726        $form['docapi_mapping_node_fields_wrapper'] = array(
727          '#type' => 'markup',
728          '#value' => '<div id="docapi_mapping_node_fields_wrapper"></div>',
729        );
730    
731        // AHAH Bindings
732        $form['ahah_nodetype_binding'] = array(
733          '#type' => 'textfield',
734          '#prefix' => '<div style="display:none;">', //don't display textfield
735          '#suffix' => '</div>',
736          '#ahah' => array(
737            'path' => 'doc/js/add_node_field',
738            'wrapper' => 'docapi_mapping_sample_node_wrapper',
739            'method' => 'replace',
740            'effect' => 'fade',
741            'event' => 'change',
742            'selector' => 'select.docapi_mapping_nodetype',
743          ),
744        );
745        $form['ahah_sample_node_binding'] = array(
746          '#type' => 'textfield',
747          '#prefix' => '<div style="display:none;">', //don't display textfield
748          '#suffix' => '</div>',
749          '#ahah' => array(
750            'path' => 'doc/js/add_mapping_add_node_fields',
751            'wrapper' => 'docapi_mapping_node_fields_wrapper',
752            'method' => 'replace',
753            'effect' => 'fade',
754            'event' => 'change',
755            'selector' => 'select.docapi_mapping_sample_node',
756          ),
757        );
758    
759        return $form;
760      }
761    
762      /**
763       * Add the nodetype select box after we have a mimetype selected. This limits the addition of mappings to only
764       * nodetypes that we have defined.
765       *
766       * @return
767       *   A select box containing the list of nodetypes without a previously assigned mapping.
768       */
769      function docapi_mappings_add_nodetype() {
770        $output = '';
771        $options = array('' => '');
772        $types = node_get_types('names');
773        $form_elements = array();
774        $mimetype = check_plain($_POST['docapi_mapping_mimetype']);
775    
776        if (!empty($mimetype)) {
777          // Retrieve a list of nodetypes we have mappings for, and remove them
778          /*$sql = "SELECT DISTINCT dmm.nodetype
779                    FROM {docapi_metadata_map} dmm
780                    WHERE dmm.mimetype = '%s'";
781          $result = db_query($sql, $mimetype);
782          while ($row = db_fetch_array($result)) {
783            unset($types[$row['nodetype']]);
784          }*/
785    
786          // List all nodetypes
787          $form_elements['docapi_mapping_nodetype'] = array(
788            '#type' => 'select',
789            '#title' => t('Node Type'),
790            '#description' => t('The node type to load fields from'),
791            '#options' => array_merge($options, $types),
792            '#attributes' => array('class' => 'docapi_mapping_nodetype'),
793            '#ahah' => array(
794              'path' => 'test',
795              'wrapper' => 'docapi_mapping_sample_node_wrapper',
796              'method' => 'replace',
797              'effect' => 'fade',
798            ),
799          );
800    
801          drupal_alter('form', $form_elements, array(), 'docapi_mapping_js');
802    
803          //Build the new form
804          $form_state = array('submitted' => false);
805          $form_build_id = $_POST['form_build_id'];
806    
807          // Retrieve the form from the cache
808          $form = form_get_cache($form_build_id, $form_state);
809    
810          // Remove previous elements
811          if (is_array($form['docapi_mapping_nodetype_wrapper']))
812            foreach ($form['docapi_mapping_nodetype_wrapper'] as $field => $element) {
813              if (substr($field, 0, 1) != '#')
814                unset($form['docapi_mapping_nodetype_wrapper']);
815            }
816          if (is_array($form['docapi_mapping_sample_node_wrapper']))
817            foreach ($form['docapi_mapping_sample_node_wrapper'] as $field => $element) {
818              if (substr($field, 0, 1) != '#')
819                unset($form['docapi_mapping_sample_node_wrapper']);
820            }
821          if (is_array($form['docapi_mapping_node_fields_wrapper']))
822            foreach ($form['docapi_mapping_node_fields_wrapper'] as $field => $element) {
823              if (substr($field, 0, 1) != '#')
824                unset($form['docapi_mapping_node_fields_wrapper']);
825            }
826    
827          // Add the elements
828          foreach ($form_elements as $key => $form_element) {
829            $form['docapi_mapping_nodetype_wrapper'][$key] = $form_element;
830          }
831    
832          // Add the form back to the cache
833          form_set_cache($form_build_id, $form, $form_state);
834          $form += array(
835            '#post' => $_POST,
836            '#programmed' => FALSE,
837          );
838    
839          // Rebuild the form
840          $form = form_builder('docapi_add_mapping_form', $form, $form_state);
841    
842          // Render the output
843          $node_form = $form['docapi_mapping_nodetype_wrapper'];
844          unset($node_form['#prefix'], $node_form['#suffix']);
845          $output = theme('status_messages') . drupal_render($node_form);
846    
847          //remove class ahah-processed to refresh just this element
848          $output .= '<script>$(document).click(function(){$(document).unbind("click");$("#edit-ahah-nodetype-binding").removeClass("ahah-processed");Drupal.behaviors.ahah();});</script>';
849        }
850    
851        drupal_json(array('status' => 0, 'data' => $output));
852      }
853    
854      /**
855       * Add the sample node select element to the form, this allows the user to pick the example to pull fields from.
856       *
857       * @return
858       *   A select box containing a list of nodes based on the selected type.
859       */
860      function docapi_mappings_add_node() {
861        $type = check_plain($_POST['docapi_mapping_nodetype']);
862        $output = '';
863        $options = array('' => '');
864        $form_elements = array();
865    
866        // Retrieve a list of nodetypes we have mappings for
867        $sql = "SELECT DISTINCT n.nid, n.title
868                  FROM {node} n
869                  WHERE n.type = '%s'";
870        $result = db_query($sql, $type);
871        while ($row = db_fetch_array($result)) {
872          $options[$row['nid']] = $row['title'];
873        }
874    
875        if (count($options) > 1) {
876          // List all nodes
877          $form_elements['docapi_mapping_sample_node'] = array(
878            '#type' => 'select',
879            '#title' => t('Sample Node'),
880            '#description' => t('The node to load example fields / data'),
881            '#options' => $options,
882            '#attributes' => array('class' => 'docapi_mapping_sample_node'),
883          );
884    
885          drupal_alter('form', $form_elements, array(), 'docapi_mapping_js');
886    
887          //Build the new form
888          $form_state = array('submitted' => false);
889          $form_build_id = $_POST['form_build_id'];
890    
891          // Retrieve the form from the cache
892          $form = form_get_cache($form_build_id, $form_state);
893    
894          // Remove previous elements
895          if (is_array($form['docapi_mapping_sample_node_wrapper']))
896            foreach ($form['docapi_mapping_sample_node_wrapper'] as $field => $element) {
897              if (substr($field, 0, 1) != '#')
898                unset($form['docapi_mapping_sample_node_wrapper']);
899            }
900          if (is_array($form['docapi_mapping_node_fields_wrapper']))
901            foreach ($form['docapi_mapping_node_fields_wrapper'] as $field => $element) {
902              if (substr($field, 0, 1) != '#')
903                unset($form['docapi_mapping_node_fields_wrapper']);
904            }
905    
906          // Add the elements
907          foreach ($form_elements as $key => $form_element) {
908            $form['docapi_mapping_sample_node_wrapper'][$key] = $form_element;
909          }
910    
911          // Add the form back to the cache
912          form_set_cache($form_build_id, $form, $form_state);
913          $form += array(
914            '#post' => $_POST,
915            '#programmed' => FALSE,
916          );
917    
918          // Rebuild the form
919          $form = form_builder('docapi_add_mapping_form', $form, $form_state);
920    
921          // Render the output
922          $node_form = $form['docapi_mapping_sample_node_wrapper'];
923          unset($node_form['#prefix'], $node_form['#suffix']);
924          $output = theme('status_messages') . drupal_render($node_form);
925        }
926        else if ($type != '') {
927          $output = 'A sample node of type "'. $type .'" is not available, please '. l('create', 'node/add/'. $type) .' one before continuing.';
928        }
929    
930        //remove class ahah-processed to refresh just this element
931        $output .= '<script>$(document).click(function(){$(document).unbind("click");$("#edit-ahah-sample-node-binding").removeClass("ahah-processed");Drupal.behaviors.ahah();});</script>';
932    
933        drupal_json(array('status' => 0, 'data' => $output));
934      }
935    
936      /**
937       * Add the fields from the selected node to the form.
938       *
939       * @return
940       *   A collection of select boxes, each containing elements from the document. One select box per node field
941       */
942      function docapi_mappings_add_node_fields() {
943        // Retrieve the example node
944        $nid = check_plain($_POST['docapi_mapping_sample_node']);
945        $type = check_plain($_POST['docapi_mapping_nodetype']);
946        $node = node_load(array('nid' => $nid));
947        $metadata_fields = _get_metadata_fields();
948        $output = '';
949    
950        if (!empty($nid) && is_numeric($nid)) {
951          // Build the new elements
952          $form_elements = array();
953    
954          foreach ($node as $key => $value) {
955            $form_element = array(
956              '#type' => 'select',
957              '#title' => t(ucwords($type) .': %key_name', array('%key_name' => $key)),
958              '#description' => t('Please select the appropriate document metadata field that correlates to this field.'),
959              '#options' => $metadata_fields,
960            );
961    
962            if ($key != 'type')
963              $form_elements['docapi_mapping_node_'. $key] = $form_element;
964          }
965    
966          // Add the submit button
967          $form_elements['mapping_submit'] = array(
968            '#type' => 'submit',
969            '#value' => t('Map'),
970          );
971    
972          drupal_alter('form', $form_elements, array(), 'docapi_mapping_js');
973    
974          //Build the new form
975          $form_state = array('submitted' => false);
976          $form_build_id = $_POST['form_build_id'];
977    
978          // Retrieve the form from the cache
979          $form = form_get_cache($form_build_id, $form_state);
980    
981          // Remove previous elements
982          if (is_array($form['docapi_mapping_node_fields_wrapper']))
983            foreach ($form['docapi_mapping_node_fields_wrapper'] as $field => $element) {
984              if (substr($field, 0, 1) != '#')
985                unset($form['docapi_mapping_node_fields_wrapper']);
986            }
987    
988          // Add the elements
989          foreach ($form_elements as $key => $form_element) {
990            $form['docapi_mapping_node_fields_wrapper'][$key] = $form_element;
991          }
992    
993          // Add the form back to the cache
994          form_set_cache($form_build_id, $form, $form_state);
995          $form += array(
996            '#post' => $_POST,
997            '#programmed' => FALSE,
998          );
999    
1000          // Rebuild the form
1001          $form = form_builder('docapi_add_mapping_form', $form, $form_state);
1002    
1003          // Render the output
1004          $node_form = $form['docapi_mapping_node_fields_wrapper'];
1005          unset($node_form['#prefix'], $node_form['#suffix']);
1006          $output = theme('status_messages') . drupal_render($node_form);
1007        }
1008    
1009        drupal_json(array('status' => 0, 'data' => $output));
1010      }
1011    
1012      /**
1013       * Validation handler for creating a map between a node and document
1014       */
1015      function docapi_mappings_add_validate($form, &$form_state) {
1016    
1017      }
1018    
1019      /**
1020       * Submit handler for creating a map between a node and document
1021       */
1022      function docapi_mappings_add_submit($form, &$form_state) {
1023        // Create the map
1024        db_query("INSERT INTO {docapi_metadata_map} (name, mimetype, nodetype, nid) VALUES ('%s', '%s', '%s', %d)", $form_state['values']['docapi_mapping_name'], $form_state['values']['docapi_mapping_mimetype'], $form_state['values']['docapi_mapping_nodetype'], $form_state['values']['docapi_mapping_sample_node']);
1025        $map_id = db_last_insert_id('docapi_metadata_map', 'metadata_map_id');
1026    
1027        // Load the exmaple node
1028        $ex_node = node_load(array('nid' => $form_state['values']['docapi_mapping_sample_node']));
1029    
1030        // Create the mappings
1031        foreach ($ex_node as $key => $value) {
1032          if (!empty($form_state['values']['docapi_mapping_node_'. $key]) && $key != 'type')
1033            db_query("INSERT INTO {docapi_metadata_mappings} (metadata_map_id, node_field, metadata_field) VALUES (%d, '%s', '%s')", $map_id, $key, $form_state['values']['docapi_mapping_node_'. $key]);
1034        }
1035    
1036      return $ret;      $form_state['redirect'] = 'doc/mappings';
1037    }    }

Legend:
Removed from v.1.2.2.8  
changed lines
  Added in v.1.2.2.9

  ViewVC Help
Powered by ViewVC 1.1.2