CWIS Developer Documentation
CWUser.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: CWUser.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
13 class CWUser extends User {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
21  public function __construct($UserInfo=NULL)
22  {
23  static $EmailWrapperSet = FALSE;
24  if (!$EmailWrapperSet)
25  {
26  User::SetEmailFunction(array("CWUser", "EmailWrapper"));
27  $EmailWrapperSet = TRUE;
28  }
29 
30  # call the parent constructor
31  parent::User($UserInfo);
32 
33  # try to fetch the associated resource if the user was found
34  if ($this->Result === U_OKAY)
35  {
36  $Resource = $this->FetchAssociatedResource($this->UserId);
37 
38  # the associated resource was successfully found
39  if ($Resource instanceof Resource)
40  {
41  $this->Resource = $Resource;
42  }
43 
44  # there was a problem finding the resource
45  else
46  {
47  $this->Result = $Resource;
48  }
49  }
50  }
51 
57  public function Privileges(PrivilegeSet $NewValue = NULL)
58  {
59  if ($NewValue !== NULL)
60  {
61  throw new Exception(
62  "Attempt to set user privileges with CWUser::Privileges(), "
63  ."which is no longer supported");
64  }
65 
66  return new PrivilegeSetCompatibilityShim($this);
67  }
68 
74  public function ResourceId()
75  {
76  return $this->IsResourceObjectSet() ? $this->Resource->Id() : NULL;
77  }
78 
84  public function GetResource()
85  {
86  return $this->IsResourceObjectSet() ? $this->Resource : NULL;
87  }
88 
89 
102  public function HasPriv($Privilege, $Privileges = NULL)
103  {
104  if ($Privilege instanceof PrivilegeSet)
105  {
106  if ($Privileges instanceof Resource)
107  {
108  return $Privilege->MeetsRequirements($this, $Privileges);
109  }
110  else
111  {
112  return $Privilege->MeetsRequirements($this);
113  }
114  }
115  else
116  {
117  return call_user_func_array( "parent::HasPriv", func_get_args() );
118  }
119  }
120 
131  public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
132  {
133  # extract "From" address from supplied headers if available
134  if (strlen($AdditionalHeaders))
135  {
136  $HeaderLines = explode("\n", $AdditionalHeaders);
137  $Headers = array();
138  foreach ($HeaderLines as $Line)
139  {
140  $HeaderLine = trim($Line);
141  if (preg_match("/^from:/i", $Line))
142  {
143  $From = preg_replace("/^from:/i", "", $Line);
144  }
145  else
146  {
147  $Headers[] = $HeaderLine;
148  }
149  }
150  }
151 
152  # send message
153  $Msg = new Email();
154  if (isset($From)) { $Msg->From($From); }
155  $Msg->To($To);
156  $Msg->Subject($Subject);
157  $Msg->AddHeaders($Headers);
158  $Msg->Body($Message);
159  $Result = $Msg->Send();
160 
161  # report success to caller
162  return $Result;
163  }
164 
169  public static function GetCustomUserFields()
170  {
171  static $CustomFields;
172 
173  if (!isset($CustomFields))
174  {
175  $CustomFields = array();
177 
178  foreach ($Schema->GetFields() as $Field)
179  {
180  # they're custom if not owned by CWIS
181  if ($Field->Owner() != "CWISCore")
182  {
183  $CustomFields[$Field->Id()] = $Field;
184  }
185  }
186  }
187 
188  return $CustomFields;
189  }
190 
195  public static function GetDefaultUserFields()
196  {
197  static $DefaultFields;
198 
199  if (!isset($DefaultFields))
200  {
201  $DefaultFields = array();
203 
204  foreach ($Schema->GetFields() as $Field)
205  {
206  # they're default if owned by CWIS
207  if ($Field->Owner() == "CWISCore")
208  {
209  $DefaultFields[$Field->Id()] = $Field;
210  }
211  }
212  }
213 
214  return $DefaultFields;
215  }
216 
217  # ---- OVERRIDDEN METHODS ------------------------------------------------
218 
224  public function Delete()
225  {
226  # delete the associated user resource if set
227  if (isset($this->Resource))
228  {
229  $this->Resource->Delete();
230  $this->Result = U_OKAY;
231  }
232 
233  return parent::Delete();
234  }
235 
242  public function Get($FieldName)
243  {
244  # provide backwards-compatibility for data migrated to users fields as
245  # of CWIS 3.0.0
246  if (in_array($FieldName, self::$MigratedUserFields))
247  {
248  # return NULL if the resource object isn't set
249  if (!$this->IsResourceObjectSet())
250  {
251  return NULL;
252  }
253 
254  # return the value from the associated resource
255  return $this->Resource->Get($FieldName);
256  }
257 
258  # otherwise, get it from the APUsers table
259  return parent::Get($FieldName);
260  }
261 
268  public function Set($FieldName, $NewValue)
269  {
270  # provide backwards-compatibility for data migrated to users fields as
271  # of CWIS 3.0.0
272  if (in_array($FieldName, self::$MigratedUserFields))
273  {
274  # set the value only if the resource object is set
275  if ($this->IsResourceObjectSet())
276  {
277  $this->Resource->Set($FieldName, $NewValue);
278  $this->Result = U_OKAY;
279  }
280  }
281 
282  # transform boolean values to 1 or 0 because that's what the User
283  # class expects
284  if (is_bool($NewValue))
285  {
286  $NewValue = $NewValue ? 1 : 0;
287  }
288 
289  # update the APUsers table
290  return parent::Set($FieldName, $NewValue);
291  }
292 
293  # ---- PRIVATE INTERFACE -------------------------------------------------
294 
299  protected $Resource;
300 
305  protected static $MigratedUserFields = array(
306  "RealName", "WebSite", "AddressLineOne", "AddressLineTwo", "City",
307  "State", "ZipCode", "Country");
308 
315  protected function FetchAssociatedResource($UserId)
316  {
317  try
318  {
321  }
322 
323  # couldn't get the factory or schema, which probably means CWIS hasn't
324  # been installed yet
325  catch (Exception $Exception)
326  {
327  return U_ERROR;
328  }
329 
330  # the UserId field doesn't exist, which probably means CWIS hasn't been
331  # installed yet
332  if (!$Schema->FieldExists("UserId"))
333  {
334  return U_ERROR;
335  }
336 
337  # get matching resources, which should only be one
338  $Field = $Schema->GetFieldByName("UserId");
339  $ResourceIds = $Factory->GetItemIds("`".$Field->DBFieldName()
340  ."` = '".intval($UserId)."'");
341  $ResourceIdCount = count($ResourceIds);
342 
343  # no resource found
344  if ($ResourceIdCount < 1)
345  {
346  return U_NOSUCHUSER;
347  }
348 
349  # too many resources found
350  if ($ResourceIdCount > 1)
351  {
352  return U_ERROR;
353  }
354 
355  # construct the associated resource and return it
356  return new Resource(array_shift($ResourceIds));
357  }
358 
363  protected function IsResourceObjectSet()
364  {
365  # there must be a user ID, which is what the User class assumes, and the
366  # resource must be set
367  return isset($this->UserId) && isset($this->Resource);
368  }
369 
370 
371  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (BEGIN)
372 
373  # ---- user interface preference mnemonics
374  # color avoidance flags
375  const UIPREF_AVOID_RED = 1;
383 
384  # content display options
388 
389  # content view options
393 
394  # audio description options
398 
399  # caption type options
403 
404  # user interface / accessibility preferences
405  function PrefFontSize($NewValue = DB_NOVALUE) { return 0; }
406  function PrefFontTypeFace($NewValue = DB_NOVALUE) { return 0; }
407  function PrefFontColor($NewValue = DB_NOVALUE) { return 0; }
408  function PrefBackgroundColor($NewValue = DB_NOVALUE) { return 0; }
409  function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE) { return 0; }
410  function PrefContentDensity($NewValue = DB_NOVALUE) { return 0; }
411  function PrefContentView($NewValue = DB_NOVALUE) { return 0; }
412  function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE) { return 0; }
413  function PrefAudioDescriptionLanguage($NewValue = DB_NOVALUE) { return 0; }
414  function PrefVisualDescriptionLanguage($NewValue = DB_NOVALUE) { return 0; }
415  function PrefImageDescriptionLanguage($NewValue = DB_NOVALUE) { return 0; }
416  function PrefUseGraphicAlternatives($NewValue = DB_NOVALUE) { return 0; }
417  function PrefSignLanguage($NewValue = DB_NOVALUE) { return 0; }
418  function PrefCaptionType($NewValue = DB_NOVALUE) { return 0; }
419  function PrefCaptionRate($NewValue = DB_NOVALUE) { return 0; }
420 
421  # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (END)
422 }
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:406
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:416
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:379
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:418
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:268
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:377
Metadata schema (in effect a Factory class for MetadataField).
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: CWUser.php:391
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:380
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:415
$Resource
The user resource associated with the user or NULL if the user isn't logged in.
Definition: CWUser.php:299
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:57
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:74
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:376
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:224
const DB_NOVALUE
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:378
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:395
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:402
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:419
Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1008
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:408
Id()
Retrieve numerical resource ID.
Definition: Resource.php:275
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:417
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:315
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:363
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:382
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:385
HasPriv($Privilege, $Privileges=NULL)
Determine if a user has a given privilege, or satisfies the conditions specified by a given privilege...
Definition: CWUser.php:102
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: CWUser.php:396
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:242
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:413
const U_NOSUCHUSER
Definition: Axis--User.php:22
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:386
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:390
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:411
Set of privileges used to access resource information or other parts of the system.
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: CWUser.php:410
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:407
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:409
Electronic mail message.
Definition: Email.php:14
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:21
const U_ERROR
Definition: Axis--User.php:20
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:401
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:414
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:131
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: Axis--User.php:178
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:392
const UIPREF_AVOID_RED
Definition: CWUser.php:375
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:373
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:412
Compatibility layer allowing interfaces built against the privilege system from CWIS 3...
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:305
Represents a "resource" in CWIS.
Definition: Resource.php:13
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:195
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: CWUser.php:397
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:400
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:169
const U_OKAY
Definition: Axis--User.php:19
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:381
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:387
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:84
Factory for Resource objects.
CWIS-specific user class.
Definition: CWUser.php:13
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:154
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:405