/**
 * Handles the construction of a submission array from a set of form values.
 * @param array $values Associative array of form data values.
 * @param array $args iform parameters.
 * @return array Submission structure.
 */
function create_submission($values, $args)
{
    // Any remembered fields need to be made available to the hook function outside this class.
    global $remembered;
    $remembered = isset($args['remembered']) ? $args['remembered'] : '';
    //Page only supported in grid mode at the moment.
    if (isset($values['gridmode'])) {
        $submission = data_entry_helper::build_sample_subsamples_occurrences_submission($values);
    } else {
        drupal_set_message('Please set the page to "gridmode"');
    }
    //The parent sample method is always effort for Shorewatch
    $submission['fields']['sample_method_id'] = $args['effort'];
    $readAuth = data_entry_helper::get_read_auth($args['website_id'], $args['password']);
    //Get the platform height which is stored against the location itself.
    $locationAttributeValueRow = data_entry_helper::get_population_data(array('table' => 'location_attribute_value', 'extraParams' => $readAuth + array('location_id' => $submission['fields']['location_id']['value'], 'location_attribute_id' => $args['platform_height'])));
    if (!empty($locationAttributeValueRow[0]['value'])) {
        $platformHeight = $locationAttributeValueRow[0]['value'];
    }
    $enteredSref = $submission['fields']['entered_sref']['value'];
    //Cycle through each occurrence
    foreach ($submission['subModels'] as &$occurrenceAndSubSampleRecord) {
        if (!empty($occurrenceAndSubSampleRecord['model']['subModels'][0]['model']['fields'])) {
            //Assign the attributes associated with the occurrence to a variable to make it easier to work with.
            $occurrenceAttrsToMatch = $occurrenceAndSubSampleRecord['model']['subModels'][0]['model']['fields'];
            foreach ($occurrenceAttrsToMatch as $occurrenceAttrToMatchKey => $occurrenceAttrToMatch) {
                //Get the reticule and bearing.
                if (preg_match('/^occAttr:' . $args['bearing_to_sighting'] . '.*/', $occurrenceAttrToMatchKey)) {
                    $bearing = $occurrenceAttrToMatch['value'];
                }
                if (preg_match('/^occAttr:' . $args['reticules'] . '.*/', $occurrenceAttrToMatchKey)) {
                    $reticuleToUse = $occurrenceAttrToMatch['value'];
                }
            }
            //Collect the actual reticules value by looking it up in the termlists_term view as currently we only have an id from the drop-down
            $readAuth = data_entry_helper::get_read_auth($args['website_id'], $args['password']);
            if (!empty($reticuleToUse)) {
                $termlistsTermsRow = data_entry_helper::get_population_data(array('table' => 'termlists_term', 'extraParams' => $readAuth + array('id' => $reticuleToUse)));
            }
            if (!empty($termlistsTermsRow[0]['term'])) {
                $reticules = $termlistsTermsRow[0]['term'];
            }
        }
        //We calculate the grid ref of the cetacean by using the reticules/bearing/platform height and the original entered sptial reference. This is stored in the sub-sample associated with the occurrence.
        if (empty($platformHeight) || empty($bearing) || empty($reticules)) {
            $occurrenceAndSubSampleRecord['model']['fields']['entered_sref']['value'] = $enteredSref;
        } else {
            $occurrenceAndSubSampleRecord['model']['fields']['entered_sref']['value'] = calculate_sighting_sref($bearing, $reticules, $platformHeight, $enteredSref, $args);
        }
        //Sub-sample sample method is always reticule sighting
        $occurrenceAndSubSampleRecord['model']['fields']['sample_method_id']['value'] = $args['reticule_sighting'];
        //We need to copy the location information to the sub-sample else it won't get
        //picked up in the cache occurrences table.
        if (!empty($submission['fields']['location_id']['value'])) {
            $occurrenceAndSubSampleRecord['model']['fields']['location_id']['value'] = $submission['fields']['location_id']['value'];
        }
        if (!empty($submission['fields']['location_name']['value'])) {
            $occurrenceAndSubSampleRecord['model']['fields']['location_name']['value'] = $submission['fields']['location_name']['value'];
        }
        //The following only applies to the Cetaceans Seen? which doesn't appear in adhoc mode.
        //Show the second tab when Cetaeans Seen is set
        if (!$args['adhocMode']) {
            //Start by assuming the second tab isn't showing.
            $gridTabShowing = false;
            //Find the value the user has set for Cetaceans Seen?
            foreach ($values as $key => $value) {
                if (substr($key, 0, 10) == "smpAttr:" . $args['cetaceans_seen']) {
                    if ($value == $args['cetaceans_seen_yes']) {
                        $gridTabShowing = true;
                    }
                }
            }
            //If Cetacean's Seen is false then we need to delete any existing Occurrences that were on the second tab if the user is editing existing data.
            //Note this does not apply to adhoc mode where the grid is on the first tab, we have already checked for adhoc mode earlier so don't need to here.
            if ($gridTabShowing === false) {
                $occurrenceAndSubSampleRecord['model']['fields']['deleted']['value'] = 't';
                $occurrenceAndSubSampleRecord['model']['subModels'][0]['model']['fields']['deleted']['value'] = 't';
            }
        }
    }
    return $submission;
}
 /**
  * Handles the construction of a submission array from a set of form values.
  * @param array $values Associative array of form data values.
  * @param array $args iform parameters.
  * @return array Submission structure.
  */
 public static function get_submission($values, $args)
 {
     // Any remembered fields need to be made available to the hook function outside this class.
     global $remembered;
     $remembered = isset($args['remembered']) ? $args['remembered'] : '';
     // default for forms setup on old versions is grid - list of occurrences
     // Can't call getGridMode in this context as we might not have the $_GET value to indicate grid
     if (isset($values['speciesgridmapmode'])) {
         $submission = data_entry_helper::build_sample_subsamples_occurrences_submission($values);
     } else {
         if (isset($values['gridmode'])) {
             $submission = data_entry_helper::build_sample_occurrences_list_submission($values);
         } else {
             $submission = data_entry_helper::build_sample_occurrence_submission($values);
         }
     }
     return $submission;
 }
 /**
  * Handles the construction of a submission array from a set of form values.
  * @param array $values Associative array of form data values.
  * @param array $args iform parameters.
  * @return array Submission structure.
  * @todo: Implement this method
  */
 public static function get_submission($values, $args)
 {
     $subsampleModels = array();
     if (isset($values['page']) && $values['page'] == 'speciesmap') {
         $submission = data_entry_helper::build_sample_subsamples_occurrences_submission($values);
     } else {
         if (!isset($values['page']) || $values['page'] == 'mainSample') {
             // submitting the first page, with top level sample details
             $read = array('nonce' => $values['read_nonce'], 'auth_token' => $values['read_auth_token']);
             if (!isset($values['sample:entered_sref'])) {
                 // the sample does not have sref data, as the user has just picked a transect site at this point. Copy the
                 // site's centroid across to the sample. Should this be cached?
                 $site = data_entry_helper::get_population_data(array('table' => 'location', 'extraParams' => $read + array('view' => 'detail', 'id' => $values['sample:location_id'], 'deleted' => 'f')));
                 $site = $site[0];
                 $values['sample:entered_sref'] = $site['centroid_sref'];
                 $values['sample:entered_sref_system'] = $site['centroid_sref_system'];
             }
             // Build the subsamples
             $sections = data_entry_helper::get_population_data(array('table' => 'location', 'extraParams' => $read + array('view' => 'detail', 'parent_id' => $values['sample:location_id'], 'deleted' => 'f'), 'nocache' => true));
             if (isset($values['sample:id'])) {
                 $existingSubSamples = data_entry_helper::get_population_data(array('table' => 'sample', 'extraParams' => $read + array('view' => 'detail', 'parent_id' => $values['sample:id'], 'deleted' => 'f'), 'nocache' => true));
             } else {
                 $existingSubSamples = array();
             }
             $sampleMethods = helper_base::get_termlist_terms(array('read' => $read), 'indicia:sample_methods', array('Transect Section'));
             $attributes = data_entry_helper::getAttributes(array('valuetable' => 'sample_attribute_value', 'attrtable' => 'sample_attribute', 'key' => 'sample_id', 'fieldprefix' => 'smpAttr', 'extraParams' => $read, 'survey_id' => $values['sample:survey_id'], 'sample_method_id' => $sampleMethods[0]['id'], 'multiValue' => false));
             $smpDate = self::parseSingleDate($values['sample:date']);
             foreach ($sections as $section) {
                 $smp = false;
                 $exists = false;
                 foreach ($existingSubSamples as $existingSubSample) {
                     if ($existingSubSample['location_id'] == $section['id']) {
                         $exists = $existingSubSample;
                         break;
                     }
                 }
                 if (!$exists) {
                     $smp = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array('survey_id' => array('value' => $values['sample:survey_id']), 'website_id' => array('value' => $values['website_id']), 'date' => array('value' => $values['sample:date']), 'location_id' => array('value' => $section['id']), 'entered_sref' => array('value' => $section['centroid_sref']), 'entered_sref_system' => array('value' => $section['centroid_sref_system']), 'sample_method_id' => array('value' => $sampleMethods[0]['id']))), 'copyFields' => array('date_start' => 'date_start', 'date_end' => 'date_end', 'date_type' => 'date_type'));
                     foreach ($attributes as $attr) {
                         foreach ($values as $key => $value) {
                             $parts = explode(':', $key);
                             if (count($parts) > 1 && $parts[0] == 'smpAttr' && $parts[1] == $attr['attributeId']) {
                                 $smp['model']['fields']['smpAttr:' . $attr['attributeId']] = array('value' => $value);
                             }
                         }
                     }
                 } else {
                     // need to ensure any date change is propagated: only do if date has changed for performance reasons.
                     $subSmpDate = self::parseSingleDate($exists['date_start']);
                     if (strcmp($smpDate, $subSmpDate)) {
                         $smp = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array('survey_id' => array('value' => $values['sample:survey_id']), 'website_id' => array('value' => $values['website_id']), 'id' => array('value' => $exists['id']), 'date' => array('value' => $values['sample:date']), 'location_id' => array('value' => $exists['location_id']))), 'copyFields' => array('date_start' => 'date_start', 'date_end' => 'date_end', 'date_type' => 'date_type'));
                     }
                 }
                 if ($smp) {
                     $subsampleModels[] = $smp;
                 }
             }
         }
         $submission = submission_builder::build_submission($values, array('model' => 'sample'));
         if (count($subsampleModels) > 0) {
             $submission['subModels'] = $subsampleModels;
         }
     }
     return $submission;
 }