00001 <?PHP 00002 00003 # 00004 # FILE: SPT--Resource.php 00005 # 00006 # AUTHOR: Edward Almasy 00007 # 00008 # Part of the Collection Workflow Integration System (CWIS) 00009 # Copyright 2002-2009 Internet Scout 00010 # http://scout.wisc.edu 00011 # 00012 00016 class Resource { 00017 00018 # ---- PUBLIC INTERFACE -------------------------------------------------- 00019 00020 # object constructor (creates temp resource if no resource ID supplied) 00021 00025 function Resource($ResourceId = NULL) 00026 { 00027 $this->DB = new SPTDatabase(); 00028 $DB = $this->DB; 00029 $this->Schema = new MetadataSchema(); 00030 00031 # if resource ID supplied 00032 if ($ResourceId !== NULL) 00033 { 00034 # save resource ID 00035 $this->Id = intval($ResourceId); 00036 00037 # locate resource in database 00038 $DB->Query("SELECT * FROM Resources WHERE ResourceId = ".$this->Id); 00039 00040 # if unable to locate resource 00041 $Record = $DB->FetchRow(); 00042 if ($Record == FALSE) 00043 { 00044 # set status to -1 to indicate that creation failed 00045 $this->LastStatus = -1; 00046 } 00047 else 00048 { 00049 # load in attributes from database 00050 $this->DBFields = $Record; 00051 $this->CumulativeRating = $Record["CumulativeRating"]; 00052 00053 # set status to 1 to indicate that creation succeeded 00054 $this->LastStatus = 1; 00055 } 00056 } 00057 else 00058 { 00059 # lock DB tables to prevent next ID from being grabbed 00060 $DB->Query( 00061 "LOCK TABLES Resources write, APUsers read, ". 00062 "APSessions write, APSessionData write"); 00063 00064 # find next temp resource ID 00065 $Factory = new ResourceFactory(); 00066 $this->Id = $Factory->GetNextTempItemId(); 00067 00068 # write out new resource record with temp resource ID and user ID 00069 global $User; 00070 $DB->Query("INSERT INTO Resources (ResourceId, AddedById," 00071 ." LastModifiedById, DateLastModified, DateOfRecordCreation)" 00072 ." VALUES (".$this->Id.", " 00073 .$User->Get("UserId").", " 00074 .$User->Get("UserId").", " 00075 ."NOW(), NOW())"); 00076 00077 # release DB tables 00078 $DB->Query("UNLOCK TABLES"); 00079 00080 # load in attributes from database 00081 $DB->Query("SELECT * FROM Resources WHERE ResourceId = ".$this->Id); 00082 $this->DBFields = $DB->FetchRow(); 00083 $this->CumulativeRating = $this->DBFields["CumulativeRating"]; 00084 00085 # set any default values 00086 $Schema = new MetadataSchema(); 00087 $Fields = $Schema->GetFields(MetadataSchema::MDFTYPE_OPTION 00088 |MetadataSchema::MDFTYPE_FLAG 00089 |MetadataSchema::MDFTYPE_NUMBER 00090 |MetadataSchema::MDFTYPE_POINT); 00091 foreach ($Fields as $Field) 00092 { 00093 $this->SetByField($Field, $Field->DefaultValue()); 00094 } 00095 00096 # set status to 1 to indicate that creation succeeded 00097 $this->LastStatus = 1; 00098 } 00099 } 00100 00104 function Delete() 00105 { 00106 global $SysConfig; 00107 00108 # grab list of classifications 00109 $Classifications = $this->Classifications(); 00110 00111 # delete resource/classification intersections 00112 $DB = $this->DB; 00113 $DB->Query("DELETE FROM ResourceClassInts WHERE ResourceId = ".$this->Id()); 00114 00115 # for each classification type 00116 foreach ($Classifications as $ClassType => $ClassesOfType) 00117 { 00118 # for each classification of that type 00119 foreach ($ClassesOfType as $ClassId => $ClassName) 00120 { 00121 # recalculate resource count for classification 00122 $Class = new Classification($ClassId); 00123 $Class->RecalcResourceCount(); 00124 } 00125 } 00126 00127 # delete resource/name intersections 00128 $DB->Query("DELETE FROM ResourceNameInts WHERE ResourceId = ".$this->Id()); 00129 00130 # delete any associated images not in use by other resources 00131 $Fields = $this->Schema->GetFields(MetadataSchema::MDFTYPE_IMAGE); 00132 foreach ($Fields as $Field) 00133 { 00134 $ImageId = $DB->Query("SELECT `".$Field->DBFieldName() 00135 ."` FROM Resources WHERE ResourceId = ".$this->Id(), 00136 $Field->DBFieldName()); 00137 if ($ImageId > 0) 00138 { 00139 $ImageCount = $DB->Query("SELECT COUNT(*) AS ImageCount FROM Resources" 00140 ." WHERE ".$Field->DBFieldName()." = ".$ImageId, 00141 "ImageCount"); 00142 if ($ImageCount < 2) 00143 { 00144 $Image = new SPTImage($ImageId); 00145 $Image->Delete(); 00146 } 00147 } 00148 } 00149 00150 # delete any associated files 00151 $Factory = new FileFactory(NULL); 00152 $Files = $Factory->GetFilesForResource($this->Id()); 00153 foreach ($Files as $File) 00154 { 00155 $File->Delete(); 00156 } 00157 00158 # delete resource record from database 00159 $DB->Query("DELETE FROM Resources WHERE ResourceId = ".$this->Id()); 00160 00161 # drop item from search engine and recommender system 00162 if ($SysConfig->SearchDBEnabled()) 00163 { 00164 $SearchEngine = new SPTSearchEngine(); 00165 $SearchEngine->DropItem($this->Id()); 00166 } 00167 if ($SysConfig->RecommenderDBEnabled()) 00168 { 00169 $Recommender = new SPTRecommender(); 00170 $Recommender->DropItem($this->Id()); 00171 } 00172 00173 # delete any resource comments 00174 $DB->Query("DELETE FROM Messages WHERE ParentId = ".$this->Id); 00175 } 00176 00181 function Status() { return $this->LastStatus; } 00182 00187 function Id() { return $this->Id; } 00188 00194 function IsTempResource($NewSetting = NULL) 00195 { 00196 # if new temp resource setting supplied 00197 if (!is_null($NewSetting)) 00198 { 00199 # if caller requested to switch 00200 $DB = $this->DB; 00201 if ((($this->Id() < 0) && ($NewSetting == FALSE)) 00202 || (($this->Id() >= 0) && ($NewSetting == TRUE))) 00203 { 00204 # lock DB tables to prevent next ID from being grabbed 00205 $DB->Query("LOCK TABLES Resources write"); 00206 00207 # get next temp resource ID 00208 $OldResourceId = $this->Id; 00209 $Factory = new ResourceFactory(); 00210 if ($NewSetting == TRUE) 00211 { 00212 $this->Id = $Factory->GetNextTempItemId(); 00213 } 00214 else 00215 { 00216 $this->Id = $Factory->GetNextItemId(); 00217 } 00218 00219 # change resource ID 00220 $DB->Query("UPDATE Resources SET ResourceId = ". 00221 $this->Id. " WHERE ResourceId = ".$OldResourceId); 00222 00223 # release DB tables 00224 $DB->Query("UNLOCK TABLES"); 00225 00226 # change associations 00227 unset($this->ClassificationCache); 00228 $DB->Query("UPDATE ResourceClassInts SET ResourceId = ". 00229 $this->Id. " WHERE ResourceId = ".$OldResourceId); 00230 unset($this->ControlledNameCache); 00231 unset($this->ControlledNameVariantCache); 00232 $DB->Query("UPDATE ResourceNameInts SET ResourceId = ". 00233 $this->Id. " WHERE ResourceId = ".$OldResourceId); 00234 } 00235 } 00236 00237 # report to caller whether we are a temp resource 00238 return ($this->Id() < 0) ? TRUE : FALSE; 00239 } 00240 00241 00242 # --- Generic Attribute Retrieval Methods ------------------------------- 00243 00252 function Get($FieldNameOrObject, $ReturnObject = FALSE, $IncludeVariants = FALSE) 00253 { 00254 # load field object if needed 00255 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject 00256 : $this->Schema->GetFieldByName($FieldNameOrObject); 00257 00258 # grab database field name 00259 $DBFieldName = $Field->DBFieldName(); 00260 00261 # format return value based on field type 00262 switch ($Field->Type()) 00263 { 00264 case MetadataSchema::MDFTYPE_TEXT: 00265 case MetadataSchema::MDFTYPE_PARAGRAPH: 00266 case MetadataSchema::MDFTYPE_NUMBER: 00267 case MetadataSchema::MDFTYPE_FLAG: 00268 case MetadataSchema::MDFTYPE_URL: 00269 if (isset($this->DBFields[$DBFieldName])) 00270 { 00271 $ReturnValue = $this->DBFields[$DBFieldName]; 00272 } 00273 else 00274 { 00275 $ReturnValue = NULL; 00276 } 00277 break; 00278 00279 case MetadataSchema::MDFTYPE_POINT: 00280 $ReturnValue = array("X" => $this->DBFields[$DBFieldName."X"], 00281 "Y" => $this->DBFields[$DBFieldName."Y"]); 00282 break; 00283 00284 case MetadataSchema::MDFTYPE_DATE: 00285 $Date = new SPTDate($this->DBFields[$DBFieldName."Begin"], 00286 $this->DBFields[$DBFieldName."End"], 00287 $this->DBFields[$DBFieldName."Precision"]); 00288 if ($ReturnObject) 00289 { 00290 $ReturnValue = $Date; 00291 } 00292 else 00293 { 00294 $ReturnValue = $Date->Formatted(); 00295 } 00296 break; 00297 00298 case MetadataSchema::MDFTYPE_TIMESTAMP: 00299 $ReturnValue = $this->DBFields[$DBFieldName]; 00300 break; 00301 00302 case MetadataSchema::MDFTYPE_TREE: 00303 # start with empty array 00304 $ReturnValue = array(); 00305 00306 # if classification cache has not been loaded 00307 if (!isset($this->ClassificationCache)) 00308 { 00309 # load all classifications associated with this resource into cache 00310 $this->ClassificationCache = array(); 00311 $this->DB->Query("SELECT Classifications.ClassificationId,Classifications.FieldId,ClassificationName " 00312 ."FROM ResourceClassInts, Classifications " 00313 ."WHERE ResourceClassInts.ResourceId = ".$this->Id." " 00314 ."AND ResourceClassInts.ClassificationId = Classifications.ClassificationId "); 00315 while ($Record = $this->DB->FetchRow()) 00316 { 00317 $this->ClassificationCache[$Record["ClassificationId"]]["Name"] = 00318 $Record["ClassificationName"]; 00319 $this->ClassificationCache[$Record["ClassificationId"]]["FieldId"] = 00320 $Record["FieldId"]; 00321 } 00322 } 00323 00324 # for each entry in classification cache 00325 foreach ($this->ClassificationCache as $ClassificationId => $ClassificationInfo) 00326 { 00327 # if classification ID matches field we are looking for 00328 if ($ClassificationInfo["FieldId"] == $Field->Id()) 00329 { 00330 # add field to result 00331 if ($ReturnObject) 00332 { 00333 $ReturnValue[$ClassificationId] = new Classification($ClassificationId); 00334 } 00335 else 00336 { 00337 $ReturnValue[$ClassificationId] = $ClassificationInfo["Name"]; 00338 } 00339 } 00340 } 00341 break; 00342 00343 case MetadataSchema::MDFTYPE_CONTROLLEDNAME: 00344 case MetadataSchema::MDFTYPE_OPTION: 00345 # start with empty array 00346 $ReturnValue = array(); 00347 00348 # if controlled name cache has not been loaded 00349 if (!isset($this->ControlledNameCache)) 00350 { 00351 # load all controlled names associated with this resource into cache 00352 $this->ControlledNameCache = array(); 00353 $this->DB->Query("SELECT ControlledNames.ControlledNameId,ControlledNames.FieldId,ControlledName " 00354 ."FROM ResourceNameInts, ControlledNames " 00355 ."WHERE ResourceNameInts.ResourceId = ".$this->Id." " 00356 ."AND ResourceNameInts.ControlledNameId = ControlledNames.ControlledNameId "); 00357 while ($Record = $this->DB->FetchRow()) 00358 { 00359 $this->ControlledNameCache[$Record["ControlledNameId"]]["Name"] = $Record["ControlledName"]; 00360 $this->ControlledNameCache[$Record["ControlledNameId"]]["FieldId"] = $Record["FieldId"]; 00361 } 00362 } 00363 00364 # if variant names requested and variant name cache has not been loaded 00365 if ($IncludeVariants && !isset($this->ControlledNameVariantCache)) 00366 { 00367 # load all controlled names associated with this resource into cache 00368 $this->ControlledNameVariantCache = array(); 00369 $this->DB->Query("SELECT ControlledNames.ControlledNameId,ControlledNames.FieldId,ControlledName,VariantName " 00370 ."FROM ResourceNameInts, ControlledNames, VariantNames " 00371 ."WHERE ResourceNameInts.ResourceId = ".$this->Id." " 00372 ."AND ResourceNameInts.ControlledNameId = ControlledNames.ControlledNameId " 00373 ."AND VariantNames.ControlledNameId = ControlledNames.ControlledNameId"); 00374 while ($Record = $this->DB->FetchRow()) 00375 { 00376 $this->ControlledNameVariantCache[$Record["ControlledNameId"]][] = $Record["VariantName"]; 00377 } 00378 } 00379 00380 # for each entry in controlled name cache 00381 foreach ($this->ControlledNameCache as $ControlledNameId => $ControlledNameInfo) 00382 { 00383 # if controlled name type matches field we are looking for 00384 if ($ControlledNameInfo["FieldId"] == $Field->Id()) 00385 { 00386 # if objects requested 00387 if ($ReturnObject) 00388 { 00389 $ReturnValue[$ControlledNameId] = 00390 new ControlledName($ControlledNameId); 00391 } 00392 else 00393 { 00394 # if variant names requested 00395 if ($IncludeVariants) 00396 { 00397 # add field to result 00398 $ReturnValue[] = $ControlledNameInfo["Name"]; 00399 00400 # add any variant names to result 00401 if (isset($this->ControlledNameVariantCache[$ControlledNameId])) 00402 { 00403 $ReturnValue = array_merge($ReturnValue, $this->ControlledNameVariantCache[$ControlledNameId]); 00404 } 00405 } 00406 else 00407 { 00408 # add field with index to result 00409 $ReturnValue[$ControlledNameId] = $ControlledNameInfo["Name"]; 00410 } 00411 } 00412 } 00413 } 00414 break; 00415 00416 case MetadataSchema::MDFTYPE_USER: 00417 $User = new User($this->DB, (int)$this->DBFields[$DBFieldName]); 00418 if ($ReturnObject) 00419 { 00420 $ReturnValue = $User; 00421 } 00422 else 00423 { 00424 $ReturnValue = $User->Get("UserName"); 00425 } 00426 break; 00427 00428 case MetadataSchema::MDFTYPE_IMAGE: 00429 if ($this->DBFields[$DBFieldName] > 0) 00430 { 00431 $ImageObject = new SPTImage($this->DBFields[$DBFieldName]); 00432 if ($ReturnObject) 00433 { 00434 $ReturnValue = $ImageObject; 00435 } 00436 else 00437 { 00438 $ReturnValue = $ImageObject->AltText(); 00439 } 00440 } 00441 else 00442 { 00443 $ReturnValue = NULL; 00444 } 00445 break; 00446 00447 case MetadataSchema::MDFTYPE_FILE: 00448 # retrieve files using factory 00449 $Factory = new FileFactory($Field->Id()); 00450 $ReturnValue = $Factory->GetFilesForResource( 00451 $this->Id, $ReturnObject); 00452 break; 00453 00454 default: 00455 # ERROR OUT 00456 exit("<br>SPT - ERROR: attempt to retrieve unknown resource field type (".$Field->Type().")<br>\n"); 00457 break; 00458 } 00459 00460 # return formatted value to caller 00461 return $ReturnValue; 00462 } 00467 function GetByField($FieldNameOrObject, 00468 $ReturnObject = FALSE, $IncludeVariants = FALSE) 00469 { return $this->Get($FieldNameOrObject, $ReturnObject, $IncludeVariants); } 00470 00479 function GetByFieldId($FieldId, $ReturnObject = FALSE, $IncludeVariants = FALSE) 00480 { 00481 $Field = $this->Schema->GetField($FieldId); 00482 return ($Field) ? $this->Get($Field, $ReturnObject, $IncludeVariants) : NULL; 00483 } 00484 00485 # return all resource attributes as an array 00486 function GetAsArray($IncludeDisabledFields = FALSE, $ReturnObjects = TRUE) 00487 { 00488 # retrieve field info 00489 $Fields = $this->Schema->GetFields(); 00490 00491 # for each field 00492 foreach ($Fields as $Field) 00493 { 00494 # if field is enabled or caller requested disabled fields 00495 if ($Field->Enabled() || $IncludeDisabledFields) 00496 { 00497 # retrieve info and add it to the array 00498 $FieldStrings[$Field->Name()] = $this->Get($Field, $ReturnObjects); 00499 00500 # if field uses qualifiers 00501 if ($Field->UsesQualifiers()) 00502 { 00503 # get qualifier attributes and add to the array 00504 $FieldStrings[$Field->Name()." Qualifier"] = 00505 $this->GetQualifierByField($Field, $ReturnObjects); 00506 } 00507 } 00508 } 00509 00510 # add in internal values 00511 $FieldStrings["ResourceId"] = $this->Id(); 00512 $FieldStrings["CumulativeRating"] = $this->CumulativeRating(); 00513 00514 # return array to caller 00515 return $FieldStrings; 00516 } 00517 00524 function GetQualifier($FieldName, $ReturnObject = TRUE) 00525 { 00526 $Field = $this->Schema->GetFieldByName($FieldName); 00527 return $this->GetQualifierByField($Field, $ReturnObject); 00528 } 00529 00536 function GetQualifierByFieldId($FieldId, $ReturnObject = TRUE) 00537 { 00538 $Field = $this->Schema->GetField($FieldId); 00539 return $this->GetQualifierByField($Field, $ReturnObject); 00540 } 00541 00548 function GetQualifierByField($Field, $ReturnObject = TRUE) 00549 { 00550 # assume no qualifiers if not otherwise determined 00551 $ReturnValue = NULL; 00552 00553 # if field uses qualifiers 00554 if ($Field->UsesQualifiers()) 00555 { 00556 # retrieve qualifiers based on field type 00557 switch ($Field->Type()) 00558 { 00559 case MetadataSchema::MDFTYPE_TREE: 00560 case MetadataSchema::MDFTYPE_CONTROLLEDNAME: 00561 case MetadataSchema::MDFTYPE_OPTION: 00562 # retrieve list of items 00563 $Items = $this->Get($Field); 00564 00565 # if field uses item-level qualifiers 00566 if ($Field->HasItemLevelQualifiers()) 00567 { 00568 # determine general item name in DB 00569 $TableName = ($Field->Type() == MetadataSchema::MDFTYPE_TREE) 00570 ? "Classification" : "ControlledName"; 00571 00572 # for each item 00573 foreach ($Items as $ItemId => $ItemName) 00574 { 00575 # look up qualifier for item 00576 $QualId = $this->DB->Query( 00577 "SELECT * FROM ".$TableName."s" 00578 ." WHERE ".$TableName."Id = ".$ItemId 00579 , "QualifierId"); 00580 00581 00582 if ($QualId > 0) 00583 { 00584 # if object was requested by caller 00585 if ($ReturnObject) 00586 { 00587 # load qualifier and add to return value array 00588 $ReturnValue[$ItemId] = new Qualifier($QualId); 00589 } 00590 else 00591 { 00592 # add qualifier ID to return value array 00593 $ReturnValue[$ItemId] = $QualId; 00594 } 00595 } 00596 else 00597 { 00598 # add NULL to return value array for this item 00599 $ReturnValue[$ItemId] = NULL; 00600 } 00601 } 00602 } 00603 else 00604 { 00605 # for each item 00606 foreach ($Items as $ItemId => $ItemName) 00607 { 00608 # if object was requested by caller 00609 if ($ReturnObject) 00610 { 00611 # load default qualifier and add to return value array 00612 $ReturnValue[$ItemId] = new Qualifier($Field->DefaultQualifier()); 00613 } 00614 else 00615 { 00616 # add default qualifier ID to return value array 00617 $ReturnValue[$ItemId] = $Field->DefaultQualifier(); 00618 } 00619 } 00620 } 00621 break; 00622 00623 default: 00624 # if field uses item-level qualifiers 00625 if ($Field->HasItemLevelQualifiers()) 00626 { 00627 # if qualifier available 00628 if ($this->DBFields[$Field->DBFieldName()."Qualifier"] > 0) 00629 { 00630 # if object was requested by caller 00631 if ($ReturnObject) 00632 { 00633 # return qualifier for field 00634 $ReturnValue = new Qualifier($this->DBFields[$Field->DBFieldName()."Qualifier"]); 00635 } 00636 else 00637 { 00638 # return qualifier ID for field 00639 $ReturnValue = $this->DBFields[$Field->DBFieldName()."Qualifier"]; 00640 } 00641 } 00642 } 00643 else 00644 { 00645 # if default qualifier available 00646 if ($Field->DefaultQualifier() > 0) 00647 { 00648 # if object was requested by caller 00649 if ($ReturnObject) 00650 { 00651 # return default qualifier 00652 $ReturnValue = new Qualifier($Field->DefaultQualifier()); 00653 } 00654 else 00655 { 00656 # return default qualifier ID 00657 $ReturnValue = $Field->DefaultQualifier(); 00658 } 00659 } 00660 } 00661 break; 00662 } 00663 } 00664 00665 # return qualifier object or ID (or array of same) to caller 00666 return $ReturnValue; 00667 } 00668 00669 00670 # --- Generic Attribute Setting Methods --------------------------------- 00671 00672 # set value using field name or field object 00673 function Set($FieldNameOrObject, $NewValue) 00674 { 00675 # load field object if needed 00676 $Field = is_object($FieldNameOrObject) ? $FieldNameOrObject 00677 : $this->Schema->GetFieldByName($FieldNameOrObject); 00678 00679 # grab commonly-used values for local use 00680 $DB = $this->DB; 00681 $ResourceId = $this->Id; 00682 00683 # grab database field name 00684 $DBFieldName = $Field->DBFieldName(); 00685 00686 # store value in DB based on field type 00687 switch ($Field->Type()) 00688 { 00689 case MetadataSchema::MDFTYPE_TEXT: 00690 case MetadataSchema::MDFTYPE_PARAGRAPH: 00691 case MetadataSchema::MDFTYPE_URL: 00692 # save value directly to DB 00693 $DB->Query("UPDATE Resources SET `" 00694 .$DBFieldName."` = '".addslashes($NewValue)."' " 00695 ."WHERE ResourceId = ".$ResourceId); 00696 00697 # save value locally 00698 $this->DBFields[$DBFieldName] = $NewValue; 00699 break; 00700 00701 case MetadataSchema::MDFTYPE_NUMBER: 00702 # save value directly to DB 00703 if (is_null($NewValue)) 00704 { 00705 $DB->Query("UPDATE Resources SET `" 00706 .$DBFieldName."` = NULL" 00707 ." WHERE ResourceId = ".$ResourceId); 00708 } 00709 else 00710 { 00711 $DB->Query("UPDATE Resources SET `" 00712 .$DBFieldName."` = ".intval($NewValue) 00713 ." WHERE ResourceId = ".$ResourceId); 00714 } 00715 00716 # save value locally 00717 $this->DBFields[$DBFieldName] = $NewValue; 00718 break; 00719 00720 00721 case MetadataSchema::MDFTYPE_POINT: 00722 if (is_null($NewValue)) 00723 { 00724 $DB->Query("UPDATE Resources SET " 00725 ."`".$DBFieldName."X` = NULL, " 00726 ."`".$DBFieldName."Y` = NULL " 00727 ."WHERE ResourceId = ".$ResourceId); 00728 $this->DBFields[$DBFieldName."X"] = NULL; 00729 $this->DBFields[$DBFieldName."Y"] = NULL; 00730 } 00731 else 00732 { 00733 $DB->Query("UPDATE Resources SET " 00734 ."`".$DBFieldName."X` = ".(strlen($NewValue["X"]) 00735 ? "'".$NewValue["X"]."'" : "NULL").", " 00736 ."`".$DBFieldName."Y` = ".(strlen($NewValue["Y"]) 00737 ? "'".$NewValue["Y"]."'" : "NULL") 00738 ." WHERE ResourceId = ".$ResourceId); 00739 00740 $Digits = $Field->PointDecimalDigits(); 00741 00742 $this->DBFields[$DBFieldName."X"] = 00743 strlen($NewValue["X"]) ? round($NewValue["X"], $Digits) : NULL; 00744 $this->DBFields[$DBFieldName."Y"] = 00745 strlen($NewValue["Y"]) ? round($NewValue["Y"], $Digits) : NULL; 00746 } 00747 break; 00748 00749 case MetadataSchema::MDFTYPE_FLAG: 00750 # save value directly to DB 00751 if (is_null($NewValue)) 00752 { 00753 $DB->Query("UPDATE Resources SET `" 00754 .$DBFieldName."` = NULL" 00755 ." WHERE ResourceId = ".$ResourceId); 00756 } 00757 else 00758 { 00759 $DB->Query("UPDATE Resources SET `" 00760 .$DBFieldName."` = ".$NewValue 00761 ." WHERE ResourceId = ".$ResourceId); 00762 } 00763 00764 # save value locally 00765 $OldValue = $this->DBFields[$DBFieldName]; 00766 $this->DBFields[$DBFieldName] = $NewValue; 00767 00768 # recalculate counts for any associated classifications if necessary 00769 if (($DBFieldName == "ReleaseFlag") && ($NewValue != $OldValue)) 00770 { 00771 $DB->Query("SELECT ClassificationId FROM ResourceClassInts WHERE ResourceId = ".$ResourceId); 00772 while ($ClassId = $DB->FetchField("ClassificationId")) 00773 { 00774 $Class = new Classification($ClassId); 00775 $Class->RecalcResourceCount(); 00776 } 00777 } 00778 break; 00779 00780 case MetadataSchema::MDFTYPE_USER: 00781 # if value passed in was object 00782 if (is_object($NewValue)) 00783 { 00784 # retrieve user ID from object 00785 $UserId = $NewValue->Get("UserId"); 00786 } 00787 # else if value passed in was user name 00788 elseif (is_string($NewValue)) 00789 { 00790 # create user object and retrieve user ID from there 00791 $User = new User($this->DB, $NewValue); 00792 $UserId = $User->Get("UserId"); 00793 } 00794 else 00795 { 00796 # assume value is user ID and use value directly 00797 $UserId = $NewValue; 00798 } 00799 00800 # save value directly to DB 00801 $DB->Query("UPDATE Resources SET `" 00802 .$DBFieldName."` = '".$UserId."' " 00803 ."WHERE ResourceId = ".$ResourceId); 00804 00805 # save value locally 00806 $this->DBFields[$DBFieldName] = $UserId; 00807 break; 00808 00809 case MetadataSchema::MDFTYPE_DATE: 00810 # if we were given a date object 00811 if (is_object($NewValue)) 00812 { 00813 # use supplied date object 00814 $Date = $NewValue; 00815 } 00816 else 00817 { 00818 # create date object 00819 $Date = new SPTDate($NewValue); 00820 } 00821 00822 # extract values from date object and store in DB 00823 $BeginDate = "'".$Date->BeginDate()."'"; 00824 if (strlen($BeginDate) < 3) { $BeginDate = "NULL"; } 00825 $EndDate = "'".$Date->EndDate()."'"; 00826 if (strlen($EndDate) < 3) { $EndDate = "NULL"; } 00827 $DB->Query("UPDATE Resources SET " 00828 .$DBFieldName."Begin = ".$BeginDate.", " 00829 .$DBFieldName."End = ".$EndDate.", " 00830 .$DBFieldName."Precision = '".$Date->Precision()."' " 00831 ."WHERE ResourceId = ".$ResourceId); 00832 00833 # save values locally 00834 $this->DBFields[$DBFieldName."Begin"] = $Date->BeginDate(); 00835 $this->DBFields[$DBFieldName."End"] = $Date->EndDate(); 00836 $this->DBFields[$DBFieldName."Precision"] = $Date->Precision(); 00837 break; 00838 00839 case MetadataSchema::MDFTYPE_TIMESTAMP: 00840 # assume value is date and use directly 00841 $DateValue = date("Y-m-d H:i:s", strtotime($NewValue)); 00842 00843 # save value directly to DB 00844 $DB->Query("UPDATE Resources SET `" 00845 .$DBFieldName."` = '".addslashes($DateValue)."' " 00846 ."WHERE ResourceId = ".$ResourceId); 00847 00848 # save value locally 00849 $this->DBFields[$DBFieldName] = $DateValue; 00850 break; 00851 00852 case MetadataSchema::MDFTYPE_TREE: 00853 # if incoming value is array 00854 if (is_array($NewValue)) 00855 { 00856 # for each element of array 00857 foreach ($NewValue as 00858 $ClassificationId => $ClassificationName) 00859 { 00860 $Class = new Classification($ClassificationId); 00861 if ($Class->Status() == Classification::CLASSSTAT_OK) 00862 { 00863 # associate with resource if not already associated 00864 $this->AddAssociation("ResourceClassInts", 00865 "ClassificationId", 00866 $ClassificationId); 00867 $Class->RecalcResourceCount(); 00868 } 00869 } 00870 } 00871 else 00872 { 00873 # associate with resource if not already associated 00874 if (is_object($NewValue)) 00875 { 00876 $Class = $NewValue; 00877 $NewValue = $Class->Id(); 00878 } 00879 else 00880 { 00881 $Class = new Classification($NewValue); 00882 } 00883 $this->AddAssociation("ResourceClassInts", 00884 "ClassificationId", 00885 $NewValue); 00886 $Class->RecalcResourceCount(); 00887 } 00888 00889 # clear our classification cache 00890 unset($this->ClassificationCache); 00891 break; 00892 00893 case MetadataSchema::MDFTYPE_CONTROLLEDNAME: 00894 case MetadataSchema::MDFTYPE_OPTION: 00895 # Clear other values if this field expects unique options 00896 if ($Field->AllowMultiple() === FALSE) 00897 { 00898 $this->RemoveAllAssociations("ResourceNameInts", 00899 "ControlledNameId", 00900 $Field ); 00901 } 00902 00903 # if incoming value is array 00904 if (is_array($NewValue) && ($Field->AllowMultiple() !== FALSE) ) 00905 { 00906 # for each element of array 00907 foreach ($NewValue as $ControlledNameId => $ControlledName) 00908 { 00909 # associate with resource if not already associated 00910 $this->AddAssociation("ResourceNameInts", 00911 "ControlledNameId", 00912 $ControlledNameId); 00913 } 00914 } 00915 else 00916 { 00917 # If we're fed an array for a unique option, 00918 # just use the last element of the array 00919 if (is_array($NewValue)) 00920 { 00921 $NewValue = array_pop($NewValue); 00922 } 00923 00924 # associate with resource if not already associated 00925 if (is_object($NewValue)) { $NewValue = $NewValue->Id(); } 00926 $this->AddAssociation("ResourceNameInts", 00927 "ControlledNameId", 00928 $NewValue); 00929 } 00930 00931 # clear our controlled name cache 00932 unset($this->ControlledNameCache); 00933 unset($this->ControlledNameVariantCache); 00934 break; 00935 00936 case MetadataSchema::MDFTYPE_IMAGE: 00937 # if we were given an image object 00938 if (is_object($NewValue)) 00939 { 00940 # grab ID from object 00941 $ImageId = $NewValue->Id(); 00942 } 00943 else 00944 { 00945 # assume incoming value is ID 00946 $ImageId = $NewValue; 00947 } 00948 00949 # store new image object ID in database 00950 $DB->Query("UPDATE Resources SET `" 00951 .$DBFieldName."` = '".$ImageId."'" 00952 ." WHERE ResourceId = ".$ResourceId); 00953 00954 # save value locally 00955 $this->DBFields[$DBFieldName] = $ImageId; 00956 break; 00957 00958 case MetadataSchema::MDFTYPE_FILE: 00959 # convert incoming value to array if necessary 00960 if (!is_array($NewValue)) { $NewValue = array($NewValue); } 00961 00962 # for each incoming file 00963 $Factory = new FileFactory($Field->Id()); 00964 foreach ($NewValue as $File) 00965 { 00966 # make copy of file 00967 $NewFile = $Factory->Copy($File); 00968 00969 # associate copy with this resource and field 00970 $NewFile->ResourceId($this->Id); 00971 $NewFile->FieldId($Field->Id()); 00972 } 00973 break; 00974 00975 default: 00976 # ERROR OUT 00977 exit("<br>SPT - ERROR: attempt to set unknown resource field type<br>\n"); 00978 break; 00979 } 00980 } 00981 # (for backward compatibility) 00982 function SetByField($Field, $NewValue) { return $this->Set($Field, $NewValue); } 00983 00984 # set value by field ID 00985 function SetByFieldId($FieldId, $NewValue) 00986 { 00987 $Field = $this->Schema->GetField($FieldId); 00988 return $this->Set($Field, $NewValue); 00989 } 00990 00991 # set qualifier by field name 00992 function SetQualifier($FieldName, $NewValue) 00993 { 00994 $Field = $this->Schema->GetFieldByName($FieldName); 00995 return $this->SetQualifierByField($Field, $NewValue); 00996 } 00997 00998 # set qualifier by field ID 00999 function SetQualifierByFieldId($FieldId, $NewValue) 01000 { 01001 $Field = $this->Schema->GetField($FieldId); 01002 return $this->SetQualifierByField($Field, $NewValue); 01003 } 01004 01005 # set qualifier using field object 01006 function SetQualifierByField($Field, $NewValue) 01007 { 01008 # if field uses qualifiers and uses item-level qualifiers 01009 if ($Field->UsesQualifiers() && $Field->HasItemLevelQualifiers()) 01010 { 01011 # if qualifier object passed in 01012 if (is_object($NewValue)) 01013 { 01014 # grab qualifier ID from object 01015 $QualifierId = $NewValue->Id(); 01016 } 01017 else 01018 { 01019 # assume value passed in is qualifier ID 01020 $QualifierId = $NewValue; 01021 } 01022 01023 # update qualifier value in database 01024 $DBFieldName = $Field->DBFieldName(); 01025 $this->DB->Query("UPDATE Resources SET " 01026 .$DBFieldName."Qualifier = '".$QualifierId."' " 01027 ."WHERE ResourceId = ".$this->Id); 01028 01029 # update local qualifier value 01030 $this->DBFields[$DBFieldName."Qualifier"] = $QualifierId; 01031 } 01032 } 01033 01034 # clear value by field name 01035 function Clear($FieldName, $ValueToClear = NULL) 01036 { 01037 $Field = $this->Schema->GetFieldByName($FieldName); 01038 return $this->ClearByField($Field, $ValueToClear); 01039 } 01040 01041 # clear value by field ID 01042 function ClearByFieldId($FieldId, $ValueToClear = NULL) 01043 { 01044 $Field = $this->Schema->GetField($FieldId); 01045 return $this->ClearByField($Field, $ValueToClear); 01046 } 01047 01048 # clear value using field object 01049 function ClearByField($Field, $ValueToClear = NULL) 01050 { 01051 # grab commonly-used values for local use 01052 $DB = $this->DB; 01053 $ResourceId = $this->Id; 01054 01055 # grab database field name 01056 $DBFieldName = $Field->DBFieldName(); 01057 01058 # store value in DB based on field type 01059 switch ($Field->Type()) 01060 { 01061 case MetadataSchema::MDFTYPE_TEXT: 01062 case MetadataSchema::MDFTYPE_PARAGRAPH: 01063 case MetadataSchema::MDFTYPE_NUMBER: 01064 case MetadataSchema::MDFTYPE_FLAG: 01065 case MetadataSchema::MDFTYPE_USER: 01066 case MetadataSchema::MDFTYPE_TIMESTAMP: 01067 case MetadataSchema::MDFTYPE_URL: 01068 # clear value in DB 01069 $DB->Query("UPDATE Resources SET `" 01070 .$DBFieldName."` = '' " 01071 ."WHERE ResourceId = ".$ResourceId); 01072 01073 # clear value locally 01074 $this->DBFields[$DBFieldName] = NULL; 01075 break; 01076 01077 case MetadataSchema::MDFTYPE_POINT: 01078 # Clear DB Values 01079 $DB->Query("UPDATE Resources SET " 01080 ."`".$DBFieldName."X` = NULL ," 01081 ."`".$DBFieldName."Y` = NULL " 01082 ."WHERE ResourceId = ".$ResourceId); 01083 01084 # Clear local values 01085 $this->DBFields[$DBFieldName."X"] = NULL; 01086 $this->DBFields[$DBFieldName."Y"] = NULL; 01087 break; 01088 01089 case MetadataSchema::MDFTYPE_DATE: 01090 # clear date object values in DB 01091 $DB->Query("UPDATE Resources SET " 01092 .$DBFieldName."Begin = '', " 01093 .$DBFieldName."End = '', " 01094 .$DBFieldName."Precision = '' " 01095 ."WHERE ResourceId = ".$ResourceId); 01096 01097 # clear value locally 01098 $this->DBFields[$DBFieldName."Begin"] = NULL; 01099 $this->DBFields[$DBFieldName."End"] = NULL; 01100 $this->DBFields[$DBFieldName."Precision"] = NULL; 01101 break; 01102 01103 case MetadataSchema::MDFTYPE_TREE: 01104 # if value to clear supplied 01105 if ($ValueToClear !== NULL) 01106 { 01107 # if supplied value is array 01108 if (is_array($ValueToClear)) 01109 { 01110 # for each element of array 01111 foreach ($ValueToClear as $ClassificationId => $Dummy) 01112 { 01113 # remove association with resource (if any) 01114 $this->RemoveAssociation("ResourceClassInts", 01115 "ClassificationId", 01116 $ClassificationId); 01117 $Class = new Classification($ClassificationId); 01118 $Class->RecalcResourceCount(); 01119 } 01120 } 01121 else 01122 { 01123 # remove association with resource (if any) 01124 $this->RemoveAssociation("ResourceClassInts", 01125 "ClassificationId", 01126 $ValueToClear); 01127 $Class = new Classification($ValueToClear); 01128 $Class->RecalcResourceCount(); 01129 } 01130 } 01131 else 01132 { 01133 # remove all associations for resource and field 01134 $this->RemoveAllAssociations("ResourceClassInts", "ClassificationId", $Field); 01135 01136 # recompute resource count 01137 $Values = $this->Get($Field); 01138 foreach ($Values as $ClassificationId => $Dummy) 01139 { 01140 $Class = new Classification($ClassificationId); 01141 $Class->RecalcResourceCount(); 01142 } 01143 } 01144 01145 # clear our classification cache 01146 unset($this->ClassificationCache); 01147 break; 01148 01149 case MetadataSchema::MDFTYPE_CONTROLLEDNAME: 01150 case MetadataSchema::MDFTYPE_OPTION: 01151 # if value to clear supplied 01152 if ($ValueToClear !== NULL) 01153 { 01154 # if incoming value is array 01155 if (is_array($ValueToClear)) 01156 { 01157 # for each element of array 01158 foreach ($ValueToClear as $ControlledNameId => 01159 $ControlledName) 01160 { 01161 # remove association with resource (if any) 01162 $this->RemoveAssociation("ResourceNameInts", 01163 "ControlledNameId", 01164 $ControlledNameId); 01165 } 01166 } 01167 else 01168 { 01169 # remove association with resource (if any) 01170 $this->RemoveAssociation("ResourceNameInts", 01171 "ControlledNameId", 01172 $ValueToClear); 01173 } 01174 } 01175 else 01176 { 01177 # remove all associations for resource and field 01178 $this->RemoveAllAssociations("ResourceNameInts", "ControlledNameId", $Field); 01179 } 01180 01181 # clear our controlled name cache 01182 unset($this->ControlledNameCache); 01183 unset($this->ControlledNameVariantCache); 01184 break; 01185 01186 case MetadataSchema::MDFTYPE_IMAGE: 01187 # delete image if no other resources are using it 01188 $ImageId = $DB->Query("SELECT `".$DBFieldName 01189 ."` FROM Resources WHERE ResourceId = ".$ResourceId, 01190 $DBFieldName); 01191 if ($ImageId > 0) 01192 { 01193 $ImageCount = $DB->Query("SELECT COUNT(*) AS ImageCount FROM Resources" 01194 ." WHERE `".$DBFieldName."` = ".$ImageId, 01195 "ImageCount"); 01196 if ($ImageCount < 2) 01197 { 01198 $Image = new SPTImage($ImageId); 01199 $Image->Delete(); 01200 } 01201 } 01202 01203 # clear stored ID 01204 $DB->Query("UPDATE Resources SET `" 01205 .$DBFieldName."` = '' " 01206 ."WHERE ResourceId = ".$ResourceId); 01207 01208 # clear value locally 01209 $this->DBFields[$DBFieldName] = NULL; 01210 break; 01211 01212 case MetadataSchema::MDFTYPE_FILE: 01213 # get array of Files associated with this resource 01214 $Files->Get($Field); 01215 01216 # for each File 01217 foreach ($Files as $File) 01218 { 01219 # delete file 01220 $File->Delete(); 01221 } 01222 break; 01223 01224 default: 01225 # ERROR OUT 01226 exit("<br>SPT - ERROR: attempt to clear unknown resource field type<br>\n"); 01227 break; 01228 } 01229 } 01230 01231 01232 # --- Field-Specific or Type-Specific Attribute Retrieval Methods ------- 01233 01234 # return 2D array of classifications associated with resource 01235 # (first index is classification (field) name, second index is classification ID) 01236 function Classifications() 01237 { 01238 $DB = $this->DB; 01239 01240 # start with empty array 01241 $Names = array(); 01242 01243 # for each controlled name 01244 $DB->Query("SELECT ClassificationName, MetadataFields.FieldName, " 01245 ."ResourceClassInts.ClassificationId FROM ResourceClassInts, " 01246 ."Classifications, MetadataFields " 01247 ."WHERE ResourceClassInts.ResourceId = ".$this->Id." " 01248 ."AND ResourceClassInts.ClassificationId = Classifications.ClassificationId " 01249 ."AND Classifications.FieldId = MetadataFields.FieldId "); 01250 while ($Record = $DB->FetchRow()) 01251 { 01252 # add name to array 01253 $Names[$Record["FieldName"]][$Record["ClassificationId"]] = 01254 $Record["ClassificationName"]; 01255 } 01256 01257 # return array to caller 01258 return $Names; 01259 } 01260 01261 01262 # --- Ratings Methods --------------------------------------------------- 01263 01264 # return cumulative rating (range is usually 0-100) 01265 function CumulativeRating() { return $this->CumulativeRating; } 01266 01267 # return cumulative rating scaled to 1/10th (range is usually 0-10) 01268 function ScaledCumulativeRating() 01269 { 01270 if ($this->CumulativeRating == NULL) 01271 { 01272 return NULL; 01273 } 01274 else 01275 { 01276 return intval(($this->CumulativeRating + 5) / 10); 01277 } 01278 } 01279 01280 # return current number of ratings for resource 01281 function NumberOfRatings() 01282 { 01283 # if number of ratings not already set 01284 if (!isset($this->NumberOfRatings)) 01285 { 01286 # obtain number of ratings 01287 $this->NumberOfRatings = 01288 $this->DB->Query("SELECT Count(*) AS NumberOfRatings " 01289 ."FROM ResourceRatings " 01290 ."WHERE ResourceId = ".$this->Id, 01291 "NumberOfRatings" 01292 ); 01293 01294 # recalculate cumulative rating if it looks erroneous 01295 if (($this->NumberOfRatings > 0) && !$this->CumulativeRating()) 01296 { 01297 $this->UpdateCumulativeRating(); 01298 } 01299 } 01300 01301 # return number of ratings to caller 01302 return $this->NumberOfRatings; 01303 } 01304 01305 # update individual rating for resource 01306 function Rating($NewRating = NULL, $UserId = NULL) 01307 { 01308 $DB = $this->DB; 01309 01310 # if user ID not supplied 01311 if ($UserId == NULL) 01312 { 01313 # if user is logged in 01314 global $User; 01315 if ($User->IsLoggedIn()) 01316 { 01317 # use ID of current user 01318 $UserId = $User->Get("UserId"); 01319 } 01320 else 01321 { 01322 # return NULL to caller 01323 return NULL; 01324 } 01325 } 01326 01327 # if there is a rating for resource and user 01328 $DB->Query("SELECT Rating FROM ResourceRatings " 01329 ."WHERE UserId = ${UserId} AND ResourceId = ".$this->Id); 01330 if ($Record = $DB->FetchRow()) 01331 { 01332 # if new rating was supplied 01333 if ($NewRating != NULL) 01334 { 01335 # update existing rating 01336 $DB->Query("UPDATE ResourceRatings " 01337 ."SET Rating = ${NewRating}, DateRated = NOW() " 01338 ."WHERE UserId = ${UserId} AND ResourceId = ".$this->Id); 01339 01340 # update cumulative rating value 01341 $this->UpdateCumulativeRating(); 01342 01343 # return value is new rating 01344 $Rating = $NewRating; 01345 } 01346 else 01347 { 01348 # get rating value to return to caller 01349 $Rating = $Record["Rating"]; 01350 } 01351 } 01352 else 01353 { 01354 # if new rating was supplied 01355 if ($NewRating != NULL) 01356 { 01357 # add new rating 01358 $DB->Query("INSERT INTO ResourceRatings " 01359 ."(ResourceId, UserId, DateRated, Rating) " 01360 ."VALUES (" 01361 .$this->Id.", " 01362 ."${UserId}, " 01363 ."NOW(), " 01364 ."${NewRating})"); 01365 01366 # update cumulative rating value 01367 $this->UpdateCumulativeRating(); 01368 01369 # return value is new rating 01370 $Rating = $NewRating; 01371 } 01372 else 01373 { 01374 # return value is NULL 01375 $Rating = NULL; 01376 } 01377 } 01378 01379 # return rating value to caller 01380 return $Rating; 01381 } 01382 01383 01384 # --- Resource Comment Methods ------------------------------------------ 01385 01386 # return comments as array of Message objects 01387 function Comments() 01388 { 01389 # read in comments if not already loaded 01390 if (!isset($this->Comments)) 01391 { 01392 $this->DB->Query("SELECT MessageId FROM Messages " 01393 ."WHERE ParentId = ".$this->Id 01394 ." AND ParentType = 2 " 01395 ."ORDER BY DatePosted DESC"); 01396 while ($MessageId = $this->DB->FetchField("MessageId")) 01397 { 01398 $this->Comments[] = new Message($MessageId); 01399 } 01400 } 01401 01402 # return array of comments to caller 01403 return $this->Comments; 01404 } 01405 01406 # return current number of comments 01407 function NumberOfComments() 01408 { 01409 # obtain number of comments if not already set 01410 if (!isset($this->NumberOfComments)) 01411 { 01412 $this->NumberOfComments = 01413 $this->DB->Query("SELECT Count(*) AS NumberOfComments " 01414 ."FROM Messages " 01415 ."WHERE ParentId = ".$this->Id 01416 ." AND ParentType = 2", 01417 "NumberOfComments" 01418 ); 01419 } 01420 01421 # return number of comments to caller 01422 return $this->NumberOfComments; 01423 } 01424 01425 01426 # --- Permission Methods ------------------------------------------------- 01427 01428 # return whether user can edit this resource 01429 function UserCanEdit($User) 01430 { 01431 return ($User->HasPriv(PRIV_RESOURCEADMIN) 01432 || $User->HasPriv(PRIV_RELEASEADMIN) 01433 || ($User->HasPriv(PRIV_MYRESOURCEADMIN) 01434 && ($User->Id() == $this->DBFields["AddedById"])) 01435 ); 01436 } 01437 01438 # report whether user can view or edit specified field 01439 function UserCanViewField($User, $FieldOrFieldName) 01440 { 01441 # get field (if not supplied) 01442 if (is_object($FieldOrFieldName)) 01443 { 01444 $Field = $FieldOrFieldName; 01445 } 01446 else 01447 { 01448 $Schema = new MetadataSchema(); 01449 $Field = $Schema->GetFieldByName($FieldOrFieldName); 01450 } 01451 01452 # retrieve enabled and viewable state from field 01453 $IsViewable = $Field->Enabled() 01454 && (($Field->ViewingPrivilege() == 0) 01455 || $User->HasPriv($Field->ViewingPrivilege()) 01456 || $this->UserCanEditField($User, $Field)); 01457 01458 # return result to caller 01459 return $IsViewable; 01460 } 01461 function UserCanEditField($User, $FieldOrFieldName) 01462 { 01463 # get field (if not supplied) 01464 if (is_object($FieldOrFieldName)) 01465 { 01466 $Field = $FieldOrFieldName; 01467 } 01468 else 01469 { 01470 $Schema = new MetadataSchema(); 01471 $Field = $Schema->GetFieldByName($FieldOrFieldName); 01472 } 01473 01474 # start out assuming field cannot be edited 01475 $IsEditable = FALSE; 01476 01477 # if user has editing privileges for field 01478 # or user added resource and has authoring privileges for field 01479 if ($User->HasPriv($Field->EditingPrivilege()) 01480 || (($User->Name() == $this->Get("Added By Id")) 01481 && (($Field->AuthoringPrivilege() == 0) 01482 || $User->HasPriv($Field->AuthoringPrivilege())))) 01483 { 01484 # if field name does not appear on "no edit" list 01485 $UneditableFields = array( 01486 "Cumulative Rating", 01487 "Date Of Record Creation", 01488 "Date Of Record Release", 01489 "Date Last Modified", 01490 "Added By Id", 01491 "Last Modified By Id", 01492 ); 01493 if (!in_array($Field->Name(), $UneditableFields)) 01494 { 01495 # user can edit field 01496 $IsEditable = TRUE; 01497 } 01498 } 01499 01500 # return result to caller 01501 return $IsEditable; 01502 } 01503 01504 01505 # ---- PRIVATE INTERFACE ------------------------------------------------- 01506 01507 private $DB; 01508 private $Schema; 01509 private $DBFields; 01510 private $Id; 01511 private $NumberOfRatings; 01512 private $CumulativeRating; 01513 private $NumberOfComments; 01514 private $Comments; 01515 private $LastStatus; 01516 private $ControlledNameCache; 01517 private $ControlledNameVariantCache; 01518 private $ClassificationCache; 01519 01520 # recalculate and save cumulative rating value for resource 01521 private function UpdateCumulativeRating() 01522 { 01523 # grab totals from DB 01524 $this->DB->Query("SELECT COUNT(Rating) AS Count, " 01525 ."SUM(Rating) AS Total FROM ResourceRatings " 01526 ."WHERE ResourceId = ".$this->Id); 01527 $Record = $this->DB->FetchRow(); 01528 01529 # calculate new cumulative rating 01530 $this->CumulativeRating = round($Record["Total"] / $Record["Count"]); 01531 01532 # save new cumulative rating in DB 01533 $this->DB->Query("UPDATE Resources " 01534 ."SET CumulativeRating = ".$this->CumulativeRating." " 01535 ."WHERE ResourceId = ".$this->Id); 01536 } 01537 01538 # add intersection if not already present 01539 private function AddAssociation($TableName, $TargetFieldName, $TargetValue) 01540 { 01541 # if target not already associated with resource 01542 if ($this->DB->Query("SELECT COUNT(*) AS RecordCount FROM ".$TableName 01543 ." WHERE ResourceId = ".$this->Id 01544 ." AND ".$TargetFieldName." = '".$TargetValue."'", 01545 "RecordCount") == 0) 01546 { 01547 # associate target with resource 01548 $this->DB->Query("INSERT INTO ".$TableName." SET" 01549 ." ResourceId = ".$this->Id 01550 .", ".$TargetFieldName." = '".$TargetValue."'"); 01551 } 01552 } 01553 01554 # remove intersections (if any) 01555 private function RemoveAssociation($TableName, $TargetFieldName, $TargetValue) 01556 { 01557 # remove any intersections with target ID from DB 01558 $this->DB->Query("DELETE FROM ".$TableName 01559 ." WHERE ResourceId = ".$this->Id 01560 ." AND ".$TargetFieldName." = '".$TargetValue."'"); 01561 } 01562 01563 # remove all intersections for resource and field (if any) 01564 private function RemoveAllAssociations($TableName, $TargetFieldName, $Field) 01565 { 01566 # retrieve list of entries for this field and resource 01567 $Entries = $this->Get($Field); 01568 01569 # for each entry 01570 foreach ($Entries as $EntryId => $EntryName) 01571 { 01572 # remove intersection 01573 $this->RemoveAssociation($TableName, $TargetFieldName, $EntryId); 01574 } 01575 } 01576 } 01577 01578 01579 ?>