Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
UserXmlPKPUserFilter.inc.php
1 <?php
2 
16 import('lib.pkp.plugins.importexport.native.filter.NativeImportFilter');
17 
23  function UserXmlPKPUserFilter($filterGroup) {
24  $this->setDisplayName('Native XML user import');
25  parent::NativeImportFilter($filterGroup);
26  }
27 
28  //
29  // Implement template methods from NativeImportFilter
30  //
35  function getPluralElementName() {
36  return 'PKPUsers';
37  }
38 
39  //
40  // Implement template methods from PersistableFilter
41  //
45  function getClassName() {
46  return 'lib.pkp.plugins.importexport.users.filter.UserXmlPKPUserFilter';
47  }
48 
54  function parseUserGroup($node) {
55 
56  $filterDao = DAORegistry::getDAO('FilterDAO');
57  $importFilters = $filterDao->getObjectsByGroup('user-xml=>usergroup');
58  assert(count($importFilters)==1); // Assert only a single unserialization filter
59  $importFilter = array_shift($importFilters);
60  $importFilter->setDeployment($this->getDeployment());
61  $userGroupDoc = new DOMDocument();
62  $userGroupDoc->appendChild($userGroupDoc->importNode($node, true));
63  return $importFilter->execute($userGroupDoc);
64  }
65 
71  function parseUser($node) {
72  $deployment = $this->getDeployment();
73  $context = $deployment->getContext();
74 
75  // Create the data object
76  $userDao = DAORegistry::getDAO('UserDAO');
77  $user = $userDao->newDataObject();
78 
79  // Handle metadata in subelements
80  for ($n = $node->firstChild; $n !== null; $n=$n->nextSibling) if (is_a($n, 'DOMElement')) switch($n->tagName) {
81  case 'username': $user->setUsername($n->textContent); break;
82  case 'firstname': $user->setFirstName($n->textContent); break;
83  case 'middlename': $user->setMiddleName($n->textContent); break;
84  case 'lastname': $user->setLastName($n->textContent); break;
85  case 'initials': $user->setInitials($n->textContent); break;
86  case 'salutation': $user->setSalutation($n->textContent); break;
87  case 'suffix': $user->setSuffix($n->textContent); break;
88  case 'gender': $user->setGender($n->textContent); break;
89  case 'affiliation': $user->setAffiliation($n->textContent, $n->getAttribute('locale')); break;
90  case 'country': $user->setCountry($n->textContent); break;
91  case 'email': $user->setEmail($n->textContent); break;
92  case 'url': $user->setUrl($n->textContent); break;
93  case 'phone': $user->setPhone($n->textContent); break;
94  case 'fax': $user->setFax($n->textContent); break;
95  case 'billing_address': $user->setBillingAddress($n->textContent); break;
96  case 'mailing_address': $user->setMailingAddress($n->textContent); break;
97  case 'biography': $user->setBiography($n->textContent, $n->getAttribute('locale')); break;
98  case 'gossip': $user->setGossip($n->textContent, $n->getAttribute('locale')); break;
99  case 'signature': $user->setSignature($n->textContent, $n->getAttribute('locale')); break;
100  case 'date_registered': $user->setDateRegistered($n->textContent); break;
101  case 'date_last_login': $user->setDateLastLogin($n->textContent); break;
102  case 'date_last_email': $user->setDateLastEmail($n->textContent); break;
103  case 'date_validated': $user->setDateValidated($n->textContent); break;
104  case 'inline_help':$n->textContent == 'true' ? $user->setInlineHelp(true) : $user->setInlineHelp(false) ; break;
105  case 'auth_id': $user->setAuthId($n->textContent); break;
106  case 'auth_string': $user->setAuthString($n->textContent); break;
107  case 'disabled_reason': $user->setDisabledReason($n->textContent); break;
108  case 'locales': $user->setLocales(preg_split('/:/', $n->textContent)); break;
109 
110  case 'password':
111  if ($n->getAttribute('must_change') == 'true') {
112  $user->setMustChangePassword(true);
113  }
114  if ($n->getAttribute('is_disabled') == 'true') {
115  $user->setIsDisabled(true);
116  }
117  $passwordValueNodeList = $n->getElementsByTagNameNS($deployment->getNamespace(), 'value');
118  if ($passwordValueNodeList->length == 1) {
119  $password = $passwordValueNodeList->item(0);
120  $user->setPassword($password->textContent);
121  } else {
122  fatalError("User has no password. Check your import XML format.");
123  }
124  break;
125  }
126 
127  // ensure that this username and email address are not already in use.
128  if (!$userDao->getByUsername($user->getUsername(), false) && !$userDao->getUserByEmail($user->getEmail(), false)) {
129  $userId = $userDao->insertObject($user);
130 
131  // Insert reviewing interests, now that there is a userId.
132  $interestNodeList = $node->getElementsByTagNameNS($deployment->getNamespace(), 'review_interests');
133  if ($interestNodeList->length == 0) {
134  $n = $interestNodeList->item(0);
135  if ($n) {
136  $interests = preg_split('/,\s*/', $n->textContent);
137  import('lib.pkp.classes.user.InterestManager');
138  $interestManager = new InterestManager();
139  $interestManager->setInterestsForUser($user, $interests);
140  }
141  }
142 
143  $userGroupDao = DAORegistry::getDAO('UserGroupDAO');
144  $userGroups = $userGroupDao->getByContextId($context->getId());
145 
146  // Extract user groups from the User XML and assign the user to those (existing) groups.
147  // Note: It is possible for a user to exist with no user group assignments so there is
148  // no fatalError() as is the case with PKPAuthor import.
149  $userGroupNodeList = $node->getElementsByTagNameNS($deployment->getNamespace(), 'user_group_ref');
150  if ($userGroupNodeList->length > 0) {
151  for ($i = 0 ; $i < $userGroupNodeList->length ; $i++) {
152  $n = $userGroupNodeList->item($i);
153  while ($userGroup = $userGroups->next()) {
154  if (in_array($n->textContent, $userGroup->getName(null))) {
155  // Found a candidate; assign user to it.
156  $userGroupDao->assignUserToGroup($userId, $userGroup->getId());
157  }
158  }
159  }
160  }
161 
162  return $user;
163  }
164  }
165 
170  function handleElement($node) {
171  $deployment = $this->getDeployment();
172  $context = $deployment->getContext();
173 
174  for ($n = $node->firstChild; $n !== null; $n=$n->nextSibling) {
175  if (is_a($n, 'DOMElement')) {
176  $this->handleChildElement($n);
177  }
178  }
179 
180  }
181 
186  function handleChildElement($n) {
187  switch ($n->tagName) {
188  case 'user_group':
189  $this->parseUserGroup($n);
190  break;
191  case 'user':
192  $this->parseUser($n);
193  break;
194  default:
195  fatalError('Unknown element ' . $n->tagName);
196  }
197  }
198 }
199 
200 ?>
static & getDAO($name, $dbconn=null)
setDisplayName($displayName)
Definition: Filter.inc.php:140
Base class that converts a User XML document to a set of users.
Handle user interest functions.
Base class that converts a Native XML document to a DataObject.