absences_upgradeEntryElemDates::updateOneElem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.4285
1
<?php
2
/************************************************************************
3
 * OVIDENTIA http://www.ovidentia.org                                   *
4
 ************************************************************************
5
 * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr )              *
6
 *                                                                      *
7
 * This file is part of Ovidentia.                                      *
8
 *                                                                      *
9
 * Ovidentia is free software; you can redistribute it and/or modify    *
10
 * it under the terms of the GNU General Public License as published by *
11
 * the Free Software Foundation; either version 2, or (at your option)  *
12
 * any later version.													*
13
 *																		*
14
 * This program is distributed in the hope that it will be useful, but  *
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of			*
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.					*
17
 * See the  GNU General Public License for more details.				*
18
 *																		*
19
 * You should have received a copy of the GNU General Public License	*
20
 * along with this program; if not, write to the Free Software			*
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
22
 * USA.																	*
23
************************************************************************/
24
25
require_once dirname(__FILE__).'/../utilit/entry.class.php';
26
27
/**
28
 * add dates to entry_elem
29
 */
30
class absences_upgradeEntryElemDates
31
{
32
    private $totalCount = 0;
33
    private $currentUpdate = 1;
34
    
35
    private $progress;
36
    
37
    public function select()
38
    {
39
        global $babDB;
40
        
41
        $res = $babDB->db_query("SELECT ee.id_entry, ee.id, ee.quantity, e.date_begin, e.date_end, e.id_user, r.quantity_unit FROM
42
        	    absences_entries e,
43
        	    absences_entries_elem ee,
44
    	        absences_rights r,
45
    	        absences_types t
46
    	    WHERE
47
    	       ee.id_entry = e.id
48
    	       AND ee.date_begin = '0000-00-00 00:00:00' 
49
    	       AND r.id = ee.id_right
50
    	       AND t.id = r.id_type
51
    	    ORDER BY e.id, r.sortkey, t.name
52
    	");
53
        
54
        $this->totalCount = $babDB->db_num_rows($res);
55
        
56
        if ($this->totalCount > 0) {
57
            $this->progress = new bab_installProgressBar;
58
            $this->progress->setTitle(sprintf(absences_translate('Update existing vacation requests (%d)'), $this->totalCount));
59
        }
60
        
61
        $entry_stack = array();
62
    	while ($entry_elem = $babDB->db_fetch_assoc($res)) {
63
    	    
64
    	    $id_entry = (int) $entry_elem['id_entry'];
65
    	    
66
    	    if ('0000-00-00 00:00:00' === $entry_elem['date_begin'] || '0000-00-00 00:00:00' === $entry_elem['date_end']) {
67
    	        absences_Entry::getById($entry_elem['id_entry'])->delete();
68
    	        continue;
69
    	    }
70
    	    
71
    	    if (!isset($entry_stack[$id_entry])) {
72
    	        
73
    	        if (!empty($entry_stack)) {
74
    	            // process previous stack
75
    	           $this->updateEntry(reset($entry_stack));
76
    	           $entry_stack = array();
77
    	        }
78
    	        
79
    	        $entry_stack[$id_entry] = array();
80
    	    }
81
    	    
82
    	    $entry_stack[$id_entry][] = $entry_elem;
83
    	}
84
    	
85
    	// process last stack entry if exists
86
    	if ($entry_stack) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $entry_stack of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
    	   $this->updateEntry(reset($entry_stack));
88
    	}
89
    	
90
    	if (isset($this->progress)) {
91
    	    $this->progress->setProgression(100);
92
    	}
93
    }
94
    
95
    /**
96
     * @param array $arrentry      Elements of one entry
97
     */
98
    protected function updateEntry(Array $arrentry)
99
    {
100
        require_once dirname(__FILE__).'/../utilit/vacincl.php';
101
        require_once $GLOBALS['babInstallPath'].'utilit/dateTime.php';
102
        
103
        if (1 === count($arrentry)) {
104
            // one elem per entry
105
            
106
            return $this->updateOneElem($arrentry);
107
        }
108
109
        $row = reset($arrentry);
110
        $entry = absences_Entry::getById($row['id_entry']);
111
        
112
        $entry->loadElements();
113
        
114
        try {
115
            $entry->setElementsDates();
116
            $entry->saveElements();
117
        } catch (absences_EntryException $e) {
118
            bab_installWindow::message(bab_toHtml($e->getMessage()));
119
        }
120
        
121
        $percent = ($this->currentUpdate * 100) / $this->totalCount;
122
        $this->progress->setProgression(round($percent));
123
        
124
        $this->currentUpdate++;
125
    }
126
    
127
    /**
128
     * Copy entry dates to elem date
129
     */
130
    protected function updateOneElem(Array $arrentry)
131
    {
132
        global $babDB;
133
        
134
        $elem = reset($arrentry);
135
        
136
        $babDB->db_query('
137
            UPDATE absences_entries_elem 
138
            SET 
139
                date_begin='.$babDB->quote($elem['date_begin']).', 
140
                date_end='.$babDB->quote($elem['date_end']).' 
141
            WHERE id='.$babDB->quote($elem['id'])
142
        );
143
    }
144
    
145
    
146
    
147
    public static function onUpgrade()
148
    {
149
        $obj = new absences_upgradeEntryElemDates();
150
        $obj->select();   
151
    }
152
}
153
154