/**
  * Preparing to display an existing sample with occurrences.
  * When displaying a grid of occurrences, just load the sample and data_entry_helper::species_checklist 
  * will load the occurrences.
  * When displaying just one occurrence we must load the sample and the occurrence
  */
 protected static function getEntity($args, $auth)
 {
     data_entry_helper::$entity_to_load = array();
     // If we know the occurrence ID but not the sample, we must look it up
     if (self::$loadedOccurrenceId && !self::$loadedSampleId) {
         $response = data_entry_helper::get_population_data(array('table' => 'occurrence', 'extraParams' => $auth['read'] + array('id' => self::$loadedOccurrenceId, 'view' => 'detail')));
         if (count($response) != 0) {
             //we found an occurrence
             self::$loadedSampleId = $response[0]['sample_id'];
         }
     }
     // For a single occurrence, if we know the sample ID but not the occurrence, we must
     // look it up
     if (!self::$loadedOccurrenceId && self::$loadedSampleId && !self::getGridMode($args)) {
         $response = data_entry_helper::get_population_data(array('table' => 'occurrence', 'extraParams' => $auth['read'] + array('sample_id' => self::$loadedSampleId, 'view' => 'detail')));
         if (count($response) != 0) {
             //we found an occurrence for this sample
             self::$loadedOccurrenceId = $response[0]['id'];
             self::$occurrenceIds = array(self::$loadedOccurrenceId);
         }
     }
     // For a single occurrence we must load the occurrence record.
     if (self::$loadedOccurrenceId && !self::getGridMode($args)) {
         data_entry_helper::load_existing_record($auth['read'], 'occurrence', self::$loadedOccurrenceId, 'detail', false, true);
     }
     // Load the sample record
     if (self::$loadedSampleId) {
         data_entry_helper::load_existing_record($auth['read'], 'sample', self::$loadedSampleId, 'detail', false, true);
         if (!empty(data_entry_helper::$entity_to_load['sample:parent_id'])) {
             data_entry_helper::load_existing_record($auth['read'], 'sample', data_entry_helper::$entity_to_load['sample:parent_id']);
         }
     }
     // Ensure that if we are used to load a different survey's data, then we get the correct survey attributes. We can change args
     // because the caller passes by reference.
     $args['survey_id'] = data_entry_helper::$entity_to_load['sample:survey_id'];
     $args['sample_method_id'] = data_entry_helper::$entity_to_load['sample:sample_method_id'];
     // enforce that people only access their own data, unless explicitly have permissions
     $editor = !empty($args['edit_permission']) && function_exists('user_access') && user_access($args['edit_permission']);
     if ($editor) {
         return;
     }
     $readOnly = !empty($args['ro_permission']) && function_exists('user_access') && user_access($args['ro_permission']);
     if (function_exists('hostsite_get_user_field') && data_entry_helper::$entity_to_load['sample:created_by_id'] != 1 && data_entry_helper::$entity_to_load['sample:created_by_id'] !== hostsite_get_user_field('indicia_user_id')) {
         if ($readOnly) {
             self::$mode = self::MODE_EXISTING_RO;
         } else {
             throw new exception(lang::get('Attempt to access a record you did not create'));
         }
     }
 }
 /**
  * Preparing to display an existing sample with occurrences.
  * When displaying a grid of occurrences, just load the sample and data_entry_helper::species_checklist 
  * will load the occurrences.
  * When displaying just one occurrence we must load the sample and the occurrence
  */
 protected static function getEntity($args, $auth)
 {
     data_entry_helper::$entity_to_load = array();
     if (self::getGridMode($args)) {
         // multi-record mode using a checklist grid. We really just need to know the sample ID.
         if (self::$loadedOccurrenceId && !self::$loadedSampleId) {
             $response = data_entry_helper::get_population_data(array('table' => 'occurrence', 'extraParams' => $auth['read'] + array('id' => self::$loadedOccurrenceId, 'view' => 'detail')));
             if (count($response) != 0) {
                 //we found an occurrence so use it to detect the sample
                 self::$loadedSampleId = $response[0]['sample_id'];
             }
         }
     } else {
         // single record entry mode. We want to load the occurrence entity and to know the sample ID.
         if (self::$loadedOccurrenceId) {
             data_entry_helper::load_existing_record($auth['read'], 'occurrence', self::$loadedOccurrenceId, 'detail', false, true);
         } elseif (self::$loadedSampleId) {
             $response = data_entry_helper::get_population_data(array('table' => 'occurrence', 'extraParams' => $auth['read'] + array('sample_id' => self::$loadedSampleId, 'view' => 'detail')));
             self::$loadedOccurrenceId = $response[0]['id'];
             data_entry_helper::load_existing_record_from($response[0], $auth['read'], 'occurrence', self::$loadedOccurrenceId, 'detail', false, true);
         }
         self::$loadedSampleId = data_entry_helper::$entity_to_load['occurrence:sample_id'];
     }
     // Load the sample record
     if (self::$loadedSampleId) {
         data_entry_helper::load_existing_record($auth['read'], 'sample', self::$loadedSampleId, 'detail', false, true);
         // If there is a parent sample - load it next so the details overwrite the child sample.
         if (!empty(data_entry_helper::$entity_to_load['sample:parent_id'])) {
             data_entry_helper::load_existing_record($auth['read'], 'sample', data_entry_helper::$entity_to_load['sample:parent_id']);
             self::$loadedSampleId = data_entry_helper::$entity_to_load['sample:id'];
         }
     }
     // Ensure that if we are used to load a different survey's data, then we get the correct survey attributes. We can change args
     // because the caller passes by reference.
     $args['survey_id'] = data_entry_helper::$entity_to_load['sample:survey_id'];
     $args['sample_method_id'] = data_entry_helper::$entity_to_load['sample:sample_method_id'];
     // enforce that people only access their own data, unless explicitly have permissions
     $editor = !empty($args['edit_permission']) && function_exists('user_access') && user_access($args['edit_permission']);
     if ($editor) {
         return;
     }
     $readOnly = !empty($args['ro_permission']) && function_exists('user_access') && user_access($args['ro_permission']);
     if (function_exists('hostsite_get_user_field') && data_entry_helper::$entity_to_load['sample:created_by_id'] != 1 && data_entry_helper::$entity_to_load['sample:created_by_id'] !== hostsite_get_user_field('indicia_user_id')) {
         if ($readOnly) {
             self::$mode = self::MODE_EXISTING_RO;
         } else {
             throw new exception(lang::get('Attempt to access a record you did not create'));
         }
     }
 }