/lib/core/model.php

https://github.com/SecPlus/secplus-php · PHP · 365 lines · 281 code · 75 blank · 9 comment · 36 complexity · 55754edc7c526b0be75310c6cb29d7b4 MD5 · raw file

  1. <?php
  2. namespace SecPlus;
  3. /**
  4. * Interface to Models
  5. */
  6. interface IModel {
  7. }
  8. /**
  9. * Every Model need extend this abstract class
  10. */
  11. abstract class AbstractModel implements IModel {
  12. /**
  13. * Singleton instance of the database connection
  14. */
  15. public static $conn = null;
  16. protected $config;
  17. protected $_table_name;
  18. protected $_id_name = 'id';
  19. protected $_vo_name;
  20. public function __construct() {
  21. $this->_connect();
  22. $this->config = \Config::getInstance();
  23. }
  24. public function _connect() {
  25. self::$conn = Database::getConnection();
  26. }
  27. public function setTableName($tname) {
  28. $this->_table_name = $tname;
  29. }
  30. public function getTableName() {
  31. return $this->_table_name;
  32. }
  33. public function setValueObjectName($name) {
  34. $this->_vo_name = $name;
  35. }
  36. public function _setupDAO() {
  37. $this->_table_name = !empty($this->_table_name) ? $this->_table_name : strtolower(str_replace(__NAMESPACE__, "", str_replace('DAO', '', get_class($this))));
  38. $this->_vo_name = !empty($this->_vo_name) ? $this->_vo_name : ucfirst($this->_table_name);
  39. }
  40. public function get($id) {
  41. $this->_setupDAO();
  42. try {
  43. $stmt = self::$conn->prepare("SELECT * FROM " . $this->_table_name . " WHERE " . $this->_id_name . " = :id");
  44. $stmt->bindParam(':id', $id, \PDO::PARAM_INT);
  45. $stmt->execute();
  46. $result = $stmt->fetchAll();
  47. if (count($result) > 0) {
  48. $r = $result[0];
  49. $voName = $this->_vo_name;
  50. $obj = new $voName();
  51. $obj = $this->map2object($obj, $r);
  52. return $obj;
  53. }
  54. return NULL;
  55. } catch (Exception $e) {
  56. if ($this->config->isDebug()) {
  57. print $e->getMessage();
  58. die();
  59. } else {
  60. print "database error.\n";
  61. die();
  62. }
  63. }
  64. return NULL;
  65. }
  66. public function delete($id) {
  67. $this->_setupDAO();
  68. try {
  69. $stmt = self::$conn->prepare("DELETE FROM " . $this->_table_name . " WHERE " . $this->_id_name . " = :id");
  70. $stmt->bindParam(':id', $id, \PDO::PARAM_INT);
  71. $stmt->execute();
  72. return $stmt->rowCount() > 0;
  73. } catch (Exception $e) {
  74. if ($this->config->isDebug()) {
  75. print $e->getMessage();
  76. die();
  77. } else {
  78. print "database error.\n";
  79. die();
  80. }
  81. }
  82. return NULL;
  83. }
  84. public function first() {
  85. $this->_setupDAO();
  86. try {
  87. $stmt = self::$conn->prepare("select * from " . $this->_table_name . " limit 1");
  88. $stmt->execute();
  89. $result = $stmt->fetchAll();
  90. $objects = array();
  91. if (count($result) == 1) {
  92. $voName = $this->_vo_name;
  93. $obj = new $voName();
  94. $obj = $this->map2object($obj, $result[0]);
  95. return $obj;
  96. } else {
  97. return NULL;
  98. }
  99. } catch (Exception $e) {
  100. if ($this->config->isDebug()) {
  101. print $e->getMessage();
  102. }
  103. }
  104. }
  105. public function getAll() {
  106. $this->_setupDAO();
  107. try {
  108. $stmt = self::$conn->prepare("select * from " . $this->_table_name);
  109. $stmt->execute();
  110. $result = $stmt->fetchAll();
  111. $objects = array();
  112. for ($i = 0; $i < count($result); $i++) {
  113. $r = $result[$i];
  114. $voName = $this->_vo_name;
  115. $obj = new $voName();
  116. $obj = $this->map2object($obj, $r);
  117. $objects[] = $obj;
  118. }
  119. return $objects;
  120. } catch (Exception $e) {
  121. if ($this->config->isDebug()) {
  122. print $e->getMessage();
  123. die();
  124. }
  125. }
  126. return NULL;
  127. }
  128. public function update($obj) {
  129. $this->_setupDAO();
  130. try {
  131. $data = $obj->getData();
  132. $keys = array_keys($data);
  133. $sql = SQLBuilder::update($this->_table_name, $keys, array($this->_id_name));
  134. $stmt = self::$conn->prepare($sql);
  135. foreach($data as $name => &$val) {
  136. if ($name == $this->_id_name) {
  137. continue;
  138. }
  139. $stmt->bindParam(':' . $name, $val[0], $val[1]);
  140. }
  141. $stmt->bindParam(':' . $this->_id_name, $data[$this->_id_name][0], \PDO::PARAM_INT);
  142. $stmt->execute();
  143. return $stmt->rowCount() > 0;
  144. } catch (Exception $e) {
  145. if ($this->config->isDebug()) {
  146. print $e->getMessage();
  147. die();
  148. }
  149. }
  150. return 0;
  151. }
  152. public function save($obj) {
  153. $this->_setupDAO();
  154. try {
  155. $data = $obj->getData();
  156. $valid_keys = $this->getColumns();
  157. $keys = array_keys($data);
  158. if (!$this->_validate_keys($keys, $valid_keys)) {
  159. throw new \Exception("Invalid field name in object: " . implode(", ", $keys));
  160. }
  161. $sql = SQLBuilder::insert($this->_table_name, $keys, $this->_id_name);
  162. $stmt = self::$conn->prepare($sql);
  163. foreach($data as $name => &$val) {
  164. if ($name == $this->_id_name) {
  165. continue;
  166. }
  167. $stmt->bindParam(':' . $name, $val[0], $val[1]);
  168. }
  169. $stmt->execute();
  170. return $stmt->rowCount() > 0;
  171. } catch (Exception $e) {
  172. if ($this->config->isDebug()) {
  173. print $e->getMessage();
  174. print "query: " . $sql;
  175. die();
  176. }
  177. }
  178. return 0;
  179. }
  180. protected function _validate_keys($keys, $valid_keys) {
  181. foreach ($keys as $k) {
  182. if (!in_array($k, $valid_keys)) {
  183. return FALSE;
  184. }
  185. }
  186. return TRUE;
  187. }
  188. public function map2object($user, $res) {
  189. $keys = array_keys($res);
  190. for ($j = 0; $j < count($keys); $j++) {
  191. if (is_string($keys[$j])) {
  192. $user->{$keys[$j]} = $res[$keys[$j]];
  193. }
  194. }
  195. return $user;
  196. }
  197. public function map2array($res) {
  198. $ar = array();
  199. $keys = array_keys($res);
  200. for ($j = 0; $j < count($keys); $j++) {
  201. if (is_string($keys[$j])) {
  202. $ar[] = array($keys[$j], $res[$keys[$j]]);
  203. }
  204. }
  205. return $ar;
  206. }
  207. public function getDataByColumn($columnName, $value, $type = \PDO::PARAM_STR) {
  208. $this->_setupDAO();
  209. try {
  210. $stmt = self::$conn->prepare("select * from " . $this->_table_name . " where " . $columnName . " = :".$columnName);
  211. $stmt->bindParam(':' . $columnName, $value, $type);
  212. $stmt->execute();
  213. $result = $stmt->fetchAll();
  214. $objects = array();
  215. for ($i = 0; $i < count($result); $i++) {
  216. $r = $result[$i];
  217. $voName = $this->_vo_name;
  218. $obj = new $voName();
  219. $obj = $this->map2object($obj, $r);
  220. $objects[] = $obj;
  221. }
  222. return $objects;
  223. } catch (Exception $e) {
  224. if ($this->config->isDebug()) {
  225. print $e->getMessage();
  226. die();
  227. }
  228. }
  229. return NULL;
  230. }
  231. public function searchInColumn($columnName, $value, $type = \PDO::PARAM_STR) {
  232. $this->_setupDAO();
  233. try {
  234. $stmt = self::$conn->prepare("select * from " . $this->_table_name . " where " . $columnName . " like :".$columnName);
  235. $value = "%".$value."%";
  236. $stmt->bindParam(':' . $columnName, $value, $type);
  237. $stmt->execute();
  238. $result = $stmt->fetchAll();
  239. $objects = array();
  240. for ($i = 0; $i < count($result); $i++) {
  241. $r = $result[$i];
  242. $voName = $this->_vo_name;
  243. $obj = new $voName();
  244. $obj = $this->map2object($obj, $r);
  245. $objects[] = $obj;
  246. }
  247. return $objects;
  248. } catch (Exception $e) {
  249. if ($this->config->isDebug()) {
  250. print $e->getMessage();
  251. die();
  252. }
  253. }
  254. return NULL;
  255. }
  256. public function getColumns() {
  257. $this->_setupDAO();
  258. try {
  259. $sql = "DESCRIBE ". $this->_table_name;
  260. $stmt = self::$conn->prepare($sql);
  261. $stmt->execute();
  262. $table_fields = $stmt->fetchAll(\PDO::FETCH_COLUMN);
  263. return $table_fields;
  264. } catch (Exception $e) {
  265. if ($this->config->isDebug()) {
  266. print $e->getMessage();
  267. print "query: " . $sql;
  268. }
  269. }
  270. return NULL;
  271. }
  272. public function __call($func, $args) {
  273. if (preg_match('/^get/', $func)) {
  274. $prop = lcfirst(substr($func, 3));
  275. if (empty($prop)) {
  276. Helper::throwPermissionDeniedMethod($func);
  277. return;
  278. }
  279. if (!in_array($prop, $this->getColumns())) {
  280. Helper::throwPermissionDeniedMethod($prop);
  281. return;
  282. }
  283. if (count($args) == 1) {
  284. return $this->getDataByColumn($prop, $args[0]);
  285. } else if (count($args) == 2) {
  286. return $this->getDataByColumn($prop, $args[0], $args[1]);
  287. }
  288. }
  289. }
  290. }