php8
[web.mtrack] / MTrack / Component.php
1 <?php
2
3 //require_once 'MTrack/Changeset.php';
4 require_once 'MTrack/DB.php';
5
6
7 class MTrackComponent {
8   public $compid = null;
9   public $name = null;
10   public $deleted = null;
11   protected $projects = null;
12   protected $origprojects = null;
13
14   static function loadById($id) {
15     return new MTrackComponent($id);
16   }
17     function loadByIds($value) 
18     {
19         $ar = explode(',', $value);
20         $ret = array();
21         foreach($ar as $k) {
22             if (empty($k)) {
23                 continue;
24             }
25             $ret[] = new MTrackComponent($k);
26         }
27         return $ret;
28     }
29     function toHtml()
30     {
31         return 
32             ($this->deleted ? '<del>' : '') . 
33             htmlentities($this->name , ENT_QUOTES, 'utf-8') .
34             ($this->deleted ? '</del>' : ''); 
35             
36     }
37    
38    
39   static function loadByName($name) {
40     $rows = MTrackDB::q('select compid from components where name = ?',
41       $name)->fetchAll(PDO::FETCH_COLUMN, 0);
42     if (isset($rows[0])) {
43       return self::loadById($rows[0]);
44     }
45     return null;
46   }
47
48   function __construct($id = null) {
49     if ($id !== null) {
50       list($row) = MTrackDB::q(
51                     'select name, deleted from components where compid = ?',
52                     $id)->fetchAll();
53       if (isset($row[0])) {
54         $this->compid = $id;
55         $this->name = $row[0];
56         $this->deleted = $row[1];
57         return;
58       }
59       throw new Exception("unable to find component with id = $id");
60     }
61     $this->deleted = false;
62   }
63
64   function getProjects() {
65     if ($this->origprojects === null) {
66       $this->origprojects = array();
67       foreach (MTrackDB::q('select projid from components_by_project where compid = ? order by projid', $this->compid) as $row) {
68         $this->origprojects[] = $row[0];
69       }
70       $this->projects = $this->origprojects;
71     }
72     return $this->projects;
73   }
74
75   function setProjects($projlist) {
76     $this->projects = $projlist;
77   }
78
79   function save(MTrackChangeset $CS)
80   {
81     if ($this->compid) {
82       list($row) = MTrackDB::q(
83                     'select name, deleted from components where compid = ?',
84                     $this->compid)->fetchAll();
85       $old = $row;
86       MTrackDB::q(
87           'update components set name = ?, deleted = ? where compid = ?',
88           $this->name, (int)$this->deleted, $this->compid);
89     } else {
90       MTrackDB::q('insert into components (name, deleted) values (?, ?)',
91         $this->name, (int)$this->deleted);
92       $this->compid = MTrackDB::lastInsertId('components', 'compid');
93       $old = null;
94     }
95     $CS->add("component:" . $this->compid . ":name", $old['name'], $this->name);
96     $CS->add("component:" . $this->compid . ":deleted", $old['deleted'], $this->deleted);
97     if ($this->projects !== $this->origprojects) {
98       $old = is_array($this->origprojects) ?
99               join(",", $this->origprojects) : '';
100       $new = is_array($this->projects) ?
101               join(",", $this->projects) : '';
102       MTrackDB::q('delete from components_by_project where compid = ?',
103           $this->compid);
104       if (is_array($this->projects)) {
105         foreach ($this->projects as $pid) {
106           MTrackDB::q(
107             'insert into components_by_project (compid, projid) values (?, ?)',
108             $this->compid, $pid);
109         }
110       }
111       $CS->add("component:$this->compid:projects", $old, $new);
112     }
113   }
114
115   
116   
117 }