public function onBeforeWrite()
 {
     $changedFields = $this->owner->getChangedFields(false, 1);
     if (array_key_exists("DocumentTags", $changedFields)) {
         $currentTags = explode(',', $this->owner->getField('DocumentTags'));
         $oldTags = DMSTag::get()->innerJoin("DMSDocument_Tags", "\"DMSDocument_Tags\".\"DMSTagID\" = \"DMSTag\".\"ID\" AND \"DMSDocument_Tags\".\"DMSDocumentID\" = " . $this->owner->ID)->column();
         // delete the tags
         foreach (array_diff($oldTags, $currentTags) as $idTag) {
             $tag = DMSTag::get()->byID($idTag);
             if ($tag) {
                 $this->owner->removeTag($tag->Category, $tag->Value ? $tag->Value : null);
             }
         }
         // add the tags
         foreach (array_diff($currentTags, $oldTags) as $idTag) {
             $tag = DMSTag::get()->byID($idTag);
             if ($tag) {
                 $tag->Documents()->add($this->owner);
             }
         }
     }
 }
Example #2
0
 /**
  * Adds a metadata tag to the Document. The tag has a category and a value.
  * Each category can have multiple values by default. So: addTag("fruit","banana") addTag("fruit", "apple") will add two items.
  * However, if the third parameter $multiValue is set to 'false', then all updates to a category only ever update a single value. So:
  * addTag("fruit","banana") addTag("fruit", "apple") would result in a single metadata tag: fruit->apple.
  * Can could be implemented as a key/value store table (although it is more like category/value, because the
  * same category can occur multiple times)
  * @param $category String of a metadata category to add (required)
  * @param $value String of a metadata value to add (required)
  * @param bool $multiValue Boolean that determines if the category is multi-value or single-value (optional)
  * @return null
  */
 function addTag($category, $value, $multiValue = true)
 {
     if ($multiValue) {
         //check for a duplicate tag, don't add the duplicate
         $currentTag = $this->Tags()->filter(array('Category' => $category, 'Value' => $value));
         if ($currentTag->Count() == 0) {
             //multi value tag
             $tag = new DMSTag();
             $tag->Category = $category;
             $tag->Value = $value;
             $tag->MultiValue = true;
             $tag->write();
             $tag->Documents()->add($this);
         } else {
             //add the relation between the tag and document
             foreach ($currentTag as $tagObj) {
                 $tagObj->Documents()->add($this);
             }
         }
     } else {
         //single value tag
         $currentTag = $this->Tags()->filter(array('Category' => $category));
         $tag = null;
         if ($currentTag->Count() == 0) {
             //create the single-value tag
             $tag = new DMSTag();
             $tag->Category = $category;
             $tag->Value = $value;
             $tag->MultiValue = false;
             $tag->write();
         } else {
             //update the single value tag
             $tag = $currentTag->first();
             $tag->Value = $value;
             $tag->MultiValue = false;
             $tag->write();
         }
         //regardless of whether we created a new tag or are just updating an existing one, add the relation
         $tag->Documents()->add($this);
     }
 }