/Application/Models/albums/albums.php

https://github.com/terasa/import_repo · PHP · 169 lines · 135 code · 8 blank · 26 comment · 13 complexity · 0014b01d4f626063ad9eec600427d1b8 MD5 · raw file

  1. <?php
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. class albumsModel extends Model {
  21. // album table supported fields
  22. public $supported_fields = array('id', 'title', 'description', 'address_id', 'owner_id', 'media_mime_type',
  23. 'media_type', 'thumbnail_url', 'app_id', 'created', 'modified', 'media_count', 'media_id');
  24. public function add_album($album) {
  25. global $db;
  26. foreach ($album as $key => $val) {
  27. if (in_array($key, $this->supported_fields)) {
  28. if (is_null($val)) {
  29. $adds[] = "`" . $db->addslashes($key) . "` = null";
  30. } else {
  31. $adds[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  32. }
  33. }
  34. }
  35. if (count($adds)) {
  36. $query = "insert into albums set " . implode(', ', $adds);
  37. $db->query($query);
  38. echo $db->insert_id();
  39. }
  40. }
  41. public function get_album($album_id) {
  42. global $db;
  43. $album_id = $db->addslashes($album_id);
  44. $query = "
  45. select
  46. albums.id,
  47. albums.title,
  48. albums.description,
  49. albums.address_id,
  50. albums.owner_id,
  51. albums.media_mime_type,
  52. albums.media_type,
  53. albums.thumbnail_url,
  54. albums.app_id,
  55. albums.created,
  56. albums.modified,
  57. albums.media_count,
  58. albums.media_id,
  59. concat(persons.first_name, ' ', persons.last_name) as name
  60. from
  61. albums, persons
  62. where
  63. albums.id = '$album_id' and
  64. persons.id = albums.owner_id
  65. limit 1";
  66. $res = $db->query($query);
  67. $ret = $db->fetch_array($res, MYSQLI_ASSOC);
  68. return $ret;
  69. }
  70. public function get_albums($owner_id, $start = false, $count = false) {
  71. global $db;
  72. $owner_id = $db->addslashes($owner_id);
  73. $start = $db->addslashes($start);
  74. $count = $db->addslashes($count);
  75. if (! $start) $start = '0';
  76. if (! $count) $count = 10;
  77. $where = "albums.owner_id = '$owner_id'";
  78. $limit = "$start, $count";
  79. $query = "
  80. select
  81. SQL_CALC_FOUND_ROWS
  82. albums.id,
  83. albums.title,
  84. albums.description,
  85. albums.address_id,
  86. albums.owner_id,
  87. albums.media_mime_type,
  88. albums.media_type,
  89. albums.thumbnail_url,
  90. albums.app_id,
  91. albums.created,
  92. albums.modified,
  93. albums.media_count,
  94. albums.media_id,
  95. concat(persons.first_name, ' ', persons.last_name) as name
  96. from
  97. albums, persons
  98. where
  99. $where and
  100. persons.id = albums.owner_id
  101. order by
  102. albums.id desc
  103. limit
  104. $limit";
  105. $res = $db->query($query);
  106. $cres = $db->query('select FOUND_ROWS();');
  107. $ret = array();
  108. while ($album = $db->fetch_array($res, MYSQLI_ASSOC)) {
  109. $ret[] = $album;
  110. }
  111. $rows = $db->fetch_array($cres, MYSQLI_NUM);
  112. $ret['found_rows'] = $rows[0];
  113. return $ret;
  114. }
  115. public function update_album($album_id, $album) {
  116. global $db;
  117. $album_id = $db->addslashes($album_id);
  118. foreach ($album as $key => $val) {
  119. if (in_array($key, $this->supported_fields)) {
  120. if (is_null($val)) {
  121. $updates[] = "`" . $db->addslashes($key) . "` = null";
  122. } else {
  123. $updates[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  124. }
  125. }
  126. }
  127. if (count($updates)) {
  128. $query = "update albums set " . implode(', ', $updates) . " where id = '$album_id'";
  129. $db->query($query);
  130. return $album_id;
  131. }
  132. }
  133. /*
  134. * update media table use literal word, so do not need to escape update code.
  135. * for example update albums set media_count = media_count + 1;
  136. */
  137. public function literal_update_album($album_id, $album) {
  138. global $db;
  139. $album_id = $db->addslashes($album_id);
  140. foreach ($album as $key => $val) {
  141. if (in_array($key, $this->supported_fields)) {
  142. $updates[] = "`" . $db->addslashes($key) . "` = $val";
  143. }
  144. }
  145. if (count($updates)) {
  146. $query = "update albums set " . implode(', ', $updates) . " where id = '$album_id'";
  147. $db->query($query);
  148. return $album_id;
  149. }
  150. }
  151. /*
  152. * delete album record.
  153. */
  154. public function delete_album($owner_id, $album_id) {
  155. global $db;
  156. $owner_id = $db->addslashes($owner_id);
  157. $album_id = $db->addslashes($album_id);
  158. $query = "delete from albums where owner_id = '$owner_id' and id = '$album_id'";
  159. $db->query($query);
  160. }
  161. }