diff -r a2342f26c9de -r b0b56e0f8c7f server/src/app/Models/Document.php --- a/server/src/app/Models/Document.php Fri Jan 15 15:27:56 2016 +0100 +++ b/server/src/app/Models/Document.php Fri Jan 15 15:35:00 2016 +0100 @@ -28,10 +28,12 @@ // memoization private $providedCHO = null; private $title = false; + private $lang = null; private $publishers = null; private $mediaArray = null; private $issued = null; private $modified = null; + private $contributors = null; public function getProvidedCHO() { if(is_null($this->providedCHO)) { @@ -43,10 +45,12 @@ private function clearMemoizationCache() { $this->providedCHO = null; $this->title = false; + $this->lang = null; $this->publishers = null; $this->mediaArray = null; $this->issued = null; $this->modified = null; + $this->contributors = null; } public function getId() { @@ -56,6 +60,30 @@ return $this->id; } + public function getLanguage() { + if(is_null($this->lang)) { + try { + $langs = $this->getProvidedCHO()->all(''); + if(count($langs) > 0) { + $this->lang = $langs[0]; + } + } catch(\Exception $e) { + $this->lang = null; + } + } + return $this->lang; + } + + public function getLanguageValue() { + $lang = $this->getLanguage(); + if($lang instanceof Resource) { + return $lang->getUri(); + } else if($lan instanceof Literal) { + return $lang->getValue(); + } + return null; + } + public function getTitle() { if($this->title === false) { try { @@ -67,6 +95,16 @@ return $this->title; } + public function setTitle($value, $lang="fr") { + $oldTitle = $this->getTitle(); + if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) { + $literalTitle = new Literal($value, $lang, null); + $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle); + //clear cache + $this->title = false; + } + } + public function getTitleValue() { $title = $this->getTitle(); return is_null($title)?null:$title->getValue(); @@ -118,6 +156,17 @@ return is_null($modified)?null:$modified->getValue(); } + public function setModified($value) { + $modified = $this->getModified(); + if($value && (!$modified || $modified->getValue() != $value) ) { + + $newModified = new Literal($value, "http://purl.org/dc/terms/W3CDTF"); + $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified); + + $this->modified = null; + } + } + public function getMediaArray() { if(is_null($this->mediaArray)) { @@ -163,25 +212,66 @@ } public function getContributors() { - return array_reduce( - CocoonUtils::OLAC_ROLES, - function($res, $olacRole) { - return array_merge( - $res, - array_map( - function($olacValue) use ($olacRole) { - return [ - 'name' => ($olacValue instanceof Literal)?$olacValue->getValue():null, - 'url' => ($olacValue instanceof Resource)?$olacValue->getUri():null, - 'role' => $olacRole - ]; - }, - $this->getProvidedCHO()->all("<$olacRole>") - ) - ); - }, - [] - ); + if(is_null($this->contributors)) { + $this->contributors = array_reduce( + CocoonUtils::OLAC_ROLES, + function($res, $olacRole) { + return array_merge( + $res, + array_map( + function($olacValue) use ($olacRole) { + return [ + 'name' => ($olacValue instanceof Literal)?$olacValue->getValue():null, + 'nameLiteral' => ($olacValue instanceof Literal)?$olacValue:null, + 'url' => ($olacValue instanceof Resource)?$olacValue->getUri():null, + 'role' => $olacRole + ]; + }, + $this->getProvidedCHO()->all("<$olacRole>") + ) + ); + }, + [] + ); + } + return $this->contributors; + } + + /** + * change contributors list + */ + public function setContributors($contributors) { + $delta = $this->startDelta(); + //remove old, + foreach ($this->getContributors() as $contribDef) { + $value = null; + if (is_null($contribDef['url'])) { + if(is_null($contribDef['nameLiteral'])) { + $value = new Literal($contribDef['name']); + } else { + $value = $contribDef['nameLiteral']; + } + } else { + $value = new Resource($contribDef['url']); + } + $this->getProvidedCHO()->delete($contribDef['role'], $value); + $delta->getDeletedGraph()->add($this->getProvidedCHO(), $contribDef['role'], $value); + } + + //put new + foreach ($contributors as $newContribDef) { + $value = null; + if (is_null($newContribDef['url'])) { + $value = new Literal($newContribDef['name'], "fr", null); + } else { + $value = new Resource($newContribDef['url']); + } + $this->getProvidedCHO()->add($newContribDef['role'], $value); + $delta->getAddedGraph()->add($this->getProvidedCHO(), $newContribDef['role'], $value); + } + + $this->contributors = null; + } /** @@ -240,13 +330,19 @@ $this->getPublishers() ); + $contributors = array_map( + function($c) { unset($c['nameLiteral']); return $c; }, + $this->getContributors() + ); + return [ 'id' => $this->getId(), 'uri' => $this->getUri(), 'title' => $this->getTitleValue(), + 'language' => $this->getLanguageValue(), 'modified' => $this->getModifiedValue(), 'publishers' => $publishers, - 'contributors' => $this->getContributors(), + 'contributors' => $contributors, 'mediaArray'=> $mediaArray ]; }