File modules/cms/action/object/ObjectTagAction.class.php
Last commit: Thu Feb 16 01:04:38 2023 +0100 Jan Dankert New: Tags for base objects.
1 <?php 2 namespace cms\action\object; 3 use cms\action\Method; 4 use cms\action\ObjectAction; 5 use cms\model\BaseObject; 6 use cms\model\Permission; 7 use cms\model\Tag; 8 use util\ArrayUtils; 9 10 class ObjectTagAction extends ObjectAction implements Method { 11 12 public function view() { 13 14 $this->setTemplateVars( $this->baseObject->getProperties() ); 15 16 $myTags = $this->baseObject->getTags(); 17 $allTags = $this->baseObject->getProject()->getTags(); 18 19 $allTags = ArrayUtils::mapToNewArray( $allTags, function($id,$name) use ($myTags) { 20 $this->setTemplateVar('tag'.$id,array_key_exists($id,$myTags) ); 21 return[ 'tag'.$id => $name ]; 22 } ); 23 24 $this->setTemplateVar('tags',$allTags ); 25 } 26 27 28 public function post() { 29 30 $allTags = $this->baseObject->getProject()->getTags(); 31 $myTags = $this->baseObject->getTags(); 32 33 foreach ( $allTags as $tagId => $tagName ) { 34 if ( $this->request->isTrue('tag'.$tagId) ) { 35 if ( ! array_key_exists( $tagId,$myTags ) ) { 36 $tag = new Tag( $tagId ); 37 $tag->addObject( $this->baseObject->objectid ); 38 } 39 } else { 40 if ( array_key_exists( $tagId,$myTags ) ) { 41 $tag = new Tag( $tagId ); 42 $tag->removeObject( $this->baseObject->objectid ); 43 } 44 } 45 46 } 47 48 $newTags = $this->request->getText('new'); 49 $newTags = explode(',',$newTags); 50 foreach ( $newTags as $newTag ) { 51 52 if ( ! $newTag ) 53 continue; // refuse empty tags 54 55 if ( in_array($newTag,$allTags) ) { 56 // the requested tag is already in the project 57 if ( in_array($newTag,$myTags) ) { 58 // the requested tag is already bound to this object, so nothing to do here. 59 }else { 60 $id = array_search($newTag,$allTags); 61 $tag = new Tag($id); 62 $tag->addObject( $this->baseObject->objectid ); 63 } 64 }else{ 65 // a new tag will be created 66 $tag = new Tag(); 67 $tag->projectid = $this->baseObject->projectid; 68 $tag->name = $newTag; 69 $tag->persist(); 70 71 $tag->addObject( $this->baseObject->objectid ); 72 } 73 } 74 } 75 }
Downloadmodules/cms/action/object/ObjectTagAction.class.php
History Thu, 16 Feb 2023 01:04:38 +0100 Jan Dankert New: Tags for base objects.