示例#1
0
 protected function importEntity($entity_payload)
 {
     // Verify the entity payload.
     if (($message = $this->verifyEntityPayload($entity_payload)) !== true) {
         throw new InvalidEntityPayloadException($message);
     }
     // Check to see if any relationships exist.
     if (array_key_exists('relationships', $entity_payload) && is_array($entity_payload['relationships'])) {
         foreach ($entity_payload['relationships'] as $relationship) {
             if (!Entity::exists($relationship['destination_uuid'], $relationship['destination_type'])) {
                 publisher_relationships_save($relationship);
             }
         }
     }
     // Load the entity.
     $entity = Entity::loadByUUID($entity_payload['uuid'], $entity_payload['entity_type']);
     if (!$entity) {
         $entity = $this->importRevisionAsNew($entity_payload);
         if (!$entity) {
             drupal_set_message("There was an error creating the <strong>{$entity_payload['entity_type']}</strong> <code>{$entity_payload['uuid']}</code>. Please check the recent log messages for more details.", 'error');
             return false;
         }
     }
     $original_vuuid = $entity->vuuid();
     // Import the remaining revisions for the entity.
     foreach ($entity_payload['revisions'] as $revision_key => $revision) {
         // Verify the revision.
         if (($message = $this->verifyRevisionPayload($revision)) !== true) {
             throw new InvalidRevisionPayloadException($message);
         }
         // Apply the revision.
         $result = $this->applyRevision($entity, $revision);
         // Do some logic to check and see if we should report on this...
         $should_report = true;
         $revision_key_segments = explode('|', $revision_key);
         if (count($revision_key_segments) == 3 && $revision_key_segments[2] == '1') {
             $should_report = false;
         }
         if ($result && $should_report) {
             // Add a success message to the transaction data.
             drupal_set_message("Updated <strong>{$entity_payload['entity_type']}</strong> <code>{$entity_payload['uuid']}</code> to revision <code>{$revision_key}</code>");
         }
     }
     // Mark the entity as synced.
     $entity->vuuid($original_vuuid);
     publisher_entity_tracking_mark_as_synced($entity, $this->remote);
     // Run the entity handlers after the entire entity has been saved.
     foreach (EntityHandlerRegistry::getEntityHandlers($entity) as $handler) {
         if (!$handler instanceof EntityHandlerBase) {
             continue;
         }
         $handler->entity = $entity;
         $handler->original_entity = $entity;
         $handler->unhandleEntity($this->getEntityFromMetadata($entity->uuid()));
     }
     // Call hook_publisher_entity_received()
     module_invoke_all('publisher_entity_received', $entity, $this->remote);
     return true;
 }
示例#2
0
 public function receive($endpoint, $payload = array())
 {
     if (!array_key_exists('entities', $payload)) {
         throw new MalformedRequestException('The payload must contain entities to delete, but it does not.');
     }
     $deleted[] = array();
     foreach ($payload['entities'] as $to_delete) {
         if (Entity::exists($to_delete['entity_uuid'], $to_delete['entity_type'])) {
             publisher_set_flag('publisher_deleting');
             $entity_ids = entity_get_id_by_uuid($to_delete['entity_type'], array($to_delete['entity_uuid']));
             $entity_id = count($entity_ids) > 0 ? reset($entity_ids) : false;
             if ($entity_id === false) {
                 continue;
             }
             entity_delete($to_delete['entity_type'], $entity_id);
             drupal_set_message(t('<strong>:type</strong> @title deleted successfully.', array(':type' => $to_delete['entity_type'], '@title' => $to_delete['entity_title'])));
             $deleted[] = $to_delete;
         } else {
             drupal_set_message(t('<strong>:type</strong> @title did not exist.', array(':type' => $to_delete['entity_type'], '@title' => $to_delete['entity_title'])));
             $deleted[] = $to_delete;
         }
     }
     return array('deleted' => $deleted);
 }