Subversion Repositories eFlore/Applications.cel

Rev

Rev 1803 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

global $champs;
$champs = Array(
        'nom_sel' => 'Espece',
        'nom_sel_nn' => 'Numero nomenclatural',
        'nom_ret' => 'Nom retenu',
        'nom_ret_nn' => 'Numero nomenclatural nom retenu',
        'nt' => 'Numero taxonomique',
        'famille' => 'Famille',
        'nom_referentiel' => 'Referentiel taxonomique',
        'zone_geo' => 'Commune',
        'ce_zone_geo' => 'Identifiant Commune',
        'date_observation' => 'Date',
        'lieudit' => 'Lieu-dit',
        'station' => 'Station',
        'milieu' => 'Milieu',
        'commentaire' => 'Notes',
        'latitude' => 'Latitude',
        'longitude' => 'Longitude',
        'altitude' => 'Altitude',
        'geodatum' => 'Referentiel Geographique',

        'ordre' => 'Ordre',
        'id_observation' => 'Identifiant',

        'mots_cles_texte' => 'Mots Clés',

        'date_creation' => 'Date Création',
        'date_modification' => 'Date Modification',

        'transmission' => 'Transmis',
        'date_transmission' => 'Date Transmission',
        'abondance' => 'Abondance',
        'certitude' => 'Certitude',
        'phenologie' => 'Phénologie',
        'images' => 'Image(s)',
        'nom_commun' => 'Nom Commun',
);


function cel_get_obs() {
        $URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
        return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
}

function cel_get_images() {
        $URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
        return json_decode(file_get_contents($URL . '/?limite=100000&numero_page=0'));
}

function cel_delete_obs($obs) {
        $URL = DOMAIN . '/jrest/InventoryObservationList/' . USER;
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $obs));
        curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}

function cel_delete_image($img) {
        $URL = DOMAIN . '/jrest/InventoryImageList/' . USER;
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $URL . '/' . implode(',', $img));
        curl_setopt($ch,CURLOPT_POSTFIELDS, array('action' => 'DELETE'));
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}


function cel_delete_all_obs() {
        $obs = cel_get_obs();
        $ordres = array_map(function($item) { return $item->ordre; }, $obs);
        cel_delete_obs($ordres);
}


function cel_delete_all_images() {
        $img = cel_get_images();
        $ids = array_map(function($item) { return $item->id_image; }, $img);
        cel_delete_image($ids);
}

function cel_upload_image($file) {
        if(! file_exists($file)) return NULL;
        $URL = DOMAIN . '/jrest/InventoryImage';

        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_POSTFIELDS, array(
                'file' => '@' . $file,
                'ce_utilisateur' => USER
        ));

        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}

function cel_link_images($user, $id_image, $id_obs) {
        $URL = DOMAIN . '/jrest/inventoryImageLink/';
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_POSTFIELDS, array(
                'id_image' => $id_obs,
                'id_observation' => $id_obs,
                'ce_utilisateur' => $user
        ));

        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}


function auth() {
        // TODO: CURLOPT_NETRC + Cel API support
        if(!defined('EMAIL') || !defined('PASS') || ! EMAIL || ! PASS) return FALSE;
        if(defined('COOKIE') && COOKIE) return TRUE;
        $URL = DOMAIN . '/jrest/User/' . EMAIL . '/' . PASS;

        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_HEADER, true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);

        $result = curl_exec($ch);
        curl_close($ch);
        $sess = $id = array();
        preg_match('/.*PHPSESSID=(\w+)/', $result, $sess);
        preg_match('/"id_utilisateur":"(\d+)"/', $result, $id);
        if(DEBUG) fwrite(STDERR, "curl \"".$URL ."\" : {$sess[1]} / {$id[1]}\n");
        if(isset($sess[1])) {
                define('COOKIE', $sess[1]);
                if(!defined('USER')) {
                        if(!isset($id[1])) return FALSE;
                        define('USER', $id[1]);
                }
                return TRUE;
        }
        return FALSE;
}

function genCSV($d = Array()) {
        $out = fopen("php://temp", 'r+');
        // don't filter-out unknown field (for testing)
        // $d = array_intersect_key($d, $GLOBALS['champs']);
        $head = array();
        foreach($d as $k => $v) {
                if(isset($GLOBALS['champs'][$k])) $head[$GLOBALS['champs'][$k]] = $v;
                else $head[$k] = $v;
        }
        fputcsv($out, array_keys($head) , ',', '"');
        fputcsv($out, array_values($head) , ',', '"');
        rewind($out);
        $csv = stream_get_contents($out);
        fclose($out);
        return $csv;
}

function import($d) {
        $URL = DOMAIN . '/jrest/InventoryImportExcel';
        if(!is_file($d)) return FALSE;
        exec("csvtool -o /tmp/a.csv col 1- $d");
        exec("/home/raphael/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv");
        //if(DEBUG) fwrite(STDERR, "/home/raphael/bin/csv2xls -v 0 -f -o /tmp/a $d\n");
        if(DEBUG) fwrite(STDERR, "/home/raphael/bin/csv2xls -v 0 -f -o /tmp/a /tmp/a.csv\n");
        $d = "/tmp/a.xls";
        $ch = curl_init();

        // curl does not support setting filename="X.csv";type=application/octet-stream'
        // with plain POST variables

        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_POSTFIELDS, array(
                'upload' => '@' . $d . ';filename=test.csv',
                'utilisateur' => USER
        ));

        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);

        if(DEBUG) fwrite(STDERR, "curl -F \"upload=@$d;filename=test.csv\" -F utilisateur=" . USER . " \"$URL\"\n");

        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}

function export($cols = NULL) {
        if(!defined('COOKIE')) return FALSE;

        /*$URL = DOMAIN . '/jrest/CelWidgetExport/csv?format=csv&utilisateur=' . EMAIL;
          $cookie = 'PHPSESSID=' . COOKIE . '; ' . 'utilisateur=' . EMAIL . '; ' . 'cel_id=' . USER;*/
        $URL = DOMAIN . '/jrest/InventoryExport/' . USER . '/';
        $cookie = 'PHPSESSID=' . COOKIE;

        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_COOKIE, $cookie);
        $fh = fopen('/tmp/b.xls', 'w'); 
        curl_setopt($ch,CURLOPT_FILE, $fh);

        if(DEBUG) fwrite(STDERR, "curl -b \"$cookie\" \"$URL\"\n");

        //execute post
        $result = curl_exec($ch);
        fflush($fh); 
        fclose($fh);
        curl_close($ch);
        exec("xls2csv /tmp/b.xls", $result);
        if(DEBUG) fwrite(STDERR, "xls2csv /tmp/b.xls\n");
        $result = implode("\n", $result);
        return $result;
}

function getCSV_line($csv, $line) {
        $line = max(0, intval($line - 1)); // -1 à cause du header

        $out = fopen("php://temp", 'rw');
        fwrite($out, $csv);
        rewind($out);

        $head = champsLongToShort(fgetcsv($out));
        while ($line--) fgetcsv($out);
        $l = fgetcsv($out);
        fclose($out);
        if(!$l) return FALSE;
        return array_combine($head, $l);
}


function champsLongToShort($a) {
        $r = array();
        $x = array_flip($GLOBALS['champs']);
        foreach($a as $k => $v) {
                if(isset($x[$v])) $r[$k] = $x[$v];
                else $r[$k] = -1;
        }
        return $r;
}

function champsLongToShort2($a) {
        $r = array();
        $x = array_flip($GLOBALS['champs']);
        foreach($a as $k => $v) {
                if(isset($x[$k])) $r[$x[$k]] = $v;
                else $r[$k] = $v;
        }
        return $r;
}