model.lib.php 6.1 KB

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