<?php

// -*- tab-width: 3; indent-tabs-mode: 1; -*-
/*  
 * $Id: createContact.php 225 2003-06-12 16:46:59Z andras $
 * Created for the StreamOnTheFly project (IST-2001-32226)
 * Authors: András Micsik, Máté Pataki, Tamás Déri 
 *          at MTA SZTAKI DSD, http://dsd.sztaki.hu
 */
require "init.inc.php";
$page->popup = true;
$page->forceLogin();
$stationId = sotf_Utils::getParameter('stationid');
$contactName = sotf_Utils::getParameter('name');
if ($contactName) {
    if (sotf_Contact::findByNameLocal($contactName)) {
        //$page->addStatusMsg('contact_name_exists');
        //$page->redirect("createCOntact.php);
        raiseError("contact_name_exists");
        exit;
    }
    // create a new contact
    $contact = new sotf_Contact();
    $status = $contact->create($contactName, $stationId);
    if (!$status) {
        $page->addStatusMsg('contact_create_failed');
    } else {
        $permissions->addPermission($contact->id, $user->id, 'admin');
        $page->redirect("editContact.php?id=" . $contact->id);
        exit;
    }
 /** static: create contact record from metadata */
 function importContact($contactData, $contactRole, $prgId, $stationId, $admins)
 {
     global $db, $permissions, $repository, $vocabularies, $config;
     $db->begin();
     // find out what should go into the 'name' field
     if ($contactData['type'] == 'organisation') {
         $name = $contactData['organizationname'];
     } elseif ($contactData['type'] == 'individual') {
         $name = $contactData['firstname'] . ' ' . $contactData['lastname'];
     } else {
         logError("unknown type of contact: " . $contactData['type']);
         return null;
     }
     // if not exists, create new contact
     $id = sotf_Contact::findByNameLocal($name);
     if (!$id) {
         $contact = new sotf_Contact();
         $status = $contact->create($name, $stationId);
         if (!$status) {
             //$page->addStatusMsg('contact_create_failed');
             return null;
         }
         // add permissions for all station admins (??)
         while (list(, $adminId) = each($admins)) {
             $permissions->addPermission($contact->id, $adminId, 'admin');
         }
     } else {
         $contact = $repository->getObject($id);
     }
     //debug("contactData", $contactData);
     // set/update contact data
     $contact->set('acronym', $contactData['organizationacronym']);
     $contact->set('alias', $contactData['alias']);
     $contact->set('url', $contactData['uri']);
     $contact->set('email', $contactData['email']);
     $contact->set('address', $contactData['address']);
     $contact->update();
     // determine role
     if ($contactData['role']) {
         $language = 'eng';
         // for now
         $rid = $vocabularies->getRoleId($contactData['role'], $language);
         if ($rid) {
             $contactRole = $rid;
         }
     }
     // create role
     if (!sotf_ComplexNodeObject::findRole($prgId, $contact->id, $contactRole)) {
         $role = new sotf_NodeObject("sotf_object_roles");
         $role->set('object_id', $prgId);
         $role->set('contact_id', $contact->id);
         $role->set('role_id', $contactRole);
         $role->create();
     }
     $db->commit();
     // fetch logo from url and store
     if (!empty($contactData['logo'])) {
         $url = $contactData['logo'];
         if ($handle = @fopen($url, 'rb')) {
             $contents = "";
             while (!feof($handle)) {
                 $contents .= fread($handle, 8192);
             }
             /*
             		  do {
             			 $data = fread ($handle, 100000);
             			 if (strlen($data) == 0) {
             				break;
             			 }
             			 //debug("received", strlen($data));
             			 $contents .= $data;
             			 } while(0); */
             fclose($handle);
             $tmpFile = tempnam($config['tmpDir'], 'logo_u');
             debug("received logo from", $url);
             sotf_Utils::save($tmpFile, $contents);
             chmod($tmpFile, 0660);
             $contact->setIcon($tmpFile);
             unlink($tmpFile);
         } else {
             logError("Could not fetch icon from {$url}");
         }
     }
     return $contact->id;
 }