/inc/snippet.php

https://bitbucket.org/yoander/mtrack · PHP · 77 lines · 65 code · 11 blank · 1 comment · 4 complexity · f8579878a6477257217d0e922307f216 MD5 · raw file

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