model.lib.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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
  6. * database see examples in: career.lib.php and promotion.lib.php
  7. * Include/require it in your code to use its features.
  8. *
  9. * @package chamilo.library
  10. */
  11. class Model
  12. {
  13. public $table;
  14. public $columns;
  15. public $required;
  16. public $is_course_model = false;
  17. /**
  18. * Constructor
  19. */
  20. public function __construct()
  21. {
  22. }
  23. /**
  24. * Useful finder - experimental akelos like only use in notification.lib.php send function
  25. */
  26. public function find($type, $options = null)
  27. {
  28. switch ($type) {
  29. case 'all':
  30. return self::get_all($options);
  31. break;
  32. default:
  33. if (is_numeric($type)) {
  34. return self::get($type);
  35. }
  36. break;
  37. }
  38. }
  39. /**
  40. * Deletes an item
  41. * @param int $id
  42. *
  43. * @return bool
  44. */
  45. public function delete($id)
  46. {
  47. if (empty($id) or $id != strval(intval($id))) {
  48. return false;
  49. }
  50. $params = array('id = ?' => $id);
  51. if ($this->is_course_model) {
  52. $course_id = api_get_course_int_id();
  53. $params = array('id = ? AND c_id = ?' => array($id, $course_id));
  54. }
  55. // Database table definition
  56. $result = Database::delete($this->table, $params);
  57. if ($result != 1) {
  58. return false;
  59. }
  60. return true;
  61. }
  62. /**
  63. * @param array $params
  64. *
  65. * @return array
  66. */
  67. private function clean_parameters($params)
  68. {
  69. $clean_params = array();
  70. if (!empty($params)) {
  71. foreach ($params as $key=>$value) {
  72. if (in_array($key, $this->columns)) {
  73. $clean_params[$key] = $value;
  74. }
  75. }
  76. }
  77. return $clean_params;
  78. }
  79. /**
  80. * Displays the title + grid
  81. */
  82. public function display()
  83. {
  84. }
  85. /**
  86. * Gets an element
  87. * @param int $id
  88. *
  89. * @return array|mixed
  90. */
  91. public function get($id)
  92. {
  93. if (empty($id)) {
  94. return array();
  95. }
  96. $params = array('id = ?' => intval($id));
  97. if ($this->is_course_model) {
  98. $course_id = api_get_course_int_id();
  99. $params = array('id = ? AND c_id = ?' => array($id, $course_id));
  100. }
  101. $result = Database::select(
  102. '*',
  103. $this->table,
  104. array('where' => $params),
  105. 'first'
  106. );
  107. return $result;
  108. }
  109. /**
  110. * @param array $options
  111. *
  112. * @return array
  113. */
  114. public function get_all($options = null)
  115. {
  116. return Database::select('*', $this->table, $options);
  117. }
  118. /**
  119. * @param array $options
  120. *
  121. * @return array
  122. */
  123. public function getDataToExport($options = array())
  124. {
  125. return Database::select('name, description', $this->table, $options);
  126. }
  127. /**
  128. * Get the count of elements
  129. */
  130. public function get_count()
  131. {
  132. $row = Database::select(
  133. 'count(*) as count',
  134. $this->table,
  135. array('where' => array('parent_id = ?' => '0')),
  136. 'first'
  137. );
  138. return $row['count'];
  139. }
  140. /**
  141. * a little bit of javascript to display
  142. */
  143. public function javascript()
  144. {
  145. }
  146. /**
  147. * Saves an element into the DB
  148. * @param array $params
  149. * @param bool $show_query Whether to show the query in logs or not (passed to Database::insert())
  150. * @return bool
  151. *
  152. */
  153. public function save($params, $show_query = false)
  154. {
  155. $params = $this->clean_parameters($params);
  156. if ($this->is_course_model) {
  157. if (!isset($params['c_id']) || empty($params['c_id'])) {
  158. $params['c_id'] = api_get_course_int_id();
  159. }
  160. }
  161. if (!empty($this->required)) {
  162. $require_ok = true;
  163. $key_params = array_keys($params);
  164. foreach ($this->required as $field) {
  165. if (!in_array($field, $key_params)) {
  166. $require_ok = false;
  167. }
  168. }
  169. if (!$require_ok) {
  170. return false;
  171. }
  172. }
  173. if (in_array('created_at', $this->columns)) {
  174. $params['created_at'] = api_get_utc_datetime();
  175. }
  176. if (!empty($params)) {
  177. $id = Database::insert($this->table, $params, $show_query);
  178. if (is_numeric($id)) {
  179. return $id;
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * Updates the obj in the database. The $params['id'] must exist in order to update a record
  186. * @param array $params
  187. *
  188. * @return bool
  189. *
  190. */
  191. public function update($params)
  192. {
  193. $params = $this->clean_parameters($params);
  194. if ($this->is_course_model) {
  195. if (!isset($params['c_id']) || empty($params['c_id'])) {
  196. $params['c_id'] = api_get_course_int_id();
  197. }
  198. }
  199. //If the class has the updated_at field we update the date
  200. if (in_array('updated_at', $this->columns)) {
  201. $params['updated_at'] = api_get_utc_datetime();
  202. }
  203. //If the class has the created_at field then we remove it
  204. if (in_array('created_at', $this->columns)) {
  205. unset($params['created_at']);
  206. }
  207. if (!empty($params) && !empty($params['id'])) {
  208. $id = intval($params['id']);
  209. unset($params['id']); //To not overwrite the id
  210. if (is_numeric($id)) {
  211. $result = Database::update(
  212. $this->table,
  213. $params,
  214. array('id = ?' => $id)
  215. );
  216. if ($result) {
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. }