model.lib.php 6.2 KB

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