Пример #1
0
 /**
  * firstImport method
  *
  * accepts an xml dump (generated from the nodes controller admin_export function)
  * and contents with the file contents.
  *
  * @param mixed $xml
  * @return void
  * @access public
  */
 function first_import($xml, $options = array(), $thisUser = false)
 {
     $deleteFirst = true;
     $addingTranslation = false;
     extract($options);
     uses('Xml');
     $xml = new Xml($xml);
     $xml = Set::reverse($xml);
     $meta = Set::extract($xml, '/Contents/Meta');
     $nodes = Set::extract($xml, '/Contents/Node');
     $nodes = $nodes[0];
     $ids = Set::extract($nodes, '/Node/id');
     set_time_limit(count($ids) * 2);
     unset($this->Revision->validate['content']);
     if (Configure::read('Languages.default') != $meta[0]['Meta']['lang']) {
         $addingTranslation = true;
         if ($this->find('count') > 0) {
             $deleteFirst = false;
         }
     }
     $db =& ConnectionManager::getDataSouce($this->useDbConfig);
     $db->begin($this);
     if ($deleteFirst) {
         $db =& ConnectionManager::getDatasource('default');
         $db->query('truncate nodes');
         $db->query('truncate changes');
         $db->query('truncate revisions');
     }
     foreach ($nodes['Node'] as $i => $row) {
         extract($row);
         if ($addingTranslation && $Revision['lang'] == Configure::read('Languages.default')) {
             continue;
         }
         $parent_id = empty($parent_id) ? null : $parent_id;
         if (!$addingTranslation) {
             $depth = empty($depth) ? 0 : $depth;
             $show_in_toc = empty($show_in_toc) ? 0 : 1;
             $status = 1;
             $sequence = null;
             $toSave = compact('id', 'parent_id', 'depth', 'status', 'show_in_toc');
             if ($i == 0) {
                 $toSave['lft'] = 1;
                 $toSave['rght'] = 2;
             } else {
                 $toSave['lft'] = $parent_id;
                 $rght = $this->field('id', array('parent_id' => $id));
                 $toSave['rght'] = empty($rght) ? 0 : $rght;
             }
             $this->create();
             $this->id = false;
             if (!$this->save(array('Node' => $toSave), false)) {
                 $db->rallback($this);
                 return false;
             }
         }
         $toSave = $Revision;
         $toSave['reason'] = empty($toSave['reason']) || is_array($toSave['reason']) ? "" : $toSave['reason'];
         $toSave['content'] = empty($toSave['content']) || is_array($toSave['content']) ? "" : $toSave['content'];
         $toSave['node_id'] = $id;
         $toSave['under_node_id'] = $parent_id;
         $toSave['status'] = 'current';
         if ($thisUser) {
             $toSave['user_id'] = $thisUser;
         }
         $this->Revision->create();
         if (!$this->Revision->save(array('Revision' => $toSave), false)) {
             $db->rallback($this);
             return false;
         }
     }
     $db->commit($this);
     return true;
 }