Open Journal Systems  3.3.0
Identity.inc.php
1 <?php
2 
22 define('IDENTITY_SETTING_GIVENNAME', 'givenName');
23 define('IDENTITY_SETTING_FAMILYNAME', 'familyName');
24 
25 class Identity extends DataObject {
26 
34  function &getLocalizedData($key, $preferredLocale = null) {
35  if (is_null($preferredLocale)) $preferredLocale = AppLocale::getLocale();
36  $localePrecedence = array($preferredLocale);
37  // the users register for the site, thus
38  // the site primary locale is the default locale
39  $site = Application::get()->getRequest()->getSite();
40  if (!in_array($site->getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = $site->getPrimaryLocale();
41  // for settings other than givenName, familyName and affiliation (that are required for registration)
42  // consider also the context primary locale
43  if (!in_array(AppLocale::getPrimaryLocale(), $localePrecedence)) $localePrecedence[] = AppLocale::getPrimaryLocale();
44  foreach ($localePrecedence as $locale) {
45  if (empty($locale)) continue;
46  $value =& $this->getData($key, $locale);
47  if (!empty($value)) return $value;
48  unset($value);
49  }
50 
51  // Fallback: Get the first available piece of data.
52  $data =& $this->getData($key, null);
53  foreach ((array) $data as $dataValue) {
54  if (!empty($dataValue)) return $dataValue;
55  }
56 
57  // No data available; return null.
58  unset($data);
59  $data = null;
60  return $data;
61  }
62 
72  function getFullName($preferred = true, $familyFirst = false, $defaultLocale = null) {
73  $locale = AppLocale::getLocale();
74  if ($preferred) {
75  $preferredPublicName = $this->getPreferredPublicName($locale);
76  if (!empty($preferredPublicName)) return $preferredPublicName;
77  }
78  $givenName = $this->getGivenName($locale);
79  if (empty($givenName)) {
80  if (is_null($defaultLocale)) {
81  // the users register for the site, thus
82  // the site primary locale is the default locale
83  $site = Application::get()->getRequest()->getSite();
84  $defaultLocale = $site->getPrimaryLocale();
85  }
86  $locale = $defaultLocale;
87  $givenName = $this->getGivenName($locale);
88  }
89  $familyName = $this->getFamilyName($locale);
90  if ($familyFirst) {
91  return ($familyName != ''?"$familyName, " :'') . $givenName;
92  } else {
93  return $givenName . ($familyName != ''?" $familyName" :'');
94  }
95  }
96 
102  function getGivenName($locale) {
103  return $this->getData(IDENTITY_SETTING_GIVENNAME, $locale);
104  }
105 
111  function setGivenName($givenName, $locale) {
112  $this->setData(IDENTITY_SETTING_GIVENNAME, $givenName, $locale);
113  }
114 
119  function getLocalizedGivenName($defaultLocale = null) {
120  return $this->getLocalizedData(IDENTITY_SETTING_GIVENNAME, $defaultLocale);
121  }
122 
128  function getFamilyName($locale) {
129  return $this->getData(IDENTITY_SETTING_FAMILYNAME, $locale);
130  }
131 
137  function setFamilyName($familyName, $locale) {
138  $this->setData(IDENTITY_SETTING_FAMILYNAME, $familyName, $locale);
139  }
140 
147  function getLocalizedFamilyName($defaultLocale = null) {
148  $localePriorityList = array();
149 
150  if (!is_null($defaultLocale)) {
151  $localePriorityList[] = $defaultLocale;
152  }
153 
154  $localePriorityList[] = AppLocale::getLocale();
155 
156  foreach ($localePriorityList as $locale) {
157  $givenName = $this->getGivenName($locale);
158  if (!empty($givenName)) {
159  return $this->getFamilyName($locale);
160  }
161  }
162 
163  $site = Application::get()->getRequest()->getSite();
164  $locale = $site->getPrimaryLocale();
165  return $this->getFamilyName($locale);
166  }
167 
173  function getPreferredPublicName($locale) {
174  return $this->getData('preferredPublicName', $locale);
175  }
176 
182  function setPreferredPublicName($preferredPublicName, $locale) {
183  $this->setData('preferredPublicName', $preferredPublicName, $locale);
184  }
185 
191  function getAffiliation($locale) {
192  return $this->getData('affiliation', $locale);
193  }
194 
200  function setAffiliation($affiliation, $locale) {
201  $this->setData('affiliation', $affiliation, $locale);
202  }
203 
208  return $this->getLocalizedData('affiliation');
209  }
210 
215  function getEmail() {
216  return $this->getData('email');
217  }
218 
223  function setEmail($email) {
224  $this->setData('email', $email);
225  }
226 
231  function getOrcid() {
232  return $this->getData('orcid');
233  }
234 
239  function setOrcid($orcid) {
240  $this->setData('orcid', $orcid);
241  }
242 
247  function getCountry() {
248  return $this->getData('country');
249  }
250 
255  function getCountryLocalized() {
256  $countryCode = $this->getCountry();
257  if (!$countryCode) return null;
258  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
259  $country = $isoCodes->getCountries()->getByAlpha2($countryCode);
260  return $country?$country->getLocalName():null;
261  }
262 
267  function setCountry($country) {
268  $this->setData('country', $country);
269  }
270 
275  function getUrl() {
276  return $this->getData('url');
277  }
278 
283  function setUrl($url) {
284  $this->setData('url', $url);
285  }
286 
292  return $this->getLocalizedData('biography');
293  }
294 
300  function getBiography($locale) {
301  return $this->getData('biography', $locale);
302  }
303 
309  function setBiography($biography, $locale) {
310  $this->setData('biography', $biography, $locale);
311  }
312 
313 }
314 
315 
DataObject\getData
& getData($key, $locale=null)
Definition: DataObject.inc.php:100
Identity\getCountryLocalized
getCountryLocalized()
Definition: Identity.inc.php:255
Identity\getBiography
getBiography($locale)
Definition: Identity.inc.php:300
Identity\getPreferredPublicName
getPreferredPublicName($locale)
Definition: Identity.inc.php:173
Identity\setCountry
setCountry($country)
Definition: Identity.inc.php:267
Identity\getEmail
getEmail()
Definition: Identity.inc.php:215
DataObject
Any class with an associated DAO should extend this class.
Definition: DataObject.inc.php:18
Identity\getUrl
getUrl()
Definition: Identity.inc.php:275
Identity\getLocalizedBiography
getLocalizedBiography()
Definition: Identity.inc.php:291
Identity\getLocalizedFamilyName
getLocalizedFamilyName($defaultLocale=null)
Definition: Identity.inc.php:147
Identity\setAffiliation
setAffiliation($affiliation, $locale)
Definition: Identity.inc.php:200
Identity\setUrl
setUrl($url)
Definition: Identity.inc.php:283
Identity\setBiography
setBiography($biography, $locale)
Definition: Identity.inc.php:309
AppLocale\getPrimaryLocale
static getPrimaryLocale()
Definition: env1/MockAppLocale.inc.php:95
Identity\getLocalizedGivenName
getLocalizedGivenName($defaultLocale=null)
Definition: Identity.inc.php:119
Identity\setEmail
setEmail($email)
Definition: Identity.inc.php:223
Identity\setFamilyName
setFamilyName($familyName, $locale)
Definition: Identity.inc.php:137
Identity\setOrcid
setOrcid($orcid)
Definition: Identity.inc.php:239
Identity\getLocalizedAffiliation
getLocalizedAffiliation()
Definition: Identity.inc.php:207
Identity\setGivenName
setGivenName($givenName, $locale)
Definition: Identity.inc.php:111
Identity\setPreferredPublicName
setPreferredPublicName($preferredPublicName, $locale)
Definition: Identity.inc.php:182
Identity\getAffiliation
getAffiliation($locale)
Definition: Identity.inc.php:191
Identity\getOrcid
getOrcid()
Definition: Identity.inc.php:231
Identity
Basic class providing common functionality for users and authors in the system.
Definition: Identity.inc.php:25
Identity\getCountry
getCountry()
Definition: Identity.inc.php:247
PKPApplication\get
static get()
Definition: PKPApplication.inc.php:235
Identity\getGivenName
getGivenName($locale)
Definition: Identity.inc.php:102
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
Identity\getFullName
getFullName($preferred=true, $familyFirst=false, $defaultLocale=null)
Definition: Identity.inc.php:72
DataObject\setData
setData($key, $value, $locale=null)
Definition: DataObject.inc.php:132
Identity\getLocalizedData
& getLocalizedData($key, $preferredLocale=null)
Definition: Identity.inc.php:34
Identity\getFamilyName
getFamilyName($locale)
Definition: Identity.inc.php:128