model.lib.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides basic methods to implement a CRUD for a new table in the database see examples in: career.lib.php and promotion.lib.php
  5. * Include/require it in your code to use its features.
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Class
  10. * @package chamilo.library
  11. */
  12. class Model {
  13. var $table;
  14. var $columns;
  15. var $required;
  16. // var $pk; some day this will be implemented
  17. public function __construct() {
  18. }
  19. /**
  20. * Useful finder - experimental akelos like only use in notification.lib.php send function
  21. */
  22. public function find($type, $options = null) {
  23. switch($type) {
  24. case 'all':
  25. return self::get_all($options);
  26. break;
  27. case (is_numeric($type)) :
  28. return self::get($type);
  29. break;
  30. }
  31. }
  32. /**
  33. * Delets an item
  34. */
  35. public function delete($id) {
  36. if (empty($id) or $id != strval(intval($id))) { return false; }
  37. // Database table definition
  38. $result = Database :: delete($this->table, array('id = ?' => $id));
  39. if ($result != 1){
  40. return false;
  41. }
  42. return true;
  43. }
  44. private function clean_parameters($params){
  45. $clean_params = array();
  46. if (!empty($params)) {
  47. foreach($params as $key=>$value) {
  48. if (in_array($key, $this->columns)) {
  49. $clean_params[$key] = $value;
  50. }
  51. }
  52. }
  53. return $clean_params;
  54. }
  55. /**
  56. * Displays the title + grid
  57. */
  58. public function display() {
  59. }
  60. /**
  61. * Gets an element
  62. */
  63. public function get($id) {
  64. if (empty($id)) { return array(); }
  65. $result = Database::select('*',$this->table, array('where'=>array('id = ?'=>intval($id))),'first');
  66. return $result;
  67. }
  68. public function get_all($options = null) {
  69. return Database::select('*',$this->table, $options);
  70. }
  71. /**
  72. * Get the count of elements
  73. */
  74. public function get_count() {
  75. $row = Database::select('count(*) as count', $this->table, array(),'first');
  76. return $row['count'];
  77. }
  78. /**
  79. * a little bit of javascript to display
  80. */
  81. public function javascript() {
  82. }
  83. /**
  84. * Saves an element into the DB
  85. *
  86. * @param array $values
  87. * @return bool
  88. *
  89. */
  90. public function save($params, $show_query = false) {
  91. $params = $this->clean_parameters($params);
  92. if (!empty($this->required)) {
  93. $require_ok = true;
  94. $kay_params = array_keys($params);
  95. foreach ($this->required as $field) {
  96. if (!in_array($field, $kay_params)) {
  97. $require_ok = false;
  98. }
  99. }
  100. if (!$require_ok) {
  101. return false;
  102. }
  103. }
  104. if (in_array('created_at', $this->columns)) {
  105. $params['created_at'] = api_get_utc_datetime();
  106. }
  107. if (!empty($params)) {
  108. $id = Database::insert($this->table, $params, $show_query);
  109. if (is_numeric($id)){
  110. return $id;
  111. }
  112. }
  113. return false;
  114. }
  115. /**
  116. * Updates the obj in the database. The $params['id'] must exist in order to update a record
  117. *
  118. * @param array $values
  119. *
  120. */
  121. public function update($params) {
  122. $params = $this->clean_parameters($params);
  123. //If the class has the updated_at field we update the date
  124. if (in_array('updated_at', $this->columns)) {
  125. $params['updated_at'] = api_get_utc_datetime();
  126. }
  127. //If the class has the created_at field then we remove it
  128. if (in_array('created_at', $this->columns)) {
  129. unset($params['created_at']);
  130. }
  131. if (!empty($params) && !empty($params['id'])) {
  132. $id = intval($params['id']);
  133. unset($params['id']); //To not overwrite the id
  134. if (is_numeric($id)) {
  135. $result = Database::update($this->table, $params, array('id = ?'=>$id));
  136. if ($result){
  137. return true;
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. }