filters.php ➔ filter_trans_set()   C
last analyzed

Complexity

Conditions 10
Paths 15

Size

Total Lines 50
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
nc 15
nop 2
dl 0
loc 50
rs 5.7647
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
//------------------------------------------------------------------------------
4
//
5
//  eTraxis - Records tracking web-based system
6
//  Copyright (C) 2005-2011  Artem Rodygin
7
//
8
//  This program is free software: you can redistribute it and/or modify
9
//  it under the terms of the GNU General Public License as published by
10
//  the Free Software Foundation, either version 3 of the License, or
11
//  (at your option) any later version.
12
//
13
//  This program is distributed in the hope that it will be useful,
14
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
//  GNU General Public License for more details.
17
//
18
//  You should have received a copy of the GNU General Public License
19
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//------------------------------------------------------------------------------
22
23
/**
24
 * Filters
25
 *
26
 * This module provides API to work with user filters.
27
 * See also {@link https://github.com/etraxis/etraxis-obsolete/wiki/tbl_filters tbl_filters} database table.
28
 *
29
 * @package DBO
30
 * @subpackage Filters
31
 */
32
33
/**#@+
34
 * Dependency.
35
 */
36
require_once('../engine/engine.php');
37
require_once('../dbo/accounts.php');
38
require_once('../dbo/fields.php');
39
require_once('../dbo/values.php');
40
/**#@-*/
41
42
//------------------------------------------------------------------------------
43
//  Definitions.
44
//------------------------------------------------------------------------------
45
46
/**#@+
47
 * Data restriction.
48
 */
49
define('MAX_FILTER_NAME', 50);
50
define('MAX_FSET_NAME',   50);
51
/**#@-*/
52
53
/**#@+
54
 * Filter type.
55
 */
56
define('FILTER_TYPE_ALL_PROJECTS',  1);
57
define('FILTER_TYPE_ALL_TEMPLATES', 2);
58
define('FILTER_TYPE_ALL_STATES',    3);
59
define('FILTER_TYPE_SEL_STATES',    4);
60
/**#@-*/
61
62
/**#@+
63
 * Filter flag.
64
 */
65
define('FILTER_FLAG_CREATED_BY',  0x0001);
66
define('FILTER_FLAG_ASSIGNED_TO', 0x0002);
67
define('FILTER_FLAG_UNCLOSED',    0x0004);
68
define('FILTER_FLAG_POSTPONED',   0x0008);
69
define('FILTER_FLAG_ACTIVE',      0x0010);
70
define('FILTER_FLAG_UNASSIGNED',  0x0020);
71
/**#@-*/
72
73
//------------------------------------------------------------------------------
74
//  Functions.
75
//------------------------------------------------------------------------------
76
77
/**
78
 * Finds in database and returns the information about specified filter.
79
 *
80
 * @param int $id Filter ID.
81
 * @return array Array with data if filter is found in database, FALSE otherwise.
82
 */
83
function filter_find ($id)
84
{
85
    debug_write_log(DEBUG_TRACE, '[filter_find]');
86
    debug_write_log(DEBUG_DUMP,  '[filter_find] $id = ' . $id);
87
88
    $rs = dal_query('filters/fndid.sql', $id, $_SESSION[VAR_USERID]);
89
90
    return ($rs->rows == 0 ? FALSE : $rs->fetch());
0 ignored issues
show
Documentation introduced by
The property $rows is declared protected in CRecordset. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
91
}
92
93
/**
94
 * Returns {@link CRecordset DAL recordset} which contains all existing filters of current user.
95
 *
96
 * @param int $id Account ID.
97
 * @param bool $active Whether to return all filters, or active only.
98
 * @param int &$sort Sort mode (used as output only). The function retrieves current sort mode from
99
 * client cookie ({@link COOKIE_FILTERS_SORT}) and updates it, if it's out of valid range.
100
 * @param int &$page Number of current page tab (used as output only). The function retrieves current
101
 * page from client cookie ({@link COOKIE_FILTERS_PAGE}) and updates it, if it's out of valid range.
102
 * @return CRecordset Recordset with list of filters.
103
 */
104
function filters_list ($id, $active, &$sort, &$page)
105
{
106
    debug_write_log(DEBUG_TRACE, '[filters_list]');
107
    debug_write_log(DEBUG_DUMP,  '[filters_list] $id     = ' . $id);
108
    debug_write_log(DEBUG_DUMP,  '[filters_list] $active = ' . $active);
109
110
    $sort_modes = array
111
    (
112
        1 => 'filter_name asc',
113
        2 => 'fullname asc, username asc, filter_name asc',
114
        3 => 'filter_name desc',
115
        4 => 'fullname desc, username desc, filter_name desc',
116
    );
117
118
    $sort = try_request('sort', try_cookie(COOKIE_FILTERS_SORT, 1));
119
    $sort = ustr2int($sort, 1, count($sort_modes));
120
121
    $page = try_request('page', try_cookie(COOKIE_FILTERS_PAGE));
122
    $page = ustr2int($page, 1, MAXINT);
123
124
    save_cookie(COOKIE_FILTERS_SORT, $sort);
125
    save_cookie(COOKIE_FILTERS_PAGE, $page);
126
127
    return dal_query($active ? 'filters/lista.sql' : 'filters/list.sql', $id, $sort_modes[$sort]);
128
}
129
130
/**
131
 * Validates filter information before creation or modification.
132
 *
133
 * @param string $filter_name Filter name.
134
 * @return int Error code:
135
 * <ul>
136
 * <li>{@link NO_ERROR} - data are valid</li>
137
 * <li>{@link ERROR_INCOMPLETE_FORM} - at least one of required field is empty</li>
138
 * </ul>
139
 */
140 View Code Duplication
function filter_validate ($filter_name)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
{
142
    debug_write_log(DEBUG_TRACE, '[filter_validate]');
143
    debug_write_log(DEBUG_DUMP,  '[filter_validate] $filter_name = ' . $filter_name);
144
145
    if (ustrlen($filter_name) == 0)
146
    {
147
        debug_write_log(DEBUG_NOTICE, '[filter_validate] At least one required field is empty.');
148
        return ERROR_INCOMPLETE_FORM;
149
    }
150
151
    return NO_ERROR;
152
}
153
154
/**
155
 * Creates new filter.
156
 *
157
 * @param string $filter_name Filter name.
158
 * @param string $filter_type Filter type.
159
 * @param string $filter_flags Filter flags.
160
 * @param string $filter_param Filter parameter.
161
 * @return int Error code:
162
 * <ul>
163
 * <li>{@link NO_ERROR} - filter is successfully created</li>
164
 * <li>{@link ERROR_ALREADY_EXISTS} - filter with specified name already exists</li>
165
 * <li>{@link ERROR_NOT_FOUND} - failure on attempt to create filter</li>
166
 * </ul>
167
 */
168
function filter_create ($filter_name, $filter_type, $filter_flags, $filter_param = NULL)
169
{
170
    debug_write_log(DEBUG_TRACE, '[filter_create]');
171
    debug_write_log(DEBUG_DUMP,  '[filter_create] $filter_name  = ' . $filter_name);
172
    debug_write_log(DEBUG_DUMP,  '[filter_create] $filter_type  = ' . $filter_type);
173
    debug_write_log(DEBUG_DUMP,  '[filter_create] $filter_flags = ' . $filter_flags);
174
    debug_write_log(DEBUG_DUMP,  '[filter_create] $filter_param = ' . $filter_param);
175
176
    // Check that user doesn't have another filter with the same name.
177
    $rs = dal_query('filters/fndk.sql', $_SESSION[VAR_USERID], ustrtolower($filter_name));
178
179
    if ($rs->rows != 0)
0 ignored issues
show
Documentation introduced by
The property $rows is declared protected in CRecordset. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
180
    {
181
        debug_write_log(DEBUG_NOTICE, '[filter_create] Filter already exists.');
182
        return ERROR_ALREADY_EXISTS;
183
    }
184
185
    // Create a filter.
186
    dal_query('filters/create.sql',
187
              $_SESSION[VAR_USERID],
188
              $filter_name,
189
              $filter_type,
190
              $filter_flags,
191
              is_null($filter_param) ? NULL : $filter_param);
192
193
    // Find newly created filter.
194
    $rs = dal_query('filters/fndk.sql', $_SESSION[VAR_USERID], ustrtolower($filter_name));
195
196
    if ($rs->rows == 0)
0 ignored issues
show
Documentation introduced by
The property $rows is declared protected in CRecordset. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
197
    {
198
        debug_write_log(DEBUG_WARNING, '[filter_create] Created filter not found.');
199
        return ERROR_NOT_FOUND;
200
    }
201
202
    // Enable new filter.
203
    dal_query('filters/set.sql', $rs->fetch('filter_id'), $_SESSION[VAR_USERID]);
204
205
    return NO_ERROR;
206
}
207
208
/**
209
 * Modifies specified filter.
210
 *
211
 * @param int $id ID of filter to be modified.
212
 * @param string $filter_name New filter name.
213
 * @param string $filter_type New filter type.
214
 * @param string $filter_flags New filter flags.
215
 * @return int Error code:
216
 * <ul>
217
 * <li>{@link NO_ERROR} - filter is successfully modified</li>
218
 * <li>{@link ERROR_ALREADY_EXISTS} - filter with specified name already exists</li>
219
 * </ul>
220
 */
221
function filter_modify ($id, $filter_name, $filter_type, $filter_flags)
222
{
223
    debug_write_log(DEBUG_TRACE, '[filter_modify]');
224
    debug_write_log(DEBUG_DUMP,  '[filter_modify] $id           = ' . $id);
225
    debug_write_log(DEBUG_DUMP,  '[filter_modify] $filter_name  = ' . $filter_name);
226
    debug_write_log(DEBUG_DUMP,  '[filter_modify] $filter_type  = ' . $filter_type);
227
    debug_write_log(DEBUG_DUMP,  '[filter_modify] $filter_flags = ' . $filter_flags);
228
229
    // Check that user doesn't have another filter with the same name, besides this one.
230
    $rs = dal_query('filters/fndku.sql', $id, $_SESSION[VAR_USERID], ustrtolower($filter_name));
231
232
    if ($rs->rows != 0)
0 ignored issues
show
Documentation introduced by
The property $rows is declared protected in CRecordset. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
233
    {
234
        debug_write_log(DEBUG_NOTICE, '[filter_modify] Filter already exists.');
235
        return ERROR_ALREADY_EXISTS;
236
    }
237
238
    // Modify the filter.
239
    dal_query('filters/modify.sql',
240
              $id,
241
              $filter_name,
242
              $filter_type,
243
              $filter_flags);
244
245
    return NO_ERROR;
246
}
247
248
/**
249
 * Enables specified filters.
250
 *
251
 * @param array $filters List of filter IDs.
252
 * @return int Always {@link NO_ERROR}.
253
 */
254
function filters_set ($filters)
255
{
256
    debug_write_log(DEBUG_TRACE, '[filters_set]');
257
258
    // Enable each of specified filters.
259
    foreach ($filters as $filter)
260
    {
261
        dal_query('filters/clear.sql', $filter, $_SESSION[VAR_USERID]);
262
        dal_query('filters/set.sql',   $filter, $_SESSION[VAR_USERID]);
263
    }
264
265
    return NO_ERROR;
266
}
267
268
/**
269
 * Disables specified filters.
270
 *
271
 * @param array $filters List of filter IDs (NULL to disable them all).
272
 * @return int Always {@link NO_ERROR}.
273
 */
274
function filters_clear ($filters = NULL)
275
{
276
    debug_write_log(DEBUG_TRACE, '[filters_clear]');
277
278
    if (is_null($filters))
279
    {
280
        // Disable all filters.
281
        dal_query('filters/clearall.sql', $_SESSION[VAR_USERID]);
282
    }
283
    else
284
    {
285
        // Disable each of specified filters.
286
        foreach ($filters as $filter)
287
        {
288
            dal_query('filters/clear.sql', $filter, $_SESSION[VAR_USERID]);
289
        }
290
    }
291
292
    return NO_ERROR;
293
}
294
295
/**
296
 * Checks whether a filter is activated.
297
 *
298
 * @param int $id ID of filter to be checked.
299
 * @return bool TRUE if template can be deleted, FALSE otherwise.
300
 */
301
function is_filter_activated ($id)
302
{
303
    debug_write_log(DEBUG_TRACE, '[is_filter_activated]');
304
    debug_write_log(DEBUG_DUMP,  '[is_filter_activated] $id = ' . $id);
305
306
    $rs = dal_query('filters/check.sql', $id, $_SESSION[VAR_USERID]);
307
308
    return ($rs->fetch(0) != 0);
309
}
310
311
/**
312
 * Deletes specified filters.
313
 *
314
 * @param array $filters List of filter IDs.
315
 * @return int Always {@link NO_ERROR}.
316
 */
317
function filters_delete ($filters)
318
{
319
    debug_write_log(DEBUG_TRACE, '[filters_delete]');
320
321
    foreach ($filters as $filter)
322
    {
323
        dal_query('filters/ffdelall.sql',  $filter, $_SESSION[VAR_USERID]);
324
        dal_query('filters/ftdelall.sql',  $filter, $_SESSION[VAR_USERID]);
325
        dal_query('filters/fsdelall.sql',  $filter, $_SESSION[VAR_USERID]);
326
        dal_query('filters/fadelall.sql',  $filter, $_SESSION[VAR_USERID]);
327
        dal_query('filters/fa2delall.sql', $filter);
328
        dal_query('filters/vdelall.sql',   $filter);
329
        dal_query('filters/fshdelall.sql', $filter, $_SESSION[VAR_USERID]);
330
        dal_query('filters/delete.sql',    $filter, $_SESSION[VAR_USERID]);
331
    }
332
333
    return NO_ERROR;
334
}
335
336
/**
337
 * @ignore
338
 */
339 View Code Duplication
function filter_states_get ($filter_id, $template_id)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
340
{
341
    debug_write_log(DEBUG_TRACE, '[filter_states_set]');
342
    debug_write_log(DEBUG_DUMP,  '[filter_states_set] $filter_id   = ' . $filter_id);
343
    debug_write_log(DEBUG_DUMP,  '[filter_states_set] $template_id = ' . $template_id);
344
345
    $states = array();
346
347
    $rs = dal_query('filters/fslist.sql', $filter_id, $template_id);
348
349
    while (($row = $rs->fetch()))
350
    {
351
        array_push($states, $row['state_id']);
352
    }
353
354
    return $states;
355
}
356
357
/**
358
 * @ignore
359
 */
360
function filter_trans_set ($filter_id, $template_id)
361
{
362
    debug_write_log(DEBUG_TRACE, '[filter_trans_set]');
363
    debug_write_log(DEBUG_DUMP,  '[filter_trans_set] $filter_id   = ' . $filter_id);
364
    debug_write_log(DEBUG_DUMP,  '[filter_trans_set] $template_id = ' . $template_id);
365
366
    dal_query('filters/ftdelall.sql', $filter_id, $_SESSION[VAR_USERID]);
367
    $rs = dal_query('states/list.sql', $template_id, 'state_type, state_name');
368
369
    while (($row = $rs->fetch()))
370
    {
371
        $name = 'state' . $row['state_id'];
372
373
        if (isset($_REQUEST[$name]))
374
        {
375
            debug_write_log(DEBUG_NOTICE, "[filter_trans_set] Found filter for state #{$row['state_id']}.");
376
377
            $min_value = ustrcut(try_request('min_' . $name), ustrlen(get_date(SAMPLE_DATE)));
378
            $max_value = ustrcut(try_request('max_' . $name), ustrlen(get_date(SAMPLE_DATE)));
379
380
            if (ustrlen($min_value) == 0 && ustrlen($max_value) == 0)
381
            {
382
                debug_write_log(DEBUG_NOTICE, '[filter_trans_set] At least one of range values must be set.');
383
            }
384
            else
385
            {
386
                $min_date = (ustrlen($min_value) == 0 ? MIN_FIELD_DATE : ustr2date($min_value));
387
                $max_date = (ustrlen($max_value) == 0 ? MAX_FIELD_DATE : ustr2date($max_value));
388
389
                if ($min_date == -1 || $max_date == -1)
390
                {
391
                    debug_write_log(DEBUG_NOTICE, '[filter_trans_set] At least one of range values is invalid.');
392
                }
393
                else
394
                {
395
                    if ($min_date > $max_date)
396
                    {
397
                        swap($min_date, $max_date);
398
                    }
399
400
                    dal_query('filters/ftcreate.sql',
401
                              $filter_id,
402
                              $row['state_id'],
403
                              $min_date,
404
                              $max_date);
405
                }
406
            }
407
        }
408
    }
409
}
410
411
/**
412
 * @ignore
413
 */
414
function filter_fields_set ($filter_id, $template_id)
415
{
416
    debug_write_log(DEBUG_TRACE, '[filter_fields_set]');
417
    debug_write_log(DEBUG_DUMP,  '[filter_fields_set] $filter_id   = ' . $filter_id);
418
    debug_write_log(DEBUG_DUMP,  '[filter_fields_set] $template_id = ' . $template_id);
419
420
    dal_query('filters/ffdelall.sql', $filter_id, $_SESSION[VAR_USERID]);
421
    $rs = dal_query('states/list.sql', $template_id, 'state_type, state_name');
422
423
    while (($row = $rs->fetch()))
424
    {
425
        $rsf = dal_query('filters/flist.sql',
426
                         $row['state_id'],
427
                         $_SESSION[VAR_USERID],
428
                         FIELD_ALLOW_TO_READ);
429
430
        while (($row = $rsf->fetch()))
431
        {
432
            $name = 'field' . $row['field_id'];
433
434
            if (isset($_REQUEST[$name]))
435
            {
436
                debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Found filter for field #{$row['field_id']}.");
437
438
                switch ($row['field_type'])
439
                {
440
                    case FIELD_TYPE_NUMBER:
441
442
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is number.");
443
444
                        $min_value = ustrcut(try_request('min_' . $name), ustrlen(MAX_FIELD_INTEGER) + 1);
445
                        $max_value = ustrcut(try_request('max_' . $name), ustrlen(MAX_FIELD_INTEGER) + 1);
446
447
                        if (ustrlen($min_value) == 0)
448
                        {
449
                            $min_value = NULL;
450
                        }
451
452
                        if (ustrlen($max_value) == 0)
453
                        {
454
                            $max_value = NULL;
455
                        }
456
457
                        if (!is_null($min_value) && !is_intvalue($min_value) ||
458
                            !is_null($max_value) && !is_intvalue($max_value))
459
                        {
460
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.');
461
                        }
462
                        else
463
                        {
464
                            if (!is_null($min_value) && !is_null($max_value) && ($min_value > $max_value))
465
                            {
466
                                swap($min_value, $max_value);
467
                            }
468
469
                            dal_query('filters/ffcreate.sql',
470
                                      $filter_id,
471
                                      $row['field_id'],
472
                                      is_null($min_value) ? NULL : $min_value,
473
                                      is_null($max_value) ? NULL : $max_value);
474
                        }
475
476
                        break;
477
478
                    case FIELD_TYPE_FLOAT:
479
480
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is float.");
481
482
                        $min_value = ustrcut(try_request('min_' . $name), ustrlen(MIN_FIELD_FLOAT));
483
                        $max_value = ustrcut(try_request('max_' . $name), ustrlen(MAX_FIELD_FLOAT));
484
485
                        if (ustrlen($min_value) == 0)
486
                        {
487
                            $min_value = NULL;
488
                        }
489
490
                        if (ustrlen($max_value) == 0)
491
                        {
492
                            $max_value = NULL;
493
                        }
494
495
                        if (!is_null($min_value) && !is_floatvalue($min_value) ||
496
                            !is_null($max_value) && !is_floatvalue($max_value))
497
                        {
498
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.');
499
                        }
500
                        else
501
                        {
502
                            if (!is_null($min_value) && !is_null($max_value) && (bccomp($min_value, $max_value) > 0))
503
                            {
504
                                swap($min_value, $max_value);
505
                            }
506
507
                            dal_query('filters/ffcreate.sql',
508
                                      $filter_id,
509
                                      $row['field_id'],
510
                                      is_null($min_value) ? NULL : value_find_float($min_value),
511
                                      is_null($max_value) ? NULL : value_find_float($max_value));
512
                        }
513
514
                        break;
515
516
                    case FIELD_TYPE_STRING:
517
                    case FIELD_TYPE_MULTILINED:
518
519
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is string/multilined.");
520
521
                        $value = ustrcut(try_request('edit_' . $name), MAX_FIELD_STRING);
522
523
                        if (ustrlen($value) == 0)
524
                        {
525
                            $value = NULL;
526
                        }
527
528
                        $param = value_find_string($value);
529
530
                        dal_query('filters/ffcreate.sql',
531
                                  $filter_id,
532
                                  $row['field_id'],
533
                                  is_null($param) ? NULL : $param,
534
                                  NULL);
535
536
                        break;
537
538
                    case FIELD_TYPE_CHECKBOX:
539
540
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is checkbox.");
541
542
                        $checked = try_request('check_' . $name, 0);
543
544
                        dal_query('filters/ffcreate.sql',
545
                                  $filter_id,
546
                                  $row['field_id'],
547
                                  bool2sql($checked),
548
                                  NULL);
549
550
                        break;
551
552 View Code Duplication
                    case FIELD_TYPE_LIST:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
553
554
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is list.");
555
556
                        $value = ustrcut(try_request('list_' . $name), ustrlen(MAXINT));
557
558
                        if (ustrlen($value) == 0)
559
                        {
560
                            $value = NULL;
561
                        }
562
563
                        if (!is_null($value) && !is_intvalue($value))
564
                        {
565
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer value.');
566
                        }
567
                        else
568
                        {
569
                            if (!is_null($value) && ($value < 1 || $value > MAXINT))
570
                            {
571
                                debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer range.');
572
                            }
573
                            else
574
                            {
575
                                dal_query('filters/ffcreate.sql',
576
                                          $filter_id,
577
                                          $row['field_id'],
578
                                          is_null($value) ? NULL : $value,
579
                                          NULL);
580
                            }
581
                        }
582
583
                        break;
584
585 View Code Duplication
                    case FIELD_TYPE_RECORD:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
586
587
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is record.");
588
589
                        $value = ustrcut(try_request('edit_' . $name), ustrlen(MAXINT));
590
591
                        if (ustrlen($value) == 0)
592
                        {
593
                            $value = NULL;
594
                        }
595
596
                        if (!is_null($value) && !is_intvalue($value))
597
                        {
598
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid record ID.');
599
                        }
600
                        else
601
                        {
602
                            if (!is_null($value) && ($value < 1 || $value > MAXINT))
603
                            {
604
                                debug_write_log(DEBUG_NOTICE, '[filter_fields_set] Invalid integer range.');
605
                            }
606
                            else
607
                            {
608
                                dal_query('filters/ffcreate.sql',
609
                                          $filter_id,
610
                                          $row['field_id'],
611
                                          is_null($value) ? NULL : $value,
612
                                          NULL);
613
                            }
614
                        }
615
616
                        break;
617
618 View Code Duplication
                    case FIELD_TYPE_DATE:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
619
620
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is date.");
621
622
                        $min_value = ustrcut(try_request('min_' . $name), ustrlen(get_date(SAMPLE_DATE)));
623
                        $max_value = ustrcut(try_request('max_' . $name), ustrlen(get_date(SAMPLE_DATE)));
624
625
                        if (ustrlen($min_value) == 0)
626
                        {
627
                            $min_date = NULL;
628
                        }
629
                        else
630
                        {
631
                            $min_date = ustr2date($min_value);
632
                        }
633
634
                        if (ustrlen($max_value) == 0)
635
                        {
636
                            $max_date = NULL;
637
                        }
638
                        else
639
                        {
640
                            $max_date = ustr2date($max_value);
641
                        }
642
643
                        if (!is_null($min_date) && $min_date == -1 ||
644
                            !is_null($max_date) && $max_date == -1)
645
                        {
646
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.');
647
                        }
648
                        else
649
                        {
650
                            if (!is_null($min_date) && !is_null($max_date) && ($min_date > $max_date))
651
                            {
652
                                swap($min_date, $max_date);
653
                            }
654
655
                            dal_query('filters/ffcreate.sql',
656
                                      $filter_id,
657
                                      $row['field_id'],
658
                                      is_null($min_date) ? NULL : $min_date,
659
                                      is_null($max_date) ? NULL : $max_date);
660
                        }
661
662
                        break;
663
664 View Code Duplication
                    case FIELD_TYPE_DURATION:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
665
666
                        debug_write_log(DEBUG_NOTICE, "[filter_fields_set] Field type is duration.");
667
668
                        $min_value = ustrcut(try_request('min_' . $name), ustrlen(time2ustr(MAX_FIELD_DURATION)));
669
                        $max_value = ustrcut(try_request('max_' . $name), ustrlen(time2ustr(MAX_FIELD_DURATION)));
670
671
                        if (ustrlen($min_value) == 0)
672
                        {
673
                            $min_time = NULL;
674
                        }
675
                        else
676
                        {
677
                            $min_time = ustr2time($min_value);
678
                        }
679
680
                        if (ustrlen($max_value) == 0)
681
                        {
682
                            $max_time = NULL;
683
                        }
684
                        else
685
                        {
686
                            $max_time = ustr2time($max_value);
687
                        }
688
689
                        if (!is_null($min_time) && $min_time == -1 ||
690
                            !is_null($max_time) && $max_time == -1)
691
                        {
692
                            debug_write_log(DEBUG_NOTICE, '[filter_fields_set] At least one of range values is invalid.');
693
                        }
694
                        else
695
                        {
696
                            if (!is_null($min_time) && !is_null($max_time) && ($min_time > $max_time))
697
                            {
698
                                swap($min_time, $max_time);
699
                            }
700
701
                            dal_query('filters/ffcreate.sql',
702
                                      $filter_id,
703
                                      $row['field_id'],
704
                                      is_null($min_time) ? NULL : $min_time,
705
                                      is_null($max_time) ? NULL : $max_time);
706
                        }
707
708
                        break;
709
710
                    default:
711
                        debug_write_log(DEBUG_WARNING, '[filter_fields_set] Unknown field type = ' . $row['field_type']);
712
                }
713
            }
714
        }
715
    }
716
717
    return NO_ERROR;
718
}
719
720
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
721