/** * 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) { $sampleMod = data_entry_helper::wrap_with_attrs($values, 'sample'); if (isset($values['sample:deleted'])) { return $sampleMod; } $subsamples = array(); // first do transect grid for ($i = 0; $i < 5; $i++) { // Fieldname is TG:speciesID:gridX:gridY:GridsampleID:OccID:AttrID for ($j = 0; $j < 5; $j++) { $sa = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array())); if (isset($values['TGS:' . $j * 2 . ':' . $i * 2])) { $sa['model']['fields']['id'] = array('value' => $values['TGS:' . $j * 2 . ':' . $i * 2]); } $sa['model']['fields']['date'] = array('value' => $values['sample:date']); $sa['model']['fields']['location_id'] = array('value' => $values['sample:location_id']); $sa['model']['fields']['location_name'] = array('value' => 'GR ' . $values['sample:location_name'] . ' ' . $j * 2 . $i * 2); $sa['model']['fields']['website_id'] = array('value' => $values['website_id']); $sa['model']['fields']['survey_id'] = array('value' => $values['survey_id']); $suboccs = array(); foreach ($values as $key => $value) { $parts = explode(':', $key); if ($parts[0] == 'TG' && $parts[2] == (string) ($j * 2) && $parts[3] == (string) ($i * 2)) { $occ = array('fkId' => 'sample_id', 'model' => array('id' => 'occurrence', 'fields' => array())); $occ['model']['fields']['taxa_taxon_list_id'] = array('value' => $parts[1]); $occ['model']['fields']['website_id'] = array('value' => $values['website_id']); if ($parts[5] != '-') { $occ['model']['fields']['id'] = array('value' => $parts[5]); } $attrFields = array('occurrence_attribute_id' => $args['qual_dist_attr_id'], 'value' => $value); if ($parts[6] != '-') { $attrFields['id'] = $parts[6]; } $occ['model']['metaFields'] = array('occAttributes' => array('value' => array(array('id' => 'occurrence', 'fields' => $attrFields)))); if ($parts[5] != '-' || $value != $args['ignore_qual_dist_id']) { $suboccs[] = $occ; } } } $sa['model']['subModels'] = $suboccs; if (isset($sa['model']['fields']['id']) || count($suboccs) > 0) { $subsamples[] = $sa; } } } // next do section list for ($i = 1; $i <= $args['max_number_sections']; $i++) { // Fieldname is SL:speciesID:section:SectionsampleID:OccID:AttrID $sa = array('fkId' => 'parent_id', 'model' => array('id' => 'sample', 'fields' => array())); if (isset($values['SLS:' . $i])) { $sa['model']['fields']['id'] = array('value' => $values['SLS:' . $i]); } $sa['model']['fields']['date'] = array('value' => $values['sample:date']); $sa['model']['fields']['location_id'] = array('value' => $values['sample:location_id']); $sa['model']['fields']['location_name'] = array('value' => 'SL ' . $values['sample:location_name'] . ' ' . $i); $sa['model']['fields']['website_id'] = array('value' => $values['website_id']); $sa['model']['fields']['survey_id'] = array('value' => $values['survey_id']); $saattrs = array(); foreach ($values as $key => $value) { $parts = explode(':', $key); if ($parts[0] == 'SLA' && $parts[1] == (string) $i) { // Fieldname is SLA:section:SectionsampleID:AttrValID:AttrID $attr = array("id" => "sample", "fields" => array()); $attr['fields']['sample_attribute_id'] = $parts[4]; if ($parts[3] != '-') { $attr['fields']['id'] = $parts[3]; } $attr['fields']['value'] = $value; if ($parts[3] != '-' || $value != '') { $saattrs[] = $attr; } } } if (count($saattrs) > 0) { $sa['model']['metaFields'] = array('smpAttributes' => array('value' => $saattrs)); } $suboccs = array(); foreach ($values as $key => $value) { $parts = explode(':', $key); if ($parts[0] == 'SL' && $parts[2] == (string) $i) { // Fieldname is SL:speciesID:section:SectionsampleID:OccID:AttrValID $occ = array('fkId' => 'sample_id', 'model' => array('id' => 'occurrence', 'fields' => array('taxa_taxon_list_id' => array('value' => $parts[1]), 'website_id' => array('value' => $values['website_id'])))); if ($parts[4] != '-') { $occ['model']['fields']['id'] = array('value' => $parts[4]); } if ($parts[5] != '-' || $value != '') { $occ['model']['fields']['occAttr:' . $args['quant_dist_attr_id'] . ($parts[5] != '-' ? ':' . $parts[5] : '')] = array('value' => $value); $suboccs[] = $occ; } } } $sa['model']['subModels'] = $suboccs; if (isset($sa['model']['fields']['id']) || count($suboccs) > 0 || count($saattrs) > 0) { $subsamples[] = $sa; } } if (isset($values['TGDEL'])) { if ($values['TGDEL'] != '') { $delList = explode(',', $values['TGDEL']); foreach ($delList as $occID) { $occ = array('fkId' => 'sample_id', 'model' => array('id' => 'occurrence', 'fields' => array())); $occ['model']['fields']['website_id'] = array('value' => $values['website_id']); $occ['model']['fields']['id'] = array('value' => $occID); $occ['model']['fields']['deleted'] = array('value' => 't'); $subsamples[] = $occ; } } } if (isset($values['SLDEL'])) { if ($values['SLDEL'] != '') { $delList = explode(',', $values['SLDEL']); foreach ($delList as $occID) { $occ = array('fkId' => 'sample_id', 'model' => array('id' => 'occurrence', 'fields' => array())); $occ['model']['fields']['website_id'] = array('value' => $values['website_id']); $occ['model']['fields']['id'] = array('value' => $occID); $occ['model']['fields']['deleted'] = array('value' => 't'); $subsamples[] = $occ; } } } if (count($subsamples) > 0) { $sampleMod['subModels'] = $subsamples; } 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; }
/** * 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 = data_entry_helper::wrap_with_attrs($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) { if (isset($values['sample:parent_id'])) { $sampleMod = data_entry_helper::build_sample_occurrence_submission($values); } else { $sampleMod = data_entry_helper::wrap_with_attrs($values, 'sample'); } return $sampleMod; }