3 //require_once 'MTrack/Changeset.php';
4 require_once 'MTrack/DB.php';
7 class MTrackComponent {
10 public $deleted = null;
11 protected $projects = null;
12 protected $origprojects = null;
14 static function loadById($id) {
15 return new MTrackComponent($id);
17 function loadByIds($value)
19 $ar = explode(',', $value);
25 $ret[] = new MTrackComponent($k);
32 ($this->deleted ? '<del>' : '') .
33 htmlentities($this->name , ENT_QUOTES, 'utf-8') .
34 ($this->deleted ? '</del>' : '');
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]);
48 function __construct($id = null) {
50 list($row) = MTrackDB::q(
51 'select name, deleted from components where compid = ?',
55 $this->name = $row[0];
56 $this->deleted = $row[1];
59 throw new Exception("unable to find component with id = $id");
61 $this->deleted = false;
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];
70 $this->projects = $this->origprojects;
72 return $this->projects;
75 function setProjects($projlist) {
76 $this->projects = $projlist;
79 function save(MTrackChangeset $CS)
82 list($row) = MTrackDB::q(
83 'select name, deleted from components where compid = ?',
84 $this->compid)->fetchAll();
87 'update components set name = ?, deleted = ? where compid = ?',
88 $this->name, (int)$this->deleted, $this->compid);
90 MTrackDB::q('insert into components (name, deleted) values (?, ?)',
91 $this->name, (int)$this->deleted);
92 $this->compid = MTrackDB::lastInsertId('components', 'compid');
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 = ?',
104 if (is_array($this->projects)) {
105 foreach ($this->projects as $pid) {
107 'insert into components_by_project (compid, projid) values (?, ?)',
108 $this->compid, $pid);
111 $CS->add("component:$this->compid:projects", $old, $new);