uploadDocument() static public méthode

Upload a new file
static public uploadDocument ( array &$input, $FILEDESC ) : true
$input array
$FILEDESC FILE descriptor
Résultat true on success
 public function saveAnswers($datas)
 {
     $form = new PluginFormcreatorForm();
     $form->getFromDB($datas['formcreator_form']);
     $query = "SELECT q.`id`, q.`fieldtype`, q.`name`\n                FROM glpi_plugin_formcreator_questions q\n                LEFT JOIN glpi_plugin_formcreator_sections s ON s.`id` = q.`plugin_formcreator_sections_id`\n                WHERE s.`plugin_formcreator_forms_id` = {$datas['formcreator_form']}";
     $result = $GLOBALS['DB']->query($query);
     // Update form answers
     if (isset($_POST['save_formanswer'])) {
         $status = $_POST['status'];
         $this->update(array('id' => (int) $datas['id'], 'status' => $status, 'comment' => isset($_POST['comment']) ? $_POST['comment'] : 'NULL'));
         // Update questions answers
         if ($status == 'waiting') {
             while ($question = $GLOBALS['DB']->fetch_array($result)) {
                 if ($question['fieldtype'] != 'file') {
                     $answer = new PluginFormcreatorAnswer();
                     $found = $answer->find('`plugin_formcreator_formanwers_id` = ' . (int) $datas['id'] . '
                                       AND `plugin_formcreator_question_id` = ' . $question['id']);
                     $found = array_shift($found);
                     $data_value = $datas['formcreator_field_' . $question['id']];
                     if (isset($data_value)) {
                         if (is_array($data_value)) {
                             foreach ($data_value as $key => $value) {
                                 $data_value[$key] = $value;
                             }
                             $answer_value = json_encode($data_value);
                         } else {
                             $answer_value = $data_value;
                         }
                     } else {
                         $answer_value = '';
                     }
                     $answer->update(array('id' => $found['id'], 'answer' => $answer_value));
                 } elseif (isset($_FILES['formcreator_field_' . $question['id']]['tmp_name']) && is_file($_FILES['formcreator_field_' . $question['id']]['tmp_name'])) {
                     $doc = new Document();
                     $answer = new PluginFormcreatorAnswer();
                     $found = $answer->find('`plugin_formcreator_formanwers_id` = ' . (int) $datas['id'] . '
                                       AND `plugin_formcreator_question_id` = ' . $question['id']);
                     $found = array_shift($found);
                     $file_datas = array();
                     $file_datas["name"] = $form->fields['name'] . ' - ' . $question['name'];
                     $file_datas["entities_id"] = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : $form->fields['entities_id'];
                     $file_datas["is_recursive"] = $form->fields['is_recursive'];
                     Document::uploadDocument($file_datas, $_FILES['formcreator_field_' . $question['id']]);
                     if ($docID = $doc->add($file_datas)) {
                         $table = getTableForItemType('Document');
                         $filename = $_FILES['formcreator_field_' . $question['id']]['name'];
                         $query = "UPDATE {$table} SET filename = '" . $filename . "' WHERE id = " . $docID;
                         $GLOBALS['DB']->query($query);
                         $docItem = new Document_Item();
                         $docItemId = $docItem->add(array('documents_id' => $docID, 'itemtype' => __CLASS__, 'items_id' => (int) $datas['id']));
                         $answer->update(array('id' => $found['id'], 'answer' => $docID));
                     }
                 }
             }
         }
         // Create new form answer object
     } else {
         // Does the form need to be validate ?
         if ($form->fields['validation_required']) {
             $status = 'waiting';
         } else {
             $status = 'accepted';
         }
         $id = $this->add(array('entities_id' => isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : $form->fields['entities_id'], 'is_recursive' => $form->fields['is_recursive'], 'plugin_formcreator_forms_id' => $datas['formcreator_form'], 'requester_id' => isset($_SESSION['glpiID']) ? $_SESSION['glpiID'] : 0, 'validator_id' => isset($datas['formcreator_validator']) ? $datas['formcreator_validator'] : 0, 'status' => $status, 'request_date' => date('Y-m-d H:i:s')));
         // Save questions answers
         while ($question = $GLOBALS['DB']->fetch_assoc($result)) {
             // If the answer is set, check if it is an array (then implode id).
             if (isset($datas[$question['id']])) {
                 $question_answer = $datas[$question['id']];
                 if (is_array(json_decode($question_answer))) {
                     $question_answer = json_decode($question_answer);
                     foreach ($question_answer as $key => $value) {
                         $question_answer[$key] = $value;
                     }
                     $question_answer = json_encode($question_answer);
                 } else {
                     $question_answer = $question_answer;
                 }
             } else {
                 $question_answer = '';
             }
             $answer = new PluginFormcreatorAnswer();
             $answerID = $answer->add(array('plugin_formcreator_formanwers_id' => $id, 'plugin_formcreator_question_id' => $question['id'], 'answer' => $question_answer));
             // If the question is a file field, save the file as a document
             if ($question['fieldtype'] == 'file' && isset($_FILES['formcreator_field_' . $question['id']]['tmp_name']) && is_file($_FILES['formcreator_field_' . $question['id']]['tmp_name'])) {
                 $doc = new Document();
                 $file_datas = array();
                 $file_datas["name"] = $form->fields['name'] . ' - ' . $question['name'];
                 $file_datas["entities_id"] = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : $form->fields['entities_id'];
                 $file_datas["is_recursive"] = $form->fields['is_recursive'];
                 Document::uploadDocument($file_datas, $_FILES['formcreator_field_' . $question['id']]);
                 if ($docID = $doc->add($file_datas)) {
                     $table = getTableForItemType('Document');
                     $filename = $_FILES['formcreator_field_' . $question['id']]['name'];
                     $query = "UPDATE {$table} SET filename = '" . $filename . "' WHERE id = " . $docID;
                     $GLOBALS['DB']->query($query);
                     $docItem = new Document_Item();
                     $docItemId = $docItem->add(array('documents_id' => $docID, 'itemtype' => __CLASS__, 'items_id' => $id));
                     $answer->update(array('id' => $answerID, 'answer' => $docID));
                 }
             }
         }
     }
     NotificationEvent::raiseEvent('plugin_formcreator_form_created', $this);
     if ($form->fields['validation_required'] || $status == 'accepted') {
         switch ($status) {
             case 'waiting':
                 // Notify the validator
                 NotificationEvent::raiseEvent('plugin_formcreator_need_validation', $this);
                 break;
             case 'refused':
                 // Notify the requester
                 NotificationEvent::raiseEvent('plugin_formcreator_refused', $this);
                 break;
             case 'accepted':
                 // Notify the requester
                 if (!$this->generateTarget()) {
                     Session::addMessageAfterRedirect(__('Cannot generate targets!', 'formcreator'), true, ERROR);
                     $this->update(array('id' => $this->getID(), 'status' => 'waiting'));
                 }
                 NotificationEvent::raiseEvent('plugin_formcreator_accepted', $this);
                 return false;
                 break;
         }
     }
     Session::addMessageAfterRedirect(__('The form have been successfully saved!', 'formcreator'), true, INFO);
 }
 public function saveToTargets($datas)
 {
     $valid = true;
     // Validate form fields
     foreach ($_POST as $key => $value) {
         if (substr($key, 0, 18) == 'formcreator_field_') {
             $question_id = (int) substr($key, 18);
             $question = new PluginFormcreatorQuestion();
             $question->getFromDB($question_id);
             $className = $question->fields['fieldtype'] . 'Field';
             $filePath = dirname(__FILE__) . '/fields/' . $question->fields['fieldtype'] . '-field.class.php';
             if ($question->fields['fieldtype'] == 'float') {
                 $value = str_replace(',', '.', $datas['formcreator_field_' . $question_id]);
                 $datas['formcreator_field_' . $question_id] = $value;
             }
             if (is_file($filePath)) {
                 include_once $filePath;
                 if (class_exists($className)) {
                     if (!$className::isValid($question->fields, $value, $datas)) {
                         $valid = false;
                     }
                 }
             } else {
                 $valid = false;
             }
         }
     }
     // If not valid back to form
     if (!$valid) {
         $_SESSION['formcreator']['datas'] = $datas;
         Html::back();
         // Otherwize  generate targets
     } else {
         $_SESSION['formcreator_documents'] = array();
         // Save files as Documents
         foreach ($_FILES as $question_name => $file) {
             if (isset($file['tmp_name']) && is_file($file['tmp_name'])) {
                 $doc = new Document();
                 $question_id = trim(strrchr($question_name, '_'), '_');
                 $question = new PluginFormcreatorQuestion();
                 $question->getFromDB($question_id);
                 $file_datas = array();
                 $file_datas["name"] = $this->fields['name'] . ' - ' . $question->fields['name'];
                 $file_datas["entities_id"] = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : $this->fields['entities_id'];
                 $file_datas["is_recursive"] = $this->fields['is_recursive'];
                 Document::uploadDocument($file_datas, $file);
                 if ($docID = $doc->add($file_datas)) {
                     $_SESSION['formcreator_documents'][] = $docID;
                     $table = getTableForItemType('Document');
                     $query = "UPDATE {$table} SET filename = '" . addslashes($file['name']) . "' WHERE id = " . $docID;
                     $GLOBALS['DB']->query($query);
                 }
             }
         }
         // Get all targets
         $target_class = new PluginFormcreatorTarget();
         $founded_targets = $target_class->find('plugin_formcreator_forms_id = ' . $this->getID());
         foreach ($founded_targets as $target) {
             $obj = new $target['itemtype']();
             $obj->getFromDB($target['items_id']);
             $obj->save($this, $datas);
         }
         Session::addMessageAfterRedirect(__('The form have been successfully saved!', 'formcreator'), true, INFO);
         unset($_SESSION['formcreator_documents']);
     }
 }