php8
[web.mtrack] / MTrack / Project.php
1 <?php
2
3 require_once 'MTrack/DB.php';
4 require_once 'MTrack/Config.php';
5 //require_once 'MTrack/Changeset.php';
6
7 class MTrackProject {
8   public $projid = null;
9   public $ordinal = 5;
10   public $name = null;
11   public $shortname = null;
12   public $notifyemail = null;
13
14   static function loadById($id) {
15     return new MTrackProject($id);
16   }
17
18   static function loadByName($name) {
19     list($row) = MTrackDB::q('select projid from projects where shortname = ?',
20       $name)->fetchAll();
21     if (isset($row[0])) {
22       return self::loadById($row[0]);
23     }
24     return null;
25   }
26
27   function __construct($id = null) {
28     if ($id !== null) {
29       list($row) = MTrackDB::q(
30                     'select * from projects where projid = ?',
31                     $id)->fetchAll();
32       if (isset($row[0])) {
33         $this->projid = $row['projid'];
34         $this->ordinal = $row['ordinal'];
35         $this->name = $row['name'];
36         $this->shortname = $row['shortname'];
37         $this->notifyemail = $row['notifyemail'];
38         return;
39       }
40       throw new Exception("unable to find project with id = $id");
41     }
42   }
43
44   function save(MTrackChangeset $CS) {
45     if ($this->projid) {
46       list($row) = MTrackDB::q(
47                     'select * from projects where projid = ?',
48                     $this->projid)->fetchAll();
49       $old = $row;
50       MTrackDB::q(
51           'update projects set ordinal = ?, name = ?, shortname = ?,
52             notifyemail = ? where projid = ?',
53           $this->ordinal, $this->name, $this->shortname,
54           $this->notifyemail, $this->projid);
55     } else {
56       MTrackDB::q('insert into projects (ordinal, name,
57           shortname, notifyemail) values (?, ?, ?, ?)',
58         $this->ordinal, $this->name, $this->shortname,
59         $this->notifyemail);
60       $this->projid = MTrackDB::lastInsertId('projects', 'projid');
61       $old = null;
62     }
63     $CS->add("project:" . $this->projid . ":name", $old['name'], $this->name);
64     $CS->add("project:" . $this->projid . ":ordinal", $old['ordinal'], $this->ordinal);
65     $CS->add("project:" . $this->projid . ":shortname", $old['shortname'], $this->shortname);
66     $CS->add("project:" . $this->projid . ":notifyemail", $old['notifyemail'], $this->notifyemail);
67   }
68
69   function _adjust_ticket_link($M) {
70     $tktlimit = MTrackConfig::get('trac_import', "max_ticket:$this->shortname");
71     if ($M[1] <= $tktlimit) {
72       return "#$this->shortname$M[1]";
73     }
74     return $M[0];
75   }
76
77   function adjust_links($reason, $use_ticket_prefix)
78   {
79     if (!$use_ticket_prefix) {
80       return $reason;
81     }
82
83     $tktlimit = MTrackConfig::get('trac_import', "max_ticket:$this->shortname");
84     if ($tktlimit !== null) {
85       $reason = preg_replace_callback('/#(\d+)/',
86         array($this, '_adjust_ticket_link'), $reason);
87     } else {
88 //      don't do this if the number is outside the valid ranges
89 //      may need to be clever about this during trac imports
90 //      $reason = preg_replace('/#(\d+)/', "#$this->shortname\$1", $reason);
91     }
92 // FIXME: this and the above need to be more intelligent
93     $reason = preg_replace('/\[(\d+)\]/', "[$this->shortname\$1]", $reason);
94     return $reason;
95   }
96 }