Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
osticket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
docker
osticket
Commits
cec6f85a
Commit
cec6f85a
authored
10 years ago
by
Jared Hancock
Browse files
Options
Downloads
Patches
Plain Diff
orm: Convert Group to ORM
parent
74bcdd9d
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
include/class.dept.php
+3
-0
3 additions, 0 deletions
include/class.dept.php
include/class.group.php
+102
-149
102 additions, 149 deletions
include/class.group.php
include/class.staff.php
+3
-0
3 additions, 0 deletions
include/class.staff.php
scp/groups.php
+2
-1
2 additions, 1 deletion
scp/groups.php
with
110 additions
and
150 deletions
include/class.dept.php
+
3
−
0
View file @
cec6f85a
...
@@ -431,6 +431,9 @@ class GroupDeptAccess extends VerySimpleModel {
...
@@ -431,6 +431,9 @@ class GroupDeptAccess extends VerySimpleModel {
'dept'
=>
array
(
'dept'
=>
array
(
'constraint'
=>
array
(
'dept_id'
=>
'Dept.dept_id'
),
'constraint'
=>
array
(
'dept_id'
=>
'Dept.dept_id'
),
),
),
'group'
=>
array
(
'constraint'
=>
array
(
'group_id'
=>
'Group.group_id'
),
),
),
),
);
);
}
}
...
...
This diff is collapsed.
Click to expand it.
include/class.group.php
+
102
−
149
View file @
cec6f85a
...
@@ -14,67 +14,41 @@
...
@@ -14,67 +14,41 @@
vim: expandtab sw=4 ts=4 sts=4:
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
**********************************************************************/
class
Group
{
class
Group
extends
VerySimpleModel
{
var
$id
;
static
$meta
=
array
(
var
$ht
;
'table'
=>
GROUP_TABLE
,
'pk'
=>
array
(
'group_id'
),
);
var
$members
;
var
$members
;
var
$departments
;
var
$departments
;
function
Group
(
$id
){
$this
->
id
=
0
;
return
$this
->
load
(
$id
);
}
function
load
(
$id
=
0
)
{
if
(
!
$id
&&
!
(
$id
=
$this
->
getId
()))
return
false
;
$sql
=
'SELECT grp.*,grp.group_name as name, grp.group_enabled as isactive, count(staff.staff_id) as users '
.
'FROM '
.
GROUP_TABLE
.
' grp '
.
'LEFT JOIN '
.
STAFF_TABLE
.
' staff USING(group_id) '
.
'WHERE grp.group_id='
.
db_input
(
$id
)
.
' GROUP BY grp.group_id '
;
if
(
!
(
$res
=
db_query
(
$sql
))
||
!
db_num_rows
(
$res
))
return
false
;
$this
->
ht
=
db_fetch_array
(
$res
);
$this
->
id
=
$this
->
ht
[
'group_id'
];
$this
->
members
=
array
();
$this
->
departments
=
array
();
return
$this
->
id
;
}
function
reload
(){
return
$this
->
load
();
}
function
getHashtable
()
{
function
getHashtable
()
{
return
$this
->
ht
;
$base
=
$this
->
ht
;
$base
[
'name'
]
=
$base
[
'group_name'
];
$base
[
'isactive'
]
=
$base
[
'group_enabled'
];
return
$base
;
}
}
function
getInfo
(){
function
getInfo
(){
return
$this
->
getHashtable
();
return
$this
->
getHashtable
();
}
}
function
getId
(){
function
getId
(){
return
$this
->
id
;
return
$this
->
group_
id
;
}
}
function
getName
(){
function
getName
(){
return
$this
->
ht
[
'
name
'
]
;
return
$this
->
group_
name
;
}
}
function
getNumUsers
(){
function
getNumUsers
(){
return
$this
->
ht
[
'users'
]
;
return
StaffModel
::
objects
()
->
filter
(
array
(
'group_id'
=>
$this
->
getId
()))
->
count
()
;
}
}
function
isEnabled
(){
function
isEnabled
(){
return
(
$this
->
ht
[
'isactive'
])
;
return
$this
->
group_enabled
;
}
}
function
isActive
(){
function
isActive
(){
...
@@ -82,7 +56,7 @@ class Group {
...
@@ -82,7 +56,7 @@ class Group {
}
}
function
getTranslateTag
(
$subtag
)
{
function
getTranslateTag
(
$subtag
)
{
return
_H
(
sprintf
(
'group.%s.%s'
,
$subtag
,
$this
->
id
));
return
_H
(
sprintf
(
'group.%s.%s'
,
$subtag
,
$this
->
getId
()
));
}
}
function
getLocal
(
$subtag
)
{
function
getLocal
(
$subtag
)
{
$tag
=
$this
->
getTranslateTag
(
$subtag
);
$tag
=
$this
->
getTranslateTag
(
$subtag
);
...
@@ -98,169 +72,148 @@ class Group {
...
@@ -98,169 +72,148 @@ class Group {
//Get members of the group.
//Get members of the group.
function
getMembers
()
{
function
getMembers
()
{
if
(
!
$this
->
members
&&
$this
->
getNumUsers
())
{
if
(
!
$this
->
members
)
{
$sql
=
'SELECT staff_id FROM '
.
STAFF_TABLE
$this
->
members
=
StaffModel
::
objects
()
.
' WHERE group_id='
.
db_input
(
$this
->
getId
())
->
filter
(
array
(
'group_id'
=>
$this
->
getId
()))
.
' ORDER BY lastname, firstname'
;
->
order_by
(
'lastname'
,
'firstname'
)
if
((
$res
=
db_query
(
$sql
))
&&
db_num_rows
(
$res
))
{
->
all
();
while
(
list
(
$id
)
=
db_fetch_row
(
$res
))
if
((
$staff
=
Staff
::
lookup
(
$id
)))
$this
->
members
[]
=
$staff
;
}
}
}
return
$this
->
members
;
return
$this
->
members
;
}
}
//Get departments the group is allowed to access.
//Get departments the group is allowed to access.
function
getDepartments
()
{
function
getDepartments
()
{
if
(
!
isset
(
$this
->
departments
))
{
if
(
!
$this
->
departments
)
{
$this
->
departments
=
array
();
$sql
=
'SELECT dept_id FROM '
.
GROUP_DEPT_TABLE
foreach
(
GroupDeptAccess
::
objects
()
.
' WHERE group_id='
.
db_input
(
$this
->
getId
())
;
->
filter
(
array
(
'group_id'
=>
$this
->
getId
())
)
if
((
$res
=
db_query
(
$sql
))
&&
db_num_rows
(
$res
))
{
->
values_flat
(
'dept_id'
)
as
$gda
while
(
list
(
$id
)
=
db_fetch_row
(
$res
))
)
{
$this
->
departments
[]
=
$
id
;
$this
->
departments
[]
=
$
gda
[
0
]
;
}
}
}
}
return
$this
->
departments
;
return
$this
->
departments
;
}
}
function
updateDeptAccess
(
$depts
)
{
function
updateDeptAccess
(
$dept
_id
s
)
{
if
(
$dept_ids
&&
is_array
(
$dept_ids
))
{
$groups
=
GroupDeptAccess
::
objects
()
if
(
$depts
&&
is_array
(
$depts
))
{
->
filter
(
array
(
'group_id'
=>
$this
->
getId
()));
foreach
(
$
dept
s
as
$
k
=>
$id
)
{
foreach
(
$
group
s
as
$
group
)
{
$sql
=
'INSERT IGNORE INTO '
.
GROUP_DEPT_TABLE
if
(
$idx
=
array_search
(
$group
->
dept_id
,
$dept_ids
))
.
' SET group_id='
.
db_input
(
$this
->
getId
())
unset
(
$dept_ids
[
$idx
]);
.
', dept_id='
.
db_input
(
$id
);
else
db_query
(
$sql
);
$group
->
delete
(
);
}
}
foreach
(
$dept_ids
as
$id
)
{
GroupDeptAccess
::
create
(
array
(
'group_id'
=>
$this
->
getId
(),
'dept_id'
=>
$id
))
->
save
();
}
return
true
;
}
}
return
false
;
$sql
=
'DELETE FROM '
.
GROUP_DEPT_TABLE
.
' WHERE group_id='
.
db_input
(
$this
->
getId
());
if
(
$depts
&&
is_array
(
$depts
))
// just inserted departments IF any.
$sql
.
=
' AND dept_id NOT IN('
.
implode
(
','
,
db_input
(
$depts
))
.
')'
;
db_query
(
$sql
);
return
true
;
}
function
update
(
$vars
,
&
$errors
)
{
if
(
!
Group
::
save
(
$this
->
getId
(),
$vars
,
$errors
))
return
false
;
$this
->
updateDeptAccess
(
$vars
[
'depts'
]);
$this
->
reload
();
return
true
;
}
}
function
delete
()
{
function
delete
()
{
//Can't delete with members
//
Can't delete with members
if
(
$this
->
getNumUsers
())
if
(
$this
->
getNumUsers
())
return
false
;
return
false
;
$res
=
db_query
(
'DELETE FROM '
.
GROUP_TABLE
.
' WHERE group_id='
.
db_input
(
$this
->
getId
())
.
' LIMIT 1'
);
if
(
!
parent
::
delete
())
if
(
!
$res
||
!
db_affected_rows
(
$res
))
return
false
;
return
false
;
//Remove dept access entry.
// Remove dept access entries
db_query
(
'DELETE FROM '
.
GROUP_DEPT_TABLE
.
' WHERE group_id='
.
db_input
(
$this
->
getId
()));
GroupDeptAccess
::
objects
()
->
filter
(
array
(
'group_id'
=>
$this
->
getId
()))
->
delete
();
return
true
;
return
true
;
}
}
/*** Static functions ***/
/*** Static functions ***/
function
getIdByName
(
$name
){
static
function
getIdByName
(
$name
){
$sql
=
'SELECT group_id FROM '
.
GROUP_TABLE
.
' WHERE group_name='
.
db_input
(
trim
(
$name
));
$id
=
static
::
objects
()
->
filter
(
array
(
'group_name'
=>
trim
(
$name
)))
if
((
$res
=
db_query
(
$sql
))
&&
db_num_rows
(
$res
))
->
values_flat
(
'group_id'
)
->
first
();
list
(
$id
)
=
db_fetch_row
(
$res
);
return
$id
;
return
$id
?
$id
[
0
]
:
0
;
}
}
static
function
getGroupNames
(
$localize
=
true
)
{
static
function
getGroupNames
(
$localize
=
true
)
{
static
$groups
=
array
();
static
$groups
=
array
();
if
(
!
$groups
)
{
if
(
!
$groups
)
{
$sql
=
'SELECT group_id, group_name, group_enabled as isactive FROM '
.
GROUP_TABLE
.
' ORDER BY group_name'
;
$query
=
static
::
objects
()
if
((
$res
=
db_query
(
$sql
))
&&
db_num_rows
(
$res
))
{
->
values_flat
(
'group_id'
,
'group_name'
,
'group_enabled'
)
while
(
list
(
$id
,
$name
,
$enabled
)
=
db_fetch_row
(
$res
))
{
->
order_by
(
'group_name'
);
$groups
[
$id
]
=
sprintf
(
'%s%s'
,
foreach
(
$query
as
$row
)
{
self
::
getLocalById
(
$id
,
'name'
,
$name
),
list
(
$id
,
$name
,
$enabled
)
=
$row
;
$enabled
?
''
:
' '
.
__
(
'(disabled)'
));
$groups
[
$id
]
=
sprintf
(
'%s%s'
,
}
self
::
getLocalById
(
$id
,
'name'
,
$name
),
$enabled
?
''
:
' '
.
__
(
'(disabled)'
));
}
}
}
}
// TODO: Sort groups if $localize
// TODO: Sort groups if $localize
return
$groups
;
return
$groups
;
}
}
function
lookup
(
$id
){
static
function
create
(
$vars
=
false
)
{
return
(
$id
&&
is_numeric
(
$id
)
&&
(
$g
=
new
Group
(
$id
))
&&
$g
->
getId
()
==
$id
)
?
$g
:
null
;
$group
=
parent
::
create
(
$vars
);
$group
->
created
=
SqlFunction
::
NOW
();
return
$group
;
}
}
function
create
(
$vars
,
&
$errors
)
{
function
save
(
$refetch
=
false
)
{
if
(
(
$
id
=
self
::
save
(
0
,
$vars
,
$errors
))
&&
(
$group
=
self
::
lookup
(
$id
)))
if
(
$
this
->
dirty
)
{
$
group
->
update
DeptAccess
(
$vars
[
'depts'
]
);
$
this
->
update
d
=
SqlFunction
::
NOW
(
);
}
return
$id
;
return
parent
::
save
(
$refetch
||
$this
->
dirty
)
;
}
}
function
save
(
$id
,
$vars
,
&
$errors
)
{
function
update
(
$vars
,
&
$errors
)
{
if
(
$
id
&&
$vars
[
'id'
]
!=
$id
)
if
(
isset
(
$this
->
group_
id
)
&&
$this
->
getId
()
!=
$vars
[
'id'
])
$errors
[
'err'
]
=
__
(
'Missing or invalid group ID'
);
$errors
[
'err'
]
=
__
(
'Missing or invalid group ID'
);
if
(
!
$vars
[
'name'
])
{
if
(
!
$vars
[
'name'
])
{
$errors
[
'name'
]
=
__
(
'Group name required'
);
$errors
[
'name'
]
=
__
(
'Group name required'
);
}
elseif
(
strlen
(
$vars
[
'name'
])
<
3
)
{
}
elseif
(
strlen
(
$vars
[
'name'
])
<
3
)
{
$errors
[
'name'
]
=
__
(
'Group name must be at least 3 chars.'
);
$errors
[
'name'
]
=
__
(
'Group name must be at least 3 chars.'
);
}
elseif
((
$gid
=
Group
::
getIdByName
(
$vars
[
'name'
]))
&&
$gid
!=
$id
){
}
elseif
((
$gid
=
static
::
getIdByName
(
$vars
[
'name'
]))
&&
(
!
isset
(
$this
->
group_id
)
||
$gid
!=
$this
->
getId
()))
{
$errors
[
'name'
]
=
__
(
'Group name already exists'
);
$errors
[
'name'
]
=
__
(
'Group name already exists'
);
}
}
if
(
$errors
)
return
false
;
$sql
=
' SET updated=NOW() '
.
', group_name='
.
db_input
(
Format
::
striptags
(
$vars
[
'name'
]))
.
', group_enabled='
.
db_input
(
$vars
[
'isactive'
])
.
', can_create_tickets='
.
db_input
(
$vars
[
'can_create_tickets'
])
.
', can_delete_tickets='
.
db_input
(
$vars
[
'can_delete_tickets'
])
.
', can_edit_tickets='
.
db_input
(
$vars
[
'can_edit_tickets'
])
.
', can_assign_tickets='
.
db_input
(
$vars
[
'can_assign_tickets'
])
.
', can_transfer_tickets='
.
db_input
(
$vars
[
'can_transfer_tickets'
])
.
', can_close_tickets='
.
db_input
(
$vars
[
'can_close_tickets'
])
.
', can_ban_emails='
.
db_input
(
$vars
[
'can_ban_emails'
])
.
', can_manage_premade='
.
db_input
(
$vars
[
'can_manage_premade'
])
.
', can_manage_faq='
.
db_input
(
$vars
[
'can_manage_faq'
])
.
', can_post_ticket_reply='
.
db_input
(
$vars
[
'can_post_ticket_reply'
])
.
', can_view_staff_stats='
.
db_input
(
$vars
[
'can_view_staff_stats'
])
.
', notes='
.
db_input
(
Format
::
sanitize
(
$vars
[
'notes'
]));
if
(
$id
)
{
$sql
=
'UPDATE '
.
GROUP_TABLE
.
' '
.
$sql
.
' WHERE group_id='
.
db_input
(
$id
);
if
((
$res
=
db_query
(
$sql
)))
return
true
;
if
(
$errors
)
return
false
;
$this
->
group_name
=
Format
::
striptags
(
$vars
[
'name'
]);
$this
->
group_enabled
=
$vars
[
'isactive'
];
$this
->
can_create_tickets
=
$vars
[
'can_create_tickets'
];
$this
->
can_delete_tickets
=
$vars
[
'can_delete_tickets'
];
$this
->
can_edit_tickets
=
$vars
[
'can_edit_tickets'
];
$this
->
can_assign_tickets
=
$vars
[
'can_assign_tickets'
];
$this
->
can_transfer_tickets
=
$vars
[
'can_transfer_tickets'
];
$this
->
can_close_tickets
=
$vars
[
'can_close_tickets'
];
$this
->
can_ban_emails
=
$vars
[
'can_ban_emails'
];
$this
->
can_manage_premade
=
$vars
[
'can_manage_premade'
];
$this
->
can_manage_faq
=
$vars
[
'can_manage_faq'
];
$this
->
can_post_ticket_reply
=
$vars
[
'can_post_ticket_reply'
];
$this
->
can_view_staff_stats
=
$vars
[
'can_view_staff_stats'
];
$this
->
notes
=
Format
::
sanitize
(
$vars
[
'notes'
]);
if
(
$this
->
save
())
return
$this
->
updateDeptAccess
(
$vars
[
'depts'
]);
if
(
isset
(
$this
->
group_id
))
{
$errors
[
'err'
]
=
sprintf
(
__
(
'Unable to update %s.'
),
__
(
'this group'
))
$errors
[
'err'
]
=
sprintf
(
__
(
'Unable to update %s.'
),
__
(
'this group'
))
.
' '
.
__
(
'Internal error occurred'
);
.
' '
.
__
(
'Internal error occurred'
);
}
}
else
{
else
{
$sql
=
'INSERT INTO '
.
GROUP_TABLE
.
' '
.
$sql
.
',created=NOW()'
;
if
((
$res
=
db_query
(
$sql
))
&&
(
$id
=
db_insert_id
()))
return
$id
;
$errors
[
'err'
]
=
sprintf
(
__
(
'Unable to create %s.'
),
__
(
'this group'
))
$errors
[
'err'
]
=
sprintf
(
__
(
'Unable to create %s.'
),
__
(
'this group'
))
.
' '
.
__
(
'Internal error occurred'
);
.
' '
.
__
(
'Internal error occurred'
);
}
}
return
false
;
return
false
;
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
include/class.staff.php
+
3
−
0
View file @
cec6f85a
...
@@ -30,6 +30,9 @@ class StaffModel extends VerySimpleModel {
...
@@ -30,6 +30,9 @@ class StaffModel extends VerySimpleModel {
'dept'
=>
array
(
'dept'
=>
array
(
'constraint'
=>
array
(
'dept_id'
=>
'Dept.dept_id'
),
'constraint'
=>
array
(
'dept_id'
=>
'Dept.dept_id'
),
),
),
'group'
=>
array
(
'constraint'
=>
array
(
'group_id'
=>
'Group.group_id'
),
),
),
),
);
);
...
...
This diff is collapsed.
Click to expand it.
scp/groups.php
+
2
−
1
View file @
cec6f85a
...
@@ -33,7 +33,8 @@ if($_POST){
...
@@ -33,7 +33,8 @@ if($_POST){
}
}
break
;
break
;
case
'create'
:
case
'create'
:
if
((
$id
=
Group
::
create
(
$_POST
,
$errors
))){
$group
=
Group
::
create
();
if
((
$group
->
update
(
$_POST
,
$errors
))){
$msg
=
sprintf
(
__
(
'Successfully added %s'
),
Format
::
htmlchars
(
$_POST
[
'name'
]));
$msg
=
sprintf
(
__
(
'Successfully added %s'
),
Format
::
htmlchars
(
$_POST
[
'name'
]));
$_REQUEST
[
'a'
]
=
null
;
$_REQUEST
[
'a'
]
=
null
;
}
elseif
(
!
$errors
[
'err'
]){
}
elseif
(
!
$errors
[
'err'
]){
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment