/** * Create and load data for the objects used for this form. * * Create the index object and if this is a form submission the load * the data from the $_POST array. */ protected function loadObjects() { $resend = $this->request_exists('resend') && $this->request('resend'); $factory = I2CE_FormFactory::instance(); if ($resend) { if (!($this->requested_user = $factory->createContainer('user_request')) instanceof I2CE_User_Form) { I2Ce::raiseError("Bad load of user_request "); $this->requested_user = false; return false; } if ($this->isPost()) { $this->requested_user->load($this->post, false, false); $msg = "The requested email address \"%1\$s\" is invalid"; I2CE::getConfig()->setIfIsSet($msg, "/modules/RequestAccount-VerifyEmail/user_messages/invalid_email"); $email = $this->requested_user->email; $msg = sprintf($msg, $email); if (!$email) { $this->userMessage($msg); $this->setRedirect($this->pageRoot()); return false; } //see if we can find a user request for this account $where = array('field' => 'email', 'operator' => 'FIELD_LIMIT', 'style' => 'equals', 'data' => array('value' => $email)); $requested_users = I2CE_FormStorage::search('user_request', false, $where); if (count($requested_users) != 1) { $this->userMessage($msg); $this->setRedirect($this->pageRoot()); return false; } $this->requested_user->cleanup(); $this->requested_user = false; $req_id = current($requested_users); if (!($this->requested_user = $factory->createContainer('user_request|' . $req_id)) instanceof I2CE_Form) { $this->userMessage($msg); $this->setRedirect($this->pageRoot()); return false; } $this->requested_user->populate(); } } else { if (!($this->requested_user = $factory->createContainer('user_request')) instanceof I2CE_User_Form || !($usernameField = $this->requested_user->getField('username')) instanceof I2CE_FormField_STRING_LINE || !($emailField = $this->requested_user->getField('email')) instanceof I2CE_FormField_STRING_LINE) { I2Ce::raiseError("Bad load of user_request "); $this->requested_user = false; return false; } if ($this->isPost()) { $post = I2CE_Module_UserRequest::manipulatePostForm($this->post, 'user', 'user_request'); $this->requested_user->load($post, true, false); $req_num = uniqid(); $this->requested_user->getField('request_number')->setValue($req_num); $username = $usernameField->getValue(); $email = $emailField->getValue(); $details = array('email' => $email); if (!I2CE_Module_UserRequest::canAddUser($this->requested_user, false, true)) { $this->requested_user = false; $msg = "The requested username \"%1\$s\" or email address \"%2\$s\" is invalid or already in use"; I2CE::getConfig()->setIfIsSet($msg, "/modules/RequestAccount-VerifyEmail/user_messages/invalid_username"); $msg = sprintf($msg, $username, $email); $this->userMessage($msg); $this->setRedirect($this->pageRoot()); } } } if ($this->requested_user) { $this->setObject($this->requested_user); } return true; }
/** * Migrate a given form from one storage method to the current storage method. * This should only be used when upgrading a module that moved a form storage * from one type to another. * @param string $form_name * @param string $storage The old storage mechanism * @param I2CE_User $user The user object to use to save the new forms. * @param string $migrate_path The full path in MagicData to save the old to new mappings. * @param string $id_field The old field name to use the value of for the new form id. * @param array $skip_fields A list of fields to not migrate to the new form. * @param array $migrate_fields A list of fields that have already been migrated and need to use * the migrate path to convert the data. Format is array( "field" => "map_form" ) * These forms and fields should have already been passed to {@link storeMigrateData} * so the data can be retrieved from there. * @return boolean */ public static function migrateForm($form_name, $storage, $user, $migrate_path = false, $id_field = false, $skip_fields = array(), $migrate_fields = array(), $callback = null) { I2CE::longExecution(); // Make sure that when the new form saves it uses the correct storage type if it was already // cached by a pre upgrade. self::getStorage($form_name, true); $factory = I2CE_FormFactory::instance(); $factory->clearFieldData($form_name); $old_storage = self::getMechanismByStorage($storage); if (!$old_storage instanceof I2CE_FormStorage_Mechanism) { I2CE::raiseError("No storage mechanism found for {$storage}"); return false; } I2CE::raiseError("Migrate form {$form_name} from {$storage} to " . self::getStorage($form_name)); $migrate_data = null; if ($migrate_path) { $migrate_data = I2CE::getConfig()->traverse($migrate_path, true, false); } else { if (count($migrate_fields) > 0) { I2CE::raiseError("Migrate fields have been passed to migrateForm without a migrate_data path in magic data."); return false; } } $use_fields = array(); $obj = $factory->createContainer($form_name, true); if (!$obj instanceof I2CE_Form) { I2Ce::raiseError("Could not instantiate form {$form_name}"); return false; } foreach ($obj as $field => $fieldObj) { if (in_array($field, $skip_fields) || array_key_exists($field, $migrate_fields)) { continue; } $use_fields[] = $field; } $obj->cleanup(); unset($obj); if (count($migrate_fields) > 0 && !$migrate_data->__isset("fields/{$form_name}")) { I2CE::raiseError("No old migrate data set for {$form_name}\n"); } $old_data = $old_storage->listFields($form_name, $use_fields, true); foreach ($old_data as $old_id => $fields) { if ($migrate_data instanceof I2CE_MagicDataNode) { if ($migrate_data->__isset("forms/{$form_name}/{$old_id}")) { I2CE::raiseError("Already migrated {$form_name} {$old_id}. Did something fail earlier? Skipping it."); continue; } } $obj = $factory->createContainer($form_name, true); foreach ($skip_fields as $skip_field) { $obj->removeField($skip_field); } if ($id_field && array_key_exists($id_field, $fields)) { if ($callback === null) { $new_id = $fields[$id_field]; } elseif ($callback === true) { $new_id = strtolower(str_replace(' ', '_', $fields[$id_field])); } else { $new_id = call_user_func($callback, $fields[$id_field]); } $obj->setId($new_id); } foreach ($fields as $key => $value) { if (!isset($value)) { continue; } if ($key == "parent") { $obj->setParent($value); if ($migrate_data instanceof I2CE_MagicDataNode) { $parent_form = $obj->getParentForm(); $parent_id = $obj->getParentID(); $new_parent = $migrate_data->forms->{$parent_form}->{$parent_id}; $obj->setParent($new_parent); //I2CE::raiseError( "Setting parent for $form_name $old_id to $new_parent (was $value)." ); } else { //I2CE::raiseError( "Setting parent for $form_name $old_id to $value." ); } continue; } $fieldObj = $obj->getField($key); if (!$fieldObj instanceof I2CE_FormField) { continue; } $fieldObj->setFromDB($value); } foreach ($migrate_fields as $field => $map_form) { $new_value = self::getMigratedValue($migrate_data, $form_name, $old_id, $field, $map_form); if (is_array($new_value) && array_key_exists('new_value', $new_value) && $new_value['new_value']) { $fieldObj = $obj->getField($field); if (!$fieldObj instanceof I2CE_FormField) { continue; } $fieldObj->setFromDB($new_value['new_value']); } } if ($obj->save($user)) { if ($migrate_data instanceof I2CE_MagicDataNode) { $migrate_data->forms->{$form_name}->{$old_id} = $form_name . "|" . $obj->getId(); } else { I2CE::raiseError("Moved over {$form_name}: {$old_id} to " . $obj->getId()); } } else { return false; } $obj->cleanup(); unset($obj); } if ($migrate_data instanceof I2CE_MagicDataNode) { $child_forms = I2CE_Form::getChildFormsByForm($form_name); foreach ($child_forms as $child_form) { $children = $factory->getRecords($child_form); $bad_ids = array(); foreach ($children as $child_id) { $child_obj = $factory->createContainer($child_form . '|' . $child_id, true); if (!$child_obj instanceof I2CE_Form) { $bad_ids[] = $child_id; continue; } $child_obj->populate(); $old_id = $child_obj->getParentID(); $new_id = $migrate_data->forms->{$form_name}->{$old_id}; $child_obj->setParent($new_id); if ($child_obj->save($user)) { } $child_obj->cleanup(); unset($child_obj); } if (count($bad_ids) > 0) { I2CE::raiseError("Bad Child ids " . implode(',', $bad_ids) . " for child form {$child_form} of form {$form_name}"); } I2CE::raiseError("Migrated parent for {$child_form} to {$form_name}."); } } $migrate_data->unpopulate(true, true); return true; }
/** * Create a check function boolean expression based on the where data. * The function takes on argument which is an array indexed by the field names and with values the value of the field. * @param I2CE_Form $form * @param mixed $expr array or class implementing ArrayAccess, Iterator, and Countable (e.g. MagicDataNode) . the where data. * @param callback $field_refernece_callback. A callback function whose first arguement is the form, the second arguements * is the field and which returns the way the field value should be references as a field. If the callback is null (the default) then * the reference used is $data["$field'] * @returns false on failure a string to which can be evalued as true/false on success. */ public function createCheckFunctionString($formObj, $expr, $field_reference_callback = null) { I2Ce::raiseError("Creaging Check Function string on " . $formObj->getName() . " from:\n" . print_r($expr, true)); if (!(is_array($expr) || $expr instanceof ArrayAccess && $expr instanceof Countable && $expr instanceof Iterator)) { I2CE::raiseError("array was not found while processing the where clause \n"); return false; } if (!isset($expr['operator']) || !is_string($expr['operator'])) { I2CE::raiseError("No operator set"); return false; } switch ($expr['operator']) { case 'FIELD_LIMIT': $subExpr = $this->createCheckLimitString($formObj, $expr, $field_reference_callback); if ($subExpr === false || !is_string($subExpr)) { I2CE::raiseError("Could not generate check function for " . print_R($expr, true)); return false; } $subExpr = trim($subExpr); if (strlen($subExpr) > 0) { $subExpr = ' ( ' . $subExpr . ')'; } return $subExpr; case 'AND': //we are allowing these to be n-ary operators //we are allowing these to be n-ary operators case 'OR': case 'XOR': if (!isset($expr['operand'])) { I2CE::raiseError("No operands set"); return false; } if (!(is_array($expr['operand']) || $expr['operand'] instanceof ArrayAccess && $expr['operand'] instanceof Iterator && $expr['operand'] instanceof Countable)) { I2CE::raiseError("Invalid operands set"); return false; } $subExpr = array(); foreach ($expr['operand'] as $sub) { $tmpExpr = $this->createCheckFunctionString($formObj, $sub, $field_reference_callback); if ($tmpExpr == false || !is_string($tmpExpr)) { I2CE::raiseError("GOT BAD subexpression from: " . print_r($sub, true)); return false; } $tmpExpr = trim($tmpExpr); if (strlen($tmpExpr) > 0) { $subExpr[] = $tmpExpr; } } if (count($subExpr) > 0) { return ' ( ' . implode(' ' . self::$checkOperatorMap[$expr['operator']] . ' ', $subExpr) . ' ) '; } else { return ''; } case 'NOT': if (!isset($expr['operand'])) { I2CE::raiseError("No operands set"); return false; } if (!(is_array($expr['operand']) || $expr['operand'] instanceof ArrayAccess && $expr['operand'] instanceof Iterator && $expr['operand'] instanceof Countable)) { I2CE::raiseError("Invalid operands set"); return false; } if (count($expr['operand']) != 1) { I2CE::raiseError("Expecing one operand but did not receive"); return false; } reset($expr['operand']); $subExpr = $this->createCheckFunctionString($formObj, current($expr['operand']), $field_reference_callback); if ($subExpr === false || !is_string($subExpr)) { I2CE::raiseError("got bad subexpression from :" . print_r(current($expr['operand']), true)); return false; } $subExpr = trim($subExpr); if (strlen($subExpr) > 0) { $subExpr = ' (!( ' . $subExpr . ' )) '; } return $subExpr; default: I2CE::raiseError("Unrecognzied operator " . $expr['operator'] . "\n"); return false; } }