model.lib.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 $pk; some day this will be implemented
  16. public function __construct() {
  17. }
  18. /**
  19. * Useful finder - experimental akelos like only use in notification.lib.php send function
  20. */
  21. public function find($type, $options = null) {
  22. switch($type) {
  23. case 'all':
  24. return self::get_all($options);
  25. break;
  26. case (is_numeric($type)) :
  27. return self::get($type);
  28. break;
  29. }
  30. }
  31. /**
  32. * Delets an item
  33. */
  34. public function delete($id) {
  35. if (empty($id) or $id != strval(intval($id))) { return false; }
  36. // Database table definition
  37. $result = Database :: delete($this->table, array('id = ?' => $id));
  38. if ($result != 1){
  39. return false;
  40. }
  41. return true;
  42. }
  43. private function clean_parameters($params){
  44. $clean_params = array();
  45. if (!empty($params)) {
  46. foreach($params as $key=>$value) {
  47. if (in_array($key, $this->columns)) {
  48. $clean_params[$key] = $value;
  49. }
  50. }
  51. }
  52. return $clean_params;
  53. }
  54. /**
  55. * Displays the title + grid
  56. */
  57. public function display() {
  58. }
  59. /**
  60. * Gets an element
  61. */
  62. public function get($id) {
  63. if (empty($id)) { return array(); }
  64. $result = Database::select('*',$this->table, array('where'=>array('id = ?'=>intval($id))),'first');
  65. return $result;
  66. }
  67. public function get_all($options = null) {
  68. return Database::select('*',$this->table, $options);
  69. }
  70. /**
  71. * Get the count of elements
  72. */
  73. public function get_count() {
  74. $row = Database::select('count(*) as count', $this->table, array(),'first');
  75. return $row['count'];
  76. }
  77. /**
  78. * a little bit of javascript to display
  79. */
  80. public function javascript() {
  81. }
  82. /**
  83. * Saves an element into the DB
  84. *
  85. * @param array $values
  86. * @return bool
  87. *
  88. */
  89. public function save($params, $show_query = false) {
  90. $params = $this->clean_parameters($params);
  91. if (in_array('created_at', $this->columns)) {
  92. $params['created_at'] = api_get_utc_datetime();
  93. }
  94. if (!empty($params)) {
  95. $id = Database::insert($this->table, $params, $show_query);
  96. if (is_numeric($id)){
  97. return $id;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * Updates the obj in the database. The $params['id'] must exist in order to update a record
  104. *
  105. * @param array $values
  106. *
  107. */
  108. public function update($params) {
  109. $params = $this->clean_parameters($params);
  110. //If the class has the updated_at field we update the date
  111. if (in_array('updated_at', $this->columns)) {
  112. $params['updated_at'] = api_get_utc_datetime();
  113. }
  114. //If the class has the created_at field then we remove it
  115. if (in_array('created_at', $this->columns)) {
  116. unset($params['created_at']);
  117. }
  118. if (!empty($params) && !empty($params['id'])) {
  119. $id = intval($params['id']);
  120. unset($params['id']); //To not overwrite the id
  121. if (is_numeric($id)) {
  122. $result = Database::update($this->table, $params, array('id = ?'=>$id));
  123. if ($result){
  124. return true;
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. }