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

Diff of /contributions/modules/word2web/word2web.module

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

revision 1.6.2.1, Wed Jan 14 14:30:08 2009 UTC revision 1.6.2.2, Thu Jan 22 00:27:32 2009 UTC
# Line 9  Line 9 
9   * This module utilizes XSLT transforms to go from "export to web"   * This module utilizes XSLT transforms to go from "export to web"
10   * Word documents to clean, valid XHTML.   * Word documents to clean, valid XHTML.
11   *   *
  * TODO: Various adjacent tags are getting their whitespace stripped out -- fix  
12   */   */
13    
14  /**  /**
# Line 17  Line 16 
16   */   */
17  function word2web_menu() {  function word2web_menu() {
18    $items = array();    $items = array();
19  /* TODO  
    Non menu code that was placed in hook_menu under the '!$may_cache' block  
    so that it could be run during initialization, should now be moved to hook_init.  
    Previously we called hook_init twice, once early in the bootstrap process, second  
    just after the bootstrap has finished. The first instance is now called boot  
    instead of init.  
   
    In Drupal 6, there are now two hooks that can be used by modules to execute code  
    at the beginning of a page request. hook_boot() replaces hook_boot() in Drupal 5  
    and runs on each page request, even for cached pages. hook_boot() now only runs  
    for non-cached pages and thus can be used for code that was previously placed in  
    hook_menu() with $may_cache = FALSE:  
   
    Dynamic menu items under a '!$may_cache' block can often be simplified  
    to remove references to arg(n) and use of '%<function-name>' to check  
    conditions. See http://drupal.org/node/103114.  
   
    The title and description arguments should not have strings wrapped in t(),  
    because translation of these happen in a later stage in the menu system.  
 */  
20    $items['admin/settings/word2web'] = array(    $items['admin/settings/word2web'] = array(
21      'title' => 'Word2Web Settings',      'title' => 'Word2Web Settings',
22      'description' => 'Configure word2web nodetypes and settings.',      'description' => 'Configure word2web nodetypes and settings.',
23      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
24      'page arguments' => array('word2web_settings_form'),      'page arguments' => array('word2web_settings_form'),
25      'access arguments' => array('administer site configuration'),      'access arguments' => array('administer site configuration'),
26    );    );
27    if (is_numeric(arg(1)) && arg(0) == 'node') {    $items['node/'.'%'.'/manage_images'] = array(
28      if (variable_get('word2web_images', false)) {      'title' => 'Manage Images',
29        $node = node_load(arg(1));      'page callback' => 'drupal_get_form',
30        'page arguments' => array('word2web_manage_images', arg(1)),
31            'access callback' => 'word2web_images_access',
32        'type' => MENU_LOCAL_TASK,
33      );
34      return $items;
35    }
36    
37    function word2web_images_access() {
38            if(arg(0) == 'node' && is_numeric(arg(1))){
39              if(variable_get('word2web_images', false)){
40              $node = node_load(arg(1));
41        $nodetypes = variable_get('word2web_nodetypes', array());        $nodetypes = variable_get('word2web_nodetypes', array());
42          if ($nodetypes[$node->type]) {            if ($nodetypes[$node->type]) {
43          $items['node/'. '%' .'/manage_images'] = array(                  return TRUE;
44            'title' => 'Manage Images',            }
45            'page callback' => 'drupal_get_form',          }
           'page arguments' => array('word2web_manage_images', arg(1)),  
           'access arguments' => array('administer nodes'),  
           'type' => MENU_LOCAL_TASK,  
         );  
       }  
     }  
46    }    }
   return $items;  
47  }  }
48    
49  /**  /**
# Line 67  function word2web_menu() { Line 51  function word2web_menu() {
51   */   */
52  function word2web_form_alter(&$form, &$form_state, $form_id) {  function word2web_form_alter(&$form, &$form_state, $form_id) {
53    $nodetypes = variable_get('word2web_nodetypes', array());    $nodetypes = variable_get('word2web_nodetypes', array());
54    //print_r($form);  
55    if ($form['#node'] && $nodetypes[$form['#node']->type]) {    if ($form['#node'] && $nodetypes[$form['#node']->type]) {
56      $form['#attributes'] = array('enctype' => "multipart/form-data");      $form['#attributes']['enctype'] = 'multipart/form-data';
57      $form['#validate'][] = 'word2web_validate';      $form['#validate'][] = 'word2web_validate';
58      $form['word_document'] = array(      $form['word_doc'] = array(
59              '#type' => 'fieldset',
60              '#title' => t('Upload a word file here'),
61            );
62            $form['word_doc']['word_document'] = array(
63        '#type' => 'file',        '#type' => 'file',
64        '#title' => t('Upload Word document'),        '#title' => t('Upload Word document'),
65        '#description' => t("Please upload an HTML file produced by Word's <strong>export to web</strong> feature."),        '#description' => t("Please upload an HTML file produced by Word's <strong>export to web</strong> feature."),
# Line 79  function word2web_form_alter(&$form, &$f Line 67  function word2web_form_alter(&$form, &$f
67      );      );
68    
69      // Modify body field to help user      // Modify body field to help user
70      $form['body_filter']['body']['#disabled'] = true;      $form['body_field']['body']['#disabled'] = true;
71      $form['body_filter']['body']['#rows'] = 10;      $form['body_field']['body']['#rows'] = 10;
72      $form['body_filter']['body']['#description'] = t('This field will be populated automatically by the uploaded document.');      $form['body_field']['body']['#description'] = t('This field will be populated automatically by the uploaded document.');
73    }    }
74  }  }
75    
# Line 93  function word2web_form_alter(&$form, &$f Line 81  function word2web_form_alter(&$form, &$f
81   */   */
82    
83  function word2web_manage_images($nid) {  function word2web_manage_images($nid) {
84    
85    set_error_handler("_word2web_suppress_errors");    set_error_handler("_word2web_suppress_errors");
86    $node = node_load($nid);    $node = node_load(arg(1));
87    $images = _word2web_get_orphan_images($node->body);    $images = _word2web_get_orphan_images($node->body);
88    if ($images) {    if ($images) {
89      $form['images_help'] = array(      $form['images_help'] = array(
# Line 111  function word2web_manage_images($nid) { Line 100  function word2web_manage_images($nid) {
100          '#value' => $img);          '#value' => $img);
101        $i++;        $i++;
102      }      }
103            $form['loop_value'] = array(
104              '#type' => 'hidden',
105              '#value' => $i
106            );
107      $form['book_nid'] = array(      $form['book_nid'] = array(
108        '#type' => 'hidden',        '#type' => 'hidden',
109        '#value' => $nid);        '#value' => $node->nid);
110      $form['#attributes'] = array('enctype' => 'multipart/form-data');      $form['#attributes'] = array('enctype' => 'multipart/form-data');
111      $form['images_submit'] = array(      $form['images_submit'] = array(
112        '#type' => 'submit',        '#type' => 'submit',
# Line 135  function word2web_manage_images($nid) { Line 128  function word2web_manage_images($nid) {
128    
129  function word2web_manage_images_submit($form, &$form_state) {  function word2web_manage_images_submit($form, &$form_state) {
130    $success = true;    $success = true;
131    foreach ($form as $f => $v) {    for ($p = 0; $p < $form_state['values']['loop_value']; $p++) {
132      // !== because PHP's type system is weird / sucks  
     if (strpos($f, 'upload') !== false) {  
 /* TODO Modify the validators array to suit your needs.  
    This array is used in the revised file_save_upload */  
133    $validators = array(    $validators = array(
134      'file_validate_is_image' => array(),      'file_validate_is_image' => array(),
135      'file_validate_image_resolution' => array('85x85'),      'file_validate_image_resolution' => array('1600x1600'),
136      'file_validate_size' => array(30 * 1024),      'file_validate_size' => array(30 * 1024),
137    );    );
138    
139    //TODO: check this line        if ($fi = file_save_upload('upload_'.$p, $validators)) {
140        if ($fi = file_save_upload($validators)) {          $new_id = _word2web_node_image($fi->filepath, $fi->filename, $fi->fid);
         $new_id = _word2web_node_image($fi->filepath, $fi->filename);  
141          if ($new_id == -1) {          if ($new_id == -1) {
142            drupal_set_message("Image files could not be uploaded because the image directory is not configured correctly.");            drupal_set_message("Image files could not be uploaded because the image directory is not configured correctly.");
143            $success = false;            $success = false;
144            break;            break;
145          }          }
146          $map[$v] = $new_id;          $map[$form_state['values'][upload_."$p"]] = $new_id;
147        }        }
     }  
148    }    }
149    if ($success) {    if ($success) {
150      _word2web_insert_images($form['book_nid'], $map);      _word2web_insert_images($form_state['values']['book_nid'], $map);
151    }    }
152  }  }
153    
# Line 171  function word2web_manage_images_submit($ Line 159  function word2web_manage_images_submit($
159    
160  function _word2web_insert_images($nid, $map) {  function _word2web_insert_images($nid, $map) {
161    $node = node_load($nid);    $node = node_load($nid);
162      restore_error_handler();
163    set_error_handler("_word2web_suppress_errors");    set_error_handler("_word2web_suppress_errors");
164    $html = new DOMDocument();    $html = new DOMDocument();
165    $html->loadXML($node->body);    $html->loadXML($node->body);
# Line 181  function _word2web_insert_images($nid, $ Line 170  function _word2web_insert_images($nid, $
170      // 1. This image hasn't already been replaced      // 1. This image hasn't already been replaced
171      // 2. A file has been uploaded to replace this image      // 2. A file has been uploaded to replace this image
172      if (!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) {      if (!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) {
       $im->removeChild($im->firstChild);  
173        $im->setAttribute("nid", $map[$im->getAttribute("src")]);        $im->setAttribute("nid", $map[$im->getAttribute("src")]);
174              $im->removeAttribute("src");
175      }      }
176    }    }
177    $node->body = $html->saveXML();    $node->body = $html->saveXML();
178    node_save($node);    node_save(&$node);
179    restore_error_handler();    restore_error_handler();
180  }  }
181    
182    
183  function _word2web_node_image($url, $title='') {  function _word2web_node_image($url, $title='', $fid) {
184    restore_error_handler();          global $user;
185      //restore_error_handler();
186    if (!$title) {    if (!$title) {
187      $title = basename($url);      $title = basename($url);
188    }    }
189    $node = array('type' => 'image');    $node = new StdClass();
190    $values['title'] = basename($url);    $node->type = 'image';
191    /* http://drupal.org/node/201594 */    $node->uid = $user->uid;
192      $node->title = $title;
193      $node->body = '';
194    
195    $file_data = file_get_contents($url);    $file_data = file_get_contents($url);
196    $file_temp = file_save_data($file_data, file_directory_path() .'/'. basename($url), FILE_EXISTS_RENAME);    $file_temp = file_save_data($file_data, file_directory_path() .'/'. basename($url), FILE_EXISTS_RENAME);
197    if ($file_temp) {    if ($file_temp) {
198      $node['field_imagefile'] = array(          $node->field_imagefile[0]['fid'] = $fid;
199        array(          $node->field_imagefile[0]['list'] = 1;
200          'fid' => 'upload',          $node->field_imagefile[0]['uid'] = $user->uid;
201          'title' => $title,          $node->field_imagefile[0]['filename'] = basename($file_temp);
202          'filename' => basename($file_temp),          $node->field_imagefile[0]['filepath'] = $file_temp;
203          'filepath' => $file_temp,          $node->field_imagefile[0]['filesize'] = filesize($file_temp);
204          'filesize' => filesize($file_temp)          $node->field_imagefile[0]['status'] = 1;
205        )    node_save(&$node);
206      );    return $node->nid;
     $x = drupal_execute('image_node_form', $values, $node);  
     return intval(str_replace("node/", "", $x)); // TODO: This will not work on certain URL schemes, right?  
207    }    }
208    else {    else {
209      return -1;      return -1;
210    }    }
211  }  }
212    
213  function word2web_image_upload_message($node) {  function word2web_image_upload_message($node) {
# Line 247  function word2web_nodeapi(&$node, $op) { Line 238  function word2web_nodeapi(&$node, $op) {
238            word2web_image_upload_message($node);            word2web_image_upload_message($node);
239          }          }
240        }        }
241      case 'submit':      case 'presave':
242      case 'update':      case 'update':
 /* TODO Modify the validators array to suit your needs.  
    This array is used in the revised file_save_upload */  
243    $validators = array(    $validators = array(
     'file_validate_is_image' => array(),  
     'file_validate_image_resolution' => array('85x85'),  
     'file_validate_size' => array(30 * 1024),  
244    );    );
245          if ($html_file = file_save_upload('word_document', $validators)) {
       if ($html_file = file_save_upload($validators)) {  
246          $path = drupal_get_path('module', 'word2web');          $path = drupal_get_path('module', 'word2web');
247          $html_raw = file_get_contents($html_file->filepath);          $html_raw = file_get_contents($html_file->filepath);
248          $html_raw = _word2web_convert_chr($html_raw);          $html_raw = _word2web_convert_chr($html_raw);
# Line 286  function word2web_nodeapi(&$node, $op) { Line 271  function word2web_nodeapi(&$node, $op) {
271            $image_node->appendChild($html->createTextNode("&nbsp;"));            $image_node->appendChild($html->createTextNode("&nbsp;"));
272            $image_node->setAttribute("src", $im->getAttribute("src"));            $image_node->setAttribute("src", $im->getAttribute("src"));
273            $im->parentNode->insertBefore($image_node, $im);            $im->parentNode->insertBefore($image_node, $im);
           // $urls[] = $im->getAttribute("src");  
274          }          }
275          $html = $html->saveXML();          $html = $html->saveXML();
276          $html = iconv("UTF-8", "UTF-8//IGNORE", $html);          $html = iconv("UTF-8", "UTF-8//IGNORE", $html);
# Line 300  function word2web_nodeapi(&$node, $op) { Line 284  function word2web_nodeapi(&$node, $op) {
284          if (variable_get('word2web_filter', 0)) {          if (variable_get('word2web_filter', 0)) {
285            $node->format = variable_get('word2web_filter', 0);            $node->format = variable_get('word2web_filter', 0);
286          }          }
   
287          $node->body = $html;          $node->body = $html;
288        }        }
289        break;        break;
# Line 328  function _word2web_get_orphan_images($c) Line 311  function _word2web_get_orphan_images($c)
311    return $urls;    return $urls;
312  }  }
313    
   
   
314  /**  /**
315   * Get embedded images in word HTML   * Get embedded images in word HTML
316   * @param $c  A string of Word HTML that went through word2web   * @param $c  A string of Word HTML that went through word2web
# Line 426  function word2web_settings_form() { Line 407  function word2web_settings_form() {
407      '#description' => t('Select an input format to use by default for imported Word documents. Choose a filter similar to "Full HTML" which allows XML tags.'),      '#description' => t('Select an input format to use by default for imported Word documents. Choose a filter similar to "Full HTML" which allows XML tags.'),
408      '#options' => $formats,      '#options' => $formats,
409      '#default_value' => variable_get('word2web_filter', 0),      '#default_value' => variable_get('word2web_filter', 0),
410    );    );
   
   
   
411    
412    if (module_exists('imagefield') && true) {    if (module_exists('imagefield') && true) {
413      if (isset($nodetypes['image'])) {      if (isset($nodetypes['image'])) {

Legend:
Removed from v.1.6.2.1  
changed lines
  Added in v.1.6.2.2

  ViewVC Help
Powered by ViewVC 1.1.2