~sit-developers/sit/master

1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
1
<?php
2
// templates.php - Manage email and notice templates
3
//
4
// SiT (Support Incident Tracker) - Support call tracking system
1239.1.66 by Paul Heaney
update copyright
5
// Copyright (C) 2010-2014 The Support Incident Tracker Project
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
6
// Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
7
//
8
// This software may be used and distributed according to the terms
9
// of the GNU General Public License, incorporated herein by reference.
10
//
11
762 by Ivan Lucas
Set $permission variable after including core.inc.php to fix Mantis 1632 page security no longer checked. Since we now use constants for permissions we need to include core first so that the constants are defined. (of course)
12
require ('core.php');
703.2.26 by Ivan Lucas
Use new permission constants in page headers, Work-in-progress: only done up to perm #33 so far, and menus and other permission checks not even started.
13
$permission = PERM_TEMPLATE_EDIT; // Edit Template
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
14
require (APPLICATION_LIBPATH . 'functions.inc.php');
15
16
// This page requires authentication
559.3.5 by Paul Heaney
Reverting 4283c1ea, this page requires authentication - menu now shows (Mantis 1564)
17
require (APPLICATION_LIBPATH . 'auth.inc.php');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
18
19
// External variables
837.1.5 by Ivan Lucas
Improve input checking
20
$id = cleanvar($_REQUEST['id']); // can be alpha (a name) as well as numeric (id)
948 by Paul Heaney
don't create template entry to early (Mantis 319)
21
$action = clean_fixed_list($_REQUEST['action'], array('showform', 'list', 'edit', 'update', 'delete', 'new'));
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
22
$templategenre = clean_fixed_list($_REQUEST['template'], array('', 'email', 'notice'));
23
24
$templatetypes = array('incident', 'user', 'system'); // 'contact','site','incident','kb'
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
25
26
if (empty($action) OR $action == 'showform' OR $action == 'list')
27
{
28
    // Show select email type form
29
    $title = $strTemplates;
30
    include (APPLICATION_INCPATH . 'htmlheader.inc.php');
31
32
    echo "<h2>".icon('templates', 32)." ";
33
    echo "{$strTemplates}</h2>";
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
34
    plugin_do('templates');
393 by Paul Heaney
move js to webtrack.js, code format
35
    echo "<p align='center'><a href='template_new.php'>{$strNewTemplate}</a></p>";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
36
37
    $sql = "SELECT * FROM `{$dbEmailTemplates}` ORDER BY id";
38
    $result = mysql_query($sql);
39
    if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
40
    while ($email = mysql_fetch_object($result))
41
    {
42
        $templates[$email->id] = array('id' => $email->id, 'template' => 'email', 'name'=> $email->name,'type' => $email->type, 'desc' => $email->description);
43
    }
44
    $sql = "SELECT * FROM `{$dbNoticeTemplates}` ORDER BY id";
45
    $result = mysql_query($sql);
46
    if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
47
    while ($notice = mysql_fetch_object($result))
48
    {
49
        $templates[$notice->name] = array('id' => $notice->id, 'template' => 'notice', 'name'=> $notice->name, 'type' => $notice->type, 'desc' => $notice->description);
50
    }
51
    ksort($templates);
393 by Paul Heaney
move js to webtrack.js, code format
52
    $shade = 'shade1';
736 by Carsten Jensen
fixing styles, puttin into css
53
    echo "<table class='maintable'>";
1238.1.3 by Ivan Lucas
Show template types (to make templates easier to debug if nothing else)
54
    echo "<tr><th>{$strTemplate}</th><th>{$strType}</th><th>{$strUsed}</th><th>{$strTemplate}</th><th>{$strActions}</th></tr>";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
55
    foreach ($templates AS $template)
56
    {
57
        $system = FALSE;
58
        $tsql = "SELECT COUNT(id) FROM `{$dbTriggers}` WHERE template='{$template['name']}'";
59
        $tresult = mysql_query($tsql);
60
        if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
61
        list($numtriggers) = mysql_fetch_row($tresult);
62
63
        $editurl = "{$_SERVER['PHP_SELF']}?id={$template['id']}&amp;action=edit&amp;template={$template['template']}";
64
        if ($numtriggers < 1 AND ($template['template'] == 'email' AND $template['type'] != 'incident')) $shade = 'expired';
65
        echo "<tr class='{$shade}'>";
66
        echo "<td>";
67
        if ($template['template'] == 'notice')
68
        {
69
            echo icon('info', 16).' '.$strNotice;
70
        }
71
        elseif ($template['template'] == 'email')
72
        {
73
            echo icon('email', 16).' '.$strEmail;
74
        }
75
        else
76
        {
77
            echo $strOther;
78
        }
79
        echo "</td>";
1238.1.3 by Ivan Lucas
Show template types (to make templates easier to debug if nothing else)
80
        echo "<td>{$template['type']} {$template['template']}</td>";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
81
        echo "<td>";
82
        if ($template['template'] == 'email' AND $template['type'] == 'incident')
83
        {
84
            echo icon('support',16, $strIncident);
85
        }
1108 by Paul Heaney
* Rework billing so we no longer have seperate fields for incident and unit rates this is the start of simplifying and improving billing
86
        if ($numtriggers > 0) echo icon('trigger', 16, $strTrigger);
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
87
        if ($numtriggers > 1) echo " (&#215;{$numtriggers})";
88
        echo "</td>";
89
        echo "<td><a href='{$editurl}'>{$template['name']}</a>";
90
        if (!empty($template['desc']))
91
        {
551 by ericthefish
There is no mb_substr_compare() function
92
            if (substr_compare($template['desc'], 'str', 0, 3) === 0)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
93
            {
94
                echo "<br />{$GLOBALS[$template['desc']]}";
95
                $system = TRUE;
96
            }
97
            else
98
            {
99
                echo "<br />{$template['desc']}";
100
            }
101
        }
102
        echo "</td>";
103
        if (!$system)
104
        {
105
            echo "<td><a href='{$editurl}'>{$strEdit}</a></td>";
106
        }
107
        else
108
        {
109
            echo "<td></td>";
110
        }
111
        echo "</tr>\n";
42.18.9 by Paul Heaney
code formatting
112
        if ($shade == 'shade1') $shade = 'shade2';
113
        else $shade = 'shade1';
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
114
    }
115
    echo "</table>";
116
    include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
117
}
948 by Paul Heaney
don't create template entry to early (Mantis 319)
118
elseif ($action == "edit" OR $action == "new")
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
119
{
120
    // Retrieve the template from the database, whether it's email or notice
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
121
    switch ($templategenre)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
122
    {
123
        case 'email':
393 by Paul Heaney
move js to webtrack.js, code format
124
            if (!is_numeric($id)) $sql = "SELECT * FROM `{$dbEmailTemplates}` WHERE name='{$id}' LIMIT 1";
125
            else $sql = "SELECT * FROM `{$dbEmailTemplates}` WHERE id='{$id}'";
126
            $title = "{$strEdit}: {$strEmailTemplate}";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
127
            $templateaction = 'ACTION_EMAIL';
128
            break;
129
        case 'notice':
130
        default:
393 by Paul Heaney
move js to webtrack.js, code format
131
            if (!is_numeric($id)) $sql = "SELECT * FROM `{$dbNoticeTemplates}` WHERE name='{$id}' LIMIT 1";
132
            else $sql = "SELECT * FROM `{$dbNoticeTemplates}` WHERE id='{$id}' LIMIT 1";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
133
            $title = "{$strEdit}: {$strNoticeTemplate}";
134
            $templateaction = 'ACTION_NOTICE';
135
    }
948 by Paul Heaney
don't create template entry to early (Mantis 319)
136
    
137
    if ($action != "new")
138
    {
139
        // This is a edit template so exists in the DB
140
        $result = mysql_query($sql);
141
        $template = mysql_fetch_object($result);
142
        if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
143
    }
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
144
145
    include (APPLICATION_INCPATH . 'htmlheader.inc.php');
146
948 by Paul Heaney
don't create template entry to early (Mantis 319)
147
    if (mysql_num_rows($result) > 0 OR $action == "new")
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
148
    {
149
        echo "<h2>{$title}</h2>";
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
150
        plugin_do('templates');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
151
        echo "<div style='width: 48%; float: left;'>";
152
        echo "<form name='edittemplate' action='{$_SERVER['PHP_SELF']}?action=update' method='post' onsubmit=\"return confirm_action('{$strAreYouSureMakeTheseChanges}')\">";
153
        echo "<table class='vertical' width='100%'>";
154
393 by Paul Heaney
move js to webtrack.js, code format
155
        $tsql = "SELECT * FROM `{$dbTriggers}` WHERE action = '{$templateaction}' AND template = '{$id}' LIMIT 1";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
156
        $tresult = mysql_query($tsql);
157
        if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
158
        if (mysql_num_rows($tresult) >= 1)
159
        {
160
            $trigaction = mysql_fetch_object($tresult);
161
            echo "<tr><th>{$strTrigger}</th><td>".trigger_description($triggerarray[$trigaction->triggerid])."<br /><br />";
162
            echo triggeraction_description($trigaction)."</td></tr>";
163
        }
164
        else
165
        {
166
            echo "<tr><th>{$strTrigger}</th><td>{$strNone}</td></tr>\n";
167
        }
168
169
        // Set template type to the trigger type if no type is already specified
170
        if (empty($template->type)) $template->type = $triggerarray[$trigaction->triggerid]['type'];
171
172
559.3.25 by Paul Heaney
standardise required field
173
        echo "<tr><th>{$strID}:</th><td>";
174
        echo "<input maxlength='50' name='name' size='5' value='{$template->id} 'readonly='readonly' disabled='disabled' /> <span class='required'>{$strRequired}</span></td></tr>\n";
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
175
        echo "<tr><th>{$strTemplate}:</th><td>";
176
        if ($templategenre == 'notice')
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
177
        {
178
            echo icon('info', 32).' '.$strNotice;
179
        }
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
180
        elseif ($templategenre == 'email')
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
181
        {
182
            echo icon('email', 32).' '.$strEmail;
183
        }
184
        else
185
        {
186
            echo $strOther;
187
        }
394 by Paul Heaney
remove fixme
188
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
189
        // Set up required params, each template type needs an entry here TODO add the rest
1238.1.3 by Ivan Lucas
Show template types (to make templates easier to debug if nothing else)
190
        if ($template->type == 'user') 
191
        {
192
            $required = array('incidentid', 'userid');
193
        }
194
        elseif ($template->type == 'incident') 
195
        {
196
            $required = array('incidentid', 'triggeruserid');
197
        }
198
        else 
199
        {
200
            $required = $triggerarray[$trigaction->triggerid]['required'];
201
        }
202
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
203
//         echo " ({$template->type})";
394 by Paul Heaney
remove fixme
204
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
205
        if (!empty($required) AND $CONFIG['debug'])
206
        {
207
            debug_log("Variables required by email template {$template->id}: ".print_r($required, TRUE));
208
        }
209
        echo "</td><tr>";
210
948 by Paul Heaney
don't create template entry to early (Mantis 319)
211
        
212
        $templatename = $template->name;
213
        if ($action == "new")
214
        {
215
            $templatename = cleanvar($_REQUEST['name']);
216
        }
217
        
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
218
        echo "<tr><th>{$strName}:</th><td><input class='required' maxlength='100' name='name' size='40' value=\"{$templatename}\" /> <span class='required'>{$strRequired}</span></td></tr>\n";
219
        echo "<tr><th>{$strType}:</th><td>". array_drop_down($templatetypes, 'type', $template->type) . " <span class='required'>{$strRequired}</span></td></tr>\n";
559.3.25 by Paul Heaney
standardise required field
220
        echo "<tr><th>{$strDescription}:</th>";
221
        echo "<td><textarea class='required' name='description' cols='50' rows='5' onfocus=\"clearFocusElement(this);\"";
281.6.8 by Ivan Lucas
Fix typo in mass replace - sorry
222
        if (mb_strlen($template->description) > 3 AND substr_compare($template->description, 'str', 0, 3) === 0)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
223
        {
224
             echo " readonly='readonly' ";
225
             $template->description = ${$template->description};
1086 by Paul Heaney
format
226
        }
559.3.25 by Paul Heaney
standardise required field
227
        echo ">{$template->description}</textarea> <span class='required'>{$strRequired}</span></td></tr>\n";
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
228
        switch ($templategenre)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
229
        {
230
            case 'email':
231
                echo "<tr><th colspan='2'>{$strEmail}</th></tr>";
232
                echo "<tr><td colspan='2'>{$strTemplatesShouldNotBeginWith}</td></tr>";
559.3.25 by Paul Heaney
standardise required field
233
                echo "<tr><th>{$strTo}</th>";
234
                echo "<td><input class='required' id='tofield' maxlength='100' name='tofield' size='40' value=\"{$template->tofield}\" onfocus=\"recordFocusElement(this);\" /> <span class='required'>{$strRequired}</span></td></tr>\n";
235
                echo "<tr><th>{$strFrom}</th>";
236
                echo "<td><input class='required' id='fromfield' maxlength='100' name='fromfield' size='40' value=\"{$template->fromfield}\" onfocus=\"recordFocusElement(this);\" /> <span class='required'>{$strRequired}</span></td></tr>\n";
237
                echo "<tr><th>{$strReplyTo}</th>";
238
                echo "<td><input class='required' id='replytofield' maxlength='100' name='replytofield' size='40' value=\"{$template->replytofield}\" onfocus=\"recordFocusElement(this);\" /> <span class='required'>{$strRequired}</span></td></tr>\n";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
239
                echo "<tr><th>{$strCC}</th>";
240
                echo "<td><input id='ccfield' maxlength='100' name='ccfield' size='40' value=\"{$template->ccfield}\" onfocus=\"recordFocusElement(this);\" /></td></tr>\n";
241
                echo "<tr><th>{$strBCC}</th>";
242
                echo "<td><input id='bccfield' maxlength='100' name='bccfield' size='40' value=\"{$template->bccfield}\" onfocus=\"recordFocusElement(this);\" /></td></tr>\n";
243
                echo "<tr><th>{$strSubject}</th>";
244
                echo "<td><input id='subject' maxlength='255' name='subjectfield' size='60' value=\"{$template->subjectfield}\" onfocus=\"recordFocusElement(this);\" /></td></tr>\n";
245
                break;
246
            case 'notice':
247
                echo "<tr><th>{$strLinkText}</th>";
248
                echo "<td><input id='linktext' maxlength='50' name='linktext' size='50' ";
281.6.8 by Ivan Lucas
Fix typo in mass replace - sorry
249
                if (mb_strlen($template->linktext) > 3 AND substr_compare($template->linktext, 'str', 0, 3) === 0)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
250
                {
251
                    echo " readonly='readonly' ";
252
                    $template->linktext = $SYSLANG[$template->linktext];
253
                }
254
                echo "value=\"{$template->linktext}\" onfocus=\"recordFocusElement(this);\" /></td></tr>\n";
255
                echo "<tr><th>{$strLink}</th>";
256
                echo "<td><input id='link' maxlength='100' name='link' size='50' value=\"{$template->link}\"  onfocus=\"recordFocusElement(this);\" /></td></tr>\n";
257
                echo "<tr><th>{$strDurability}</th>";
258
                echo "<td><select id='durability' onfocus=\"recordFocusElement(this);\">";
259
                echo "<option";
260
                if ($template->durability == 'sticky')
261
                {
262
                    echo " checked='checked' ";
263
                }
264
                echo ">sticky</option>";
265
                echo "<option";
266
                if ($template->durability == 'session')
267
                {
268
                    echo " checked='checked' ";
269
                }
270
                echo ">session</option>";
271
                echo "</option></select>";
272
        }
273
274
        //if ($trigaction AND $template->type != $triggerarray[$trigaction->triggerid]['type']) echo "<p class='warning'>Trigger type mismatch</p>";
275
        echo "</td></tr>\n";
276
277
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
278
        if ($templategenre == 'email') $body = $template->body;
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
279
        else $body = $template->text;
280
        echo "<tr><th>{$strText}</th>";
281
        echo "<td>";
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
282
        if ($templategenre == 'notice') echo bbcode_toolbar('bodytext');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
283
284
        echo "<textarea id='bodytext' name='bodytext' rows='20' cols='50' onfocus=\"recordFocusElement(this);\"";
281.6.8 by Ivan Lucas
Fix typo in mass replace - sorry
285
        if (mb_strlen($body) > 3 AND substr_compare($body, 'str', 0, 3) === 0)
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
286
        {
287
            echo " readonly='readonly' ";
288
            $body = $SYSLANG[$body];
289
        }
290
        echo ">{$body}</textarea></td>";
291
292
        if ($template->type == 'incident')
293
        {
294
            echo "<tr><th></th><td><label><input type='checkbox' name='storeinlog' value='Yes' ";
295
            if ($template->storeinlog == 'Yes')
296
            {
297
                echo "checked='checked'";
298
            }
299
            echo " /> {$strStoreInLog}</label>";
300
            echo " &nbsp; (<input type='checkbox' name='cust_vis' value='yes' ";
301
            if ($template->customervisibility == 'show')
302
            {
303
                echo "checked='checked'";
304
            }
305
            echo " /> {$strVisibleToCustomer})";
306
            echo "</td></tr>\n";
307
        }
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
308
        plugin_do('templates_form');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
309
        echo "</table>\n";
310
457.2.35 by Ivan Lucas
Standardise forms by having a [Reset] and [Save] button on each, plus a link to return to the previous page (where appropriate).
311
        echo "<p class='formbuttoms'>";
948 by Paul Heaney
don't create template entry to early (Mantis 319)
312
        echo "<input name='savenew' type='hidden' value='";
1040 by Paul Heaney
format
313
        if ($action == "new") echo "yes";
948 by Paul Heaney
don't create template entry to early (Mantis 319)
314
        else echo "no";
315
        echo "' />";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
316
        echo "<input name='type' type='hidden' value='{$template->type}' />";
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
317
        echo "<input name='template' type='hidden' value='{$templategenre}' />";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
318
        echo "<input name='focuselement' id='focuselement' type='hidden' value='' />";
319
        echo "<input name='id' type='hidden' value='{$id}' />";
457.2.35 by Ivan Lucas
Standardise forms by having a [Reset] and [Save] button on each, plus a link to return to the previous page (where appropriate).
320
        echo "<input name='reset' type='reset' value='{$strReset}' /> ";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
321
        echo "<input name='submit' type='submit' value=\"{$strSave}\" />";
322
        echo "</p>\n";
1239 by Paul Heaney
only prevent unused triggers being deleted, mantis 1885 - needs further work so we can delete notice templates as well
323
324
        // Don't allow deletion when template is being used
325
        $sql = "SELECT * FROM `{$dbTriggers}` WHERE template = '{$template->name}'";
326
        $resultUsed = mysql_query($sql);
1239.1.1 by Paul Heaney
Merge remote-tracking branch 'sit/master'
327
1238.1.1 by Ivan Lucas
Remove FIXME (logged in Mantis as 1885)
328
        // TODO We should check whether a template is in use perhaps before allowing deletion? Mantis 1885
1239 by Paul Heaney
only prevent unused triggers being deleted, mantis 1885 - needs further work so we can delete notice templates as well
329
        if ($template->type == 'user' AND mysql_num_rows($resultUsed) == 0)
42.18.9 by Paul Heaney
code formatting
330
        {
331
            echo "<p align='center'><a href='{$_SERVER['PHP_SELF']}?action=delete&amp;id={$id}'>{$strDelete}</a></p>";
332
        }
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
333
        echo "</form>";
334
        echo "</div>";
335
336
        // Show a list of available template variables.  Only variables that have 'requires' matching the 'required'
337
        // that the trigger provides is shown
338
        echo "<div id='templatevariables' style='display:none;'>";
339
        echo "<h4>{$strTemplateVariables}</h4>";
340
        echo "<p align='center'>{$strFollowingSpecialIdentifiers}</p>";
341
        if (!is_array($required)) echo "<p class='info'>{$strSomeOfTheseIdentifiers}</p>";
342
343
        echo "<dl>";
344
345
        foreach ($ttvararray AS $identifier => $ttvar)
346
        {
347
            $showtvar = FALSE;
348
349
            // if we're a multiply-defined variable, get the actual data
350
            if (!isset($ttvar['name']) AND !isset($ttvar['description']))
351
            {
352
                $ttvar = $ttvar[0];
353
            }
354
355
            if (empty($ttvar['requires']) AND $ttvar['show'] !== FALSE)
356
            {
357
                $showtvar = TRUE;
358
            }
359
            elseif ($ttvar['show'] === FALSE)
360
            {
361
                $showtvar = FALSE;
362
            }
363
            else
364
            {
365
                if (!is_array($ttvar['requires'])) $ttvar['requires'] = array($ttvar['requires']);
366
                foreach ($ttvar['requires'] as $needle)
367
                {
368
                    if (!is_array($required) OR in_array($needle, $required)) $showtvar = TRUE;
369
                }
370
            }
371
372
            if ($showtvar)
373
            {
374
                echo "<dt><code><a href=\"javascript:insertTemplateVar('{$identifier}');\">{$identifier}</a></code></dt>";
375
                if (!empty($ttvar['description'])) echo "<dd>{$ttvar['description']}";
376
                {
377
                    if (!empty($ttvar[0]['description'])) echo "<dd>{$ttvar[0]['description']}";
378
                }
379
                echo "<br />";
380
            }
381
        }
382
383
        echo "</dl>";
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
384
        plugin_do('templates_variables_content');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
385
        echo "</table>\n";
386
        echo "</div>";
387
737 by Carsten Jensen
some good night reading for ericthefish: styling tables for centering, submitbuttons as formbuttons, back/return
388
        echo "<p style='clear:both; margin-top: 2em;' class='return'><a href='{$_SERVER['PHP_SELF']}'>{$strBackToList}</a></p>";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
389
390
        include (APPLICATION_INCPATH . 'htmlfooter.inc.php');
391
    }
392
    else
393
    {
394
        echo user_alert(sprintf($strFieldMustNotBeBlank, "'{$strEmailTemplate}'"), E_USER_ERROR);
395
    }
396
}
397
elseif ($action == "delete")
398
{
399
    if (empty($id) OR is_numeric($id) == FALSE)
400
    {
401
        // id must be filled and be a number
402
        header("Location: {$_SERVER['PHP_SELF']}?action=showform");
403
        exit;
404
    }
1239 by Paul Heaney
only prevent unused triggers being deleted, mantis 1885 - needs further work so we can delete notice templates as well
405
    
406
    // Don't allow deletion when template is being used
407
    $sql = "SELECT * FROM `{$dbTriggers}` AS t, `{$dbEmailTemplates}` AS et WHERE t.template = et.name AND et.id = {$id}";
408
    $resultUsed = mysql_query($sql);
409
    
410
    if (mysql_num_rows($resultUsed) > 0)
411
    {
412
        // Only try and delete if not used
413
        // We only allow user templates to be deleted
414
        $sql = "DELETE FROM `{$dbEmailTemplates}` WHERE id='{$id}' AND type='user' LIMIT 1";
415
        mysql_query($sql);
416
        if (mysql_error()) trigger_error(mysql_error(), E_USER_ERROR);
417
    }
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
418
    header("Location: {$_SERVER['PHP_SELF']}?action=showform");
419
    exit;
420
}
421
elseif ($action == "update")
422
{
423
    // External variables
424
    $template = cleanvar($_POST['template']);
425
    $name = cleanvar($_POST['name']);
426
    $description = cleanvar($_POST['description']);
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
427
    $templatetype = clean_fixed_list($_POST['type'], $templatetypes);
428
    
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
429
    $tofield = cleanvar($_POST['tofield']);
430
    $fromfield = cleanvar($_POST['fromfield']);
431
    $replytofield = cleanvar($_POST['replytofield']);
432
    $ccfield = cleanvar($_POST['ccfield']);
433
    $bccfield = cleanvar($_POST['bccfield']);
434
    $subjectfield = cleanvar($_POST['subjectfield']);
435
    $bodytext = cleanvar($_POST['bodytext']);
436
437
    $link = cleanvar($_POST['link']);
438
    $linktext = cleanvar($_POST['linktext']);
439
    $durability = cleanvar($_POST['durability']);
440
441
    $cust_vis = cleanvar($_POST['cust_vis']);
442
    $storeinlog = cleanvar($_POST['storeinlog']);
243.1.12 by Ivan Lucas
Fix invalid clean_int
443
    $id = cleanvar($_POST['id']);
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
444
    $type = cleanvar($_POST['type']);
948 by Paul Heaney
don't create template entry to early (Mantis 319)
445
    
446
    $savenew = clean_fixed_list($_REQUEST['savenew'], array('yes', 'no'));
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
447
393 by Paul Heaney
move js to webtrack.js, code format
448
    // echo "<pre>".print_r($_POST,true)."</pre>";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
449
450
    // User templates may not have _ (underscore) in their names, we replace with spaces
451
    // in contrast system templates must have _ (underscore) instead of spaces, so we do a replace
452
    // the other way around for those
453
    // We do this to help prevent user templates having names that clash with system templates
454
    if ($type == 'user') $name = str_replace('_', ' ', $name);
455
    else $name = str_replace(' ', '_', strtoupper(trim($name)));
456
249 by Paul Heaney
code formatting
457
    if ($cust_vis == 'yes') $cust_vis = 'show';
458
    else $cust_vis = 'hide';
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
459
249 by Paul Heaney
code formatting
460
    if ($storeinlog == 'Yes') $storeinlog = 'Yes';
461
    else $storeinlog = 'No';
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
462
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
463
    plugin_do('templates_submitted');
464
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
465
    switch ($template)
466
    {
467
        case 'email':
948 by Paul Heaney
don't create template entry to early (Mantis 319)
468
            if ($savenew == "yes")
469
            {
470
                // First check the template does not already exist
471
                $sql = "SELECT id FROM `{$dbEmailTemplates}` WHERE name = '{$name}' LIMIT 1";
472
                $result = mysql_query($sql);
473
                if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
474
                if (mysql_num_rows($result) < 1)
475
                {
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
476
                    $sql = "INSERT INTO `{$dbEmailTemplates}` (name, type) VALUES('{$name}', '{$templatetype}')";
948 by Paul Heaney
don't create template entry to early (Mantis 319)
477
                    mysql_query($sql);
478
                    if (mysql_error()) trigger_error(mysql_error(), E_USER_ERROR);
479
                    $id = mysql_insert_id();
480
                }
481
                else
482
                {
483
                    html_redirect($_SERVER['PHP_SELF'], FALSE, $strADuplicateAlreadyExists);
484
                    exit;
485
                }
486
            }
1238.1.5 by Ivan Lucas
Allow setting and editing of template types (user/system/incident) - not sure what the other types are used for at the moment.
487
            $sql  = "UPDATE `{$dbEmailTemplates}` SET name='{$name}', type='{$templatetype}', description='{$description}', tofield='{$tofield}', fromfield='{$fromfield}', ";
42.18.9 by Paul Heaney
code formatting
488
            $sql .= "replytofield='{$replytofield}', ccfield='{$ccfield}', bccfield='{$bccfield}', subjectfield='{$subjectfield}', ";
489
            $sql .= "body='{$bodytext}', customervisibility='{$cust_vis}', storeinlog='{$storeinlog}' ";
948 by Paul Heaney
don't create template entry to early (Mantis 319)
490
            $sql .= "WHERE id='{$id}' LIMIT 1";
42.18.9 by Paul Heaney
code formatting
491
            break;
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
492
        case 'notice':
948 by Paul Heaney
don't create template entry to early (Mantis 319)
493
            if ($savenew == "yes")
494
            {
495
                // First check the template does not already exist
496
                $sql = "SELECT id FROM `{$dbNoticeTemplates}` WHERE name = '{$name}' LIMIT 1";
497
                $result = mysql_query($sql);
498
                if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
499
                if (mysql_num_rows($result) < 1)
500
                {
501
                    $sql = "INSERT INTO `{$dbNoticeTemplates}`(name) VALUES('{$name}')";
502
                    mysql_query($sql);
503
                    if (mysql_error()) trigger_error(mysql_error(), E_USER_ERROR);
504
                    $id = mysql_insert_id();
505
                }
506
                else
507
                {
508
                    html_redirect($_SERVER['PHP_SELF'], FALSE, $strADuplicateAlreadyExists);
509
                    exit;
510
                }
511
            }
281.1.5 by Kieran Hogg
* fix a bug in action_details.php which was stopping editing notice templates
512
            $sql  = "UPDATE `{$dbNoticeTemplates}` SET name='{$name}', description='{$description}', type='".USER_DEFINED_NOTICE_TYPE."', ";
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
513
            $sql .= "linktext='{$linktext}', link='{$link}', durability='{$durability}', ";
42.18.9 by Paul Heaney
code formatting
514
            $sql .= "text='{$bodytext}' ";
515
            $sql .= "WHERE id='{$id}' LIMIT 1";
516
            break;
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
517
        default:
518
            trigger_error('Error: Invalid template type', E_USER_WARNING);
519
            html_redirect($_SERVER['PHP_SELF'], FALSE);
520
    }
521
522
    $result = mysql_query($sql);
523
    if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
524
    if ($result)
525
    {
802 by Ivan Lucas
Plugin contexts to follow convention in Mantis 1681, see http://sitracker.org/wiki/Talk:Creating_Plugins
526
        plugin_do('templates_saved');
1 by Ivan Lucas
Initial commit (ported from sourceforge svn trunk)
527
        journal(CFG_LOGGING_NORMAL, 'Email Template Updated', "Email Template {$type} was modified", CFG_JOURNAL_ADMIN, $type);
528
        html_redirect($_SERVER['PHP_SELF']);
529
    }
530
    else
531
    {
532
        html_redirect($_SERVER['PHP_SELF'], FALSE);
533
    }
534
}
1240 by Paul Heaney
merge changes for template deletion
535
?>