Skip to content
Snippets Groups Projects
manage_action.php 19.8 KiB
Newer Older
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
    $tab = explode('$$',$val);
    $values = array();
    for($i=0; $i<count($tab);$i++)
    {
        $tmp = explode('#', $tab[$i]);

        $val_tmp=array();
        for($idiese=1;$idiese<count($tmp);$idiese++){
                $val_tmp[]=$tmp[$idiese];
            }
            $valeurDiese = implode("#",$val_tmp);
SNA's avatar
SNA committed
        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));
Giovannoni Laurent's avatar
Giovannoni Laurent committed
    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'){
        $bool_history = $res->history;
    }else{
        $bool_history = 'N';
    }
SNA's avatar
SNA committed
    $create_id = $res->create_id;
SNA's avatar
SNA committed
        //No script defined for this action
    if($action_page == '')
    {
        $_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']))
    {
        $custom_path = 'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.$path_action_page;
    }
    if($custom_path <> '' && file_exists($_SESSION['config']['corepath'].$custom_path))
    {
        include($custom_path);
    }
    else
    {
        if(file_exists($path_action_page))
        {
            include($path_action_page);
        }
        else
        {
            // 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();
        }
    }


    $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();
    }
    else
    {
        if($create_id == 'N')
        {
            echo "{status : 0, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."', page_result : '', manage_form_now : false}";
SNA's avatar
SNA committed
        }
        else
        {
            echo "{status : 0, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."', page_result : '', manage_form_now : true}";
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])) {
                if ($_POST['table'] == 'folders') {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                    $query_str = "update " . $_POST['table'] 
                        .  " set status = ? where folders_system_id = ?";
                } else {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                    $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
else if(empty($_POST['values']) || !isset($_POST['action_id']) || empty($_POST['action_id']) ||
($_POST['mode'] <> 'mass' && $_POST['mode'] <> 'page')  || empty($_POST['table'])
SNA's avatar
SNA committed
|| 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'){
        $bool_history = $res->history;
    }else{
        $bool_history = 'N';
    }
SNA's avatar
SNA committed
    //No script defined for this action
    if($action_page == '')
    {
        //If second request : Error
        if($_POST['req'] == 'second_request')
        {
            $_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')
        {
            $_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) <> '') {
                    if ($_POST['table'] == 'folders') {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                        $query_str = "update " . $_POST['table'] 
                            .  " set status = ? where folders_system_id = ?";
                    } else {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                        $query_str = "update " . $_POST['table'] 
                            .  " 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;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                        echo "{status : 7, error_txt : '" . addslashes(functions::xssafe($label_action) 
                            . ' : ' . 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
    else
    {
        $custom_path = '';
        $path_action_page = $core->get_path_action_page($action_page);
SNA's avatar
SNA committed

SNA's avatar
SNA committed
        if(isset($_SESSION['custom_override_id']) && !empty($_SESSION['custom_override_id']))
        {
            $custom_path = 'custom'.DIRECTORY_SEPARATOR.$_SESSION['custom_override_id'].DIRECTORY_SEPARATOR.$path_action_page;
        }
        if($custom_path <> '' && file_exists($_SESSION['config']['corepath'].$custom_path))
        {
            include($custom_path);
        }
        else
        {
            if(file_exists($path_action_page))
            {
                include($path_action_page);
            }
            else
            {
                // 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();
            }
        }
SNA's avatar
SNA committed
        if($_POST['req'] == 'first_request' && in_array('form', $etapes))
        {
            $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 == 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)."'}";
SNA's avatar
SNA committed
        }
        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();
        }
SNA's avatar
SNA committed
        elseif( $_POST['req'] == 'first_request' && $confirm == true)
        {
            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
        else
        {
            if($confirm == false)
            {
                $_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']);
                            }
                        }
                        catch(Exception $e)
                        {
                            echo "{status : 9, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
SNA's avatar
SNA committed
                            exit();
                        }
                    }
                    else
                    {
                        echo "{status : 9, error_txt : '".addslashes(functions::xssafe($_SESSION['action_error']))."'}";
SNA's avatar
SNA committed
                        exit();
                    }
                }
            }
            //print_r($res_action);
            if($res_action == false)
            {
                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']))
            {
                $comp = ", page_result  : '".$res_action['page_result']."'";
            }
            if(isset($res_action['table_dest']) && !empty($res_action['table_dest']))
            {
                $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++)
SNA's avatar
SNA committed
    {
        if(!empty($arr_res[$i]))
SNA's avatar
SNA committed
        {
            if(is_numeric($arr_res[$i])){
                $docLocker = new docLocker($arr_res[$i]);
                $docLocker->unlock(); 
            }
            // Save action in history if needed
            if($bool_history=='Y')
SNA's avatar
SNA committed
            {
                $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()) {
Giovannoni Laurent's avatar
Giovannoni Laurent committed
                        $what = $data->basket_name;
                        $what .= " (".$_SESSION['current_basket']['basket_owner'].")";
                        }

                    }else{
                        $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]))
SNA's avatar
SNA committed
                {
                    $what .= $res_action['history_msg'][$i];
SNA's avatar
SNA committed
                }
                $_SESSION['info'] = $what . ' ';
                $_SESSION['cpt_info_basket'] = 0;
                if($_POST['module'] == 'null'){
                    $_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']);
SNA's avatar
SNA committed
            }
        }
    }
SNA's avatar
SNA committed
    exit();