//chdir("/var/www/html/pm3");
//include "include/include.php";
/**
* Change Log:
* Jul 14, 2023 Harman Singh Migrated from PHP 5.3 to PHP 8.1
*/
#Transition: PHP 4.3 to PHP 5.16 to target PHP 5.3
#Testing environment: PHP 5.1.6
#Methods changed: none
#Methods Tested: getAllPrefOptions, getOptionsByName (namesapce = "Project Console"), getPrefByUser (userid = "32").
# Note: Userid is madeup. Table not populated so expect no result for query.
#Testing Performed: test/test_UserPrefManager.php
#DAO called: UserPrefDao, UserPrefOptionsDao
#DB directly called: NO
#Functions: - getAllPrefOptions: retrieve all UserPref obj and add to a UserPrefSet obj
# - allow get, update, insert, delete userPref
#Note - can't do full testing. Script still under developement.
#Transition closed
class UserPrefManager {
//preference options
function getAllPrefOptions($limit=NULL,$offset=NULL){
$prefDao= new UserPrefOptionsDao();
$result=$prefDao->searchAllOptions($limit,$offset);
return UserPrefManager::processPrefOptionResults($result);
}
//preference options
function & getOptionsByName($namespace){
$prefDao= new UserPrefOptionsDao();
$result=$prefDao->searchOptionsByName($namespace);
return UserPrefManager::processPrefOptionResults($result);
}
//preference options
function processPrefOptionResults($result){
if ( $result == NULL || pg_num_rows($result) < 1 )
return false;
$prefSet = new UserPrefSet();
while ($row=$pg_fetch_assoc($result) ) {
$pref = new UserPref();
$pref->setId($row["id"]);
$pref->setPredicate($row["predicate"]);
$pref->setNamespace($row["namespace"]);
$pref->setDescr($row["descr"]);
$prefSet->addUserPref($pref);
}
return $prefSet;
}
//user preferences
function getPrefByUser($userid){
$prefDao= new UserPrefDao();
$result=$prefDao->searchPrefByUser($userid);
return UserPrefManager::processPrefResults($result);
}
//user preferences
function processPrefResults($result){
if ( $result == NULL || pg_num_rows($result) < 1 )
return false;
$prefSet = new UserPrefSet();
while ($row=$pg_fetch_assoc($result) ) {
$pref = new UserPref();
$pref->setId($row["id"]);
$pref->setPredicate($row["predicate"]);
$pref->setNamespace($row["namespace"]);
if ($row["pref_value"] == 't'){
$pref->setPrefValue(true);
}
$prefSet->addUserPref($pref);
}
return $prefSet;
}
function insertUserPref($userid, $namespace, $predicate, $prefValue){
$prefDao= new UserPrefDao();
return $prefDao->insertUserPref($userid, $namespace, $predicate, $prefValue);
}
function updateUserPrefValue($userid, $namespace, $predicate, $prefValue){
$prefDao= new UserPrefDao();
return $prefDao->updateUserPrefValue($userid, $namespace, $predicate, $prefValue);
}
function updateUserPrefById($id, $userid, $namespace, $predicate, $prefValue){
$prefDao= new UserPrefDao();
return $prefDao->updateUserPrefById($id, $userid, $namespace, $predicate, $prefValue);
}
function deleteUserPrefById($id){
$prefDao= new UserPrefDao();
return $prefDao->deleteUserPrefById($id);
}
function addUserPrefByUserid($userid, $namespace, $predicate, $prefValue){
$found=false;
$prefSet=UserPrefManager::getPrefByUser($userid);
if($prefSet){
$prefArray=$prefSet->toArray();
foreach($prefArray as $pref){
if(!strcmp($pref->getNamespace(),$namespace) and !strcmp($pref->getPredicate(),$predicate)){
$found=true;
return UserPrefManager::updateUserPrefValue($userid, $namespace, $predicate, $prefValue);
break;
}
}
}
if(!$found){
return UserPrefManager::insertUserPref($userid, $namespace, $predicate, $prefValue);
}
}
}
//$pref = new UserPrefManager();
//$pref->insertUserPref(8,"Project List","Hide Public Projects",true);
//$pref->insertUserPref(8,"Project List","Hide Campaigns",false);
//$pref->updateUserPrefValue(8,"Project List","Hide Campaigns",true);
//$pref->updateUserPrefById(2,8,"Project List","Hide Campaigns Projects",false);
//$pref = $pref->getPrefByUser(8);
//$p = $pref->getAllPrefOptions();
//var_dump($p);
?>