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

Diff of /contributions/modules/freemind/freemind.module

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

revision 1.4, Tue Mar 17 19:30:01 2009 UTC revision 1.5, Tue Mar 17 19:34:08 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
 // $Id: freemind.module,v 1.3 2007/12/01 22:00:35 joelguesclin Exp $  
 /*  
  * Acknowledgments: this module could not have been produced without  
  * liberal hacking from the taxonomy_dhtml and xml_import modules  
  */  
   
 /*  
  * Module to render vocabularies in freemind node format  
  */  
 function freemind_format($fr_overview_vocab, $fr_description, $fr_taxlinks, $fr_nodes, $fr_teasers) {  
   $n=0;  
   $vocabularies = freemind_get_vocabularies($fr_overview_vocab);  
   foreach ($vocabularies as $vocabulary) {  
   
     $tree = taxonomy_get_tree($vocabulary->vid);  
   
 // get rid of $type here?  
     $tree_nodes = freemind_inject_nodes($tree, $type);  
   
     $freenodes[$n]["content"] = freemind_render_nodes($tree_nodes, $fr_description, $fr_taxlinks, $fr_nodes, $fr_teasers);  
     $freenodes[$n]["subject"] = $vocabulary->name;  
     $freenodes[$n]["subject-id"] = $vocabulary->vid;  
     $freenodes[$n]["subject-description"] = $vocabulary->description;  
     $freenodes[$n]["subject-nodes"] = $vocabulary->nodes;  
     $freenodes[$n]["subject-weight"] = $vocabulary->weight;  
     $freenodes[$n]["subject-hierarchy"] = $vocabulary->hierarchy;  
     $freenodes[$n]["subject-multiple"] = $vocabulary->multiple;  
     $freenodes[$n]["subject-relations"] = $vocabulary->relations;  
     $freenodes[$n]["subject-required"] = $vocabulary->required;  
     $n++;  
   }  
   
   return $freenodes ? $freenodes : array();  
 }  
   
 function freemind_get_vocabularies($fr_overview_vocab) {  
   $vocabularies = taxonomy_get_vocabularies($type);  
   foreach ($fr_overview_vocab as $omit) {  
     unset($vocabularies[$omit]);  
     }  
   return $vocabularies;  
 }  
   
 function freemind_menu($may_cache) {  
 global $base_url;  
    $output = "<script type=\"text/javascript\" src=\"".$base_url."/modules/freemind/flashobject.js\"></script>\n";  
    drupal_set_html_head($output);  
   
   if ($may_cache) {  
     $items[] = array('path' => 'freemind_map', 'title' => t("create freemind map"),  
     'callback' => 'freemind_page',  
     'access' => user_access('download map'),  
     'weight' => 5,  
     'type' => MENU_NORMAL_ITEM);  
     $items[] = array('path' => 'admin/taxonomy/freemind',  
       'title' => t('freemind'),  
       'access' => user_access('administer taxonomy'),  
       'callback' => 'freemind_xml_import_form',  
       'type' => MENU_LOCAL_TASK);  
     $items[] = array('path' => variable_get("freemind_flash",''), 'title' => t("flash map"),  
     'access' => user_access('access flash map'),  
     'weight' => 5,  
     'type' => MENU_NORMAL_ITEM);  
     $items[] = array('path' => variable_get("freemind_java",''), 'title' => t("java map"),  
     'access' => user_access('access java map'),  
     'weight' => 5,  
     'type' => MENU_NORMAL_ITEM);  
     return $items;  
   }  
  else {  
    }  
 }  
   
 /*  
  * given a taxonomy tree, add nodes below all relevant terms  
  */  
 function freemind_inject_nodes($tree, $type = NULL) {  
   $tree_node = $tree;  
   // iterate over the tree backwards, so I don't trip on the new items  
   for ($i=count($tree)-1; $i>=0 ; $i--) {  
     $term = $tree[$i];  
     /* restrict to a single type if given - should get rid of this since we don't use it*/  
     $type_q = ($type ? " AND n.type = '$type'" : "");  
 // Revised the query to get the teaser from the revisions table and remove unnecessary user info  
     $result = db_query_range("SELECT n.nid, n.title, n.type, r.teaser FROM {term_node} t LEFT JOIN {node} n ON t.nid = n.nid LEFT JOIN {node_revisions} r on r.vid = n.vid WHERE n.status = '1'  AND t.tid = '$term->tid' ORDER BY n.changed DESC", 0, variable_get("freemind_overview_count", 50));  
     while ($node = db_fetch_object($result)) {  
       $link = l($node->title, "node/$node->nid", array ("title" => $detail, "class" => "dhtml_node"));  
 // this line modified to include title and teaser  
 // and again to remove the link - this stuff needs tidying up  
       $term_node = (object) array ("nid" => $node->nid, "depth" => $term->depth+1, "link" => $link, "title" => $node->title, "teaser" => $node->teaser);  
   
       $part1 = array_slice($tree_node, 0, $i+1);  
       $part2 = array_slice($tree_node, $i+1, count($tree_node));  
       $part1[] = $term_node;  
       $tree_node = array_merge($part1, $part2);  
     }  
   }  
   return $tree_node;  
 }  
   
 /*  
  * This renders into Freemind format the description of a taxonomy term,  
  * or a node (the teaser in the latter case). The description can be folded  
  * or unfolded in the map  
  */  
 function freemind_render_desc($desc_var, $description) {  
   $desc = '';  
   if ($description != '') {  
     $desc = '<node COLOR="#999999" TEXT="desc" '.($desc_var == 'folded' ? 'FOLDED="true"' : 'FOLDED="false"' ).'><font NAME="Default" SIZE="10"/>';  
     $output .= "\r\n";  
     $desc .= '<node COLOR="#339900" TEXT="'.freemind_strings($description).'" STYLE="bubble"><edge COLOR="#999999" WIDTH="thin" STYLE="bezier"/><font NAME="Default" SIZE="10"/>';  
     $output .= "\r\n";  
     $desc .= "</node>\r\n</node>\r\n";  
     $output .= "\r\n";  
     }  
   return $desc;  
   
 }  
 /*  
  * Convert special characters to hex representation, because Freemind  
  * is picky that way (this was cribbed straight from php.net)  
  */  
 function freemind_spec2hex($str) {  
    $result = '';  
    for($i = 0; $i < strlen($str); $i++) {  
            if(ord(substr($str, $i, 1)) > 127 || ord(substr($str, $i, 1)) == 10){  
                $result .= '&#x'.bin2hex(substr($str, $i, 1)).';';  
            }  
            else{  
                $result .= substr($str, $i, 1);  
            }  
    }  
    return $result;  
 }  
   
 /*  
  * Kill the dubious quotes and double-quotes + desperate efforts to render  
  * even the teasers viewable by stripping all html tags from them + converting  
  * all special characters first out of UTF-8 and then into hex representation!  
  */  
 function freemind_strings($text, $strip_tags = FALSE) {  
        $badwordchars=array(  
                            "&lsquo;",  
                            "&rsquo;",  
                            "&ldquo;",  
                            "&rdquo;",  
                            "&quot;",  
                            "&#039;",  
                            "&#146;",  
                            "&#145;",  
                            "&ndash;",  
                            " & "  
                            );  
        $fixedwordchars=array(  
                            "'",  
                            "'",  
                            "'",  
                            "'",  
                            "'",  
                            "'",  
                            "'",  
                            "'",  
                            "-",  
                            " &amp; "  
                            );  
        $txt = trim($text);  
        $txt = htmlentities($txt,ENT_QUOTES,'UTF-8');  
        $txt = str_replace($badwordchars,$fixedwordchars,$txt);  
        $txt = html_entity_decode($txt, ENT_NOQUOTES, 'ISO8859-1');  
        if ($strip_tags) {$txt = strip_tags($txt);}  
        $txt = freemind_spec2hex($txt);  
        return $txt;  
    }  
 /*  
  * Puts the the tree in freemind node format  
  */  
 function freemind_render_nodes($tree, $fr_description, $fr_taxlinks, $fr_nodes, $fr_teasers) {  
   global $base_url;  
   $desc_var = $fr_description;  
   $taxlinks = ($fr_taxlinks == 1);  
   $nodes = ($fr_nodes == 1);  
   $teasers = ($fr_teasers == 1);  
   $old_depth = -1;  
   $output = "";  
   for ($m = 0; $m < count($tree); $m++) {  
     $term = $tree[$m];  
     $next_term_depth = $tree[$m+1]->depth;  
     if ($term->tid) {  
       $output .= '<node ID="tid_'.$term->tid.($taxlinks ? '" LINK="'.$base_url.'/taxonomy/term/'.$term->tid.'" ': '"' ).' TEXT="'.freemind_strings($term->name).'">';  
       $output .= "\r\n";  
 // This is where we put the Description indicator for level 3 terms (ie terms with no parents)  
       if ($fr_maptype == 'full') {  
         if ($term->parents[0] == 0) {  
           $output .= '<attribute NAME="Description" VALUE="No"/>';  
           $output .= "\r\n";  
           }  
         }  
       if ($desc_var != 'none') {$output .= freemind_render_desc($desc_var,trim(freemind_strings($term->description)));}  
       $current_tid_depth = $term->depth;  
       }  
     if ($term->nid) {  
       if ($nodes) {  
 // Stick the node teaser stuff in here - needs checking to see if it works  
         $output .= '<node ID="nid_'.$term->nid.'" TEXT="'.freemind_strings($term->title).'" LINK="'.$base_url.'/'.node.'/'.$term->nid.(!$teasers ? '" />' : '" FOLDED="true">');  
         if ($teasers) {  
           $output .= '<node COLOR="#339900" TEXT="'.freemind_strings($term->teaser, TRUE).'" STYLE="bubble"><edge COLOR="#999999" WIDTH="thin" STYLE="bezier"/><font NAME="Default" SIZE="10"/></node>';  
           $output .= '</node>';  
           $output .= "\r\n";  
           }  
         $output .= "\r\n";  
         }  
       }  
       if ($next_term_depth <= $current_tid_depth) {  
         $delta = $current_tid_depth - $next_term_depth + 1;  
         $output .= str_repeat("</node>\r\n", ($delta > 0 ? $delta : 0 ));  
         }  
   
     $old_depth = $term->depth;  
   }  
   return $output;  
 }  
 /*  
  * Freemind permissions (instantiates hook_perm)  
  */  
 function freemind_perm() {  
   return array('create server map','access java map','access flash map', 'download map' );  
   }  
   
 /*  
  * Freemind cron (instantiates hook_cron) - needs checking to see if it works  
  */  
 function freemind_cron() {  
   if (variable_get("freemind_cron", 0 ) == 1) {  
     $freemind_file = variable_get("file_directory_path",'').'/'.variable_get('freemind_path', 'freemind').'/'.variable_get("freemind_server_file", "index.mm");  
     freemind_build(variable_get("freemind_overview_vocab", array()), variable_get("freemind_maptype",'display'), variable_get("freemind_description",'folded'), variable_get("freemind_taxlinks", 0 ), variable_get("freemind_nodes", 0 ), variable_get("freemind_teasers", 0 ), variable_get("freemind_logo", 0 ), variable_get("freemind_server_title", "Site Map"), $freemind_file);  
     }  
 }  
 /*  
  * Freemind settings (instantiates hook_settings)  
  */  
 function freemind_settings() {  
   if (!file_check_directory(file_create_path(variable_get('freemind_path', 'freemind')), FILE_CREATE_DIRECTORY)) {  
     $error = theme('warning', t('The script directory does not exist, or is not writable.  The freemind module will not work in cron mode until you resolve this error.'));  
   }  
   $group1name = 'freemind_settings';  
   $form1[$group1name] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Default Freemind map settings'),  
     '#description' => t('If you generate the map via a cron job, then these settings will be used. If you generate the map on-line, then these are default settings which can be modified'),  
     '#collapsible' => TRUE,  
     '#collapsed' => FALSE,  
   );  
   $get1 = freemind_get_settings($group1name);  
   $group2name = 'freemind_server_settings';  
   $form2[$group2name] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Freemind server settings'),  
     '#description' => t('Server file and cron settings'),  
     '#collapsible' => TRUE,  
     '#collapsed' => FALSE,  
   );  
   $get2 = freemind_get_cron_settings($group2name);  
   $group3name = 'freemind_map_settings';  
   $form3[$group3name] = array(  
     '#type' => 'fieldset',  
     '#title' => t('Site map display settings'),  
     '#description' => t('Settings for display of the site map in Java applet or Flash viewer'),  
     '#collapsible' => TRUE,  
     '#collapsed' => FALSE,  
   );  
   $get3 = freemind_get_map_settings($group3name);  
   $form = array_merge_recursive($form1, $get1, $form2, $get2, $form3, $get3);  
   return $form;  
 }  
   
 function freemind_get_settings($groupname, $select="") {  
   $vocabularies = taxonomy_get_vocabularies();  
   $select[0] = "<". t("none") .">";  
   foreach ($vocabularies as $vocabulary) {  
     $select[$vocabulary->vid] = $vocabulary->name;  
   }  
   $form[$groupname]['freemind_overview_vocab'] = array(  
     '#type'          => 'select',  
     '#title'         => t('Omitted vocabularies'),  
     '#default_value' => variable_get("freemind_overview_vocab", array()),  
     '#options'       => $select,  
     '#description'   => t('Select vocabularies which should be <b>omitted</b> from listings.'),  
     '#multiple'      => TRUE,  
     '#required'      => FALSE,  
   );  
   $form[$groupname]['freemind_maptype'] = array(  
     '#type'          => 'select',  
     '#title'         => t('Type of map produced'),  
     '#default_value' => variable_get("freemind_maptype", 'display'),  
     '#options'       => array('full' => 'Include vocabulary attributes', 'display' => 'Generate display only'),  
     '#description'   => t("A 'display only' map is suitable for mapping the content of the site and is usually the most suitable choice. A 'vocabulary attributes' map is intended to allow freemind to be used, in a later version, as a graphical taxonomy manipulation tool"),  
     '#multiple'      => FALSE,  
     '#required'      => TRUE,  
   );  
   $form[$groupname]['freemind_taxlinks'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Include taxonomy links?'),  
     '#default_value' => variable_get('freemind_taxlinks', 0),  
     '#description' => t("Check this box to have links included in the map for each taxonomy term; otherwise, only nodes will show links, if selected"),  
     '#return_value' => 1,  
     '#required' => FALSE,  
   );  
   $form[$groupname]['freemind_nodes'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Include nodes?'),  
     '#default_value' => variable_get('freemind_nodes', 0),  
     '#description' => t("Check this box to have nodes included in the map"),  
     '#return_value' => 1,  
     '#required' => FALSE,  
   );  
   $form[$groupname]['freemind_logo'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Include Logo?'),  
     '#default_value' => variable_get('freemind_logo', 0),  
     '#description' => t("Check this box to have a logo included in the centre of the map (logo must be loaded to modules/freemind/logo.gif"),  
     '#return_value' => 1,  
     '#required' => FALSE,  
   );  
   $form[$groupname]['freemind_server_title'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Map title'),  
     '#size' => 50,  
     '#maxlength' => 80,  
     '#default_value' => variable_get("freemind_server_title", "Site Map"),  
     '#description' => t("Name for the map's central node. Note required if a logo is used instead"),  
     '#required' => TRUE,  
   );  
   return $form;  
 }  
   
 /*  
  * Format the settings for cron jobs and server files  
  */  
 function freemind_get_cron_settings($groupname) {  
   
   $form[$groupname]['freemind_cron'] = array(  
     '#type' => 'checkbox',  
     '#title' => t('Produce Freemind map in cron run?'),  
     '#default_value' => variable_get('freemind_cron', 0),  
     '#description' => t("Check this box to have Drupal's cron run produce the Freemind map. In this case you must make sure that the default server file name is included in the configuration (see below)"),  
     '#return_value' => 1,  
     '#required' => FALSE,  
   );  
   $form[$groupname]['freemind_path'] = array(  
     '#type' => 'textfield',  
     '#title' => t('File path name'),  
     '#size' => 50,  
     '#maxlength' => 80,  
     '#default_value' => variable_get("freemind_path", "freemind"),  
     '#description' => t("The path name for maps produced on the server. Should be present if cron is set to produce maps"),  
     '#required' => TRUE,  
   );  
   
   $form[$groupname]['freemind_server_file'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Default server file name'),  
     '#size' => 50,  
     '#maxlength' => 80,  
     '#default_value' => variable_get("freemind_server_file", "index.mm"),  
     '#description' => t("The default file name for maps produced on the server. Should be present if cron is set to produce maps"),  
     '#required' => TRUE,  
   );  
   
   return $form;  
 }  
   
 /*  
  * Format the settings for Java applet and flash map display  
  */  
 function freemind_get_map_settings($groupname) {  
   
   $form[$groupname]['freemind_java'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Freemind Java map'),  
     '#size' => 50,  
     '#maxlength' => 80,  
     '#default_value' => (variable_get("freemind_java", variable_get("file_directory_path",'').'/'.variable_get('freemind_path', 'freemind').'/sitemapjava.html')),  
     '#description' => t("Fully qualified path to html page for displaying site maps in Java applet. This is only useful if there are users who have Java map permissions, in which case they will see the link in their navigation menu"),  
     '#required' => FALSE,  
   );  
   $form[$groupname]['freemind_flash'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Freemind Flash map'),  
     '#size' => 50,  
     '#maxlength' => 80,  
     '#default_value' => (variable_get("freemind_flash", variable_get("file_directory_path",'').'/'.variable_get('freemind_path', 'freemind').'/sitemapflash.html')),  
     '#description' => t("Fully qualified path to html page for displaying site maps in Flash viewer. This is only useful if there are users who have Flash map permissions, in which case they will see the link in their navigation menu"),  
     '#required' => FALSE,  
   );  
   return $form;  
 }  
   
 /*  
  * Create the attributes registry at the beginning of the map  
  */  
 function freemind_attributes_registry() {  
   $att_reg = '<attribute_registry FONT_SIZE="10" SHOW_ATTRIBUTES="selected">';  
   $att_reg .= '<attribute_name NAME="hierarchy" RESTRICTED="true" VISIBLE="true">';  
   $att_reg .= '<attribute_value VALUE="Disabled"/>';  
   $att_reg .= '<attribute_value VALUE="Single"/>';  
   $att_reg .= '<attribute_value VALUE="Multiple"/>';  
   $att_reg .= '</attribute_name>';  
   $att_reg .= '<attribute_name NAME="multiple" RESTRICTED="true" VISIBLE="true">';  
   $att_reg .= '<attribute_value VALUE="Yes"/>';  
   $att_reg .= '<attribute_value VALUE="No"/>';  
   $att_reg .= '</attribute_name>';  
   $att_reg .= '<attribute_name NAME="related" RESTRICTED="true" VISIBLE="true">';  
   $att_reg .= '<attribute_value VALUE="Yes"/>';  
   $att_reg .= '<attribute_value VALUE="No"/>';  
   $att_reg .= '</attribute_name>';  
   $att_reg .= '<attribute_name NAME="Description" RESTRICTED="true">';  
   $att_reg .= '<attribute_value VALUE="Yes"/>';  
   $att_reg .= '<attribute_value VALUE="No"/>';  
   $att_reg .= '</attribute_name>';  
   $att_reg .= '<attribute_name NAME="nodes" RESTRICTED="true" VISIBLE="true">';  
   foreach (node_get_types() as $key => $name) {  
     $att_reg .= '<attribute_value VALUE="'.$key.'"/>';  
     }  
   $att_reg .= '</attribute_name>';  
   $att_reg .= "</attribute_registry>";  
   
   return $att_reg;  
 }  
2    
3  // Format an attribute node  /**
4  function freemind_format_attribute($vid,$attribute,$attribute_val) {   * Plugin manager for the freemind module.
5  $attr_label = $attribute;   *
6  $attr = '';   * @staticvar array $plugins array with the list of the plugins
7  switch ($attribute) {   * @param string $op It can be 'add' or 'get'.
8    case 'hierarchy':   * @param string $arg If the $op is 'add', this parameter is the value.
9      switch ($attribute_val) {   * @return boolean|array
10        case 0:   * array if the $op=='get', true if the plugin added,
11          $attribute = 'Disabled';   * false if the plugin is already in the $plugins array,
12          break;   * null if the operation is unknown
13        case 1:   */
14          $attribute = 'Single';  function freemind_plugin_manager($op, $arg = NULL){
15          break;    static $plugins = array();
16        case 2:    switch($op){
17          $attribute = 'Multiple';      case 'add':
18          break;        if(!in_array($arg, $plugins) && $arg !== NULL){
19        default:          $plugins []= $arg;
20          break;          return true;
21        }        }
22        $attr = '<attribute NAME="'.$attr_label.'" VALUE="'.$attribute.'"/>';        return false;
23        break;        break;
24    case 'multiple':      case 'get':
25      switch ($attribute_val) {        return $plugins;
       case 0:  
         $attribute = 'No';  
         break;  
       case 1:  
         $attribute = 'Yes';  
         break;  
       default:  
         break;  
      }  
      $attr = '<attribute NAME="'.$attr_label.'" VALUE="'.$attribute.'"/>';  
      break;  
   case 'related':  
     switch ($attribute_val) {  
       case 0:  
         $attribute = 'No';  
         break;  
       case 1:  
         $attribute = 'Yes';  
         break;  
       default:  
         break;  
       }  
       $attr = '<attribute NAME="'.$attr_label.'" VALUE="'.$attribute.'"/>';  
26        break;        break;
   case 'nodes':  
 //    $attribute = implode(',',$attribute_val);  
     foreach ($attribute_val as $node_type) {  
       $attr .= '<attribute NAME="nodes" VALUE="'.$node_type.'"/>';  
       }  
     break;  
   case 'Description':  
     $attr .= '<node COLOR="#339900" TEXT="'.freemind_strings($attribute_val).'" STYLE="bubble"><edge COLOR="#999999" WIDTH="thin" STYLE="bezier"/><font NAME="Default" SIZE="10"/>';  
     $attr .= '<attribute NAME="Description" VALUE="Yes"/>';  
     $attr .= '</node>';  
     break;  
   default:  
     break;  
27    }    }
28    return $attr;    return null;
   
29  }  }
 // Format the attributes of a vocabulary for Freemind  
 function freemind_format_node_attributes($vid,$hierarchy,$multiple,$related,$nodes, $desc) {  
   $attribs = "";  
   $attribs .= freemind_format_attribute($vid,'hierarchy',$hierarchy);  
   $attribs .= freemind_format_attribute($vid,'multiple',$multiple);  
   $attribs .= freemind_format_attribute($vid,'related',$related);  
   $attribs .= freemind_format_attribute($vid,'nodes',$nodes);  
   if (trim($desc) != "") {$attribs .= freemind_format_attribute($vid,'Description',$desc);}  
   return $attribs;  
   
 }  
   
 /* Allows the user to modify the default  
    parameters for the site map, and to choose whether to build  
    it on the server, or to download it to his workstation  
 */  
 function freemind_page() {  
   $edit = $_POST['edit'];  
   $op = $_POST['op'];  
 // Needs changing to add permissions for creating site map on server  
  if (!$op || $op == t('Change file')) {  
   $output = t("You can use this page to modify the default settings for the site map if you wish");  
     if (user_access('create map')) {  
       $form['freemind_select_map'] = array(  
         '#type' => 'fieldset',  
         '#title' => t('Select server or download to workstation'),  
         '#description' => t('If you select the sitemap on the server, then you must provide a file name'),  
       );  
       $form['freemind_select_map']['freemind_where'] = array(  
         '#type' => 'radios',  
         '#title' => t(''),  
         '#default_value' => 'server',  
         '#options'       => array(  
           "server" => t("Build site map on the server"),  
           "download" => t("Download site map to workstation")),  
         '#required'      => TRUE,  
         '#collapsible' => FALSE,  
         '#collapsed' => FALSE,  
       );  
       }  
     else {  
       $form['freemind_where'] = array(  
         '#type' => 'hidden',  
         '#default_value' => 'download',  
       );  
       }  
     $form['default_freemind_map_settings'] = array(  
       '#type' => 'fieldset',  
       '#title' => t('Default Freemind map settings'),  
       '#description' => t("You can modify these settings for your map"),  
       '#collapsible' => FALSE,  
       '#collapsed' => FALSE,  
     );  
     $get1 = freemind_get_settings('default_freemind_map_settings', $select);  
     $form = array_merge_recursive($form, $get1);  
     $form['default_freemind_map_settings']['freemind_server_file'] = array(  
       '#type' => 'textfield',  
       '#title' => t('File name on server'),  
       '#size' => 50,  
       '#maxlength' => 80,  
       '#default_value' => variable_get("freemind_server_file", "index.mm"),  
       '#description' => "The file will be created in : ".file_create_path(variable_get('freemind_path', 'freemind')),  
       '#required' => TRUE,  
     );  
     $form['default_freemind_map_settings']['submit'] = array(  
       '#type' => 'submit',  
       "#value" => t('Generate map'),  
     );  
     $form['#method'] = 'post';  
     $output = drupal_get_form('freemind_map_settings', $form);  
     }  
   else {  
   $output = variable_get("file_directory_path",'').'/'.variable_get('freemind_path', 'freemind').'/'.$edit['freemind_server_file'];  
     if ($edit['freemind_where'] == 'download') {  
 // Need to give a non-conflicting temp file name  
       $freemind_file = file_create_path(variable_get("file_directory_temp",FILE_DIRECTORY_TEMP).'/freemind.mm');  
       }  
     else {  
       $freemind_file = variable_get("file_directory_path",'').'/'.variable_get('freemind_path', 'freemind').'/'.$edit['freemind_server_file'];  
       }  
     freemind_build($edit['freemind_overview_vocab'], $edit['freemind_maptype'], $edit['freemind_description'], $edit['freemind_taxlinks'], $edit['freemind_nodes'], $edit['freemind_teasers'], $edit['freemind_logo'], $edit['freemind_server_title'], $freemind_file);  
     if ($edit['freemind_where'] == 'download') {  
         $headers[] = 'Content-type: application/mm';  
         $headers[] = 'Content-Disposition: attachment; filename="sitemap.mm"';  
         file_transfer($freemind_file, $headers);  
       }  
     else {  
       $output = "The new file was built ok";  
       }  
     }  
   print theme("page",$output);  
 }  
   
 function freemind_build($fr_overview_vocab, $fr_maptype, $fr_description, $fr_taxlinks, $fr_nodes, $fr_teasers, $fr_logo, $fr_server_title, $file_name) {  
   global $base_url;  
   $freenodes = freemind_format($fr_overview_vocab, $fr_description, $fr_taxlinks, $fr_nodes, $fr_teasers);  
 // write out the Freemind structure  
   $mmfile = fopen($file_name, 'w');  
   fwrite($mmfile, '<map version="0.8.1">');  
   if ($fr_maptype == 'full') {  
     fwrite($mmfile, freemind_attributes_registry());  
     }  
   fwrite($mmfile, '<node ID="Drupal_Sitemap" '.($fr_logo == 1 ? 'TEXT="&lt;html&gt;&lt;img src=&quot;'.$base_url.'/modules/freemind/logo.gif&quot;&gt;"' : 'TEXT="'.$fr_server_title.'"').' >');  
   $vidi = 0;  
   foreach ($freenodes as $freenode) {  
     $vidi++;  
     $vid_pos = ($vidi % 2 != 0) ? 'right' : 'left';  
     fwrite($mmfile,'<node ID="vid_'.$freenode['subject-id'].'" TEXT="'.freemind_strings($freenode['subject']). '" POSITION="'.$vid_pos.'">');  
 // This bit does the attributes for the vocabulary  
     if ($fr_maptype == 'full') {  
        fwrite($mmfile,freemind_format_node_attributes($freenode['subject-id'],$freenode['subject-hierarchy'],$freenode['subject-multiple'],$freenode['subject-related'],$freenode['subject-nodes'],$freenode['subject-description']));  
        }  
 // get the vocabulary content and put it here  
     fwrite($mmfile,$freenode['content']);  
 // write the end of the Vocabulary  
     fwrite($mmfile,'</node>');  
     }  
 // write the end of the home node  
   fwrite($mmfile,'</node>');  
 // end of map  
   fwrite($mmfile,'</map>');  
   fclose($mmfile);  
   
 }  
 // FROM HERE ON IT IS THE IMPORTING BIT ++++++++++++++++++++++++++++  
 //  
   
 function freemind_load_map_form_submit($form_id, $form_values) {  
   $edit = $_POST['edit'];  
   if ($file = file_check_upload("xml")) {  
     $fd = fopen($file->filepath, "rb");  
     if (!$fd) {  
       $output = '<p>' . t('Vocabulary import failed: file %filename cannot be read.', array('%filename' => "<em>$file->filename</em>")) . "</p>\n";  
     } else {  
       $info = fstat($fd);  
       $len = $info["size"];  
       $text = fread($fd, $len);  
       fclose($fd);  
           $output = '<p>' . t('Loaded file %filename.', array('%filename' => "<i>$file->filename</i>")) . "</p>\n";  
         $test = '';  
       $output .= '<p>The file is '.$len.' bytes : START <br />';  
       $output .= freemind_xml_parse($file->filepath);  
       $output .= 'END';  
     }  
   } else {  
     $output = '<p>' . t('Vocabulary import failed: file was not uploaded.') . "</p>\n";  
   }  
   drupal_set_message("The taxonomy was loaded");  
   print theme("page",$output);  
 }  
   
 /**  
  * freemind_xml_import_form  
  */  
 function freemind_xml_import_form() {  
 $form["xml"] = array(  
   '#type' => 'file',  
   '#title' => "From file",  
   '#size' => 50,  
   '#description' => t("Click \"Browse...\" to select a modified Freemind sitemap to upload."),  
   );  
 $form[] = array(  
   '#type' => 'submit',  
   '#value' => t("Load"),  
   );  
 $form['#attributes'] = array('enctype' => 'multipart/form-data');  
 $output = drupal_get_form('freemind_load_map_form', $form);  
 return $output;  
 }  
 /**  
  * Save a term  
  */  
 function freemind_save_tid($term_details) {  
   global $term, $element, $tag, $mkterms, $mkdepth, $freemind_vocab_attributes, $freemind_vocab_desc, $freemind_term, $freemind_term_desc, $freemind_vocab, $freemind_parents, $freemind_last_tid_depth;  
   if (!is_array($term_details)) {  
     $freemind_term = array();  
     $freemind_term['name'] = $term_details;  
     $freemind_term['vid'] = $freemind_vocab['vid'];  
     $mkterms[] = 'In - '.$mkdepth .' Tid - '.$freemind_term['name'].'<br />';  
 // Here we work out whether the Term has a parent  
     if ($mkdepth > 3) {  
 //      if ($freemind_last_tid_depth > $mkdepth) {  
         $freemind_term['parent'] = $freemind_parents[$mkdepth - 1];  
       }  
     if (taxonomy_save_term($freemind_term) == SAVED_NEW) {  
       drupal_set_message(t('Created term %term in vocabulary %vocab.', array('%term' => theme('placeholder', $freemind_term['name']), '%vocab' => theme('placeholder', $freemind_vocab['name']))));  
       $freemind_parents[$mkdepth] = $freemind_term['tid'];  
 //      $freemind_term = array();  
       $freemind_last_tid_depth = $mkdepth;  
       }  
     }  
   else {  
 print("In principle this should not happen!<br/>");  
     }  
30    
31    # dynamic plugin loading, harrr
32    foreach( file_scan_directory(drupal_get_path('module', 'freemind'), 'plugin\.[^\.]*\.inc') as $f ) {
33      include_once $f->basename;
34      freemind_plugin_manager('add', substr($f->name, 7)); # remove the 'plugin.' part
35  }  }
36    
37  /**  /**
38   * Associate a node with a term   * Works the same as the module_invoke_all(),
39     * but this function is for this module's plugins
40     *
41     * @param string $hook the name of the hook
42     * @param array $args array of arguments, which will be passed to every called function
43     * @return array merged array of the functions' return values
44   */   */
45  function freemind_save_nid($nid) {  function _freemind_plugin_invoke_all($hook, $args = array()){
46    global $term, $element, $tag, $mkterms, $mkdepth, $freemind_vocab_attributes, $freemind_vocab_desc, $freemind_term, $freemind_term_desc, $freemind_vocab, $freemind_parents;    $items = array();
47      foreach(freemind_plugin_manager('get') as $p){
48    taxonomy_node_save($nid, array($freemind_parents[$mkdepth - 1]));      if(function_exists($p.'_'.$hook))
49        $items = array_merge_recursive($items, call_user_func_array($p.'_'.$hook, $args));
 }  
   
 /**  
  * Call-back for the XML parser.  
  */  
 function freemind_xml_element_start($parser, $name, $attributes) {  
   global $term, $element, $tag, $mkterms, $mkdepth, $freemind_vocab_attributes, $freemind_vocab_desc, $freemind_term, $freemind_term_desc, $freemind_vocab, $freemind_parents, $freemind_node_attributes;  
   switch ($name)  {  
    case 'node':  
     $freemind_node_attributes = $attributes;  
     $mkdepth += 1;  
     $mkterms[] = 'SE - '.$mkdepth .' '.$name.' ID - '.$attributes['ID'].' TEXT - '.$attributes['TEXT'].' '.$freemind_global_test.' '.$freemind_term_desc.'<br />';  
     switch ($mkdepth) {  
       case 0:  
         break;  
       case 1:  
       $freemind_global_test = "Global test";  
       $freemind_term_desc = FALSE;  
         break;  
       case 2:  
 // Level 2 is necessarily a new or existing vocabulary  
         if (substr($attributes['ID'],0,4) == 'vid_') {  
           if (taxonomy_del_vocabulary(substr($attributes['ID'],4))) {  
             drupal_set_message(t('Deleted vocabulary %name.', array('%name' => theme('placeholder', $attributes['TEXT']))));  
             }  
           }  
         $freemind_vocab = array();  
         $freemind_vocab['name'] = $attributes['TEXT'];  
 // hierarchy by default - needs adapting - probably could get rid of this line  
         $freemind_vocab['hierarchy'] = 1;  
         if (taxonomy_save_vocabulary($freemind_vocab)) {  
           drupal_set_message(t('Created vocabulary %name.', array('%name' => theme('placeholder', $freemind_vocab['name']))));  
           }  
         else {  
           drupal_set_message(t('Failed to create vocabulary %name.', array('%name' => theme('placeholder', $freemind_vocab['name']))));  
           }  
         break;  
       case 3:  
 // It's a term or a vocabulary description  
           $freemind_vocab['description'] = $attributes['TEXT'];  
           $freemind_term['title'] = $attributes['TEXT'];  
         break;  
        default:  // All values above 3 handled the same  
          if ($freemind_term_desc) {  
 // It's a term description  
            $freemind_term_desc = FALSE;  
            $freemind_term['description'] = $attributes['TEXT'];  
            taxonomy_save_term($freemind_term);  
            }  
 //       elseif (substr($attributes['ID'],0,4) == 'nid_') {  
          elseif (array_key_exists('LINK', $attributes)) {  
 // It's a node  
            freemind_save_nid(end(explode("/", $attributes['LINK'])));  
            }  
          elseif ($attributes['TEXT'] == 'desc') {  
 //It's a term description flag  
            $freemind_term_desc = TRUE;  
            }  
          else {  
 // It's a term  
            freemind_save_tid($attributes['TEXT']);  
            }  
       }  
      break;  
 // The tag is an 'attribute'  
    case 'attribute':  
     $mkterms[] = 'SE - '.$mkdepth .' '.$name.' NAME - '.$attributes['NAME'].' VALUE - '.$attributes['VALUE'].' '.$freemind_global_test.' '.$freemind_term_desc.'<br />';  
     switch ($mkdepth) {  
       case 0:  
         break;  
       case 1:  
         break;  
       case 2:  
 // This is a vocabulary attribute  
         if ($attributes['NAME'] == 'hierarchy') {  
            switch ($attributes['VALUE']) {  
              case 'disabled':  
                $freemind_vocab['hierarchy'] = 0;  
                break;  
              case 'single':  
                $freemind_vocab['hierarchy'] = 1;  
                break;  
              case 'multiple':  
                $freemind_vocab['hierarchy'] = 2;  
                break;  
              default:  
                break;  
            }  
           }  
         elseif ($attributes['NAME'] == 'nodes') {  
           $freemind_vocab['nodes'][$attributes['VALUE']] = 1;  
 //        $node_types = explode(",", $attributes['VALUE']);  
 // PHP5 only  
 //        $freemind_vocab['nodes'] = array_combine($node_types, array_fill(0, count($node_types), 1));  
 //        foreach ($node_types as $type) {$nodes[$type] = 1;}  
           }  
         else {  
           $freemind_vocab[$attributes['NAME']] = $attributes['VALUE'] == 'Yes' ? 1 : 0;  
           }  
         taxonomy_save_vocabulary($freemind_vocab);  
         print_r($freemind_vocab);  
         print("<br />");  
         break;  
       case 3:  
 //        If Description is "Yes" then this is a vocabulary description  
         if ($attributes['NAME'] == 'Description' && $attributes['VALUE'] == 'Yes') {  
           $freemind_vocab_desc = TRUE;  
           }  
 //        If Description is "No" then this is a term  
         if ($attributes['NAME'] == 'Description' && $attributes['VALUE'] == 'No') {  
           freemind_save_tid($freemind_term['title']);  
           }  
         break;  
       default:  
         break;  
       }  
     default:  
       break;  
     }  
   $tag = $name;  
 }  
   
 /**  
  * Call-back for the XML parser.  
  */  
 function freemind_xml_element_end($parser, $name) {  
   global $term, $element, $tag, $mkterms, $mkdepth, $freemind_vocab_attributes, $freemind_vocab_desc, $freemind_term, $freemind_term_desc, $freemind_vocab, $freemind_parents, $freemind_node_attributes;  
   if ($name == 'node') {  
 //    $mkdepth -= 1;  
     $mkterms[] = 'EE - '.$mkdepth .' '.$name.' ID - '.$freemind_node_attributes['ID'].' TEXT - '.$freemind_node_attributes['TEXT'].' '.$freemind_global_test.' '.$freemind_term_desc.'<br />';  
     switch ($mkdepth) {  
       case 1:  
         break;  
       case 2:  
 /*        if (taxonomy_save_vocabulary($freemind_vocab)) {  
           drupal_set_message(t('Created vocabulary %name.', array('%name' => theme('placeholder', $freemind_vocab['name']))));  
           }  
         else {  
           drupal_set_message(t('Failed to create vocabulary %name.', array('%name' => theme('placeholder', $freemind_vocab['name']))));  
           }*/  
         break;  
       case 3:  
         if ($freemind_vocab_desc) {  
           taxonomy_save_vocabulary($freemind_vocab);  
           $freemind_vocab_desc = FALSE;  
           }  
 /*        else {  
           freemind_save_tid($freemind_node_attributes['TEXT']);  
           }*/  
       default:  
     }  
     $mkdepth -= 1;  
   }  
   
 }  
   
 /**  
  * Call-back for the XML parser. This is currently a dummy function  
  */  
 function freemind_xml_element_data($parser, $data) {  
   
 }  
   
 //function freemind_xml_parse(&$data, $vid) {  
 function freemind_xml_parse($filedetail) {  
   global $terms, $vocabulary, $mkterms, $mkdepth;  
   
   // Unset the global variables before we use them:  
   unset($GLOBALS['element'], $GLOBALS['term'], $GLOBALS['tag']);  
   $terms = array();  
   $mkterms = array();  
   $mkdepth = 0;  
   $vocabulary = array();  
         $document = file($filedetail);  
 //  not sure why i didn't use drupal_xml_parser_create here: must be a good reason  
   $xml_parser = xml_parser_create();  
   xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); //  
   xml_set_element_handler($xml_parser, 'freemind_xml_element_start', 'freemind_xml_element_end');  
   xml_set_character_data_handler($xml_parser, 'freemind_xml_element_data');  
   
   foreach ($document as $line) {  
     xml_parse($xml_parser, $line);  
50    }    }
51      return $items;
   xml_parser_free($xml_parser);  
   $test = implode(' ', $mkterms);  
   return $test;  
52  }  }
53    
54  /**  /**
55   * Implementation of hook_help().   * Implementation of hook_menu()
56     *
57     * @return array merged array of the return values
58   */   */
59  function freemind_help($section) {  function freemind_menu(){
60    switch ($section) {    return _freemind_plugin_invoke_all('freemind_plugin_menu');
     case 'admin/modules#description':  
       return t('Creates Freemind site maps');  
 /*    case 'admin/checkout':  
       return t("Users automatically check-out a document when they load it for modification.  Documents are checked back in once that user migrates to a different page in the system.  Administrators can also check documents back in via content administration.");  
     case 'admin/help#checkout':  
       return t("  
 <h3>Document management</h3>  
   
 ");*/  
   
   }  
61  }  }
   
 ?>  

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.2