Subversion Repositories eFlore/Applications.cel

Rev

Rev 1773 | Blame | Last modification | View Log | RSS feed

<?php

global $champs;
$champs = Array(
        'nom_sel' => 'Espèce',
        'nom_sel_nn' => 'Numéro nomenclatural',
        'nom_ret' => 'Nom retenu',
        'nom_ret_nn' => 'Numéro nomenclatural nom retenu',
        'nt' => 'Numéro 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' => 'Référentiel Géographique',

        '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;
        $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 = array();
        preg_match('/.*PHPSESSID=(\w+).*/', $result, $sess);
        if(isset($sess[1])) {
                define('COOKIE', $sess[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/ImportXLS';
        if(!is_file($d)) return FALSE;
        $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);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
}

function export($cols = "standard,avance") {
        if(!defined('COOKIE')) return FALSE;

        $URL = DOMAIN . '/jrest/CelWidgetExport/export?';
        $URL .= http_build_query(array('colonnes' => $cols,
                                                                   'id_utilisateur' => USER,
                                                                   'format' => 'csv'));
        $ch = curl_init();

        curl_setopt($ch,CURLOPT_URL, $URL);
        curl_setopt($ch,CURLOPT_COOKIE, 'PHPSESSID=' . COOKIE);

        if(DEBUG) fwrite(STDERR, "curl -b PHPSESSID=" . COOKIE . ' "' . $URL ."\"\n");

        curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
        //execute post
        $result = curl_exec($ch);
        curl_close($ch);
        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;
}