public function getSpecificTargets($data, $options)
 {
     switch ($data['items_id']) {
         case self::AUTHOR:
             $this->getUserByField('requester_id', true);
             break;
         case self::APPROVER:
             $form = new PluginFormcreatorForm();
             $form->getFromDB($this->obj->fields['plugin_formcreator_forms_id']);
             if ($form->fields['validation_required'] == 1) {
                 $this->getUserByField('validator_id', true);
             } elseif ($form->fields['validation_required'] == 2) {
                 $this->getAddressesByGroup(0, $this->obj->fields['validator_id']);
             }
             break;
     }
 }
 public function getDatasForTemplate($event, $options = array())
 {
     $form = new PluginFormcreatorForm();
     $form->getFromDB($this->obj->fields['plugin_formcreator_forms_id']);
     $link = 'http://' . $_SERVER['SERVER_NAME'] . $GLOBALS['CFG_GLPI']['root_doc'];
     $link .= '/plugins/formcreator/front/formanswer.form.php?id=' . $this->obj->getID();
     $requester = new User();
     $requester->getFromDB($this->obj->fields['requester_id']);
     $validator = new User();
     $validator->getFromDB($this->obj->fields['validator_id']);
     $this->datas['##formcreator.form_id##'] = $form->getID();
     $this->datas['##formcreator.form_name##'] = $form->fields['name'];
     $this->datas['##formcreator.form_requester##'] = $requester->getName();
     $this->datas['##formcreator.form_validator##'] = $validator->getName();
     $this->datas['##formcreator.form_creation_date##'] = Html::convDateTime($this->obj->fields['request_date']);
     $this->datas['##formcreator.form_full_answers##'] = $this->obj->getFullForm();
     $this->datas['##formcreator.validation_comment##'] = $this->obj->fields['comment'];
     $this->datas['##formcreator.validation_link##'] = $link;
     $this->datas['##formcreator.request_id##'] = $this->obj->fields['id'];
 }
Example #3
0
 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);
 }
<?php

include "../../../inc/includes.php";
// Check if plugin is activated...
$plugin = new Plugin();
if ($plugin->isActivated("formcreator") && isset($_REQUEST['id']) && is_numeric($_REQUEST['id'])) {
    $form = new PluginFormcreatorForm();
    if ($form->getFromDB((int) $_REQUEST['id'])) {
        if ($form->fields['access_rights'] != PluginFormcreatorForm::ACCESS_PUBLIC) {
            Session::checkLoginUser();
        }
        if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
            $table = getTableForItemType('PluginFormcreatorFormprofiles');
            $query = "SELECT *\n                   FROM {$table}\n                   WHERE plugin_formcreator_profiles_id = {$_SESSION['glpiactiveprofile']['id']}\n                   AND plugin_formcreator_forms_id = {$form->fields['id']}";
            $result = $DB->query($query);
            if ($DB->numrows($result) == 0) {
                Html::displayRightError();
                exit;
            }
        }
        if ($form->fields['access_rights'] == PluginFormcreatorForm::ACCESS_PUBLIC && !isset($_SESSION['glpiID'])) {
            // If user is not authenticated, create temporary user
            if (!isset($_SESSION['glpiname'])) {
                $_SESSION['formcreator_forms_id'] = $form->fields['id'];
                $_SESSION['glpiname'] = 'formcreator_temp_user';
                $_SESSION['valid_id'] = session_id();
                $_SESSION['glpiactiveentities'] = $form->fields['entities_id'];
                $subentities = getSonsOf('glpi_entities', $form->fields['entities_id']);
                $_SESSION['glpiactiveentities_string'] = !empty($subentities) ? "'" . implode("', '", $subentities) . "'" : "'" . $form->fields['entities_id'] . "'";
            }
        }
        Html::back();
    } elseif (isset($_POST['actor_role'])) {
        Session::checkRight("entity", UPDATE);
        $id = (int) $_POST['id'];
        $actor_value = isset($_POST['actor_value_' . $_POST['actor_type']]) ? $_POST['actor_value_' . $_POST['actor_type']] : '';
        $use_notification = $_POST['use_notification'] == 0 ? 0 : 1;
        $query = "INSERT INTO glpi_plugin_formcreator_targettickets_actors SET\n                  `plugin_formcreator_targettickets_id` = {$id},\n                  `actor_role`                          = '" . $_POST['actor_role'] . "',\n                  `actor_type`                          = '" . $_POST['actor_type'] . "',\n                  `actor_value`                         = " . (int) $actor_value . ",\n                  `use_notification`                    = " . (int) $use_notification;
        $DB->query($query);
        Html::back();
    } elseif (isset($_GET['delete_actor'])) {
        $query = "DELETE FROM glpi_plugin_formcreator_targettickets_actors\n                WHERE id = " . (int) $_GET['delete_actor'];
        $DB->query($query);
        Html::back();
        // Show target ticket form
    } else {
        Html::header(__('Form Creator', 'formcreator'), $_SERVER['PHP_SELF'], 'admin', 'PluginFormcreatorForm');
        $itemtype = "PluginFormcreatorTargetTicket";
        $target = new PluginFormcreatorTarget();
        $found = $target->find("itemtype = '{$itemtype}' AND items_id = " . (int) $_REQUEST['id']);
        $first = array_shift($found);
        $form = new PluginFormcreatorForm();
        $form->getFromDB($first['plugin_formcreator_forms_id']);
        $_SESSION['glpilisttitle'][$itemtype] = sprintf(__('%1$s = %2$s'), $form->getTypeName(1), $form->getName());
        $_SESSION['glpilisturl'][$itemtype] = $form->getFormURL() . "?id=" . $form->getID();
        $targetticket->display($_REQUEST);
        Html::footer();
    }
    // Or display a "Not found" error
} else {
    Html::displayNotFoundError();
}
 /**
  * Save form datas to the target
  *
  * @param  PluginFormcreatorFormanswer $formanswer    Answers previously saved
  */
 public function save(PluginFormcreatorFormanswer $formanswer)
 {
     $datas = array();
     $ticket = new Ticket();
     $docItem = new Document_Item();
     $form = new PluginFormcreatorForm();
     $form->getFromDB($formanswer->fields['plugin_formcreator_forms_id']);
     // Get default request type
     $query = "SELECT id FROM `glpi_requesttypes` WHERE `name` LIKE 'Formcreator';";
     $result = $GLOBALS['DB']->query($query) or die($GLOBALS['DB']->error());
     list($requesttypes_id) = $GLOBALS['DB']->fetch_array($result);
     $datas['requesttypes_id'] = $requesttypes_id;
     // Get predefined Fields
     $ttp = new TicketTemplatePredefinedField();
     $predefined_fields = $ttp->getPredefinedFields($this->fields['tickettemplates_id'], true);
     $datas = array_merge($datas, $predefined_fields);
     // Parse datas and tags
     $datas['name'] = addslashes($this->parseTags($this->fields['name'], $formanswer));
     $datas['content'] = htmlentities($this->parseTags($this->fields['comment'], $formanswer));
     $datas['entities_id'] = isset($_SESSION['glpiactive_entity']) ? $_SESSION['glpiactive_entity'] : $form->fields['entities_id'];
     $datas['_users_id_requester'] = 0;
     $datas['_users_id_recipient'] = $_SESSION['glpiID'];
     $datas['_tickettemplates_id'] = $this->fields['tickettemplates_id'];
     // Define due date
     $answer = new PluginFormcreatorAnswer();
     $found = $answer->find('plugin_formcreator_formanwers_id = ' . (int) $formanswer->fields['id'] . ' AND plugin_formcreator_question_id = ' . (int) $this->fields['due_date_question']);
     $date = array_shift($found);
     $str = "+" . $this->fields['due_date_value'] . " " . $this->fields['due_date_period'];
     switch ($this->fields['due_date_rule']) {
         case 'answer':
             $due_date = $date['answer'];
             break;
         case 'ticket':
             $due_date = date('Y-m-d H:i:s', strtotime($str));
             break;
         case 'calcul':
             $due_date = date('Y-m-d H:i:s', strtotime($date['answer'] . " " . $str));
             break;
         default:
             $due_date = null;
             break;
     }
     if (!is_null($due_date)) {
         $datas['due_date'] = $due_date;
     }
     // Select ticket actors
     $query = "SELECT id, actor_type, actor_value, use_notification\n                FROM glpi_plugin_formcreator_targettickets_actors\n                WHERE plugin_formcreator_targettickets_id = " . $this->getID() . "\n                AND actor_role = 'requester'";
     $result = $GLOBALS['DB']->query($query);
     // If there is only one requester add it on creation, otherwize we will add them later
     if ($GLOBALS['DB']->numrows($result) == 1) {
         $actor = $GLOBALS['DB']->fetch_array($result);
         switch ($actor['actor_type']) {
             case 'creator':
                 $user_id = $formanswer->fields['requester_id'];
                 break;
             case 'validator':
                 $user_id = $formanswer->fields['validator_id'];
                 break;
             case 'person':
             case 'group':
             case 'supplier':
                 $user_id = $actor['actor_value'];
                 break;
             case 'question_person':
             case 'question_group':
             case 'question_supplier':
                 $answer = new PluginFormcreatorAnswer();
                 $found = $answer->find('`plugin_formcreator_question_id` = ' . (int) $actor['actor_value'] . ' AND `plugin_formcreator_formanwers_id` = ' . (int) $formanswer->fields['id']);
                 $found = array_shift($found);
                 if (empty($found['answer'])) {
                     continue;
                 } else {
                     $user_id = (int) $found['answer'];
                 }
                 break;
         }
         $datas['_users_id_requester'] = $user_id;
     }
     // Create the target ticket
     if (!($ticketID = $ticket->add($datas))) {
         return false;
     }
     // Add link between Ticket and FormAnswer
     $itemlink = new Item_Ticket();
     $itemlink->add(array('itemtype' => 'PluginFormcreatorFormanswer', 'items_id' => $formanswer->fields['id'], 'tickets_id' => $ticketID));
     // Add actors to ticket
     $query = "SELECT id, actor_role, actor_type, actor_value, use_notification\n                FROM glpi_plugin_formcreator_targettickets_actors\n                WHERE plugin_formcreator_targettickets_id = " . $this->getID();
     $result = $GLOBALS['DB']->query($query);
     while ($actor = $GLOBALS['DB']->fetch_array($result)) {
         // If actor type is validator and if the form doesn't have a validator, continue to other actors
         if ($actor['actor_type'] == 'validator' && !$form->fields['validation_required']) {
             continue;
         }
         switch ($actor['actor_role']) {
             case 'requester':
                 $role = CommonITILActor::REQUESTER;
                 break;
             case 'observer':
                 $role = CommonITILActor::OBSERVER;
                 break;
             case 'assigned':
                 $role = CommonITILActor::ASSIGN;
                 break;
         }
         switch ($actor['actor_type']) {
             case 'creator':
                 $user_id = $formanswer->fields['requester_id'];
                 break;
             case 'validator':
                 $user_id = $formanswer->fields['validator_id'];
                 break;
             case 'person':
             case 'group':
             case 'supplier':
                 $user_id = $actor['actor_value'];
                 break;
             case 'question_person':
             case 'question_group':
             case 'question_supplier':
                 $answer = new PluginFormcreatorAnswer();
                 $found = $answer->find('`plugin_formcreator_question_id` = ' . (int) $actor['actor_value'] . ' AND `plugin_formcreator_formanwers_id` = ' . (int) $formanswer->fields['id']);
                 $found = array_shift($found);
                 if (empty($found['answer'])) {
                     continue;
                 } else {
                     $user_id = (int) $found['answer'];
                 }
                 break;
         }
         switch ($actor['actor_type']) {
             case 'creator':
             case 'validator':
             case 'person':
             case 'question_person':
                 $obj = new Ticket_User();
                 $obj->add(array('tickets_id' => $ticketID, 'users_id' => $user_id, 'type' => $role, 'use_notification' => $actor['use_notification']));
                 break;
             case 'group':
             case 'question_group':
                 $obj = new Group_Ticket();
                 $obj->add(array('tickets_id' => $ticketID, 'groups_id' => $user_id, 'type' => $role, 'use_notification' => $actor['use_notification']));
                 break;
             case 'supplier':
             case 'question_supplier':
                 $obj = new Supplier_Ticket();
                 $obj->add(array('tickets_id' => $ticketID, 'suppliers_id' => $user_id, 'type' => $role, 'use_notification' => $actor['use_notification']));
                 break;
         }
     }
     // Attach documents to ticket
     $found = $docItem->find("itemtype = 'PluginFormcreatorFormanswer' AND items_id = " . (int) $formanswer->getID());
     if (count($found) > 0) {
         foreach ($found as $document) {
             $docItem->add(array('documents_id' => $document['documents_id'], 'itemtype' => 'Ticket', 'items_id' => $ticketID));
         }
     }
     // Attach validation message as first ticket followup if validation is required and
     // if is set in ticket target configuration
     // /!\ Followup is directly saved to the database to avoid double notification on ticket
     //     creation and add followup
     if ($form->fields['validation_required'] && $this->fields['validation_followup']) {
         $message = addslashes(__('Your form have been accepted by the validator', 'formcreator'));
         if (!empty($formanswer->fields['comment'])) {
             $message .= "\n" . addslashes($formanswer->fields['comment']);
         }
         $query = "INSERT INTO `glpi_ticketfollowups` SET\n                     `tickets_id` = {$ticketID},\n                     `date`       = NOW(),\n                     `users_id`   = {$_SESSION['glpiID']},\n                     `content`    = \"{$message}\"";
         $GLOBALS['DB']->query($query);
     }
     return true;
 }
Example #7
0
 static function viewFormInListForm($formID)
 {
     $ver = 0;
     //Obtener el tipo de acceso
     $form = new PluginFormcreatorForm();
     $form->getFromDB($formID);
     if (isset($form->fields['access_rights'])) {
         $access = $form->fields['access_rights'];
     } else {
         $access = 0;
     }
     if ($_SESSION['glpiactiveprofile']['id'] != 4) {
         switch ($access) {
             case self::ACCESS_PUBLIC:
                 //PUBLIC: acceso publico al pedido
                 $ver = 1;
                 break;
             case self::ACCESS_PRIVATE:
                 // PRIVATE: es 0 porque no esta implementado
                 $ver = 0;
                 break;
             case self::ACCESS_RESTRICTED:
                 // RESTRICTED: es regringido por perfil, comprobar el acceso con la funcion checkRestrictedProfileInForm
                 if (PluginFormcreatorForm::checkRestrictedProfileInForm($formID) == 1) {
                     $ver = 1;
                 }
                 break;
             case self::ACCESS_GROUP:
                 // GROUP: es regringido por grupo, comprobar el acceso con la funcion checkGroupUserFromForm
                 if (PluginFormcreatorForm::checkGroupUserFromForm($formID) == 1) {
                     $ver = 1;
                 }
                 break;
             default:
                 return 0;
         }
     } else {
         $ver = 1;
     }
     return $ver;
 }
Example #8
0
                $string = preg_replace($regex_if, "\\1", $string, 1);
                $string = preg_replace($regex_else, "", $string, 1);
            } else {
                // Do ELSE
                $string = preg_replace($regex_if, "", $string, 1);
                $string = preg_replace($regex_else, "\\1", $string, 1);
            }
        }
    }
    return $string;
}
echo "<div class='center'>" . "\n\r";
$helpdesk = new PluginFormcreatorHelpdesk();
$formID = $_REQUEST['id'];
$form = new PluginFormcreatorForm();
$form->getFromDB($formID);
$tableau_beneficiary_name = "";
$targets = $helpdesk->getTarget($formID);
foreach ($targets as $target_id => $target_value) {
    $ticket = array();
    $validation_exist = false;
    $validationTab = array();
    $cpt_valid = 0;
    $ticket['entities_id'] = $form->fields['entities_id'];
    $ticket['urgency'] = $target_value['urgency'];
    $ticket['priority'] = $target_value['priority'];
    $ticket['itilcategories_id'] = $target_value['itilcategories_id'];
    $ticket['name'] = $target_value['name'];
    $questions = $helpdesk->getQuestionByForm($formID);
    foreach ($questions as $question_id => $question_value) {
        $question_name = 'question_' . $question_id;
     $form->check($_POST['id'], 'd');
     $form->delete($_POST);
     $form->redirectToList();
     // Restore a deleteted form (is_deleted = false)
 } elseif (isset($_POST["restore"])) {
     $form->check($_POST['id'], 'd');
     $form->restore($_POST);
     $form->redirectToList();
     // Delete defenitively a form from DB and all its datas
 } elseif (isset($_POST["purge"])) {
     $form->check($_POST['id'], 'd');
     $form->delete($_POST, 1);
     $form->redirectToList();
     // Save form to target
 } elseif (isset($_POST['submit_formcreator'])) {
     if ($form->getFromDB($_POST['formcreator_form'])) {
         // If user is not authenticated, create temporary user
         if (!isset($_SESSION['glpiname'])) {
             $_SESSION['glpiname'] = 'formcreator_temp_user';
         }
         // Save form
         $form->saveToTargets($_POST);
         // If user was not authenticated, remove temporary user
         if ($_SESSION['glpiname'] == 'formcreator_temp_user') {
             unset($_SESSION['glpiname']);
             Html::back();
         } else {
             Html::redirect('formlist.php');
         }
     }
     // Show forms form