| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class Package |
|---|
| 4 | { |
|---|
| 5 | private $_id; |
|---|
| 6 | private $_pkgname; |
|---|
| 7 | private $_developer; |
|---|
| 8 | private $_distro; |
|---|
| 9 | private $_status = FALSE; |
|---|
| 10 | |
|---|
| 11 | private $_db; |
|---|
| 12 | private $_update = FALSE; |
|---|
| 13 | |
|---|
| 14 | public function __construct($id=NULL) |
|---|
| 15 | { |
|---|
| 16 | $this->_id = $id; |
|---|
| 17 | |
|---|
| 18 | if($id != NULL) { |
|---|
| 19 | $this->_connect(); |
|---|
| 20 | $result=$this->_query("select * from queue where id='$id'"); |
|---|
| 21 | $row=$this->_fetch($result); |
|---|
| 22 | |
|---|
| 23 | $this->setPkgName($row['pkgname']); |
|---|
| 24 | $this->setId($row['id']); |
|---|
| 25 | $this->setDeveloper($row['developer']); |
|---|
| 26 | $this->setDistro($row['distro']); |
|---|
| 27 | $this->setStatus($row['status']); |
|---|
| 28 | $this->_setUpdate(FALSE); |
|---|
| 29 | } |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | protected function _connect() |
|---|
| 33 | { |
|---|
| 34 | $this->_db = sqlite_open(SQLITE_LOCATION); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | private function _disconnect() |
|---|
| 38 | { |
|---|
| 39 | if($this->_db) { |
|---|
| 40 | $this->_db = sqlite_open(SQLITE_LOCATION); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | protected function _query($query) |
|---|
| 45 | { |
|---|
| 46 | if(!$this->_db) { |
|---|
| 47 | die("OOPS, call Alan ! 001"); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | return sqlite_query($this->_db,$query); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | protected function _fetch($result) |
|---|
| 54 | { |
|---|
| 55 | if(!$this->_db) { |
|---|
| 56 | die("OOPS, Call Alan ! 002"); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | return sqlite_fetch_array($result); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public function setPkgName($name) |
|---|
| 63 | { |
|---|
| 64 | $this->_pkgname = $name; |
|---|
| 65 | $this->_update = TRUE; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public function getPkgName() |
|---|
| 69 | { |
|---|
| 70 | return $this->_pkgname; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public function setId($id) |
|---|
| 74 | { |
|---|
| 75 | $this->_id = $id; |
|---|
| 76 | $this->_update = TRUE; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | public function getId() |
|---|
| 80 | { |
|---|
| 81 | return $this->_id; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | public function setDeveloper($name) |
|---|
| 85 | { |
|---|
| 86 | $this->_developer = $name; |
|---|
| 87 | $this->_update = TRUE; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | public function getDeveloper() |
|---|
| 91 | { |
|---|
| 92 | return $this->_developer; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public function setDistro($name) |
|---|
| 96 | { |
|---|
| 97 | $this->_distro = $name; |
|---|
| 98 | $this->_update = TRUE; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | public function getDistro() |
|---|
| 102 | { |
|---|
| 103 | return $this->_distro; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | public function setStatus($bool) |
|---|
| 107 | { |
|---|
| 108 | $this->_status = $bool; |
|---|
| 109 | $this->_update = TRUE; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | public function getStatus() |
|---|
| 113 | { |
|---|
| 114 | return $this->_status; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | protected function _setUpdate($value){ |
|---|
| 118 | $this->_update=$value; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | public function checkisuniq(){ |
|---|
| 122 | $this->_connect(); |
|---|
| 123 | if(trim($this->_pkgname)=='') |
|---|
| 124 | return FALSE; |
|---|
| 125 | $result=$this->_query("select * from queue where pkgname='{$this->_pkgname}' and distro='{$this->_distro}' "); |
|---|
| 126 | if(!sqlite_num_rows($result)) |
|---|
| 127 | return TRUE; |
|---|
| 128 | return FALSE; |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | private function _new() |
|---|
| 132 | { |
|---|
| 133 | |
|---|
| 134 | $this->_connect(); |
|---|
| 135 | if($this->checkisuniq($this->_pkgname,$this->_distro)){ |
|---|
| 136 | $this->_query("insert into queue values (NULL,'{$this->_pkgname}','{$this->_developer}','{$this->_distro}','{$this->_status}')"); |
|---|
| 137 | $this->_update=FALSE; |
|---|
| 138 | return TRUE; |
|---|
| 139 | } |
|---|
| 140 | return FALSE; |
|---|
| 141 | |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | private function _edit() |
|---|
| 145 | { |
|---|
| 146 | $this->_query("update queue set pkgname='{$this->_pkgname}', developer='{$this->_developer}', ". |
|---|
| 147 | "distro='{$this->_distro}', status='{$this->_status}' where id='{$this->_id}'"); |
|---|
| 148 | $this->_update=FALSE; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private function _delete($id){ |
|---|
| 152 | $this->_query("delete from queue where id='$id'"); |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | public function delete(){ |
|---|
| 156 | if($this->_id!=null) |
|---|
| 157 | $this->_delete($this->_id); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | public function save(){ |
|---|
| 161 | |
|---|
| 162 | if($this->_id == NULL) { |
|---|
| 163 | $this->_new(); |
|---|
| 164 | } else { |
|---|
| 165 | $this->_edit(); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | public function __destruct() |
|---|
| 171 | { |
|---|
| 172 | if($this->_update==TRUE) |
|---|
| 173 | $this->save(); |
|---|
| 174 | $this->_disconnect(); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | class Packages extends Package { |
|---|
| 180 | |
|---|
| 181 | public function getAll($condition='') { |
|---|
| 182 | |
|---|
| 183 | $this->_connect(); |
|---|
| 184 | if($condition!='') |
|---|
| 185 | $condition= ' And ' . $condition; |
|---|
| 186 | $result = $this->_query("select * from queue where 1 $condition"); |
|---|
| 187 | |
|---|
| 188 | $objs=array(); |
|---|
| 189 | while($row = $this->_fetch($result)) { |
|---|
| 190 | $obj=new Package(); |
|---|
| 191 | $obj->setPkgName($row['pkgname']); |
|---|
| 192 | $obj->setId($row['id']); |
|---|
| 193 | $obj->setDeveloper($row['developer']); |
|---|
| 194 | $obj->setDistro($row['distro']); |
|---|
| 195 | $obj->setStatus($row['status']); |
|---|
| 196 | $obj->_setUpdate(FALSE); |
|---|
| 197 | $objs[]=$obj; |
|---|
| 198 | |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | return ($objs); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | public function deleteAll() { |
|---|
| 205 | $this->_connect(); |
|---|
| 206 | $this->_query("delete from queue where 1"); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|