1<?php
2
3/*
4 * Data import load
5 *************************************************/
6
7# include required scripts
8require_once( dirname(__FILE__) . '/../../../functions/functions.php' );
9
10# initialize user object, if not already set
11if (!isset($Database)) { $Database 	= new Database_PDO; }
12if (!isset($User)) { $User = new User ($Database); }
13if (!isset($Admin)) { $Admin = new Admin ($Database); }
14if (!isset($Tools)) { $Tools = new Tools ($Database); }
15if (!isset($Devtype)) { $Devtype = new Devtype ($Database); }
16
17# verify that user is logged in, to guard against direct access of page and possible exploits
18$User->check_user_session();
19
20$deviceTypes = $Devtype->fetch_all_objects("deviceTypes", "tid");
21
22# Load existing data
23$edata = array();
24# process for easier later check
25foreach ($deviceTypes as $dt) {
26	//cast
27	$dt = (array) $dt;
28	$edata[$dt['tname']] = $dt;
29}
30
31$rows = "";
32$counters = array();
33$unique = array();
34
35# check the fields
36foreach ($data as &$cdata) {
37	$msg = ""; $action = ""; $cfieldtds = "";
38
39	# check if required fields are present and not empty
40	foreach($reqfields as $creq) {
41		if ((!isset($cdata[$creq])) or ($cdata[$creq] == "")) { $msg.= "Required field ".$creq." missing or empty."; $action = "error"; }
42	}
43
44
45	# check if existing
46	if ($action != "error") {
47		if (isset($edata[$cdata['tname']])) {
48			$cdata['tid'] = $edata[$cdata['tname']]['tid'];
49			$action = "skip"; # skip duplicate fields if identical, update if different
50			if ($cdata['tdescription'] != $edata[$cdata['tname']]['tdescription']) { $msg.= " description will be updated."; $action = "edit"; }
51
52			if ($action == "skip") {
53				$msg.= "Duplicate, will skip.";
54			}
55		} else {
56			$msg.="New entry, will be added."; $action = "add";
57		}
58	}
59
60	$cdata['msg'].= $msg;
61	$cdata['action'] = $action;
62	$counters[$action]++;
63	if (!isset($unique[$cdata['tname']])) { $unique[$cdata['tname']] = $cdata['tname']; }
64
65	$rows.="<tr class='".$colors[$action]."'><td><i class='fa ".$icons[$action]."' rel='tooltip' data-placement='bottom' title='"._($msg)."'></i></td>
66		<td>".$cdata['tname']."</td>
67		<td>".$cdata['tdescription']."</td>
68		<td>"._($cdata['msg'])."</td></tr>";
69
70}
71
72?>