/[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.2, Mon Jul 7 13:43:14 2008 UTC revision 1.3, Mon Jul 7 14:38:30 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3    /*
4     * Author   : Tom MacWright, Young Hahn
5     * Revision : $Id$
6     */
7    
8  /**  /**
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.
# Line 21  function word2web_menu($may_cache) { Line 27  function word2web_menu($may_cache) {
27        'access' => user_access('administer site configuration'),        'access' => user_access('administer site configuration'),
28      );      );
29    }    }
30    if(is_numeric(arg(1)) && arg(0) == 'node') {    if (is_numeric(arg(1)) && arg(0) == 'node') {
31      if(variable_get('word2web_images', false)) {      if (variable_get('word2web_images', false)) {
32        $node = node_load(arg(1));        $node = node_load(arg(1));
33        $nodetypes = variable_get('word2web_nodetypes', array());        $nodetypes = variable_get('word2web_nodetypes', array());
34          if($nodetypes[$node->type]) {          if ($nodetypes[$node->type]) {
35          $items[] = array('path' => 'node/'. arg(1) .'/manage_images',          $items[] = array('path' => 'node/'. arg(1) .'/manage_images',
36            'title' => t('Manage Images'),            'title' => t('Manage Images'),
37            'callback' => 'drupal_get_form',            'callback' => 'drupal_get_form',
# Line 73  function word2web_manage_images($nid) { Line 79  function word2web_manage_images($nid) {
79    set_error_handler("_word2web_suppress_errors");    set_error_handler("_word2web_suppress_errors");
80    $node = node_load($nid);    $node = node_load($nid);
81    $images = _word2web_get_orphan_images($node->body);    $images = _word2web_get_orphan_images($node->body);
82    if($images) {    if ($images) {
83      $form['images_help'] = array(      $form['images_help'] = array(
84        '#value' => t("These images were included in the !type but have not been uploaded yet.<br /><br />",        '#value' => t("These images were included in the !type but have not been uploaded yet.<br /><br />",
85          array('!type' => $node->type)));          array('!type' => $node->type)));
86      $i = 0;      $i = 0;
87      foreach($images as $img) {      foreach ($images as $img) {
88        $form['images']['upload_'.$i] = array(        $form['images']['upload_'. $i] = array(
89          '#type'   =>  'file',          '#type'   =>  'file',
90          '#title'  =>  t('!filename', array('!filename' => $img)),          '#title'  =>  t('!filename', array('!filename' => $img)),
91          '#size' => 40);          '#size' => 40);
92        $form['file_names']['upload_'.$i] = array(        $form['file_names']['upload_'. $i] = array(
93          '#type' => 'hidden',          '#type' => 'hidden',
94          '#value' => $img);          '#value' => $img);
95        $i++;        $i++;
# Line 96  function word2web_manage_images($nid) { Line 102  function word2web_manage_images($nid) {
102        '#type' => 'submit',        '#type' => 'submit',
103        '#value' => 'Upload Images');        '#value' => 'Upload Images');
104      restore_error_handler();      restore_error_handler();
105    } else {    }
106      else {
107      $form['images_complete'] = array(      $form['images_complete'] = array(
108        '#value' => t("All of the images in this report have been uploaded"));        '#value' => t("All of the images in this report have been uploaded"));
109    }    }
# Line 111  function word2web_manage_images($nid) { Line 118  function word2web_manage_images($nid) {
118    
119  function word2web_manage_images_submit($form_id, $form) {  function word2web_manage_images_submit($form_id, $form) {
120    $success = true;    $success = true;
121    foreach($form as $f=>$v) {    foreach ($form as $f => $v) {
122      // !== because PHP's type system is weird / sucks      // !== because PHP's type system is weird / sucks
123      if(strpos($f, 'upload') !== false) {      if (strpos($f, 'upload') !== false) {
124        if($fi = file_check_upload($f)) {        if ($fi = file_check_upload($f)) {
125          $new_id = _word2web_node_image($fi->filepath, $fi->filename);          $new_id = _word2web_node_image($fi->filepath, $fi->filename);
126          if($new_id == -1) {          if ($new_id == -1) {
127            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.");
128            $success = false;            $success = false;
129            break;            break;
# Line 125  function word2web_manage_images_submit($ Line 132  function word2web_manage_images_submit($
132        }        }
133      }      }
134    }    }
135    if($success) {    if ($success) {
136      _word2web_insert_images($form['book_nid'], $map);      _word2web_insert_images($form['book_nid'], $map);
137    }    }
138  }  }
# Line 143  function _word2web_insert_images($nid, $ Line 150  function _word2web_insert_images($nid, $
150    $html->loadHTML($node->body);    $html->loadHTML($node->body);
151    $images = $html->getElementsByTagName("image");    $images = $html->getElementsByTagName("image");
152    $i = 0;    $i = 0;
153    foreach($images as $im) {    foreach ($images as $im) {
154      // Only do the replacement if      // Only do the replacement if
155      // 1. This image hasn't already been replaced      // 1. This image hasn't already been replaced
156      // 2. A file has been uploaded to replace this image      // 2. A file has been uploaded to replace this image
157      if(!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) {      if (!$im->getAttribute("nid") && $map[$im->getAttribute("src")]) {
158        $im->removeChild($im->firstChild);        $im->removeChild($im->firstChild);
159        $im->setAttribute("nid", $map[$im->getAttribute("src")]);        $im->setAttribute("nid", $map[$im->getAttribute("src")]);
160      }      }
# Line 160  function _word2web_insert_images($nid, $ Line 167  function _word2web_insert_images($nid, $
167    
168  function _word2web_node_image($url, $title='') {  function _word2web_node_image($url, $title='') {
169    restore_error_handler();    restore_error_handler();
170    if(!$title) {    if (!$title) {
171      $title = basename($url);      $title = basename($url);
172    }    }
173    $node = array('type' => 'image');    $node = array('type' => 'image');
174    $values['title'] = basename($url);    $values['title'] = basename($url);
175    /* http://drupal.org/node/201594 */    /* http://drupal.org/node/201594 */
176    $file_data = file_get_contents($url);    $file_data = file_get_contents($url);
177    $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);
178    if($file_temp) {    if ($file_temp) {
179      $node['field_imagefile'] = array(      $node['field_imagefile'] = array(
180        array(        array(
181          'fid' => 'upload',          'fid' => 'upload',
# Line 192  function word2web_image_upload_message($ Line 199  function word2web_image_upload_message($
199    $html->loadHTML($node->body);    $html->loadHTML($node->body);
200    $images = $html->getElementsByTagName("image");    $images = $html->getElementsByTagName("image");
201    $i = 0;    $i = 0;
202    foreach($images as $im) {    foreach ($images as $im) {
203      if(!$im->getAttribute("nid")) {      if (!$im->getAttribute("nid")) {
204        $i++;        $i++;
205      }      }
206    }    }
207    restore_error_handler();    restore_error_handler();
208    if($i > 0) {    if ($i > 0) {
209      drupal_set_message(l(t('!num images in this !type need to be uploaded.', array('!type' => $node->type, '!num' => $i)), 'node/'.$node->nid.'/manage_images'), 'error');      drupal_set_message(l(t('!num images in this !type need to be uploaded.', array('!type' => $node->type, '!num' => $i)), 'node/'. $node->nid .'/manage_images'), 'error');
210    }    }
211  }  }
212    
# Line 209  function word2web_image_upload_message($ Line 216  function word2web_image_upload_message($
216  function word2web_nodeapi(&$node, $op) {  function word2web_nodeapi(&$node, $op) {
217    switch ($op) {    switch ($op) {
218      case 'view':      case 'view':
219        if(in_array($node->type, variable_get('word2web_nodetypes', array()))) {        if (in_array($node->type, variable_get('word2web_nodetypes', array()))) {
220          if(user_access("administer nodes")) {          if (user_access("administer nodes")) {
221            word2web_image_upload_message($node);            word2web_image_upload_message($node);
222          }          }
223        }        }
224      case 'submit':      case 'submit':
225      case 'update':      case 'update':
226        if($html_file = file_check_upload('word_document')) {        if ($html_file = file_check_upload('word_document')) {
227          $path = drupal_get_path('module', 'word2web');          $path = drupal_get_path('module', 'word2web');
228          $html_raw = file_get_contents($html_file->filepath);          $html_raw = file_get_contents($html_file->filepath);
229          $html_raw = _word2web_convert_chr($html_raw);          $html_raw = _word2web_convert_chr($html_raw);
# Line 232  function word2web_nodeapi(&$node, $op) { Line 239  function word2web_nodeapi(&$node, $op) {
239           * XSL transformations. It also collects a list of URLs of images           * XSL transformations. It also collects a list of URLs of images
240           * so that we'll know what to fetch from the user           * so that we'll know what to fetch from the user
241           */           */
242          foreach($images as $im) {          foreach ($images as $im) {
243            $image_node = $html->createElement("image");            $image_node = $html->createElement("image");
244            $image_node->appendChild($html->createTextNode("&nbsp;"));            $image_node->appendChild($html->createTextNode("&nbsp;"));
245            $image_node->setAttribute("src", $im->getAttribute("src"));            $image_node->setAttribute("src", $im->getAttribute("src"));
# Line 270  function _word2web_get_orphan_images($c) Line 277  function _word2web_get_orphan_images($c)
277    $html = new DOMDocument();    $html = new DOMDocument();
278    $html->loadHTML($c);    $html->loadHTML($c);
279    $images = $html->getElementsByTagName('image');    $images = $html->getElementsByTagName('image');
280    foreach($images as $im) {    foreach ($images as $im) {
281      if(!$im->getAttribute("nid")) { // If it doesn't have a node id, it isn't up      if (!$im->getAttribute("nid")) { // If it doesn't have a node id, it isn't up
282        $urls[] =  $im->getAttribute("src");        $urls[] =  $im->getAttribute("src");
283      }      }
284    }    }
# Line 292  function _word2web_get_images($c) { Line 299  function _word2web_get_images($c) {
299    $html = new DOMDocument();    $html = new DOMDocument();
300    $html->loadHTML($c);    $html->loadHTML($c);
301    $images = $html->getElementsByTagName('image');    $images = $html->getElementsByTagName('image');
302    foreach($images as $im) {    foreach ($images as $im) {
303      $urls[] =  $im->getAttribute("src");      $urls[] =  $im->getAttribute("src");
304    }    }
305    restore_error_handler();    restore_error_handler();
# Line 308  function _word2web_get_images($c) { Line 315  function _word2web_get_images($c) {
315    
316  function word2web_manual($html_raw) {  function word2web_manual($html_raw) {
317    $html_raw = _word2web_convert_chr($html_raw);    $html_raw = _word2web_convert_chr($html_raw);
318          $path = drupal_get_path('module', 'word2web');    $path = drupal_get_path('module', 'word2web');
319    set_error_handler('_word2web_suppress_errors');    set_error_handler('_word2web_suppress_errors');
320          $html = $html_raw;    $html = $html_raw;
321    $html_raw = preg_replace("/<(img)([^>]*)>/mi",    $html_raw = preg_replace("/<(img)([^>]*)>/mi", "<addr class='image' $2>-</addr>", $html_raw);
                 "<addr class='image' $2>-</addr>", $html_raw);  
   // Get the charset from the invalid Word HTML/XML  
322    //preg_match('/charset=([\w-]+)/', $html_raw, $matches);    //preg_match('/charset=([\w-]+)/', $html_raw, $matches);
   //print_r($matches);  
323    // This step apparently cleans up the XML a little.    // This step apparently cleans up the XML a little.
324    $html = new DOMDocument();    $html = new DOMDocument();
325    $html->loadHTML($html_raw);    $html->loadHTML($html_raw);
# Line 327  function word2web_manual($html_raw) { Line 331  function word2web_manual($html_raw) {
331    //$html = iconv($matches[1], "UTF-8//IGNORE", $html);    //$html = iconv($matches[1], "UTF-8//IGNORE", $html);
332    $html = iconv("UTF-8", "UTF-8//IGNORE", $html);    $html = iconv("UTF-8", "UTF-8//IGNORE", $html);
333    // Actually run the xsl transformations    // Actually run the xsl transformations
334          $html = _word2web_xslt_transform($html, $path .'/empty.xsl');    $html = _word2web_xslt_transform($html, $path .'/empty.xsl');
335    
336    $html = _word2web_xslt_transform($html, $path .'/w2html.xslt');    $html = _word2web_xslt_transform($html, $path .'/w2html.xslt');
337          restore_error_handler();    restore_error_handler();
338    $html = str_replace('/&gt;', '', $html);    $html = str_replace('/&gt;', '', $html);
339          return $html;    return $html;
340  }  }
341    
342  /**  /**
# Line 373  function word2web_settings_form() { Line 377  function word2web_settings_form() {
377    
378    
379    
380    if(module_exists('imagefield') && true) {    if (module_exists('imagefield') && true) {
381      if(isset($nodetypes['image'])) {      if (isset($nodetypes['image'])) {
382        $imagetype = content_types("image");        $imagetype = content_types("image");
383        if(isset($imagetype['fields']['field_imagefile'])) {        if (isset($imagetype['fields']['field_imagefile'])) {
384          $form['word2web_images'] = array(          $form['word2web_images'] = array(
385            '#type' => 'checkbox',            '#type' => 'checkbox',
386            '#default_value' => variable_get('word2web_images', 0),            '#default_value' => variable_get('word2web_images', 0),

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

  ViewVC Help
Powered by ViewVC 1.1.2