forked from sivann/itdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.php
654 lines (516 loc) · 16.4 KB
/
model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<?php
/********************************* DB QUERIES IN FUNCTION FORM ****************************************/
function getstatusidofitem($itemid,$dbh)
{
$sql="SELECT status FROM items WHERE items.id='$itemid'";
$sth=db_execute($dbh,$sql);
$statusid=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$statusid=$statusid['status'];
return $statusid;
}
/* return css and title based on item status */
function attrofstatus($statusid,$dbh)
{
$sql="SELECT * from statustypes WHERE id='$statusid'";
$sth=db_execute($dbh,$sql);
$statusdesc=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$statusdesc=$statusdesc['statusdesc'];
$attr=" class='statusx status".$statusid."' title='Status: $statusdesc' ";
$statustxt=$statusdesc;
return array($attr,$statustxt);
}
function ftype2str($typeid,$dbh) {
$sql="SELECT typedesc from filetypes WHERE ".
" id='$typeid'";
$sth=db_execute($dbh,$sql);
$typestr=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$typestr=$typestr['typedesc'];
return ucfirst($typestr);
}
//returns files array from invoice id
function invid2files($invid,$dbh) {
$sql="SELECT files.* from files,invoice2file WHERE ".
" invoice2file.invoiceid='$invid' AND ".
" invoice2file.fileid=files.id";
$sth=db_execute($dbh,$sql);
$fn=$sth->fetchAll(PDO::FETCH_ASSOC);
return $fn;
}
//returns files array from software id
function softid2files($softid,$dbh) {
$sql="SELECT files.* from files,software2file WHERE ".
" software2file.softwareid='$softid' AND ".
" software2file.fileid=files.id";
$sth=db_execute($dbh,$sql);
$fn=$sth->fetchAll(PDO::FETCH_ASSOC);
return $fn;
}
function softid2invoicefiles($softid,$dbh) {
$sql="SELECT files.* from files,invoice2file,soft2inv WHERE ".
" soft2inv.softid='$softid' AND ".
" invoice2file.invoiceid=soft2inv.invid AND ".
" invoice2file.fileid=files.id";
$sthi=db_execute($dbh,$sql);
$f=$sthi->fetchAll(PDO::FETCH_ASSOC);
return $f;
}
function softid2contractfiles($softid,$dbh) {
$sql="SELECT files.* from files,contract2file,contract2soft WHERE ".
" contract2soft.softid='$softid' AND ".
" contract2file.contractid=contract2soft.contractid AND ".
" contract2file.fileid=files.id";
$sthi=db_execute($dbh,$sql);
$f=$sthi->fetchAll(PDO::FETCH_ASSOC);
return $f;
}
//returns files array from item id
function itemid2files($itemid,$dbh) {
$sql="SELECT files.* from files,item2file WHERE ".
" item2file.itemid='$itemid' AND ".
" item2file.fileid=files.id";
$sth=db_execute($dbh,$sql);
$fn=$sth->fetchAll(PDO::FETCH_ASSOC);
return $fn;
}
function itemid2invoicefiles($itemid,$dbh) {
$sql="SELECT files.* from files,invoice2file,item2inv WHERE ".
" item2inv.itemid='$itemid' AND ".
" invoice2file.invoiceid=item2inv.invid AND ".
" invoice2file.fileid=files.id";
$sthi=db_execute($dbh,$sql);
$f=$sthi->fetchAll(PDO::FETCH_ASSOC);
return $f;
}
function itemid2contractfiles($itemid,$dbh) {
$sql="SELECT files.* from files,contract2file,contract2item WHERE ".
" contract2item.itemid='$itemid' AND ".
" contract2file.contractid=contract2item.contractid AND ".
" contract2file.fileid=files.id";
$sthi=db_execute($dbh,$sql);
$f=$sthi->fetchAll(PDO::FETCH_ASSOC);
return $f;
}
//returns files array from contract id
function contractid2files($contractid,$dbh) {
$sql="SELECT files.* from files,contract2file WHERE ".
" contract2file.contractid='$contractid' AND ".
" contract2file.fileid=files.id";
$sth=db_execute($dbh,$sql);
$fn=$sth->fetchAll(PDO::FETCH_ASSOC);
return $fn;
}
//returns number of connected items/racks with a locationid
function countloclinks($locid,$dbh) {
$sql="SELECT count(id) count from items where locationid=$locid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
$sql="SELECT count(id) count from racks where locationid=$locid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
return $count;
}
//returns number of connected items/racks with a location areaid
function countlocarealinks($locareaid,$dbh) {
$sql="SELECT count(id) count from items where locareaid=$locareaid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
$sql="SELECT count(id) count from racks where locareaid=$locareaid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
return $count;
}
function countfileidlinks($fileid,$dbh) {
$count=0;
$sql="SELECT count(softwareid) count from software2file where fileid=$fileid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
$sql="SELECT count(*) count from invoice2file where fileid=$fileid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
$sql="SELECT count(*) count from item2file where fileid=$fileid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
$sql="SELECT count(*) count from contract2file where fileid=$fileid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$count+=$r['count'];
return $count;
}
function delfile($fileid,$dbh) {
global $uploaddir;
//delete inter-item links
$sql="SELECT fname from files where id=$fileid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$fname=$r['fname'];
$sql="DELETE from files where id=$fileid";
$sth=db_exec($dbh,$sql);
$sql="DELETE from invoice2file where fileid=$fileid";
$sth=db_exec($dbh,$sql);
$sql="DELETE from software2file where fileid=$fileid";
$sth=db_exec($dbh,$sql);
$sql="DELETE from item2file where fileid=$fileid";
$sth=db_exec($dbh,$sql);
$sql="DELETE from contract2file where fileid=$fileid";
$sth=db_exec($dbh,$sql);
if (strlen($fname))
unlink($uploaddir.$fname);
}
function countitemtags($tagid) {
global $dbh;
$sql="SELECT count(itemid) count from tag2item where tagid=$tagid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
return $r['count'];
}
function countsoftwaretags($tagid) {
global $dbh;
$sql="SELECT count(softwareid) count from tag2software where tagid=$tagid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
return $r['count'];
}
function tagid2name($id) {
global $dbh;
$sql="SELECT name from tags where id = '$id'";
$sth=$dbh->query($sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$name=$r['name'];
return $name;
}
function tagname2id($name) {
global $dbh;
$id="";
$sql="SELECT id from tags where name = '$name'";
$sth=$dbh->query($sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$id=$r['id'];
return $id;
}
function showtags($type="item",$id,$lnk=1) {
// id: item id
global $dbh;
$sql="SELECT name FROM tags,tag2$type WHERE tags.id=tag2$type.tagid AND tag2$type.{$type}id='$id' ORDER BY name";
$sth = $dbh->query($sql);
$tags=array();
while ($r=$sth->fetch(PDO::FETCH_ASSOC)) array_push($tags,$r['name']);
$sth->closeCursor();
$ret= "\n<!-- showtags -->\n";
$ret.= "\n <ul class='tags'>\n";
if (count($tags)) {
for ($i=0;$i<count($tags)-1 ; $i++) {
if ($lnk)
$ret.= " <li><a href=''>{$tags[$i]}</a></li>\n";
else
$ret.= " <li>{$tags[$i]}</li>\n";
}
if ($lnk)
$ret.= " <li class='last'><a href=''>{$tags[$i]}</a></li>\n";
else
$ret.= " <li class='last'>{$tags[$i]}</li>\n";
}
$ret.= " </ul>\n ";
return $ret;
}
function countitemsinrack($rackid) {
global $dbh;
$sql="SELECT count(id) count from items where rackid=$rackid";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
return $r['count'];
}
function delrack($rackid,$dbh) {
$sql="UPDATE items set rackid='' where rackid='$rackid'";
$sth=db_exec($dbh,$sql);
$sql="DELETE from racks where id='$rackid'";
$sth=db_exec($dbh,$sql);
}
function deluser($userid,$dbh) {
if ($userid==1) {
echo "Cannot remove user with ID=1";
return;
}
$sql="UPDATE items set userid=1 where userid='$userid'";
$sth=db_exec($dbh,$sql);
$sql="DELETE from users where id='$userid'";
$sth=db_exec($dbh,$sql);
}
function countitemsofuser($userid) {
global $dbh;
$sql="SELECT count(id) count from items where userid='$userid'";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
return $r['count'];
}
function showfiles($f,$class="fileslist",$wantdel=1,$divtitle='') {
// f: array with data from "files" table (from fetchall)
global $dateparam,$scriptname,$action,$id,$uploaddirwww,$dbh;
$flnk="";
for ($lnk="",$c=0;$c<count($f);$c++) {
$fname=$f[$c]['fname'];
$ftitle=$f[$c]['title'];
$fid=$f[$c]['id'];
$ftype=$f[$c]['type'];
$fdate=empty($f[$c]['date'])?"":date($dateparam,$f[$c]['date']);
$ftypestr=ftype2str($ftype,$dbh);
if (strlen($ftitle)) $t="<br>Title:$ftitle"; else $t="";
$flnk.="<div title='$divtitle' class='$class' >";
if ($wantdel)
$flnk.="<a title='Remove association. If file is orphaned (nothing links to it), it gets deleted.' ".
"href='javascript:delconfirm2(\"[$fid] $fname\",\"$scriptname?action=$action&id=$id&delfid=$fid\");'>".
"<img src='images/delete.png'></a> ";
$flnk.= "<a target=_blank title='Edit File $fid' href='$scriptname?action=editfile&id=$fid'><img src='images/edit.png'></a>".
" <a target=_blank title='Download $fname' href='".$uploaddirwww.$fname."'><img src='images/down.png'></a>".
"<br>Type:<b>$ftypestr</b>".
"<br>Date:<b>$fdate</b>".
"<br>Title:$ftitle\n".
"</div>\n ";
}
return $flnk;
}
function calcremdays($purchdate_ts,$warrantymonths) {
if (!strlen($warrantymonths))
return array('string'=>'','days'=>'');
if (!is_numeric($purchdate_ts))
return array('string'=>'','days'=>'');
$nowdate = new DateTime();
$pdate = new DateTime();
$pdate->setTimestamp(intval($purchdate_ts));
$d_interval=new DateInterval("P{$warrantymonths}M");
$d_interval->invert=0;
$enddate=$pdate->add($d_interval);
$exp_interval = $nowdate->diff($enddate);
if ($exp_interval->format('%y'))
$exp_interval_str=$exp_interval->format('%r %y yr %m mon, %d d');
else
$exp_interval_str=$exp_interval->format('%r %m mon, %d d');
$exp_interval_sign=$exp_interval->format('%r');
$exp_interval_days="$exp_interval_sign".$exp_interval->days;
if ($exp_interval_sign=="-")
$exp_interval_str="<span style='color:#F90000'>$exp_interval_str</span>";
else
$exp_interval_str="<span style='color:green'>$exp_interval_str</span>";
return array('string'=>$exp_interval_str,'days'=>$exp_interval_days);
}
function showremdays($remdays) {
if (abs($remdays)>360) $remw=sprintf("%.1f",($remdays/360))."yr";
else if (abs($remdays)>70) $remw=sprintf("%.1f",($remdays/30))."mon";
else if (strlen($remdays)) $remw="$remdays"."d";
else $remw="";
if ($remdays<0) $remw="<span style='color:#F90000'>$remw</span>";
if ($remdays>0) $remw="<span style='color:green'>$remw</span>";
if (abs($remdays)>360*20) $remw="";
return $remw;
}
function t($s) {
global $trans_tbl,$settings,$trans_showmissing,$scriptdir;
$lang=$settings['lang'];
if ($lang=="en") return $s;
if (isset($trans_tbl[$lang][$s]) && strlen(trim($trans_tbl[$lang][$s]))) {
return $trans_tbl[$lang][$s];
}
else {
if ($trans_showmissing) {
$fn="$scriptdir/translations/{$lang}.missing.txt";
file_put_contents($fn,$s."\n",FILE_APPEND);
}
return $s;
}
}
function te($s) {
echo t($s);
}
function read_trans($lang) {
global $trans_tbl,$scriptdir;
$row = 0;
if ($lang=="en") return;
$fn="$scriptdir/translations/$lang.txt";
if (is_readable($fn) && (($handle = fopen($fn, "r")) !== FALSE)) {
while ($line=trim(fgets($handle))) {
if($pos=strpos($line,'#')){
$key=substr($line,0,$pos);
$value=substr($line, $pos+1);
$trans_tbl[$lang][$key]=$value;
}
}
fclose($handle);
}
else {
echo "<p style='display:inline;background-color:white;color:red'> Error openning $fn <br /></p>\n";
}
}
/* return h/w manufacturers agent */
function getagenthwmanufacturers() {
global $dbh;
$sql="SELECT * from agents where type&4";
$sth=db_execute($dbh,$sql);
$r=$sth->fetchAll(PDO::FETCH_ASSOC);
$sth->closeCursor();
$agents=$r;
return $r;
}
function gethwmanufacturerbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
if (!strlen($name))
return -1;
$sql="SELECT * from agents where type&8 AND lower(title) = '$name'";
$sth=db_execute($dbh,$sql);
$r=$sth->fetchAll(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r[0]['id']))
return -1;
else
return $r;
}
function getuseridbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
if (!strlen($name))
return -1;
$sql="SELECT id from users where LOWER(username) ='$name' ";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r['id']))
return -1;
else
return $r['id'];
}
function getagentidbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
if (!strlen($name))
return -1;
$sql="SELECT id from agents where LOWER(title) ='$name' ";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r['id']))
return -1;
else
return $r['id'];
}
function getitemtypeidbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
if (!strlen($name))
return -1;
$sql="SELECT id from itemtypes where LOWER(typedesc) ='$name' ";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r['id']))
return -1;
else
return $r['id'];
}
function getstatustypeidbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
if (!strlen($name))
return -1;
$sql="SELECT id from statustypes where LOWER(statusdesc) ='$name' ";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r['id']))
return -1;
else
return $r['id'];
}
function getlocidsbynames ($locname,$areaname) {
global $dbh;
$locname=trim(strtolower($locname));
$areaname=trim(strtolower($areaname));
if (!strlen($locname))
return array(-1,-1);
//if (!strlen($locname) || (!strlen($areaname))) return array(-1,-1);
if (strlen($areaname)) {
$sql="SELECT locations.id as locid, locareas.id as locareaid from locations,locareas ".
" WHERE locareas.locationid=locations.id AND ".
" LOWER(locareas.areaname) =:areaname AND ".
" LOWER(locations.name) =:locname";
$sth=db_execute2($dbh,$sql,array('areaname'=>$areaname,'locname'=>$locname));
$r=$sth->fetch(PDO::FETCH_BOTH);
$sth->closeCursor();
if (!count($r['locid'])) return array(-1,-1);
else return $r;
}
else {
$sql="SELECT locations.id as locid, locareas.id as locareaid from locations,locareas ".
" WHERE LOWER(locations.name) =:locname";
$sth=db_execute2($dbh,$sql,array('locname'=>$locname));
$r=$sth->fetch(PDO::FETCH_BOTH);
$sth->closeCursor();
if (!count($r['locid'])) return array(-1,-1);
else return array($r['locid'],-1);
}
}
function getuserbyname ($name) {
global $dbh;
$name=trim(strtolower($name));
$sql="SELECT * from users where lower(username) = :name ";
$sth=db_execute2($dbh,$sql,array('name'=>$name));
$r=$sth->fetchAll(PDO::FETCH_ASSOC);
$sth->closeCursor();
if (!count($r[0]['id'])) {
return -1;
}
else {
return $r;
}
}
/* return URL of an agent which has a specific $tag description. E.g. "Service Tag" */
function getagenturlbytag($agenturl,$tagstr) {
global $dbh;
$sql="SELECT urls from agents where id='$agenturl'";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$urls=$r['urls'];
$allurls=explode("|",$urls);
for ($i=0;$i<count($allurls);$i++) {
$row=explode("#",$allurls[$i]);
$description=$row[0];
$url=urldecode($row[1]);
if (stristr($description,$tagstr))
return $url;
}
return "";
}
function dbversion() {
global $dbh;
$sql="SELECT * from settings";
$sth=db_execute($dbh,$sql);
$r=$sth->fetch(PDO::FETCH_ASSOC);
$sth->closeCursor();
$ver=$r['dbversion'];
return $ver;
}
?>