import
[web.mtrack] / inc / keywords.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 class MTrackKeyword {
5   public $kid;
6   public $keyword;
7
8   static function loadByWord($word)
9   {
10     foreach (MTrackDB::q('select kid from keywords where keyword = ?', $word)
11         ->fetchAll() as $row) {
12       return new MTrackKeyword($row[0]);
13     }
14     return null;
15   }
16
17   function __construct($id = null)
18   {
19     if ($id !== null) {
20       list($row) = MTrackDB::q('select keyword from keywords where kid = ?',
21           $id)->fetchAll();
22       $this->kid = $id;
23       $this->keyword = $row[0];
24       return;
25     }
26   }
27
28   function save(MTrackChangeset $CS)
29   {
30     if ($this->kid === null) {
31       MTrackDB::q('insert into keywords (keyword) values (?)', $this->keyword);
32       $this->kid = MTrackDB::lastInsertId('keywords', 'kid');
33       $CS->add("keywords:keyword", null, $this->keyword);
34     } else {
35       throw new Exception("not allowed to rename keywords");
36     }
37   }
38 }
39