deleted ? '' : '') . htmlentities($this->name , ENT_QUOTES, 'utf-8') . ($this->deleted ? '' : ''); } static function loadByName($name) { $rows = MTrackDB::q('select compid from components where name = ?', $name)->fetchAll(PDO::FETCH_COLUMN, 0); if (isset($rows[0])) { return self::loadById($rows[0]); } return null; } function __construct($id = null) { if ($id !== null) { list($row) = MTrackDB::q( 'select name, deleted from components where compid = ?', $id)->fetchAll(); if (isset($row[0])) { $this->compid = $id; $this->name = $row[0]; $this->deleted = $row[1]; return; } throw new Exception("unable to find component with id = $id"); } $this->deleted = false; } function getProjects() { if ($this->origprojects === null) { $this->origprojects = array(); foreach (MTrackDB::q('select projid from components_by_project where compid = ? order by projid', $this->compid) as $row) { $this->origprojects[] = $row[0]; } $this->projects = $this->origprojects; } return $this->projects; } function setProjects($projlist) { $this->projects = $projlist; } function save(MTrackChangeset $CS) { if ($this->compid) { list($row) = MTrackDB::q( 'select name, deleted from components where compid = ?', $this->compid)->fetchAll(); $old = $row; MTrackDB::q( 'update components set name = ?, deleted = ? where compid = ?', $this->name, (int)$this->deleted, $this->compid); } else { MTrackDB::q('insert into components (name, deleted) values (?, ?)', $this->name, (int)$this->deleted); $this->compid = MTrackDB::lastInsertId('components', 'compid'); $old = null; } $CS->add("component:" . $this->compid . ":name", $old['name'], $this->name); $CS->add("component:" . $this->compid . ":deleted", $old['deleted'], $this->deleted); if ($this->projects !== $this->origprojects) { $old = is_array($this->origprojects) ? join(",", $this->origprojects) : ''; $new = is_array($this->projects) ? join(",", $this->projects) : ''; MTrackDB::q('delete from components_by_project where compid = ?', $this->compid); if (is_array($this->projects)) { foreach ($this->projects as $pid) { MTrackDB::q( 'insert into components_by_project (compid, projid) values (?, ?)', $this->compid, $pid); } } $CS->add("component:$this->compid:projects", $old, $new); } } }