/
projectsDatabase.php
executable file
·260 lines (222 loc) · 9.65 KB
/
projectsDatabase.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
<?php
# Class to create a projects database system
class projectsDatabase extends frontControllerApplication
{
# Function to assign defaults additional to the general application defaults
public function defaults ()
{
# Specify available arguments as defaults or as NULL (to represent a required argument)
$defaults = array (
'div' => strtolower (__CLASS__),
'database' => 'projects',
'table' => 'projects',
'useCamUniLookup' => true,
'emailDomain' => 'cam.ac.uk',
'administrators' => true,
'useEditing' => true,
'databaseStrictWhere' => true,
'tabUlClass' => 'tabsflat',
'truncateValues' => 200,
);
# Return the defaults
return $defaults;
}
# Function assign additional actions
public function actions ()
{
# Specify additional actions
$actions = array (
'home' => array (
'description' => 'Projects listing',
'url' => '',
'tab' => 'Projects listing',
'icon' => 'house',
),
'add' => array (
'description' => ($this->userIsAdministrator ? 'Add' : 'Request') . ' a project',
'url' => 'add.html',
'tab' => ($this->userIsAdministrator ? 'Add' : 'Request') . ' a project',
'icon' => 'add',
'authentication' => true,
),
'general' => array (
'description' => 'General ongoing work',
'url' => 'general.html',
'tab' => 'General ongoing work',
'icon' => 'application_view_icons',
'authentication' => true,
'enableIf' => ($this->settings['generalHtml']),
),
'editing' => array (
'description' => false,
'url' => 'projects/',
'tab' => 'Data editing',
'icon' => 'pencil',
'administrator' => true,
),
);
# Return the actions
return $actions;
}
# Database structure definition
public function databaseStructure ()
{
return "
CREATE TABLE `administrators` (
`username` varchar(255) NOT NULL COMMENT 'Username' PRIMARY KEY,
`active` enum('','Yes','No') NOT NULL DEFAULT 'Yes' COMMENT 'Currently active?',
`privilege` enum('Administrator','Restricted administrator') NOT NULL DEFAULT 'Administrator' COMMENT 'Administrator level'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='System administrators';
CREATE TABLE `projects` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '#' PRIMARY KEY,
`name` varchar(255) NOT NULL COMMENT 'Project name',
`client` varchar(20) NOT NULL COMMENT 'For',
`status` enum('proposed','specced','developing','completed','additional','cancelled') NOT NULL DEFAULT 'proposed' COMMENT 'Status',
`startDate` date DEFAULT NULL COMMENT 'Start date',
`finishDate` date DEFAULT NULL COMMENT 'Finish date',
`description` text NOT NULL COMMENT 'Description',
`progress` text COMMENT 'Progress',
`daysEstimated` int(11) NULL DEFAULT NULL COMMENT 'Days estimated',
`daysSpent` int(11) NOT NULL COMMENT 'Days spent',
`url` varchar(255) DEFAULT NULL COMMENT 'URL'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Table of projects';
CREATE TABLE `settings` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Automatic key (ignored)' PRIMARY KEY,
`frontPageHtml` TEXT NULL COMMENT 'Front page notice',
`generalHtml` text COMMENT 'General ongoing work'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Settings';
";
}
# Welcome screen
public function home ()
{
# Start the HTML
$html = '';
# Get the list of projects
if (!$projectsRaw = $this->databaseConnection->select ($this->settings['database'], $this->settings['table'], $conditions = "status != 'proposed'", $columns = array (), true, $orderBy = "FIELD(status, 'developing','additional','specced','proposed','completed'), id DESC")) {
$html = "\n<p>There are no confirmed projects at present.</p>";
echo $html;
return;
}
# Attach status string to the key for CSS styling purposes
$projects = array ();
foreach ($projectsRaw as $id => $project) {
$key = $id . ' ' . lcfirst ($project['status']);
$projects[$key] = $project;
}
# Assemble fields
foreach ($projects as $id => $project) {
if ($this->userIsAdministrator) {
$projects[$id]['id'] = "<a href=\"{$this->baseUrl}/projects/{$project['id']}/edit.html\"><strong>" . $projects[$id]['id'] . '</strong></a>';
}
$projects[$id]['name'] = "<a href=\"{$projects[$id]['url']}\" target=\"_blank\">" . htmlspecialchars ($projects[$id]['name']) . '</a>';
unset ($projects[$id]['url']);
$projects[$id]['status'] = ucfirst ($projects[$id]['status']);
unset ($projects[$id]['client']);
$projects[$id]['description'] = nl2br (htmlspecialchars (application::str_truncate ($projects[$id]['description'], $this->settings['truncateValues'], false, false, true, $htmlMode = false)));
$projects[$id]['progress'] = nl2br (htmlspecialchars (application::str_truncate ($projects[$id]['progress'], $this->settings['truncateValues'], false, false, true, $htmlMode = false)));
}
$allowHtml = array ('id', 'name', 'description', 'progress');
# Find the earliest date
$earliestDate = $this->databaseConnection->selectOneField ($this->settings['database'], $this->settings['table'], 'finishDate', 'finishDate IS NOT NULL', array (), false, $orderBy = 'finishDate', $limit = 1);
# Create the HTML
$html .= "\n<p>The table below shows a list of all confirmed projects" . ($earliestDate ? ' since ' . date ('F Y', strtotime ($earliestDate . ' 12:00:00')) : '') . '.</p>';
if ($this->settings['generalHtml']) {
$html .= "\n<p>This does not include <a href=\"{$this->baseUrl}/general.html\">general, ongoing work</a>.</p>";
}
if ($this->settings['frontPageHtml']) {
$html .= $this->settings['frontPageHtml'];
}
$html .= "\n<p>You can <a href=\"{$this->baseUrl}/add.html\">request a project</a>.</p>";
$tableHeadingSubstitutions = $this->databaseConnection->getHeadings ($this->settings['database'], $this->settings['table']);
$html .= "\n" . '<!-- Enable table sortability: --><script language="javascript" type="text/javascript" src="/sitetech/sorttable.js"></script>';
$html .= application::htmlTable ($projects, $tableHeadingSubstitutions, 'lines sortable" id="sortable', $keyAsFirstColumn = false, false, $allowHtml, false, $addCellClasses = true, $addRowKeyClasses = true);
# Show the HTML
echo $html;
}
# Request project
public function add ()
{
# Start the HTML
$html = '';
# Determine the recipients
$recipients = array ();
$recipients[] = $this->settings['administratorEmail']; // Ensure this is first, i.e. the To: address
foreach ($this->administrators as $administrator) {
$recipients[] = $administrator['email'];
}
$recipients = array_unique ($recipients);
# Create a new form
$form = new form (array (
'div' => 'lines form',
'displayRestrictions' => false,
'nullText' => '',
'formCompleteText' => $this->tick . ' Thank you for your submission. We will be in touch shortly.',
'autofocus' => true,
'databaseConnection' => $this->databaseConnection,
'picker' => true,
'usersAutocomplete' => false,
'rows' => 10,
'cols' => 70,
));
$form->heading ('', "<p>Proposed projects can be submitted using this form.</p>");
$form->dataBinding (array (
'database' => $this->settings['database'],
'table' => $this->settings['table'],
'includeOnly' => ($this->userIsAdministrator ? array () : array ('name', 'client', 'description')),
'intelligence' => true,
'size' => 70,
'attributes' => $this->formDataBindingAttributes (),
));
#!# Reply-to field needs to be fully-qualified with e-mail domain
$form->setOutputEmail ($recipients, $this->settings['administratorEmail'], $this->settings['applicationName'] . ': project submission', NULL, 'client');
$form->setOutputScreen ();
if ($result = $form->process ($html)) {
# Set fixed fields
$result['client'] = $this->user;
# Insert into the database
$this->databaseConnection->insert ($this->settings['database'], $this->settings['table'], $result);
}
# Show the HTML
echo $html;
}
# Helper function to define the dataBinding attributes
private function formDataBindingAttributes ()
{
# Define the properties
$dataBindingAttributes = array (
'client' => array ('editable' => ($this->userIsAdministrator), 'default' => $this->user, 'autocomplete' => $this->settings['usersAutocomplete'], 'autocompleteOptions' => array ('delay' => 0), 'description' => ($this->userIsAdministrator ? '(Field editable by admins only.)' : ''), ),
'description' => array ('description' => 'Please also include a indication of desired timescale for delivery.'),
);
# Return the properties
return $dataBindingAttributes;
}
# General ongoing work page
public function general ()
{
# Start the HTML
$html = '';
# Add editing link for admins
if ($this->userIsAdministrator) {
$html .= "\n<p class=\"right actions\"><a href=\"{$this->baseUrl}/settings.html\"><img src=\"/images/icons/pencil.png\" alt=\"\" border=\"0\"> Edit</a></p>";
}
# Show the general ongoing work HTML
$html .= $this->settings['generalHtml'];
# Show the HTML
echo $html;
}
# Admin editing section, substantially delegated to the sinenomine editing component
public function editing ($attributes = array (), $deny = false, $sinenomineExtraSettings = array ())
{
# Get the databinding attributes
$dataBindingAttributes = $this->formDataBindingAttributes ();
# Define general sinenomine settings
$sinenomineExtraSettings = array (
'truncateValues' => $this->settings['truncateValues'],
'datePicker' => true,
);
# Delegate to the standard function for editing
echo $this->editingTable ($this->settings['table'], $dataBindingAttributes, 'ultimateform', false, $sinenomineExtraSettings);
}
}
?>