Exemplo n.º 1
0
if (isset($_POST["repoaction"]) && $_POST["repoaction"] == "Sync") {
    $c = curl_init('https://repository.opendcim.org/api/manufacturer');
    curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_COOKIEFILE, "/tmp/repocookies.txt");
    curl_setopt($c, CURLOPT_COOKIEJAR, "/tmp/repocookies.txt");
    curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'GET');
    $result = curl_exec($c);
    $jr = json_decode($result);
    $m = new Manufacturer();
    if (is_array($jr->manufacturers)) {
        foreach ($jr->manufacturers as $tmpman) {
            $m->GlobalID = $tmpman->ManufacturerID;
            if ($m->getManufacturerByGlobalID()) {
                $m->Name = $tmpman->Name;
                $m->UpdateManufacturer();
            } else {
                // We don't already have this one linked, so search for a candidate or add as a new one
                $m->Name = $tmpman->Name;
                if ($m->GetManufacturerByName()) {
                    // Reset to the values from the repo (especially CaSe)
                    $m->GlobalID = $tmpman->ManufacturerID;
                    $m->Name = $tmpman->Name;
                    $m->UpdateManufacturer();
                } else {
                    $m->ManufacturerID = $tmpman->ManufacturerID;
                    $m->Name = $tmpman->Name;
                    $m->CreateManufacturer();
                }