model.lib.php 3.9 KB

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