CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- 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
16 # ---- PUBLIC INTERFACE --------------------------------------------------
17
22 public function __construct($UserInfo=NULL)
23 {
24 static $EmailWrapperSet = FALSE;
25 if (!$EmailWrapperSet)
26 {
27 User::SetEmailFunction(array("CWUser", "EmailWrapper"));
28 $EmailWrapperSet = TRUE;
29 }
30
31 parent::__construct($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
59 public function Privileges(PrivilegeSet $NewValue = NULL)
60 {
61 if ($NewValue !== NULL)
62 {
63 throw new Exception(
64 "Attempt to set user privileges with CWUser::Privileges(), "
65 ."which is no longer supported");
66 }
67
68 return new PrivilegeSetCompatibilityShim($this);
69 }
70
76 public function ResourceId()
77 {
78 return $this->IsResourceObjectSet() ? $this->Resource->Id() : NULL;
79 }
80
86 public function GetResource()
87 {
88 return $this->IsResourceObjectSet() ? $this->Resource : NULL;
89 }
90
91
104 public function HasPriv($Privilege, $Privileges = NULL)
105 {
106 if ($Privilege instanceof PrivilegeSet)
107 {
108 if ($Privileges instanceof Resource)
109 {
110 return $Privilege->MeetsRequirements($this, $Privileges);
111 }
112 else
113 {
114 return $Privilege->MeetsRequirements($this);
115 }
116 }
117 else
118 {
119 return call_user_func_array( "parent::HasPriv", func_get_args() );
120 }
121 }
122
133 public static function EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
134 {
135 # extract "From" address from supplied headers if available
136 if (strlen($AdditionalHeaders))
137 {
138 $HeaderLines = explode("\n", $AdditionalHeaders);
139 $Headers = array();
140 foreach ($HeaderLines as $Line)
141 {
142 $HeaderLine = trim($Line);
143 if (preg_match("/^from:/i", $Line))
144 {
145 $From = preg_replace("/^from:/i", "", $Line);
146 }
147 else
148 {
149 $Headers[] = $HeaderLine;
150 }
151 }
152 }
153
154 # send message
155 $Msg = new Email();
156 if (isset($From)) { $Msg->From($From); }
157 $Msg->To($To);
158 $Msg->Subject($Subject);
159 $Msg->AddHeaders($Headers);
160 $Msg->Body($Message);
161 $Result = $Msg->Send();
162
163 # report success to caller
164 return $Result;
165 }
166
171 public static function GetCustomUserFields()
172 {
173 static $CustomFields;
174
175 if (!isset($CustomFields))
176 {
177 $CustomFields = array();
178 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
179
180 foreach ($Schema->GetFields() as $Field)
181 {
182 # they're custom if not owned by CWIS
183 if ($Field->Owner() != "CWISCore")
184 {
185 $CustomFields[$Field->Id()] = $Field;
186 }
187 }
188 }
189
190 return $CustomFields;
191 }
192
197 public static function GetDefaultUserFields()
198 {
199 static $DefaultFields;
200
201 if (!isset($DefaultFields))
202 {
203 $DefaultFields = array();
204 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
205
206 foreach ($Schema->GetFields() as $Field)
207 {
208 # they're default if owned by CWIS
209 if ($Field->Owner() == "CWISCore")
210 {
211 $DefaultFields[$Field->Id()] = $Field;
212 }
213 }
214 }
215
216 return $DefaultFields;
217 }
218
219 # ---- OVERRIDDEN METHODS ------------------------------------------------
220
226 public function Delete()
227 {
228 # delete the associated user resource if set
229 if (isset($this->Resource))
230 {
231 $this->Resource->Delete();
232 $this->Result = U_OKAY;
233 }
234
235 return parent::Delete();
236 }
237
244 public function Get($FieldName)
245 {
246 # provide backwards-compatibility for data migrated to users fields as
247 # of CWIS 3.0.0
248 if (in_array($FieldName, self::$MigratedUserFields))
249 {
250 # return NULL if the resource object isn't set
251 if (!$this->IsResourceObjectSet())
252 {
253 return NULL;
254 }
255
256 # return the value from the associated resource
257 return $this->Resource->Get($FieldName);
258 }
259
260 # otherwise, get it from the APUsers table
261 return parent::Get($FieldName);
262 }
263
270 public function Set($FieldName, $NewValue)
271 {
272 # provide backwards-compatibility for data migrated to users fields as
273 # of CWIS 3.0.0
274 if (in_array($FieldName, self::$MigratedUserFields))
275 {
276 # set the value only if the resource object is set
277 if ($this->IsResourceObjectSet())
278 {
279 $this->Resource->Set($FieldName, $NewValue);
280 $this->Result = U_OKAY;
281 }
282 }
283
284 # transform boolean values to 1 or 0 because that's what the User
285 # class expects
286 if (is_bool($NewValue))
287 {
288 $NewValue = $NewValue ? 1 : 0;
289 }
290
291 # update the APUsers table
292 return parent::Set($FieldName, $NewValue);
293 }
294
295 # ---- PRIVATE INTERFACE -------------------------------------------------
296
301 protected $Resource;
302
307 protected static $MigratedUserFields = array(
308 "RealName", "WebSite", "AddressLineOne", "AddressLineTwo", "City",
309 "State", "ZipCode", "Country");
310
317 protected function FetchAssociatedResource($UserId)
318 {
319 try
320 {
321 $Factory = new ResourceFactory(MetadataSchema::SCHEMAID_USER);
322 $Schema = new MetadataSchema(MetadataSchema::SCHEMAID_USER);
323 }
324
325 # couldn't get the factory or schema, which probably means CWIS hasn't
326 # been installed yet
327 catch (Exception $Exception)
328 {
329 return U_ERROR;
330 }
331
332 # the UserId field doesn't exist, which probably means CWIS hasn't been
333 # installed yet
334 if (!$Schema->FieldExists("UserId"))
335 {
336 return U_ERROR;
337 }
338
339 # get matching resources, which should only be one
340 $Field = $Schema->GetFieldByName("UserId");
341
342 $DB = new Database();
343 $DB->Query(
344 "SELECT ResourceId FROM ResourceUserInts WHERE ".
345 "FieldId=".intval($Field->Id()).
346 " AND UserId=".intval($UserId) );
347 $ResourceIds = $DB->FetchColumn("ResourceId");
348 $ResourceIdCount = count($ResourceIds);
349
350 # no resource found
351 if ($ResourceIdCount < 1)
352 {
353 return U_NOSUCHUSER;
354 }
355
356 # too many resources found
357 if ($ResourceIdCount > 1)
358 {
359 return U_ERROR;
360 }
361
362 # construct the associated resource and return it
363 return new Resource(array_shift($ResourceIds));
364 }
365
370 protected function IsResourceObjectSet()
371 {
372 # there must be a user ID, which is what the User class assumes, and the
373 # resource must be set
374 return isset($this->UserId) && isset($this->Resource);
375 }
376
377
378 # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (BEGIN)
379
380 # ---- user interface preference mnemonics
381 # color avoidance flags
382 const UIPREF_AVOID_RED = 1;
383 const UIPREF_AVOID_REDGREEN = 2;
384 const UIPREF_AVOID_BLUEYELLOW = 4;
385 const UIPREF_AVOID_GREENYELLOW = 8;
386 const UIPREF_AVOID_ORANGE = 16;
387 const UIPREF_AVOID_REDBLACK = 32;
388 const UIPREF_AVOID_PURPLEGREY = 64;
389 const UIPREF_AVOID_USEMAXMONOCHR = 128;
390
391 # content display options
392 const UIPREF_CONTENTDENSITY_NOPREFERENCE = 0;
393 const UIPREF_CONTENTDENSITY_DETAILED = 1;
394 const UIPREF_CONTENTDENSITY_OVERVIEW = 2;
395
396 # content view options
397 const UIPREF_CONTENTVIEW_NOPREFERENCE = 0;
398 const UIPREF_CONTENTVIEW_TEXTINTENSIVE = 1;
399 const UIPREF_CONTENTVIEW_IMAGEINTENSIVE = 2;
400
401 # audio description options
402 const UIPREF_AUDIODESCRIPTION_NONE = 0;
403 const UIPREF_AUDIODESCRIPTION_STANDARD = 1;
404 const UIPREF_AUDIODESCRIPTION_EXPANDED = 2;
405
406 # caption type options
407 const UIPREF_CAPTIONTYPE_NONE = 0;
408 const UIPREF_CAPTIONTYPE_VERBATIM = 1;
409 const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL = 2;
410
411 // @codingStandardsIgnoreStart
412
413 # user interface / accessibility preferences
414 function PrefFontSize($NewValue = DB_NOVALUE)
415 { return 0; }
416
417 function PrefFontTypeFace($NewValue = DB_NOVALUE)
418 { return 0; }
419
420 function PrefFontColor($NewValue = DB_NOVALUE)
421 { return 0; }
422
423 function PrefBackgroundColor($NewValue = DB_NOVALUE)
424 { return 0; }
425
426 function PrefColorAvoidanceFlags($NewValue = DB_NOVALUE)
427 { return 0; }
428
429 function PrefContentDensity($NewValue = DB_NOVALUE)
430 { return 0; }
431
432 function PrefContentView($NewValue = DB_NOVALUE)
433 { return 0; }
434
435 function PrefAudioDescriptionLevel($NewValue = DB_NOVALUE)
436 { return 0; }
437
438 function PrefAudioDescriptionLanguage($NewValue = DB_NOVALUE)
439 { return 0; }
440
441 function PrefVisualDescriptionLanguage($NewValue = DB_NOVALUE)
442 { return 0; }
443
444 function PrefImageDescriptionLanguage($NewValue = DB_NOVALUE)
445 { return 0; }
446
447 function PrefUseGraphicAlternatives($NewValue = DB_NOVALUE)
448 { return 0; }
449
450 function PrefSignLanguage($NewValue = DB_NOVALUE)
451 { return 0; }
452
453 function PrefCaptionType($NewValue = DB_NOVALUE)
454 { return 0; }
455
456 function PrefCaptionRate($NewValue = DB_NOVALUE)
457 { return 0; }
458
459 // @codingStandardsIgnoreEnd
460 # ---- MAINTAINED FOR BACKWARD COMPATIBILITY IN INTERFACES (END)
461 }
CWUser\PrefFontTypeFace
PrefFontTypeFace($NewValue=DB_NOVALUE)
Definition: CWUser.php:417
CWUser\PrefUseGraphicAlternatives
PrefUseGraphicAlternatives($NewValue=DB_NOVALUE)
Definition: CWUser.php:447
User\$DB
$DB
Definition: User.php:976
CWUser\UIPREF_AVOID_ORANGE
const UIPREF_AVOID_ORANGE
Definition: CWUser.php:386
CWUser\PrefCaptionType
PrefCaptionType($NewValue=DB_NOVALUE)
Definition: CWUser.php:453
CWUser\Set
Set($FieldName, $NewValue)
Set a value for the specified field.
Definition: CWUser.php:270
CWUser\UIPREF_AVOID_BLUEYELLOW
const UIPREF_AVOID_BLUEYELLOW
Definition: CWUser.php:384
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:398
CWUser\UIPREF_AVOID_REDBLACK
const UIPREF_AVOID_REDBLACK
Definition: CWUser.php:387
CWUser\PrefImageDescriptionLanguage
PrefImageDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:444
CWUser\$Resource
$Resource
The user resource associated with the user or NULL if the user isn&#39;t logged in.
Definition: CWUser.php:301
CWUser\Privileges
Privileges(PrivilegeSet $NewValue=NULL)
THIS FUNCTION HAS BEEN DEPRECATED This provides compatibility for interfaces written to use a version...
Definition: CWUser.php:59
CWUser\ResourceId
ResourceId()
Get the ID of the user resource associated with the user.
Definition: CWUser.php:76
Database
SQL database abstraction object with smart query caching.
Definition: Database.php:22
CWUser\UIPREF_AVOID_REDGREEN
const UIPREF_AVOID_REDGREEN
Definition: CWUser.php:383
CWUser\Delete
Delete()
Delete the user and its associated user resource.
Definition: CWUser.php:226
CWUser\UIPREF_AVOID_GREENYELLOW
const UIPREF_AVOID_GREENYELLOW
Definition: CWUser.php:385
CWUser\UIPREF_AUDIODESCRIPTION_NONE
const UIPREF_AUDIODESCRIPTION_NONE
Definition: CWUser.php:402
CWUser\UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
const UIPREF_CAPTIONTYPE_REDUCEDREADINGLEVEL
Definition: CWUser.php:409
CWUser\PrefCaptionRate
PrefCaptionRate($NewValue=DB_NOVALUE)
Definition: CWUser.php:456
CWUser\PrefBackgroundColor
PrefBackgroundColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:423
Resource\Id
Id()
Retrieve numerical resource ID.
Definition: Resource.php:287
CWUser\PrefSignLanguage
PrefSignLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:450
CWUser\FetchAssociatedResource
FetchAssociatedResource($UserId)
Fetch the associated user resource based off of a user ID.
Definition: CWUser.php:317
CWUser\IsResourceObjectSet
IsResourceObjectSet()
Determine if the resource object for this object is set.
Definition: CWUser.php:370
U_ERROR
const U_ERROR
Definition: User.php:19
CWUser\UIPREF_AVOID_USEMAXMONOCHR
const UIPREF_AVOID_USEMAXMONOCHR
Definition: CWUser.php:389
CWUser\UIPREF_CONTENTDENSITY_NOPREFERENCE
const UIPREF_CONTENTDENSITY_NOPREFERENCE
Definition: CWUser.php:392
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:104
CWUser\UIPREF_AUDIODESCRIPTION_STANDARD
const UIPREF_AUDIODESCRIPTION_STANDARD
Definition: CWUser.php:403
CWUser\Get
Get($FieldName)
Get a value from the specified field.
Definition: CWUser.php:244
CWUser\PrefAudioDescriptionLanguage
PrefAudioDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:438
MetadataSchema\SCHEMAID_USER
const SCHEMAID_USER
Definition: MetadataSchema.php:55
CWUser\UIPREF_CONTENTDENSITY_DETAILED
const UIPREF_CONTENTDENSITY_DETAILED
Definition: CWUser.php:393
CWUser\UIPREF_CONTENTVIEW_NOPREFERENCE
const UIPREF_CONTENTVIEW_NOPREFERENCE
Definition: CWUser.php:397
User
Definition: User.php:41
CWUser\PrefContentView
PrefContentView($NewValue=DB_NOVALUE)
Definition: CWUser.php:432
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:429
CWUser\PrefFontColor
PrefFontColor($NewValue=DB_NOVALUE)
Definition: CWUser.php:420
CWUser\PrefColorAvoidanceFlags
PrefColorAvoidanceFlags($NewValue=DB_NOVALUE)
Definition: CWUser.php:426
Email
Electronic mail message.
Definition: Email.php:14
Resource\Get
Get($Field, $ReturnObject=FALSE, $IncludeVariants=FALSE)
Retrieve value using field name or field object.
Definition: Resource.php:410
PHP
PHP
Definition: RSSClient--Test.php:12
CWUser\__construct
__construct($UserInfo=NULL)
Load user data from the given user info or from the session if available.
Definition: CWUser.php:22
DB_NOVALUE
const DB_NOVALUE
Definition: Database.php:1541
CWUser\UIPREF_CAPTIONTYPE_VERBATIM
const UIPREF_CAPTIONTYPE_VERBATIM
Definition: CWUser.php:408
CWUser\PrefVisualDescriptionLanguage
PrefVisualDescriptionLanguage($NewValue=DB_NOVALUE)
Definition: CWUser.php:441
CWUser\EmailWrapper
static EmailWrapper($To, $Subject, $Message, $AdditionalHeaders)
Adapter method to bridge between AxisPHP User class and ScoutLib Email class.
Definition: CWUser.php:133
User\SetEmailFunction
static SetEmailFunction($NewValue)
Set email function to use instead of mail().
Definition: User.php:177
U_OKAY
const U_OKAY
Definition: User.php:18
CWUser\UIPREF_CONTENTVIEW_IMAGEINTENSIVE
const UIPREF_CONTENTVIEW_IMAGEINTENSIVE
Definition: CWUser.php:399
CWUser\UIPREF_AVOID_RED
const UIPREF_AVOID_RED
Definition: CWUser.php:382
CWUser\PrefAudioDescriptionLevel
PrefAudioDescriptionLevel($NewValue=DB_NOVALUE)
Definition: CWUser.php:435
User\$Result
$Result
Definition: User.php:978
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:307
Resource
Represents a "resource" in CWIS.
Definition: Resource.php:13
CWUser\GetDefaultUserFields
static GetDefaultUserFields()
Get the default user fields.
Definition: CWUser.php:197
CWUser\UIPREF_AUDIODESCRIPTION_EXPANDED
const UIPREF_AUDIODESCRIPTION_EXPANDED
Definition: CWUser.php:404
U_NOSUCHUSER
const U_NOSUCHUSER
Definition: User.php:21
CWUser\UIPREF_CAPTIONTYPE_NONE
const UIPREF_CAPTIONTYPE_NONE
Definition: CWUser.php:407
User\$UserId
$UserId
Definition: User.php:977
CWUser\GetCustomUserFields
static GetCustomUserFields()
Get all custom user fields.
Definition: CWUser.php:171
Resource\Set
Set($Field, $NewValue, $Reset=FALSE)
Set value using field name or field object.
Definition: Resource.php:1142
CWUser\UIPREF_AVOID_PURPLEGREY
const UIPREF_AVOID_PURPLEGREY
Definition: CWUser.php:388
CWUser\UIPREF_CONTENTDENSITY_OVERVIEW
const UIPREF_CONTENTDENSITY_OVERVIEW
Definition: CWUser.php:394
CWUser\GetResource
GetResource()
Get the associated user resource for this user.
Definition: CWUser.php:86
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:138
CWUser\PrefFontSize
PrefFontSize($NewValue=DB_NOVALUE)
Definition: CWUser.php:414