model.lib.php 5.4 KB

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