php8
[web.mtrack] / MTrack / Keyword.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3 require_once 'MTrack/DB.php';
4 //require_once 'MTrack/Changeset.php';
5
6 class MTrackKeyword {
7   public $kid;
8   public $keyword;
9
10   static function loadByWord($word)
11   {
12     foreach (MTrackDB::q('select kid from keywords where keyword = ?', $word)
13         ->fetchAll() as $row) {
14       return new MTrackKeyword($row[0]);
15     }
16     return null;
17   }
18
19   function __construct($id = null)
20   {
21     if ($id !== null) {
22       list($row) = MTrackDB::q('select keyword from keywords where kid = ?',
23           $id)->fetchAll();
24       $this->kid = $id;
25       $this->keyword = $row[0];
26       return;
27     }
28   }
29    function loadByIds($value) 
30     {
31         $ar = explode(',', $value);
32         $ret = array();
33         foreach($ar as $k) {
34             if (empty($k)) {
35                 continue;
36             }
37             $ret[] = new MTrackKeyword($k);
38         }
39         return $ret;
40     }
41     function toHtml()
42     {
43         return  htmlentities($this->keyword, ENT_QUOTES, 'utf-8') ;
44             
45     }
46    
47   function save(MTrackChangeset $CS)
48   {
49     if ($this->kid === null) {
50       MTrackDB::q('insert into keywords (keyword) values (?)', $this->keyword);
51       $this->kid = MTrackDB::lastInsertId('keywords', 'kid');
52       $CS->add("keywords:keyword", null, $this->keyword);
53     } else {
54       throw new Exception("not allowed to rename keywords");
55     }
56   }
57 }
58