php8
[web.mtrack] / MTrack / Snippet.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 class MTrack_Snippet {
5   public $snid = null;
6   public $description = null;
7   public $lang = null;
8   public $snippet = null;
9   public $created = null;
10   public $updated = null;
11
12   static function loadById($id)
13   {
14     foreach (MTrackDB::q('select snid from snippets where snid = ?', $id)
15         ->fetchAll() as $row) {
16       return new self($row[0]);
17     }
18     return null;
19   }
20
21   function __construct($id = null)
22   {
23     if ($id !== null) {
24       $this->snid = $id;
25
26       list($row) = MTrackDB::q('select * from snippets where snid = ?', $id)
27         ->fetchAll(PDO::FETCH_ASSOC);
28       foreach ($row as $k => $v) {
29         $this->$k = $v;
30       }
31     }
32   }
33
34   function save(MTrackChangeset $CS)
35   {
36     $this->updated = $CS->cid;
37
38     if ($this->snid === null) {
39       $this->created = $CS->cid;
40
41       $this->snid = sha1(
42         $CS->who . ':' .
43         $CS->when . ':' .
44         $this->description . ':' .
45         $this->lang . ':' .
46         $this->snippet);
47
48       MTrackDB::q('insert into snippets
49           (snid, created, updated, description, lang, snippet)
50           values (?, ?, ?, ?, ?, ?)',
51         $this->snid,
52         $this->created,
53         $this->updated,
54         $this->description,
55         $this->lang,
56         $this->snippet
57         );
58     } else {
59       MTrackDB::q('update snippets set updated = ?,
60           description = ?, lang = ?, snippet = ?
61           WHERE snid = ?',
62         $this->updated,
63         $this->description,
64         $this->lang,
65         $this->snippet,
66         $this->snid
67         );
68     }
69   }
70 }
71
72