-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathnumberplanedit.php
123 lines (103 loc) · 4.08 KB
/
numberplanedit.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
<?php
/*
* LMS version 1.11-git
*
* (C) Copyright 2001-2012 LMS Developers
*
* Please, see the doc/AUTHORS for more information about authors!
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* $Id$
*/
$numberplan = $DB->GetRow('SELECT id, period, template, doctype, isdefault
FROM numberplans WHERE id=?', array($_GET['id']));
$template = $numberplan['template'];
$numberplanedit = isset($_POST['numberplanedit']) ? $_POST['numberplanedit'] : NULL;
if(sizeof($numberplanedit))
{
$numberplanedit['template'] = trim($numberplanedit['template']);
$numberplanedit['id'] = $numberplan['id'];
if($numberplanedit['template'] == '')
$error['template'] = trans('Number template is required!');
elseif(!preg_match('/%[1-9]{0,1}N/', $numberplanedit['template']))
$error['template'] = trans('Template must consist "%N" specifier!');
if(!isset($numberplanedit['isdefault']))
$numberplanedit['isdefault'] = 0;
if($numberplanedit['doctype'] == 0)
$error['doctype'] = trans('Document type is required!');
if($numberplanedit['period'] == 0)
$error['period'] = trans('Numbering period is required!');
if($numberplanedit['doctype'] && $numberplanedit['isdefault'])
if($DB->GetOne('SELECT 1 FROM numberplans n
WHERE doctype = ? AND isdefault = 1 AND n.id != ?'
.(!empty($_POST['selected']) ? ' AND EXISTS (
SELECT 1 FROM numberplanassignments WHERE planid = n.id
AND divisionid IN ('.implode(',', array_keys($_POST['selected'])).'))'
: ' AND NOT EXISTS (SELECT 1 FROM numberplanassignments
WHERE planid = n.id)'),
array($numberplanedit['doctype'], $numberplanedit['id'])))
{
$error['doctype'] = trans('Selected document type has already defined default plan!');
}
if(!$error)
{
$DB->BeginTrans();
$DB->Execute('UPDATE numberplans SET template=?, doctype=?, period=?, isdefault=? WHERE id=?',
array(
$numberplanedit['template'],
$numberplanedit['doctype'],
$numberplanedit['period'],
$numberplanedit['isdefault'],
$numberplanedit['id']
));
$DB->Execute('DELETE FROM numberplanassignments WHERE planid = ?', array($numberplanedit['id']));
if(!empty($_POST['selected']))
foreach($_POST['selected'] as $idx => $name)
$DB->Execute('INSERT INTO numberplanassignments (planid, divisionid)
VALUES (?, ?)', array($numberplanedit['id'], intval($idx)));
$DB->CommitTrans();
$SESSION->redirect('?m=numberplanlist');
}
else
{
$numberplanedit['selected'] = array();
if(isset($_POST['selected']))
{
foreach($_POST['selected'] as $idx => $name)
{
$numberplanedit['selected'][$idx]['id'] = $idx;
$numberplanedit['selected'][$idx]['name'] = $name;
}
}
}
$numberplan = $numberplanedit;
}
else
{
$numberplan['selected'] = $DB->GetAllByKey('SELECT d.id, d.shortname AS name
FROM numberplanassignments, divisions d
WHERE d.id = divisionid AND planid = ?', 'id', array($numberplan['id']));
}
$layout['pagetitle'] = trans('Numbering Plan Edit: $a', $template);
$SESSION->save('backto', $_SERVER['QUERY_STRING']);
$SMARTY->assign('numberplanedit', $numberplan);
$SMARTY->assign('available', $DB->GetAllByKey('SELECT id, shortname AS name
FROM divisions WHERE status = 0 '
.(!empty($numberplan['selected']) ? 'OR id IN ('.implode(',', array_keys($numberplan['selected'])).')' : '')
.'ORDER BY shortname', 'id', array($numberplan['id'])));
$SMARTY->assign('error', $error);
$SMARTY->display('numberplanedit.html');
?>