/** * Return the generated form output. * @return Form HTML. */ public static function get_form($args, $node) { define("MODE_GRID", 0); define("MODE_NEW_SAMPLE", 1); define("MODE_EXISTING", 2); self::parse_defaults($args); self::getArgDefaults($args); global $user; $logged_in = $user->uid > 0; self::$node = $node; // Get authorisation tokens to update and read from the Warehouse. $auth = data_entry_helper::get_read_write_auth($args['website_id'], $args['password']); $svcUrl = data_entry_helper::$base_url . '/index.php/services'; self::$auth = $auth; $mode = isset($args['no_grid']) && $args['no_grid'] ? MODE_NEW_SAMPLE : MODE_GRID; // default mode when no grid set to false - display grid of existing data // mode MODE_EXISTING: display existing sample $loadedSampleId = null; $loadedOccurrenceId = null; if ($_POST) { if (!array_key_exists('website_id', $_POST)) { // non Indicia POST, in this case must be the location allocations. add check to ensure we don't corrept the data by accident if (function_exists('iform_loctools_checkaccess') && iform_loctools_checkaccess($node, 'admin') && array_key_exists('mnhnld1', $_POST)) { iform_loctools_deletelocations($node); foreach ($_POST as $key => $value) { $parts = explode(':', $key); iform_loctools_insertlocation($node, $parts[2], $parts[1]); } } } else { if (!is_null(data_entry_helper::$entity_to_load)) { $mode = MODE_EXISTING; // errors with new sample, entity populated with post, so display this data. } } // else valid save, so go back to gridview: default mode 0 } if (array_key_exists('sample_id', $_GET) && $_GET['sample_id'] != '{sample_id}') { $mode = MODE_EXISTING; $loadedSampleId = $_GET['sample_id']; } if (array_key_exists('occurrence_id', $_GET) && $_GET['occurrence_id'] != '{occurrence_id}') { $mode = MODE_EXISTING; $loadedOccurrenceId = $_GET['occurrence_id']; self::$occurrenceIds = array($loadedOccurrenceId); } if ($mode != MODE_EXISTING && array_key_exists('newSample', $_GET)) { $mode = MODE_NEW_SAMPLE; data_entry_helper::$entity_to_load = array(); } // else default to mode MODE_GRID or MODE_NEW_SAMPLE depending on no_grid parameter self::$mode = $mode; // default mode MODE_GRID : display grid of the samples to add a new one // or edit an existing one. if ($mode == MODE_GRID) { $r = ''; $attributes = data_entry_helper::getAttributes(array('valuetable' => 'sample_attribute_value', 'attrtable' => 'sample_attribute', 'key' => 'sample_id', 'fieldprefix' => 'smpAttr', 'extraParams' => $auth['read'], 'survey_id' => $args['survey_id']), false); $tabs = array('#sampleList' => lang::get('LANG_Main_Samples_Tab')); if ($args['includeLocTools'] && function_exists('iform_loctools_checkaccess') && iform_loctools_checkaccess($node, 'admin')) { $tabs['#setLocations'] = lang::get('LANG_Allocate_Locations'); } if (method_exists(get_called_class(), 'getExtraGridModeTabs')) { $extraTabs = call_user_func(array(get_called_class(), 'getExtraGridModeTabs'), false, $auth['read'], $args, $attributes); if (is_array($extraTabs)) { $tabs = $tabs + $extraTabs; } } if (count($tabs) > 1) { $r .= "<div id=\"controls\">" . data_entry_helper::enable_tabs(array('divId' => 'controls', 'active' => '#sampleList')) . "<div id=\"temp\"></div>"; $r .= data_entry_helper::tab_header(array('tabs' => $tabs)); } $r .= "<div id=\"sampleList\">" . call_user_func(array(get_called_class(), 'getSampleListGrid'), $args, $node, $auth, $attributes) . "</div>"; if ($args['includeLocTools'] && function_exists('iform_loctools_checkaccess') && iform_loctools_checkaccess($node, 'admin')) { $r .= ' <div id="setLocations"> <form method="post"> <input type="hidden" id="mnhnld1" name="mnhnld1" value="mnhnld1" /><table border="1"><tr><td></td>'; $url = $svcUrl . '/data/location?mode=json&view=detail&auth_token=' . $auth['read']['auth_token'] . "&nonce=" . $auth['read']["nonce"] . "&parent_id=NULL&orderby=name"; $session = curl_init($url); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); $entities = json_decode(curl_exec($session), true); $userlist = iform_loctools_listusers($node); foreach ($userlist as $uid => $a_user) { $r .= '<td>' . $a_user->name . '</td>'; } $r .= "</tr>"; if (!empty($entities)) { foreach ($entities as $entity) { if (!$entity["parent_id"]) { // only assign parent locations. $r .= "<tr><td>" . $entity["name"] . "</td>"; $defaultuserids = iform_loctools_getusers($node, $entity["id"]); foreach ($userlist as $uid => $a_user) { $r .= '<td><input type="checkbox" name="location:' . $entity["id"] . ':' . $uid . (in_array($uid, $defaultuserids) ? '" checked="checked"' : '"') . '></td>'; } $r .= "</tr>"; } } } $r .= "</table>\n <input type=\"submit\" class=\"ui-state-default ui-corner-all\" value=\"" . lang::get('LANG_Save_Location_Allocations') . "\" />\n </form>\n </div>"; } if (method_exists(get_called_class(), 'getExtraGridModeTabs')) { $r .= call_user_func(array(get_called_class(), 'getExtraGridModeTabs'), true, $auth['read'], $args, $attributes); } if (count($tabs) > 1) { // close tabs div if present $r .= "</div>"; } return $r; } if ($mode == MODE_EXISTING && is_null(data_entry_helper::$entity_to_load)) { // only load if not in error situation data_entry_helper::$entity_to_load = array(); // Displaying an existing sample. If we know the occurrence ID, and don't know the sample ID or are displaying just one occurrence // rather than a grid of occurrences then we must load the occurrence data to get the sample id. if ($loadedOccurrenceId && (!$loadedSampleId || !self::getGridMode($args))) { data_entry_helper::load_existing_record($auth['read'], 'occurrence', $loadedOccurrenceId); // Get the sample ID for the occurrence. This overwrites it if supply in GET but did not match the occurrence's sample $loadedSampleId = data_entry_helper::$entity_to_load['occurrence:sample_id']; if (self::getGridMode($args)) { // in grid mode, we only needed to load the occurrence to find out the sample id. data_entry_helper::$entity_to_load = array(); } } if ($loadedSampleId) { data_entry_helper::load_existing_record($auth['read'], 'sample', $loadedSampleId); } } // attributes must be fetched after the entity to load is filled in - this is because the id gets filled in then! $attrOpts = array('id' => data_entry_helper::$entity_to_load['sample:id'], 'valuetable' => 'sample_attribute_value', 'attrtable' => 'sample_attribute', 'key' => 'sample_id', 'fieldprefix' => 'smpAttr', 'extraParams' => $auth['read'], 'survey_id' => $args['survey_id']); // select only the custom attributes that are for this sample method or all sample methods, if this // form is for a specific sample method. if (!empty($args['sample_method_id'])) { $attrOpts['sample_method_id'] = $args['sample_method_id']; } $attributes = data_entry_helper::getAttributes($attrOpts, false); //// Make sure the form action points back to this page $reload = data_entry_helper::get_reload_link_parts(); unset($reload['params']['sample_id']); unset($reload['params']['occurrence_id']); unset($reload['params']['newSample']); $reloadPath = $reload['path']; if (count($reload['params'])) { $reloadPath .= '?' . http_build_query($reload['params']); } $r = "<form method=\"post\" id=\"entry_form\" action=\"{$reloadPath}\">\n"; // Get authorisation tokens to update the Warehouse, plus any other hidden data. $hiddens = $auth['write'] . "<input type=\"hidden\" id=\"website_id\" name=\"website_id\" value=\"" . $args['website_id'] . "\" />\n" . "<input type=\"hidden\" id=\"survey_id\" name=\"survey_id\" value=\"" . $args['survey_id'] . "\" />\n"; if (!empty($args['sample_method_id'])) { $hiddens .= '<input type="hidden" name="sample:sample_method_id" value="' . $args['sample_method_id'] . '"/>'; } if (isset(data_entry_helper::$entity_to_load['sample:id'])) { $hiddens .= "<input type=\"hidden\" id=\"sample:id\" name=\"sample:id\" value=\"" . data_entry_helper::$entity_to_load['sample:id'] . "\" />\n"; } if (isset(data_entry_helper::$entity_to_load['occurrence:id'])) { $hiddens .= "<input type=\"hidden\" id=\"occurrence:id\" name=\"occurrence:id\" value=\"" . data_entry_helper::$entity_to_load['occurrence:id'] . "\" />\n"; } // Check if Record Status is included as a control. If not, then add it as a hidden. $arr = explode("\r\n", $args['structure']); if (!in_array('[record status]', $arr)) { $value = isset($args['defaults']['occurrence:record_status']) ? $args['defaults']['occurrence:record_status'] : 'C'; $hiddens .= "<input type=\"hidden\" id=\"occurrence:record_status\" name=\"occurrence:record_status\" value=\"{$value}\" />\n"; } // request automatic JS validation if (!isset($args['clientSideValidation']) || $args['clientSideValidation']) { data_entry_helper::enable_validation('entry_form'); } // If logged in, output some hidden data about the user if (isset($args['copyFromProfile']) && $args['copyFromProfile'] == true) { self::profile_load_all_profile($user); } foreach ($attributes as &$attribute) { $attrPropName = 'profile_' . strtolower(str_replace(' ', '_', $attribute['caption'])); if (isset($args['copyFromProfile']) && $args['copyFromProfile'] == true && isset($user->{$attrPropName})) { if ($args['nameShow'] == true) { $attribute['default'] = $user->{$attrPropName}; } else { // profile attributes are not displayed as the user is logged in $attribute['handled'] = true; $attribute['value'] = $user->{$attrPropName}; } } elseif (strcasecmp($attribute['caption'], 'cms user id') == 0) { if ($logged_in) { $attribute['value'] = $user->uid; } $attribute['handled'] = true; // user id attribute is never displayed } elseif (strcasecmp($attribute['caption'], 'cms username') == 0) { if ($logged_in) { $attribute['value'] = $user->name; } $attribute['handled'] = true; // username attribute is never displayed } elseif (strcasecmp($attribute['caption'], 'email') == 0) { if ($logged_in) { if ($args['emailShow'] != true) { // email attribute is not displayed $attribute['value'] = $user->mail; $attribute['handled'] = true; } else { $attribute['default'] = $user->mail; } } } elseif ((strcasecmp($attribute['caption'], 'first name') == 0 || strcasecmp($attribute['caption'], 'last name') == 0 || strcasecmp($attribute['caption'], 'surname') == 0) && $logged_in) { if ($args['nameShow'] != true) { // name attributes are not displayed because we have the users login $attribute['handled'] = true; } } // If we have a value for one of the user login attributes then we need to output this value. BUT, for existing data // we must not overwrite the user who created the record. if (isset($attribute['value']) && $mode != MODE_EXISTING) { $hiddens .= '<input type="hidden" name="' . $attribute['fieldname'] . '" value="' . $attribute['value'] . '" />' . "\n"; } } $customAttributeTabs = get_attribute_tabs($attributes); $tabs = self::get_all_tabs($args['structure'], $customAttributeTabs); $r .= "<div id=\"controls\">\n"; // Build a list of the tabs that actually have content $tabHtml = self::get_tab_html($tabs, $auth, $args, $attributes, $hiddens); // Output the dynamic tab headers if ($args['interface'] != 'one_page') { $headerOptions = array('tabs' => array()); foreach ($tabHtml as $tab => $tabContent) { $alias = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($tab)); $tabtitle = lang::get("LANG_Tab_{$alias}"); if ($tabtitle == "LANG_Tab_{$alias}") { // if no translation provided, we'll just use the standard heading $tabtitle = $tab; } $headerOptions['tabs']['#' . $alias] = $tabtitle; } $r .= data_entry_helper::tab_header($headerOptions); data_entry_helper::enable_tabs(array('divId' => 'controls', 'style' => $args['interface'], 'progressBar' => isset($args['tabProgress']) && $args['tabProgress'] == true)); } // Output the dynamic tab content $pageIdx = 0; foreach ($tabHtml as $tab => $tabContent) { // get a machine readable alias for the heading $tabalias = preg_replace('/[^a-zA-Z0-9]/', '', strtolower($tab)); $r .= '<div id="' . $tabalias . '">' . "\n"; // For wizard include the tab title as a header. if ($args['interface'] == 'wizard') { $r .= '<h1>' . $headerOptions['tabs']['#' . $tabalias] . '</h1>'; } $r .= $tabContent; // Add any buttons required at the bottom of the tab if ($args['interface'] == 'wizard') { $r .= data_entry_helper::wizard_buttons(array('divId' => 'controls', 'page' => $pageIdx === 0 ? 'first' : ($pageIdx == count($tabHtml) - 1 ? 'last' : 'middle'))); } elseif ($pageIdx == count($tabHtml) - 1 && !($args['interface'] == 'tabs' && $args['save_button_below_all_pages'])) { // last part of a non wizard interface must insert a save button, unless it is tabbed interface with save button beneath all pages $r .= "<input type=\"submit\" class=\"ui-state-default ui-corner-all\" id=\"save-button\" value=\"" . lang::get('LANG_Save') . "\" />\n"; } $pageIdx++; $r .= "</div>\n"; } $r .= "</div>\n"; if ($args['interface'] == 'tabs' && $args['save_button_below_all_pages']) { $r .= "<input type=\"submit\" class=\"ui-state-default ui-corner-all\" id=\"save-button\" value=\"" . lang::get('LANG_Save') . "\" />\n"; } if (!empty(data_entry_helper::$validation_errors)) { $r .= data_entry_helper::dump_remaining_errors(); } $r .= "</form>"; $r .= self::link_species_popups($args); return $r; }