mtrack /inc/snippet.php

Language PHP Lines 78
MD5 Hash f8579878a6477257217d0e922307f216 Estimated Cost $1,507 (why?)
Repository https://bitbucket.org/yoander/mtrack View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php # vim:ts=2:sw=2:et:
/* For licensing and copyright terms, see the file named LICENSE */

class MTrackSnippet {
  public $snid = null;
  public $description = null;
  public $lang = null;
  public $snippet = null;
  public $created = null;
  public $updated = null;

  static function loadById($id)
  {
    foreach (MTrackDB::q('select snid from snippets where snid = ?', $id)
        ->fetchAll() as $row) {
      return new self($row[0]);
    }
    return null;
  }

  function __construct($id = null)
  {
    if ($id !== null) {
      $this->snid = $id;

      list($row) = MTrackDB::q('select * from snippets where snid = ?', $id)
        ->fetchAll(PDO::FETCH_ASSOC);
      foreach ($row as $k => $v) {
        $this->$k = $v;
      }
    }
  }

  function save(MTrackChangeset $CS)
  {
    $this->updated = $CS->cid;

    if (!strlen(trim($this->snippet))) {
      throw new Exception("Snippet cannot be empty");
    }

    if ($this->snid === null) {
      $this->created = $CS->cid;

      $this->snid = sha1(
        $CS->who . ':' .
        $CS->when . ':' .
        $this->description . ':' .
        $this->lang . ':' .
        $this->snippet);

      MTrackDB::q('insert into snippets
          (snid, created, updated, description, lang, snippet)
          values (?, ?, ?, ?, ?, ?)',
        $this->snid,
        $this->created,
        $this->updated,
        $this->description,
        $this->lang,
        $this->snippet
        );
    } else {
      MTrackDB::q('update snippets set updated = ?,
          description = ?, lang = ?, snippet = ?
          WHERE snid = ?',
        $this->updated,
        $this->description,
        $this->lang,
        $this->snippet,
        $this->snid
        );
    }
  }
}

MTrackACL::registerAncestry('snippet', 'Snippets');
Back to Top