protected function displayAddMenu($mainNode, $existingChildForms, $action)
 {
     // Get the node to add the new add child menu to
     $appendNode = $this->template->getElementById('add_new_childform', $mainNode);
     if (!$appendNode instanceof DOMNode) {
         return false;
     }
     // Append the add_childform.html file to $appendNode
     $addNewChildNode = $this->template->appendFileByNode('swiss_pageformautoview_add_childform.html', 'div', $appendNode);
     if (!$addNewChildNode instanceof DOMNode) {
         return false;
     }
     // Get the child form type selector node
     $childType = $this->template->getElementById('add_childform_type_selector', $addNewChildNode);
     if (!$childType instanceof DOMNode) {
         return false;
     }
     // Add blank default option to the beginning of the child form type selector dropdown list
     $this->template->appendElementByNode($childType, 'option');
     // Get the child form name node
     $childName = $this->template->getElementById('childform_displayname', $addNewChildNode);
     if (!$childName instanceof DOMNode) {
         return false;
     }
     // Get the primary form of the page from the Arguments
     $primaryForm = $this->getParent()->getParent()->getField('primary_form');
     // Get all child forms that can be added to the primary form
     $allChildForms = I2CE_Form::getChildFormsByForm($primaryForm);
     // Boolean flag for whether all available forms already exist
     $allFormsExist = true;
     // Attempt to add each child form type to the dropdown selector
     foreach ($allChildForms as $form) {
         // Boolean flag to check whether a child form already exists for the page
         $formExists = false;
         // Check to see whether each child form already exists for the page
         foreach ($existingChildForms as $existingForm) {
             // If it does, set boolean flag to true
             if ($form == $existingForm) {
                 $formExists = true;
             }
         }
         // Add the child form type if the child form doesn't already exist
         if (!$formExists) {
             $this->template->appendElementByNode($childType, 'option', array('value' => $form), $form);
             // Set allFormsExist flag to false if a form has been added to the dropdown selector
             $allFormsExist = false;
         }
     }
     // If all available child forms already exist for the page, add N/A option
     if ($allFormsExist) {
         $this->template->appendElementByNode($childType, 'option', array(), 'All available forms already exist');
     }
     // Get the list of existing child form names and use it to validate the name
     $existingNames = $this->getChildNames();
     $this->template->setClassValue($childName, 'validate_data', array('notinlist' => $existingNames), '%');
     //Ajax link to add the new child form
     return $this->addAjaxOptionMenu('add_childform', 'child_forms_link', $mainNode);
 }
예제 #2
0
 /**
  * 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;
 }