Example #1
0
function _import_oami(&$args, &$start, &$count)
{
    $result = '';
    $start = $count = 0;
    $ds = DIRECTORY_SEPARATOR;
    $root = sfConfig::get('sf_root_dir') . $ds . sfConfig::get('sf_web_dir_name') . $ds . "uploads" . $ds;
    $zipFile = $args[0];
    $source = $root . "zip" . $ds . $zipFile;
    $outDir = $root . "files" . $ds;
    $zip = new ZipArchive();
    $x = $zip->open($source);
    // open the zip file to extract
    if ($x == true) {
        // 20140502
        $nameParts = explode("_", $zipFile);
        $contestation = $nameParts[2];
        $contestationDate = date("Y-m-d", strtotime(substr($contestation, 0, 4) . "-" . substr($contestation, 4, 2) . "-" . substr($contestation, 6, 2) . " +3months"));
        //exit("\nContestationDate: ".$contestationDate."\n");
        $extract = $zip->extractTo($outDir);
        // place in the directory with same name
        $zip->close();
        //unlink($source);
        _echo_cms('SUCCESS: ZIP file has been unziped.');
        _echo_cms('--------------------------------------------------------------------------------');
        $files = array();
        $ind = 0;
        foreach (glob($outDir . '*.*') as $f) {
            $files[] = $f;
            //var_dump($f);
            $parts = pathinfo($f);
            if (substr($parts['basename'], 0, 10) == 'DIFF_CTMS_') {
                //					$c = new Criteria();
                //					$c->add(EmailTemplatePeer::ID, 7206, '>');
                //					EmailTemplatePeer::doDelete($c);
                //echo "*** done delete *** | ";
                $fileContent = file_get_contents($f);
                if ($fileContent !== false && $fileContent > '') {
                    _echo_cms_title("Importing XML file: " . $f);
                    $enc = mb_detect_encoding($fileContent, 'ISO-8859-1');
                    //var_dump($enc);
                    $mainData = json_decode(json_encode((array) simplexml_load_string($fileContent, null, LIBXML_NOCDATA)), 1);
                    //var_dump($mainData); //exit();
                    $trademarks = $mainData["TradeMarkTransactionBody"]["TransactionContentDetails"]["TransactionData"]["TradeMarkDetails"]["TradeMark"];
                    echo "cnt=" . count($trademarks) . "\n";
                    unset($mainData);
                    //var_dump($trademarks[0]); //die("\n-------------------\n");
                    foreach ($trademarks as $t) {
                        $dc = $t['DesignatedCountryDetails']['DesignatedCountry'];
                        $cc = array();
                        foreach ($dc as $dd) {
                            $cc[] = $dd['DesignatedCountryCode'];
                        }
                        //if(!in_array(array("EM","BG"),$cc)) continue;
                        if ($t['@attributes']['operationCode'] != 'Insert') {
                            continue;
                        }
                        // only INSERT
                        //var_dump($t); //_echo_cms('--------------------------------------------------------------------------------'); continue;
                        $c = new Criteria();
                        $c->add(TrademarkPeer::APPLICATION_NUMBER, $t['ApplicationNumber']);
                        $tm = TrademarkPeer::doSelectOne($c);
                        if (!$tm) {
                            $tm = new Trademark();
                        }
                        $tm->setFromSystem(2);
                        // OAMI
                        $tm->setApplicationNumber($t['ApplicationNumber']);
                        //if ($t['ApplicationNumber'] == '012438404') var_dump($t);
                        $tm->setApplicationDate($t['ApplicationDate']);
                        $tm->setRegisterNumber($t['BasicRegistrationApplicationDetails']['BasicRegistrationApplication']['BasicRegistrationDetails']['BasicRegistration']['BasicRegistrationNumber']);
                        $tm->setRegistrationDate($t['BasicRegistrationApplicationDetails']['BasicRegistrationApplication']['BasicRegistrationDetails']['BasicRegistration']['BasicRegistrationDate']);
                        $tm->setExpiresOn($t['ExpiryDate']);
                        //						$tm->setContestation($contestationDate);
                        $tm->setDesignatedContractingParty(implode(',', $cc));
                        if ($t['WordMarkSpecification']['MarkVerbalElementText']) {
                            $tm->setLabel($t['WordMarkSpecification']['MarkVerbalElementText']);
                        } else {
                            $tm->setLabel('no label trademark: ' . $t['ApplicationNumber']);
                        }
                        if (isset($t['MarkImageDetails']['MarkImage'])) {
                            if (isset($t['MarkImageDetails']['MarkImage']['MarkImageFilename'])) {
                                $img = $t['MarkImageDetails']['MarkImage']['MarkImageFilename'];
                            }
                            if (isset($t['MarkImageDetails']['MarkImage']['MarkImageCategory']['CategoryCodeDetails']['CategoryCode'])) {
                                if (is_array($t['MarkImageDetails']['MarkImage']['MarkImageCategory']['CategoryCodeDetails']['CategoryCode'])) {
                                    $tm->setViennaClasses(implode(',', $t['MarkImageDetails']['MarkImage']['MarkImageCategory']['CategoryCodeDetails']['CategoryCode']));
                                } else {
                                    $tm->setViennaClasses($t['MarkImageDetails']['MarkImage']['MarkImageCategory']['CategoryCodeDetails']['CategoryCode']);
                                }
                            }
                        }
                        if (isset($t['GoodsServicesDetails']['GoodsServices']['ClassDescriptionDetails']['ClassDescription'][0])) {
                            $codes = array();
                            foreach ($t['GoodsServicesDetails']['GoodsServices']['ClassDescriptionDetails']['ClassDescription'] as $ic) {
                                $codes[] = $ic['ClassNumber'];
                            }
                            $tm->setNiceClasses(implode(',', $codes));
                        } else {
                            $tm->setNiceClasses($t['GoodsServicesDetails']['GoodsServices']['ClassDescriptionDetails']['ClassDescription']['ClassNumber']);
                        }
                        if ($t['MarkFeature'] == 'Figurative') {
                            $tm->setKind('image');
                        } elseif ($t['MarkFeature'] == 'Word') {
                            $tm->setKind('text');
                        } elseif ($t['MarkFeature'] == 'Sound') {
                            $tm->setKind('sound');
                        } else {
                            $tm->setKind('mixed');
                        }
                        if (isset($t['ApplicantDetails']['ApplicantKey'][0])) {
                            $recs = array();
                            foreach ($t['ApplicantDetails']['ApplicantKey'] as $rr) {
                                $recs[] = $rr['Identifier'];
                            }
                            $tm->setRightsOwner(implode(',', $recs));
                            $tm->setRightsOwnerId(implode(',', $recs));
                        } else {
                            $tm->setRightsOwner($t['ApplicantDetails']['ApplicantKey']['Identifier']);
                            $tm->setRightsOwnerId($t['ApplicantDetails']['ApplicantKey']['Identifier']);
                        }
                        $tm->setRightsOwnerAddress('');
                        $tm->setRightsRepresentative($t['RepresentativeDetails']['RepresentativeKey']['Identifier']);
                        $tm->setRightsRepresentativeId($t['RepresentativeDetails']['RepresentativeKey']['Identifier']);
                        $tm->setRightsRepresentativeAddress('');
                        $tm->setOfficeOfOrigin($t['RegistrationOfficeCode']);
                        $tm->setStatus($t['MarkCurrentStatusCode']);
                        $pDate = '-';
                        $pub = '-';
                        $contestation = '-';
                        if (isset($t['PublicationDetails']['Publication'])) {
                            //							_echo_cms("* checking #$ind: ".$tm->getLabel());
                            // if we have multiple publications
                            if (isset($t['PublicationDetails']['Publication'][0])) {
                                //_echo_cms("A) checking #$ind: ".$tm->getLabel());
                                /*								$recs = array();
                                								foreach ($t['PublicationDetails']['Publication'] as $pp)
                                								{
                                								$recs[] = $pp['PublicationDate'].' | '.$pp['PublicationIdentifier'].' | '.$pp['PublicationSection'];
                                								}
                                								$tm->setPublications(implode("\n", $recs));*/
                                $recs = '';
                                $found = false;
                                foreach ($t['PublicationDetails']['Publication'] as $pp) {
                                    $recs = $pp['PublicationDate'] . ' | ' . $pp['PublicationIdentifier'] . ' | ' . $pp['PublicationSection'];
                                    $pDate = $pp['PublicationDate'];
                                    $pub = $pp['PublicationSection'];
                                    $today = date('Y-m-d');
                                    if ($pp['PublicationSection'] == 'M.1') {
                                        $contestation = date('Y-m-d', strtotime("+10 months", strtotime($pp['PublicationDate'])));
                                        if ($today > $contestation) {
                                            //echo "SKIP M1: $today > $contestation | ";
                                            continue;
                                        }
                                        //echo "FOUND M1: $today  < $contestation \n";
                                        $contestation = date('Y-m-d', strtotime("+9 months", strtotime($pp['PublicationDate'])));
                                        $tm->setContestation($contestation);
                                        $found = true;
                                    } else {
                                        if ($pp['PublicationSection'] == 'A.1') {
                                            $contestation = date('Y-m-d', strtotime("+4 months", strtotime($pp['PublicationDate'])));
                                            if ($today > $contestation) {
                                                //echo "SKIP A1: $today > $contestation | ";
                                                continue;
                                            }
                                            //echo "FOUND A1: $today  < $contestation \n";
                                            $contestation = date('Y-m-d', strtotime("+3 months", strtotime($pp['PublicationDate'])));
                                            $tm->setContestation($contestation);
                                            $found = true;
                                        }
                                    }
                                }
                                if (!$found) {
                                    //echo " > Not Found\n";
                                    continue;
                                }
                                $tm->setPublications($recs);
                            } else {
                                //_echo_cms("B) checking #$ind: ".$tm->getLabel());
                                $pDate = $t['PublicationDetails']['Publication']['PublicationDate'];
                                $pub = $t['PublicationDetails']['Publication']['PublicationSection'];
                                $today = date('Y-m-d');
                                if ($pub == 'M.1') {
                                    $contestation = date('Y-m-d', strtotime("+10 months", strtotime($pDate)));
                                    if ($today > $contestation) {
                                        //echo "SKIP M1: $today > $contestation \n";
                                        continue;
                                    }
                                    //echo "FOUND M1: $today  < $contestation \n";
                                    $contestation = date('Y-m-d', strtotime("+9 months", strtotime($pDate)));
                                    $tm->setContestation($contestation);
                                } else {
                                    if ($pub == 'A.1') {
                                        $contestation = date('Y-m-d', strtotime("+4 months", strtotime($pDate)));
                                        if ($today > $contestation) {
                                            //echo "SKIP A1: $today > $contestation \n";
                                            continue;
                                        }
                                        //echo "FOUND A1: $today  < $contestation \n";
                                        $contestation = date('Y-m-d', strtotime("+3 months", strtotime($pDate)));
                                        $tm->setContestation($contestation);
                                    } else {
                                        //echo " > Not Found A1 or M1\n";
                                        continue;
                                        // skip OTHER Publications except A.1 and M.1
                                    }
                                }
                                $tm->setPublications($t['PublicationDetails']['Publication']['PublicationDate'] . ' | ' . $t['PublicationDetails']['Publication']['PublicationIdentifier'] . ' | ' . $t['PublicationDetails']['Publication']['PublicationSection']);
                            }
                        } else {
                            //echo "SKIP: #$ind: ".$tm->getLabel()." : NO PUBLICATION\n";
                            continue;
                        }
                        $tm->save();
                        if ($start == 0) {
                            $start = $tm->getId();
                        }
                        $ind++;
                        $count++;
                        _echo_cms("{$ind}: " . $tm->getLabel() . " | pub={$pub}, contestation={$contestation}");
                    }
                    unset($trademarks);
                }
                echo "*** XML import done! ***\n";
                unset($fileContent);
                $result = 'imported';
            }
        }
        //var_dump($files);
        foreach ($files as $f) {
            @unlink($f);
        }
    } else {
        _echo_cms_error('ERROR: Error unzipping uploaded file.');
        $result = 'error';
    }
    return $result;
}