server/src/app/Models/Document.php
changeset 28 b0b56e0f8c7f
parent 20 a9b98b16b053
child 111 af85c436048f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
    26     private $id = null;
    26     private $id = null;
    27 
    27 
    28     // memoization
    28     // memoization
    29     private $providedCHO = null;
    29     private $providedCHO = null;
    30     private $title = false;
    30     private $title = false;
       
    31     private $lang = null;
    31     private $publishers = null;
    32     private $publishers = null;
    32     private $mediaArray = null;
    33     private $mediaArray = null;
    33     private $issued = null;
    34     private $issued = null;
    34     private $modified = null;
    35     private $modified = null;
       
    36     private $contributors = null;
    35 
    37 
    36     public function getProvidedCHO() {
    38     public function getProvidedCHO() {
    37         if(is_null($this->providedCHO)) {
    39         if(is_null($this->providedCHO)) {
    38             $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>");
    40             $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>");
    39         }
    41         }
    41     }
    43     }
    42 
    44 
    43     private function clearMemoizationCache() {
    45     private function clearMemoizationCache() {
    44         $this->providedCHO = null;
    46         $this->providedCHO = null;
    45         $this->title = false;
    47         $this->title = false;
       
    48         $this->lang = null;
    46         $this->publishers = null;
    49         $this->publishers = null;
    47         $this->mediaArray = null;
    50         $this->mediaArray = null;
    48         $this->issued = null;
    51         $this->issued = null;
    49         $this->modified = null;
    52         $this->modified = null;
       
    53         $this->contributors = null;
    50     }
    54     }
    51 
    55 
    52     public function getId() {
    56     public function getId() {
    53         if(is_null($this->id)) {
    57         if(is_null($this->id)) {
    54             $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
    58             $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
    55         }
    59         }
    56         return $this->id;
    60         return $this->id;
    57     }
    61     }
    58 
    62 
       
    63     public function getLanguage() {
       
    64         if(is_null($this->lang)) {
       
    65             try {
       
    66                 $langs = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/language>');
       
    67                 if(count($langs) > 0) {
       
    68                     $this->lang = $langs[0];
       
    69                 }
       
    70             } catch(\Exception $e) {
       
    71                 $this->lang = null;
       
    72             }
       
    73         }
       
    74         return $this->lang;
       
    75     }
       
    76 
       
    77     public function getLanguageValue() {
       
    78         $lang = $this->getLanguage();
       
    79         if($lang instanceof Resource) {
       
    80             return $lang->getUri();
       
    81         } else if($lan instanceof Literal) {
       
    82             return $lang->getValue();
       
    83         }
       
    84         return null;
       
    85     }
       
    86 
    59     public function getTitle() {
    87     public function getTitle() {
    60         if($this->title === false) {
    88         if($this->title === false) {
    61             try {
    89             try {
    62                 $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>');
    90                 $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>');
    63             } catch(\Exception $e) {
    91             } catch(\Exception $e) {
    64                 $this->title = null;
    92                 $this->title = null;
    65             }
    93             }
    66         }
    94         }
    67         return $this->title;
    95         return $this->title;
       
    96     }
       
    97 
       
    98     public function setTitle($value, $lang="fr") {
       
    99         $oldTitle = $this->getTitle();
       
   100         if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) {
       
   101             $literalTitle = new Literal($value, $lang, null);
       
   102             $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle);
       
   103             //clear cache
       
   104             $this->title = false;
       
   105         }
    68     }
   106     }
    69 
   107 
    70     public function getTitleValue() {
   108     public function getTitleValue() {
    71         $title = $this->getTitle();
   109         $title = $this->getTitle();
    72         return is_null($title)?null:$title->getValue();
   110         return is_null($title)?null:$title->getValue();
   114     }
   152     }
   115 
   153 
   116     public function getModifiedValue() {
   154     public function getModifiedValue() {
   117         $modified = $this->getModified();
   155         $modified = $this->getModified();
   118         return is_null($modified)?null:$modified->getValue();
   156         return is_null($modified)?null:$modified->getValue();
       
   157     }
       
   158 
       
   159     public function setModified($value) {
       
   160         $modified = $this->getModified();
       
   161         if($value && (!$modified || $modified->getValue() != $value) ) {
       
   162 
       
   163             $newModified = new Literal($value, "http://purl.org/dc/terms/W3CDTF");
       
   164             $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified);
       
   165 
       
   166             $this->modified = null;
       
   167         }
   119     }
   168     }
   120 
   169 
   121     public function getMediaArray() {
   170     public function getMediaArray() {
   122 
   171 
   123         if(is_null($this->mediaArray)) {
   172         if(is_null($this->mediaArray)) {
   161         }));
   210         }));
   162         return $res;
   211         return $res;
   163     }
   212     }
   164 
   213 
   165     public function getContributors() {
   214     public function getContributors() {
   166         return array_reduce(
   215         if(is_null($this->contributors)) {
   167             CocoonUtils::OLAC_ROLES,
   216             $this->contributors = array_reduce(
   168             function($res, $olacRole) {
   217                 CocoonUtils::OLAC_ROLES,
   169                 return array_merge(
   218                 function($res, $olacRole) {
   170                     $res,
   219                     return array_merge(
   171                     array_map(
   220                         $res,
   172                         function($olacValue) use ($olacRole) {
   221                         array_map(
   173                             return [
   222                             function($olacValue) use ($olacRole) {
   174                                 'name' => ($olacValue instanceof Literal)?$olacValue->getValue():null,
   223                                 return [
   175                                 'url' => ($olacValue instanceof Resource)?$olacValue->getUri():null,
   224                                     'name' => ($olacValue instanceof Literal)?$olacValue->getValue():null,
   176                                 'role' => $olacRole
   225                                     'nameLiteral' => ($olacValue instanceof Literal)?$olacValue:null,
   177                             ];
   226                                     'url' => ($olacValue instanceof Resource)?$olacValue->getUri():null,
   178                         },
   227                                     'role' => $olacRole
   179                         $this->getProvidedCHO()->all("<$olacRole>")
   228                                 ];
   180                     )
   229                             },
   181                 );
   230                             $this->getProvidedCHO()->all("<$olacRole>")
   182             },
   231                         )
   183             []
   232                     );
   184         );
   233                 },
       
   234                 []
       
   235             );
       
   236         }
       
   237         return $this->contributors;
       
   238     }
       
   239 
       
   240     /**
       
   241      * change contributors list
       
   242      */
       
   243     public function setContributors($contributors) {
       
   244         $delta = $this->startDelta();
       
   245         //remove old,
       
   246         foreach ($this->getContributors() as $contribDef) {
       
   247             $value = null;
       
   248             if (is_null($contribDef['url'])) {
       
   249                 if(is_null($contribDef['nameLiteral'])) {
       
   250                     $value = new Literal($contribDef['name']);
       
   251                 } else {
       
   252                     $value = $contribDef['nameLiteral'];
       
   253                 }
       
   254             } else {
       
   255                 $value = new Resource($contribDef['url']);
       
   256             }
       
   257             $this->getProvidedCHO()->delete($contribDef['role'], $value);
       
   258             $delta->getDeletedGraph()->add($this->getProvidedCHO(), $contribDef['role'], $value);
       
   259         }
       
   260 
       
   261         //put new
       
   262         foreach ($contributors as $newContribDef) {
       
   263             $value = null;
       
   264             if (is_null($newContribDef['url'])) {
       
   265                 $value = new Literal($newContribDef['name'], "fr", null);
       
   266             } else {
       
   267                 $value = new Resource($newContribDef['url']);
       
   268             }
       
   269             $this->getProvidedCHO()->add($newContribDef['role'], $value);
       
   270             $delta->getAddedGraph()->add($this->getProvidedCHO(), $newContribDef['role'], $value);
       
   271         }
       
   272 
       
   273         $this->contributors = null;
       
   274 
   185     }
   275     }
   186 
   276 
   187     /**
   277     /**
   188      * change discourse type list
   278      * change discourse type list
   189      */
   279      */
   238             $publishers = array_map(
   328             $publishers = array_map(
   239                 function($v) { return Utils::processLiteralOrString($v); },
   329                 function($v) { return Utils::processLiteralOrString($v); },
   240                 $this->getPublishers()
   330                 $this->getPublishers()
   241             );
   331             );
   242 
   332 
       
   333             $contributors = array_map(
       
   334                 function($c) { unset($c['nameLiteral']); return $c; },
       
   335                 $this->getContributors()
       
   336             );
       
   337 
   243             return [
   338             return [
   244                 'id' => $this->getId(),
   339                 'id' => $this->getId(),
   245                 'uri' => $this->getUri(),
   340                 'uri' => $this->getUri(),
   246                 'title' => $this->getTitleValue(),
   341                 'title' => $this->getTitleValue(),
       
   342                 'language' => $this->getLanguageValue(),
   247                 'modified' => $this->getModifiedValue(),
   343                 'modified' => $this->getModifiedValue(),
   248                 'publishers' => $publishers,
   344                 'publishers' => $publishers,
   249                 'contributors' => $this->getContributors(),
   345                 'contributors' => $contributors,
   250                 'mediaArray'=> $mediaArray
   346                 'mediaArray'=> $mediaArray
   251             ];
   347             ];
   252         }
   348         }
   253     }
   349     }
   254 
   350