CWIS Developer Documentation
Privilege.php
Go to the documentation of this file.
1 <?PHP
2 
3 #
4 # FILE: Privilege.php
5 #
6 # METHODS PROVIDED:
7 # Privilege()
8 # - constructor
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
11 #
12 # AUTHOR: Edward Almasy
13 #
14 # Part of the Collection Workflow Integration System
15 # Copyright 2007 Edward Almasy and Internet Scout
16 # http://scout.wisc.edu
17 #
18 
24 class Privilege {
25 
26  # ---- PUBLIC INTERFACE --------------------------------------------------
27 
30 
37  function Privilege($Id, $Name = NULL)
38  {
39  global $G_PrivDescriptions;
40 
41  # if caller requested creation of new entry
42  if ($Id === NULL)
43  {
44  # get highest current ID
45  $DB = new Database();
46  $HighestId = $DB->Query("SELECT Id FROM CustomPrivileges"
47  ." ORDER BY Id DESC LIMIT 1", "Id");
48 
49  # select new ID
50  $this->Id = max(100, ($HighestId + 1));
51 
52  # add new entry to database
53  $DB->Query("INSERT INTO CustomPrivileges (Id, Name)"
54  ." VALUES (".$this->Id.", '".addslashes($Name)."')");
55  $this->Name = $Name;
56  }
57  else
58  {
59  # save ID
60  $this->Id = intval($Id);
61 
62  # if ID indicates predefined privilege
63  if ($this->IsPredefined())
64  {
65  # load privilege info from predefined priv array
66  $this->Name = $G_PrivDescriptions[$this->Id];
67  }
68  else
69  {
70  # load privilege info from database
71  $DB = new Database();
72  $this->Name = $DB->Query("SELECT Name FROM CustomPrivileges"
73  ." WHERE Id = ".$this->Id, "Name");
74  }
75  }
76  }
77 
82  function Delete()
83  {
84  if (!$this->IsPredefined())
85  {
86  $DB = new Database();
87  $DB->Query("DELETE FROM CustomPrivileges"
88  ." WHERE Id = ".$this->Id);
89  }
90  }
91 
96 
98  function Id() { return $this->Id; }
104  function Name($NewValue = NULL)
105  {
106  if (($NewValue !== NULL) && !$this->IsPredefined())
107  {
108  $DB = new Database();
109  $DB->Query("UPDATE CustomPrivileges"
110  ." SET Name = '".addslashes($NewValue)."'"
111  ." WHERE Id = ".$this->Id);
112  $this->Name = $NewValue;
113  }
114  return $this->Name;
115  }
116 
123  function IsPredefined($Id = NULL)
124  {
125  if ($Id === NULL)
126  {
127  $Id = $this->Id;
128  }
129  return (($Id > 0) && ($Id < 100)) ? TRUE : FALSE;
130  }
131 
134  # ---- PRIVATE INTERFACE -------------------------------------------------
135 
136  private $Id;
137  private $Name;
138 }