PageRenderTime 40ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/Application/Models/medias/medias.php

https://github.com/terasa/import_repo
PHP | 244 lines | 198 code | 12 blank | 34 comment | 15 complexity | 0c0cf6da2a25e5a22d0b083508f1ebbb 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 mediasModel extends Model {
  21. // media_items table supproted fields;
  22. public $supported_fields = array('id','activity_id','album_id','owner_id','mime_type','file_size',
  23. 'duration','created','last_updated','language','address_id','num_comments','num_views',
  24. 'num_votes','rating','start_time','title','description','tagged_people','tags',
  25. 'thumbnail_url','type','url','app_id');
  26. public function add_media($media) {
  27. global $db;
  28. foreach ($media as $key => $val) {
  29. if (in_array($key, $this->supported_fields)) {
  30. if (is_null($val)) {
  31. $adds[] = "`" . $db->addslashes($key) . "` = null";
  32. } else {
  33. $adds[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  34. }
  35. }
  36. }
  37. if (count($adds)) {
  38. $query = "insert into media_items set " . implode(', ', $adds);
  39. $db->query($query);
  40. return $db->insert_id();
  41. }
  42. }
  43. public function get_media($media_id) {
  44. global $db;
  45. $media_id = $db->addslashes($media_id);
  46. $query = "
  47. select
  48. media_items.id,
  49. media_items.activity_id,
  50. media_items.album_id,
  51. media_items.owner_id,
  52. media_items.mime_type,
  53. media_items.file_size,
  54. media_items.duration,
  55. media_items.created,
  56. media_items.last_updated,
  57. media_items.language,
  58. media_items.address_id,
  59. media_items.num_comments,
  60. media_items.num_views,
  61. media_items.num_votes,
  62. media_items.rating,
  63. media_items.start_time,
  64. media_items.title,
  65. media_items.description,
  66. media_items.tagged_people,
  67. media_items.tags,
  68. media_items.thumbnail_url,
  69. media_items.type,
  70. media_items.url,
  71. media_items.app_id
  72. from
  73. media_items
  74. where
  75. media_items.id = '$media_id'";
  76. $res = $db->query($query);
  77. $ret = $db->fetch_array($res, MYSQLI_ASSOC);
  78. return $ret;
  79. }
  80. public function get_medias($owner_id, $album_id, $start = false, $count = false) {
  81. global $db;
  82. $owner_id = $db->addslashes($owner_id);
  83. $album_id = $db->addslashes($album_id);
  84. $start = $db->addslashes($start);
  85. $count = $db->addslashes($count);
  86. if (! $start) $start = '0';
  87. if (! $count) $count = 20;
  88. $where = is_numeric($album_id) ? "media_items.album_id = '$album_id'" : "media_items.owner_id = '$owner_id'";
  89. $order = "media_items.id asc";
  90. $limit = "$start, $count";
  91. global $db;
  92. $query = "
  93. select
  94. SQL_CALC_FOUND_ROWS
  95. media_items.id,
  96. media_items.activity_id,
  97. media_items.album_id,
  98. media_items.owner_id,
  99. media_items.mime_type,
  100. media_items.file_size,
  101. media_items.duration,
  102. media_items.created,
  103. media_items.last_updated,
  104. media_items.language,
  105. media_items.address_id,
  106. media_items.num_comments,
  107. media_items.num_views,
  108. media_items.num_votes,
  109. media_items.rating,
  110. media_items.start_time,
  111. media_items.title,
  112. media_items.description,
  113. media_items.tagged_people,
  114. media_items.tags,
  115. media_items.thumbnail_url,
  116. media_items.type,
  117. media_items.url,
  118. media_items.app_id
  119. from
  120. media_items
  121. where
  122. $where and
  123. media_items.type = 'IMAGE'
  124. order by
  125. $order
  126. limit
  127. $limit";
  128. $res = $db->query($query);
  129. $cres = $db->query('SELECT FOUND_ROWS();');
  130. $ret = array();
  131. while ($media = $db->fetch_array($res, MYSQLI_ASSOC)) {
  132. $ret[] = $media;
  133. }
  134. $rows = $db->fetch_array($cres, MYSQLI_NUM);
  135. $ret['found_rows'] = $rows[0];
  136. return $ret;
  137. }
  138. public function update_media($media_id, $media) {
  139. global $db;
  140. $media_id = $db->addslashes($media_id);
  141. foreach ($media as $key => $val) {
  142. if (in_array($key, $this->supported_fields)) {
  143. if (is_null($val)) {
  144. $updates[] = "`" . $db->addslashes($key) . "` = null";
  145. } else {
  146. $updates[] = "`" . $db->addslashes($key) . "` = '" . $db->addslashes($val) . "'";
  147. }
  148. }
  149. }
  150. if (count($updates)) {
  151. $query = "update media_items set " . implode(', ', $updates) . " where id = '$media_id'";
  152. $db->query($query);
  153. return $media_id;
  154. }
  155. }
  156. /*
  157. * update media table using literal word, so do not need to escape update code.
  158. * for example update media_items set num_media = num_media + 1;
  159. */
  160. public function literal_update_media($media_id, $media) {
  161. global $db;
  162. $media_id = $db->addslashes($media_id);
  163. foreach ($media as $key => $val) {
  164. if (in_array($key, $this->supported_fields)) {
  165. $updates[] = "`" . $db->addslashes($key) . "` = $val";
  166. }
  167. }
  168. if (count($updates)) {
  169. $query = "update media_items set " . implode(', ', $updates) . " where id = '$media_id'";
  170. $db->query($query);
  171. return $media_id;
  172. }
  173. }
  174. public function delete_media($owner_id, $media_id) {
  175. global $db;
  176. $query = "delete from media_items where owner_id = '" . $db->addslashes($owner_id) .
  177. "' and id = '" . $db->addslashes($media_id) ."'";
  178. $db->query($query);
  179. }
  180. /**
  181. * get media in album before $media_id ;
  182. */
  183. public function get_media_previous($album_id, $media_id) {
  184. global $db;
  185. $query = "select * from media_items where album_id = '" . $db->addslashes($album_id) .
  186. "' and id > '" . $db->addslashes($media_id) . "' order by id desc limit 1";
  187. $res = $db->query($query);
  188. $ret = $db->fetch_array($res, MYSQLI_ASSOC);
  189. return $ret;
  190. }
  191. /**
  192. * get media in album next to $media_id ;
  193. */
  194. public function get_media_next($album_id, $media_id) {
  195. global $db;
  196. $query = "select * from media_items where album_id = '" . $db->addslashes($album_id) .
  197. "' and id < '" . $db->addslashes($media_id) . "' order by id desc limit 1";
  198. $res = $db->query($query);
  199. $ret = $db->fetch_array($res, MYSQLI_ASSOC);
  200. return $ret;
  201. }
  202. /**
  203. * get media in album, results are previous one, current one and next one.
  204. */
  205. public function get_media_has_order($album_id, $media_id) {
  206. global $db;
  207. $ret = array();
  208. // get previous one, whose id is less than current one
  209. $query = "select SQL_CALC_FOUND_ROWS * from media_items where album_id = '" . $db->addslashes($album_id) .
  210. "' and id < '" . $db->addslashes($media_id) . "' order by id desc limit 1";
  211. $res = $db->query($query);
  212. $row = $db->fetch_array($res, MYSQLI_ASSOC);
  213. if (!empty($row)) {
  214. $ret[] = $row;
  215. }
  216. $cres = $db->query('SELECT FOUND_ROWS();');
  217. $rows = $db->fetch_array($cres, MYSQLI_NUM);
  218. $found_rows = $rows[0];
  219. // get current and next one
  220. $query = "select SQL_CALC_FOUND_ROWS * from media_items where album_id = '" . $db->addslashes($album_id) .
  221. "' and id >= '" . $db->addslashes($media_id) . "' order by id asc limit 2";
  222. $res = $db->query($query);
  223. $cres = $db->query('SELECT FOUND_ROWS();');
  224. while ($row = $db->fetch_array($res, MYSQLI_ASSOC)) {
  225. $ret[] = $row;
  226. }
  227. $rows = $db->fetch_array($cres, MYSQLI_NUM);
  228. $ret['found_rows'] = $rows[0] + $found_rows;
  229. $ret['item_order'] = 1 + $found_rows;
  230. return $ret;
  231. }
  232. }