server/src/app/Models/Document.php
changeset 138 3079cbf80006
parent 130 fac22d8c2df8
child 141 c0e8626a271c
--- a/server/src/app/Models/Document.php	Thu Mar 03 17:34:12 2016 +0100
+++ b/server/src/app/Models/Document.php	Fri Mar 04 10:08:52 2016 +0100
@@ -177,11 +177,18 @@
         return is_null($modified)?null:$modified->getValue();
     }
 
-    public function setModified($value) {
+    public function setModified($value = null) {
+        if(is_null($value)) {
+            $value = gmdate(\DateTime::ATOM);
+        } elseif ($value instanceof \DateTime) {
+            $value = $value->format(\DateTime::ATOM);
+        }
+        $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value);
+
         $modified = $this->getModified();
-        if($value && (!$modified || $modified->getValue() != $value) ) {
+        if($value && (!$modified || $modified->getValue() !== $value) ) {
 
-            $newModified = new Literal($value, "http://purl.org/dc/terms/W3CDTF");
+            $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF");
             $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified);
 
             $this->modified = null;
@@ -328,6 +335,35 @@
         return $this->subjects;
     }
 
+    /**
+     * change subjecs list
+     */
+    public function setSubjects($subjects) {
+        $delta = $this->startDelta();
+        //remove old,
+        foreach ($this->getSubjects() as $subject) {
+            $this->getProvidedCHO()->delete('<http://purl.org/dc/elements/1.1/subject>', $subject);
+            $delta->getDeletedGraph()->add($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/subject', $subject);
+        }
+
+        //put new
+        foreach ($subjects as $newSubject) {
+            $value = null;
+            if(filter_var($newSubject, FILTER_VALIDATE_URL)) {
+                $value = new Resource($newSubject);
+            }
+            else {
+                $value = new Literal($newSubject, "fr", null);
+            }
+
+            $this->getProvidedCHO()->add('http://purl.org/dc/elements/1.1/subject', $value);
+            $delta->getAddedGraph()->add($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/subject', $value);
+        }
+
+        $this->subjects = null;
+
+    }
+
     public function isIsomorphic($doc) {
         return Isomorphic::isomorphic($this->graph, $doc->graph);
     }