-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web_Phonebook.php
132 lines (112 loc) · 4.99 KB
/
Web_Phonebook.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
<!DOCTYPE html>
<html lang="en">
<?php
## Constants
define("REGULARELEMENT", "Regular");
// Global Variables
$ArrayArray = array();
$ImportArray = [];
//Settings Variables
$PhonebookPath = 'PhonebookFiles/Web_Phonebook.xml';
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Loads phonebook.xml into array
function LoadInfo($XMLPath)
{
$XMLFile = file_get_contents($XMLPath);
$ContactArrayXML = simplexml_load_string($XMLFile, "SimpleXMLElement", LIBXML_NOCDATA);
$ContactArrayJSON = json_encode($ContactArrayXML);
return json_decode($ContactArrayJSON, TRUE);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Displays array in neatly formatted table
function DisplayTable($Array)
{
$TableHeading ="";
$TableData ="";
$TableHeading = '
<table class="table-striped w-100 text-white">
<tr>
<th class="text-left">Name</th>
<th class="text-right">Extension</th>
</tr>
';
for ($i = 0; $i < sizeof($Array); $i++)
{
$TableData = $TableData . "
<tr>
<td class='text-left'>
<a href='tel:" . $Array[$i]['Ext'] . "'>" . $Array[$i]['Name'] . "</a>
</td>
<td class='text-right'>
<a href='tel:" . $Array[$i]['Ext'] . "'>" . $Array[$i]['Ext'] . "</a>
</td>
</tr>";
}
echo $TableHeading . $TableData . "</table>";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Creates an array key that accepts an arrray for value IF one does not alreay exist
function CreateMasterArrayValue($Tag)
{
global $ArrayArray;
if (!array_key_exists($Tag, $ArrayArray))
{
$ArrayArray[$Tag] = array();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sorts contacts by tag into their corresponding arrays in the master array
function AutoTagger($Array)
{
global $ArrayArray;
$ContactElement = "Contact";
$NameElement = "Name";
$ExtElement = "Ext";
for ($i = 0; $i < sizeof($Array[$ContactElement]); $i++)
{
if (preg_match("/([\(\[]\w+[\)\]])\ ?(.*)/m", $Array[$ContactElement][$i][$NameElement], $matches)) // If name contains a tag
{
$TagName = str_replace(array('(', ')', '[', ']'), '', $matches[1]);
CreateMasterArrayValue($TagName);
array_push($ArrayArray[$TagName], array('Name'=>$matches[2], 'Ext'=>$Array[$ContactElement][$i][$ExtElement]));
} else
{
CreateMasterArrayValue(REGULARELEMENT);
array_push($ArrayArray[REGULARELEMENT], array('Name'=>$Array[$ContactElement][$i][$NameElement], 'Ext'=>$Array[$ContactElement][$i][$ExtElement]));
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Script Starts Here
$ImportArray = LoadInfo($PhonebookPath);
AutoTagger($ImportArray);
?>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="Style.css">
<title>WebPhonebook Main Content</title>
</head>
<body>
<div class="d-flex m-1">
<div class="p-2 m-1 bg-dark text-white flex-grow-1">
<h2>Contacts</h2>
<?php DisplayTable($ArrayArray[REGULARELEMENT]) ?>
</div>
<div class="p-2 m-1 bg-dark text-dark">
<?php
foreach ($ArrayArray as $Key => $Value)
{
if ($Key != REGULARELEMENT)
{
echo '
<div class="p-1 m-1 mb-2 text-white">
<h2>' . $Key . '</h2>';
DisplayTable($Value);
echo '</div>';
}
}
?>
</div>
</div>
</body>
</html>