PageRenderTime 79ms CodeModel.GetById 22ms RepoModel.GetById 5ms app.codeStats 0ms

/lib/class/shoutbox.class.php

https://gitlab.com/x33n/ampache
PHP | 288 lines | 164 code | 52 blank | 72 comment | 16 complexity | 557aeb14587509e15443a3c04ecb102d MD5 | raw file
  1. <?php
  2. /* vim:set softtabstop=4 shiftwidth=4 expandtab: */
  3. /**
  4. *
  5. * LICENSE: GNU General Public License, version 2 (GPLv2)
  6. * Copyright 2001 - 2015 Ampache.org
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License v2
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. class Shoutbox
  23. {
  24. public $id;
  25. public $object_type;
  26. public $object_id;
  27. public $user;
  28. public $sticky;
  29. public $text;
  30. public $data;
  31. public $date;
  32. public $f_link;
  33. /**
  34. * Constructor
  35. * This pulls the shoutbox information from the database and returns
  36. * a constructed object, uses user_shout table
  37. */
  38. public function __construct($shout_id)
  39. {
  40. // Load the data from the database
  41. $this->_get_info($shout_id);
  42. return true;
  43. } // Constructor
  44. /**
  45. * _get_info
  46. * does the db call, reads from the user_shout table
  47. */
  48. private function _get_info($shout_id)
  49. {
  50. $sql = "SELECT * FROM `user_shout` WHERE `id` = ?";
  51. $db_results = Dba::read($sql, array($shout_id));
  52. $data = Dba::fetch_assoc($db_results);
  53. foreach ($data as $key=>$value) {
  54. $this->$key = $value;
  55. }
  56. return true;
  57. } // _get_info
  58. /**
  59. * gc
  60. *
  61. * Cleans out orphaned shoutbox items
  62. */
  63. public static function gc()
  64. {
  65. foreach (array('song', 'album', 'artist') as $object_type) {
  66. Dba::write("DELETE FROM `user_shout` USING `user_shout` LEFT JOIN `$object_type` ON `$object_type`.`id` = `user_shout`.`object_id` WHERE `$object_type`.`id` IS NULL AND `user_shout`.`object_type` = '$object_type'");
  67. }
  68. }
  69. /**
  70. * get_top
  71. * This returns the top user_shouts, shoutbox objects are always shown regardless and count against the total
  72. * number of objects shown
  73. */
  74. public static function get_top($limit)
  75. {
  76. $shouts = self::get_sticky();
  77. // If we've already got too many stop here
  78. if (count($shouts) > $limit) {
  79. $shouts = array_slice($shouts,0,$limit);
  80. return $shouts;
  81. }
  82. // Only get as many as we need
  83. $limit = intval($limit) - count($shouts);
  84. $sql = "SELECT * FROM `user_shout` WHERE `sticky`='0' ORDER BY `date` DESC LIMIT $limit";
  85. $db_results = Dba::read($sql);
  86. while ($row = Dba::fetch_assoc($db_results)) {
  87. $shouts[] = $row['id'];
  88. }
  89. return $shouts;
  90. } // get_top
  91. public static function get_shouts_since($time)
  92. {
  93. $sql = "SELECT * FROM `user_shout` WHERE `date` > ? ORDER BY `date` DESC";
  94. $db_results = Dba::read($sql, array($time));
  95. $shouts = array();
  96. while ($row = Dba::fetch_assoc($db_results)) {
  97. $shouts[] = $row['id'];
  98. }
  99. return $shouts;
  100. }
  101. /**
  102. * get_sticky
  103. * This returns all current sticky shoutbox items
  104. */
  105. public static function get_sticky()
  106. {
  107. $sql = "SELECT * FROM `user_shout` WHERE `sticky`='1' ORDER BY `date` DESC";
  108. $db_results = Dba::read($sql);
  109. $results = array();
  110. while ($row = Dba::fetch_assoc($db_results)) {
  111. $results[] = $row['id'];
  112. }
  113. return $results;
  114. } // get_sticky
  115. /**
  116. * get_object
  117. * This takes a type and an ID and returns a created object
  118. */
  119. public static function get_object($type,$object_id)
  120. {
  121. if (!Core::is_library_item($type))
  122. return false;
  123. $object = new $type($object_id);
  124. return $object;
  125. } // get_object
  126. /**
  127. * get_image
  128. * This returns an image tag if the type of object we're currently rolling with
  129. * has an image associated with it
  130. */
  131. public function get_image()
  132. {
  133. $image_string = '';
  134. if (Art::has_db($this->object_id, $this->object_type)) {
  135. $image_string = "<img class=\"shoutboximage\" height=\"75\" width=\"75\" src=\"" . AmpConfig::get('web_path') . "/image.php?object_id=" . $this->object_id . "&object_type=" . $this->object_type . "&thumb=1\" />";
  136. }
  137. return $image_string;
  138. } // get_image
  139. /**
  140. * create
  141. * This takes a key'd array of data as input and inserts a new shoutbox entry, it returns the auto_inc id
  142. */
  143. public static function create(array $data)
  144. {
  145. $sticky = isset($data['sticky']) ? 1 : 0;
  146. $sql = "INSERT INTO `user_shout` (`user`,`date`,`text`,`sticky`,`object_id`,`object_type`, `data`) " .
  147. "VALUES (? , ?, ?, ?, ?, ?, ?)";
  148. Dba::write($sql, array($GLOBALS['user']->id, time(), strip_tags($data['comment']), $sticky, $data['object_id'], $data['object_type'], $data['data']));
  149. $insert_id = Dba::insert_id();
  150. return $insert_id;
  151. } // create
  152. /**
  153. * update
  154. * This takes a key'd array of data as input and updates a shoutbox entry
  155. */
  156. public function update(array $data)
  157. {
  158. $sql = "UPDATE `user_shout` SET `text` = ?, `sticky` = ? WHERE `id` = ?";
  159. Dba::write($sql, array($data['comment'], make_bool($data['sticky']), $this->id));
  160. return $this->id;
  161. } // create
  162. /**
  163. * format
  164. * this function takes the object and reformats some values
  165. */
  166. public function format()
  167. {
  168. $this->sticky = ($this->sticky == "0") ? 'No' : 'Yes';
  169. $this->date = date("m\/d\/Y - H:i", $this->date);
  170. return true;
  171. } //format
  172. /**
  173. * delete
  174. * this function deletes a specific shoutbox entry
  175. */
  176. public function delete($shout_id)
  177. {
  178. // Delete the shoutbox post
  179. $shout_id = Dba::escape($shout_id);
  180. $sql = "DELETE FROM `user_shout` WHERE `id`='$shout_id'";
  181. Dba::write($sql);
  182. } // delete
  183. public function get_display($details = true, $jsbuttons = false)
  184. {
  185. $object = Shoutbox::get_object($this->object_type, $this->object_id);
  186. $object->format();
  187. $user = new User($this->user);
  188. $user->format();
  189. $img = $this->get_image();
  190. $html = "<div class='shoutbox-item'>";
  191. $html .= "<div class='shoutbox-data'>";
  192. if ($details && $img) {
  193. $html .= "<div class='shoutbox-img'>" . $img . "</div>";
  194. }
  195. $html .= "<div class='shoutbox-info'>";
  196. if ($details) {
  197. $html .= "<div class='shoutbox-object'>" . ($object->f_name_link ?: $object->f_link) . "</div>";
  198. $html .= "<div class='shoutbox-date'>".date("Y/m/d H:i:s", $this->date) . "</div>";
  199. }
  200. $html .= "<div class='shoutbox-text'>" . preg_replace('/(\r\n|\n|\r)/', '<br />', $this->text) . "</div>";
  201. $html .= "</div>";
  202. $html .= "</div>";
  203. $html .= "<div class='shoutbox-footer'>";
  204. if ($details) {
  205. $html .= "<div class='shoutbox-actions'>";
  206. if ($jsbuttons) {
  207. $html .= Ajax::button('?page=stream&action=directplay&playtype=' . $this->object_type .'&' . $this->object_type . '_id=' . $this->object_id,'play', T_('Play'),'play_' . $this->object_type . '_' . $this->object_id);
  208. $html .= Ajax::button('?action=basket&type=' . $this->object_type .'&id=' . $this->object_id,'add', T_('Add'),'add_' . $this->object_type . '_' . $this->object_id);
  209. }
  210. if (Access::check('interface','25')) {
  211. $html .= "<a href=\"" . AmpConfig::get('web_path') . "/shout.php?action=show_add_shout&type=" . $this->object_type . "&id=" . $this->object_id . "\">" . UI::get_icon('comment', T_('Post Shout')) . "</a>";
  212. }
  213. $html .= "</div>";
  214. }
  215. $html .= "<div class='shoutbox-user'>by ";
  216. if ($details) {
  217. $html .= $user->f_link;
  218. } else {
  219. $html .= $user->username;
  220. }
  221. $html .= "</div>";
  222. $html .= "</div>";
  223. $html .= "</div>";
  224. return $html;
  225. }
  226. public static function get_shouts($object_type, $object_id)
  227. {
  228. $sql = "SELECT `id` FROM `user_shout` WHERE `object_type` = ? AND `object_id` = ? ORDER BY `sticky`, `date` DESC";
  229. $db_results = Dba::read($sql, array($object_type, $object_id));
  230. $results = array();
  231. while ($row = Dba::fetch_assoc($db_results)) {
  232. $results[] = $row['id'];
  233. }
  234. return $results;
  235. }
  236. } // Shoutbox class