/** * updates/creates/deletes the reciprocal property * * @param string $action * @param int $userId * @param int $schemaId * @param Connection $con * * @throws \PropelException */ public function updateReciprocal($action, $userId, $schemaId, $con = null) { $inverseProfilePropertyId = $this->getProfileProperty()->getInverseProfilePropertyId(); if (empty($inverseProfilePropertyId) and $this->getProfileProperty()->getIsReciprocal()) { $inverseProfilePropertyId = $this->getProfileProperty()->getId(); } if (!$inverseProfilePropertyId) { //there's no reciprocal or inverse to process return; } $relatedPropertyId = $this->getRelatedSchemaPropertyId(); if (!$relatedPropertyId) { $relatedProperty = SchemaPropertyPeer::retrieveByUri($this->getObject()); if (!$relatedProperty) { //there's no related property in the registry return; } else { $relatedPropertyId = $relatedProperty->getId(); $this->setRelatedSchemaPropertyId($relatedPropertyId); $this->save(); } } $schemaPropertyID = $this->getSchemaPropertyId(); $property = $this->getSchemaPropertyRelatedBySchemaPropertyId(); //does the user have editorial rights to the reciprocal... $permission = false; //get the maintainers of the reciprocal property $maintainers = $property->getSchema()->getMaintainerIds(); foreach ($maintainers as $maintainerId) { if ($userId == $maintainerId) { $permission = true; break; } } if (false === $permission) { return; } $c = new Criteria(); $c->add(SchemaPropertyElementPeer::SCHEMA_PROPERTY_ID, $relatedPropertyId); $c->add(SchemaPropertyElementPeer::PROFILE_PROPERTY_ID, $inverseProfilePropertyId); $c->add(SchemaPropertyElementPeer::OBJECT, $property->getUri()); $recipElement = SchemaPropertyElementPeer::doSelectOne($c, $con); $recipSchemaProperty = SchemaPropertyPeer::retrieveByPK($relatedPropertyId, $con); $recipProfileProperty = ProfilePropertyPeer::retrieveByPK($inverseProfilePropertyId, $con); $statusId = $this->getStatusId(); $language = ''; if ($recipProfileProperty) { $recipField = $recipProfileProperty->getName(); if ($recipProfileProperty->getHasLanguage()) { $language = $this->getLanguage(); } } //if action == deleted then if ('deleted' == $action && $recipElement) { //delete the reciprocal $recipElement->delete($con); return; } //undelete the element if it's deleted and we get this far if (isset($recipElement)) { $recipElement->setDeletedAt(null); } //if action == added, and reciprocal doesn't exist if ('added' == $action && !$recipElement) { //add the reciprocal $recipElement = SchemaPropertyElementPeer::createElement($recipSchemaProperty, $userId, $inverseProfilePropertyId, $statusId, $language, false, true); } //if action == updated if ('updated' == $action) { //check to see if there's a reciprocal if (!$recipElement) { //create a new one $recipElement = SchemaPropertyElementPeer::createElement($recipSchemaProperty, $userId, $inverseProfilePropertyId, $statusId, $language, false, true); } } if ($recipElement) { if (isset($this->importId)) { $recipElement->importId = $this->importId; } $recipElement->setUpdatedUserId($userId); $recipElement->setRelatedSchemaPropertyId($schemaPropertyID); $recipElement->setObject($this->getSchemaPropertyRelatedBySchemaPropertyId()->getUri()); $recipElement->save($con); } return; }
/** * @param SchemaProperty $property * @param string $fieldName * @param string $object * @param int $objectId if the object has a related Id * @param int $userId * @param array $fieldIds * @param int $statusId * @param Connection $con * * @throws Exception * @throws PropelException * @return bool */ public static function updateRelatedElements($property, $fieldName, $object, $objectId, $userId, $fieldIds, $statusId, $con) { if (isset($fieldIds[$fieldName])) { $profileId = $fieldIds[$fieldName]['id']; } else { return false; } $language = $fieldIds[$fieldName]['hasLang'] ? $property->getLanguage() : null; $element = SchemaPropertyElementPeer::lookupDetailElement($property->getId(), $profileId, $language); if ($element) { //no matter what we do, it's not generated any more $element->setIsGenerated(false); //did we make it null? if (0 === strlen(trim($object))) { //we have to make sure that it's not a subclass or subproperty if (('is_subproperty_of' == $fieldName || 'is_subclass_of' == $fieldName) && $property->getParentUri()) { //there's a uri but it doesn't match anything registered //so we have to delete just the reciprocal $element->updateReciprocal('deleted', $userId, $property->getSchemaId(), $con); } else { //delete the element $element->delete($con); } } else { //modify it $element->setObject($object); $element->setRelatedSchemaPropertyId($objectId); $element->setUpdatedUserId($userId); $element->save(); } } elseif ($profileId && $object) { //create one $element = SchemaPropertyElementPeer::createElement($property, $userId, $profileId, $statusId, $language, false); $element->setObject($object); $element->setRelatedSchemaPropertyId($objectId); $element->setIsSchemaProperty(TRUE); $element->save(); } return true; }