Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / reservation.class.php @ b67d8923

Historique | Voir | Annoter | Télécharger (9,27 ko)

1
<?php
2
/*
3
 * @version $Id: reservation.class.php 7875 2009-01-23 15:16:47Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file: Julien Dombre
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36
if (!defined('GLPI_ROOT')){
37
        die("Sorry. You can't access directly to this file");
38
        }
39

    
40

    
41
/// Reservation item class
42
class ReservationItem extends CommonDBTM {
43
        /**
44
         * Constructor
45
        **/
46
        function ReservationItem () {
47
                $this->table="glpi_reservation_item";
48
                $this->type=-1;
49
        }
50

    
51
        /**
52
         * Retrieve an item from the database for a specific item
53
         *
54
         *@param $ID ID of the item
55
         *@param $type type of the item
56
         *@return true if succeed else false
57
        **/        
58
        function getFromDBbyItem($type,$ID){
59
                global $DB;
60

    
61
                $query = "SELECT * FROM glpi_reservation_item WHERE (device_type = '$type' AND id_device = '$ID')";
62
                if ($result = $DB->query($query)) {
63
                        if ($DB->numrows($result)==1){
64
                                $this->fields = $DB->fetch_assoc($result);
65
                                return true;
66
                        }
67
                }
68
                return false;
69

    
70
        }
71
        
72
        function cleanDBonPurge($ID) {
73

    
74
                global $DB;
75

    
76
                $query2 = "DELETE FROM glpi_reservation_resa WHERE (id_item = '$ID')";
77
                $result2 = $DB->query($query2);
78
        }
79
        function prepareInputForAdd($input) {
80
                if (!$this->getFromDBbyItem($input['device_type'],$input['id_device'])){ 
81
                        if (!isset($input['active'])){
82
                                $input['active']=1;
83
                        }
84
                        return $input;
85
                }
86
                return false; 
87
        }
88
}
89

    
90
/// Reservation class
91
class ReservationResa extends CommonDBTM {
92

    
93
        /**
94
         * Constructor
95
        **/
96
        function ReservationResa () {
97
                $this->table="glpi_reservation_resa";
98
                $this->type=-1;
99
        }
100

    
101
        function pre_deleteItem($ID) {
102
                global $CFG_GLPI;
103
                if ($this->getFromDB($ID))
104
                        if (isset($this->fields["id_user"])&&($this->fields["id_user"]==$_SESSION["glpiID"]||haveRight("reservation_central","w"))){
105
                                // Processing Email
106
                                if ($CFG_GLPI["mailing"]){
107
                                        $mail = new MailingResa($this,"delete");
108
                                        $mail->send();
109
                                }
110

    
111
                }
112
                return true;
113
        }
114

    
115

    
116
        function update($input,$history=1){
117
                global $LANG,$CFG_GLPI;
118
                // Update a printer in the database
119

    
120
                $target="";
121
                if (isset($input['_target'])){
122
                        $target=$input['_target'];
123
                }
124
                $item=0;
125
                if (isset($input['_item'])){
126
                        $item=$_POST['_item'];
127
                }
128

    
129
                $this->getFromDB($input["ID"]);
130

    
131
                list($begin_year,$begin_month,$begin_day)=split("-",$input["begin_date"]);
132
                list($end_year,$end_month,$end_day)=split("-",$input["end_date"]);
133

    
134
                list($begin_hour,$begin_min)=split(":",$input["begin_hour"]);
135
                list($end_hour,$end_min)=split(":",$input["end_hour"]);
136
                $input["begin"]=date("Y-m-d H:i:00",mktime($begin_hour,$begin_min,0,$begin_month,$begin_day,$begin_year));
137
                $input["end"]=date("Y-m-d H:i:00",mktime($end_hour,$end_min,0,$end_month,$end_day,$end_year));
138

    
139

    
140
                // Fill the update-array with changes
141
                $x=0;
142
                foreach ($input as $key => $val) {
143
                        if (array_key_exists($key,$this->fields) && $this->fields[$key] != $input[$key]) {
144
                                $this->fields[$key] = $input[$key];
145
                                $updates[$x] = $key;
146
                                $x++;
147
                        }
148
                }
149

    
150
                if (!$this->test_valid_date()){
151
                        $this->displayError("date",$item,$target);
152
                        return false;
153
                }
154

    
155
                if ($this->is_reserved()){
156
                        $this->displayError("is_res",$item,$target);
157
                        return false;
158
                }
159

    
160

    
161
                if (isset($updates)){
162
                        $this->updateInDB($updates);
163
                        // Processing Email
164
                        if ($CFG_GLPI["mailing"]){
165
                                $mail = new MailingResa($this,"update");
166
                                $mail->send();
167
                        }
168
                }
169
                return true;
170
        }
171

    
172
        function add($input){
173
                global $CFG_GLPI;
174
                       
175
                // Add a Reservation
176
                if (!isset($input['_ok'])||$input['_ok']){
177
                        $target="";
178
                        if (isset($input['_target'])){
179
                                $target=$input['_target'];
180
                        }
181
                        // set new date.
182
                        $this->fields["id_item"] = $input["id_item"];
183
                        $this->fields["comment"] = $input["comment"];
184
                        $this->fields["id_user"] = $input["id_user"];
185
                        $this->fields["begin"] = $input["begin_date"]." ".$input["begin_hour"].":00";
186
                        $this->fields["end"] = $input["end_date"]." ".$input["end_hour"].":00";
187

    
188
                        if (!$this->test_valid_date()){
189
                                $this->displayError("date",$input["id_item"],$target);
190
                                return false;
191
                        }
192

    
193
                        if ($this->is_reserved()){
194
                                $this->displayError("is_res",$input["id_item"],$target);
195
                                return false;
196
                        }
197

    
198
                        if ($input["id_user"]>0)
199
                                if ($this->addToDB()){
200
                                        // Processing Email
201
                                        if ($CFG_GLPI["mailing"]){
202
                                                $mail = new MailingResa($this,"new");
203
                                                $mail->send();
204
                                        }
205
                                        return true;
206
                                } else {
207
                                        return false;
208
                                }
209
                }
210
        }
211

    
212

    
213
        // SPECIFIC FUNCTIONS
214
        /**
215
         * Is the item already reserved ?
216
         *
217
         *@return boolean
218
         **/
219
        function is_reserved(){
220
                global $DB;
221
                if (!isset($this->fields["id_item"])||empty($this->fields["id_item"]))
222
                        return true;
223

    
224
                // When modify a reservation do not itself take into account 
225
                $ID_where="";
226
                if(isset($this->fields["ID"]))
227
                        $ID_where=" (ID <> '".$this->fields["ID"]."') AND ";
228

    
229
                $query = "SELECT * FROM glpi_reservation_resa".
230
                        " WHERE $ID_where (id_item = '".$this->fields["id_item"]."') AND ( ('".$this->fields["begin"]."' < begin AND '".$this->fields["end"]."' > begin) OR ('".$this->fields["begin"]."' < end AND '".$this->fields["end"]."' >= end) OR ('".$this->fields["begin"]."' >= begin AND '".$this->fields["end"]."' < end))";
231
                //                echo $query."<br>";
232
                if ($result=$DB->query($query)){
233
                        return ($DB->numrows($result)>0);
234
                }
235
                return true;
236
        }
237
        /**
238
         * Current dates are valid ? begin before end
239
         *
240
         *@return boolean
241
         **/
242
        function test_valid_date(){
243
                return (strtotime($this->fields["begin"])<strtotime($this->fields["end"]));
244
        }
245

    
246
        /**
247
         * display error message 
248
         * @param $type error type : date / is_res / other
249
         * @param $ID ID of the item
250
         * @param $target where to go on error
251
         *@return nothing
252
         **/
253
        function displayError($type,$ID,$target){
254
                global $LANG;
255

    
256
                echo "<br><div class='center'>";
257
                switch ($type){
258
                        case "date":
259
                                echo $LANG["planning"][1];
260
                        break;
261
                        case "is_res":
262
                                echo $LANG["reservation"][18];
263
                        break;
264
                        default :
265
                        echo "Unknown error";
266
                        break;
267
                }
268
                echo "<br><a href='".$target."?show=resa&amp;ID=$ID'>".$LANG["reservation"][20]."</a>";
269
                echo "</div>";
270
        }
271
        /**
272
         * Get text describing reservation
273
         * 
274
        * @param $format text or html
275
         */
276
        function textDescription($format="text"){
277
                global $LANG;
278

    
279
                $ri=new ReservationItem();
280
                $ci=new CommonItem();
281
                $name="";
282
                $tech="";
283
                if ($ri->getFromDB($this->fields["id_item"])){
284
                        if ($ci->getFromDB($ri->fields['device_type'],$ri->fields['id_device'])        ){
285
                                $name=$ci->getType()." ".$ci->getName();
286
                                if ($ci->getField('tech_num')){
287
                                        $tech=getUserName($ci->getField('tech_num'));
288
                                }
289
                        }
290
                }
291
                
292
                $u=new User();
293
                $u->getFromDB($this->fields["id_user"]);
294
                $content="";
295

    
296
                if($format=="html"){
297
                        $content= "<html><head> <style type=\"text/css\">";
298
                        $content.=".description{ color: inherit; background: #ebebeb; border-style: solid; border-color: #8d8d8d; border-width: 0px 1px 1px 0px; }";
299
                        $content.=" </style></head><body>";
300
                        $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>".$LANG["common"][37].":</span> ".$u->getName()."<br>";
301
                        $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>".$LANG["mailing"][7]."</span> ".$name."<br>";
302
                        if (!empty($tech)){
303
                                $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>". $LANG["common"][10].":</span> ".$tech."<br>";
304
                        }
305
                        $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>".$LANG["search"][8].":</span> ".convDateTime($this->fields["begin"])."<br>";
306
                        $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>".$LANG["search"][9].":</span> ".convDateTime($this->fields["end"])."<br>";
307
                        $content.="<span style='color:#8B8C8F; font-weight:bold;  text-decoration:underline; '>".$LANG["common"][25].":</span> ".nl2br($this->fields["comment"])."<br>";
308
                } else { // text format
309
                        $content.=$LANG["mailing"][1]."\n";
310
                        $content.=$LANG["common"][37].": ".$u->getName()."\n";
311
                        $content.=$LANG["mailing"][7]." ".$name."\n";
312
                        if (!empty($tech)){
313
                                $content.= $LANG["common"][10].": ".$tech."\n";
314
                        }
315

    
316
                        $content.=$LANG["search"][8].": ".convDateTime($this->fields["begin"])."\n";
317
                        $content.=$LANG["search"][9].": ".convDateTime($this->fields["end"])."\n";
318
                        $content.=$LANG["common"][25].": ".$this->fields["comment"]."\n";
319
                        $content.=$LANG["mailing"][1]."\n";
320
                }
321
                return $content;
322

    
323
        }
324

    
325
}
326

    
327

    
328
?>
Redmine Appliance - Powered by TurnKey Linux