File modules/cms/model/Tag.class.php
Last commit: Thu Feb 16 22:57:35 2023 +0100 Jan Dankert New: More functions (adding, deleting) for tags.
1 <?php 2 3 namespace cms\model; 4 5 use cms\base\DB; 6 use database\Database; 7 use logger\Logger; 8 use util\exception\ObjectNotFoundException; 9 use util\FileUtils; 10 use util\Request; 11 use util\Session; 12 13 14 /** 15 * Tag. 16 * 17 * @author Jan Dankert 18 */ 19 class Tag extends ModelBase 20 { 21 public $tagid; 22 23 public $name; 24 25 public $objects; 26 27 public $projectid; 28 29 // Konstruktor 30 public function __construct( $tagid='' ) 31 { 32 if ( $tagid ) 33 $this->tagid = $tagid; 34 } 35 36 37 38 /** 39 * Reads all objects with this tag 40 * 41 * @return BaseObject[] objects 42 */ 43 function getObjects() 44 { 45 $db = DB::get(); 46 47 $sql = $db->sql( <<<SQL 48 SELECT {{object}}.* 49 FROM {{object}} 50 WHERE id IN ( 51 SELECT objectid FROM {{tag_object}} WHERE tagid = {tagid} 52 ) 53 SQL 54 ); 55 $sql->setInt('tagid' ,$this->tagid ); 56 57 return array_map( function($row) { 58 // converting the result row into an baseObject. 59 $baseObject = new BaseObject( $row['id'] ); 60 $baseObject->setDatabaseRow( $row ); 61 return $baseObject; 62 63 },$sql->getAll() ); 64 } 65 66 67 68 // Laden 69 70 /** 71 * @throws \util\exception\ObjectNotFoundException 72 */ 73 public function load() 74 { 75 $sql = Db::sql( <<<SQL 76 SELECT * FROM {{tag}} 77 WHERE id={tagid} 78 SQL 79 ); 80 $sql->setInt( 'tagid',$this->tagid ); 81 82 $row = $sql->getRow(); 83 84 if ( empty($row) ) 85 throw new ObjectNotFoundException('tag '.$this->tagid.' not found'); 86 87 $this->name = $row['name' ]; 88 $this->projectid = $row['projectid']; 89 90 return $this; 91 } 92 93 94 /** 95 * Save. 96 * @return void 97 */ 98 public function save() 99 { 100 $stmt = DB::sql( <<<SQL 101 UPDATE {{tag}} 102 SET name = {name} 103 WHERE id= {tagid} 104 SQL 105 ); 106 $stmt->setString('name' ,$this->name ); 107 $stmt->setInt ('tagid',$this->tagid ); 108 109 $stmt->execute(); 110 } 111 112 113 /** 114 * Add a project to the database. 115 */ 116 public function add() 117 { 118 $sql = DB::sql('SELECT MAX(id) FROM {{tag}}'); 119 $this->tagid = intval($sql->getOne())+1; 120 121 $sql = DB::sql( <<<SQL 122 INSERT INTO {{tag}} 123 ( id ,projectid ,name ) 124 VALUES ( {tagid},{projectid},{name} ) 125 SQL 126 ); 127 $sql->setInt ('tagid' ,$this->tagid ); 128 $sql->setInt ('projectid',$this->projectid ); 129 $sql->setString('name' ,$this->name ); 130 131 $sql->execute(); 132 } 133 134 135 /** 136 * Remove tag 137 * @return void 138 */ 139 public function delete() 140 { 141 // Deleting the tag objects 142 $sql = DB::sql( 'DELETE FROM {{tag_object}}'. 143 ' WHERE tagid= {tagid} ' ); 144 $sql->setInt( 'tagid',$this->tagid ); 145 $sql->execute(); 146 147 // Deleting the tag itself 148 $sql = DB::sql( 'DELETE FROM {{tag}}'. 149 ' WHERE id= {tagid} ' ); 150 $sql->setInt( 'tagid',$this->tagid ); 151 $sql->execute(); 152 } 153 154 public function getName() 155 { 156 return $this->name; 157 } 158 159 160 public function getId() 161 { 162 return $this->tagid; 163 } 164 165 public function addObject(float $objectid) 166 { 167 $sql = DB::sql('SELECT MAX(id) FROM {{tag_object}}'); 168 $id = intval($sql->getOne())+1; 169 170 $sql = DB::sql( <<<SQL 171 INSERT INTO {{tag_object}} 172 ( id ,tagid ,objectid ) 173 VALUES ( {id},{tagid},{objectid} ) 174 SQL 175 ); 176 $sql->setInt ('id' ,$id ); 177 $sql->setInt ('tagid' ,$this->tagid ); 178 $sql->setString('objectid',$objectid ); 179 180 $sql->execute(); 181 182 } 183 184 185 public function removeObject(float $objectid) 186 { 187 $sql = DB::sql( <<<SQL 188 DELETE FROM {{tag_object}} 189 WHERE tagid={tagid} 190 AND objectid={objectid} 191 SQL 192 ); 193 $sql->setInt ('tagid' ,$this->tagid ); 194 $sql->setString('objectid',$objectid ); 195 196 $sql->execute(); 197 } 198 199 } 200
Downloadmodules/cms/model/Tag.class.php
History Thu, 16 Feb 2023 22:57:35 +0100 Jan Dankert New: More functions (adding, deleting) for tags. Thu, 16 Feb 2023 01:04:38 +0100 Jan Dankert New: Tags for base objects.