/** * Use method to duplicate a form object. * @param I2CE_Form $form * @param boolean $recurse. Defaults to false. If true we duplicate all children, and {@param $save is forced to be true} * @param boolean $save. Defaults to true. If true we save the form at the same time we duplicate it. * @param string $parentid. Defaults to null in which case the parent id of the duplicated form is not set * @param I2CE_User $user, the user object to save. Defaults to null. * @returns mixed false on error or I2CE_Form, the duplicated form */ public function duplicate($form, $recurse = false, $save = true, $parentid = null, $user = null) { if (!$form instanceof I2CE_Form) { I2CE::raiseError("Not A form"); return false; } $form->populate(); $save |= $recurse; //if we recurse, we must save $ff = I2CE_FormFactory::instance(); $newForm = $ff->createForm($form->getName()); if (!$newForm instanceof I2CE_Form) { I2CE::raiseError("Could create form " . $form->getName() . "to duplicate "); return false; } foreach ($form as $field) { if (!$field instanceof I2CE_FormField) { continue; } $newField = $newForm->getField($field->getName()); if (!$newField instanceof I2CE_FormField) { I2CE::raiseError("Could not duplicate field " . $field->getName() . " of form " . $form->getName()); return false; } $newField->setFromDB($field->getDBValue()); } if ($parentid !== null) { $newForm->setParent($parentid); } if ($save) { if (!$user instanceof I2CE_User) { $user = new I2CE_User(); } $newForm->save($user); if ($recurse) { $child_forms = $form->getChildForms(); $form->populateChildren($child_forms); foreach ($form->getChildren() as $children) { foreach ($children as $childForm) { $childForm->duplicate(true, true, $newForm->getNameID(), $user); } } } } return $newForm; }