model.lib.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Model
  5. * 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
  6. * Include/require it in your code to use its features.
  7. * @package chamilo.library
  8. */
  9. class Model
  10. {
  11. public $table;
  12. public $columns;
  13. public $required;
  14. public $is_course_model =false;
  15. public function __construct()
  16. {
  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. {
  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. * Deletes an item
  34. */
  35. public function delete($id)
  36. {
  37. if (empty($id) or $id != strval(intval($id))) {
  38. return false;
  39. }
  40. $params = array('id = ?' => $id);
  41. if ($this->is_course_model) {
  42. $course_id = api_get_course_int_id();
  43. $params = array('id = ? AND c_id = ?' => array($id, $course_id));
  44. }
  45. // Database table definition
  46. $result = Database::delete($this->table, $params);
  47. if ($result != 1) {
  48. return false;
  49. }
  50. return true;
  51. }
  52. /**
  53. * @param array $params
  54. * @return array
  55. */
  56. private function clean_parameters($params)
  57. {
  58. $clean_params = array();
  59. if (!empty($params)) {
  60. foreach ($params as $key=>$value) {
  61. if (in_array($key, $this->columns)) {
  62. $clean_params[$key] = $value;
  63. }
  64. }
  65. }
  66. return $clean_params;
  67. }
  68. /**
  69. * Displays the title + grid
  70. */
  71. public function display() {
  72. }
  73. /**
  74. * Gets an element
  75. */
  76. public function get($id)
  77. {
  78. if (empty($id)) {
  79. return array();
  80. }
  81. $params = array('id = ?'=>intval($id));
  82. if ($this->is_course_model) {
  83. $course_id = api_get_course_int_id();
  84. $params = array('id = ? AND c_id = ?' => array($id, $course_id));
  85. }
  86. $result = Database::select('*',$this->table, array('where' => $params),'first');
  87. return $result;
  88. }
  89. /**
  90. * @param array $options
  91. * @return array
  92. */
  93. public function get_all($options = null)
  94. {
  95. return Database::select('*', $this->table, $options);
  96. }
  97. /**
  98. * @param array $options
  99. *
  100. * @return array
  101. */
  102. public function getDataToExport($options = array())
  103. {
  104. return Database::select('name, description', $this->table, $options);
  105. }
  106. /**
  107. * Get the count of elements
  108. */
  109. public function get_count()
  110. {
  111. $row = Database::select('count(*) as count', $this->table, array('where' => array('parent_id = ?' => '0')),'first');
  112. return $row['count'];
  113. }
  114. /**
  115. * a little bit of javascript to display
  116. */
  117. public function javascript()
  118. {
  119. }
  120. /**
  121. * Saves an element into the DB
  122. *
  123. * @param array $values
  124. * @return bool
  125. *
  126. */
  127. public function save($params, $show_query = false)
  128. {
  129. $params = $this->clean_parameters($params);
  130. if ($this->is_course_model) {
  131. if (!isset($params['c_id']) || empty($params['c_id'])) {
  132. $params['c_id'] = api_get_course_int_id();
  133. }
  134. }
  135. if (!empty($this->required)) {
  136. $require_ok = true;
  137. $kay_params = array_keys($params);
  138. foreach ($this->required as $field) {
  139. if (!in_array($field, $kay_params)) {
  140. $require_ok = false;
  141. }
  142. }
  143. if (!$require_ok) {
  144. return false;
  145. }
  146. }
  147. if (in_array('created_at', $this->columns)) {
  148. $params['created_at'] = api_get_utc_datetime();
  149. }
  150. if (!empty($params)) {
  151. $id = Database::insert($this->table, $params, $show_query);
  152. if (is_numeric($id)) {
  153. return $id;
  154. }
  155. }
  156. return false;
  157. }
  158. /**
  159. * Updates the obj in the database. The $params['id'] must exist in order to update a record
  160. * @param array $values
  161. *
  162. * @return bool
  163. *
  164. */
  165. public function update($params)
  166. {
  167. $params = $this->clean_parameters($params);
  168. if ($this->is_course_model) {
  169. if (!isset($params['c_id']) || empty($params['c_id'])) {
  170. $params['c_id'] = api_get_course_int_id();
  171. }
  172. }
  173. //If the class has the updated_at field we update the date
  174. if (in_array('updated_at', $this->columns)) {
  175. $params['updated_at'] = api_get_utc_datetime();
  176. }
  177. //If the class has the created_at field then we remove it
  178. if (in_array('created_at', $this->columns)) {
  179. unset($params['created_at']);
  180. }
  181. if (!empty($params) && !empty($params['id'])) {
  182. $id = intval($params['id']);
  183. unset($params['id']); //To not overwrite the id
  184. if (is_numeric($id)) {
  185. $result = Database::update($this->table, $params, array('id = ?'=>$id));
  186. if ($result) {
  187. return true;
  188. }
  189. }
  190. }
  191. return false;
  192. }
  193. }