public static function get_submission($values, $args) { $values['habitat-count'] = 2; $habitatSamples = array(); for ($i = 1; $i <= $values['habitat-count']; $i++) { $habitatSamples["habitat{$i}"] = array(); } foreach ($values as $key => $value) { if (substr_count($key, ':') === 3) { $parts = explode(':', $key, 4); if ($parts[0] === 'smpAttr') { // If the last part of a habitat field is not a number, this is just a dummy field used // when cloning habitat controls. We also skip empty new attribute values. if (preg_match('/^\\d+$/', $parts[3]) && (!empty($parts[2]) || !empty($value))) { // habitat number is the last part of the attribute field name $id = array_pop($parts); // remove empty stuff from the attribute name (e.g. an unused space for the existing value ID, if a new attribute value). while (empty($parts[count($parts) - 1])) { array_pop($parts); } $fieldname = implode(':', $parts); $habitatSamples["habitat{$id}"][$fieldname] = $value; } unset($values[$key]); } } $parts = explode(':', $key); // For the group_name, the autocomplete functionality needs to be removed from the submission. // So copy the edit box into the underlying field which gets posted. if (array_pop($parts) === 'group_name') { $values[implode(':', $parts)] = $value; } } $buddyPairSubmission = submission_builder::wrap_with_images($values, 'sample'); unset($buddyPairSubmission['fields']['habitat-count']); // Get the list of records implied by the SACFOR data for each habitat. At this point we'll create 1 big list and split // it across the habitats later. $occurrences = data_entry_helper::wrap_species_checklist($values, true, array(), array()); // now work out which habitat contains which occurrence $habitatOccurrences = array(); foreach (array_keys($habitatSamples) as $habitatId) { $habitatOccurrences[$habitatId] = array(); } foreach ($occurrences as $occurrence) { // take a copy of the fields with all habitat data $fields = array_merge($occurrence['model']['fields']); // @todo Remove hard coded field ID. $habitatFields = preg_grep('/occAttr:243(:\\d+)?/', array_keys($fields)); if (count($habitatFields)) { $habitatId = $fields[array_pop($habitatFields)]['value']; } else { // this case occurs when deleting an occurrence, as the habitat ID input field is disabled. Therefore // we need to revert to the original hidden sampleIdx field for the loaded record. $habitatId = $fields['sampleIDX']['value'] + 1; } // zero indexed $habitatOccurrences["habitat{$habitatId}"][] = $occurrence; } // now create the submodel data for each habitat. $buddyPairSubmission['subModels'] = array(); // copy the basic sample data into each set of habitat subsample values foreach ($habitatSamples as $habitatId => &$habitatSample) { $habitatIdx = str_replace('habitat', '', $habitatId); $habitatSample['website_id'] = $values['website_id']; $habitatSample['survey_id'] = $values['survey_id']; if (isset($_POST["habitat_sample_id:{$habitatIdx}"])) { $habitatSample['sample:id'] = $_POST["habitat_sample_id:{$habitatIdx}"]; } $habitatSample['sample:date'] = $values['sample:date']; $habitatSample['sample:entered_sref'] = $values['sample:entered_sref']; $habitatSample['sample:entered_sref_system'] = $values['sample:entered_sref_system']; $habitatSample['sample:input_form'] = $values['sample:input_form']; if (isset($_POST["sample:comment:{$habitatIdx}"])) { $habitatSample['sample:comment'] = $_POST["sample:comment:{$habitatIdx}"]; unset($buddyPairSubmission['fields']["comment:{$habitatIdx}"]); } $habitatSubmission = submission_builder::wrap($habitatSample, 'sample'); $habitatSubmission['subModels'] = $habitatOccurrences[$habitatId]; $buddyPairSubmission['subModels'][] = array('fkId' => 'parent_id', 'model' => $habitatSubmission); } return $buddyPairSubmission; }
/** * Helper function to simplify building of a submission that contains a single supersample, * with multiple subsamples, each of which has multiple occurrences records, as generated * by a species_checklist control. * * @param array $values List of the posted values to create the submission from. * @param boolean $include_if_any_data If true, then any list entry which has any data * set will be included in the submission. Set this to true when hiding the select checkbox * in the grid. * @param array $zero_attrs Set to an array of abundance attribute field IDs that can be * treated as abundances. Alternatively set to true to treat all occurrence custom attributes * as possible zero abundance indicators. * @param array $zero_values Set to an array of values which are considered to indicate a * zero abundance record if found for one of the zero_attrs. Values are case-insensitive. Defaults to * array('0','None','Absent'). * of values that can be treated as meaning a zero abundance record. E.g. * array(' * @return array Sample submission array */ public static function build_sample_subsamples_occurrences_submission($values, $include_if_any_data = false, $zero_attrs = true, $zero_values = array('0', 'None', 'Absent')) { // We're mainly submitting to the sample model $sampleMod = submission_builder::wrap_with_images($values, 'sample'); $subModels = data_entry_helper::wrap_species_checklist_with_subsamples($values, $include_if_any_data, $zero_attrs, $zero_values); // Add the subsamples/occurrences in as subModels without overwriting others such as a sample image if (array_key_exists('subModels', $sampleMod)) { $sampleMod['subModels'] = array_merge($sampleMod['subModels'], $subModels); } else { $sampleMod['subModels'] = $subModels; } return $sampleMod; }
/** * 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) { if (isset($values['source'])) { // comes from main Sites tab, Admins may create so need to check for locations_website entry $locModel = submission_builder::wrap_with_images($values, 'location'); if (isset($values['locations_website:website_id'])) { // assume no other submodels $locModel['subModels'] = array(array('fkId' => 'location_id', 'model' => array('id' => 'locations_website', 'fields' => array('website_id' => array('value' => $values['locations_website:website_id']))))); } return $locModel; } if (isset($values['sample:location_id']) && $values['sample:location_id'] == '') { unset($values['sample:location_id']); } if (isset($values['sample:recorder_names'])) { if (is_array($values['sample:recorder_names'])) { $values['sample:recorder_names'] = implode("\r\n", $values['sample:recorder_names']); } } // else just load the string if (isset($values['location:name'])) { $values['sample:location_name'] = $values['location:name']; } $sampleMod = submission_builder::wrap_with_images($values, 'sample'); if (!isset($values['sample:deleted'])) { if (isset($values['gridmode'])) { $subModels = self::wrap_species_checklist($values); } else { $subModels = submission_builder::wrap_with_images($values, 'occurrence'); } foreach ($values as $key => $value) { $parts = explode(':', $key, 5); if ($parts[0] == 'targ' && $parts[3] == 'presence') { $smp = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array())); $smp['model']['fields']['survey_id'] = array('value' => $values['survey_id']); $smp['model']['fields']['website_id'] = array('value' => $values['website_id']); $smp['model']['fields']['date'] = array('value' => $values['sample:date']); $smp['model']['fields']['smpAttr:' . $parts[4]] = array('value' => $parts[2]); if (isset($values['sample:location_id'])) { $smp['model']['fields']['location_id'] = array('value' => $values['sample:location_id']); } else { if (isset($values['location:parent_id'])) { $smp['model']['fields']['location_id'] = array('value' => $values['location:parent_id']); } else { $smp['model']['fields']['geom'] = array('value' => $values['location:centroid_geom']); $smp['model']['fields']['entered_sref'] = array('value' => $values['location:centroid_sref']); $smp['model']['fields']['entered_sref_system'] = array('value' => $values['location:centroid_sref_system']); } } if ($value != '1') { $smp['model']['fields']['deleted'] = array('value' => 't'); } if ($parts[1] != '-') { $smp['model']['fields']['id'] = array('value' => $parts[1]); } foreach ($values as $key1 => $value1) { $moreParts = explode(':', $key1, 5); if ($moreParts[0] == 'targ' && $moreParts[1] == $parts[1] && $moreParts[2] == $parts[2] && $moreParts[3] == 'smpAttr') { $smp['model']['fields']['smpAttr:' . $moreParts[4]] = array('value' => $value1); } } if (isset($values['sample:location_id'])) { $smp['model']['fields']['location_id'] = array('value' => $values['sample:location_id']); } else { $smp['model']['fields']['centroid_sref'] = array('value' => $values['sample:entered_sref']); $smp['model']['fields']['centroid_sref_system'] = array('value' => $values['sample:entered_sref_system']); $smp['model']['fields']['centroid_geom'] = array('value' => $values['sample:geom']); } if ($value == '1' || $parts[1] != '-') { $subModels[] = $smp; } } } if (count($subModels) > 0) { $sampleMod['subModels'] = $subModels; } if (isset($values['location:location_type_id'])) { $locationMod = submission_builder::wrap_with_images($values, 'location'); $locationMod['subModels'] = array(array('fkId' => 'location_id', 'model' => $sampleMod)); if (array_key_exists('locations_website:website_id', $_POST)) { $lw = submission_builder::wrap_with_images($values, 'locations_website'); $locationMod['subModels'][] = array('fkId' => 'location_id', 'model' => $lw); } return $locationMod; } } else { if (isset($values['location:deleted'])) { $locationMod = submission_builder::wrap_with_images($values, 'location'); $locationMod['subModels'] = array(array('fkId' => 'location_id', 'model' => $sampleMod)); return $locationMod; } } return $sampleMod; }
/** * 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) { if (isset($values['sample:recorder_names'])) { if (is_array($values['sample:recorder_names'])) { $values['sample:recorder_names'] = implode("\r\n", $values['sample:recorder_names']); } } // else just load the string if (isset($values['gridmode'])) { $occurrences = data_entry_helper::wrap_species_checklist($values); } else { $occurrences = submission_builder::wrap_with_images($values, 'occurrence'); } // when a non admin selects an existing location they can not modify it or its attributes and the location record does not form part of the submission if (isset($values['location:name'])) { $sampleMod = submission_builder::wrap_with_images($values, 'sample'); if (count($occurrences) > 0) { $sampleMod['subModels'] = $occurrences; } $locationMod = submission_builder::wrap_with_images($values, 'location'); $locationMod['subModels'] = array(array('fkId' => 'location_id', 'model' => $sampleMod)); if (array_key_exists('locations_website:website_id', $_POST)) { $lw = submission_builder::wrap_with_images($values, 'locations_website'); $locationMod['subModels'][] = array('fkId' => 'location_id', 'model' => $lw); } return $locationMod; } $values['sample:location_id'] = $values['location:id']; $sampleMod = submission_builder::wrap_with_images($values, 'sample'); if (count($occurrences) > 0) { $sampleMod['subModels'] = $occurrences; } return $sampleMod; }
/** * 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) { if (isset($values['source'])) { // comes from main Sites tab, Admins may create so need to check for locations_website entry $locModel = submission_builder::wrap_with_images($values, 'location'); if (isset($values['locations_website:website_id'])) { // assume no other submodels $locModel['subModels'] = array(array('fkId' => 'location_id', 'model' => array('id' => 'locations_website', 'fields' => array('website_id' => array('value' => $values['locations_website:website_id']))))); } return $locModel; } if (isset($values['sample:recorder_names'])) { if (is_array($values['sample:recorder_names'])) { $values['sample:recorder_names'] = implode("\r\n", $values['sample:recorder_names']); } } // else just load the string $sampleMod = data_entry_helper::wrap_with_attrs($values, 'sample'); if (isset($values['sample:deleted'])) { return $sampleMod; } $subsamples = array(); $locations = array(); foreach ($values as $key => $value) { $parts = explode(':', $key, 4); // $cloneprefix='CG:--rownum--:--sampleid--:smpAttr:--attr_id--[:--attr_value_id--] // $cloneprefix='CG:--rownum--:--sampleid--:smpAttr:--attr_id--\[\] for a multivalue checkbox group, with an array value if ($parts[0] == 'CG' && count($parts) > 1 && $parts[1] != '--rownum--' && $parts[1] != '') { $field = explode(':', $parts[3]); if ($field[0] == 'location') { $locations[$parts[1]][$field[1]] = array('value' => $value); $locations[$parts[1]]['website_id'] = array('value' => $values['website_id']); $locations[$parts[1]]['survey_id'] = array('value' => $values['survey_id']); } else { if ($parts[2] != "--sampleid--" && $parts[2] != "") { $subsamples[$parts[1]]['id'] = array('value' => $parts[2]); } if (is_array($value)) { for ($i = count($value) - 1; $i >= 0; $i--) { // reverse order so we can unset array members. $tokens = explode(':', $value[$i], 5); // need to discard the CG prefix if (count($tokens) > 1) { unset($value[$i]); $subsamples[$parts[1]][$tokens[4]] = array('value' => $tokens[0]); } } if (count($value) > 0) { // sweep up any new ones. $subsamples[$parts[1]][$parts[3]] = array('value' => $value); } } else { $subsamples[$parts[1]][$parts[3]] = array('value' => $value); } $subsamples[$parts[1]]['website_id'] = array('value' => $values['website_id']); $subsamples[$parts[1]]['survey_id'] = array('value' => $values['survey_id']); } } } // locations and subsamples arrays are indexed by cgrownum. // next create an array of subsample models with their occurrences attached. $subsamples2 = array(); $newsgrowContents = array(); // array indexed on sgrownum of subarrays of samples which have new occurrences ksort($subsamples); // by cgrownum foreach ($subsamples as $sampleIndex => $subsampleFields) { if (isset($subsampleFields['date']) && $subsampleFields['date']['value'] != "") { $subsampleFields['location_name'] = $subsampleFields['name']; $subsample = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => $subsampleFields)); if (isset($locations[$sampleIndex])) { $locweb = array('fkId' => 'location_id', 'model' => array('id' => 'locations_website', 'fields' => array('website_id' => array('value' => $values['website_id'])))); $locations[$sampleIndex]['name'] = $subsampleFields['name']; $locations[$sampleIndex]['parent_id'] = array('value' => $values['sample:location_id']); $locations[$sampleIndex]['centroid_sref_system'] = array('value' => $values['location:centroid_sref_system']); $subsample['model']['superModels'] = array(array('fkId' => 'location_id', 'model' => array('id' => 'location', 'fields' => $locations[$sampleIndex], 'subModels' => array($locweb)))); } $occs = array(); foreach ($values as $key => $value) { $parts = explode(':', $key, 7); // SG:--sgrownum--:--cgrownum--:--sampleid--:--ttlid--:--occid--:occAttr:--attr_id--[:--attr_value_id--] if ($parts[0] == 'SG' && count($parts) > 1 && $parts[4] != '--ttlid--' && $parts[4] != '' && $parts[2] == $sampleIndex && ($parts[5] != "--occid--" && $parts[5] != "" || $value != "")) { $occ = array('fkId' => 'sample_id', 'model' => array('id' => 'occurrence', 'fields' => array('taxa_taxon_list_id' => array('value' => $parts[4]), 'website_id' => array('value' => $values['website_id']), 'survey_id' => array('value' => $values['survey_id']), $parts[6] => array('value' => $value)))); if ($value == "") { $occ['model']['fields']['deleted'] = array('value' => 't'); unset($occ['model']['fields'][$parts[6]]); } if ($parts[5] != "--occid--" && $parts[5] != "") { $occ['model']['fields']['id'] = array('value' => $parts[5]); } else { if (!isset($newsgrowContents[intval($parts[1])])) { $newsgrowContents[intval($parts[1])] = array(); } $newsgrowContents[intval($parts[1])][] = $sampleIndex; } $occs[] = $occ; } } if (count($occs) > 0) { $subsample['model']['subModels'] = $occs; } $subsamples2[$sampleIndex] = $subsample; } else { if (isset($subsampleFields['id']) && $subsampleFields['id']['value'] != "") { $subsample = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array('id' => $subsampleFields['id'], 'deleted' => array('value' => 't'), 'website_id' => $subsampleFields['website_id'], 'survey_id' => $subsampleFields['survey_id']))); $subsamples2[$sampleIndex] = $subsample; } } } // finally create an unindexed array of subsamples, in the order of sgrownum.... // don't care about order of already saved occurrences, only new ones: in fact old ones may mess things up! // The order in which we need to save the occurrences is how they are displayed in the form. This allows us to recreate the form later on. $subsamples3 = array(); // don't want this indexed $subsampleslist = array(); // but need a list of those included so far! // new sgrowContents just has a list of cgrownums for that sgrow ksort($newsgrowContents); foreach ($newsgrowContents as $sgrownum => $list) { // this is done in sgrownum order. foreach ($list as $cgrow) { if (!isset($subsampleslist[$cgrow])) { $subsampleslist[$cgrow] = true; $subsamples3[] = $subsamples2[$cgrow]; } } } // now we dump in any other subsamples foreach ($subsamples2 as $cgrownum => $subsample) { if (!isset($subsampleslist[$cgrownum])) { $subsampleslist[$cgrownum] = true; $subsamples3[] = $subsample; } } if (count($subsamples3) > 0) { $sampleMod['subModels'] = $subsamples3; } return $sampleMod; }
/** * 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) { if (isset($values['source'])) { // comes from main Sites tab, Admins may create so need to check for locations_website entry $locModel = submission_builder::wrap_with_images($values, 'location'); if (isset($values['locations_website:website_id'])) { // assume no other submodels $locModel['subModels'] = array(array('fkId' => 'location_id', 'model' => array('id' => 'locations_website', 'fields' => array('website_id' => array('value' => $values['locations_website:website_id']))))); } return $locModel; } if (isset($values['sample:recorder_names'])) { if (is_array($values['sample:recorder_names'])) { $values['sample:recorder_names'] = implode("\r\n", $values['sample:recorder_names']); } } // else just load the string $sampleMod = submission_builder::wrap_with_images($values, 'sample'); if (!isset($values['sample:deleted'])) { if (isset($values['gridmode'])) { $occurrences = data_entry_helper::wrap_species_checklist($values); } else { $occurrences = submission_builder::wrap_with_images($values, 'occurrence'); } // when a non admin selects an existing location they can not modify it or its attributes and the location record does not form part of the submission if (isset($values['location:location_type_id'])) { if (count($occurrences) > 0) { $sampleMod['subModels'] = $occurrences; } $locationMod = submission_builder::wrap_with_images($values, 'location'); $locationMod['subModels'] = array(array('fkId' => 'location_id', 'model' => $sampleMod)); if (array_key_exists('locations_website:website_id', $_POST)) { $lw = submission_builder::wrap_with_images($values, 'locations_website'); $locationMod['subModels'][] = array('fkId' => 'location_id', 'model' => $lw); } return $locationMod; } // $values['sample:location_id'] = $values['location:id']; if (count($occurrences) > 0) { $sampleMod['subModels'] = $occurrences; } } return $sampleMod; }
/** * Builds the special submission format required for seasearch data * @param array $values * @param array $args * @return array * @throws \Exception */ public static function get_submission($values, $args) { // First, build an array of the field values for each habitat sample. $habitatSamples = array(); foreach ($values as $key => $value) { if (substr_count($key, ':') === 3) { $parts = explode(':', $key, 4); if ($parts[0] === 'smpAttr') { // If the last part of a habitat field is not a number, this is just a dummy field used // when cloning habitat controls. if (preg_match('/^\\d+$/', $parts[3])) { // habitat number is the last part of the attribute field name $id = array_pop($parts); if (!isset($habitatSamples["habitat{$id}"])) { $habitatSamples["habitat{$id}"] = array(); } // Skip empty new attribute values. if (!empty($parts[2]) || !empty($value)) { // remove empty stuff from the attribute name (e.g. an unused space for the existing value ID, if a new attribute value). while (empty($parts[count($parts) - 1])) { array_pop($parts); } $fieldname = implode(':', $parts); $habitatSamples["habitat{$id}"][$fieldname] = $value; } } unset($values[$key]); } } // Also check for habitat photos, attached to a fake table name indicating the habitat index if (preg_match('/^sample_medium(?P<id>\\d)/', $key, $matches)) { $parts = explode(':', $key); if (!isset($habitatSamples["habitat{$matches['id']}"])) { $habitatSamples["habitat{$matches['id']}"] = array(); } $parts[0] = 'sample_medium'; $habitatSamples["habitat{$matches['id']}"][implode(':', $parts)] = $value; } $parts = explode(':', $key); // For the group_name, the autocomplete functionality needs to be removed from the submission. // So copy the edit box into the underlying field which gets posted. if (array_pop($parts) === 'group_name') { $values[implode(':', $parts)] = $value; } } // Find the attribute which contains SACFORP habitat data. $readAuth = data_entry_helper::get_read_auth(variable_get('indicia_website_id'), variable_get('indicia_password')); $attributeOpts = array('valuetable' => 'occurrence_attribute_value', 'attrtable' => 'occurrence_attribute', 'key' => 'occurrence_id', 'fieldprefix' => 'occAttr', 'extraParams' => $readAuth + array('caption' => 'SACFOR/P'), 'survey_id' => $args['survey_id']); $attributes = data_entry_helper::getAttributes($attributeOpts, false); $values['habitat-count'] = count($habitatSamples); $buddyPairSubmission = submission_builder::wrap_with_images($values, 'sample'); unset($buddyPairSubmission['fields']['habitat-count']); // Get the list of records implied by the SACFOR data for each habitat. At this point we'll create 1 big list and split // it across the habitats later. $occurrences = data_entry_helper::wrap_species_checklist($values, true, array(), array()); // now work out which habitat contains which occurrence $habitatOccurrences = array(); foreach (array_keys($habitatSamples) as $habitatId) { $habitatOccurrences[$habitatId] = array(); } foreach ($occurrences as $uniqueId => $occurrence) { // for existing occurrences, they are already linked to a sample via the SampleIDX field. if (!empty($occurrence['model']['fields']['sampleIDX'])) { $habitatOccurrences['habitat' . ($occurrence['model']['fields']['sampleIDX']['value'] + 1)][] = $occurrence; } else { // search the posted data for a habitat SACFOR value for the occurrence row foreach (array_keys($habitatSamples) as $habitatId) { $habitatIdx = str_replace('habitat', '', $habitatId); if (!empty($values["{$uniqueId}:habitat-{$habitatIdx}"])) { $thisOccurrence = array_merge($occurrence); $thisOccurrence['model']['fields'][$attributes[0]['fieldname']] = array('value' => $values["{$uniqueId}:habitat-{$habitatIdx}"]); $habitatOccurrences[$habitatId][] = $thisOccurrence; } } } } // now create the submodel data for each habitat. $buddyPairSubmission['subModels'] = array(); // copy the basic sample data into each set of habitat subsample values foreach ($habitatSamples as $habitatId => &$habitatSample) { $habitatIdx = str_replace('habitat', '', $habitatId); $habitatSample['website_id'] = $values['website_id']; $habitatSample['survey_id'] = $values['survey_id']; if (isset($_POST["habitat_sample_id:{$habitatIdx}"])) { $habitatSample['sample:id'] = $_POST["habitat_sample_id:{$habitatIdx}"]; } $habitatSample['sample:date'] = $values['sample:date']; $habitatSample['sample:entered_sref'] = $values['sample:entered_sref']; $habitatSample['sample:entered_sref_system'] = $values['sample:entered_sref_system']; $habitatSample['sample:input_form'] = $values['sample:input_form']; if (isset($_POST["sample:comment:{$habitatIdx}"])) { $habitatSample['sample:comment'] = $_POST["sample:comment:{$habitatIdx}"]; unset($buddyPairSubmission['fields']["comment:{$habitatIdx}"]); } $habitatSubmission = submission_builder::wrap_with_images($habitatSample, 'sample'); if (!isset($habitatSubmission['subModels'])) { $habitatSubmission['subModels'] = array(); } $habitatSubmission['subModels'] += $habitatOccurrences[$habitatId]; $buddyPairSubmission['subModels'][] = array('fkId' => 'parent_id', 'model' => $habitatSubmission); } return $buddyPairSubmission; }
/** * Helper function to simplify building of a submission that contains a single sample * and multiple occurrences records generated by a species_checklist control. * * @param array $values List of the posted values to create the submission from. * @param boolean $include_if_any_data If true, then any list entry which has any data * set will be included in the submission. Set this to true when hiding the select checkbox * in the grid. * @return array Sample submission array */ public static function build_sample_occurrences_list_submission($values, $include_if_any_data = false) { // We're mainly submitting to the sample model $sampleMod = submission_builder::wrap_with_images($values, 'sample'); $occurrences = data_entry_helper::wrap_species_checklist($values, $include_if_any_data); // Add the occurrences in as subModels $sampleMod['subModels'] = $occurrences; return $sampleMod; }
/** * 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) { $fromDate = self::getStartDate($args); $values['sample:date_start'] = date('Y-m-d', $fromDate); $dateEnd = strtotime('+' . ($args['weeks'] * 7 - 1) . ' days', $fromDate); // force max date to today to pass validation. if ($dateEnd > time()) { $dateEnd = time(); } $values['sample:date_end'] = date('Y-m-d', $dateEnd); $values['sample:date_type'] = 'DD'; $weekData = array(); $countValues = json_decode($values['table-data']); // existing samples being posted? $samplesDates = array(); if (!empty($values['samples-dates'])) { $samplesDates = json_decode($values['samples-dates'], true); } unset($values['table-data']); unset($values['samples-dates']); $parentSample = submission_builder::wrap_with_images($values, 'sample'); foreach ($countValues as $key => $value) { $tokens = explode(':', $key); // consider existing values, or filled in values only if (($value !== '' || count($tokens) === 6) && preg_match('/^sc([0-9]+):/', $key, $matches)) { $weekIdx = $matches[1]; if (!isset($weekData["week{$weekIdx}"])) { $weekData["week{$weekIdx}"] = array(); } $datelessKey = preg_replace('/^sc([0-9]+):/', 'sc:', $key); $weekData["week{$weekIdx}"][$datelessKey] = $value; $presenceKey = preg_replace('/occAttr:[0-9]+(:[0-9]+)?$/', 'present', $datelessKey); $weekData["week{$weekIdx}"][$presenceKey] = $tokens[1]; } } $parentSample['subModels'] = array(); // retrieve any sample data for each week $weekSampleData = array(); foreach ($values as $key => $value) { if (preg_match('/^smpAttr(\\d+):(.+)/', $key, $matches)) { if (!isset($weekSampleData["week{$matches['1']}"])) { $weekSampleData["week{$matches['1']}"] = array(); } $weekSampleData["week{$matches['1']}"]["smpAttr:{$matches['2']}"] = $value; } } foreach ($weekData as $week => $data) { $weekno = substr($week, 4); $weekstart = strtotime('+' . $weekno . ' weeks', $fromDate); if (isset($samplesDates[$weekstart])) { $data['sample:id'] = $samplesDates[$weekstart]; } $data['sample:date_start'] = date('Y-m-d', $weekstart); $data['sample:date_end'] = date('Y-m-d', strtotime('+6 days', $weekstart)); $data['sample:date_type'] = 'DD'; $data['website_id'] = $values['website_id']; $data['survey_id'] = $values['survey_id']; $data['entered_sref'] = $values['sample:entered_sref']; $data['entered_sref_system'] = $values['sample:entered_sref_system']; $data['geom'] = $values['sample:geom']; if (isset($weekSampleData["week{$weekno}"])) { $data = array_merge($data, $weekSampleData["week{$weekno}"]); } $subSampleAndOccs = data_entry_helper::build_sample_occurrences_list_submission($data); $parentSample['subModels'][] = array('fkId' => 'parent_id', 'model' => $subSampleAndOccs); } return $parentSample; }
public static function get_splash_subsamples_occurrences_submission($args, $values, $include_if_any_data = false, $zero_attrs = false, $zero_values = array('0', 'None', 'Absent')) { $sampleMod = submission_builder::wrap_with_images($values, 'sample'); $subModels = self::create_splash_subsample_occurrence_structure($values, $include_if_any_data, $zero_attrs, $zero_values, $args); // Add the subsamples/occurrences in as subModels without overwriting others such as a sample image if (array_key_exists('subModels', $sampleMod)) { $sampleMod['subModels'] = array_merge($sampleMod['subModels'], $subModels); } else { $sampleMod['subModels'] = $subModels; } return $sampleMod; }