Beispiel #1
0
 public static function submit($formID, $objectID = NULL, $importing = FALSE)
 {
     $engine = mfcs::$engine;
     $backgroundProcessing = array();
     if (isnull($objectID)) {
         $newObject = TRUE;
     } else {
         $newObject = FALSE;
     }
     // Get the current Form
     if (($form = self::get($formID)) === FALSE) {
         errorHandle::newError(__METHOD__ . "() - retrieving form by formID", errorHandle::DEBUG);
         return FALSE;
     }
     // the form is an object form, make sure that it has an ID field defined.
     // @TODO this check can probably be removed, its being checked in object class
     if ($form['metadata'] == "0") {
         $idnoInfo = self::getFormIDInfo($formID);
         if ($idnoInfo === FALSE) {
             errorHandle::newError(__METHOD__ . "() - no IDNO field for object form.", errorHandle::DEBUG);
             return FALSE;
         }
     }
     $fields = $form['fields'];
     if (usort($fields, 'sortFieldsByPosition') !== TRUE) {
         errorHandle::newError(__METHOD__ . "() - usort", errorHandle::DEBUG);
         if (!$importing) {
             errorHandle::errorMsg("Error retrieving form.");
         }
         return FALSE;
     }
     $values = array();
     // go through all the fields, get their values
     foreach ($fields as $field) {
         $value = isset($engine->cleanPost['RAW'][$field['name']]) ? $engine->cleanPost['RAW'][$field['name']] : "";
         $validationTests = self::validateSubmission($formID, $field, $value, $objectID);
         if (isnull($validationTests) || $validationTests === FALSE) {
             continue;
         }
         if (strtolower($field['readonly']) == "true") {
             // need to pull the data that loaded with the form
             if ($newObject === FALSE) {
                 // grab it from the database
                 $oldObject = objects::get($objectID);
                 $values[$field['name']] = $oldObject['data'][$field['name']];
             } else {
                 // If the form has a variable in the value we apply the variable, otherwise, field value.
                 // we need to check for disabled on insert form
                 if (!isset($field['disabledInsert']) || isset($field['disabledInsert']) && $field['disabledInsert'] == "false") {
                     $values[$field['name']] = self::hasFieldVariables($field['value']) ? self::applyFieldVariables($value) : $field['value'];
                 }
                 // grab the default value from the form.
                 // $values[$field['name']] = $field['value'];
             }
         } else {
             if (strtolower($field['type']) == "file" && isset($engine->cleanPost['MYSQL'][$field['name']])) {
                 // Process uploaded files
                 $uploadID = $engine->cleanPost['MYSQL'][$field['name']];
                 // Process the uploads and put them into their archival locations
                 if (($tmpArray = files::processObjectUploads($objectID, $uploadID)) === FALSE) {
                     errorHandle::newError(__METHOD__ . "() - Archival Location", errorHandle::DEBUG);
                     return FALSE;
                 }
                 if ($tmpArray !== TRUE) {
                     // didn't generate a proper uuid for the items, rollback
                     if (!isset($tmpArray['uuid'])) {
                         $engine->openDB->transRollback();
                         $engine->openDB->transEnd();
                         errorHandle::newError(__METHOD__ . "() - No UUID", errorHandle::DEBUG);
                         return FALSE;
                     }
                     // ads this field to the files object
                     // we can't do inserts yet because we don't have the objectID on
                     // new objects
                     files::addProcessingField($field['name']);
                     // Should the files be processed now or later?
                     if (isset($field['bgProcessing']) && str2bool($field['bgProcessing']) === TRUE) {
                         $backgroundProcessing[$field['name']] = TRUE;
                     } else {
                         $backgroundProcessing[$field['name']] = FALSE;
                     }
                     $values[$field['name']] = $tmpArray;
                 } else {
                     // if we don't have files, and this is an update, we need to pull the files information from the
                     // version that is already in the system.
                     $oldObject = objects::get($objectID);
                     if ($newObject === FALSE && objects::hasFiles($objectID, $field['name']) === TRUE) {
                         $values[$field['name']] = $oldObject['data'][$field['name']];
                     }
                 }
             } else {
                 $values[$field['name']] = $value;
             }
         }
     }
     if (isset($engine->errorStack['error']) && count($engine->errorStack['error']) > 0) {
         // errorHandle::newError(__METHOD__."() - Error stack not empty.", errorHandle::DEBUG);
         return FALSE;
     }
     // start transactions
     $result = $engine->openDB->transBegin("objects");
     if ($result !== TRUE) {
         if (!$importing) {
             errorHandle::errorMsg("Database transactions could not begin.");
         }
         errorHandle::newError(__METHOD__ . "() - unable to start database transactions", errorHandle::DEBUG);
         return FALSE;
     }
     if ($newObject === TRUE) {
         if (objects::create($formID, $values, $form['metadata'], isset($engine->cleanPost['MYSQL']['parentID']) ? $engine->cleanPost['MYSQL']['parentID'] : "0") === FALSE) {
             $engine->openDB->transRollback();
             $engine->openDB->transEnd();
             if (!$importing) {
                 errorHandle::errorMsg("Error inserting new object.");
             }
             errorHandle::newError(__METHOD__ . "() - Error inserting new object.", errorHandle::DEBUG);
             return FALSE;
         }
         // Grab the objectID of the new object
         $objectID = localvars::get("newObjectID");
     } else {
         if (objects::update($objectID, $formID, $values, $form['metadata'], isset($engine->cleanPost['MYSQL']['parentID']) ? $engine->cleanPost['MYSQL']['parentID'] : "0") === FALSE) {
             $engine->openDB->transRollback();
             $engine->openDB->transEnd();
             if (!$importing) {
                 errorHandle::errorMsg("Error updating.");
             }
             errorHandle::newError(__METHOD__ . "() - Error updating.", errorHandle::DEBUG);
             return FALSE;
         }
     }
     // Now that we have a valid objectID, we insert into the processing table
     if (files::insertIntoProcessingTable($objectID) === FALSE) {
         $engine->openDB->transRollback();
         $engine->openDB->transEnd();
         errorHandle::newError(__METHOD__ . "() - Processing Table", errorHandle::DEBUG);
         return FALSE;
     }
     // end transactions
     $engine->openDB->transCommit();
     $engine->openDB->transEnd();
     if (!is_empty($backgroundProcessing)) {
         foreach ($backgroundProcessing as $fieldName => $V) {
             if ($V === FALSE) {
                 // No background processing. do it now.
                 files::process($objectID, $fieldName);
             }
         }
     }
     if ($newObject === TRUE) {
         if (!$importing) {
             errorHandle::successMsg("Object created successfully.");
         }
     } else {
         if (!$importing) {
             errorHandle::successMsg("Object updated successfully.");
         }
     }
     return TRUE;
 }