Skip to content
Snippets Groups Projects
manage_action.php 19.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    *    Copyright 2008-2015 Maarch
    
    *
    *  This file is part of Maarch Framework.
    *
    *   Maarch Framework is free software: you can redistribute it and/or modify
    *   it under the terms of the GNU General Public License as published by
    *   the Free Software Foundation, either version 3 of the License, or
    *   (at your option) any later version.
    *
    *   Maarch Framework is distributed in the hope that it will be useful,
    *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    *   GNU General Public License for more details.
    *
    *   You should have received a copy of the GNU General Public License
    *    along with Maarch Framework.  If not, see <http://www.gnu.org/licenses/>.
    */
    
    /**
    * @brief   Manage core actions
    *
    * @file
    * @author Claire Figueras <dev@maarch.org>
    * @date $date$
    * @version $Revision$
    * @ingroup core
    */
    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    $db = new Database();
    
    $core = new core_tools();
    $core->load_lang();
    $res_action = array();
    
    
    SNA's avatar
    SNA committed
    /*
    
    * Puts the values that are in a string into an array.
    * $$ field separator, # field_name / value separator
    *
    * @param $val string Values to split
    * @return array Values in array
    
    SNA's avatar
    SNA committed
    */
    
    function get_values_in_array($val)
    {
    
    SNA's avatar
    SNA committed
        $values = array();
    
    SNA's avatar
    SNA committed
            $tmp = explode('#', $tab[$i]);
    
            for ($idiese=1;$idiese<count($tmp);$idiese++) {
                $val_tmp[]=$tmp[$idiese];
            }
            $valeurDiese = implode("#", $val_tmp);
            if (isset($tmp[1])) {
    
                array_push($values, array('ID' => $tmp[0], 'VALUE' => $valeurDiese));
    
    SNA's avatar
    SNA committed
            }
        }
        return $values;
    
    }
    
    // Form validation
    
    if ($_POST['req'] == 'valid_form' && !empty($_POST['action_id']) && isset($_POST['action_id']) && !empty($_POST['form_to_check'])&& isset($_POST['form_to_check'])) {
    
    SNA's avatar
    SNA committed
        $id_action = $_POST['action_id'];
        // Gets the action informations from the database
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $stmt = $db->query("select * from ".$_SESSION['tablename']['actions']." where id = ?", array($id_action));
    
    SNA's avatar
    SNA committed
            $_SESSION['action_error'] = _ACTION_NOT_IN_DB;
    
            echo "{status : 5, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
            exit();
        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $res = $stmt->fetchObject();
    
    SNA's avatar
    SNA committed
        $label_action = $res->label_action;
        $status = $res->id_status;
        $action_page = $res->action_page;
    
        if ($res->history == 'Y' && $_POST['hist']!='N') {
    
        //No script defined for this action
        if ($action_page == '') {
    
    SNA's avatar
    SNA committed
            $_SESSION['action_error'] = _ACTION_NOT_IN_DB;
    
            echo "{status : 5, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
            exit();
        }
        $custom_path = '';
        $path_action_page = $core->get_path_action_page($action_page);
    
    
        if (isset($_SESSION['custom_override_id']) && !empty($_SESSION['custom_override_id'])) {
    
    SNA's avatar
    SNA committed
            $custom_path = 'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.$path_action_page;
        }
    
        if ($custom_path <> '' && file_exists($_SESSION['config']['corepath'].$custom_path)) {
    
    SNA's avatar
    SNA committed
            include($custom_path);
    
        } else {
            if (file_exists($path_action_page)) {
    
    SNA's avatar
    SNA committed
                include($path_action_page);
    
    SNA's avatar
    SNA committed
                // Invalid path to script
                $_SESSION['action_error'] = $label_action.' '._ACTION_PAGE_MISSING;
    
                echo "{status : 8, error_txt: '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
        $frm_error = check_form(trim($_POST['form_to_check']), get_values_in_array($_POST['form_values']));
        if ($frm_error == false) {
    
            echo "{status : 1, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
            exit();
    
            echo "{status : 0, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."', page_result : '', manage_form_now : false}";
    
    SNA's avatar
    SNA committed
            exit();
        }
    
    } elseif (trim($_POST['req']) == 'change_status' && !empty($_POST['values']) && !empty($_POST['new_status']) && !empty($_POST['table'])) {
    
        $stmt = $db->query("select id, label_status from status where id = ?", array($_POST['new_status']));
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $lineStatus = $stmt->fetchObject();
    
        if ($lineStatus->id <> '') {
            $arr_id = explode(',', $_POST['values']);
            $result = '';
    
            for ($i=0; $i<count($arr_id);$i++) {
    
                $arr_id[$i] = str_replace('#', '', $arr_id[$i]);
                $result .= $arr_id[$i].'#';
    
                if (trim($_POST['new_status']) <> '' && is_numeric($arr_id[$i])) {
    
                    
                    $query_str = "update " . $_POST['table']
                        . " set status = ? where res_id = ?";
                    
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    $stmt = $db->query($query_str, array($_POST['new_status'], $arr_id[$i]));
                    if (!$stmt) {
    
                        $_SESSION['action_error'] = _SQL_ERROR.' : '.$query_str;
    
                        echo "{status : 1, error_txt : '".addslashes(_ERROR_WITH_STATUS." ".functions::xssafe($query_str))."'}";
    
    SNA's avatar
    SNA committed
            }
    
            echo json_encode(['status' => 0, 'error_txt' => _STATUS_UPDATED.' : '.functions::xssafe($_POST['new_status']) ]);
    
            exit();
        } else {
    
            echo json_encode(['status' => 0, 'error_txt' => _STATUS_NOT_EXISTS.' : '.functions::xssafe($_POST['new_status']) ]);
    
            exit();
    
    SNA's avatar
    SNA committed
        }
    
    SNA's avatar
    SNA committed
    }
    
    // Post variables error
    
    elseif (empty($_POST['values']) || !isset($_POST['action_id']) || empty($_POST['action_id']) ||
    
    ($_POST['mode'] <> 'mass' && $_POST['mode'] <> 'page')  || empty($_POST['table'])
    
    || empty($_POST['coll_id']) || empty($_POST['module']) || ($_POST['req'] <> 'first_request' && $_POST['req'] <> 'second_request' && $_POST['req'] <> 'change_status')) {
    
    SNA's avatar
    SNA committed
        $tmp = 'values : '.$_POST['values'].', action_id : '.$_POST['action_id'].', mode : '. $_POST['mode'].', table : '.$_POST['table'].', coll_id : '.$_POST['coll_id'].', module : '.$_POST['module'].', req : '.$_POST['req'];
        $_SESSION['action_error'] = $tmp._AJAX_PARAM_ERROR;
    
        echo "{status : 1, error_txt : '".functions::xssafe($id_action).addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
        exit();
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
    } else {
    
    SNA's avatar
    SNA committed
        // Puts the res_id into an array
        $arr_id = explode(',', $_POST['values']);
        $id_action = $_POST['action_id'];
        // Gets the action informations from the database
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $stmt = $db->query("select * from ".$_SESSION['tablename']['actions']." where id = ?", array($id_action));
        if ($stmt->rowCount() < 1) {
    
    SNA's avatar
    SNA committed
            $_SESSION['action_error'] = _ACTION_NOT_IN_DB;
    
            echo "{status : 5, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
            exit();
        }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
        $res = $stmt->fetchObject();
    
    SNA's avatar
    SNA committed
        $label_action = $res->label_action;
        $status = $res->id_status;
        $action_page = $res->action_page;
    
        if ($res->history == 'Y' && $_POST['hist']!='N') {
    
    SNA's avatar
    SNA committed
        //No script defined for this action
    
    SNA's avatar
    SNA committed
            //If second request : Error
    
            if ($_POST['req'] == 'second_request') {
    
    SNA's avatar
    SNA committed
                $_SESSION['action_error'] = _ACTION_NOT_IN_DB;
    
                echo "{status : 5, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                exit();
            }
    
    SNA's avatar
    SNA committed
            //If no status defined in the action file , error
    
            if ($status == '' || $status == 'NONE') {
    
    SNA's avatar
    SNA committed
                $_SESSION['action_error'] = $label_action.' : '._ERROR_PARAM_ACTION;
    
                echo "{status : 6, error_txt : '".functions::xssafe(addslashes($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                exit();
            }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            $stmt = $db->query("select id from status where id = ?", array($status));
            $lineStatus = $stmt->fetchObject();
    
            if ($lineStatus->id <> '') {
                // Update the status
                $result = '';
    
                for ($i=0;$i<count($arr_id);$i++) {
    
                    $arr_id[$i] = str_replace('#', '', $arr_id[$i]);
                    $result .= $arr_id[$i].'#';
                    if (trim($status) <> '') {
    
                            $query_str = "update " . $_POST['table']
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                .  " set status = ? where res_id = ?";
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                        $stmt = $db->query($query_str, array($status, $arr_id[$i]));
                        if (!$stmt) {
    
                            $_SESSION['action_error'] = _SQL_ERROR . ' : ' . $query_str;
    
                            echo "{status : 7, error_txt : '" . addslashes(functions::xssafe($label_action)
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                                . ' : ' . functions::xssafe($_SESSION['action_error'])) . "'}";
    
    SNA's avatar
    SNA committed
                }
            }
            $res_action = array('result' => $result, 'history_msg' => '');
            $_SESSION['action_error'] = _ACTION_DONE.' : '.$label_action;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
            echo "{status : 0, error_txt : '".addslashes($_SESSION['action_error']).", status : "
                .functions::xssafe($status).", ".functions::xssafe($_POST['values'])."', page_result : ''}";
    
    SNA's avatar
    SNA committed
        }
        // There is a script for the action
    
    SNA's avatar
    SNA committed
            $custom_path = '';
            $path_action_page = $core->get_path_action_page($action_page);
    
    SNA's avatar
    SNA committed
    
    
            if (isset($_SESSION['custom_override_id']) && !empty($_SESSION['custom_override_id'])) {
    
    SNA's avatar
    SNA committed
                $custom_path = 'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.$path_action_page;
            }
    
            if ($custom_path <> '' && file_exists($_SESSION['config']['corepath'].$custom_path)) {
    
    SNA's avatar
    SNA committed
                include($custom_path);
    
            } else {
                if (file_exists($path_action_page)) {
    
    SNA's avatar
    SNA committed
                    include($path_action_page);
    
    SNA's avatar
    SNA committed
                    // Invalid path to script
                    $_SESSION['action_error'] = $label_action.' '._ACTION_PAGE_MISSING;
    
                    echo "{status : 8, error_txt: '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                    exit();
                }
            }
    
            if ($_POST['req'] == 'first_request' && in_array('form', $etapes) && $error_visa_workflow_signature_book != true) {
                $frm_test = get_form_txt($arr_id, $_SESSION['config']['businessappurl'].'index.php?display=true&page=manage_action&module=core', $id_action, $_POST['table'], $_POST['module'], $_POST['coll_id'], $_POST['mode']);
    
                echo "{status : 3, form_content : '".$frm_test."', height : '".$frm_height."', width : '".$frm_width."', 'mode_frm' : '".$mode_form."', 'action_status' : '".functions::xssafe($status)."'}";
    
    SNA's avatar
    SNA committed
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && in_array('no_attachment', $etapes)) {
    
                echo "{status : 3, form_content : '<div class=\"h2_title\">" . addslashes(_ADD_ATTACHMENT_TO_SEND_TO_CONTACT) .
                    "</div><p class=\"buttons\"><input type=\"button\" class=\"button\" value=\""._CANCEL."\" onclick=\"destroyModal(\'modal_" .$id_action . "\')\" id=\"submit\" name=\"submit\"></p>', height : '250px', width : '300px', 'mode_frm' : '', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && $error_visa_workflow_signature_book == true) {
    
                echo "{status : 4, error : '".addslashes(_NO_RESPONSE_PROJECT_VISA)."', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && $error_visa == true) {
    
                echo "{status : 4, error : '".addslashes(_NO_VISA)."', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && $error_visa_response_project == true) {
    
                echo "{status : 3, form_content : '<div class=\"h2_title\">" . addslashes($visa->errorMessageVisa) .
    
    Damien's avatar
    Damien committed
                    "</div><p class=\"buttons\"><input type=\"button\" onclick=\"destroyModal(\'modal_" .$id_action . "\')\" class=\"button\" value=\"OK\" id=\"submit\" name=\"submit\"></p>', height : 'auto', width : 'auto', 'mode_frm' : '', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
    
            } elseif ($_POST['req'] == 'first_request' && $error_visa_workflow == true) {
    
                echo "{status : 4, error : '".addslashes(_NO_NEXT_STEP_VISA)."', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && $confirm == true) {
    
                if (!empty($warnMsg)) {
                    echo "{status : 2, confirm_content : '".addslashes("<p style='font-size: 12px;text-align: center;color: white;background: #F99830;padding: 5px;margin: -10px -10px 20px -10px;'><i class='fa fa-exclamation-triangle'></i> ".$warnMsg."<p>"._ACTION_CONFIRM." ".functions::xssafe($label_action))."', validate : '"._VALIDATE."', cancel : '"._CANCEL."', label_action : '".addslashes(functions::xssafe($label_action))."', 'action_status' : '".functions::xssafe($status)."'}";
                } else {
                    echo "{status : 2, confirm_content : '".addslashes(_ACTION_CONFIRM." ".functions::xssafe($label_action))."', validate : '"._VALIDATE."', cancel : '"._CANCEL."', label_action : '".addslashes(functions::xssafe($label_action))."', 'action_status' : '".functions::xssafe($status)."'}";
                }
    
    SNA's avatar
    SNA committed
                exit();
    
            } elseif ($_POST['req'] == 'first_request' && $confirm == false && $action_page == 'close_mail_with_attachment') {
    
                echo "{status : 3, form_content : '<div class=\"h2_title\">" . addslashes(_ADD_ATTACHMENT_OR_NOTE) .
                    "</div><p class=\"buttons\"><input type=\"button\" class=\"button\" value=\""._CANCEL."\" onclick=\"destroyModal(\'modal_" .$id_action . "\')\" id=\"submit\" name=\"submit\"></p>', height : '250px', width : '300px', 'mode_frm' : '', validate : 'OK', 'action_status' : '".functions::xssafe($status)."'}";
                exit();
    
    SNA's avatar
    SNA committed
                    $_SESSION['action_error'] = $label_action.' : '._ERROR_SCRIPT;
                }
    
                for ($i=0; $i<count($etapes);$i++) {
                    if ($etapes[$i] <> 'status') {
                        if (function_exists('manage_'.$etapes[$i])) {
                            try {
                                if ($_POST['req'] == 'second_request') {
                                    $res_action = call_user_func('manage_'.$etapes[$i], $arr_id, $bool_history, $id_action, $label_action, $status, $_POST['coll_id'], $_POST['table'], get_values_in_array($_POST['form_values']));
                                } else {
                                    $res_action = call_user_func('manage_'.$etapes[$i], $arr_id, $bool_history, $id_action, $label_action, $status, $_POST['coll_id'], $_POST['table']);
    
    SNA's avatar
    SNA committed
                                }
    
                                echo "{status : 9, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                                exit();
                            }
    
                            echo "{status : 9, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                            exit();
                        }
                    }
                }
                //print_r($res_action);
    
                    echo "{status : 9, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
    
    SNA's avatar
    SNA committed
                    exit();
                }
                $comp = ", page_result  : ''";
    
                if (isset($res_action['page_result']) && !empty($res_action['page_result'])) {
    
    SNA's avatar
    SNA committed
                    $comp = ", page_result  : '".$res_action['page_result']."'";
                }
    
                if (isset($res_action['table_dest']) && !empty($res_action['table_dest'])) {
    
    SNA's avatar
    SNA committed
                    $comp .= ", table : '".$res_action['table_dest']."'";
                }
    
                
                
                if (isset($res_action['newResultId']) && !empty($res_action['newResultId'])) {
    
                    $comp .= ", newResultId : '".$res_action['newResultId']."'";
                }
    
                if (isset($res_action['action_status']) && !empty($res_action['action_status'])) {
    
                    $comp .= ", action_status : '".$res_action['action_status']."'";
                }
    
    SNA's avatar
    SNA committed
                $_SESSION['action_error'] = _ACTION_DONE.' : '.$label_action;
    
                echo "{status : 0, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'".$comp.", result_id : '".$res_action['result']."'}";
    
    SNA's avatar
    SNA committed
            }
        }
    
    
        require_once 'apps/maarch_entreprise/actions/docLocker.php';
        require_once("core".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."class_history.php");
        $hist = new history();
        $arr_res = explode('#', $res_action['result']);
    
        if (!is_array($res_action['history_msg'])) {
    
            $res_action['history_msg'] = [$res_action['history_msg']];
        }
        
    
        for ($i=0; $i<count($arr_res);$i++) {
            if (!empty($arr_res[$i])) {
                if (is_numeric($arr_res[$i])) {
    
                    $docLocker = new docLocker($arr_res[$i]);
    
                // Save action in history if needed
    
                    $what = '';
                    if (isset($_SESSION['current_basket']['id']) && !empty($_SESSION['current_basket']['id'])) {
    
                        if (isset($_SESSION['current_basket']['basket_owner']) && !empty($_SESSION['current_basket']['basket_owner'])) {
    
                            $pos =stripos($_SESSION['current_basket']['id'], $_SESSION['current_basket']['basket_owner']);
                            $string = substr($_SESSION['current_basket']['id'], 0, $pos -1);
                            $stmt = $db->query("SELECT basket_name FROM baskets WHERE basket_id = ?", array($string));
    
                            while ($data = $stmt->fetchObject()) {
                                $what = $data->basket_name;
                                $what .= " (".$_SESSION['current_basket']['basket_owner'].")";
    
                            $stmt = $db->query("SELECT basket_name FROM baskets WHERE basket_id = ?", array($_SESSION['current_basket']['id']));
    
                            while ($data = $stmt->fetchObject()) {
                                $what = $data->basket_name;
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    //$what .= $label_action.'('._NUM.$arr_res[$i].') ';
                    $what .= $label_action;
    
                    if (isset($res_action['history_msg'][$i]) && !empty($res_action['history_msg'][$i])) {
    
                        $what .= $res_action['history_msg'][$i];
    
    SNA's avatar
    SNA committed
                    }
    
    //                $_SESSION['info'] = $what . ' ';
    
                        $_POST['module'] = '';
                    }
    
    Giovannoni Laurent's avatar
    Giovannoni Laurent committed
                    $hist->add(
    
                        $_POST['table'],
    
                        $arr_res[$i],
                        'ACTION#'.$id_action,
                        $id_action,
                        $what,
                        $_SESSION['config']['databasetype'],
                        $_POST['module']
                    );
    
    Florian Azizian's avatar
    Florian Azizian committed
    
                    // SEND MESSAGE EXCHANGE REVIEW M2M
                    require_once "modules/sendmail/Controllers/MessageExchangeReviewController.php";
                    \Sendmail\Controllers\MessageExchangeReviewController::sendMessageExchangeReview(['res_id' => $arr_res[$i], 'action_id' => $id_action]);
    
    SNA's avatar
    SNA committed
                }
            }
        }
    
    SNA's avatar
    SNA committed
        exit();