/** * Return the generated form output. * @param array $args List of parameter values passed through to the form depending on how the form has been configured. * This array always contains a value for language. * @param object $node The Drupal node object. * @param array $response When this form is reloading after saving a submission, contains the response from the service call. * Note this does not apply when redirecting (in this case the details of the saved object are in the $_GET data). * @return Form HTML. */ public static function get_form($args, $node, $response = null) { if (!($user_id = hostsite_get_user_field('indicia_user_id'))) { return self::abort('Please ensure that you\'ve filled in your surname on your user profile before leaving a group.', $args); } if (empty($_GET['group_id'])) { return self::abort('This form must be called with a group_id in the URL parameters.', $args); } $r = ''; $auth = data_entry_helper::get_read_write_auth($args['website_id'], $args['password']); $group = data_entry_helper::get_population_data(array('table' => 'group', 'extraParams' => $auth['read'] + array('id' => $_GET['group_id']), 'nocache' => true)); if (count($group) !== 1) { return self::abort('The group you\'ve requested membership of does not exist.', $args); } iform_load_helpers(array('submission_builder')); $group = $group[0]; // Check for an existing group user record $existing = data_entry_helper::get_population_data(array('table' => 'groups_user', 'extraParams' => $auth['read'] + array('group_id' => $_GET['group_id'], 'user_id' => $user_id), 'nocache' => true)); if (count($existing) !== 1) { return self::abort('You are not a member of this group.', $args); } if (!empty($_POST['response']) && $_POST['response'] === lang::get('Cancel')) { drupal_goto($args['groups_page_path']); } elseif (!empty($_POST['response']) && $_POST['response'] === lang::get('Confirm')) { $data = array('groups_user:id' => $existing[0]['id'], 'groups_user:group_id' => $group['id'], 'groups_user:user_id' => $user_id, 'deleted' => 't'); $wrap = submission_builder::wrap($data, 'groups_user'); $response = data_entry_helper::forward_post_to('groups_user', $wrap, $auth['write_tokens']); if (isset($response['success'])) { hostsite_show_message("You are no longer participating in {$group['title']}!"); drupal_goto($args['groups_page_path']); } else { return self::abort('An error occurred whilst trying to update your group membership.'); } } else { // First access of the form. Let's get confirmation $reload = data_entry_helper::get_reload_link_parts(); $reloadpath = $reload['path'] . '?' . data_entry_helper::array_to_query_string($reload['params']); $r = '<form action="' . $reloadpath . '" method="POST"><fieldset>'; $r .= '<legend>' . lang::get('Confirmation') . '</legend>'; $r .= '<input type="hidden" name="leave" value="1" />'; $r .= '<p>' . lang::get('Are you sure you want to stop participating in {1}?', $group['title']) . '</p>'; $r .= '<input type="submit" value="' . lang::get('Confirm') . '" name="response" />'; $r .= '<input type="submit" value="' . lang::get('Cancel') . '" name="response" />'; $r .= '</fieldset></form>'; } return $r; }
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; }
/** * Return the generated form output. * @param array $args List of parameter values passed through to the form depending on how the form has been configured. * This array always contains a value for language. * @param object $node The Drupal node object. * @param array $response When this form is reloading after saving a submission, contains the response from the service call. * Note this does not apply when redirecting (in this case the details of the saved object are in the $_GET data). * @return Form HTML. */ public static function get_form($args, $node, $response = null) { if (!($user_id = hostsite_get_user_field('indicia_user_id'))) { return self::abort('Please ensure that you\'ve filled in your surname on your user profile before joining a group.', $args); } if (empty($_GET['group_id'])) { return self::abort('This form must be called with a group_id in the URL parameters.', $args); } $r = ''; $auth = data_entry_helper::get_read_write_auth($args['website_id'], $args['password']); $group = data_entry_helper::get_population_data(array('table' => 'group', 'extraParams' => $auth['read'] + array('id' => $_GET['group_id']), 'nocache' => true)); if (count($group) !== 1) { return self::abort('The group you\'ve requested membership of does not exist.', $args); } iform_load_helpers(array('submission_builder')); $group = $group[0]; // Check for an existing group user record $existing = data_entry_helper::get_population_data(array('table' => 'groups_user', 'extraParams' => $auth['read'] + array('group_id' => $_GET['group_id'], 'user_id' => $user_id), 'nocache' => true)); if (count($existing)) { if ($existing[0]['pending'] === 'true') { // if a previous request was made and unapproved when the group was request only, but the group is now public, we can approve their existing // groups_user record. if ($group['joining_method'] === 'P') { $data = array('groups_user:id' => $existing[0]['id'], 'groups_user:pending' => 'f'); $wrap = submission_builder::wrap($data, 'groups_user'); $r = data_entry_helper::forward_post_to('groups_user', $wrap, $auth['write_tokens']); return self::success($auth, $group, $args); } else { return self::abort("You've already got a membership request for {$group['title']} pending approval.", $args); } } else { return self::abort("You're already a member of {$group['title']}.", $args); } } else { $data = array('groups_user:group_id' => $group['id'], 'groups_user:user_id' => $user_id); // request only, so make the groups_user record pending approval if ($group['joining_method'] === 'R') { $data['groups_user:pending'] = 't'; } $wrap = submission_builder::wrap($data, 'groups_user'); $r = data_entry_helper::forward_post_to('groups_user', $wrap, $auth['write_tokens']); if (!isset($r['success'])) { return self::abort('An error occurred whilst trying to update your group membership.', $args); } elseif ($group['joining_method'] === 'R') { return self::abort("Your request to join {$group['title']} is now awaiting approval.", $args); } else { return self::success($auth, $group, $args); } } return $r; }
/** * Wraps an array (e.g. Post or Session data generated by a form) into a structure * suitable for submission. * <p>The attributes in the array are all included, unless they * named using the form entity:attribute (e.g. sample:date) in which case they are * only included if wrapping the matching entity. This allows the content of the wrap * to be limited to only the appropriate information.</p> * <p>Do not prefix the survey_id or website_id attributes being posted with an entity * name as these IDs are used by Indicia for all entities.</p> * * @param array $array Array of data generated from data entry controls. * @param string $entity Name of the entity to wrap data for. */ public static function wrap($array, $entity) { return submission_builder::wrap($array, $entity); }
/** * Extracts the sub-models required to populate member and administrator info from the form data. */ private static function extractUserInfoFromFormValues(&$s, $values, $fieldname, $isAdmin) { $count = 0; if (!empty($values["groups_user:{$fieldname}"])) { if (!isset($s['subModels'])) { $s['subModels'] = array(); } if (!empty($values["groups_user:{$fieldname}"])) { foreach ($values["groups_user:{$fieldname}"] as $userId) { if ($userId) { $values = array('user_id' => $userId, 'administrator' => $isAdmin); $s['subModels'][] = array('fkId' => 'group_id', 'model' => submission_builder::wrap($values, 'groups_user')); $count++; } } } } return $count; }
private static function extractUserInfoFromFormValues(&$s, $values, $fieldname, $isAdmin) { $existingAdmins = preg_grep("/^groups_user\\:{$fieldname}\\:[0-9]+\$/", array_keys($values)); if (!empty($values["groups_user:{$fieldname}"]) || !empty($existingAdmins)) { if (!isset($s['subModels'])) { $s['subModels'] = array(); } if (!empty($values["groups_user:{$fieldname}"])) { foreach ($values["groups_user:{$fieldname}"] as $userId) { $s['subModels'][] = array('fkId' => 'group_id', 'model' => submission_builder::wrap(array('user_id' => $userId, 'administrator' => $isAdmin), 'groups_user')); } } // for existing, we just need to look for deletions which will have an empty value foreach ($existingAdmins as $admin) { if (empty($values[$admin])) { $id = substr($admin, 20); $s['subModels'][] = array('fkId' => 'group_id', 'model' => submission_builder::wrap(array('id' => $id, 'deleted' => 't'), 'groups_user')); } } } return $s; }