model.lib.php 5.8 KB

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