/** * Parse the XSD and update the list of search fields * * @param MetadataProfile $metadataProfile * @param partnerId * * @return TBD */ public static function parseProfileSearchFields($partnerId, MetadataProfile $metadataProfile) { $key = $metadataProfile->getSyncKey(MetadataProfile::FILE_SYNC_METADATA_DEFINITION); $xsdPath = kFileSyncUtils::getLocalFilePathForKey($key); $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'true'); MetadataProfileFieldPeer::setUseCriteriaFilter(false); $profileFields = MetadataProfileFieldPeer::retrieveByMetadataProfileId($metadataProfile->getId()); MetadataProfileFieldPeer::setUseCriteriaFilter(true); // check all existing fields foreach ($profileFields as $profileField) { $xPath = $profileField->getXpath(); // field removed if (!isset($xPaths[$xPath])) { $profileField->setStatus(MetadataProfileField::STATUS_DEPRECATED); $profileField->save(); continue; } $xPathData = $xPaths[$xPath]; if ($profileField->getStatus() != MetadataProfileField::STATUS_ACTIVE && isset($xPathData['type']) && ($xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_INT)) { $availableSearchIndex = self::getAvailableSearchIndex($partnerId, $metadataProfile->getObjectType()); if (!isset($availableSearchIndex)) { throw new Exception('could not find available search index'); } $profileField->setSearchIndex($availableSearchIndex); } $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->save(); unset($xPaths[$xPath]); } // add new searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_ACTIVE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); if ($xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_DATE || $xPathData['type'] == MetadataSearchFilter::KMC_FIELD_TYPE_INT) { $availableSearchIndex = self::getAvailableSearchIndex($partnerId, $metadataProfile->getObjectType()); if (!isset($availableSearchIndex)) { throw new Exception('could not find available search index'); } $profileField->setSearchIndex($availableSearchIndex); } $profileField->save(); } } // set none searchable existing fields $xPaths = kXsd::findXpathsByAppInfo($xsdPath, self::APP_INFO_SEARCH, 'false'); foreach ($profileFields as $profileField) { $xPath = $profileField->getXpath(); if (!isset($xPaths[$xPath])) { continue; } $xPathData = $xPaths[$xPath]; if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->save(); unset($xPaths[$xPath]); } // add new none searchable fields foreach ($xPaths as $xPath => $xPathData) { $profileField = new MetadataProfileField(); $profileField->setMetadataProfileId($metadataProfile->getId()); $profileField->setMetadataProfileVersion($metadataProfile->getVersion()); $profileField->setPartnerId($metadataProfile->getPartnerId()); $profileField->setStatus(MetadataProfileField::STATUS_NONE_SEARCHABLE); $profileField->setXpath($xPath); if (isset($xPathData['name'])) { $profileField->setKey($xPathData['name']); } if (isset($xPathData['label'])) { $profileField->setLabel($xPathData['label']); } if (isset($xPathData['type'])) { $profileField->setType($xPathData['type']); } $profileField->save(); } }