/[drupal]/contributions/modules/bookimport/save_node.php
ViewVC logotype

Diff of /contributions/modules/bookimport/save_node.php

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

revision 1.2, Fri Jan 27 06:45:45 2006 UTC revision 1.3, Sat Jan 28 07:27:10 2006 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id:$  // $Id: save_node.php,v 1.2 2006/01/27 06:45:45 puregin Exp $
4    
5  /**  /**
6   * @file   * @file
# Line 18  Line 18 
18   *   *
19   */   */
20    
21    // global hash provides mapping from old nid to new
22    $new_nid = array();
23    $parent_of = array();
24    
25  /**  /**
26   * Fills in the body of the given node.   * Fills in the body of the given node.
27     * Note: @arg $parent is not actually used
28   */   */
29  function save($tnode, $new_nid, $parent_of, $mode) {  function save($tnode, $nid, $parent, $mode) {
30        global $new_nid;
31        global $parent_of;
32    
33      if (DEBUG > 2) {      if (DEBUG > 2) {
34          echo "saving node '". $tnode->title . "' ";          echo "saving node '". $tnode->title . "' ";
# Line 42  function save($tnode, $new_nid, $parent_ Line 48  function save($tnode, $new_nid, $parent_
48      }      }
49    
50      if ($mode == 'update') {      if ($mode == 'update') {
51          update($tnode);          _update_book_node($tnode);
52      }      }
53      else if ($mode == 'insert') {      else if ($mode == 'insert') {
54          create($tnode);          _create_book_node($tnode);
55      }      }
56      else {      else {
57          // Write node body          // Write node body
# Line 55  function save($tnode, $new_nid, $parent_ Line 61  function save($tnode, $new_nid, $parent_
61  /**  /**
62   * Creates a new node in the database   * Creates a new node in the database
63   */   */
64  function create($tnode) {  function _create_book_node($tnode) {
65      global $user;      global $user;
     global $parent_of;  
66      global $new_nid;      global $new_nid;
67        global $parent_of;
68    
69      $node->type = $tnode->nodetype;      $node->type = $tnode->nodetype;
70      $node->title = $tnode->title;      $node->title = $tnode->title;
# Line 82  function create($tnode) { Line 88  function create($tnode) {
88      if (DEBUG > 1)  {      if (DEBUG > 1)  {
89          echo "new nid is: ". $node->nid ."<br />";          echo "new nid is: ". $node->nid ."<br />";
90      }      }
91        // identify newly created nodes by mapping new_id => new_id;
92        // newly created nodes are ones which have an empty $tnode->id
93        if ($tnode->id == '') {
94            $tnode->id = $node->nid;
95        }
96      // get the new nid, store it hashed by old nid:      // get the new nid, store it hashed by old nid:
97      $new_nid[$tnode->id] = $node->nid;      $new_nid[$tnode->id] = $node->nid;
98      // record the parent-child of the node,      // record the parent-child of the node,
# Line 95  function create($tnode) { Line 106  function create($tnode) {
106  }  }
107    
108  function _update_book_node($tnode) {  function _update_book_node($tnode) {
109        global $parent_of;
110    
111      $timestamp = time();      $timestamp = time();
112      $log = "\nInserted on ". date("D F j, Y") ." at ". date("G:i:s T") ."<br />\n";      $log = "\nInserted on ". date("D F j, Y") ." at ". date("G:i:s T") ."<br />\n";
113    
# Line 135  function _update_book_node($tnode) { Line 148  function _update_book_node($tnode) {
148   * Updates a given node in the database   * Updates a given node in the database
149   */   */
150  function update($tnode) {  function update($tnode) {
   
151      // We need to deal with case where tnode does not have an id.      // We need to deal with case where tnode does not have an id.
152      // In this case, create the node rather than updating      // In this case, create the node rather than updating
153      // an existing node.      // an existing node.
# Line 143  function update($tnode) { Line 155  function update($tnode) {
155      // echo "update: tnode->id = " . $tnode->id. "<br />\n";      // echo "update: tnode->id = " . $tnode->id. "<br />\n";
156    
157      if (!isset($tnode->id) || $tnode->id == 0) {      if (!isset($tnode->id) || $tnode->id == 0) {
158          create($tnode);          _create_book_node($tnode);
159      }      }
160      else { // were updating an existing entry      else { // were updating an existing entry
161          _update_book_node($tnode);          _update_book_node($tnode);
# Line 151  function update($tnode) { Line 163  function update($tnode) {
163  }  }
164    
165  # at end, if inserting new records iterate over new_nid  # at end, if inserting new records iterate over new_nid
166  function make_links($parent_of, $new_nid) {  function make_links() {
     global $parent_of;  
167      global $new_nid;      global $new_nid;
168        global $parent_of;
169    
170    
171      if (DEBUG > 0) {      if (DEBUG > 0) {
172          echo "make_links()<br />";          echo "make_links()<br />";
# Line 182  function make_links($parent_of, $new_nid Line 195  function make_links($parent_of, $new_nid
195                         "  vid='%d' "                         "  vid='%d' "
196                         );                         );
197    
198        if (!empty($new_nid)) {
199      foreach ($new_nid as $nid => $new_value) {          foreach ($new_nid as $nid => $new_value) {
200          $node = node_load(array('nid' => $new_value));              $node = node_load(array('nid' => $new_value));
201          $node->nid = $new_value;              $node->nid = $new_value;
202          $node->parent = $new_nid[$parent_of[$nid]];              $node->parent = $new_nid[$parent_of[$nid]];
203    
204          /*              /*
205           $log = "Imported on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br />";               $log = "Imported on " . date("D F j, Y") . " at " . date("G:i:s T") . "<br />";
206           $log .= "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)";               $log .= "parent[$nid ($new_value)] = " .$parent_of[$nid]. " ($this_parent)";
207           $log .= $node->log;               $log .= $node->log;
208          */              */
209          if (!isset($nid) || $nid == '') {              if (!isset($nid) || $nid == '') {
210              $node->parent = $parent_of[$nid];                  $node->parent = $parent_of[$nid];
211              $nid = $new_value;                  $nid = $new_value;
212                }
213    
214                if (DEBUG > 0) {
215                    echo sprintf("UPDATE {book} ".
216                                 "SET ".
217                                 "  nid=%d, ".
218                                 "  parent=%d, ".
219                                 "  weight=%d ".
220                                 "WHERE".
221                                 "  vid=%d ",
222                                 $node->nid,
223                                 $node->parent,
224                                 $node->weight,
225                                 $node->vid
226                                 );
227                    echo "<br />";
228                }
229                $result = db_query($book_sql,
230                                   $node->nid,
231                                   $node->parent,
232                                   $node->weight,
233                                   $node->vid
234                                   );
235    
236                if (DEBUG > 0) {
237                  echo "parent[$nid ($new_value)] = ". $parent_of[$nid] ." ($node->parent)<br />";
238                }
239          }          }
   
         if (DEBUG > 0) {  
             echo sprintf("UPDATE {book} ".  
                          "SET ".  
                          "  nid=%d, ".  
                          "  parent=%d, ".  
                          "  weight=%d ".  
                          "WHERE".  
                          "  vid='%d' ",  
                          $node->nid,  
                          $node->parent,  
                          $node->weight,  
                          $node->vid  
                          );  
             echo "<br />";  
         }  
         $result = db_query($book_sql,  
                            $node->nid,  
                            $node->parent,  
                            $node->weight,  
                            $node->vid  
                            );  
   
         echo "parent[$nid ($new_value)] = ". $parent_of[$nid] ." ($node->parent)<br />";  
   
   
240      }      }
241  }  }
242    

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

  ViewVC Help
Powered by ViewVC 1.1.2