CWIS Developer Documentation

- Home
- Main Page
- Classes
- Files

- File List
- File Members

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();
176 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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();
202 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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 {
319 $Factory = new ResourceFactory(MetadataSchema::SCHEMAID_USER);
320 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
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;
376 const UIPREF_AVOID_REDGREEN = 2;
377 const UIPREF_AVOID_BLUEYELLOW = 4;
378 const UIPREF_AVOID_GREENYELLOW = 8;
379 const UIPREF_AVOID_ORANGE = 16;
380 const UIPREF_AVOID_REDBLACK = 32;
381 const UIPREF_AVOID_PURPLEGREY = 64;
382 const UIPREF_AVOID_USEMAXMONOCHR = 128;
383
384 # content display options
385 const UIPREF_CONTENTDENSITY_NOPREFERENCE = 0;
386 const UIPREF_CONTENTDENSITY_DETAILED = 1;
387 const UIPREF_CONTENTDENSITY_OVERVIEW = 2;
388
389 # content view options
390 const UIPREF_CONTENTVIEW_NOPREFERENCE = 0;
391 const UIPREF_CONTENTVIEW_TEXTINTENSIVE = 1;
392 const UIPREF_CONTENTVIEW_IMAGEINTENSIVE = 2;
393
394 # audio description options
395 const UIPREF_AUDIODESCRIPTION_NONE = 0;
396 const UIPREF_AUDIODESCRIPTION_STANDARD = 1;
397 const UIPREF_AUDIODESCRIPTION_EXPANDED = 2;
398
399 # caption type options
400 const UIPREF_CAPTIONTYPE_NONE = 0;
401 const UIPREF_CAPTIONTYPE_VERBATIM = 1;
402 const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL = 2;
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 }
CWUser\PrefFontTypeFace
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:406
CWUser\PrefUseGraphicAlternatives
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:416
CWUser\UIPREF_AVOID_ORANGE
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:379
CWUser\PrefCaptionType
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:418
CWUser\Set
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:268
CWUser\UIPREF_AVOID_BLUEYELLOW
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:377
MetadataSchema
Metadata schema (in effect a Factory class for MetadataField).
Definition: MetadataSchema.php:13
CWUser\UIPREF_CONTENTVIEW_TEXTINTENSIVE
const UIPREF_CONTENTVIEW_TEXTINTENSIVE
Definition: CWUser.php:391
CWUser\UIPREF_AVOID_REDBLACK
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:380
CWUser\PrefImageDescriptionLanguage
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:415
CWUser\$Resource
$Resource
The user resource associated with the user or NULL if the user isn't logged in.
Definition: CWUser.php:299
CWUser\Privileges
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:57
CWUser\ResourceId
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:74
CWUser\UIPREF_AVOID_REDGREEN
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:376
CWUser\Delete
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:224
DB_NOVALUE
const DB_NOVALUE
Definition: Axis--Database.php:1284
CWUser\UIPREF_AVOID_GREENYELLOW
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:378
CWUser\UIPREF_AUDIODESCRIPTION_NONE
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:395
CWUser\UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:402
CWUser\PrefCaptionRate
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:419
Resource\Set
Set($FieldNameOrObject, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:977
CWUser\PrefBackgroundColor
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:408
Resource\Id
Id()
Retrieve numerical resource ID.
Definition: Resource.php:261
CWUser\PrefSignLanguage
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:417
CWUser\FetchAssociatedResource
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:315
CWUser\IsResourceObjectSet
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:363
CWUser\UIPREF_AVOID_USEMAXMONOCHR
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:382
CWUser\UIPREF_CONTENTDENSITY_NOPREFERENCE
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:385
CWUser\HasPriv
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
CWUser\UIPREF_AUDIODESCRIPTION_STANDARD
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: CWUser.php:396
CWUser\Get
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:242
CWUser\PrefAudioDescriptionLanguage
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:413
U_NOSUCHUSER
const U_NOSUCHUSER
Definition: Axis--User.php:22
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:53
CWUser\UIPREF_CONTENTDENSITY_DETAILED
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:386
CWUser\UIPREF_CONTENTVIEW_NOPREFERENCE
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:390
User
Definition: Axis--User.php:43
CWUser\PrefContentView
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:411
PrivilegeSet
Set of privileges used to access resource information or other parts of the system.
Definition: PrivilegeSet.php:16
CWUser\PrefContentDensity
PrefContentDensity($NewValue=DB_NOVALUE)
Definition: CWUser.php:410
CWUser\PrefFontColor
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:407
CWUser\PrefColorAvoidanceFlags
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:409
Email
Electronic mail message.
Definition: Email.php:14
PHP
PHP
Definition: OAIClient.php:39
CWUser\__construct
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:21
U_ERROR
const U_ERROR
Definition: Axis--User.php:20
CWUser\UIPREF_CAPTIONTYPE_VERBATIM
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:401
CWUser\PrefVisualDescriptionLanguage
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:414
CWUser\EmailWrapper
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:131
User\SetEmailFunction
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: Axis--User.php:178
CWUser\UIPREF_CONTENTVIEW_IMAGEINTENSIVE
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:392
CWUser\UIPREF_AVOID_RED
const UIPREF_AVOID_RED
Definition: CWUser.php:375
Resource\Get
Get($FieldNameOrObject, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:353
CWUser\PrefAudioDescriptionLevel
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:412
User\$Result
$Result
Definition: Axis--User.php:963
PrivilegeSetCompatibilityShim
Compatibility layer allowing interfaces built against the privilege system from CWIS 3...
Definition: PrivilegeSetCompatibilityShim.php:15
CWUser\$MigratedUserFields
static $MigratedUserFields
Fields that were previously part of the APUsers table that have been migrated to the Resources table ...
Definition: CWUser.php:305
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
CWUser\GetDefaultUserFields
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:195
CWUser\UIPREF_AUDIODESCRIPTION_EXPANDED
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: CWUser.php:397
CWUser\UIPREF_CAPTIONTYPE_NONE
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:400
User\$UserId
$UserId
Definition: Axis--User.php:962
CWUser\GetCustomUserFields
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:169
U_OKAY
const U_OKAY
Definition: Axis--User.php:19
CWUser\UIPREF_AVOID_PURPLEGREY
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:381
CWUser\UIPREF_CONTENTDENSITY_OVERVIEW
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:387
CWUser\GetResource
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:84
ResourceFactory
Factory for Resource objects.
Definition: ResourceFactory.php:13
CWUser
CWIS-specific user class.
Definition: CWUser.php:13
Resource\Delete
Delete()
Remove resource (and accompanying associations) from database and delete any associated files...
Definition: Resource.php:148
CWUser\PrefFontSize
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:405