Projet

Général

Profil

change_doctype.php

Henri QUENEAU, 09/07/2018 12:33

 
1
<?php
2
/*
3
*   Copyright 2008-2012 Maarch
4
*
5
*   This file is part of Maarch Framework.
6
*
7
*   Maarch Framework is free software: you can redistribute it and/or modify
8
*   it under the terms of the GNU General Public License as published by
9
*   the Free Software Foundation, either version 3 of the License, or
10
*   (at your option) any later version.
11
*
12
*   Maarch Framework is distributed in the hope that it will be useful,
13
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
*   GNU General Public License for more details.
16
*
17
*   You should have received a copy of the GNU General Public License
18
*   along with Maarch Framework.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20

    
21
/**
22
* @brief  Script called by an ajax object to process the document type 
23
* change during indexing (index_mlb.php), process limit date calcul and 
24
* possible services from apps or module
25
*
26
* @file change_doctype.php
27
* @author Claire Figueras <dev@maarch.org>
28
* @date $date$
29
* @version $Revision$
30
* @ingroup indexing_searching_mlb
31
*/
32
require_once('core/class/class_security.php');
33
require_once('apps/' . $_SESSION['config']['app_id'] . '/class/class_types.php');
34

    
35
$db = new Database();
36
$core = new core_tools();
37
$core->load_lang();
38
$type = new types();
39

    
40
if (!isset($_REQUEST['type_id']) || empty($_REQUEST['type_id'])) {
41
    $_SESSION['error'] = _DOCTYPE.' '._IS_EMPTY;
42
    echo "{status : 1, error_txt : '".addslashes(functions::xssafe($_SESSION['error']))."'}";
43
    exit();
44
}
45

    
46
if (!isset($_REQUEST['id_action']) || empty($_REQUEST['id_action'])) {
47
    $_SESSION['error'] = _ACTION_ID.' '._IS_EMPTY;
48
    echo "{status : 1, error_txt : '".addslashes(functions::xssafe($_SESSION['error']))."'}";
49
    exit();
50
}
51
$id_action = $_REQUEST['id_action'];
52

    
53
if (
54
    isset($_REQUEST['res_id']) 
55
    && !empty($_REQUEST['res_id']) 
56
    && isset($_REQUEST['coll_id']) 
57
    && !empty($_REQUEST['coll_id'])
58
) {
59
    $res_id = $_REQUEST['res_id'];
60
    $coll_id = $_REQUEST['coll_id'];
61
}
62
if ($coll_id == '') {
63
    $coll_id = $_REQUEST['coll_id'];
64
}
65
if ($coll_id == '') {
66
    $coll_id = 'letterbox_coll';
67
}
68
if ($res_id == '') {
69
    $res_id = $_REQUEST['res_id'];
70
}
71
if (isset($_REQUEST['admission_date']) 
72
    && !empty($_REQUEST['admission_date'])
73
) {
74
    $admissionDate = $_REQUEST['admission_date'];
75
}
76

    
77
$priorityId = $_SESSION['process_mode_priority'][$_SESSION['process_mode']];
78
$_SESSION['process_mode'] = NULL;
79
$priorityWDay = $_SESSION['mail_priorities_wdays'][$priorityId] == 'true' ? 'workingDay' : 'calendar';
80
foreach ($_SESSION['mail_priorities_id'] as $key => $value) {
81
    if ($value == $priorityId) {
82
        $fakeId = $key;
83
    }
84
}
85
if ($_SESSION['mail_priorities_attribute'][$fakeId] <> 'false') {
86
    $priorityDelay = $_SESSION['mail_priorities_attribute'][$fakeId];
87
}
88

    
89
// Process limit date calcul
90
//Bug fix if delay process is disabled in services
91
if ($core->service_is_enabled('param_mlb_doctypes')) {
92
    $stmt = $db->query("SELECT process_delay FROM " 
93
        . $_SESSION['tablename']['mlb_doctype_ext'] . " WHERE type_id = ?", 
94
        array($_REQUEST['type_id'])
95
    );
96

    
97
    $res = $stmt->fetchObject();
98
    $delay = $res->process_delay;
99
}
100
$mandatory_indexes = $type->get_mandatory_indexes($_REQUEST['type_id'], $coll_id);
101
$indexes = $type->get_indexes($_REQUEST['type_id'], $coll_id);
102

    
103
if(is_array($indexes)){
104
    foreach ($indexes as $key => $value) {
105
        if(filter_var($value['only_detail'], FILTER_VALIDATE_BOOLEAN)){
106
            unset($indexes[$key]);
107
        }
108
    }
109
}
110

    
111
$opt_indexes = '';
112
$display_value = 'table-row';
113
$opt_indexes  = '';
114
if(count($indexes) > 0)
115
{
116
    if((isset($res_id) && isset($coll_id)) && (!empty($res_id) && !empty($coll_id)))
117
    {
118
        $sec = new security();
119
        $table = $sec->retrieve_table_from_coll($coll_id);
120
        if(!empty($table))
121
        {
122
            $fields = 'res_id ';
123
            foreach(array_keys($indexes) as $key)
124
            {
125
                $fields .= ', '.$key;
126
            }
127
            $stmt = $db->query("SELECT " . $fields . " FROM " . $table 
128
                . " WHERE res_id = ?", array($res_id)
129
            );
130
            $values_fields = $stmt->fetchObject();
131
        }
132
    }
133
    $opt_indexes .= '<hr />';
134

    
135
    $opt_indexes .= '<h4 onclick="new Effect.toggle(\'doctype_fields\', \'blind\', {delay:0.2});'
136
        . 'whatIsTheDivStatus(\'doctype_fields\', \'divStatus_doctype_fields\');" '
137
        . 'class="categorie" style="width:90%;" onmouseover="this.style.cursor=\'pointer\';">';
138
    $opt_indexes .= ' <span id="divStatus_doctype_fields" style="color:#1C99C5;"><i class="fa fa-minus-square-o"></i></span>&nbsp;' 
139
        . _DOCTYPE_INDEXES;
140
    $opt_indexes .= '</h4>';
141

    
142
    $opt_indexes .= '<div id="doctype_fields"  style="display:inline">';
143
    $opt_indexes .= '<div>';
144
    
145
    $opt_indexes .= '<table width="100%" align="center" border="0">';
146
    foreach (array_keys($indexes) as $key) {
147
        $mandatory = false;
148
        if (in_array($key, $mandatory_indexes)) {
149
            $mandatory = true;
150
        }
151
            $opt_indexes .= '<tr >';
152
            $opt_indexes.='<td><label for="' . functions::xssafe($key) . '" class="form_title" >' 
153
                . $indexes[$key]['label'].'</label></td>';
154
            $opt_indexes .='<td>&nbsp;</td>';
155
            $opt_indexes .='<td class="indexing_field">';
156
            if ($indexes[$key]['type_field'] == 'input') {
157
                if ($indexes[$key]['type'] == 'date') {
158
                    $opt_indexes .='<input name="' . functions::xssafe($key) . '" type="text" id="' 
159
                        . $key . '" value="';
160
                    if (isset($values_fields->{$key})) {
161
                        $opt_indexes .= functions::format_date_db(
162
                            functions::xssafe($values_fields->{$key}), true
163
                        );
164
                    } elseif ($indexes[$key]['default_value'] <> false) {
165
                        $opt_indexes .= functions::format_date_db(
166
                            functions::xssafe($indexes[$key]['default_value']), true
167
                        );
168
                    }
169
                    $opt_indexes .= '" onclick="clear_error(\'frm_error_' 
170
                        . $id_action . '\');showCalender(this);"/>';
171
                } else {
172
                    $opt_indexes .= '<textarea name="'.functions::xssafe($key).'" type="text" id="'                     
173
                        . $key . '" value="';
174
                    if (isset($values_fields->{$key})) {
175
                        $valueCustom = null;
176
                        $valueCustom = $values_fields->{$key};
177
                        $opt_indexes .= functions::show_string(
178
                            functions::xssafe($values_fields->{$key}), true
179
                        );
180
                    } else if ($indexes[$key]['default_value'] <> false) {
181
                        $opt_indexes .= functions::show_string(
182
                            functions::xssafe($indexes[$key]['default_value']), true
183
                        );
184
                    }
185
                    $opt_indexes .= '" onclick="clear_error(\'frm_error_' 
186
                        . $id_action . '\');">'.$valueCustom.'</textarea>';
187
                }
188
            } else {
189
                $opt_indexes .= '<select name="'.functions::xssafe($key).'" id="'.functions::xssafe($key).'" >';
190
                    $opt_indexes .= '<option value="">'._CHOOSE.'...</option>';
191
                    for ($i=0; $i<count($indexes[$key]['values']);$i++) {
192
                        $opt_indexes .= '<option value="' 
193
                            . functions::xssafe($indexes[$key]['values'][$i]['id']) . '"';
194
                        if ($indexes[$key]['values'][$i]['id'] 
195
                            == $values_fields->{$key}) {
196
                            $opt_indexes .= 'selected="selected"';
197
                        } elseif (
198
                            $indexes[$key]['default_value'] <> false 
199
                            && $indexes[$key]['values'][$i]['id'] 
200
                                == $indexes[$key]['default_value']
201
                        ) {
202
                            $opt_indexes .= 'selected="selected"';
203
                        }
204
                        $opt_indexes .= ' >' . functions::xssafe($indexes[$key]['values'][$i]['label']) 
205
                            . '</option>';
206
                    }
207
                $opt_indexes .= '</select>';
208
            }
209
            $opt_indexes .='</td>';
210
            if ($mandatory) {
211
                $opt_indexes .='<td><span class="red_asterisk" id="' 
212
                    . $key . '_mandatory">';
213
                //$opt_indexes .= 'inline';
214
                $opt_indexes .= '*</span>&nbsp;</td>';
215
            } else {
216
                $opt_indexes .='<td><span style="visibility:hidden;" id="' 
217
                    . $key . '_mandatory">';
218
                $opt_indexes .= '*</span>&nbsp;</td>';
219
            }
220
            //$opt_indexes .= ';">*</span>&nbsp;</td>';
221
        $opt_indexes .= '</tr>';
222
    }
223
    $opt_indexes .= '</table>';
224
    
225
    $opt_indexes .= '</div></div>';
226
}
227

    
228
$services = '[';
229
$_SESSION['indexing_services'] = array();
230
$_SESSION['indexing_type_id'] = $_REQUEST['type_id'];
231
$_SESSION['category_id_session'] = $_SESSION['indexing_type_id'];
232

    
233
// Module and apps services
234
$core->execute_modules_services(
235
    $_SESSION['modules_services'], 'change_doctype.php', 'include'
236
);
237
$core->execute_app_services(
238
    $_SESSION['app_services'], 'change_doctype.php', 'include'
239
);
240
for ($i=0;$i< count($_SESSION['indexing_services']);$i++) {
241
    $services .= "{ script : '" . $_SESSION['indexing_services'][$i]['script'] 
242
        . "', function_to_execute : '" 
243
        . functions::xssafe($_SESSION['indexing_services'][$i]['function_to_execute'])
244
        . "', arguments : '[";
245
    for ($j=0;$j<count($_SESSION['indexing_services'][$i]['arguments']);$j++) {
246
        $services .= " { id : \'" 
247
            . functions::xssafe($_SESSION['indexing_services'][$i]['arguments'][$j]['id'])
248
            . "\', value : \'" 
249
            . addslashes(
250
                $_SESSION['indexing_services'][$i]['arguments'][$j]['value']
251
            )
252
            . "\' }, ";
253
    }
254
    $services = preg_replace('/, $/', '', $services);
255
    $services .= "]' }, ";
256
}
257
$services = preg_replace('/, $/', '', $services);
258
$services .= ']';
259
unset($_SESSION['indexing_type_id']);
260
unset($_SESSION['indexing_services']);
261

    
262
if ($priorityDelay <> '') {
263
    $delay = $priorityDelay;
264
}
265

    
266
if (isset($delay) && $delay > 0) {
267
    require_once('core/class/class_alert_engine.php');
268
    $alert_engine = new alert_engine();
269
    if (isset($admissionDate) && !empty($admissionDate)) {
270
        $convertedDate = $alert_engine->dateFR2Time(str_replace("-", "/", $admissionDate));
271
        $date = $alert_engine->WhenOpenDay($convertedDate, $delay, false, $priorityWDay);
272
        //$date = $alert_engine->date_max_treatment($delay, false);
273
    } else {
274
        $date = $alert_engine->date_max_treatment($delay, false);
275
    }
276
    $process_date = functions::dateformat($date, '-');
277
    $tmpProcessDate = explode(" ", $process_date);
278
    $date = $tmpProcessDate[0];
279
    
280
    echo "{status : 0, process_date : '" . trim(functions::xssafe($date)) 
281
        . "', opt_indexes : '" . addslashes($opt_indexes) . "', services : " 
282
        . $services . "}";
283
    exit();
284
} else {
285
    echo "{status : 1, opt_indexes : '" . addslashes($opt_indexes) 
286
        . "', services : " . $services . "}";
287
    exit();
288
}