view digitalobjects.import.php @ 0:6f6e07baad80 default tip

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Tue, 02 Jun 2015 08:57:48 +0200
parents
children
line wrap: on
line source

<?php

function digitalcollection_import_from_file($filename,$nid){


  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);

  $node = node_load($nid);

  foreach(explode("\n",$contents) as $line){
    $node->field_objid[LANGUAGE_NONE][]['objid']=$line;
    print $line;
  }

  node_save($node);
}



function digitalobjects_importPagesFromFile(){
  $filename = "/tmp/maps.json";
  # $filename = "/tmp/echo.json";
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);

  $pages = drupal_json_decode($contents);

  $cnt=0;
  foreach ($pages as $key => $page){

    $page = base64_decode($page);
    digitalobjects_importPage($page,$key);
    $cnt++;

  }


}


function digitalobjects_importPage($page,$key){

  $baseUrl = "http://localhost:18080/echo_nav/echo_pages/content/scientific_revolution/harriot/maps/";

  $fnames = explode("/",$key);

  if ($fnames[1] != "maps"){
    dpm($fnames[0]);
    return;
  }

  $fname = str_replace(".pt","1_1.png",$fnames[sizeof($fnames)-1]);

  $image = file_get_contents($baseUrl . $fname); // string
  $file = file_save_data($image, 'public://' . $fname,FILE_EXISTS_REPLACE);




  $node = new stdClass();
  $node->type = 'map_page';
  node_object_prepare($node);

  $node->body['und'][0]['value'] = check_markup($page,'full_html');

  $node->body['und'][0]['format']  = 'full_html';

  $node->path['alias']="harriot/maps/" . $fnames[sizeof($fnames)-1];

  $node->title = "_".$key;


  $node->language = LANGUAGE_NONE;


  $node->field_echopath['und'][0]['value']=$key;

  $node->field_image[LANGUAGE_NONE]['0']['fid'] = $file->fid;
  $node = node_submit($node);




  /*$node->field_admintag['und'][0] = array (
      'tid' => $tid,

  );*/
  // Try to set your custom field

  $node = node_submit($node);

  node_save($node);

  }



function digitalobjects_importCollectionFromFile() {


  $filename = "/tmp/china.json";
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);

  $collection = drupal_json_decode($contents);
  digitalobjects_importCollection($collection);
}

function digitalobjects_importCollection($collection,$tid){


  //schaue ob die collection schon gibt
  $qs = "select count(*) as n from field_data_field_echopath where field_echopath_value='". $collection['echo_path']   ."'";
  $results = db_query($qs);
  foreach($results as $result){

    if ($result->n> 0){
    return;
  }
  }
  dpm($collection['echo_path']);
  $node = new stdClass();
  $node->type = 'digitalcollection';
  node_object_prepare($node);

  $node->body['und'][0]['value'] = check_markup($collection['description'],'full_html');

  $node->body['und'][0]['format']  = 'full_html';


  $node->title = $collection['title'];


  $node->language = LANGUAGE_NONE;


  $node->field_echopath['und'][0]['value']=$collection['echo_path'];
  $node->field_label['und'][0]['value']=$collection['label'];
  $node = node_submit($node);


  $cnt=0;
  foreach ($collection['content'] as $cont){
    print $cont;
    if (($cont != "") && ($cont !=null)){
      $node->field_objid['und'][$cnt]['objid']=$cont;
      $cnt++;


    }
  }

  $node->field_admintag['und'][0] = array (
      'tid' => $tid,

  );
  // Try to set your custom field

  $node = node_submit($node);

  node_save($node);

}

function digitalobjects_importCollections($tid) {
  #filename = "/tmp/export.json";
  $filename = "/tmp/groups.json";
 # $filename = "/tmp/echo.json";
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);

  $collections = drupal_json_decode($contents);

  $cnt=0;
  foreach ($collections as $key => $collection){
    digitalobjects_importCollection($collection,$tid);
    $cnt++;

  }

}

/* suche all e nodes mit echopath = $path */
function digitalobjects_searchCollectionByPath($path){
  $results = db_query("SELECT * FROM {field_data_field_echopath} n where field_echopath_value ='" . $path . "'");

  return $results;
  }

function digitalobjects_importStructure(){
  $filename = "/Library/WebServer/Documents/drupal-dev/testData/exportAllCollectionsAndSubCollectionAsJSON";
  # $filename = "/tmp/echo.json";
  $handle = fopen($filename, "r");
  $contents = fread($handle, filesize($filename));
  fclose($handle);

  $collections = drupal_json_decode($contents);

  foreach ($collections as $parentCollection => $value){
    /* finde node id von parent */
    $nodes = digitalobjects_searchCollectionByPath($parentCollection);
    if (sizeof($nodes) == 0){ //parent existiert nicht
      //generate parent
    } else {
      foreach($nodes as $node){ // sollte eigentlich nur eine geben aber wenn mehrere gibt, dann eben überall die entsprechenden einträge machen.
        // gehe jetzt durch content

        $cnt=0;
        foreach($value['content'] as $content){
          $childnodes = digitalobjects_searchCollectionByPath($content);
          if (sizeof($nodes) == 0){ // gibt es nicht
           //erzeuge
          }else{

            $nodeid = $node->entity_id;
            $entities = entity_load("node",array($nodeid)); // hole die entities
            foreach($entities as $entity){
              foreach($childnodes as $childnode){ // trage jetzt die childnodes ein.

                $newnode ="node/".$childnode->entity_id;
                $qs = "SELECT count(*) as n FROM {field_data_field_collid} n where field_collid_objid ='" . $newnode . "' and entity_id ='". $nodeid . "'";
                $res = db_query($qs);

                foreach($res as $result){
                  dpm($result);
                  dpm($qs);
                  if ($result->n == 0){ //gibt es noch nicht

                    $entity->field_collid['und'][]['objid']=$newnode;
                  }
                }
              }

              $entity = node_submit($entity);

              node_save($entity);
            }
          }
        }
        }

      }
    }

}