model.lib.php 6.2 KB

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